bsp_uart.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*----------------------------------------------------------------------------------------------------
  2. INCLUDE HEADE FILES
  3. ----------------------------------------------------------------------------------------------------*/
  4. #include "Platform.h"
  5. #define CPR_BASE 0x40000000
  6. #define UART0_BASE 0x40010000
  7. #define UART1_BASE 0x40011000
  8. #define CPR_UARTx_CLK_GRCTL(a) ((volatile unsigned *)(CPR_BASE + 0x30 + (a * 0x08)))
  9. #define CPR_UARTx_CLK_CTL(a) ((volatile unsigned *)(CPR_BASE + 0x34 + (a * 0x08)))
  10. #define UARTx_RBR(a) ((volatile unsigned *)(UART0_BASE + 0x00 + (a * 0x1000)))
  11. #define UARTx_THR(a) ((volatile unsigned *)(UART0_BASE + 0x00 + (a * 0x1000)))
  12. #define UARTx_DLL(a) ((volatile unsigned *)(UART0_BASE + 0x00 + (a * 0x1000)))
  13. #define UARTx_IER(a) ((volatile unsigned *)(UART0_BASE + 0x04 + (a * 0x1000)))
  14. #define UARTx_DLH(a) ((volatile unsigned *)(UART0_BASE + 0x04 + (a * 0x1000)))
  15. #define UARTx_IIR(a) ((volatile unsigned *)(UART0_BASE + 0x08 + (a * 0x1000)))
  16. #define UARTx_FCR(a) ((volatile unsigned *)(UART0_BASE + 0x08 + (a * 0x1000)))
  17. #define UARTx_TCR(a) ((volatile unsigned *)(UART0_BASE + 0x0c + (a * 0x1000)))
  18. #define UARTx_MCR(a) ((volatile unsigned *)(UART0_BASE + 0x10 + (a * 0x1000)))
  19. #define UARTx_TSR(a) ((volatile unsigned *)(UART0_BASE + 0x14 + (a * 0x1000)))
  20. #define UARTx_MSR(a) ((volatile unsigned *)(UART0_BASE + 0x18 + (a * 0x1000)))
  21. #define UARTx_USR(a) ((volatile unsigned *)(UART0_BASE + 0x7c + (a * 0x1000)))
  22. #define UART0_RBR ((volatile unsigned *)(UART0_BASE + 0x00))
  23. #define UART0_THR ((volatile unsigned *)(UART0_BASE + 0x00))
  24. #define UART0_DLL ((volatile unsigned *)(UART0_BASE + 0x00))
  25. #define UART0_IER ((volatile unsigned *)(UART0_BASE + 0x04))
  26. #define UART0_DLH ((volatile unsigned *)(UART0_BASE + 0x04))
  27. #define UART0_IIR ((volatile unsigned *)(UART0_BASE + 0x08))
  28. #define UART0_FCR ((volatile unsigned *)(UART0_BASE + 0x08))
  29. #define UART0_TCR ((volatile unsigned *)(UART0_BASE + 0x0c))
  30. #define UART0_MCR ((volatile unsigned *)(UART0_BASE + 0x10))
  31. #define UART0_TSR ((volatile unsigned *)(UART0_BASE + 0x14))
  32. #define UART0_MSR ((volatile unsigned *)(UART0_BASE + 0x18))
  33. #define UART0_USR ((volatile unsigned *)(UART0_BASE + 0x7c))
  34. #define UART1_RBR ((volatile unsigned *)(UART1_BASE + 0x00))
  35. #define UART1_THR ((volatile unsigned *)(UART1_BASE + 0x00))
  36. #define UART1_DLL ((volatile unsigned *)(UART1_BASE + 0x00))
  37. #define UART1_IER ((volatile unsigned *)(UART1_BASE + 0x04))
  38. #define UART1_DLH ((volatile unsigned *)(UART1_BASE + 0x04))
  39. #define UART1_IIR ((volatile unsigned *)(UART1_BASE + 0x08))
  40. #define UART1_FCR ((volatile unsigned *)(UART1_BASE + 0x08))
  41. #define UART1_TCR ((volatile unsigned *)(UART1_BASE + 0x0c))
  42. #define UART1_MCR ((volatile unsigned *)(UART1_BASE + 0x10))
  43. #define UART1_TSR ((volatile unsigned *)(UART1_BASE + 0x14))
  44. #define UART1_MSR ((volatile unsigned *)(UART1_BASE + 0x18))
  45. #define UART1_USR ((volatile unsigned *)(UART1_BASE + 0x7c))
  46. #define CPR_RSTCTL_CTLAPB_SW ((volatile unsigned *)(CPR_BASE + 0x100))
  47. #define CPR_RSTCTL_SUBRST_SW ((volatile unsigned *)(CPR_BASE + 0x104))
  48. #define CPR_RSTCTL_M0RST_SW ((volatile unsigned *)(CPR_BASE + 0x108))
  49. #define CPR_RSTCTL_M0RST_MASK ((volatile unsigned *)(CPR_BASE + 0x10c))
  50. #define CPR_RSTCTL_LOCKUP_STATE ((volatile unsigned *)(CPR_BASE + 0x110))
  51. #define CPR_RSTCTL_WDTRST_MASK ((volatile unsigned *)(CPR_BASE + 0x114))
  52. #define CPR_LP_CTL ((volatile unsigned *)(CPR_BASE + 0x118))
  53. #define CPR_CTLAPBCLKEN_GRCTL ((volatile unsigned *)(CPR_BASE + 0x070))
  54. uHandler_callback uHandler_Callback[2] = {(uHandler_callback)0, (uHandler_callback)0};
  55. /* ---------------------------------------------------------------------------------------------------
  56. - 函数名称: Init_uart
  57. - 函数功能: UART初始化
  58. - 输入参数: 初始化串口号(0:uart0 1:uart1); 波特率
  59. - 创建日期: 2016-05-26
  60. ----------------------------------------------------------------------------------------------------*/
  61. void Init_uart(uint32_t ch , uint32_t baud)
  62. {
  63. uint32_t val;
  64. __write_hw_reg32(CPR_RSTCTL_SUBRST_SW , (1<<(16+ch))); //复位uart模块
  65. __write_hw_reg32(CPR_RSTCTL_SUBRST_SW , ((1<<(16+ch))|(1<<ch)));//解复位uart模块
  66. __read_hw_reg32(CPR_LP_CTL , val);
  67. val &= ~(1<<(1-ch));
  68. __write_hw_reg32(CPR_LP_CTL , val); //关闭uartx时钟保护
  69. val = (1<<(16+ch+4)) | (1<<(ch+4));
  70. __write_hw_reg32(CPR_CTLAPBCLKEN_GRCTL , val); //打开uartx pclk
  71. __write_hw_reg32(CPR_UARTx_CLK_GRCTL(ch), 0x110018);
  72. //__write_hw_reg32(CPR_UARTx_CLK_CTL(ch), 0x480271);
  73. __write_hw_reg32(CPR_UARTx_CLK_CTL(ch), baud>>4);
  74. __write_hw_reg32(UARTx_TCR(ch) , 0x80); //使能DLAB位
  75. //__write_hw_reg32(UARTx_DLL(ch) , baud);
  76. __write_hw_reg32(UARTx_DLL(ch) , baud&0x0F);
  77. __write_hw_reg32(UARTx_DLH(ch) , 0);
  78. __write_hw_reg32(UARTx_TCR(ch) , 0x03); //清零DLAB位,8个BIT位
  79. __write_hw_reg32(UARTx_FCR(ch) , 0xb7); //接收FIFO半满报中断, 并使能FIFO中断
  80. __write_hw_reg32(UARTx_IER(ch) , 0x01); //打开接收中断
  81. val = UART0_IRQn + ch;
  82. NVIC_EnableIRQ((IRQn_Type)val);
  83. #if 0
  84. if(ch==1) Uart_DMA_Send(1,"\nuart1 dma!\n",sizeof("\nuart1 dma!\n")-1);
  85. else Uart_DMA_Send(0,"\nuart0 dma!\n",sizeof("\nuart0 dma!\n")-1);
  86. #endif
  87. }
  88. /* ---------------------------------------------------------------------------------------------------
  89. - 函数名称: Uart_Register_Callback
  90. - 函数功能: 为uart注册中断回调函数
  91. - 输入参数: 串口号;回调函数
  92. - 创建日期: 2016-05-26
  93. ----------------------------------------------------------------------------------------------------*/
  94. void Uart_Register_Callback(uint32_t ch, uHandler_callback callback)
  95. {
  96. uHandler_Callback[ch] = callback;
  97. }
  98. /* ---------------------------------------------------------------------------------------------------
  99. - 函数名称: UART0_Handler
  100. - 函数功能: UART0中断处理函数
  101. - 输入参数: 无
  102. - 创建日期: 2016-05-26
  103. ----------------------------------------------------------------------------------------------------*/
  104. void UART0_Handler(void)
  105. {
  106. uint32_t iWK = 0 ;
  107. uint8_t tWK = 0 ;
  108. __read_hw_reg32(UART0_IIR , iWK);
  109. iWK &= 0x0F;
  110. if((iWK != 0x04) && (iWK != 0x0c)) return;
  111. __read_hw_reg32(UART0_RBR , tWK);
  112. if(uHandler_Callback[0] != (uHandler_callback)0)
  113. (uHandler_Callback[0])(tWK);
  114. }
  115. /* ---------------------------------------------------------------------------------------------------
  116. - 函数名称: UART1_Handler
  117. - 函数功能: UART1中断处理函数
  118. - 输入参数: 无
  119. - 创建日期: 2016-05-26
  120. ----------------------------------------------------------------------------------------------------*/
  121. void UART1_Handler(void)
  122. {
  123. uint32_t iWK = 0 ;
  124. uint8_t tWK = 0 ;
  125. __read_hw_reg32(UART1_IIR , iWK);
  126. iWK &= 0x0F;
  127. if((iWK != 0x04) && (iWK != 0x0c)) return;
  128. __read_hw_reg32(UART1_RBR , tWK);
  129. if(uHandler_Callback[1] != (uHandler_callback)0)
  130. (uHandler_Callback[1])(tWK);
  131. }
  132. /* ---------------------------------------------------------------------------------------------------
  133. - 函数名称: Uart_Send_Char
  134. - 函数功能: Uart发送一个字符
  135. - 输入参数: 串口号; 要发送的字符
  136. - 创建日期: 2016-05-26
  137. ----------------------------------------------------------------------------------------------------*/
  138. void Uart_Send_Char(uint32_t ch, uint8_t c)
  139. {
  140. unsigned int status;
  141. for(; ;)
  142. {
  143. __read_hw_reg32(UARTx_TSR(ch), status);
  144. status &= 0x20;
  145. if(status == 0x20) break;
  146. }
  147. __write_hw_reg32(UARTx_THR(ch), c);
  148. }
  149. /* ---------------------------------------------------------------------------------------------------
  150. - 函数名称: Uart_Send_String
  151. - 函数功能: Uart发送一个字符串
  152. - 输入参数: 串口号; 要发送的字符串
  153. - 创建日期: 2016-05-26
  154. ----------------------------------------------------------------------------------------------------*/
  155. void Uart_Send_String(uint32_t ch, uint8_t *s)
  156. {
  157. while(*s != '\0')
  158. {
  159. Uart_Send_Char(ch, *s++);
  160. }
  161. }
  162. /* ---------------------------------------------------------------------------------------------------
  163. - 函数名称: Uart_Send_Buf
  164. - 函数功能: Uart发送一个长度为length的uint8_t类型的数组
  165. - 输入参数: 串口号; 要发送的数组,数组长度
  166. - 创建日期: 2019-04-02
  167. - 作 者:陈俊伟
  168. ----------------------------------------------------------------------------------------------------*/
  169. void Uart_Send_Buf(uint32_t ch, uint8_t *s,uint32_t length)
  170. {
  171. while(length--)
  172. {
  173. Uart_Send_Char(ch, *s++);
  174. }
  175. }
  176. /* ---------------------------------------------------------------------------------------------------
  177. - 函数名称: Uart_DMA_Send
  178. - 函数功能: Uart用DMA方式发送一个长度为length的uint8_t类型的数组 --注意DMA的只能访问SHRAM0(0x10010000 ~ 0x1001FFFF)
  179. - 输入参数: 串口号 要发送的数组,数组长度
  180. - 创建日期: 2019-04-02
  181. - 作 者:陈俊伟
  182. ----------------------------------------------------------------------------------------------------*/
  183. static uint8_t __attribute__((aligned(4))) uart_tx_buf[256]={0};
  184. void Uart_DMA_Send(uint8_t ch,uint8_t *s,uint32_t length)
  185. {
  186. for(int i=0;i<length;i++)
  187. uart_tx_buf[i]=s[i];
  188. uint32_t iWK = 0;
  189. //- TX Channel
  190. __write_hw_reg32(DMAS_CHx_SAR(ch) , (uint32_t)uart_tx_buf);
  191. __write_hw_reg32(DMAS_CHx_DAR(ch) , (ch*0x1000+0x40010000));//发送地址写UART_THR
  192. __write_hw_reg32(DMAS_CHx_CTL1(ch) ,((2 << 8)));//通道优先级2
  193. __write_hw_reg32(DMAS_CHx_CTL0(ch) ,length);
  194. __write_hw_reg32(DMAS_EN , ch);
  195. do {
  196. __read_hw_reg32(DMAS_INT_RAW , iWK);
  197. }while((iWK&(1+ch)) != (1+ch));
  198. __write_hw_reg32(DMAS_INT_RAW, (1+ch));
  199. __write_hw_reg32(DMAS_CLR ,ch);
  200. }