|
这个函数是配置和启动SPI的DMA传输吗?:HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) 是从用STM32CubeMX生成的,给果没有传输。参考了STM32Cube_FW_F3_V1.1.0\Projects\STM32F303RE-Nucleo\Examples\SPI\SPI_FullDuplex_ComDMA这 个例子。以下是我的程序。 /** ******************************************************************************! B2 n4 G" B7 s. ?, ]! l3 d9 k * File Name : main.c' d R# d5 a% \' M% r * Date : 04/06/2015 09:34:39 * Description : Main program body+ t" k( u& t- m4 ]; B3 H ******************************************************************************. N. t5 b( N, f1 G y *& X: g* ^/ U9 w& [# [ * COPYRIGHT(c) 2015 STMicroelectronics" V6 y9 u) f" u- \4 } * * Redistribution and use in source and binary forms, with or without modification,4 I7 \* Z. Q+ U4 H( s * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice,9 @+ W% ~0 h' B& A# w * this list of conditions and the following disclaimer.! y q/ r8 D ]% p * 2. Redistributions in binary form must reproduce the above copyright notice,4 H9 }6 a5 Z4 f+ T* B F$ M * this list of conditions and the following disclaimer in the documentation( o z Y8 Y+ l- }/ c) i * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors# A2 N |7 c" v; `) i* f * may be used to endorse or promote products derived from this software- y0 F' o' F% o* I9 M * without specific prior written permission.4 H; K3 T1 Q: ~ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+ j& t q- \! o" x2 b) H ? * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE) `+ \. S' P. f; b/ c * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE7 p% v y+ l6 j2 l/ }; x * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR; z) H! }+ G* w) i* x" r: T8 K * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE& a# a( R ]# ~8 p * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.% G) t' @. [& l) Y9 m; R1 o' ` * ****************************************************************************** */* z8 n' z {1 m /* Includes ------------------------------------------------------------------*/: X8 h% y( `: c #include "stm32f3xx_hal.h"; c9 k3 a% O3 n2 c+ P) h- P' k /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ SPI_HandleTypeDef hspi2;7 r: L. @4 M+ c: ? T6 c S DMA_HandleTypeDef hdma_spi2_rx; DMA_HandleTypeDef hdma_spi2_tx;, C/ p& k% g% k( ]& o9 _ /* USER CODE BEGIN PV */ /* Buffer used for transmission */ #define BUFFERSIZE 16& Q! |" a( O3 j3 q- D uint8_t aTxBuffer[] = "****SPI - Two Boards communication based on DMA **** SPI Message ******** SPI Message ******** SPI Message ****";* p. |5 x4 `" C( I6 w' `! E5 } /* Buffer used for reception */ uint8_t aRxBuffer[BUFFERSIZE]; /* USER CODE END PV */, ^* ?! b( L) G. f1 C( q% J /* Private function prototypes -----------------------------------------------*/# o T* b5 w& F$ C& w; n8 ? void SystemClock_Config(void); static void MX_GPIO_Init(void);# q. ?7 X7 p7 w Q1 g q( b static void MX_DMA_Init(void);7 m& I* E% i! y& n* Z: P static void MX_SPI2_Init(void); /* USER CODE BEGIN PFP */1 i* s# \ j, m* y! J" x. A9 [ static void Error_Handler(void);$ y3 X7 v1 ^, y. t2 X /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */8 }, M K" W) ?. v /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init();$ A: p% ?& B4 \" }/ j" ? /* Configure the system clock */( f& e8 Y% ~8 C K+ f! ] SystemClock_Config(); /* Initialize all configured peripherals */. J* r$ o* [: u, K MX_GPIO_Init();' x" [7 i7 o3 x1 d1 H MX_DMA_Init(); MX_SPI2_Init();# U2 V. o( Y b& X j) P /* USER CODE BEGIN 2 */ if(HAL_SPI_TransmitReceive_DMA(&hspi2, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE) != HAL_OK)/ B7 ]6 [9 @6 e0 w {2 q$ Y+ r& d9 g% T7 o' Z. n /* Transfer error in transmission process */ Error_Handler();: H' y( G+ e& i* q } /* USER CODE END 2 */: |" @4 I. Z* r9 U. q& W /* Infinite loop */ /* USER CODE BEGIN WHILE */" H$ L0 U: ?+ }! G# K while (1)5 |4 q+ K' M; O' H { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET); ' G6 f. O( X& D( x ^4 f0 O$ h HAL_Delay(100); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET); HAL_Delay(100);" j) ~! s. d5 B# g }' s6 ~: i2 k, m0 | /* USER CODE END 3 */ } /** System Clock Configuration */ void SystemClock_Config(void)- {1 X( @& m7 W: }" ^4 K {5 [. O; j3 J& u4 K* @! y RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON;4 N) E, a: V! `! J _ RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; HAL_RCC_OscConfig(&RCC_OscInitStruct); c) P9 F" d( X1 {, F RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;& K! M) m6 I+ c: [ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;$ w4 n3 l3 b5 P- H& F, n RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); __SYSCFG_CLK_ENABLE();/ [( ]# D0 s; g5 g) \ t } /* SPI2 init function */$ w; D! M( N& i! k8 q void MX_SPI2_Init(void) { hspi2.Instance = SPI2;$ V) i. s& r$ S& r, w. z* [( ^ hspi2.Init.Mode = SPI_MODE_MASTER;! d" g3 b. M- l. x9 e e) D hspi2.Init.Direction = SPI_DIRECTION_2LINES;! O$ ~: e" \7 `1 I: \* O- T6 f hspi2.Init.DataSize = SPI_DATASIZE_16BIT; hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;8 G* e+ b& D& L6 ?$ ~4 a- e hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLED;; s7 |8 V5 r ]2 v hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;9 x, F \+ B) U; [/ }. w hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLED;- \5 d( Z0 s" }" W$ p HAL_SPI_Init(&hspi2); }& C6 p5 W- m4 _- t& x2 R /** ; r9 F7 O' x) z$ l, ^1 ^ * Enable DMA controller clock */ void MX_DMA_Init(void) { /* DMA controller clock enable */5 g- \. P! j: J9 U; {$ r __DMA1_CLK_ENABLE();0 |2 V" ~$ Y* z5 k) S /* DMA interrupt init */ HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);: k/ D+ R2 c3 a9 b; \; E2 y HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn); HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); }. x) O, F, q7 k+ A& Q /** Configure pins as 3 H! q/ M( O' M4 y2 T * Analog * Input 3 G1 a! Z* h7 v1 b * Output5 `4 {4 e+ \/ {: z6 @ * EVENT_OUT& c3 c8 H) T* t * EXTI6 p! \4 O6 X. L */ void MX_GPIO_Init(void)8 {) j$ e- J* n; q {! Z" Z& W5 H$ l# {9 ^6 W; R GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */# ^6 ^- f& ^1 A) w0 q( ` __GPIOF_CLK_ENABLE();) b4 b. W2 G& u; v: c __GPIOB_CLK_ENABLE(); /*Configure GPIO pin : PB13 */" }. h" d0 ~* K. M4 ^& Y GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;% u h' Y( R" l2 T6 }& d8 z) R8 a GPIO_InitStruct.Pull = GPIO_NOPULL;8 x6 f) @$ C7 w* |) b GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /** * @brief This function is executed in case of error occurrence. * @param None* ~* M' V5 D$ Q * @retval None */ x; [& T, ?* q9 u+ C static void Error_Handler(void) {# G& P: Q1 O, W, G9 y$ W while(1) {, s G+ A" H) T: G /* Toogle LED for error */6 q \, c$ s$ n" u1 }( w' u HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET); - T" h7 d# A$ n6 ] HAL_Delay(100);+ _/ S8 ^6 w; c1 k HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET); HAL_Delay(1000);! M1 ^" \- [8 ~. d, g3 i+ y1 R }; ]) D; [+ b2 d& O } /* USER CODE END 4 */! h, l; e H% ~2 R, I0 j1 l( _ #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number! c2 _5 k& M7 U" M1 U * @retval None */ void assert_failed(uint8_t* file, uint32_t line)- _9 }' G) ^- o2 N2 n { /* USER CODE BEGIN 6 */! o) o7 z( E( v /* User can add his own implementation to report the file name and line number,+ s+ ?- W$ l* L ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */$ s, P; h7 C. i7 j }& ?* Y0 c# R! ^ #endif! G, T/ K# x7 `( `' p6 } /**+ M* C; v a( ?; r' v7 m" R * @} */ m3 Y8 \8 V% B& v( | /**9 M* t9 {3 C* R% m5 q' V3 V+ C * @} */ 2 g" o6 H0 ]% U* N" W1 } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/0 Q z2 y7 q7 M |
微信公众号
手机版
__IO uint32_t wTransferState = SPI_DMA_TRANSFER_WAIT;- S3 i4 X% N) n1 t: k, h! Z
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)% {$ w/ a& z* {/ l$ f! i) S
{) t/ h- I7 u7 h" ^; {& M+ ]
wTransferState = SPI_DMA_TRANSFER_COMPLETE;
}+ C2 w# A! l9 U5 `- q/ G, u! w
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)- a+ a! Z; l- H- _* k, \
{
wTransferState = SPI_DMA_TRANSFER_ERROR;/ I' Z' i! Y# [/ O! Q" ~
}
请问楼主,我使用DMA接收SPI,在ReceiveOnly模式,3个字节接收,接收到的数据有点奇怪,从我读取到数据来看,像是错位了。
大神,请问那个参数hspi是什么意思?
请问楼主的SPI发送和接收函数怎么写的?使用的就只有HAL_SPI_TransmitReceive_DMA函数吗?全双工怎么打开发送和接收呢?