今天在调试时,遇到个棘手的问题,分享解决方法给大家, 我使用HAL库,一个让人抓狂的烂库啊,想骂娘,一直骂都不解气的烂库。" E9 V& [) Z3 w* Z% z9 c! m" ~ 串口+DMA,官网407的DMA例程移植过来的,不需要过多说明,上代码即可+ w( ^# w7 i) s' L' T void HAL_UART_MspInit(UART_HandleTypeDef* huart) {6 q# M7 _! O0 |' w0 ~; @ GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART3)7 _9 N* x0 {8 w/ D7 S( ]+ h( M {. H9 R( a- o4 ? r& J% T /* USER CODE BEGIN USART3_MspInit 0 */; M& R4 [* x% {2 Z0 D# B# y5 H * ^6 B8 F3 H8 ]2 G5 q: i% Q /* USER CODE END USART3_MspInit 0 */4 V3 B4 a9 b% N' l /* Peripheral clock enable */ __HAL_RCC_USART3_CLK_ENABLE();9 M+ R' }3 ?4 y% l4 L$ q 9 u I3 o: ?: S$ U4 Y /**USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */. w$ [, _6 J! ]; u) J3 ? GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;$ B+ S0 ^" Q+ {. e* W GPIO_InitStruct.Pull = GPIO_PULLUP;6 `, ? i; V6 \) z- ~7 T1 ? GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART3;+ C3 t3 `+ e( u HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);' G) F! _ ]* g. [9 M% p5 t * |& |; E x( o+ u$ Q /*##-3- Configure the DMA streams ##########################################*/ /* Configure the DMA handler for Transmission process */ hdma_usart3_tx.Instance = USARTx_TX_DMA_STREAM;$ g- ~9 r8 H/ q# W% \- I6 T O( ]# ] }( a1 }0 t) ] hdma_usart3_tx.Init.Channel = USARTx_TX_DMA_CHANNEL;. x& B% X) w$ d2 S( |; S" ~/ J6 s hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; f) i: E; Z6 ?& F. P3 Q hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;# w4 e" U! {; c! s* m. o P hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart3_tx.Init.Mode = DMA_NORMAL;% ?6 Y6 h, }& j, e hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW; p, V% ~& d0 V9 t. h0 | N hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_usart3_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_usart3_tx.Init.MemBurst = DMA_MBURST_INC4;0 B+ f, \, i; R# J3 r hdma_usart3_tx.Init.PeriphBurst = DMA_PBURST_INC4;' [. G" R0 p% c) w' i& f" N) b K HAL_DMA_Init(&hdma_usart3_tx); + a0 D+ z* x% g/ L5 R5 f- W( } /* Associate the initialized DMA handle to the UART handle */. Y" j1 B W. r" o9 M5 Z. \& N& n3 P __HAL_LINKDMA(huart, hdmatx, hdma_usart3_tx);* A# n: p: I) r0 d. g, J5 {: ] /* Associate the initialized DMA handle to the the UART handle */$ T3 Y: H0 B. H+ {2 f$ i, ~# C __HAL_LINKDMA(huart, hdmarx, hdma_usart3_rx); : B# t8 u8 h& d6 D5 b, [( Y /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */ HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 0, 1); HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);4 g7 a: y% f3 }) S $ L" s. X8 [ X# p /* USER CODE BEGIN USART3_MspInit 1 */3 k/ J- h% G2 b" U) u HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);2 t( m9 o6 z8 S ^7 ]- g HAL_NVIC_EnableIRQ(USART3_IRQn);$ M& X O: p9 B" Y6 ? , P! q" S4 j7 c" Z /* USER CODE END USART3_MspInit 1 */ } }0 ]! ], { b7 e1 z5 o /* USART3 init function */ static void MX_USART3_UART_Init(void)3 T+ A6 E; ^; Q% f0 |6 j { ; B' K% r, E: d " K% g4 b4 i8 T. g. p* n. Y0 X huart3.Instance = USART3; huart3.Init.BaudRate = 115200*2; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1;0 T7 T; A7 k# Z5 n, H# V huart3.Init.Parity = UART_PARITY_NONE; huart3.Init.Mode = UART_MODE_TX_RX;0 A& N, ^* W8 t huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;- R, w1 A3 E( d; b# I2 P huart3.Init.OverSampling = UART_OVERSAMPLING_16;( g6 O) C+ s8 {; [4 `" i if (HAL_UART_Init(&huart3) != HAL_OK) {, X7 d6 e' A, I2 P5 c. h _Error_Handler(__FILE__, __LINE__); } ! m) @! d# Q; X- F 6 ~* K) e1 c5 i1 D }/ ?$ s7 L3 X+ S/ R% J3 s 以上是初始化代码# m4 _* u$ T: Y 使用的DMA发送,中断接收, 串口是和4G模块通讯,具体哪个厂家的就不说了,只能说很low。/ F9 o) e& q5 W* S* B 在配置模块时,出现了如图所示, 再次上电没有错误了,就接上f4的串口,开始调试,顿时来个下马威啊,一直在反复进入串口中断,单步跟踪,先是 Framing error,后来一直是 Noise detected flag,Overrun error这2个错误,相当于进入死循环了,急得我直骂娘啊,百度一下,基本没有收获,能知道是硬件问题,就上了示波器,果然是波形出问题了,f4的串口是使用串口助手调试了N遍的,肯定没问题,只能把4G模块的波特率降低一个档次,因为之前使用115200(出厂默认值)一直没出问题,果然是没问题,又再次把波特率升到230400,直到在调试软件上没有任何错误,反复测试都OK,才放心大胆的使用,果然,一切正常了,权当发个水贴,给大家分享一下经验了。- x! D) ^( u4 v, X. B! Y) h+ g8 D % h8 T7 h v8 w9 m! M7 y 2 D% Q. @1 f3 `3 p3 r$ U1 x! ]2 T |
谢谢分享啊 |
客气了 |
这个库各种出错,唉,不能提,怎么办,只有在错误中前行,收获也很大。 |
是啊,库里问题太多 |