hal_uart.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "hal_uart.h"
  2. #include "stm32f4xx.h"
  3. // #include <stdint.h>
  4. #include <sys/unistd.h>
  5. static uint8_t usart1_tx_buf[256];
  6. static uint8_t usart1_rx_buf[256];
  7. #define HAL_UART1_TX_PORT GPIOA
  8. #define HAL_UART1_TX_PIN GPIO_Pin_9
  9. #define HAL_UART1_TX_SOURCE GPIO_PinSource9
  10. #define HAL_UART1_RX_PORT GPIOA
  11. #define HAL_UART1_RX_PIN GPIO_Pin_10
  12. #define HAL_UART1_RX_SOURCE GPIO_PinSource10
  13. int usart1_snd_tx_cnt = 0;
  14. // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
  15. // 确保没有从 C 库链接使用半主机的函数
  16. #pragma import(__use_no_semihosting)
  17. #pragma import(__use_no_semihosting_swi)
  18. // 定义_sys_exit() 以避免使用半主机模式
  19. void _sys_exit(int x)
  20. {
  21. x = x;
  22. }
  23. // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
  24. // 标准库需要的支持函数
  25. struct __FILE
  26. {
  27. int handle;
  28. };
  29. /* FILE is typedef ’ d in stdio.h. */
  30. FILE __stdout;
  31. FILE __stdin;
  32. int _ttywrch(int ch)
  33. {
  34. ch = ch;
  35. return ch;
  36. }
  37. // && !defined(__clang__)
  38. #if defined(__GNUC__)
  39. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  40. #define GETCHAR_PROTOTYPE int __io_getchar(void)
  41. #else
  42. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  43. #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
  44. #endif
  45. PUTCHAR_PROTOTYPE
  46. {
  47. uint32_t tick;
  48. usart1_snd_tx_cnt++;
  49. usart_send_it(&usart1_context, &ch, 1);
  50. tick = get_systick_ms();
  51. while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE))
  52. {
  53. if (get_systick_ms() - tick > 10)
  54. break;
  55. }
  56. return ch;
  57. }
  58. GETCHAR_PROTOTYPE
  59. {
  60. uint8_t ch;
  61. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  62. {
  63. return ch;
  64. }
  65. return -1;
  66. }
  67. // &&!defined(__clang__)
  68. #if defined(__GNUC__)
  69. __attribute__((weak)) int _write(int file, char *ptr, int len)
  70. {
  71. (void)file;
  72. if ((file != STDOUT_FILENO) && (file != STDERR_FILENO))
  73. {
  74. return -1;
  75. }
  76. for (int index = 0; index < len; index++)
  77. __io_putchar(*ptr++);
  78. return len;
  79. }
  80. __attribute__((weak)) int _read(int file, char *ptr, int len)
  81. {
  82. (void)file;
  83. if (file != STDIN_FILENO)
  84. {
  85. return -1;
  86. }
  87. uint8_t ch;
  88. for (int index = 0; index < len; index++)
  89. {
  90. // *ptr++ = __io_getchar();
  91. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  92. {
  93. return ch;
  94. }
  95. return -1;
  96. }
  97. return len;
  98. }
  99. #endif
  100. #if 0
  101. int getchar(void)
  102. {
  103. char c = 0;
  104. // ssize_t
  105. int bytes_read = read(STDIN_FILENO, &c, sizeof(char));
  106. if (bytes_read == -1)
  107. {
  108. // 读取错误
  109. return EOF;
  110. }
  111. else if (bytes_read == 0)
  112. {
  113. // 读取结束
  114. return EOF;
  115. }
  116. else
  117. {
  118. // 返回读取的字符
  119. return (int)c;
  120. }
  121. }
  122. #else
  123. int getchar(void)
  124. {
  125. uint8_t ch;
  126. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  127. {
  128. return ch;
  129. }
  130. return -1;
  131. }
  132. #endif
  133. static void usart1_config(void)
  134. {
  135. GPIO_InitTypeDef GPIO_StructInit;
  136. NVIC_InitTypeDef NVIC_InitStructure;
  137. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  138. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  139. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  140. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  141. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  142. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  143. GPIO_StructInit.GPIO_Pin = HAL_UART1_TX_PIN;
  144. GPIO_PinAFConfig(HAL_UART1_TX_PORT, HAL_UART1_TX_SOURCE, GPIO_AF_USART1);
  145. GPIO_Init(HAL_UART1_TX_PORT, &GPIO_StructInit);
  146. GPIO_StructInit.GPIO_Pin = HAL_UART1_RX_PIN;
  147. GPIO_PinAFConfig(HAL_UART1_RX_PORT, HAL_UART1_RX_SOURCE, GPIO_AF_USART1);
  148. GPIO_Init(HAL_UART1_RX_PORT, &GPIO_StructInit);
  149. // Usart1 NVIC 配置
  150. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 串口1中断通道
  151. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  152. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  153. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  154. NVIC_Init(&NVIC_InitStructure);
  155. }
  156. static void usart1_deconfig(void)
  157. {
  158. }
  159. usart_context_t usart1_context = {
  160. USART1,
  161. usart1_tx_buf,
  162. sizeof(usart1_tx_buf),
  163. 0,
  164. 0,
  165. 0,
  166. usart1_rx_buf,
  167. sizeof(usart1_rx_buf),
  168. 0,
  169. 0,
  170. 0,
  171. usart1_config,
  172. usart1_deconfig};
  173. void usart_config_init(usart_context_t *p_usart_content, uint32_t band_rate)
  174. {
  175. p_usart_content->config();
  176. USART_InitTypeDef USART_InitStructure;
  177. USART_InitStructure.USART_BaudRate = band_rate;
  178. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  179. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  180. USART_InitStructure.USART_Parity = USART_Parity_No;
  181. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  182. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  183. USART_Init(p_usart_content->usart_periph, &USART_InitStructure); // 初始化串口1
  184. USART_ITConfig(p_usart_content->usart_periph, USART_IT_RXNE, ENABLE);
  185. USART_Cmd(USART1, ENABLE); // 使能串口1
  186. }
  187. void usart_config_deinit(usart_context_t *p_usart_content)
  188. {
  189. USART_DeInit(p_usart_content->usart_periph);
  190. p_usart_content->deconfig();
  191. }
  192. void usart_send(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
  193. {
  194. const uint8_t *send_buf = (const uint8_t *)_send_buf;
  195. uint16_t i;
  196. for (i = 0; i < send_count; i++)
  197. {
  198. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
  199. ;
  200. USART_SendData(p_usart_content->usart_periph, (uint8_t)send_buf[i]);
  201. }
  202. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  203. ;
  204. }
  205. void usart_send_it(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
  206. {
  207. const uint8_t *send_buf = (const uint8_t *)_send_buf;
  208. uint16_t i;
  209. uint16_t left;
  210. // 空间不够直接返回
  211. if (p_usart_content->tx_wr >= p_usart_content->tx_rd)
  212. {
  213. left = p_usart_content->tx_buf_size - (p_usart_content->tx_wr - p_usart_content->tx_rd);
  214. }
  215. else
  216. {
  217. left = p_usart_content->tx_rd - p_usart_content->tx_wr;
  218. if (send_count > left)
  219. {
  220. p_usart_content->tx_err++;
  221. return;
  222. }
  223. }
  224. // 内存拷贝
  225. for (i = 0; i < send_count; i++)
  226. {
  227. // send buffer overflow just wait
  228. p_usart_content->tx_buf[p_usart_content->tx_wr++] = send_buf[i];
  229. p_usart_content->tx_wr %= p_usart_content->tx_buf_size;
  230. }
  231. // 使能发送缓存空中断
  232. USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, ENABLE);
  233. }
  234. void usart_wait_send_finished(usart_context_t *p_usart_content)
  235. {
  236. while (p_usart_content->tx_wr != p_usart_content->tx_rd)
  237. ;
  238. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  239. ;
  240. }
  241. int usart_receive_read(usart_context_t *p_usart_context, void *_receive_buf, const int receive_count)
  242. {
  243. uint8_t *receive_buf = (uint8_t *)_receive_buf;
  244. int i, receive_count_real;
  245. /* Read data from receive buffer.
  246. *The buffer have data that received from usart.
  247. */
  248. for (i = 0, receive_count_real = 0; i < receive_count; i++)
  249. {
  250. if (p_usart_context->rx_rd == p_usart_context->rx_wr)
  251. {
  252. return receive_count_real;
  253. }
  254. else
  255. {
  256. receive_buf[i] = p_usart_context->rx_buf[p_usart_context->rx_rd++];
  257. p_usart_context->rx_rd %= p_usart_context->rx_buf_size;
  258. receive_count_real++;
  259. }
  260. }
  261. return receive_count_real;
  262. }
  263. static void usart_rxne_it(usart_context_t *p_usart_content)
  264. {
  265. p_usart_content->rx_buf[p_usart_content->rx_wr++] = USART_ReceiveData(p_usart_content->usart_periph);
  266. p_usart_content->rx_wr %= p_usart_content->rx_buf_size;
  267. // overflow handle
  268. if (p_usart_content->rx_wr == p_usart_content->rx_rd)
  269. {
  270. p_usart_content->rx_rd++;
  271. p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
  272. }
  273. }
  274. static void usart_txe_it(usart_context_t *p_usart_content)
  275. {
  276. if (p_usart_content->tx_rd != p_usart_content->tx_wr)
  277. {
  278. USART_SendData(p_usart_content->usart_periph, p_usart_content->tx_buf[p_usart_content->tx_rd++]);
  279. p_usart_content->tx_rd %= p_usart_content->tx_buf_size;
  280. }
  281. else
  282. {
  283. USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, DISABLE);
  284. }
  285. }
  286. void usart_it(usart_context_t *p_usart_content)
  287. {
  288. // 读取缓沖区非空中断
  289. if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_RXNE))
  290. {
  291. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_RXNE);
  292. usart_rxne_it(p_usart_content);
  293. }
  294. // 发送缓沖区空中断
  295. if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
  296. {
  297. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_TXE);
  298. usart_txe_it(p_usart_content);
  299. }
  300. // // 发送完成中断
  301. // if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  302. // {
  303. // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_TC, DISABLE);
  304. // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_RXNE, ENABLE);
  305. // }
  306. // 发送错误
  307. if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_ORE))
  308. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_ORE);
  309. }
  310. void USART1_IRQHandler(void)
  311. {
  312. usart_it(&usart1_context);
  313. }