uart.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.h
  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. #ifndef __UART_H__
  40. #define __UART_H__
  41. #include "types.h"
  42. #include "gpio.h"
  43. #define MAX_UART_BUF_SIZE 364
  44. #define MAX_UART_BUF_ID (MAX_UART_BUF_SIZE-1)
  45. #define MAX_UART_RX_SIZE_2_NOTIFY_APP (64) //1/4*MAX_UART_BUF_SIZE
  46. #define UART_IRQ_ENABLE *(volatile unsigned int *) 0xe000e100 |= BIT(11)
  47. #define UART_IRQ_DISABLE *(volatile unsigned int *) 0xe000e100 &= ~BIT(11)
  48. #define IS_RBR_DATA_READY (*(volatile unsigned int *) 0x40004014 & 0x01)
  49. #define IS_THR_DATA_EMPTY (*(volatile unsigned int *) 0x40004014 & 0x20)
  50. #define IS_TX_DATA_EMPTY (*(volatile unsigned int *) 0x40004014 & 0x40)
  51. #define IS_UART_BUSY (*(volatile unsigned int *) 0x4000407c & 0x01)
  52. #define GetIRQ_ID (*(volatile unsigned int *) 0x40004008) & 0x0f
  53. #define TX_NOT_DONE !(*(volatile unsigned int *) 0x40004014 & 0x40)
  54. #define UART_TX_FIFO_SIZE 16
  55. #define UART_RX_FIFO_SIZE 16
  56. #define TX_FIFO_MODE 1
  57. #define RX_FIFO_MODE 2
  58. #define TX_RX_FIFO_MODE 3
  59. #define FIFO_MODE 0//TX_RX_FIFO_MODE //0
  60. #define FCR_RX_TRIGGER_00 0x00
  61. #define FCR_RX_TRIGGER_01 0x40
  62. #define FCR_RX_TRIGGER_10 0x80
  63. #define FCR_RX_TRIGGER_11 0xc0
  64. #define FCR_TX_TRIGGER_00 0x00
  65. #define FCR_TX_TRIGGER_01 0x10
  66. #define FCR_TX_TRIGGER_10 0x20
  67. #define FCR_TX_TRIGGER_11 0x30
  68. #define FCR_TX_FIFO_RESET 0x04
  69. #define FCR_RX_FIFO_RESET 0x02
  70. #define FCR_FIFO_ENABLE 0x01
  71. #define IER_PTIME 0x80
  72. #define IER_EDSSI 0x08
  73. #define IER_ELSI 0x04
  74. #define IER_ETBEI 0x02
  75. #define IER_ERBFI 0x01
  76. /*LSR 0x14*/
  77. #define LSR_RFE 0x80
  78. #define LSR_TEMT 0x40
  79. #define LSR_THRE 0x20
  80. #define LSR_BI 0x10
  81. #define LSR_FE 0x08
  82. #define LSR_PE 0x04
  83. #define LSR_OE 0x02
  84. #define LSR_DR 0x01
  85. /*USR 0x7c*/
  86. #define USR_RFF 0x10
  87. #define USR_RFNE 0x08
  88. #define USR_TFE 0x04
  89. #define USR_TFNF 0x02
  90. #define USR_BUSY 0x01
  91. #define UART_FIFO_RX_TRIGGER FCR_RX_TRIGGER_10//FCR_RX_TRIGGER_10//FCR_RX_TRIGGER_11
  92. #define UART_FIFO_TX_TRIGGER FCR_TX_TRIGGER_00//FCR_TX_TRIGGER_00//FCR_TX_TRIGGER_01
  93. enum UARTIRQID{
  94. NONE_IRQ = 0,
  95. NO_IRQ_PENDING_IRQ = 1,
  96. THR_EMPTY = 2,
  97. RDA_IRQ = 4,
  98. RLS_IRQ = 6,
  99. BUSY_IRQ = 7,
  100. TIMEOUT_IRQ = 12,
  101. };
  102. enum{
  103. TX_STATE_UNINIT = 0,
  104. TX_STATE_IDLE,
  105. TX_STATE_TX,
  106. TX_STATE_ERR
  107. };
  108. typedef struct _uart_Evt_t{
  109. uint8_t type;
  110. uint8_t* data;
  111. uint8_t len;
  112. }uart_Evt_t;
  113. typedef enum{
  114. UART_EVT_TYPE_RX_DATA = 1,
  115. UART_EVT_TYPE_RX_DATA_TO, //case rx data of uart RX timeout
  116. UART_EVT_TYPE_TX_COMPLETED,
  117. } uart_Evt_Type_t;
  118. typedef void (*uart_Hdl_t)(uart_Evt_t* pev);
  119. typedef struct _uart_Cfg_t{
  120. GPIO_Pin_e tx_pin;
  121. GPIO_Pin_e rx_pin;
  122. GPIO_Pin_e rts_pin;
  123. GPIO_Pin_e cts_pin;
  124. uint32_t baudrate;
  125. bool use_fifo;
  126. bool hw_fwctrl;
  127. bool use_tx_buf;
  128. bool parity;
  129. uart_Hdl_t evt_handler;
  130. }uart_Cfg_t;
  131. typedef struct _uart_Tx_Buf_t{
  132. uint8_t tx_state;
  133. uint16_t tx_data_offset;
  134. uint16_t tx_data_size;
  135. uint16_t tx_buf_size;
  136. uint8_t* tx_buf;
  137. }uart_Tx_Buf_t;
  138. int hal_uart_deinit(void);
  139. int hal_uart_init(uart_Cfg_t cfg);
  140. int hal_uart_set_tx_buf(uint8_t* buf, uint16_t size);
  141. int hal_uart_get_tx_ready(void);
  142. int hal_uart_send_buff(uint8_t *buff,uint16_t len);
  143. int hal_uart_send_byte(unsigned char data);
  144. void __attribute__((weak)) hal_UART0_IRQHandler(void);
  145. #endif