gd32f30x_can.h 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*!
  2. \file gd32f30x_can.h
  3. \brief definitions for the CAN
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2019-11-27, V2.0.1, firmware for GD32F30x
  8. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  9. */
  10. /*
  11. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #ifndef GD32F30X_CAN_H
  34. #define GD32F30X_CAN_H
  35. #include "gd32f30x.h"
  36. /* CAN definitions */
  37. #define CAN0 CAN_BASE /*!< CAN0 base address */
  38. #define CAN1 (CAN0 + 0x00000400U) /*!< CAN1 base address */
  39. /* registers definitions */
  40. #define CAN_CTL(canx) REG32((canx) + 0x00000000U) /*!< CAN control register */
  41. #define CAN_STAT(canx) REG32((canx) + 0x00000004U) /*!< CAN status register */
  42. #define CAN_TSTAT(canx) REG32((canx) + 0x00000008U) /*!< CAN transmit status register*/
  43. #define CAN_RFIFO0(canx) REG32((canx) + 0x0000000CU) /*!< CAN receive FIFO0 register */
  44. #define CAN_RFIFO1(canx) REG32((canx) + 0x00000010U) /*!< CAN receive FIFO1 register */
  45. #define CAN_INTEN(canx) REG32((canx) + 0x00000014U) /*!< CAN interrupt enable register */
  46. #define CAN_ERR(canx) REG32((canx) + 0x00000018U) /*!< CAN error register */
  47. #define CAN_BT(canx) REG32((canx) + 0x0000001CU) /*!< CAN bit timing register */
  48. #define CAN_TMI0(canx) REG32((canx) + 0x00000180U) /*!< CAN transmit mailbox0 identifier register */
  49. #define CAN_TMP0(canx) REG32((canx) + 0x00000184U) /*!< CAN transmit mailbox0 property register */
  50. #define CAN_TMDATA00(canx) REG32((canx) + 0x00000188U) /*!< CAN transmit mailbox0 data0 register */
  51. #define CAN_TMDATA10(canx) REG32((canx) + 0x0000018CU) /*!< CAN transmit mailbox0 data1 register */
  52. #define CAN_TMI1(canx) REG32((canx) + 0x00000190U) /*!< CAN transmit mailbox1 identifier register */
  53. #define CAN_TMP1(canx) REG32((canx) + 0x00000194U) /*!< CAN transmit mailbox1 property register */
  54. #define CAN_TMDATA01(canx) REG32((canx) + 0x00000198U) /*!< CAN transmit mailbox1 data0 register */
  55. #define CAN_TMDATA11(canx) REG32((canx) + 0x0000019CU) /*!< CAN transmit mailbox1 data1 register */
  56. #define CAN_TMI2(canx) REG32((canx) + 0x000001A0U) /*!< CAN transmit mailbox2 identifier register */
  57. #define CAN_TMP2(canx) REG32((canx) + 0x000001A4U) /*!< CAN transmit mailbox2 property register */
  58. #define CAN_TMDATA02(canx) REG32((canx) + 0x000001A8U) /*!< CAN transmit mailbox2 data0 register */
  59. #define CAN_TMDATA12(canx) REG32((canx) + 0x000001ACU) /*!< CAN transmit mailbox2 data1 register */
  60. #define CAN_RFIFOMI0(canx) REG32((canx) + 0x000001B0U) /*!< CAN receive FIFO0 mailbox identifier register */
  61. #define CAN_RFIFOMP0(canx) REG32((canx) + 0x000001B4U) /*!< CAN receive FIFO0 mailbox property register */
  62. #define CAN_RFIFOMDATA00(canx) REG32((canx) + 0x000001B8U) /*!< CAN receive FIFO0 mailbox data0 register */
  63. #define CAN_RFIFOMDATA10(canx) REG32((canx) + 0x000001BCU) /*!< CAN receive FIFO0 mailbox data1 register */
  64. #define CAN_RFIFOMI1(canx) REG32((canx) + 0x000001C0U) /*!< CAN receive FIFO1 mailbox identifier register */
  65. #define CAN_RFIFOMP1(canx) REG32((canx) + 0x000001C4U) /*!< CAN receive FIFO1 mailbox property register */
  66. #define CAN_RFIFOMDATA01(canx) REG32((canx) + 0x000001C8U) /*!< CAN receive FIFO1 mailbox data0 register */
  67. #define CAN_RFIFOMDATA11(canx) REG32((canx) + 0x000001CCU) /*!< CAN receive FIFO1 mailbox data1 register */
  68. #define CAN_FCTL(canx) REG32((canx) + 0x00000200U) /*!< CAN filter control register */
  69. #define CAN_FMCFG(canx) REG32((canx) + 0x00000204U) /*!< CAN filter mode register */
  70. #define CAN_FSCFG(canx) REG32((canx) + 0x0000020CU) /*!< CAN filter scale register */
  71. #define CAN_FAFIFO(canx) REG32((canx) + 0x00000214U) /*!< CAN filter associated FIFO register */
  72. #define CAN_FW(canx) REG32((canx) + 0x0000021CU) /*!< CAN filter working register */
  73. #define CAN_F0DATA0(canx) REG32((canx) + 0x00000240U) /*!< CAN filter 0 data 0 register */
  74. #define CAN_F1DATA0(canx) REG32((canx) + 0x00000248U) /*!< CAN filter 1 data 0 register */
  75. #define CAN_F2DATA0(canx) REG32((canx) + 0x00000250U) /*!< CAN filter 2 data 0 register */
  76. #define CAN_F3DATA0(canx) REG32((canx) + 0x00000258U) /*!< CAN filter 3 data 0 register */
  77. #define CAN_F4DATA0(canx) REG32((canx) + 0x00000260U) /*!< CAN filter 4 data 0 register */
  78. #define CAN_F5DATA0(canx) REG32((canx) + 0x00000268U) /*!< CAN filter 5 data 0 register */
  79. #define CAN_F6DATA0(canx) REG32((canx) + 0x00000270U) /*!< CAN filter 6 data 0 register */
  80. #define CAN_F7DATA0(canx) REG32((canx) + 0x00000278U) /*!< CAN filter 7 data 0 register */
  81. #define CAN_F8DATA0(canx) REG32((canx) + 0x00000280U) /*!< CAN filter 8 data 0 register */
  82. #define CAN_F9DATA0(canx) REG32((canx) + 0x00000288U) /*!< CAN filter 9 data 0 register */
  83. #define CAN_F10DATA0(canx) REG32((canx) + 0x00000290U) /*!< CAN filter 10 data 0 register */
  84. #define CAN_F11DATA0(canx) REG32((canx) + 0x00000298U) /*!< CAN filter 11 data 0 register */
  85. #define CAN_F12DATA0(canx) REG32((canx) + 0x000002A0U) /*!< CAN filter 12 data 0 register */
  86. #define CAN_F13DATA0(canx) REG32((canx) + 0x000002A8U) /*!< CAN filter 13 data 0 register */
  87. #define CAN_F14DATA0(canx) REG32((canx) + 0x000002B0U) /*!< CAN filter 14 data 0 register */
  88. #define CAN_F15DATA0(canx) REG32((canx) + 0x000002B8U) /*!< CAN filter 15 data 0 register */
  89. #define CAN_F16DATA0(canx) REG32((canx) + 0x000002C0U) /*!< CAN filter 16 data 0 register */
  90. #define CAN_F17DATA0(canx) REG32((canx) + 0x000002C8U) /*!< CAN filter 17 data 0 register */
  91. #define CAN_F18DATA0(canx) REG32((canx) + 0x000002D0U) /*!< CAN filter 18 data 0 register */
  92. #define CAN_F19DATA0(canx) REG32((canx) + 0x000002D8U) /*!< CAN filter 19 data 0 register */
  93. #define CAN_F20DATA0(canx) REG32((canx) + 0x000002E0U) /*!< CAN filter 20 data 0 register */
  94. #define CAN_F21DATA0(canx) REG32((canx) + 0x000002E8U) /*!< CAN filter 21 data 0 register */
  95. #define CAN_F22DATA0(canx) REG32((canx) + 0x000002F0U) /*!< CAN filter 22 data 0 register */
  96. #define CAN_F23DATA0(canx) REG32((canx) + 0x000003F8U) /*!< CAN filter 23 data 0 register */
  97. #define CAN_F24DATA0(canx) REG32((canx) + 0x00000300U) /*!< CAN filter 24 data 0 register */
  98. #define CAN_F25DATA0(canx) REG32((canx) + 0x00000308U) /*!< CAN filter 25 data 0 register */
  99. #define CAN_F26DATA0(canx) REG32((canx) + 0x00000310U) /*!< CAN filter 26 data 0 register */
  100. #define CAN_F27DATA0(canx) REG32((canx) + 0x00000318U) /*!< CAN filter 27 data 0 register */
  101. #define CAN_F0DATA1(canx) REG32((canx) + 0x00000244U) /*!< CAN filter 0 data 1 register */
  102. #define CAN_F1DATA1(canx) REG32((canx) + 0x0000024CU) /*!< CAN filter 1 data 1 register */
  103. #define CAN_F2DATA1(canx) REG32((canx) + 0x00000254U) /*!< CAN filter 2 data 1 register */
  104. #define CAN_F3DATA1(canx) REG32((canx) + 0x0000025CU) /*!< CAN filter 3 data 1 register */
  105. #define CAN_F4DATA1(canx) REG32((canx) + 0x00000264U) /*!< CAN filter 4 data 1 register */
  106. #define CAN_F5DATA1(canx) REG32((canx) + 0x0000026CU) /*!< CAN filter 5 data 1 register */
  107. #define CAN_F6DATA1(canx) REG32((canx) + 0x00000274U) /*!< CAN filter 6 data 1 register */
  108. #define CAN_F7DATA1(canx) REG32((canx) + 0x0000027CU) /*!< CAN filter 7 data 1 register */
  109. #define CAN_F8DATA1(canx) REG32((canx) + 0x00000284U) /*!< CAN filter 8 data 1 register */
  110. #define CAN_F9DATA1(canx) REG32((canx) + 0x0000028CU) /*!< CAN filter 9 data 1 register */
  111. #define CAN_F10DATA1(canx) REG32((canx) + 0x00000294U) /*!< CAN filter 10 data 1 register */
  112. #define CAN_F11DATA1(canx) REG32((canx) + 0x0000029CU) /*!< CAN filter 11 data 1 register */
  113. #define CAN_F12DATA1(canx) REG32((canx) + 0x000002A4U) /*!< CAN filter 12 data 1 register */
  114. #define CAN_F13DATA1(canx) REG32((canx) + 0x000002ACU) /*!< CAN filter 13 data 1 register */
  115. #define CAN_F14DATA1(canx) REG32((canx) + 0x000002B4U) /*!< CAN filter 14 data 1 register */
  116. #define CAN_F15DATA1(canx) REG32((canx) + 0x000002BCU) /*!< CAN filter 15 data 1 register */
  117. #define CAN_F16DATA1(canx) REG32((canx) + 0x000002C4U) /*!< CAN filter 16 data 1 register */
  118. #define CAN_F17DATA1(canx) REG32((canx) + 0x0000024CU) /*!< CAN filter 17 data 1 register */
  119. #define CAN_F18DATA1(canx) REG32((canx) + 0x000002D4U) /*!< CAN filter 18 data 1 register */
  120. #define CAN_F19DATA1(canx) REG32((canx) + 0x000002DCU) /*!< CAN filter 19 data 1 register */
  121. #define CAN_F20DATA1(canx) REG32((canx) + 0x000002E4U) /*!< CAN filter 20 data 1 register */
  122. #define CAN_F21DATA1(canx) REG32((canx) + 0x000002ECU) /*!< CAN filter 21 data 1 register */
  123. #define CAN_F22DATA1(canx) REG32((canx) + 0x000002F4U) /*!< CAN filter 22 data 1 register */
  124. #define CAN_F23DATA1(canx) REG32((canx) + 0x000002FCU) /*!< CAN filter 23 data 1 register */
  125. #define CAN_F24DATA1(canx) REG32((canx) + 0x00000304U) /*!< CAN filter 24 data 1 register */
  126. #define CAN_F25DATA1(canx) REG32((canx) + 0x0000030CU) /*!< CAN filter 25 data 1 register */
  127. #define CAN_F26DATA1(canx) REG32((canx) + 0x00000314U) /*!< CAN filter 26 data 1 register */
  128. #define CAN_F27DATA1(canx) REG32((canx) + 0x0000031CU) /*!< CAN filter 27 data 1 register */
  129. /* CAN transmit mailbox bank */
  130. #define CAN_TMI(canx, bank) REG32((canx) + 0x180U + ((bank) * 0x10U)) /*!< CAN transmit mailbox identifier register */
  131. #define CAN_TMP(canx, bank) REG32((canx) + 0x184U + ((bank) * 0x10U)) /*!< CAN transmit mailbox property register */
  132. #define CAN_TMDATA0(canx, bank) REG32((canx) + 0x188U + ((bank) * 0x10U)) /*!< CAN transmit mailbox data0 register */
  133. #define CAN_TMDATA1(canx, bank) REG32((canx) + 0x18CU + ((bank) * 0x10U)) /*!< CAN transmit mailbox data1 register */
  134. /* CAN filter bank */
  135. #define CAN_FDATA0(canx, bank) REG32((canx) + 0x240U + ((bank) * 0x8U) + 0x0U) /*!< CAN filter data 0 register */
  136. #define CAN_FDATA1(canx, bank) REG32((canx) + 0x240U + ((bank) * 0x8U) + 0x4U) /*!< CAN filter data 1 register */
  137. /* CAN receive FIFO mailbox bank */
  138. #define CAN_RFIFOMI(canx, bank) REG32((canx) + 0x1B0U + ((bank) * 0x10U)) /*!< CAN receive FIFO mailbox identifier register */
  139. #define CAN_RFIFOMP(canx, bank) REG32((canx) + 0x1B4U + ((bank) * 0x10U)) /*!< CAN receive FIFO mailbox property register */
  140. #define CAN_RFIFOMDATA0(canx, bank) REG32((canx) + 0x1B8U + ((bank) * 0x10U)) /*!< CAN receive FIFO mailbox data0 register */
  141. #define CAN_RFIFOMDATA1(canx, bank) REG32((canx) + 0x1BCU + ((bank) * 0x10U)) /*!< CAN receive FIFO mailbox data1 register */
  142. /* bits definitions */
  143. /* CAN_CTL */
  144. #define CAN_CTL_IWMOD BIT(0) /*!< initial working mode */
  145. #define CAN_CTL_SLPWMOD BIT(1) /*!< sleep working mode */
  146. #define CAN_CTL_TFO BIT(2) /*!< transmit FIFO order */
  147. #define CAN_CTL_RFOD BIT(3) /*!< receive FIFO overwrite disable */
  148. #define CAN_CTL_ARD BIT(4) /*!< automatic retransmission disable */
  149. #define CAN_CTL_AWU BIT(5) /*!< automatic wakeup */
  150. #define CAN_CTL_ABOR BIT(6) /*!< automatic bus-off recovery */
  151. #define CAN_CTL_TTC BIT(7) /*!< time triggered communication */
  152. #define CAN_CTL_SWRST BIT(15) /*!< CAN software reset */
  153. #define CAN_CTL_DFZ BIT(16) /*!< CAN debug freeze */
  154. /* CAN_STAT */
  155. #define CAN_STAT_IWS BIT(0) /*!< initial working state */
  156. #define CAN_STAT_SLPWS BIT(1) /*!< sleep working state */
  157. #define CAN_STAT_ERRIF BIT(2) /*!< error interrupt flag*/
  158. #define CAN_STAT_WUIF BIT(3) /*!< status change interrupt flag of wakeup from sleep working mode */
  159. #define CAN_STAT_SLPIF BIT(4) /*!< status change interrupt flag of sleep working mode entering */
  160. #define CAN_STAT_TS BIT(8) /*!< transmitting state */
  161. #define CAN_STAT_RS BIT(9) /*!< receiving state */
  162. #define CAN_STAT_LASTRX BIT(10) /*!< last sample value of rx pin */
  163. #define CAN_STAT_RXL BIT(11) /*!< CAN rx signal */
  164. /* CAN_TSTAT */
  165. #define CAN_TSTAT_MTF0 BIT(0) /*!< mailbox0 transmit finished */
  166. #define CAN_TSTAT_MTFNERR0 BIT(1) /*!< mailbox0 transmit finished and no error */
  167. #define CAN_TSTAT_MAL0 BIT(2) /*!< mailbox0 arbitration lost */
  168. #define CAN_TSTAT_MTE0 BIT(3) /*!< mailbox0 transmit error */
  169. #define CAN_TSTAT_MST0 BIT(7) /*!< mailbox0 stop transmitting */
  170. #define CAN_TSTAT_MTF1 BIT(8) /*!< mailbox1 transmit finished */
  171. #define CAN_TSTAT_MTFNERR1 BIT(9) /*!< mailbox1 transmit finished and no error */
  172. #define CAN_TSTAT_MAL1 BIT(10) /*!< mailbox1 arbitration lost */
  173. #define CAN_TSTAT_MTE1 BIT(11) /*!< mailbox1 transmit error */
  174. #define CAN_TSTAT_MST1 BIT(15) /*!< mailbox1 stop transmitting */
  175. #define CAN_TSTAT_MTF2 BIT(16) /*!< mailbox2 transmit finished */
  176. #define CAN_TSTAT_MTFNERR2 BIT(17) /*!< mailbox2 transmit finished and no error */
  177. #define CAN_TSTAT_MAL2 BIT(18) /*!< mailbox2 arbitration lost */
  178. #define CAN_TSTAT_MTE2 BIT(19) /*!< mailbox2 transmit error */
  179. #define CAN_TSTAT_MST2 BIT(23) /*!< mailbox2 stop transmitting */
  180. #define CAN_TSTAT_NUM BITS(24,25) /*!< mailbox number */
  181. #define CAN_TSTAT_TME0 BIT(26) /*!< transmit mailbox0 empty */
  182. #define CAN_TSTAT_TME1 BIT(27) /*!< transmit mailbox1 empty */
  183. #define CAN_TSTAT_TME2 BIT(28) /*!< transmit mailbox2 empty */
  184. #define CAN_TSTAT_TMLS0 BIT(29) /*!< last sending priority flag for mailbox0 */
  185. #define CAN_TSTAT_TMLS1 BIT(30) /*!< last sending priority flag for mailbox1 */
  186. #define CAN_TSTAT_TMLS2 BIT(31) /*!< last sending priority flag for mailbox2 */
  187. /* CAN_RFIFO0 */
  188. #define CAN_RFIFO0_RFL0 BITS(0,1) /*!< receive FIFO0 length */
  189. #define CAN_RFIFO0_RFF0 BIT(3) /*!< receive FIFO0 full */
  190. #define CAN_RFIFO0_RFO0 BIT(4) /*!< receive FIFO0 overfull */
  191. #define CAN_RFIFO0_RFD0 BIT(5) /*!< receive FIFO0 dequeue */
  192. /* CAN_RFIFO1 */
  193. #define CAN_RFIFO1_RFL1 BITS(0,1) /*!< receive FIFO1 length */
  194. #define CAN_RFIFO1_RFF1 BIT(3) /*!< receive FIFO1 full */
  195. #define CAN_RFIFO1_RFO1 BIT(4) /*!< receive FIFO1 overfull */
  196. #define CAN_RFIFO1_RFD1 BIT(5) /*!< receive FIFO1 dequeue */
  197. /* CAN_INTEN */
  198. #define CAN_INTEN_TMEIE BIT(0) /*!< transmit mailbox empty interrupt enable */
  199. #define CAN_INTEN_RFNEIE0 BIT(1) /*!< receive FIFO0 not empty interrupt enable */
  200. #define CAN_INTEN_RFFIE0 BIT(2) /*!< receive FIFO0 full interrupt enable */
  201. #define CAN_INTEN_RFOIE0 BIT(3) /*!< receive FIFO0 overfull interrupt enable */
  202. #define CAN_INTEN_RFNEIE1 BIT(4) /*!< receive FIFO1 not empty interrupt enable */
  203. #define CAN_INTEN_RFFIE1 BIT(5) /*!< receive FIFO1 full interrupt enable */
  204. #define CAN_INTEN_RFOIE1 BIT(6) /*!< receive FIFO1 overfull interrupt enable */
  205. #define CAN_INTEN_WERRIE BIT(8) /*!< warning error interrupt enable */
  206. #define CAN_INTEN_PERRIE BIT(9) /*!< passive error interrupt enable */
  207. #define CAN_INTEN_BOIE BIT(10) /*!< bus-off interrupt enable */
  208. #define CAN_INTEN_ERRNIE BIT(11) /*!< error number interrupt enable */
  209. #define CAN_INTEN_ERRIE BIT(15) /*!< error interrupt enable */
  210. #define CAN_INTEN_WIE BIT(16) /*!< wakeup interrupt enable */
  211. #define CAN_INTEN_SLPWIE BIT(17) /*!< sleep working interrupt enable */
  212. /* CAN_ERR */
  213. #define CAN_ERR_WERR BIT(0) /*!< warning error */
  214. #define CAN_ERR_PERR BIT(1) /*!< passive error */
  215. #define CAN_ERR_BOERR BIT(2) /*!< bus-off error */
  216. #define CAN_ERR_ERRN BITS(4,6) /*!< error number */
  217. #define CAN_ERR_TECNT BITS(16,23) /*!< transmit error count */
  218. #define CAN_ERR_RECNT BITS(24,31) /*!< receive error count */
  219. /* CAN_BT */
  220. #define CAN_BT_BAUDPSC BITS(0,9) /*!< baudrate prescaler */
  221. #define CAN_BT_BS1 BITS(16,19) /*!< bit segment 1 */
  222. #define CAN_BT_BS2 BITS(20,22) /*!< bit segment 2 */
  223. #define CAN_BT_SJW BITS(24,25) /*!< resynchronization jump width */
  224. #define CAN_BT_LCMOD BIT(30) /*!< loopback communication mode */
  225. #define CAN_BT_SCMOD BIT(31) /*!< silent communication mode */
  226. /* CAN_TMIx */
  227. #define CAN_TMI_TEN BIT(0) /*!< transmit enable */
  228. #define CAN_TMI_FT BIT(1) /*!< frame type */
  229. #define CAN_TMI_FF BIT(2) /*!< frame format */
  230. #define CAN_TMI_EFID BITS(3,31) /*!< the frame identifier */
  231. #define CAN_TMI_SFID BITS(21,31) /*!< the frame identifier */
  232. /* CAN_TMPx */
  233. #define CAN_TMP_DLENC BITS(0,3) /*!< data length code */
  234. #define CAN_TMP_TSEN BIT(8) /*!< time stamp enable */
  235. #define CAN_TMP_TS BITS(16,31) /*!< time stamp */
  236. /* CAN_TMDATA0x */
  237. #define CAN_TMDATA0_DB0 BITS(0,7) /*!< transmit data byte 0 */
  238. #define CAN_TMDATA0_DB1 BITS(8,15) /*!< transmit data byte 1 */
  239. #define CAN_TMDATA0_DB2 BITS(16,23) /*!< transmit data byte 2 */
  240. #define CAN_TMDATA0_DB3 BITS(24,31) /*!< transmit data byte 3 */
  241. /* CAN_TMDATA1x */
  242. #define CAN_TMDATA1_DB4 BITS(0,7) /*!< transmit data byte 4 */
  243. #define CAN_TMDATA1_DB5 BITS(8,15) /*!< transmit data byte 5 */
  244. #define CAN_TMDATA1_DB6 BITS(16,23) /*!< transmit data byte 6 */
  245. #define CAN_TMDATA1_DB7 BITS(24,31) /*!< transmit data byte 7 */
  246. /* CAN_RFIFOMIx */
  247. #define CAN_RFIFOMI_FT BIT(1) /*!< frame type */
  248. #define CAN_RFIFOMI_FF BIT(2) /*!< frame format */
  249. #define CAN_RFIFOMI_EFID BITS(3,31) /*!< the frame identifier */
  250. #define CAN_RFIFOMI_SFID BITS(21,31) /*!< the frame identifier */
  251. /* CAN_RFIFOMPx */
  252. #define CAN_RFIFOMP_DLENC BITS(0,3) /*!< receive data length code */
  253. #define CAN_RFIFOMP_FI BITS(8,15) /*!< filter index */
  254. #define CAN_RFIFOMP_TS BITS(16,31) /*!< time stamp */
  255. /* CAN_RFIFOMDATA0x */
  256. #define CAN_RFIFOMDATA0_DB0 BITS(0,7) /*!< receive data byte 0 */
  257. #define CAN_RFIFOMDATA0_DB1 BITS(8,15) /*!< receive data byte 1 */
  258. #define CAN_RFIFOMDATA0_DB2 BITS(16,23) /*!< receive data byte 2 */
  259. #define CAN_RFIFOMDATA0_DB3 BITS(24,31) /*!< receive data byte 3 */
  260. /* CAN_RFIFOMDATA1x */
  261. #define CAN_RFIFOMDATA1_DB4 BITS(0,7) /*!< receive data byte 4 */
  262. #define CAN_RFIFOMDATA1_DB5 BITS(8,15) /*!< receive data byte 5 */
  263. #define CAN_RFIFOMDATA1_DB6 BITS(16,23) /*!< receive data byte 6 */
  264. #define CAN_RFIFOMDATA1_DB7 BITS(24,31) /*!< receive data byte 7 */
  265. /* CAN_FCTL */
  266. #define CAN_FCTL_FLD BIT(0) /*!< filter lock disable */
  267. #define CAN_FCTL_HBC1F BITS(8,13) /*!< header bank of CAN1 filter */
  268. /* CAN_FMCFG */
  269. #define CAN_FMCFG_FMOD(regval) BIT(regval) /*!< filter mode, list or mask */
  270. /* CAN_FSCFG */
  271. #define CAN_FSCFG_FS(regval) BIT(regval) /*!< filter scale, 32 bits or 16 bits */
  272. /* CAN_FAFIFO */
  273. #define CAN_FAFIFOR_FAF(regval) BIT(regval) /*!< filter associated with FIFO */
  274. /* CAN_FW */
  275. #define CAN_FW_FW(regval) BIT(regval) /*!< filter working */
  276. /* CAN_FxDATAy */
  277. #define CAN_FDATA_FD(regval) BIT(regval) /*!< filter data */
  278. /* constants definitions */
  279. /* define the CAN bit position and its register index offset */
  280. #define CAN_REGIDX_BIT(regidx, bitpos) (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
  281. #define CAN_REG_VAL(canx, offset) (REG32((canx) + ((uint32_t)(offset) >> 6)))
  282. #define CAN_BIT_POS(val) ((uint32_t)(val) & 0x1FU)
  283. #define CAN_REGIDX_BITS(regidx, bitpos0, bitpos1) (((uint32_t)(regidx) << 12) | ((uint32_t)(bitpos0) << 6) | (uint32_t)(bitpos1))
  284. #define CAN_REG_VALS(canx, offset) (REG32((canx) + ((uint32_t)(offset) >> 12)))
  285. #define CAN_BIT_POS0(val) (((uint32_t)(val) >> 6) & 0x1FU)
  286. #define CAN_BIT_POS1(val) ((uint32_t)(val) & 0x1FU)
  287. /* register offset */
  288. #define STAT_REG_OFFSET ((uint8_t)0x04U) /*!< STAT register offset */
  289. #define TSTAT_REG_OFFSET ((uint8_t)0x08U) /*!< TSTAT register offset */
  290. #define RFIFO0_REG_OFFSET ((uint8_t)0x0CU) /*!< RFIFO0 register offset */
  291. #define RFIFO1_REG_OFFSET ((uint8_t)0x10U) /*!< RFIFO1 register offset */
  292. #define ERR_REG_OFFSET ((uint8_t)0x18U) /*!< ERR register offset */
  293. /* CAN flags */
  294. typedef enum {
  295. /* flags in STAT register */
  296. CAN_FLAG_RXL = CAN_REGIDX_BIT(STAT_REG_OFFSET, 11U), /*!< RX level */
  297. CAN_FLAG_LASTRX = CAN_REGIDX_BIT(STAT_REG_OFFSET, 10U), /*!< last sample value of RX pin */
  298. CAN_FLAG_RS = CAN_REGIDX_BIT(STAT_REG_OFFSET, 9U), /*!< receiving state */
  299. CAN_FLAG_TS = CAN_REGIDX_BIT(STAT_REG_OFFSET, 8U), /*!< transmitting state */
  300. CAN_FLAG_SLPIF = CAN_REGIDX_BIT(STAT_REG_OFFSET, 4U), /*!< status change flag of entering sleep working mode */
  301. CAN_FLAG_WUIF = CAN_REGIDX_BIT(STAT_REG_OFFSET, 3U), /*!< status change flag of wakeup from sleep working mode */
  302. CAN_FLAG_ERRIF = CAN_REGIDX_BIT(STAT_REG_OFFSET, 2U), /*!< error flag */
  303. CAN_FLAG_SLPWS = CAN_REGIDX_BIT(STAT_REG_OFFSET, 1U), /*!< sleep working state */
  304. CAN_FLAG_IWS = CAN_REGIDX_BIT(STAT_REG_OFFSET, 0U), /*!< initial working state */
  305. /* flags in TSTAT register */
  306. CAN_FLAG_TMLS2 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 31U), /*!< transmit mailbox 2 last sending in TX FIFO */
  307. CAN_FLAG_TMLS1 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 30U), /*!< transmit mailbox 1 last sending in TX FIFO */
  308. CAN_FLAG_TMLS0 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 29U), /*!< transmit mailbox 0 last sending in TX FIFO */
  309. CAN_FLAG_TME2 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 28U), /*!< transmit mailbox 2 empty */
  310. CAN_FLAG_TME1 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 27U), /*!< transmit mailbox 1 empty */
  311. CAN_FLAG_TME0 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 26U), /*!< transmit mailbox 0 empty */
  312. CAN_FLAG_MTE2 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 19U), /*!< mailbox 2 transmit error */
  313. CAN_FLAG_MTE1 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 11U), /*!< mailbox 1 transmit error */
  314. CAN_FLAG_MTE0 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 3U), /*!< mailbox 0 transmit error */
  315. CAN_FLAG_MAL2 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 18U), /*!< mailbox 2 arbitration lost */
  316. CAN_FLAG_MAL1 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 10U), /*!< mailbox 1 arbitration lost */
  317. CAN_FLAG_MAL0 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 2U), /*!< mailbox 0 arbitration lost */
  318. CAN_FLAG_MTFNERR2 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 17U), /*!< mailbox 2 transmit finished with no error */
  319. CAN_FLAG_MTFNERR1 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 9U), /*!< mailbox 1 transmit finished with no error */
  320. CAN_FLAG_MTFNERR0 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 1U), /*!< mailbox 0 transmit finished with no error */
  321. CAN_FLAG_MTF2 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 16U), /*!< mailbox 2 transmit finished */
  322. CAN_FLAG_MTF1 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 8U), /*!< mailbox 1 transmit finished */
  323. CAN_FLAG_MTF0 = CAN_REGIDX_BIT(TSTAT_REG_OFFSET, 0U), /*!< mailbox 0 transmit finished */
  324. /* flags in RFIFO0 register */
  325. CAN_FLAG_RFO0 = CAN_REGIDX_BIT(RFIFO0_REG_OFFSET, 4U), /*!< receive FIFO0 overfull */
  326. CAN_FLAG_RFF0 = CAN_REGIDX_BIT(RFIFO0_REG_OFFSET, 3U), /*!< receive FIFO0 full */
  327. /* flags in RFIFO1 register */
  328. CAN_FLAG_RFO1 = CAN_REGIDX_BIT(RFIFO1_REG_OFFSET, 4U), /*!< receive FIFO1 overfull */
  329. CAN_FLAG_RFF1 = CAN_REGIDX_BIT(RFIFO1_REG_OFFSET, 3U), /*!< receive FIFO1 full */
  330. /* flags in ERR register */
  331. CAN_FLAG_BOERR = CAN_REGIDX_BIT(ERR_REG_OFFSET, 2U), /*!< bus-off error */
  332. CAN_FLAG_PERR = CAN_REGIDX_BIT(ERR_REG_OFFSET, 1U), /*!< passive error */
  333. CAN_FLAG_WERR = CAN_REGIDX_BIT(ERR_REG_OFFSET, 0U), /*!< warning error */
  334. } can_flag_enum;
  335. /* CAN interrupt flags */
  336. typedef enum {
  337. /* interrupt flags in STAT register */
  338. CAN_INT_FLAG_SLPIF = CAN_REGIDX_BITS(STAT_REG_OFFSET, 4U, 17U), /*!< status change interrupt flag of sleep working mode entering */
  339. CAN_INT_FLAG_WUIF = CAN_REGIDX_BITS(STAT_REG_OFFSET, 3U, 16), /*!< status change interrupt flag of wakeup from sleep working mode */
  340. CAN_INT_FLAG_ERRIF = CAN_REGIDX_BITS(STAT_REG_OFFSET, 2U, 15), /*!< error interrupt flag */
  341. /* interrupt flags in TSTAT register */
  342. CAN_INT_FLAG_MTF2 = CAN_REGIDX_BITS(TSTAT_REG_OFFSET, 16U, 0U), /*!< mailbox 2 transmit finished interrupt flag */
  343. CAN_INT_FLAG_MTF1 = CAN_REGIDX_BITS(TSTAT_REG_OFFSET, 8U, 0U), /*!< mailbox 1 transmit finished interrupt flag */
  344. CAN_INT_FLAG_MTF0 = CAN_REGIDX_BITS(TSTAT_REG_OFFSET, 0U, 0U), /*!< mailbox 0 transmit finished interrupt flag */
  345. /* interrupt flags in RFIFO0 register */
  346. CAN_INT_FLAG_RFO0 = CAN_REGIDX_BITS(RFIFO0_REG_OFFSET, 4U, 3U), /*!< receive FIFO0 overfull interrupt flag */
  347. CAN_INT_FLAG_RFF0 = CAN_REGIDX_BITS(RFIFO0_REG_OFFSET, 3U, 2U), /*!< receive FIFO0 full interrupt flag */
  348. CAN_INT_FLAG_RFL0 = CAN_REGIDX_BITS(RFIFO0_REG_OFFSET, 2U, 1U), /*!< receive FIFO0 not empty interrupt flag */
  349. /* interrupt flags in RFIFO0 register */
  350. CAN_INT_FLAG_RFO1 = CAN_REGIDX_BITS(RFIFO1_REG_OFFSET, 4U, 6U), /*!< receive FIFO1 overfull interrupt flag */
  351. CAN_INT_FLAG_RFF1 = CAN_REGIDX_BITS(RFIFO1_REG_OFFSET, 3U, 5U), /*!< receive FIFO1 full interrupt flag */
  352. CAN_INT_FLAG_RFL1 = CAN_REGIDX_BITS(RFIFO1_REG_OFFSET, 2U, 4U), /*!< receive FIFO1 not empty interrupt flag */
  353. /* interrupt flags in ERR register */
  354. CAN_INT_FLAG_ERRN = CAN_REGIDX_BITS(ERR_REG_OFFSET, 3U, 11U), /*!< error number interrupt flag */
  355. CAN_INT_FLAG_BOERR = CAN_REGIDX_BITS(ERR_REG_OFFSET, 2U, 10U), /*!< bus-off error interrupt flag */
  356. CAN_INT_FLAG_PERR = CAN_REGIDX_BITS(ERR_REG_OFFSET, 1U, 9U), /*!< passive error interrupt flag */
  357. CAN_INT_FLAG_WERR = CAN_REGIDX_BITS(ERR_REG_OFFSET, 0U, 8U), /*!< warning error interrupt flag */
  358. } can_interrupt_flag_enum;
  359. /* CAN initiliaze parameters structure */
  360. typedef struct {
  361. uint8_t working_mode; /*!< CAN working mode */
  362. uint8_t resync_jump_width; /*!< CAN resynchronization jump width */
  363. uint8_t time_segment_1; /*!< time segment 1 */
  364. uint8_t time_segment_2; /*!< time segment 2 */
  365. ControlStatus time_triggered; /*!< time triggered communication mode */
  366. ControlStatus auto_bus_off_recovery; /*!< automatic bus-off recovery */
  367. ControlStatus auto_wake_up; /*!< automatic wake-up mode */
  368. ControlStatus auto_retrans; /*!< automatic retransmission mode */
  369. ControlStatus rec_fifo_overwrite; /*!< receive FIFO overwrite mode */
  370. ControlStatus trans_fifo_order; /*!< transmit FIFO order */
  371. uint16_t prescaler; /*!< baudrate prescaler */
  372. } can_parameter_struct;
  373. /* CAN transmit message structure */
  374. typedef struct {
  375. uint32_t tx_sfid; /*!< standard format frame identifier */
  376. uint32_t tx_efid; /*!< extended format frame identifier */
  377. uint8_t tx_ff; /*!< format of frame, standard or extended format */
  378. uint8_t tx_ft; /*!< type of frame, data or remote */
  379. uint8_t tx_dlen; /*!< data length */
  380. uint8_t tx_data[8]; /*!< transmit data */
  381. } can_trasnmit_message_struct;
  382. /* CAN receive message structure */
  383. typedef struct {
  384. uint32_t rx_sfid; /*!< standard format frame identifier */
  385. uint32_t rx_efid; /*!< extended format frame identifier */
  386. uint8_t rx_ff; /*!< format of frame, standard or extended format */
  387. uint8_t rx_ft; /*!< type of frame, data or remote */
  388. uint8_t rx_dlen; /*!< data length */
  389. uint8_t rx_data[8]; /*!< receive data */
  390. uint8_t rx_fi; /*!< filtering index */
  391. } can_receive_message_struct;
  392. /* CAN filter parameters structure */
  393. typedef struct {
  394. uint16_t filter_list_high; /*!< filter list number high bits */
  395. uint16_t filter_list_low; /*!< filter list number low bits */
  396. uint16_t filter_mask_high; /*!< filter mask number high bits */
  397. uint16_t filter_mask_low; /*!< filter mask number low bits */
  398. uint16_t filter_fifo_number; /*!< receive FIFO associated with the filter */
  399. uint16_t filter_number; /*!< filter number */
  400. uint16_t filter_mode; /*!< filter mode, list or mask */
  401. uint16_t filter_bits; /*!< filter scale */
  402. ControlStatus filter_enable; /*!< filter work or not */
  403. } can_filter_parameter_struct;
  404. /* CAN errors */
  405. typedef enum {
  406. CAN_ERROR_NONE = 0, /*!< no error */
  407. CAN_ERROR_FILL, /*!< fill error */
  408. CAN_ERROR_FORMATE, /*!< format error */
  409. CAN_ERROR_ACK, /*!< ACK error */
  410. CAN_ERROR_BITRECESSIVE, /*!< bit recessive error */
  411. CAN_ERROR_BITDOMINANTER, /*!< bit dominant error */
  412. CAN_ERROR_CRC, /*!< CRC error */
  413. CAN_ERROR_SOFTWARECFG, /*!< software configure */
  414. } can_error_enum;
  415. /* transmit states */
  416. typedef enum {
  417. CAN_TRANSMIT_FAILED = 0U, /*!< CAN transmitted failure */
  418. CAN_TRANSMIT_OK = 1U, /*!< CAN transmitted success */
  419. CAN_TRANSMIT_PENDING = 2U, /*!< CAN transmitted pending */
  420. CAN_TRANSMIT_NOMAILBOX = 4U, /*!< no empty mailbox to be used for CAN */
  421. } can_transmit_state_enum;
  422. typedef enum {
  423. CAN_INIT_STRUCT = 0, /* CAN initiliaze parameters struct */
  424. CAN_FILTER_STRUCT, /* CAN filter parameters struct */
  425. CAN_TX_MESSAGE_STRUCT, /* CAN transmit message struct */
  426. CAN_RX_MESSAGE_STRUCT, /* CAN receive message struct */
  427. } can_struct_type_enum;
  428. /* CAN baudrate prescaler */
  429. #define BT_BAUDPSC(regval) (BITS(0,9) & ((uint32_t)(regval) << 0))
  430. /* CAN bit segment 1 */
  431. #define BT_BS1(regval) (BITS(16,19) & ((uint32_t)(regval) << 16))
  432. /* CAN bit segment 2 */
  433. #define BT_BS2(regval) (BITS(20,22) & ((uint32_t)(regval) << 20))
  434. /* CAN resynchronization jump width */
  435. #define BT_SJW(regval) (BITS(24,25) & ((uint32_t)(regval) << 24))
  436. /* CAN communication mode */
  437. #define BT_MODE(regval) (BITS(30,31) & ((uint32_t)(regval) << 30))
  438. /* CAN FDATA high 16 bits */
  439. #define FDATA_MASK_HIGH(regval) (BITS(16,31) & ((uint32_t)(regval) << 16))
  440. /* CAN FDATA low 16 bits */
  441. #define FDATA_MASK_LOW(regval) (BITS(0,15) & ((uint32_t)(regval) << 0))
  442. /* CAN1 filter start bank_number */
  443. #define FCTL_HBC1F(regval) (BITS(8,13) & ((uint32_t)(regval) << 8))
  444. /* CAN transmit mailbox extended identifier */
  445. #define TMI_EFID(regval) (BITS(3,31) & ((uint32_t)(regval) << 3))
  446. /* CAN transmit mailbox standard identifier */
  447. #define TMI_SFID(regval) (BITS(21,31) & ((uint32_t)(regval) << 21))
  448. /* transmit data byte 0 */
  449. #define TMDATA0_DB0(regval) (BITS(0,7) & ((uint32_t)(regval) << 0))
  450. /* transmit data byte 1 */
  451. #define TMDATA0_DB1(regval) (BITS(8,15) & ((uint32_t)(regval) << 8))
  452. /* transmit data byte 2 */
  453. #define TMDATA0_DB2(regval) (BITS(16,23) & ((uint32_t)(regval) << 16))
  454. /* transmit data byte 3 */
  455. #define TMDATA0_DB3(regval) (BITS(24,31) & ((uint32_t)(regval) << 24))
  456. /* transmit data byte 4 */
  457. #define TMDATA1_DB4(regval) (BITS(0,7) & ((uint32_t)(regval) << 0))
  458. /* transmit data byte 5 */
  459. #define TMDATA1_DB5(regval) (BITS(8,15) & ((uint32_t)(regval) << 8))
  460. /* transmit data byte 6 */
  461. #define TMDATA1_DB6(regval) (BITS(16,23) & ((uint32_t)(regval) << 16))
  462. /* transmit data byte 7 */
  463. #define TMDATA1_DB7(regval) (BITS(24,31) & ((uint32_t)(regval) << 24))
  464. /* receive mailbox extended identifier */
  465. #define GET_RFIFOMI_EFID(regval) GET_BITS((uint32_t)(regval), 3U, 31U)
  466. /* receive mailbox standard identifier */
  467. #define GET_RFIFOMI_SFID(regval) GET_BITS((uint32_t)(regval), 21U, 31U)
  468. /* receive data length */
  469. #define GET_RFIFOMP_DLENC(regval) GET_BITS((uint32_t)(regval), 0U, 3U)
  470. /* the index of the filter by which the frame is passed */
  471. #define GET_RFIFOMP_FI(regval) GET_BITS((uint32_t)(regval), 8U, 15U)
  472. /* receive data byte 0 */
  473. #define GET_RFIFOMDATA0_DB0(regval) GET_BITS((uint32_t)(regval), 0U, 7U)
  474. /* receive data byte 1 */
  475. #define GET_RFIFOMDATA0_DB1(regval) GET_BITS((uint32_t)(regval), 8U, 15U)
  476. /* receive data byte 2 */
  477. #define GET_RFIFOMDATA0_DB2(regval) GET_BITS((uint32_t)(regval), 16U, 23U)
  478. /* receive data byte 3 */
  479. #define GET_RFIFOMDATA0_DB3(regval) GET_BITS((uint32_t)(regval), 24U, 31U)
  480. /* receive data byte 4 */
  481. #define GET_RFIFOMDATA1_DB4(regval) GET_BITS((uint32_t)(regval), 0U, 7U)
  482. /* receive data byte 5 */
  483. #define GET_RFIFOMDATA1_DB5(regval) GET_BITS((uint32_t)(regval), 8U, 15U)
  484. /* receive data byte 6 */
  485. #define GET_RFIFOMDATA1_DB6(regval) GET_BITS((uint32_t)(regval), 16U, 23U)
  486. /* receive data byte 7 */
  487. #define GET_RFIFOMDATA1_DB7(regval) GET_BITS((uint32_t)(regval), 24U, 31U)
  488. /* error number */
  489. #define GET_ERR_ERRN(regval) GET_BITS((uint32_t)(regval), 4U, 6U)
  490. /* transmit error count */
  491. #define GET_ERR_TECNT(regval) GET_BITS((uint32_t)(regval), 16U, 23U)
  492. /* receive error count */
  493. #define GET_ERR_RECNT(regval) GET_BITS((uint32_t)(regval), 24U, 31U)
  494. /* CAN errors */
  495. #define ERR_ERRN(regval) (BITS(4,6) & ((uint32_t)(regval) << 4))
  496. #define CAN_ERRN_0 ERR_ERRN(0U) /*!< no error */
  497. #define CAN_ERRN_1 ERR_ERRN(1U) /*!< fill error */
  498. #define CAN_ERRN_2 ERR_ERRN(2U) /*!< format error */
  499. #define CAN_ERRN_3 ERR_ERRN(3U) /*!< ACK error */
  500. #define CAN_ERRN_4 ERR_ERRN(4U) /*!< bit recessive error */
  501. #define CAN_ERRN_5 ERR_ERRN(5U) /*!< bit dominant error */
  502. #define CAN_ERRN_6 ERR_ERRN(6U) /*!< CRC error */
  503. #define CAN_ERRN_7 ERR_ERRN(7U) /*!< software error */
  504. #define CAN_STATE_PENDING ((uint32_t)0x00000000U) /*!< CAN pending */
  505. /* CAN communication mode */
  506. #define CAN_NORMAL_MODE ((uint8_t)0x00U) /*!< normal communication mode */
  507. #define CAN_LOOPBACK_MODE ((uint8_t)0x01U) /*!< loopback communication mode */
  508. #define CAN_SILENT_MODE ((uint8_t)0x02U) /*!< silent communication mode */
  509. #define CAN_SILENT_LOOPBACK_MODE ((uint8_t)0x03U) /*!< loopback and silent communication mode */
  510. /* CAN resynchronisation jump width */
  511. #define CAN_BT_SJW_1TQ ((uint8_t)0x00U) /*!< 1 time quanta */
  512. #define CAN_BT_SJW_2TQ ((uint8_t)0x01U) /*!< 2 time quanta */
  513. #define CAN_BT_SJW_3TQ ((uint8_t)0x02U) /*!< 3 time quanta */
  514. #define CAN_BT_SJW_4TQ ((uint8_t)0x03U) /*!< 4 time quanta */
  515. /* CAN time segment 1 */
  516. #define CAN_BT_BS1_1TQ ((uint8_t)0x00U) /*!< 1 time quanta */
  517. #define CAN_BT_BS1_2TQ ((uint8_t)0x01U) /*!< 2 time quanta */
  518. #define CAN_BT_BS1_3TQ ((uint8_t)0x02U) /*!< 3 time quanta */
  519. #define CAN_BT_BS1_4TQ ((uint8_t)0x03U) /*!< 4 time quanta */
  520. #define CAN_BT_BS1_5TQ ((uint8_t)0x04U) /*!< 5 time quanta */
  521. #define CAN_BT_BS1_6TQ ((uint8_t)0x05U) /*!< 6 time quanta */
  522. #define CAN_BT_BS1_7TQ ((uint8_t)0x06U) /*!< 7 time quanta */
  523. #define CAN_BT_BS1_8TQ ((uint8_t)0x07U) /*!< 8 time quanta */
  524. #define CAN_BT_BS1_9TQ ((uint8_t)0x08U) /*!< 9 time quanta */
  525. #define CAN_BT_BS1_10TQ ((uint8_t)0x09U) /*!< 10 time quanta */
  526. #define CAN_BT_BS1_11TQ ((uint8_t)0x0AU) /*!< 11 time quanta */
  527. #define CAN_BT_BS1_12TQ ((uint8_t)0x0BU) /*!< 12 time quanta */
  528. #define CAN_BT_BS1_13TQ ((uint8_t)0x0CU) /*!< 13 time quanta */
  529. #define CAN_BT_BS1_14TQ ((uint8_t)0x0DU) /*!< 14 time quanta */
  530. #define CAN_BT_BS1_15TQ ((uint8_t)0x0EU) /*!< 15 time quanta */
  531. #define CAN_BT_BS1_16TQ ((uint8_t)0x0FU) /*!< 16 time quanta */
  532. /* CAN time segment 2 */
  533. #define CAN_BT_BS2_1TQ ((uint8_t)0x00U) /*!< 1 time quanta */
  534. #define CAN_BT_BS2_2TQ ((uint8_t)0x01U) /*!< 2 time quanta */
  535. #define CAN_BT_BS2_3TQ ((uint8_t)0x02U) /*!< 3 time quanta */
  536. #define CAN_BT_BS2_4TQ ((uint8_t)0x03U) /*!< 4 time quanta */
  537. #define CAN_BT_BS2_5TQ ((uint8_t)0x04U) /*!< 5 time quanta */
  538. #define CAN_BT_BS2_6TQ ((uint8_t)0x05U) /*!< 6 time quanta */
  539. #define CAN_BT_BS2_7TQ ((uint8_t)0x06U) /*!< 7 time quanta */
  540. #define CAN_BT_BS2_8TQ ((uint8_t)0x07U) /*!< 8 time quanta */
  541. /* CAN mailbox number */
  542. #define CAN_MAILBOX0 ((uint8_t)0x00U) /*!< mailbox0 */
  543. #define CAN_MAILBOX1 ((uint8_t)0x01U) /*!< mailbox1 */
  544. #define CAN_MAILBOX2 ((uint8_t)0x02U) /*!< mailbox2 */
  545. #define CAN_NOMAILBOX ((uint8_t)0x03U) /*!< no mailbox empty */
  546. /* CAN frame format */
  547. #define CAN_FF_STANDARD ((uint32_t)0x00000000U) /*!< standard frame */
  548. #define CAN_FF_EXTENDED ((uint32_t)0x00000004U) /*!< extended frame */
  549. /* CAN receive FIFO */
  550. #define CAN_FIFO0 ((uint8_t)0x00U) /*!< receive FIFO0 */
  551. #define CAN_FIFO1 ((uint8_t)0x01U) /*!< receive FIFO1 */
  552. /* frame number of receive FIFO */
  553. #define CAN_RFIF_RFL_MASK ((uint32_t)0x00000003U) /*!< mask for frame number in receive FIFOx */
  554. #define CAN_SFID_MASK ((uint32_t)0x000007FFU) /*!< mask of standard identifier */
  555. #define CAN_EFID_MASK ((uint32_t)0x1FFFFFFFU) /*!< mask of extended identifier */
  556. /* CAN working mode */
  557. #define CAN_MODE_INITIALIZE ((uint8_t)0x01U) /*!< CAN initialize mode */
  558. #define CAN_MODE_NORMAL ((uint8_t)0x02U) /*!< CAN normal mode */
  559. #define CAN_MODE_SLEEP ((uint8_t)0x04U) /*!< CAN sleep mode */
  560. /* filter bits */
  561. #define CAN_FILTERBITS_16BIT ((uint8_t)0x00U) /*!< CAN filter 16 bits */
  562. #define CAN_FILTERBITS_32BIT ((uint8_t)0x01U) /*!< CAN filter 32 bits */
  563. /* filter mode */
  564. #define CAN_FILTERMODE_MASK ((uint8_t)0x00U) /*!< mask mode */
  565. #define CAN_FILTERMODE_LIST ((uint8_t)0x01U) /*!< list mode */
  566. /* filter 16 bits mask */
  567. #define CAN_FILTER_MASK_16BITS ((uint32_t)0x0000FFFFU) /*!< can filter 16 bits mask */
  568. /* frame type */
  569. #define CAN_FT_DATA ((uint32_t)0x00000000U) /*!< data frame */
  570. #define CAN_FT_REMOTE ((uint32_t)0x00000002U) /*!< remote frame */
  571. /* CAN timeout */
  572. #define CAN_TIMEOUT ((uint32_t)0x0000FFFFU) /*!< timeout value */
  573. /* interrupt enable bits */
  574. #define CAN_INT_TME CAN_INTEN_TMEIE /*!< transmit mailbox empty interrupt enable */
  575. #define CAN_INT_RFNE0 CAN_INTEN_RFNEIE0 /*!< receive FIFO0 not empty interrupt enable */
  576. #define CAN_INT_RFF0 CAN_INTEN_RFFIE0 /*!< receive FIFO0 full interrupt enable */
  577. #define CAN_INT_RFO0 CAN_INTEN_RFOIE0 /*!< receive FIFO0 overfull interrupt enable */
  578. #define CAN_INT_RFNE1 CAN_INTEN_RFNEIE1 /*!< receive FIFO1 not empty interrupt enable */
  579. #define CAN_INT_RFF1 CAN_INTEN_RFFIE1 /*!< receive FIFO1 full interrupt enable */
  580. #define CAN_INT_RFO1 CAN_INTEN_RFOIE1 /*!< receive FIFO1 overfull interrupt enable */
  581. #define CAN_INT_WERR CAN_INTEN_WERRIE /*!< warning error interrupt enable */
  582. #define CAN_INT_PERR CAN_INTEN_PERRIE /*!< passive error interrupt enable */
  583. #define CAN_INT_BO CAN_INTEN_BOIE /*!< bus-off interrupt enable */
  584. #define CAN_INT_ERRN CAN_INTEN_ERRNIE /*!< error number interrupt enable */
  585. #define CAN_INT_ERR CAN_INTEN_ERRIE /*!< error interrupt enable */
  586. #define CAN_INT_WAKEUP CAN_INTEN_WIE /*!< wakeup interrupt enable */
  587. #define CAN_INT_SLPW CAN_INTEN_SLPWIE /*!< sleep working interrupt enable */
  588. /* function declarations */
  589. /* initialization functions */
  590. /* deinitialize CAN */
  591. void can_deinit(uint32_t can_periph);
  592. /* initialize CAN structure */
  593. void can_struct_para_init(can_struct_type_enum type, void *p_struct);
  594. /* initialize CAN */
  595. ErrStatus can_init(uint32_t can_periph, can_parameter_struct *can_parameter_init);
  596. /* CAN filter initialization */
  597. void can_filter_init(can_filter_parameter_struct *can_filter_parameter_init);
  598. /* function configuration */
  599. /* set can1 filter start bank number */
  600. void can1_filter_start_bank(uint8_t start_bank);
  601. /* enable functions */
  602. /* CAN debug freeze enable */
  603. void can_debug_freeze_enable(uint32_t can_periph);
  604. /* CAN debug freeze disable */
  605. void can_debug_freeze_disable(uint32_t can_periph);
  606. /* CAN time trigger mode enable */
  607. void can_time_trigger_mode_enable(uint32_t can_periph);
  608. /* CAN time trigger mode disable */
  609. void can_time_trigger_mode_disable(uint32_t can_periph);
  610. /* transmit functions */
  611. /* transmit CAN message */
  612. uint8_t can_message_transmit(uint32_t can_periph, can_trasnmit_message_struct *transmit_message);
  613. /* get CAN transmit state */
  614. can_transmit_state_enum can_transmit_states(uint32_t can_periph, uint8_t mailbox_number);
  615. /* stop CAN transmission */
  616. void can_transmission_stop(uint32_t can_periph, uint8_t mailbox_number);
  617. /* CAN receive message */
  618. void can_message_receive(uint32_t can_periph, uint8_t fifo_number, can_receive_message_struct *receive_message);
  619. /* CAN release FIFO */
  620. void can_fifo_release(uint32_t can_periph, uint8_t fifo_number);
  621. /* CAN receive message length */
  622. uint8_t can_receive_message_length_get(uint32_t can_periph, uint8_t fifo_number);
  623. /* CAN working mode */
  624. ErrStatus can_working_mode_set(uint32_t can_periph, uint8_t working_mode);
  625. /* CAN wakeup from sleep mode */
  626. ErrStatus can_wakeup(uint32_t can_periph);
  627. /* CAN get error type */
  628. can_error_enum can_error_get(uint32_t can_periph);
  629. /* get CAN receive error number */
  630. uint8_t can_receive_error_number_get(uint32_t can_periph);
  631. /* get CAN transmit error number */
  632. uint8_t can_transmit_error_number_get(uint32_t can_periph);
  633. /* interrupt & flag functions */
  634. /* CAN get flag state */
  635. FlagStatus can_flag_get(uint32_t can_periph, can_flag_enum flag);
  636. /* CAN clear flag state */
  637. void can_flag_clear(uint32_t can_periph, can_flag_enum flag);
  638. /* CAN interrupt enable */
  639. void can_interrupt_enable(uint32_t can_periph, uint32_t interrupt);
  640. /* CAN interrupt disable */
  641. void can_interrupt_disable(uint32_t can_periph, uint32_t interrupt);
  642. /* CAN get interrupt flag state */
  643. FlagStatus can_interrupt_flag_get(uint32_t can_periph, can_interrupt_flag_enum flag);
  644. /* CAN clear interrupt flag state */
  645. void can_interrupt_flag_clear(uint32_t can_periph, can_interrupt_flag_enum flag);
  646. #endif /* GD32F30X_CAN_H */