|
这个函数是配置和启动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这 个例子。以下是我的程序。$ f0 M, v+ P" ~" C /*** \( k( [: [6 p$ `. X ****************************************************************************** * File Name : main.c9 l8 V1 r9 J7 I * Date : 04/06/2015 09:34:39! j# `4 A1 G$ ^) } * Description : Main program body; ~' [, j2 w5 s% u* @; V% m5 D. X0 r ****************************************************************************** * * COPYRIGHT(c) 2015 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice,6 V; F8 M0 l9 a b/ H; t6 C# q * this list of conditions and the following disclaimer.1 W, N+ Y+ p B * 2. Redistributions in binary form must reproduce the above copyright notice,4 I9 g! A% I; K7 U& x * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software7 P- a6 L- Y) V4 e1 ` * without specific prior written permission. ** S3 x; a4 R7 ]" T9 w X# Z * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE4 T; B6 M/ f7 s7 O4 Z* s4 s1 y * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL0 \) k1 i7 x" O8 d; Q * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER6 a9 |# l! Y# z9 P * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,6 v# w4 t4 c5 m7 G3 e * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE3 j6 G5 [- X% {' O * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.$ A$ |& `8 @9 u6 t0 N * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/2 ]' h' j( q' n! ]) W #include "stm32f3xx_hal.h" /* USER CODE BEGIN Includes */ /* USER CODE END Includes */' D) ~& m7 b4 S) m" [ /* Private variables ---------------------------------------------------------*/ SPI_HandleTypeDef hspi2;. @( ^' E8 b9 H5 r* j. Q: X DMA_HandleTypeDef hdma_spi2_rx; DMA_HandleTypeDef hdma_spi2_tx;7 s9 ?# ]/ P3 Y% l! | /* USER CODE BEGIN PV */9 x6 ]1 i& F2 h2 I C /* Buffer used for transmission */ #define BUFFERSIZE 165 K! O }/ A5 ]. T' M uint8_t aTxBuffer[] = "****SPI - Two Boards communication based on DMA **** SPI Message ******** SPI Message ******** SPI Message ****";- ~5 g+ ^" [' y5 ~; ]4 `. c /* Buffer used for reception */ uint8_t aRxBuffer[BUFFERSIZE]; /* USER CODE END PV */0 P( Q! `( ]# I2 k* X8 | /* Private function prototypes -----------------------------------------------*/$ b' Y$ t, j+ ]) C void SystemClock_Config(void);4 c1 C) t( b9 }' [$ u5 _' D static void MX_GPIO_Init(void); static void MX_DMA_Init(void); static void MX_SPI2_Init(void); /* USER CODE BEGIN PFP */( L, Y) F) L/ z; t: ^ static void Error_Handler(void); /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ int main(void)( f8 |7 F- q& I6 y4 I5 A2 i( o {$ X+ _# ]# J7 g+ f /* USER CODE BEGIN 1 */- h" `+ ?2 K2 v |! r /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/* l2 [" [- @3 t# C# J. Z* `4 p /* Reset of all peripherals, Initializes the Flash interface and the Systick. */+ K# P2 q8 T( }6 w( V! A4 M1 v HAL_Init(); _! y7 I- `! w! E r0 M/ w /* Configure the system clock */% _4 Y2 \. P: L SystemClock_Config();7 S: K6 r0 `5 r6 v! W0 |/ q1 e, K /* Initialize all configured peripherals */ MX_GPIO_Init();( ?9 ^& A0 q# D* p. r& Y8 V0 } MX_DMA_Init();: w: }* a5 R, l9 @) g! h MX_SPI2_Init(); /* USER CODE BEGIN 2 */4 l9 Q0 s1 Y- `" N if(HAL_SPI_TransmitReceive_DMA(&hspi2, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE) != HAL_OK); g6 ~1 a& @! b2 O( g { /* Transfer error in transmission process */: {' I+ h3 D" k0 R- w- f% N Error_Handler(); } /* USER CODE END 2 */ /* Infinite loop */( W2 q9 z0 ~0 S: v /* USER CODE BEGIN WHILE */ while (1)6 P0 }9 M# z: W V { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */- `& ] } N# r# k' ^ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET); 0 L+ t8 ]) U4 m6 ^9 g4 B2 X HAL_Delay(100);& \% A" h# m3 R: f! [ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET); - y* j$ }# G+ y# [ HAL_Delay(100);/ B6 j& D9 v, ^( w$ n ` }9 b: W- @# E3 \4 Z6 I2 G1 @ /* USER CODE END 3 */ }7 d4 q W2 ]! Q" H8 K1 y; I /** System Clock Configuration% ]4 e! i; H1 N3 Z- D9 { */* f4 h% R+ j/ D, m( r0 z; } void SystemClock_Config(void)% [% v, }6 X$ z, k8 v {, M4 P1 p8 `* v. C/ m/ Y; r+ q RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON;, F+ B7 j9 o# F0 ~! E, U RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; HAL_RCC_OscConfig(&RCC_OscInitStruct);5 P' z* V, [5 Z+ U2 z RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;% t0 b# `$ Z N RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); __SYSCFG_CLK_ENABLE(); } /* SPI2 init function */! k: Q) |5 o0 l7 L; v void MX_SPI2_Init(void)0 _; Q. m+ q! d1 t {9 G2 H; @. {6 m7 k! d5 h C q hspi2.Instance = SPI2;/ H, P2 H* c' p+ b" P% S hspi2.Init.Mode = SPI_MODE_MASTER;) R5 I9 C2 Y8 p3 M7 a hspi2.Init.Direction = SPI_DIRECTION_2LINES; hspi2.Init.DataSize = SPI_DATASIZE_16BIT;2 i3 g# E6 y: r4 l# w, v) t hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;# P8 {. N K: F% i, ` hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;* p% |4 v P6 c$ q/ }4 H) m) f hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLED; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;2 t0 p7 z( I* S+ V hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLED; HAL_SPI_Init(&hspi2);" Z4 k5 d1 I& x8 S$ b } B+ b/ }( S' r; S /** + Q _' |2 I- D3 b * Enable DMA controller clock */) V# m! X7 R2 U. A void MX_DMA_Init(void) {( V H. }# ~$ U4 h /* DMA controller clock enable */; @# Q% N! [/ d. e. h __DMA1_CLK_ENABLE();$ _, B$ t. M) U& J% z /* DMA interrupt init */ HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);( s; x- t" J. V7 D' `" U- D3 o- @ HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);. L+ T! d6 \& A0 Q$ g" v3 Z HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);+ W3 a% Q% x5 M& }* W$ A9 B- C9 v HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); } /** Configure pins as . {( K* \% x& t * Analog + V% J/ o8 h+ f( g * Input ' D& B6 D- }2 z+ B4 W# w * Output * EVENT_OUT, |7 c, b5 Z( z P; v8 T q7 | * EXTI */. M: i+ A o/ R; g! u void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __GPIOF_CLK_ENABLE();" r T' y8 ~5 K0 M1 X __GPIOB_CLK_ENABLE(); /*Configure GPIO pin : PB13 */; B, y' ^8 A4 ~6 @. G$ G# Q# d GPIO_InitStruct.Pin = GPIO_PIN_13;. [+ S: l& |- B GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;; F& m( o/ E" ?8 _3 u GPIO_InitStruct.Pull = GPIO_NOPULL;! L; {' [8 G4 K GPIO_InitStruct.Speed = GPIO_SPEED_LOW;; z2 y2 D0 F- x, \2 [/ W, P3 j HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);8 H) {5 [; R2 K! w3 i, N& z } /* USER CODE BEGIN 4 */ /**! Q$ s# h4 [5 N% p$ C2 _" V * @brief This function is executed in case of error occurrence. * @param None * @retval None */1 h% z4 l8 }8 ~ a! b( @1 h5 I static void Error_Handler(void) { while(1) { /* Toogle LED for error */' I6 G9 y2 t) l1 A HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET); ! |9 v5 b6 p+ d$ x0 C5 b3 t HAL_Delay(100);) ^( R4 ]: \/ M3 }( S HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET); ! o( ~- N; ~7 K5 f HAL_Delay(1000); }& V2 y" g- Z* K. X } /* USER CODE END 4 */ #ifdef USE_FULL_ASSERT9 E/ f% M" }% l6 }" M& I% c /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred.5 I$ u4 \! {) N * @param file: pointer to the source file name3 }. z0 i/ ~0 H' x3 e * @param line: assert_param error line source number * @retval None */" X8 P2 ~" n. I6 N5 n+ H, w3 [ void assert_failed(uint8_t* file, uint32_t line) {* N: {7 H6 l) ?8 C/ V: g4 _5 W /* USER CODE BEGIN 6 */ |7 _6 M; a! e8 @3 E$ C /* User can add his own implementation to report the file name and line number,& @3 R3 a' g) v- T ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */3 Y# h5 n, M$ o" ~+ W7 S f /* USER CODE END 6 */% m i, v6 k3 M4 t }. O& B" Z" S0 K #endif0 J3 e: w% c. @+ ^/ }4 e9 m' T /** * @}, Y4 m8 D. O" p' x0 a */ 5 k7 a6 H; h: O1 t9 ?6 ^ /** * @} */ 7 {* P" {: N8 P/ ]/ x5 W& k# L /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
微信公众号
手机版
__IO uint32_t wTransferState = SPI_DMA_TRANSFER_WAIT;
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{- S" o n u' A) _9 ~' N3 V! R
wTransferState = SPI_DMA_TRANSFER_COMPLETE;" V% Q2 K* N4 `
}
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)5 F3 z- V. x# v% n: h
{
wTransferState = SPI_DMA_TRANSFER_ERROR;
}
请问楼主,我使用DMA接收SPI,在ReceiveOnly模式,3个字节接收,接收到的数据有点奇怪,从我读取到数据来看,像是错位了。
大神,请问那个参数hspi是什么意思?
请问楼主的SPI发送和接收函数怎么写的?使用的就只有HAL_SPI_TransmitReceive_DMA函数吗?全双工怎么打开发送和接收呢?