uart1.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. ************************************************************************************************
  3. * @文件 : uart1.c
  4. * @作者 : 樊春春
  5. * @版本 : V1.0
  6. * @时间 : 2022/05/31 20:20:39
  7. * @邮箱 : [email protected]
  8. * @说明 : uart1底层驱动,待修复
  9. ************************************************************************************************
  10. **/
  11. #include "uart1.h"
  12. #include "queue.h"
  13. static uint8_t uart1_tx_buf[UART1_TX_LEN] = {0};
  14. static uint8_t uart1_rx_buf[UART1_REC_LEN] = {0};
  15. __IO uint8_t rx_index = 0x00;
  16. __IO uint8_t tx_index = 0x00;
  17. __IO uint8_t TimeOut = 0x00;
  18. // 重定义fputc函数
  19. #ifdef __GNUC__
  20. int _write(int fd, char *buffer, int size)
  21. {
  22. /* 写一个字节到USART1 */
  23. for (int i = 0; i < size; i++)
  24. {
  25. USART_SendData(USART1, buffer[i]);
  26. /* 等待发送结束 */
  27. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  28. ;
  29. }
  30. return size;
  31. }
  32. int _read(int fd, char *buffer, int size)
  33. {
  34. for (int i = 0; i < size; i++)
  35. {
  36. /* 等待串口1输入数据 */
  37. while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
  38. ;
  39. buffer[i] = USART_ReceiveData(USART1);
  40. }
  41. return size;
  42. }
  43. // int _write(int fd, char *buffer, int size)
  44. // {
  45. // uart1_dma_send((uint8_t *)buffer, size);
  46. // while (USART_GetITStatus(USART1, USART_IT_TC) != RESET)
  47. // ;
  48. // return size;
  49. // }
  50. // int _read(int fd, char *buffer, int size)
  51. // {
  52. // /* 等待串口1输入数据 */
  53. // while (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
  54. // ;
  55. // // uint8_t err = 0;
  56. // // UartFrame_TypeDef *msg;
  57. // // msg = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
  58. // // return msg->len;
  59. // return (int)USART_ReceiveData(USART1);
  60. // }
  61. #endif
  62. void uart1_nvic_config(void)
  63. {
  64. NVIC_InitTypeDef NVIC_InitStructure;
  65. // Usart1 NVIC 配置
  66. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //串口1中断通道
  67. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
  68. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级
  69. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  70. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  71. }
  72. void uart1_config(void)
  73. {
  74. USART_InitTypeDef USART_InitStructure;
  75. USART_InitStructure.USART_BaudRate = 115200; //波特率设置
  76. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
  77. USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
  78. USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
  79. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
  80. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  81. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  82. USART_Cmd(USART1, ENABLE); //使能串口1
  83. }
  84. void uart1_init(void)
  85. {
  86. uart1_config();
  87. uart1_nvic_config();
  88. /* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
  89. 如下语句解决第1个字节无法正确发送出去的问题 */
  90. USART_ClearFlag(USART1, USART_FLAG_TC);
  91. }
  92. void USART1_IRQHandler(void)
  93. {
  94. /* 处理接收中断 */
  95. if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  96. {
  97. if (rx_index < UART1_REC_LEN)
  98. {
  99. /* 接收数据 */
  100. uart1_rx_buf[rx_index++] = USART_ReceiveData(USART1);
  101. }
  102. else
  103. {
  104. /* 禁止接收中断 */
  105. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
  106. }
  107. }
  108. /* 处理发送缓冲区空中断 */
  109. if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
  110. {
  111. if (tx_index < UART1_TX_LEN)
  112. {
  113. /* 发送数据 */
  114. USART_SendData(USART1, uart1_tx_buf[tx_index++]);
  115. }
  116. else
  117. {
  118. /* 禁止发送缓冲区空中断 */
  119. USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
  120. }
  121. }
  122. }