uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file uart.c
  30. * @brief Contains all functions support for uart driver
  31. * @version 0.0
  32. * @date 19. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #include <string.h>
  40. #include "ap_cp.h"
  41. //#include "config.h"
  42. #include "uart.h"
  43. #include "gpio.h"
  44. #include "common.h"
  45. #include "OSAL.h"
  46. #include "clock.h"
  47. #include "pwrmgr.h"
  48. #include "error.h"
  49. #include "hal_defs.h"
  50. #include "hal_mcu.h"
  51. #define UART_TX_BUFFER_SIZE 256
  52. typedef struct _uart_Context{
  53. bool enable;
  54. uint8_t tx_state;
  55. uart_Tx_Buf_t tx_buf;
  56. uart_Cfg_t cfg;
  57. }uart_Ctx_t;
  58. static uart_Ctx_t m_uartCtx = {
  59. .enable = FALSE,
  60. };
  61. static int txmit_buf_use_tx_buf(uint8_t *buf,uint16_t len)
  62. {
  63. uart_Tx_Buf_t* p_txbuf = &m_uartCtx.tx_buf;
  64. uint8_t* p_data;
  65. if(len == 0 || buf == NULL)
  66. return PPlus_ERR_INVALID_PARAM;
  67. if(p_txbuf->tx_state == TX_STATE_UNINIT)
  68. return PPlus_ERR_NO_MEM;
  69. if(p_txbuf->tx_state != TX_STATE_IDLE)
  70. return PPlus_ERR_BUSY;
  71. if(p_txbuf->tx_buf_size < len)
  72. return PPlus_ERR_NO_MEM;
  73. memcpy(p_txbuf->tx_buf, buf, len);
  74. p_txbuf->tx_data_size = len;
  75. p_txbuf->tx_data_offset = 0;
  76. p_txbuf->tx_state = TX_STATE_TX;
  77. p_data = p_txbuf->tx_buf;
  78. len = p_txbuf->tx_data_size - p_txbuf->tx_data_offset;
  79. len = len > UART_TX_FIFO_SIZE ? UART_TX_FIFO_SIZE : len;
  80. AP_UART0->IER &= ~(IER_ETBEI);
  81. while(len--){
  82. AP_UART0->THR = p_data[p_txbuf->tx_data_offset++];
  83. }
  84. hal_pwrmgr_lock(MOD_UART);
  85. AP_UART0->IER |= IER_ETBEI;
  86. return PPlus_SUCCESS;
  87. }
  88. int txmit_buf_polling(uint8_t *buf,uint16_t len)
  89. {
  90. volatile int timeout=0;
  91. HAL_WAIT_CONDITION_TIMEOUT(!(AP_UART0->USR & USR_BUSY), 100000);
  92. while(len--)
  93. {
  94. HAL_WAIT_CONDITION_TIMEOUT((AP_UART0->LSR & LSR_THRE), 100000);
  95. AP_UART0->THR = *buf++;
  96. timeout=0;
  97. }
  98. //wait shift register empty
  99. HAL_WAIT_CONDITION_TIMEOUT((AP_UART0->LSR & LSR_TEMT), 100000);
  100. return PPlus_SUCCESS;;
  101. }
  102. static void irq_rx_handler(uint8_t flg)
  103. {
  104. int i;
  105. uint8_t data[UART_RX_FIFO_SIZE];
  106. uint8_t len;
  107. if(m_uartCtx.cfg.use_fifo){
  108. len = AP_UART0->RFL;
  109. for(i = 0; i< len; i++){
  110. data[i] = (uint8_t)(AP_UART0->RBR & 0xff);
  111. }
  112. }else{
  113. len = 1;
  114. AP_UART0->LSR; //clear interrupt
  115. data[0] = (uint8_t)(AP_UART0->RBR & 0xff);
  116. }
  117. if(m_uartCtx.cfg.evt_handler){
  118. uart_Evt_t evt;
  119. evt.type = flg;
  120. evt.data = data;
  121. evt.len = len;
  122. m_uartCtx.cfg.evt_handler(&evt);
  123. }
  124. }
  125. static void irq_tx_empty_handler(void)
  126. {
  127. uart_Tx_Buf_t* p_txbuf = &m_uartCtx.tx_buf;
  128. uint8_t* p_data;
  129. uint16_t len;
  130. if(m_uartCtx.enable == FALSE)
  131. return;
  132. if(m_uartCtx.cfg.use_fifo == FALSE)
  133. return;
  134. if(m_uartCtx.cfg.use_tx_buf == FALSE)
  135. return;
  136. if(p_txbuf->tx_state != TX_STATE_TX)
  137. return;
  138. p_data = p_txbuf->tx_buf;
  139. len = p_txbuf->tx_data_size - p_txbuf->tx_data_offset;
  140. len = len > UART_TX_FIFO_SIZE ? UART_TX_FIFO_SIZE : len;
  141. if(len == 0){
  142. p_txbuf->tx_state = TX_STATE_IDLE;
  143. p_txbuf->tx_data_offset = 0;
  144. p_txbuf->tx_data_size = 0;
  145. if(m_uartCtx.cfg.evt_handler){
  146. uart_Evt_t evt = {
  147. .type = UART_EVT_TYPE_TX_COMPLETED,
  148. .data = NULL,
  149. .len = 0,
  150. };
  151. m_uartCtx.cfg.evt_handler(&evt);
  152. }
  153. //when uart tx completed, release sleeping lock
  154. hal_pwrmgr_unlock(MOD_UART);
  155. return;
  156. }
  157. while(len--){
  158. AP_UART0->THR = p_data[p_txbuf->tx_data_offset++];
  159. }
  160. }
  161. static void uart_hw_config(void)
  162. {
  163. uart_Cfg_t* pcfg = &(m_uartCtx.cfg);
  164. int pclk = clk_ap_pclk();
  165. uint32_t dll;
  166. //enable clk gate
  167. clk_gate_enable(MOD_UART);
  168. clk_reset(MOD_UART);
  169. if(m_uartCtx.enable == FALSE){
  170. hal_gpio_fmux(P9, Bit_DISABLE); //set fmux(uart tx)
  171. hal_gpio_fmux(P10, Bit_DISABLE); //set fmux(uart rx)
  172. }
  173. AP_UART0->LCR =0;
  174. //temp1= pclk % (16* pcfg->baudrate);
  175. //dll = ((16*pcfg->baudrate) > 2* temp1) ? (pclk / (16*pcfg->baudrate)): (pclk / (16*pcfg->baudrate)+1);
  176. dll = ((pclk>>4)+(pcfg->baudrate>>1))/pcfg->baudrate;
  177. AP_UART0->MCR=0x0;
  178. AP_UART0->LCR=0x80;
  179. //AP_UART0->DLL=dll;
  180. AP_UART0->DLM=(dll & 0xFF00) >> 8;
  181. AP_UART0->DLL=(dll & 0xFF);
  182. // AP_UART0->LCR = 0x3; //8bit, 1 stop no parity
  183. if(pcfg->parity)
  184. AP_UART0->LCR = 0x1b; //8bit, 1 stop even parity
  185. else
  186. AP_UART0->LCR = 0x3; //8bit, 1 stop no parity
  187. //set fifo
  188. if(pcfg->use_fifo)
  189. {
  190. //enable tx FIFO mode(empty trigger), rx FIFO mode(1/2 trigger)
  191. AP_UART0->FCR= FCR_TX_FIFO_RESET|FCR_RX_FIFO_RESET|FCR_FIFO_ENABLE|UART_FIFO_RX_TRIGGER|UART_FIFO_TX_TRIGGER;
  192. }
  193. else
  194. {
  195. AP_UART0->FCR=0;
  196. }
  197. //enable Received Data Available Interrupt
  198. AP_UART0->IER = IER_ERBFI;
  199. if(pcfg->use_fifo)
  200. AP_UART0->IER |= IER_PTIME;
  201. if(pcfg->use_tx_buf)
  202. AP_UART0->IER |= IER_ETBEI;
  203. //enable uart irq
  204. NVIC_EnableIRQ((IRQn_Type)UART_IRQ);
  205. NVIC_SetPriority((IRQn_Type)UART_IRQ, IRQ_PRIO_HAL);
  206. hal_gpio_fmux_set(pcfg->tx_pin, UART_TX); //set fmux(uart tx)
  207. hal_gpio_fmux_set(pcfg->rx_pin, UART_RX); //set fmux(uart rx)
  208. }
  209. static int uart_hw_deinit(void)
  210. {
  211. AP_UART0->LCR=0x80;
  212. AP_UART0->DLM=0;
  213. AP_UART0->DLL=0;
  214. AP_UART0->LCR =0;
  215. AP_UART0->FCR=0;
  216. AP_UART0->IER = 0;
  217. NVIC_DisableIRQ((IRQn_Type)UART_IRQ);
  218. hal_gpio_fmux(m_uartCtx.cfg.tx_pin,Bit_DISABLE);
  219. hal_gpio_fmux(m_uartCtx.cfg.rx_pin,Bit_DISABLE);
  220. clk_reset(MOD_UART);
  221. clk_gate_disable(MOD_UART);
  222. return PPlus_SUCCESS;
  223. }
  224. /**************************************************************************************
  225. * @fn hal_UART0_IRQHandler
  226. *
  227. * @brief This function process for uart interrupt
  228. *
  229. * input parameters
  230. *
  231. * @param None.
  232. *
  233. * output parameters
  234. *
  235. * @param None.
  236. *
  237. * @return None.
  238. **************************************************************************************/
  239. void __attribute__((used)) hal_UART0_IRQHandler(void)
  240. {
  241. uint8_t IRQ_ID=GetIRQ_ID;
  242. switch(IRQ_ID)
  243. {
  244. case TIMEOUT_IRQ:
  245. irq_rx_handler(UART_EVT_TYPE_RX_DATA_TO);
  246. break;
  247. case RDA_IRQ:
  248. irq_rx_handler(UART_EVT_TYPE_RX_DATA);
  249. break;
  250. case THR_EMPTY:
  251. irq_tx_empty_handler();
  252. break;
  253. case RLS_IRQ:
  254. break;
  255. case BUSY_IRQ:
  256. VOID AP_UART0->USR;
  257. break;
  258. }
  259. }
  260. int hal_uart_deinit(void)
  261. {
  262. uart_hw_deinit();
  263. memset(&m_uartCtx, 0, sizeof(m_uartCtx));
  264. hal_pwrmgr_unregister(MOD_UART);
  265. return PPlus_SUCCESS;
  266. }
  267. int hal_uart_init(uart_Cfg_t cfg)
  268. {
  269. if(m_uartCtx.enable)
  270. return PPlus_ERR_BUSY;
  271. memset(&m_uartCtx, 0, sizeof(m_uartCtx));
  272. // if(cfg.hw_fwctrl || cfg.parity)
  273. // return PPlus_ERR_NOT_SUPPORTED;
  274. if(cfg.hw_fwctrl)
  275. return PPlus_ERR_NOT_SUPPORTED;
  276. m_uartCtx.cfg = cfg;
  277. uart_hw_config();
  278. m_uartCtx.enable = TRUE;
  279. hal_pwrmgr_register(MOD_UART, NULL, uart_hw_config);
  280. return PPlus_SUCCESS;
  281. }
  282. int hal_uart_set_tx_buf(uint8_t* buf, uint16_t size)
  283. {
  284. uart_Tx_Buf_t* p_txbuf = &m_uartCtx.tx_buf;
  285. if(m_uartCtx.enable == FALSE)
  286. return PPlus_ERR_INVALID_STATE;
  287. if(m_uartCtx.cfg.use_tx_buf == FALSE)
  288. return PPlus_ERR_NOT_SUPPORTED;
  289. if(p_txbuf->tx_state != TX_STATE_UNINIT)
  290. return PPlus_ERR_INVALID_STATE;
  291. HAL_ENTER_CRITICAL_SECTION();
  292. p_txbuf->tx_buf = buf;
  293. p_txbuf->tx_buf_size = size;
  294. p_txbuf->tx_data_offset = 0;
  295. p_txbuf->tx_data_size= 0;
  296. p_txbuf->tx_state = TX_STATE_IDLE;
  297. HAL_EXIT_CRITICAL_SECTION();
  298. return PPlus_SUCCESS;
  299. }
  300. int hal_uart_get_tx_ready(void)
  301. {
  302. if(m_uartCtx.cfg.use_tx_buf == FALSE)
  303. return PPlus_SUCCESS;
  304. if(m_uartCtx.tx_buf.tx_state == TX_STATE_IDLE)
  305. return PPlus_SUCCESS;
  306. return PPlus_ERR_BUSY;
  307. }
  308. int hal_uart_send_buff(uint8_t *buff,uint16_t len)
  309. {
  310. if(m_uartCtx.cfg.use_tx_buf){
  311. return txmit_buf_use_tx_buf(buff,len);
  312. }
  313. return txmit_buf_polling(buff,len);
  314. }
  315. int hal_uart_send_byte(unsigned char data)
  316. {
  317. HAL_WAIT_CONDITION_TIMEOUT((AP_UART0->LSR & LSR_THRE), 10000);
  318. AP_UART0->THR=data;
  319. HAL_WAIT_CONDITION_TIMEOUT((AP_UART0->LSR & LSR_TEMT), 10000);
  320. return PPlus_SUCCESS;
  321. }