hal_uart.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include "hal_uart.h"
  2. #include "stm32f4xx.h"
  3. // #include <stdint.h>
  4. #include <stdint.h>
  5. #include <sys/unistd.h>
  6. static uint8_t usart1_tx_buf[256];
  7. static uint8_t usart1_rx_buf[256];
  8. static uint8_t usart2_tx_buf[256];
  9. static uint8_t usart2_rx_buf[256];
  10. static uint8_t usart3_tx_buf[256];
  11. static uint8_t usart3_rx_buf[256];
  12. #define HAL_UART1_TX_PORT GPIOA
  13. #define HAL_UART1_TX_PIN GPIO_Pin_9
  14. #define HAL_UART1_TX_SOURCE GPIO_PinSource9
  15. #define HAL_UART1_RX_PORT GPIOA
  16. #define HAL_UART1_RX_PIN GPIO_Pin_10
  17. #define HAL_UART1_RX_SOURCE GPIO_PinSource10
  18. #define HAL_UART2_TX_PORT GPIOA
  19. #define HAL_UART2_TX_PIN GPIO_Pin_2
  20. #define HAL_UART2_TX_SOURCE GPIO_PinSource2
  21. #define HAL_UART2_RX_PORT GPIOA
  22. #define HAL_UART2_RX_PIN GPIO_Pin_3
  23. #define HAL_UART2_RX_SOURCE GPIO_PinSource3
  24. #define HAL_UART3_TX_PORT GPIOB
  25. #define HAL_UART3_TX_PIN GPIO_Pin_10
  26. #define HAL_UART3_TX_SOURCE GPIO_PinSource2
  27. #define HAL_UART3_RX_PORT GPIOB
  28. #define HAL_UART3_RX_PIN GPIO_Pin_11
  29. #define HAL_UART3_RX_SOURCE GPIO_PinSource3
  30. int usart1_snd_tx_cnt = 0;
  31. // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
  32. // 确保没有从 C 库链接使用半主机的函数
  33. #pragma import(__use_no_semihosting)
  34. #pragma import(__use_no_semihosting_swi)
  35. // 定义_sys_exit() 以避免使用半主机模式
  36. void _sys_exit(int x)
  37. {
  38. x = x;
  39. }
  40. // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
  41. // 标准库需要的支持函数
  42. struct __FILE
  43. {
  44. int handle;
  45. };
  46. /* FILE is typedef ’ d in stdio.h. */
  47. FILE __stdout;
  48. FILE __stdin;
  49. int _ttywrch(int ch)
  50. {
  51. ch = ch;
  52. return ch;
  53. }
  54. // && !defined(__clang__)
  55. #if defined(__GNUC__)
  56. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  57. #define GETCHAR_PROTOTYPE int __io_getchar(void)
  58. #else
  59. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  60. #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
  61. #endif
  62. PUTCHAR_PROTOTYPE
  63. {
  64. uint32_t tick;
  65. usart1_snd_tx_cnt++;
  66. usart_send_it(&usart1_context, &ch, 1);
  67. tick = get_systick_ms();
  68. while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE))
  69. {
  70. if (get_systick_ms() - tick > 10)
  71. break;
  72. }
  73. return ch;
  74. }
  75. GETCHAR_PROTOTYPE
  76. {
  77. uint8_t ch;
  78. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  79. {
  80. return ch;
  81. }
  82. return -1;
  83. }
  84. // &&!defined(__clang__)
  85. #if defined(__GNUC__)
  86. __attribute__((weak)) int _write(int file, char *ptr, int len)
  87. {
  88. (void)file;
  89. if ((file != STDOUT_FILENO) && (file != STDERR_FILENO))
  90. {
  91. return -1;
  92. }
  93. for (int index = 0; index < len; index++)
  94. __io_putchar(*ptr++);
  95. return len;
  96. }
  97. __attribute__((weak)) int _read(int file, char *ptr, int len)
  98. {
  99. (void)file;
  100. if (file != STDIN_FILENO)
  101. {
  102. return -1;
  103. }
  104. uint8_t ch;
  105. for (int index = 0; index < len; index++)
  106. {
  107. // *ptr++ = __io_getchar();
  108. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  109. {
  110. return ch;
  111. }
  112. return -1;
  113. }
  114. return len;
  115. }
  116. #endif
  117. #if 0
  118. int getchar(void)
  119. {
  120. char c = 0;
  121. // ssize_t
  122. int bytes_read = read(STDIN_FILENO, &c, sizeof(char));
  123. if (bytes_read == -1)
  124. {
  125. // 读取错误
  126. return EOF;
  127. }
  128. else if (bytes_read == 0)
  129. {
  130. // 读取结束
  131. return EOF;
  132. }
  133. else
  134. {
  135. // 返回读取的字符
  136. return (int)c;
  137. }
  138. }
  139. #else
  140. int getchar(void)
  141. {
  142. uint8_t ch;
  143. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  144. {
  145. return ch;
  146. }
  147. return -1;
  148. }
  149. #endif
  150. static void usart1_config(void)
  151. {
  152. GPIO_InitTypeDef GPIO_StructInit;
  153. NVIC_InitTypeDef NVIC_InitStructure;
  154. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  155. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  156. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  157. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  158. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  159. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  160. GPIO_StructInit.GPIO_Pin = HAL_UART1_TX_PIN;
  161. GPIO_PinAFConfig(HAL_UART1_TX_PORT, HAL_UART1_TX_SOURCE, GPIO_AF_USART1);
  162. GPIO_Init(HAL_UART1_TX_PORT, &GPIO_StructInit);
  163. GPIO_StructInit.GPIO_Pin = HAL_UART1_RX_PIN;
  164. GPIO_PinAFConfig(HAL_UART1_RX_PORT, HAL_UART1_RX_SOURCE, GPIO_AF_USART1);
  165. GPIO_Init(HAL_UART1_RX_PORT, &GPIO_StructInit);
  166. // Usart1 NVIC 配置
  167. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 串口1中断通道
  168. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  169. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  170. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  171. NVIC_Init(&NVIC_InitStructure);
  172. }
  173. static void usart1_deconfig(void)
  174. {
  175. }
  176. static void usart2_config(void)
  177. {
  178. GPIO_InitTypeDef GPIO_StructInit;
  179. NVIC_InitTypeDef NVIC_InitStructure;
  180. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  181. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  182. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  183. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  184. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  185. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  186. GPIO_StructInit.GPIO_Pin = HAL_UART2_TX_PIN;
  187. GPIO_PinAFConfig(HAL_UART2_TX_PORT, HAL_UART2_TX_SOURCE, GPIO_AF_USART2);
  188. GPIO_Init(HAL_UART2_TX_PORT, &GPIO_StructInit);
  189. GPIO_StructInit.GPIO_Pin = HAL_UART2_RX_PIN;
  190. GPIO_PinAFConfig(HAL_UART2_RX_PORT, HAL_UART2_RX_SOURCE, GPIO_AF_USART2);
  191. GPIO_Init(HAL_UART2_RX_PORT, &GPIO_StructInit);
  192. // Usart1 NVIC 配置
  193. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // 串口1中断通道
  194. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  195. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  196. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  197. NVIC_Init(&NVIC_InitStructure);
  198. }
  199. static void usart2_deconfig(void)
  200. {
  201. }
  202. static void usart3_config(void)
  203. {
  204. GPIO_InitTypeDef GPIO_StructInit;
  205. NVIC_InitTypeDef NVIC_InitStructure;
  206. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  207. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  208. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  209. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  210. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  211. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  212. GPIO_StructInit.GPIO_Pin = HAL_UART3_TX_PIN;
  213. GPIO_PinAFConfig(HAL_UART3_TX_PORT, HAL_UART3_TX_SOURCE, GPIO_AF_USART3);
  214. GPIO_Init(HAL_UART3_TX_PORT, &GPIO_StructInit);
  215. GPIO_StructInit.GPIO_Pin = HAL_UART3_RX_PIN;
  216. GPIO_PinAFConfig(HAL_UART3_RX_PORT, HAL_UART3_RX_SOURCE, GPIO_AF_USART3);
  217. GPIO_Init(HAL_UART3_RX_PORT, &GPIO_StructInit);
  218. // Usart1 NVIC 配置
  219. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // 串口1中断通道
  220. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  221. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  222. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  223. NVIC_Init(&NVIC_InitStructure);
  224. }
  225. static void usart3_deconfig(void)
  226. {
  227. }
  228. usart_context_t usart1_context = {
  229. USART1,
  230. usart1_tx_buf,
  231. sizeof(usart1_tx_buf),
  232. 0,
  233. 0,
  234. 0,
  235. usart1_rx_buf,
  236. sizeof(usart1_rx_buf),
  237. 0,
  238. 0,
  239. 0,
  240. usart1_config,
  241. usart1_deconfig};
  242. usart_context_t usart2_context = {
  243. USART2,
  244. usart2_tx_buf,
  245. sizeof(usart2_tx_buf),
  246. 0,
  247. 0,
  248. 0,
  249. usart2_rx_buf,
  250. sizeof(usart2_rx_buf),
  251. 0,
  252. 0,
  253. 0,
  254. usart2_config,
  255. usart2_deconfig};
  256. usart_context_t usart3_context = {
  257. USART3,
  258. usart3_tx_buf,
  259. sizeof(usart3_tx_buf),
  260. 0,
  261. 0,
  262. 0,
  263. usart3_rx_buf,
  264. sizeof(usart3_rx_buf),
  265. 0,
  266. 0,
  267. 0,
  268. usart3_config,
  269. usart3_deconfig};
  270. void usart_config_init(usart_context_t *p_usart_content, uint32_t band_rate)
  271. {
  272. p_usart_content->config();
  273. USART_InitTypeDef USART_InitStructure;
  274. USART_InitStructure.USART_BaudRate = band_rate;
  275. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  276. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  277. USART_InitStructure.USART_Parity = USART_Parity_No;
  278. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  279. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  280. USART_Init(p_usart_content->usart_periph, &USART_InitStructure); // 初始化串口
  281. USART_ITConfig(p_usart_content->usart_periph, USART_IT_RXNE, ENABLE);
  282. USART_Cmd(p_usart_content->usart_periph, ENABLE); // 使能串口
  283. }
  284. void usart_config_deinit(usart_context_t *p_usart_content)
  285. {
  286. USART_DeInit(p_usart_content->usart_periph);
  287. p_usart_content->deconfig();
  288. }
  289. void usart_send(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
  290. {
  291. const uint8_t *send_buf = (const uint8_t *)_send_buf;
  292. uint16_t i;
  293. for (i = 0; i < send_count; i++)
  294. {
  295. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
  296. ;
  297. USART_SendData(p_usart_content->usart_periph, (uint8_t)send_buf[i]);
  298. }
  299. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  300. ;
  301. }
  302. int32_t usart_read_until_char(usart_context_t *p_usart_content, char c)
  303. {
  304. uint32_t i;
  305. for (i = p_usart_content->rx_rd; i < p_usart_content->rx_wr; i++)
  306. {
  307. if (p_usart_content->rx_buf[i % p_usart_content->rx_buf_size] == c)
  308. {
  309. return i;
  310. }
  311. }
  312. return -1;
  313. }
  314. // read until memory
  315. // return:第一次出现mem位置, -1 没找到
  316. int32_t usart_read_until_mem(usart_context_t *p_usart_content, char *mem, int len)
  317. {
  318. int32_t i, j, pos;
  319. for (i = p_usart_content->rx_rd; i < p_usart_content->rx_wr; i++)
  320. {
  321. // overflow
  322. for (j = 0; j < len; j++)
  323. {
  324. pos = i + j;
  325. if (p_usart_content->rx_buf[pos % p_usart_content->rx_buf_size] != mem[j])
  326. {
  327. break;
  328. }
  329. }
  330. // 找到内存快
  331. if (j == len)
  332. {
  333. return i;
  334. }
  335. }
  336. return -1;
  337. }
  338. // read ble format 04 FC code len data
  339. // return 满足format条件结束位置, -1 没找到
  340. int32_t usart_read_ble_format(usart_context_t *p_usart_content)
  341. {
  342. uint16_t rx_wr = p_usart_content->rx_wr;
  343. uint16_t rx_rd = p_usart_content->rx_rd;
  344. if (rx_wr < rx_rd)
  345. {
  346. rx_wr += p_usart_content->rx_buf_size;
  347. }
  348. if (rx_wr - rx_rd < 4)
  349. {
  350. return -1;
  351. }
  352. // 第一次开机跳过特殊桢处理 00 04,跳过00
  353. if (p_usart_content->rx_buf[rx_rd % p_usart_content->rx_buf_size] == 0x00 || p_usart_content->rx_buf[(rx_rd + 1) % p_usart_content->rx_buf_size] == 0x04)
  354. {
  355. p_usart_content->rx_rd++;
  356. p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
  357. return -4;
  358. }
  359. if (p_usart_content->rx_buf[rx_rd % p_usart_content->rx_buf_size] != 0x04 || p_usart_content->rx_buf[(rx_rd + 1) % p_usart_content->rx_buf_size] != 0xFC)
  360. {
  361. return -2;
  362. }
  363. // 蓝牙长度不够
  364. if (rx_wr - rx_rd < p_usart_content->rx_buf[(rx_rd + 3) % p_usart_content->rx_buf_size] + 4)
  365. {
  366. return -3;
  367. }
  368. return p_usart_content->rx_buf[(rx_rd + 3) % p_usart_content->rx_buf_size] + 4;
  369. }
  370. int32_t usart_copy_data(usart_context_t *p_usart_content, int rlen, char *data)
  371. {
  372. int32_t i, p;
  373. // 拷贝rlen + 1个数据
  374. for (i = 0; i < rlen; i++)
  375. {
  376. p = (p_usart_content->rx_rd + i) % p_usart_content->rx_buf_size;
  377. data[i] = p_usart_content->rx_buf[p];
  378. }
  379. return 0;
  380. }
  381. int32_t usart_read_update(usart_context_t *p_usart_content, int rlen, char *data)
  382. {
  383. int32_t i, p;
  384. // 拷贝rlen + 1个数据
  385. for (i = 0; i < rlen; i++)
  386. {
  387. p = (p_usart_content->rx_rd + i) % p_usart_content->rx_buf_size;
  388. data[i] = p_usart_content->rx_buf[p];
  389. }
  390. // 指定pos下一个位置
  391. // return p_usart_content->rx_rd = (p_usart_content->rx_rd + rlen) % p_usart_content->rx_buf_size;
  392. p = p_usart_content->rx_rd = (p_usart_content->rx_rd + rlen) % p_usart_content->rx_buf_size;
  393. return p;
  394. }
  395. void usart_send_it(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
  396. {
  397. const uint8_t *send_buf = (const uint8_t *)_send_buf;
  398. uint16_t i;
  399. uint16_t left;
  400. // 空间不够直接返回
  401. if (p_usart_content->tx_wr >= p_usart_content->tx_rd)
  402. {
  403. left = p_usart_content->tx_buf_size - (p_usart_content->tx_wr - p_usart_content->tx_rd);
  404. }
  405. else
  406. {
  407. left = p_usart_content->tx_rd - p_usart_content->tx_wr;
  408. if (send_count > left)
  409. {
  410. p_usart_content->tx_err++;
  411. return;
  412. }
  413. }
  414. // 内存拷贝
  415. for (i = 0; i < send_count; i++)
  416. {
  417. // send buffer overflow just wait
  418. p_usart_content->tx_buf[p_usart_content->tx_wr++] = send_buf[i];
  419. p_usart_content->tx_wr %= p_usart_content->tx_buf_size;
  420. }
  421. // 使能发送缓存空中断
  422. USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, ENABLE);
  423. }
  424. void usart_wait_send_finished(usart_context_t *p_usart_content)
  425. {
  426. while (p_usart_content->tx_wr != p_usart_content->tx_rd)
  427. ;
  428. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  429. ;
  430. }
  431. int usart_receive_read(usart_context_t *p_usart_context, void *_receive_buf, const int receive_count)
  432. {
  433. uint8_t *receive_buf = (uint8_t *)_receive_buf;
  434. int i, receive_count_real;
  435. /* Read data from receive buffer.
  436. *The buffer have data that received from usart.
  437. */
  438. for (i = 0, receive_count_real = 0; i < receive_count; i++)
  439. {
  440. if (p_usart_context->rx_rd == p_usart_context->rx_wr)
  441. {
  442. return receive_count_real;
  443. }
  444. else
  445. {
  446. receive_buf[i] = p_usart_context->rx_buf[p_usart_context->rx_rd++];
  447. p_usart_context->rx_rd %= p_usart_context->rx_buf_size;
  448. receive_count_real++;
  449. }
  450. }
  451. return receive_count_real;
  452. }
  453. static void usart_rxne_it(usart_context_t *p_usart_content)
  454. {
  455. p_usart_content->rx_buf[p_usart_content->rx_wr++] = USART_ReceiveData(p_usart_content->usart_periph);
  456. p_usart_content->rx_wr %= p_usart_content->rx_buf_size;
  457. // overflow handle
  458. if (p_usart_content->rx_wr == p_usart_content->rx_rd)
  459. {
  460. p_usart_content->rx_rd++;
  461. p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
  462. }
  463. }
  464. static void usart_txe_it(usart_context_t *p_usart_content)
  465. {
  466. if (p_usart_content->tx_rd != p_usart_content->tx_wr)
  467. {
  468. USART_SendData(p_usart_content->usart_periph, p_usart_content->tx_buf[p_usart_content->tx_rd++]);
  469. p_usart_content->tx_rd %= p_usart_content->tx_buf_size;
  470. }
  471. else
  472. {
  473. USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, DISABLE);
  474. }
  475. }
  476. void usart_it(usart_context_t *p_usart_content)
  477. {
  478. // 读取缓沖区非空中断
  479. if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_RXNE))
  480. {
  481. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_RXNE);
  482. usart_rxne_it(p_usart_content);
  483. }
  484. // 发送缓沖区空中断
  485. if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
  486. {
  487. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_TXE);
  488. usart_txe_it(p_usart_content);
  489. }
  490. // // 发送完成中断
  491. // if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  492. // {
  493. // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_TC, DISABLE);
  494. // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_RXNE, ENABLE);
  495. // }
  496. // 发送错误
  497. if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_ORE))
  498. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_ORE);
  499. }
  500. void USART1_IRQHandler(void)
  501. {
  502. usart_it(&usart1_context);
  503. }
  504. void USART2_IRQHandler(void)
  505. {
  506. usart_it(&usart2_context);
  507. }
  508. void USART3_IRQHandler(void)
  509. {
  510. usart_it(&usart3_context);
  511. }