drv_usart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. #include "drv_usart.h"
  2. #include "rkfifo.h"
  3. #include "stm32f4xx.h"
  4. #include "string.h"
  5. #include "board.h"
  6. static inline int _stm32_uart_rcc_enable(USART_TypeDef *uartx)
  7. {
  8. int ret = 0;
  9. switch ((uint32_t)uartx)
  10. {
  11. case (uint32_t)USART1:
  12. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  13. break;
  14. case (uint32_t)USART2:
  15. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  16. break;
  17. case (uint32_t)USART3:
  18. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  19. break;
  20. case (uint32_t)UART4:
  21. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
  22. break;
  23. case (uint32_t)UART5:
  24. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
  25. break;
  26. case (uint32_t)USART6:
  27. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
  28. break;
  29. default:
  30. ret = -1;
  31. break;
  32. }
  33. return ret;
  34. }
  35. static inline int _stm32_dma_rcc_enable(DMA_Stream_TypeDef *tx_dma)
  36. {
  37. int ret = 0;
  38. switch ((uint32_t)tx_dma)
  39. {
  40. case (uint32_t)DMA1_Stream0:
  41. case (uint32_t)DMA1_Stream1:
  42. case (uint32_t)DMA1_Stream2:
  43. case (uint32_t)DMA1_Stream3:
  44. case (uint32_t)DMA1_Stream4:
  45. case (uint32_t)DMA1_Stream5:
  46. case (uint32_t)DMA1_Stream6:
  47. case (uint32_t)DMA1_Stream7:
  48. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  49. break;
  50. case (uint32_t)DMA2_Stream0:
  51. case (uint32_t)DMA2_Stream1:
  52. case (uint32_t)DMA2_Stream2:
  53. case (uint32_t)DMA2_Stream3:
  54. case (uint32_t)DMA2_Stream4:
  55. case (uint32_t)DMA2_Stream5:
  56. case (uint32_t)DMA2_Stream6:
  57. case (uint32_t)DMA2_Stream7:
  58. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  59. break;
  60. default:
  61. ret = -1;
  62. break;
  63. }
  64. return ret;
  65. }
  66. static inline int _stm32_gpio_rcc_enable(GPIO_TypeDef *port)
  67. {
  68. int ret = 0;
  69. switch ((uint32_t)port)
  70. {
  71. case (uint32_t)GPIOA:
  72. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  73. break;
  74. case (uint32_t)GPIOB:
  75. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  76. break;
  77. case (uint32_t)GPIOC:
  78. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  79. break;
  80. case (uint32_t)GPIOD:
  81. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  82. break;
  83. case (uint32_t)GPIOE:
  84. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  85. break;
  86. case (uint32_t)GPIOF:
  87. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  88. break;
  89. case (uint32_t)GPIOG:
  90. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  91. break;
  92. case (uint32_t)GPIOH:
  93. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
  94. break;
  95. case (uint32_t)GPIOI:
  96. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
  97. break;
  98. case (uint32_t)GPIOJ:
  99. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOJ, ENABLE);
  100. break;
  101. case (uint32_t)GPIOK:
  102. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOK, ENABLE);
  103. break;
  104. default:
  105. ret = -1;
  106. break;
  107. }
  108. return ret;
  109. }
  110. static inline uint16_t _stm32_get_pinaf(uint16_t pin)
  111. {
  112. uint16_t ret = 0xff;
  113. switch (pin)
  114. {
  115. case GPIO_Pin_0:
  116. ret = GPIO_PinSource0;
  117. break;
  118. case GPIO_Pin_1:
  119. ret = GPIO_PinSource1;
  120. break;
  121. case GPIO_Pin_2:
  122. ret = GPIO_PinSource2;
  123. break;
  124. case GPIO_Pin_3:
  125. ret = GPIO_PinSource3;
  126. break;
  127. case GPIO_Pin_4:
  128. ret = GPIO_PinSource4;
  129. break;
  130. case GPIO_Pin_5:
  131. ret = GPIO_PinSource5;
  132. break;
  133. case GPIO_Pin_6:
  134. ret = GPIO_PinSource6;
  135. break;
  136. case GPIO_Pin_7:
  137. ret = GPIO_PinSource7;
  138. break;
  139. case GPIO_Pin_8:
  140. ret = GPIO_PinSource8;
  141. break;
  142. case GPIO_Pin_9:
  143. ret = GPIO_PinSource9;
  144. break;
  145. case GPIO_Pin_10:
  146. ret = GPIO_PinSource10;
  147. break;
  148. case GPIO_Pin_11:
  149. ret = GPIO_PinSource11;
  150. break;
  151. case GPIO_Pin_12:
  152. ret = GPIO_PinSource12;
  153. break;
  154. case GPIO_Pin_13:
  155. ret = GPIO_PinSource13;
  156. break;
  157. case GPIO_Pin_14:
  158. ret = GPIO_PinSource14;
  159. break;
  160. case GPIO_Pin_15:
  161. ret = GPIO_PinSource15;
  162. break;
  163. default:
  164. break;
  165. }
  166. return ret;
  167. }
  168. static inline uint8_t _stm32_get_uartaf(USART_TypeDef *uartx)
  169. {
  170. uint8_t ret = 0;
  171. switch ((uint32_t)uartx)
  172. {
  173. case (uint32_t)USART1:
  174. ret = GPIO_AF_USART1;
  175. break;
  176. case (uint32_t)USART2:
  177. ret = GPIO_AF_USART2;
  178. break;
  179. case (uint32_t)USART3:
  180. ret = GPIO_AF_USART3;
  181. break;
  182. case (uint32_t)UART4:
  183. ret = GPIO_AF_UART4;
  184. break;
  185. case (uint32_t)UART5:
  186. ret = GPIO_AF_UART5;
  187. break;
  188. case (uint32_t)USART6:
  189. ret = GPIO_AF_USART6;
  190. break;
  191. default:
  192. ret = -1;
  193. break;
  194. }
  195. return ret;
  196. }
  197. static int _uart_config(struct stm32_uart_config *uart, uint32_t bps)
  198. {
  199. int ret = 0;
  200. _stm32_uart_rcc_enable(uart->uartx);
  201. _stm32_gpio_rcc_enable(uart->_tx_port);
  202. _stm32_gpio_rcc_enable(uart->_rx_port);
  203. USART_DeInit(uart->uartx);
  204. GPIO_InitTypeDef GPIO_InitStructure;
  205. GPIO_StructInit(&GPIO_InitStructure);
  206. GPIO_InitStructure.GPIO_Pin = uart->_tx_pin;
  207. GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
  208. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  209. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  210. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  211. GPIO_Init(uart->_tx_port, &GPIO_InitStructure);
  212. GPIO_InitStructure.GPIO_Pin = uart->_rx_pin;
  213. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  214. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  215. GPIO_Init(uart->_rx_port, &GPIO_InitStructure);
  216. GPIO_PinAFConfig(uart->_rx_port, _stm32_get_pinaf(uart->_rx_pin), _stm32_get_uartaf(uart->uartx));
  217. GPIO_PinAFConfig(uart->_tx_port, _stm32_get_pinaf(uart->_tx_pin), _stm32_get_uartaf(uart->uartx));
  218. USART_InitTypeDef USART_InitStructure;
  219. USART_StructInit(&USART_InitStructure);
  220. USART_InitStructure.USART_BaudRate = bps;
  221. USART_Init(uart->uartx, &USART_InitStructure);
  222. if (uart->_rx_dma == NULL)
  223. {
  224. /* 采用中断式接收 */
  225. USART_ITConfig(uart->uartx, USART_IT_RXNE, ENABLE);
  226. }
  227. else
  228. {
  229. /* 采用 dma 式接收*/
  230. USART_ITConfig(uart->uartx, USART_IT_IDLE, ENABLE);
  231. }
  232. USART_Cmd(uart->uartx, ENABLE);
  233. return ret;
  234. }
  235. static int _uart_dma_config(struct stm32_uart_config *uart)
  236. {
  237. int ret = 0;
  238. DMA_InitTypeDef dma_conf;
  239. /* tx dma config */
  240. if (uart->_tx_dma && uart->_dma_tx_buff)
  241. {
  242. _stm32_dma_rcc_enable(uart->_tx_dma);
  243. DMA_DeInit(uart->_tx_dma);
  244. while (DMA_GetCmdStatus(uart->_tx_dma) != DISABLE)
  245. {
  246. };
  247. DMA_StructInit(&dma_conf);
  248. dma_conf.DMA_Channel = uart->_tx_dma_channel;
  249. dma_conf.DMA_PeripheralBaseAddr = (u32)(&(uart->uartx->DR));
  250. dma_conf.DMA_Memory0BaseAddr = (u32)uart->_dma_tx_buff;
  251. dma_conf.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  252. dma_conf.DMA_BufferSize = (uint32_t)uart->_dma_tx_buff;
  253. dma_conf.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  254. dma_conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
  255. dma_conf.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  256. dma_conf.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
  257. dma_conf.DMA_Mode = DMA_Mode_Normal;
  258. dma_conf.DMA_Priority = DMA_Priority_Medium;
  259. dma_conf.DMA_FIFOMode = DMA_FIFOMode_Disable;
  260. dma_conf.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  261. dma_conf.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  262. dma_conf.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  263. DMA_Init(uart->_tx_dma, &dma_conf);
  264. DMA_ITConfig(uart->_tx_dma, DMA_IT_TC, ENABLE);
  265. DMA_ITConfig(uart->_tx_dma, DMA_IT_TE, ENABLE);
  266. DMA_Cmd(uart->_tx_dma, DISABLE);
  267. // 串口采用 dma 发送
  268. USART_DMACmd(uart->uartx, USART_DMAReq_Tx, ENABLE);
  269. //等待关闭使能完成
  270. while (DMA_GetCmdStatus(uart->_tx_dma) != DISABLE)
  271. {
  272. };
  273. }
  274. /* rx dma config */
  275. if (uart->_rx_dma && uart->_dma_rx_buff)
  276. {
  277. // 配置串口的 dma 接收
  278. _stm32_dma_rcc_enable(uart->_rx_dma);
  279. DMA_StructInit(&dma_conf);
  280. DMA_DeInit(uart->_rx_dma);
  281. while (DMA_GetCmdStatus(uart->_rx_dma) != DISABLE)
  282. {
  283. };
  284. dma_conf.DMA_Channel = uart->_rx_dma_channel;
  285. dma_conf.DMA_PeripheralBaseAddr = (u32)(&uart->uartx->DR);
  286. dma_conf.DMA_Memory0BaseAddr = (u32)uart->_dma_rx_buff;
  287. dma_conf.DMA_DIR = DMA_DIR_PeripheralToMemory;
  288. dma_conf.DMA_BufferSize = uart->_dma_rx_buff_size;
  289. dma_conf.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  290. dma_conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
  291. dma_conf.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  292. dma_conf.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
  293. dma_conf.DMA_Mode = DMA_Mode_Normal;
  294. dma_conf.DMA_Priority = DMA_Priority_High;
  295. dma_conf.DMA_FIFOMode = DMA_FIFOMode_Disable;
  296. dma_conf.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  297. dma_conf.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  298. dma_conf.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  299. DMA_Init(uart->_rx_dma, &dma_conf);
  300. DMA_ITConfig(uart->_rx_dma, DMA_IT_TC, ENABLE);
  301. DMA_ITConfig(uart->_rx_dma, DMA_IT_TE, ENABLE);
  302. DMA_Cmd(uart->_rx_dma, ENABLE);
  303. /* 串口采用 dma 接收 */
  304. USART_DMACmd(uart->uartx, USART_DMAReq_Rx, ENABLE);
  305. while (DMA_GetCmdStatus(uart->_rx_dma) != ENABLE)
  306. {
  307. };
  308. }
  309. return ret;
  310. }
  311. void _uart_nvic_config(struct stm32_uart_config *uart)
  312. {
  313. NVIC_InitTypeDef NVIC_InitStructure;
  314. NVIC_InitStructure.NVIC_IRQChannel = uart->_uart_irq_channel;
  315. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  316. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  317. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  318. NVIC_Init(&NVIC_InitStructure);
  319. if (uart->_rx_dma && uart->_dma_rx_buff)
  320. {
  321. NVIC_InitStructure.NVIC_IRQChannel = uart->_rx_dma_irq_channel;
  322. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  323. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  324. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  325. NVIC_Init(&NVIC_InitStructure);
  326. }
  327. if (uart->_tx_dma && uart->_dma_tx_buff)
  328. {
  329. NVIC_InitStructure.NVIC_IRQChannel = uart->_tx_dma_irq_channel;
  330. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  331. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  332. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  333. NVIC_Init(&NVIC_InitStructure);
  334. }
  335. }
  336. int uart_init(struct stm32_uart_config *uart, uint32_t bps)
  337. {
  338. int ret = 0;
  339. rkfifo_init(&uart->_rx_fifo, uart->_rx_fifo_buff, uart->_rx_fifo_buff_size, 1);
  340. rkfifo_init(&uart->_tx_fifo, uart->_tx_fifo_buff, uart->_tx_fifo_buff_size, 1);
  341. _uart_config(uart, bps);
  342. _uart_dma_config(uart);
  343. _uart_nvic_config(uart);
  344. return ret;
  345. }
  346. /**
  347. * @brief 串口发送数据
  348. *
  349. * @param uart 串口对象
  350. * @param pdata 发送的数据指针
  351. * @param len 发送的数据长度
  352. * @return uint32_t 实际发送的数据长度
  353. */
  354. static uint32_t uart_tx_data(struct stm32_uart_config *uart, const void *pdata, uint32_t len)
  355. {
  356. assert_param(uart != NULL);
  357. assert_param(pdata != NULL);
  358. uint32_t ret = 0;
  359. if (len > 0)
  360. {
  361. /* 将数据压入 tx FIFO */
  362. ret = rkfifo_in(&uart->_tx_fifo, pdata, len);
  363. if (uart->_tx_dma)
  364. {
  365. /* dma 形式发送数据 */
  366. if ((USART_GetFlagStatus(uart->uartx, USART_FLAG_TXE) == SET) &&
  367. (USART_GetFlagStatus(uart->uartx, USART_FLAG_TC) == SET))
  368. {
  369. // fifo 中是否还有数据, 如果还有数据, 则读取到 dma buff 中并进行发送
  370. uint32_t count = rkfifo_out(&uart->_tx_fifo,
  371. uart->_dma_tx_buff,
  372. uart->_dma_tx_buff_size);
  373. if (count > 0)
  374. {
  375. DMA_SetCurrDataCounter(uart->_tx_dma, count);
  376. DMA_Cmd(uart->_tx_dma, ENABLE);
  377. }
  378. }
  379. }
  380. else
  381. {
  382. /* 中断形式发送数据 */
  383. USART_ITConfig(uart->uartx, USART_IT_TXE, ENABLE);
  384. }
  385. }
  386. return ret;
  387. }
  388. /**
  389. * @brief 串口读取数据
  390. *
  391. * @param uart 串口对象
  392. * @param pdata 数据读取输出 buffer
  393. * @param len 数据读取长度
  394. * @return uint32_t 实际读出的长度
  395. */
  396. static uint32_t uart_rx_data(struct stm32_uart_config *uart, void *pdata, uint32_t len)
  397. {
  398. assert_param(uart != NULL);
  399. assert_param(pdata != NULL);
  400. uint32_t ret = rkfifo_out(&uart->_rx_fifo, pdata, len);
  401. return ret;
  402. }
  403. static void _rxne_isr_callback(struct stm32_uart *uart)
  404. {
  405. assert_param(uart != NULL);
  406. USART_TypeDef *uartx = uart->_config->uartx;
  407. rkfifo_t *rxfifo = &uart->_config->_rx_fifo;
  408. uint8_t data;
  409. data = USART_ReceiveData(uartx) & 0xFF;
  410. /* 将接收到的数据压入 fifo */
  411. rkfifo_in(rxfifo, &data, 1);
  412. USART_ClearITPendingBit(uartx, USART_IT_RXNE);
  413. }
  414. static void _txe_isr_callback(struct stm32_uart *uart)
  415. {
  416. assert_param(uart != NULL);
  417. USART_TypeDef *usartx = uart->_config->uartx;
  418. rkfifo_t *txfifo = &uart->_config->_tx_fifo;
  419. if (USART_GetITStatus(usartx, USART_IT_TXE) == SET)
  420. {
  421. uint8_t c;
  422. if (rkfifo_out(txfifo, &c, 1))
  423. {
  424. /* fifo 有数据, 继续发送 */
  425. USART_SendData(usartx, c);
  426. }
  427. else
  428. {
  429. /* fifo 无数据, 关闭 txe 中断, 打开 TC 中断 */
  430. USART_ITConfig(usartx, USART_IT_TXE, DISABLE);
  431. USART_ITConfig(usartx, USART_IT_TC, ENABLE);
  432. }
  433. USART_ClearITPendingBit(usartx, USART_IT_TXE);
  434. }
  435. }
  436. static void _tc_isr_callback(struct stm32_uart *uart)
  437. {
  438. assert_param(uart != NULL);
  439. USART_TypeDef *usartx = uart->_config->uartx;
  440. USART_ClearITPendingBit(usartx, USART_IT_TC);
  441. }
  442. static void _uart_isr_callback(struct stm32_uart *uart)
  443. {
  444. USART_TypeDef *uartx = uart->_config->uartx;
  445. DMA_Stream_TypeDef *rx_dma = uart->_config->_rx_dma;
  446. uint8_t *rx_dma_buff = uart->_config->_dma_rx_buff;
  447. uint32_t rx_dma_buff_size = uart->_config->_dma_rx_buff_size;
  448. rkfifo_t *rx_fifo = &uart->_config->_rx_fifo;
  449. if (USART_GetITStatus(uartx, USART_IT_RXNE) == SET)
  450. {
  451. _rxne_isr_callback(uart);
  452. }
  453. else if (USART_GetITStatus(uartx, USART_IT_TXE) == SET)
  454. {
  455. _txe_isr_callback(uart);
  456. }
  457. else if (USART_GetITStatus(uartx, USART_IT_TC) == SET)
  458. {
  459. _tc_isr_callback(uart);
  460. }
  461. else if (USART_GetITStatus(uartx, USART_IT_IDLE) == SET)
  462. {
  463. if (rx_dma)
  464. {
  465. DMA_Cmd(rx_dma, DISABLE);
  466. uint32_t dma_cnt = rx_dma_buff_size -
  467. DMA_GetCurrDataCounter(rx_dma);
  468. if (dma_cnt)
  469. {
  470. rkfifo_in(rx_fifo, &rx_dma_buff[0], dma_cnt);
  471. }
  472. //重新设置传输数据长度
  473. DMA_SetCurrDataCounter(rx_dma, uart->_config->_dma_rx_buff_size);
  474. //打开DMA
  475. DMA_Cmd(rx_dma, ENABLE);
  476. }
  477. uartx->SR;
  478. uartx->DR;
  479. }
  480. else
  481. {
  482. uartx->SR;
  483. uartx->DR;
  484. }
  485. }
  486. static void _uart_tx_dma_isr_callback(struct stm32_uart *uart)
  487. {
  488. DMA_Stream_TypeDef *tx_dma = uart->_config->_tx_dma;
  489. rkfifo_t *tx_fifo = &uart->_config->_tx_fifo;
  490. uint32_t tcif = uart->_config->_tx_dma_tcif;
  491. uint32_t teif = uart->_config->_tx_dma_teif;
  492. if (DMA_GetITStatus(tx_dma, tcif) == SET)
  493. {
  494. DMA_ClearITPendingBit(tx_dma, tcif);
  495. DMA_Cmd(tx_dma, DISABLE);
  496. // fifo 中是否还有数据, 如果还有数据, 则读取到 dma buff 中并进行发送
  497. uint8_t *dma_buff = uart->_config->_dma_tx_buff;
  498. uint32_t dma_buff_size = uart->_config->_dma_tx_buff_size;
  499. uint32_t count = rkfifo_out(tx_fifo, dma_buff, dma_buff_size);
  500. if (count > 0)
  501. {
  502. DMA_SetCurrDataCounter(tx_dma, count);
  503. DMA_Cmd(tx_dma, ENABLE);
  504. }
  505. else
  506. {
  507. // 数据发送完成,在多线程时可以抛出信号量
  508. }
  509. }
  510. else if (DMA_GetITStatus(tx_dma, teif) == SET)
  511. {
  512. DMA_ClearITPendingBit(tx_dma, teif);
  513. }
  514. }
  515. static void _uart_rx_dma_isr_callback(struct stm32_uart *uart)
  516. {
  517. DMA_Stream_TypeDef *rx_dma = uart->_config->_rx_dma;
  518. rkfifo_t *rx_fifo = &uart->_config->_rx_fifo;
  519. uint32_t tcif = uart->_config->_rx_dma_tcif;
  520. uint32_t teif = uart->_config->_rx_dma_teif;
  521. if (DMA_GetITStatus(rx_dma, tcif) == SET)
  522. {
  523. DMA_ClearITPendingBit(rx_dma, tcif);
  524. DMA_Cmd(rx_dma, DISABLE);
  525. uint8_t *dma_buff = uart->_config->_dma_rx_buff;
  526. uint32_t dma_buff_size = uart->_config->_dma_rx_buff_size;
  527. uint32_t dma_cnt = dma_buff_size - DMA_GetCurrDataCounter(rx_dma);
  528. if (dma_cnt)
  529. {
  530. rkfifo_in(rx_fifo, &dma_buff[0], dma_cnt);
  531. }
  532. //重新设置传输数据长度
  533. DMA_SetCurrDataCounter(rx_dma, dma_buff_size);
  534. //打开DMA
  535. DMA_Cmd(rx_dma, ENABLE);
  536. }
  537. else if (DMA_GetITStatus(rx_dma, teif) == SET)
  538. {
  539. DMA_ClearITPendingBit(rx_dma, teif);
  540. }
  541. }
  542. /**--------------------------- UART1 -----------------------------------------*/
  543. #ifdef DRV_USING_UART1
  544. static uint8_t u1_rx_fifo_buff[UART1_RX_FIFO_BUFFER_LEN] = {0};
  545. static uint8_t u1_tx_fifo_buff[UART1_TX_FIFO_BUFFER_LEN] = {0};
  546. #ifdef UART1_TX_USING_DMA
  547. static uint8_t usart1_dma_tx_buf[UART1_TX_DMA_BUFFER_LEN] = {0};
  548. #endif
  549. #ifdef UART1_RX_USING_DMA
  550. static uint8_t usart1_dma_rx_buf[UART1_RX_DMA_BUFFER_LEN] = {0};
  551. #endif
  552. struct stm32_uart_config _u1_config = {
  553. .uartx = USART1,
  554. ._tx_port = GPIOA,
  555. ._tx_pin = GPIO_Pin_9,
  556. ._rx_port = GPIOA,
  557. ._rx_pin = GPIO_Pin_10,
  558. ._uart_irq_channel = USART1_IRQn,
  559. ._tx_fifo_buff = u1_tx_fifo_buff,
  560. ._tx_fifo_buff_size = sizeof(u1_tx_fifo_buff),
  561. ._rx_fifo_buff = u1_rx_fifo_buff,
  562. ._rx_fifo_buff_size = sizeof(u1_rx_fifo_buff),
  563. #ifdef UART1_RX_USING_DMA
  564. ._rx_dma = DMA2_Stream2,
  565. ._rx_dma_tcif = DMA_IT_TCIF2,
  566. ._rx_dma_teif = DMA_IT_TEIF2,
  567. ._rx_dma_channel = DMA_Channel_4,
  568. ._dma_rx_buff = usart1_dma_rx_buf,
  569. ._dma_rx_buff_size = sizeof(usart1_dma_rx_buf),
  570. ._rx_dma_irq_channel = DMA2_Stream2_IRQn,
  571. #else
  572. ._rx_dma = NULL,
  573. #endif // USART1_RX_USING_DMA
  574. #ifdef UART1_TX_USING_DMA
  575. ._tx_dma = DMA2_Stream7,
  576. ._tx_dma_tcif = DMA_IT_TCIF7,
  577. ._tx_dma_teif = DMA_IT_TEIF7,
  578. ._tx_dma_channel = DMA_Channel_4,
  579. ._dma_tx_buff = usart1_dma_tx_buf,
  580. ._dma_tx_buff_size = sizeof(usart1_dma_tx_buf),
  581. ._tx_dma_irq_channel = DMA2_Stream7_IRQn,
  582. #else
  583. ._tx_dma = NULL,
  584. #endif // USART1_TX_USING_DMA
  585. };
  586. static uint32_t u1_write_data(const void *pdata, uint32_t len)
  587. {
  588. return uart_tx_data(&_u1_config, pdata, len);
  589. }
  590. static uint32_t u1_read_data(void *pdata, uint32_t len)
  591. {
  592. return uart_rx_data(&_u1_config, pdata, len);
  593. }
  594. static int u1_init(uint32_t bps)
  595. {
  596. return uart_init(&_u1_config, bps);
  597. }
  598. static struct stm32_uart_ops _u1_ops = {
  599. .init = u1_init,
  600. .read = u1_read_data,
  601. .write = u1_write_data};
  602. static struct stm32_uart uart1 = {
  603. ._config = &_u1_config,
  604. .ops = &_u1_ops};
  605. void USART1_IRQHandler(void)
  606. {
  607. _uart_isr_callback(&uart1);
  608. }
  609. #ifdef UART1_TX_USING_DMA
  610. void DMA2_Stream7_IRQHandler(void)
  611. {
  612. _uart_tx_dma_isr_callback(&uart1);
  613. }
  614. #endif
  615. #ifdef UART1_RX_USING_DMA
  616. void DMA2_Stream2_IRQHandler(void)
  617. {
  618. _uart_rx_dma_isr_callback(&uart1);
  619. }
  620. #endif
  621. #endif // DRV_USING_UART1
  622. /**--------------------------- UART2 -----------------------------------------*/
  623. #ifdef DRV_USING_UART2
  624. static uint8_t u2_rx_fifo_buff[UART2_RX_FIFO_BUFFER_LEN] = {0};
  625. static uint8_t u2_tx_fifo_buff[UART2_TX_FIFO_BUFFER_LEN] = {0};
  626. #ifdef UART2_TX_USING_DMA
  627. static uint8_t usart2_dma_tx_buf[UART2_TX_DMA_BUFFER_LEN] = {0};
  628. #endif
  629. #ifdef UART2_RX_USING_DMA
  630. static uint8_t usart2_dma_rx_buf[UART2_RX_DMA_BUFFER_LEN] = {0};
  631. #endif
  632. struct stm32_uart_config _u2_config = {
  633. .uartx = USART2,
  634. ._tx_port = GPIOA,
  635. ._tx_pin = GPIO_Pin_2,
  636. ._rx_port = GPIOA,
  637. ._rx_pin = GPIO_Pin_3,
  638. ._uart_irq_channel = USART2_IRQn,
  639. ._tx_fifo_buff = u2_tx_fifo_buff,
  640. ._tx_fifo_buff_size = sizeof(u2_tx_fifo_buff),
  641. ._rx_fifo_buff = u2_rx_fifo_buff,
  642. ._rx_fifo_buff_size = sizeof(u2_rx_fifo_buff),
  643. #ifdef UART2_RX_USING_DMA
  644. ._rx_dma = DMA1_Stream5,
  645. ._rx_dma_tcif = DMA_IT_TCIF5,
  646. ._rx_dma_teif = DMA_IT_TEIF5,
  647. ._rx_dma_channel = DMA_Channel_4,
  648. ._dma_rx_buff = usart2_dma_rx_buf,
  649. ._dma_rx_buff_size = sizeof(usart2_dma_rx_buf),
  650. ._rx_dma_irq_channel = DMA1_Stream5_IRQn,
  651. #else
  652. ._rx_dma = NULL,
  653. #endif // USART1_RX_USING_DMA
  654. #ifdef UART2_TX_USING_DMA
  655. ._tx_dma = DMA1_Stream6,
  656. ._tx_dma_tcif = DMA_IT_TCIF6,
  657. ._tx_dma_teif = DMA_IT_TEIF6,
  658. ._tx_dma_channel = DMA_Channel_4,
  659. ._dma_tx_buff = usart2_dma_tx_buf,
  660. ._dma_tx_buff_size = sizeof(usart2_dma_tx_buf),
  661. ._tx_dma_irq_channel = DMA1_Stream6_IRQn,
  662. #else
  663. ._tx_dma = NULL,
  664. #endif // USART2_TX_USING_DMA
  665. };
  666. static uint32_t u2_write_data(const void *pdata, uint32_t len)
  667. {
  668. return uart_tx_data(&_u2_config, pdata, len);
  669. }
  670. static uint32_t u2_read_data(void *pdata, uint32_t len)
  671. {
  672. return uart_rx_data(&_u2_config, pdata, len);
  673. }
  674. static int u2_init(uint32_t bps)
  675. {
  676. return uart_init(&_u2_config, bps);
  677. }
  678. static struct stm32_uart_ops _u2_ops = {
  679. .init = u2_init,
  680. .read = u2_read_data,
  681. .write = u2_write_data};
  682. static struct stm32_uart uart2 = {
  683. ._config = &_u2_config,
  684. .ops = &_u2_ops};
  685. void USART2_IRQHandler(void)
  686. {
  687. _uart_isr_callback(&uart2);
  688. }
  689. #ifdef UART2_TX_USING_DMA
  690. void DMA1_Stream6_IRQHandler(void)
  691. {
  692. _uart_tx_dma_isr_callback(&uart1);
  693. }
  694. #endif
  695. #ifdef UART2_RX_USING_DMA
  696. void DMA1_Stream5_IRQHandler(void)
  697. {
  698. _uart_rx_dma_isr_callback(&uart1);
  699. }
  700. #endif
  701. #endif // DRV_USING_UART2
  702. /**--------------------------- UART3 -----------------------------------------*/
  703. /**--------------------------- UART4 -----------------------------------------*/
  704. #ifdef DRV_USING_UART4
  705. static uint8_t u4_rx_fifo_buff[UART4_RX_FIFO_BUFFER_LEN] = {0};
  706. static uint8_t u4_tx_fifo_buff[UART4_TX_FIFO_BUFFER_LEN] = {0};
  707. #ifdef UART4_TX_USING_DMA
  708. static uint8_t u4_dma_tx_buf[UART4_TX_DMA_BUFFER_LEN] = {0};
  709. #endif
  710. #ifdef UART4_RX_USING_DMA
  711. static uint8_t u4_dma_rx_buf[UART4_RX_DMA_BUFFER_LEN] = {0};
  712. #endif
  713. struct stm32_uart_config _u4_config = {
  714. .uartx = UART4,
  715. ._tx_port = GPIOA,
  716. ._tx_pin = GPIO_Pin_0,
  717. ._rx_port = GPIOA,
  718. ._rx_pin = GPIO_Pin_1,
  719. ._uart_irq_channel = UART4_IRQn,
  720. ._tx_fifo_buff = u4_tx_fifo_buff,
  721. ._tx_fifo_buff_size = sizeof(u4_tx_fifo_buff),
  722. ._rx_fifo_buff = u4_rx_fifo_buff,
  723. ._rx_fifo_buff_size = sizeof(u4_rx_fifo_buff),
  724. #ifdef UART4_RX_USING_DMA
  725. ._rx_dma = DMA1_Stream2,
  726. ._rx_dma_tcif = DMA_IT_TCIF2,
  727. ._rx_dma_teif = DMA_IT_TEIF2,
  728. ._rx_dma_channel = DMA_Channel_4,
  729. ._dma_rx_buff = u4_dma_rx_buf,
  730. ._dma_rx_buff_size = sizeof(u4_dma_rx_buf),
  731. ._rx_dma_irq_channel = DMA1_Stream2_IRQn,
  732. #else
  733. ._rx_dma = NULL,
  734. #endif
  735. #ifdef UART4_TX_USING_DMA
  736. ._tx_dma = DMA1_Stream4,
  737. ._tx_dma_tcif = DMA_IT_TCIF4,
  738. ._tx_dma_teif = DMA_IT_TEIF4,
  739. ._tx_dma_channel = DMA_Channel_4,
  740. ._dma_tx_buff = u4_dma_tx_buf,
  741. ._dma_tx_buff_size = sizeof(u4_dma_tx_buf),
  742. ._tx_dma_irq_channel = DMA1_Stream4_IRQn,
  743. #else
  744. ._tx_dma = NULL,
  745. #endif
  746. };
  747. static uint32_t u4_write_data(const void *pdata, uint32_t len)
  748. {
  749. return uart_tx_data(&_u4_config, pdata, len);
  750. }
  751. static uint32_t u4_read_data(void *pdata, uint32_t len)
  752. {
  753. return uart_rx_data(&_u4_config, pdata, len);
  754. }
  755. static int u4_init(uint32_t bps)
  756. {
  757. return uart_init(&_u4_config, bps);
  758. }
  759. static struct stm32_uart_ops _u4_ops = {
  760. .init = u4_init,
  761. .read = u4_read_data,
  762. .write = u4_write_data};
  763. static struct stm32_uart uart4 = {
  764. ._config = &_u4_config,
  765. .ops = &_u4_ops};
  766. void UART4_IRQHandler(void)
  767. {
  768. _uart_isr_callback(&uart4);
  769. }
  770. #ifdef UART4_RX_USING_DMA
  771. void DMA1_Stream2_IRQHandler(void)
  772. {
  773. _uart_rx_dma_isr_callback(&uart4);
  774. }
  775. #endif
  776. #ifdef UART4_TX_USING_DMA
  777. void DMA1_Stream4_IRQHandler(void)
  778. {
  779. _uart_tx_dma_isr_callback(&uart4);
  780. }
  781. #endif
  782. #endif // DRV_USING_UART4
  783. struct stm32_uart *uart_find(const char *name)
  784. {
  785. struct stm32_uart *uart = NULL;
  786. if (strncmp(name, "uart1", 5) == 0)
  787. {
  788. #ifdef DRV_USING_UART1
  789. uart = &uart1;
  790. #endif // DEBUG
  791. }
  792. else if (strncmp(name, "uart2", 5) == 0)
  793. {
  794. #ifdef DRV_USING_UART2
  795. uart = &uart2;
  796. #endif // DEBUG
  797. }
  798. else if (strncmp(name, "uart3", 5) == 0)
  799. {
  800. #ifdef DRV_USING_UART3
  801. uart = &uart3;
  802. #endif // DEBUG
  803. }
  804. else if (strncmp(name, "uart4", 5) == 0)
  805. {
  806. #ifdef DRV_USING_UART4
  807. uart = &uart4;
  808. #endif // DEBUG
  809. }
  810. else if (strncmp(name, "uart5", 5) == 0)
  811. {
  812. #ifdef DRV_USING_UART5
  813. uart = &uart5;
  814. #endif // DEBUG
  815. }
  816. else if (strncmp(name, "uart6", 5) == 0)
  817. {
  818. #ifdef DRV_USING_UART6
  819. uart = &uart6;
  820. #endif // DEBUG
  821. }
  822. else
  823. {
  824. uart = NULL;
  825. }
  826. return uart;
  827. }