core_uart.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __CORE_UART_H__
  22. #define __CORE_UART_H__
  23. #include "bsp_common.h"
  24. /**
  25. * @brief 对串口做基本的初始化工作,不开启任何中断和DMA
  26. *
  27. * @param UartID 串口号 0~MAX,对应USART1~UARTX,0默认用于log输出
  28. * @param BaudRate 波特率
  29. * @param IsRxCacheEnable 是否打开RX缓存功能,打开后,只有接收超时中断才会返回,否则每次数据到来都会返回
  30. * @param DataBits 数据位5~8
  31. * @param Parity 奇偶校验,可选下列
  32. * UART_PARITY_NONE,
  33. UART_PARITY_ODD,
  34. UART_PARITY_EVEN,
  35. * @param StopBits 停止位
  36. * UART_STOP_BIT1,
  37. UART_STOP_BIT1_5,
  38. UART_STOP_BIT2,
  39. * @param CB 回调函数,用于通知是否有新数据达到,DMA TX或者RX完成,是否有错误
  40. */
  41. void Uart_BaseInit(uint8_t UartID, uint32_t BaudRate, uint8_t IsRxCacheEnable, uint8_t DataBits, uint8_t Parity, uint8_t StopBits, CBFuncEx_t CB);
  42. void Uart_SetCb(uint8_t UartID, CBFuncEx_t CB);
  43. void Uart_DeInit(uint8_t UartID);
  44. /**
  45. * @brief 串口做阻塞输出,一般用于bootloader或者紧急场合
  46. *
  47. * @param UartID 串口号 0~MAX,对应USART1~UARTX,0默认用于log输出
  48. * @param Data
  49. * @param Len
  50. */
  51. void Uart_BlockTx(uint8_t UartID, uint8_t *Data, uint32_t Len);
  52. void Uart_NoBlockTx(uint8_t UartID, uint8_t Data);
  53. void Uart_EnableRxIrq(uint8_t UartID);
  54. void Uart_EnableTxDoneIrq(uint8_t UartID);
  55. /**
  56. * @brief 串口的DMA配置
  57. *
  58. * @param UartID 串口号 0~5
  59. * @param Stream DMA流序号
  60. * @param Channel DMA通道
  61. * @return >0 成功返回中断号,其他失败
  62. */
  63. int Uart_DMATxInit(uint8_t UartID, uint8_t Stream, uint32_t Channel);
  64. int Uart_DMARxInit(uint8_t UartID, uint8_t Stream, uint32_t Channel);
  65. /**
  66. * @brief 串口做DMA非阻塞输出,只能用于APP
  67. *
  68. * @param UartID 串口号 0~5
  69. * @param Stream DMA流序号
  70. * @param Data
  71. * @param Len
  72. */
  73. void Uart_DMATx(uint8_t UartID, uint8_t Stream, const uint8_t *Data, uint32_t Len);
  74. void Uart_DMARx(uint8_t UartID, uint8_t Stream, uint8_t *Data, uint32_t Len);
  75. uint8_t Uart_IsTSREmpty(uint8_t UartID);
  76. uint32_t Uart_RxBufferRead(uint8_t UartID, uint8_t *Data, uint32_t Len);
  77. void Uart_RxBufferCB(uint8_t UartID, CBFuncEx_t CB);
  78. uint32_t Uart_FifoRead(uint8_t UartID, uint8_t *Data);
  79. /**
  80. * @brief 串口做FIFO非阻塞输出
  81. *
  82. * @param UartID 串口号 0~5
  83. * @param Data
  84. * @param Len
  85. * @return <0 失败 =0 所有数据传入fifo,不再回调buffer_done,直接回调all_done >0 还有数据没有传入fifo
  86. */
  87. int32_t Uart_BufferTx(uint8_t UartID, const uint8_t *Data, uint32_t Len);
  88. void Uart_BufferTxStop(uint8_t UartID);
  89. void Uart_PrintReg(uint8_t UartID);
  90. void Uart_ChangeBR(uint8_t UartID, uint32_t BaudRate);
  91. uint32_t Uart_GetLastError(uint8_t UartID);
  92. void Uart_GlobalInit(void);
  93. void Uart_Sleep(uint8_t UartID, uint8_t OnOff);
  94. #endif