uart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef __UART_H__
  2. #define __UART_H__
  3. #define BAUD_RATE 9600
  4. /*
  5. UARTAn control register 0 (UAnCTL0)
  6. */
  7. #define _10_UARTA_UAnCTL0_INITIALVALUE 0x10U
  8. /* UARTAn operation control (UAnPWR) */
  9. #define _00_UARTA_OPERATION_DISABLE 0x00U /* disable UARTAn operation (UARTAn reset asynchronously) */
  10. #define _80_UARTA_OPERATION_ENABLE 0x80U /* enable UARTAn operation */
  11. /* Transmission operation enable (UAnTXE) */
  12. #define _00_UARTA_TRANSMISSION_DISABLE 0x00U /* disable transmission operation */
  13. #define _40_UARTA_TRANSMISSION_ENABLE 0x40U /* enable transmission operation */
  14. /* Reception operation enable (UAnRXE) */
  15. #define _00_UARTA_RECEPTION_DISABLE 0x00U /* disable reception operation */
  16. #define _20_UARTA_RECEPTION_ENABLE 0x20U /* enable reception operation */
  17. /* Transfer direction selection (UAnDIR) */
  18. #define _00_UARTA_TRANSFDIR_MSB 0x00U /* MSB-first transfer */
  19. #define _10_UARTA_TRANSFDIR_LSB 0x10U /* LSB-first transfer */
  20. /* Parity selection during transmission/reception (UAnPS1,UAnPS0) */
  21. #define _00_UARTA_PARITY_NONE 0x00U /* no parity output/reception with no parity */
  22. #define _04_UARTA_PARITY_ZREO 0x04U /* 0 parity output/reception with 0 parity */
  23. #define _08_UARTA_PARITY_ODD 0x08U /* odd parity output/odd parity check */
  24. #define _0C_UARTA_PARITY_EVEN 0x0CU /* even parity output/even parity check */
  25. /* Specification of data character length of 1 frame of transmit/receive data (UAnCL) */
  26. #define _00_UARTA_DATALENGTH_7BIT 0x00U /* 7 bits */
  27. #define _02_UARTA_DATALENGTH_8BIT 0x02U /* 8 bits */
  28. /* Specification of length of stop bit for transmit data (UAnSL) */
  29. #define _00_UARTA_STOPLENGTH_1BIT 0x00U /* 1 bit */
  30. #define _01_UARTA_STOPLENGTH_2BIT 0x01U /* 2 bits */
  31. /*
  32. UARTAn base clock selects register (UAnCTL1)
  33. */
  34. /* UAnCTL1 register (UAnCKS3 - UAnCKS0) */
  35. #define _00_UARTA_BASECLK_FXX_2 0x00U /* fXX/2 */
  36. #define _01_UARTA_BASECLK_FXX_4 0x01U /* fXX/2^2 */
  37. #define _02_UARTA_BASECLK_FXX_8 0x02U /* fXX/2^3 */
  38. #define _03_UARTA_BASECLK_FXX_16 0x03U /* fXX/2^4 */
  39. #define _04_UARTA_BASECLK_FXX_32 0x04U /* fXX/2^5 */
  40. #define _05_UARTA_BASECLK_FXX_64 0x05U /* fXX/2^6 */
  41. #define _06_UARTA_BASECLK_FXX_128 0x06U /* fXX/2^7 */
  42. #define _07_UARTA_BASECLK_FXX_256 0x07U /* fXX/2^8 */
  43. #define _08_UARTA_BASECLK_FXX_512 0x08U /* fXX/2^9 */
  44. #define _09_UARTA_BASECLK_FXX_1024 0x09U /* fXX/2^10 */
  45. #define _0A_UARTA_BASECLK_FXX_2048 0x0AU /* fXX/2^11 */
  46. #define _0B_UARTA_BASECLK_FXX_4096 0x0BU /* fXX/2^12 */
  47. /*
  48. UARTAn option control register 0 (UAnOPT0)
  49. */
  50. #define _14_UARTA_UAnOPT0_INITIALVALUE 0x14U
  51. /* Transmit data level bit(UAnTDL) */
  52. #define _00_UARTA_TRAN_DATALEVEL_NORMAL 0x00U /* normal output of transfer data */
  53. #define _02_UARTA_TRAN_DATALEVEL_INVERTED 0x02U /* inverted output of transfer data */
  54. /* Receive data level bit(UAnRDL) */
  55. #define _00_UARTA_REC_DATALEVEL_NORMAL 0x00U /* normal input of transfer data */
  56. #define _01_UARTA_REC_DATALEVEL_INVERTED 0x01U /* inverted input of transfer data */
  57. /*
  58. CSIBn control register 0 (CBnCTL0)
  59. */
  60. /* Specification of CSIBn operation disable/enable (CBnPWR)*/
  61. #define _00_CSIB_OPERATION_DISABLE 0x00U /* disable CSIBn operation and reset the CBnSTR register */
  62. #define _80_CSIB_OPERATION_ENABLE 0x80U /* enable CSIBn operation */
  63. /* Specification of transmit operation disable/enable (CBnTXE)*/
  64. #define _00_CSIB_TRANSMIT_DISABLE 0x00U /* disable transmit operation */
  65. #define _40_CSIB_TRANSMIT_ENABLE 0x40U /* enable transmit operation */
  66. /* Specification of receive operation disable/enable (CBnRXE)*/
  67. #define _00_CSIB_RECEIVE_DISABLE 0x00U /* disable receive operation */
  68. #define _20_CSIB_RECEIVE_ENABLE 0x20U /* enable receive operation */
  69. /* Specification of transfer direction mode (MSB/LSB) (CBnDIR) */
  70. #define _00_CSIB_DATA_MSB 0x00U /* MSB first */
  71. #define _10_CSIB_DATA_LSB 0x10U /* LSB first */
  72. /* Transfer mode specification (CBnTMS) */
  73. #define _00_CSIB_MODE_SINGLE 0x00U /* single transfer mode */
  74. #define _02_CSIB_MODE_CONTINUOUS 0x02U /* continuous transfer mode */
  75. /* Specification of start transfer disable/enable (CBnSCE) */
  76. #define _00_CSIB_STARTTRG_INVALID 0x00U /* communication start trigger invalid */
  77. #define _01_CSIB_STARTTRG_VALID 0x01U /* communication start trigger valid */
  78. /*
  79. CSIBn control register 1 (CBnCTL1)
  80. */
  81. /* Specification of data transmission/reception timing in relation to SCKBn (CBnCKP, CBnDAP) */
  82. #define _00_CSIB_DATA_TIMING1 0x00U /* communication type 1 */
  83. #define _08_CSIB_DATA_TIMING2 0x08U /* communication type 2 */
  84. #define _10_CSIB_DATA_TIMING3 0x10U /* communication type 3 */
  85. #define _18_CSIB_DATA_TIMING4 0x18U /* communication type 4 */
  86. /* Specification of input clock (CBnCKS2 - CBnCKS0) */
  87. #define _00_CSIB_CLOCK_1 0x00U /* fXX /2 */
  88. #define _01_CSIB_CLOCK_2 0x01U /* fXX/4 */
  89. #define _02_CSIB_CLOCK_3 0x02U /* fXX /8 */
  90. #define _03_CSIB_CLOCK_4 0x03U /* fXX /16 */
  91. #define _04_CSIB_CLOCK_5 0x04U /* fXX /32 */
  92. #define _05_CSIB_CLOCK_6 0x05U /* fXX /64 */
  93. #define _06_CSIB_CLOCK_7 0x06U /* fBRGm */
  94. #define _07_CSIB_CLOCK_EXT 0x07U /* external clock SCKBn */
  95. /*
  96. CSIBn control register 2 (CBnCTL2)
  97. */
  98. /* Serial register bit length (CBnCL3,CBnCL2,CBnCL1,CBnCL0) */
  99. #define _00_CSIB_DATA_LENGTH_8 0x00U /* 8 bits */
  100. #define _01_CSIB_DATA_LENGTH_9 0x01U /* 9 bits */
  101. #define _02_CSIB_DATA_LENGTH_10 0x02U /* 10 bits */
  102. #define _03_CSIB_DATA_LENGTH_11 0x03U /* 11 bits */
  103. #define _04_CSIB_DATA_LENGTH_12 0x04U /* 12 bits */
  104. #define _05_CSIB_DATA_LENGTH_13 0x05U /* 13 bits */
  105. #define _06_CSIB_DATA_LENGTH_14 0x06U /* 14 bits */
  106. #define _07_CSIB_DATA_LENGTH_15 0x07U /* 15 bits */
  107. #define _08_CSIB_DATA_LENGTH_16 0x08U /* 16 bits */
  108. /*
  109. CSIBn status register (CBnSTR)
  110. */
  111. /* Communication status flag (CBnTSF) */
  112. #define _00_CSIB_COMMUNICATION_STOP 0x00U /* communication stopped */
  113. #define _80_CSIB_COMMUNICATING 0x80U /* communicating */
  114. /* Overrun error flag (CBnOVE) */
  115. #define _00_CSIB_OVERRUN_NONE 0x00U /* no overrun */
  116. #define _01_CSIB_OVERRUN 0x01U /* overrun */
  117. /*
  118. BRGm prescaler mode registers (PRSMm)
  119. */
  120. /* Baud rate output(BGCEm) */
  121. #define _00_CSIB_FBRGM_DISABLE 0x00U /* baudrate output disabled */
  122. #define _10_CSIB_FBRGM_ENABLE 0x10U /* baudrate output enabled */
  123. /* Input clock selection (BGCSm1,BGCSm0) */
  124. #define _00_CSIB_FBGCS_0 0x00U /* fXX */
  125. #define _01_CSIB_FBGCS_1 0x01U /* fXX/2 */
  126. #define _02_CSIB_FBGCS_2 0x02U /* fXX/4 */
  127. #define _03_CSIB_FBGCS_3 0x03U /* fXX/8 */
  128. #define CB4RIC UA0RIC
  129. #define CB4TIC UA0TIC
  130. #define CB0RIC IICIC1
  131. /*
  132. IIC control register (IICCn)
  133. */
  134. /* IIC operation enable (IICEn) */
  135. #define _80_IIC_OPERATION 0x80U
  136. #define _00_IIC_OPERATION_DISABLE 0x00U /* stop operation */
  137. #define _80_IIC_OPERATION_ENABLE 0x80U /* enable operation */
  138. /* Exit from communications (LRELn) */
  139. #define _40_IIC_COMMUNICATION 0x40U
  140. #define _00_IIC_COMMUNICATION_NORMAL 0x00U /* normal operation */
  141. #define _40_IIC_COMMUNICATION_EXIT 0x40U /* exit from current communication */
  142. /* Wait cancellation (WRELn) */
  143. #define _20_IIC_WAITCANCEL 0x20U
  144. #define _00_IIC_WAIT_NOTCANCEL 0x00U /* do not cancel wait */
  145. #define _20_IIC_WAIT_CANCEL 0x20U /* cancel wait */
  146. /* Generation of interrupt when stop condition (SPIEn) */
  147. #define _10_IIC_STOPINT 0x10U
  148. #define _00_IIC_STOPINT_DISABLE 0x00U /* disable */
  149. #define _10_IIC_STOPINT_ENABLE 0x10U /* enable */
  150. /* Wait and interrupt generation (WTIMn) */
  151. #define _08_IIC_WAITINT 0x08U
  152. #define _00_IIC_WAITINT_CLK8FALLING 0x00U /* generate at the eighth clock falling edge */
  153. #define _08_IIC_WAITINT_CLK9FALLING 0x08U /* generated at the ninth clock falling edge */
  154. /* Acknowledgement control (ACKEn) */
  155. #define _04_IIC_ACK 0x04
  156. #define _00_IIC_ACK_DISABLE 0x00U /* disable acknowledgement */
  157. #define _04_IIC_ACK_ENABLE 0x04U /* enable acknowledgement */
  158. /* Start condition trigger (STTn) */
  159. #define _02_IIC_STARTCONDITION 0x02U
  160. #define _00_IIC_START_NOTGENERATE 0x00U /* do not generate start condition */
  161. #define _02_IIC_START_GENERATE 0x02U /* generate start condition */
  162. /* Stop condition trigger (SPTn) */
  163. #define _01_IIC_STOPCONDITION 0x01U
  164. #define _00_IIC_STOP_NOTGENERATE 0x00U /* do not generate stop condition */
  165. #define _01_IIC_STOP_GENERATE 0x01U /* generate stop condition */
  166. /*
  167. IIC Status Register (IICSn)
  168. */
  169. /* Master device status (MSTSn) */
  170. #define _80_IIC_MASTERSTATUS 0x80U
  171. #define _00_IIC_STATUS_NOTMASTER 0x00U /* slave device status or communication standby status */
  172. #define _80_IIC_STATUS_MASTER 0x80U /* master device communication status */
  173. /* Detection of arbitration loss (ALDn) */
  174. #define _40_IIC_ARBITRATION 0x40U
  175. #define _00_IIC_ARBITRATION_NO 0x00U /* arbitration win or no arbitration */
  176. #define _40_IIC_ARBITRATION_LOSS 0x40U /* arbitration loss */
  177. /* Detection of extension code reception (EXCn) */
  178. #define _20_IIC_EXTENSIONCODE 0x20U
  179. #define _00_IIC_EXTCODE_NOT 0x00U /* extension code not received */
  180. #define _20_IIC_EXTCODE_RECEIVED 0x20U /* extension code received */
  181. /* Detection of matching addresses (COIn) */
  182. #define _10_IIC_ADDRESSMATCH 0x10U
  183. #define _00_IIC_ADDRESS_NOTMATCH 0x00U /* addresses do not match */
  184. #define _10_IIC_ADDRESS_MATCH 0x10U /* addresses match */
  185. /* Detection of transmit/receive status (TRCn) */
  186. #define _08_IIC_STATUS 0x08U
  187. #define _00_IIC_STATUS_RECEIVE 0x00U /* receive status */
  188. #define _08_IIC_STATUS_TRANSMIT 0x08U /* transmit status */
  189. /* Detection of acknowledge signal (ACKDn) */
  190. #define _04_IIC_ACKDETECTION 0x04U
  191. #define _00_IIC_ACK_NOTDETECTED 0x00U /* ACK signal was not detected */
  192. #define _04_IIC_ACK_DETECTED 0x04U /* ACK signal was detected */
  193. /* Detection of start condition (STDn) */
  194. #define _02_IIC_STARTDETECTION 0x02U
  195. #define _00_IIC_START_NOTDETECTED 0x00U /* start condition not detected */
  196. #define _02_IIC_START_DETECTED 0x02U /* start condition detected */
  197. /* Detection of stop condition (SPDn) */
  198. #define _01_IIC_STOPDETECTION 0x01U
  199. #define _00_IIC_STOP_NOTDETECTED 0x00U /* stop condition not detected */
  200. #define _01_IIC_STOP_DETECTED 0x01U /* stop condition detected */
  201. /*
  202. IIC Flag Register (IICFn)
  203. */
  204. /* STTn clear flag (STCFn) */
  205. #define _80_IIC_STARTFLAG 0x80U
  206. #define _00_IIC_STARTFLAG_GENERATE 0x00U /* generate start condition */
  207. #define _80_IIC_STARTFLAG_UNSUCCESSFUL 0x80U /* start condition generation unsuccessful */
  208. /* IIC bus status flag (IICBSYn) */
  209. #define _40_IIC_BUSSTATUS 0x40U
  210. #define _00_IIC_BUS_RELEASE 0x00U /* bus release status */
  211. #define _40_IIC_BUS_COMMUNICATION 0x40U /* bus communication status */
  212. /* Initial start enable trigger (STCENn) */
  213. #define _02_IIC_STARTWITHSTOP 0x02U
  214. #define _00_IIC_START_WITHSTOP 0x00U /* generation of a start condition upon detection of a stop condition */
  215. #define _02_IIC_START_WITHOUTSTOP 0x02U /* generation of a start condition without detecting a stop condition */
  216. /* Communication reservation function disable bit (IICRSVn) */
  217. #define _01_IIC_RESERVATION 0x01U
  218. #define _00_IIC_RESERVATION_ENABLE 0x00U /* enable communication reservation */
  219. #define _01_IIC_RESERVATION_DISABLE 0x01U /* disable communication reservation */
  220. /*
  221. IIC clock selection register (IICCLn)
  222. */
  223. #define _00_IICCL_INITIALVALUE 0x00U
  224. /* Detection of SCL0n pin level (CLDn) */
  225. #define _20_IIC_SCLLEVEL 0x20U
  226. #define _00_IIC_SCL_LOW 0x00U /* clock line at low level */
  227. #define _20_IIC_SCL_HIGH 0x20U /* clock line at high level */
  228. /* Detection of SDA0 pin level (DADn) */
  229. #define _10_IIC_SDALEVEL 0x10U
  230. #define _00_IIC_SDA_LOW 0x00U /* data line at low level */
  231. #define _10_IIC_SDA_HIGH 0x10U /* data line at high level */
  232. /* Operation mode switching (SMCn) */
  233. #define _08_IIC_OPERATIONMODE 0x08U
  234. #define _00_IIC_MODE_STANDARD 0x00U /* operates in standard mode */
  235. #define _08_IIC_MODE_HIGHSPEED 0x08U /* operates in high-speed mode */
  236. /* Digital filter operation control (DFCn) */
  237. #define _04_IIC_DIGITALFILTER 0x04U
  238. #define _00_IIC_FILTER_OFF 0x00U /* digital filter off */
  239. #define _04_IIC_FILTER_ON 0x04U /* digital filter on */
  240. /* Operation mode switching (CLn1, CLn0) */
  241. #define _03_IIC_CLOCKSELECTION 0x03U
  242. /* Combine of (CLn1, CLn0)*/
  243. #define _00_IIC_CLOCK0 0x00U
  244. #define _01_IIC_CLOCK1 0x01U
  245. #define _02_IIC_CLOCK2 0x02U
  246. #define _03_IIC_CLOCK3 0x03U
  247. /*
  248. IIC division clock select register (OCKSn)
  249. */
  250. #define _10_IIC_SELECTED0 0x10U
  251. #define _11_IIC_SELECTED1 0x11U
  252. #define _12_IIC_SELECTED2 0x12U
  253. #define _13_IIC_SELECTED3 0x13U
  254. #define _18_IIC_SELECTED4 0x18U
  255. /*
  256. IIC function expansion register 0 (IICXn)
  257. */
  258. /* IIC clock expension (CLXn) */
  259. #define _01_IIC_CLOCKEXPENSION 0x01U
  260. #define _00_IIC_EXPENSION0 0x00U
  261. #define _01_IIC_EXPENSION1 0x01U
  262. #define _80_ADDRESS_COMPLETE 0x80U
  263. #define _00_IIC_MASTER_FLAG_CLEAR 0x00U
  264. #define IICIC2 UA1RIC
  265. #define IICIC0 UA2RIC
  266. /*
  267. *******************************************************************************
  268. ** Macro define
  269. *******************************************************************************
  270. */
  271. /* Selection of 8-bit counter output clock (UA1BRS7~UA1BRS0) */
  272. #define _D0_UARTA1_BASECLK_DIVISION 0xD0U /* 4 ~ 255 */ //9600
  273. #define _11_UARTA1_BASECLK_DIVISION 0x11U /* 4 ~ 255 */ //115200
  274. enum TransferMode
  275. {
  276. SEND, RECEIVE
  277. };
  278. void rt_hw_uart_init(void);
  279. #endif