gd32f10x_can.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*!
  2. \file gd32f10x_can.c
  3. \brief CAN driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2019-11-27, V2.1.1, firmware for GD32F10x
  8. \version 2020-07-14, V2.1.2, firmware for GD32F10x
  9. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  10. \version 2021-07-21, V2.2.1, firmware for GD32F10x
  11. */
  12. /*
  13. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  14. Redistribution and use in source and binary forms, with or without modification,
  15. are permitted provided that the following conditions are met:
  16. 1. Redistributions of source code must retain the above copyright notice, this
  17. list of conditions and the following disclaimer.
  18. 2. Redistributions in binary form must reproduce the above copyright notice,
  19. this list of conditions and the following disclaimer in the documentation
  20. and/or other materials provided with the distribution.
  21. 3. Neither the name of the copyright holder nor the names of its contributors
  22. may be used to endorse or promote products derived from this software without
  23. specific prior written permission.
  24. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  28. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  33. OF SUCH DAMAGE.
  34. */
  35. #include "gd32f10x_can.h"
  36. #define CAN_ERROR_HANDLE(s) do{}while(1)
  37. #define RFO1_CLEAR_VAL ((uint32_t)0x00000000U) /*!< RFO1 clear value */
  38. #define RFF1_CLEAR_VAL ((uint32_t)0x00000018U) /*!< RFF1 clear value */
  39. /*!
  40. \brief deinitialize CAN
  41. \param[in] can_periph
  42. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  43. \param[out] none
  44. \retval none
  45. */
  46. void can_deinit(uint32_t can_periph)
  47. {
  48. #ifdef GD32F10x_CL
  49. if(CAN0 == can_periph){
  50. rcu_periph_reset_enable(RCU_CAN0RST);
  51. rcu_periph_reset_disable(RCU_CAN0RST);
  52. }else{
  53. rcu_periph_reset_enable(RCU_CAN1RST);
  54. rcu_periph_reset_disable(RCU_CAN1RST);
  55. }
  56. #else
  57. if(CAN0 == can_periph){
  58. rcu_periph_reset_enable(RCU_CAN0RST);
  59. rcu_periph_reset_disable(RCU_CAN0RST);
  60. }
  61. #endif
  62. }
  63. /*!
  64. \brief initialize CAN parameter struct with a default value
  65. \param[in] type: the type of CAN parameter struct
  66. only one parameter can be selected which is shown as below:
  67. \arg CAN_INIT_STRUCT: the CAN initial struct
  68. \arg CAN_FILTER_STRUCT: the CAN filter struct
  69. \arg CAN_TX_MESSAGE_STRUCT: the CAN TX message struct
  70. \arg CAN_RX_MESSAGE_STRUCT: the CAN RX message struct
  71. \param[in] p_struct: the pointer of the specific struct
  72. \param[out] none
  73. \retval none
  74. */
  75. void can_struct_para_init(can_struct_type_enum type, void* p_struct)
  76. {
  77. uint8_t i;
  78. /* get type of the struct */
  79. switch(type){
  80. /* used for can_init() */
  81. case CAN_INIT_STRUCT:
  82. ((can_parameter_struct*)p_struct)->auto_bus_off_recovery = DISABLE;
  83. ((can_parameter_struct*)p_struct)->auto_retrans = DISABLE;
  84. ((can_parameter_struct*)p_struct)->auto_wake_up = DISABLE;
  85. ((can_parameter_struct*)p_struct)->prescaler = 0x03FFU;
  86. ((can_parameter_struct*)p_struct)->rec_fifo_overwrite = DISABLE;
  87. ((can_parameter_struct*)p_struct)->resync_jump_width = CAN_BT_SJW_1TQ;
  88. ((can_parameter_struct*)p_struct)->time_segment_1 = CAN_BT_BS1_3TQ;
  89. ((can_parameter_struct*)p_struct)->time_segment_2 = CAN_BT_BS2_1TQ;
  90. ((can_parameter_struct*)p_struct)->time_triggered = DISABLE;
  91. ((can_parameter_struct*)p_struct)->trans_fifo_order = DISABLE;
  92. ((can_parameter_struct*)p_struct)->working_mode = CAN_NORMAL_MODE;
  93. break;
  94. /* used for can_filter_init() */
  95. case CAN_FILTER_STRUCT:
  96. ((can_filter_parameter_struct*)p_struct)->filter_bits = CAN_FILTERBITS_32BIT;
  97. ((can_filter_parameter_struct*)p_struct)->filter_enable = DISABLE;
  98. ((can_filter_parameter_struct*)p_struct)->filter_fifo_number = CAN_FIFO0;
  99. ((can_filter_parameter_struct*)p_struct)->filter_list_high = 0x0000U;
  100. ((can_filter_parameter_struct*)p_struct)->filter_list_low = 0x0000U;
  101. ((can_filter_parameter_struct*)p_struct)->filter_mask_high = 0x0000U;
  102. ((can_filter_parameter_struct*)p_struct)->filter_mask_low = 0x0000U;
  103. ((can_filter_parameter_struct*)p_struct)->filter_mode = CAN_FILTERMODE_MASK;
  104. ((can_filter_parameter_struct*)p_struct)->filter_number = 0U;
  105. break;
  106. /* used for can_message_transmit() */
  107. case CAN_TX_MESSAGE_STRUCT:
  108. for(i = 0U; i < 8U; i++){
  109. ((can_trasnmit_message_struct*)p_struct)->tx_data[i] = 0U;
  110. }
  111. ((can_trasnmit_message_struct*)p_struct)->tx_dlen = 0u;
  112. ((can_trasnmit_message_struct*)p_struct)->tx_efid = 0U;
  113. ((can_trasnmit_message_struct*)p_struct)->tx_ff = (uint8_t)CAN_FF_STANDARD;
  114. ((can_trasnmit_message_struct*)p_struct)->tx_ft = (uint8_t)CAN_FT_DATA;
  115. ((can_trasnmit_message_struct*)p_struct)->tx_sfid = 0U;
  116. break;
  117. /* used for can_message_receive() */
  118. case CAN_RX_MESSAGE_STRUCT:
  119. for(i = 0U; i < 8U; i++){
  120. ((can_receive_message_struct*)p_struct)->rx_data[i] = 0U;
  121. }
  122. ((can_receive_message_struct*)p_struct)->rx_dlen = 0U;
  123. ((can_receive_message_struct*)p_struct)->rx_efid = 0U;
  124. ((can_receive_message_struct*)p_struct)->rx_ff = (uint8_t)CAN_FF_STANDARD;
  125. ((can_receive_message_struct*)p_struct)->rx_fi = 0U;
  126. ((can_receive_message_struct*)p_struct)->rx_ft = (uint8_t)CAN_FT_DATA;
  127. ((can_receive_message_struct*)p_struct)->rx_sfid = 0U;
  128. break;
  129. default:
  130. CAN_ERROR_HANDLE("parameter is invalid \r\n");
  131. }
  132. }
  133. /*!
  134. \brief initialize CAN
  135. \param[in] can_periph
  136. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  137. \param[in] can_parameter_init: parameters for CAN initializtion
  138. \arg working_mode: CAN_NORMAL_MODE, CAN_LOOPBACK_MODE, CAN_SILENT_MODE, CAN_SILENT_LOOPBACK_MODE
  139. \arg resync_jump_width: CAN_BT_SJW_xTQ(x=1, 2, 3, 4)
  140. \arg time_segment_1: CAN_BT_BS1_xTQ(1..16)
  141. \arg time_segment_2: CAN_BT_BS2_xTQ(1..8)
  142. \arg time_triggered: ENABLE or DISABLE
  143. \arg auto_bus_off_recovery: ENABLE or DISABLE
  144. \arg auto_wake_up: ENABLE or DISABLE
  145. \arg auto_retrans: ENABLE or DISABLE
  146. \arg rec_fifo_overwrite: ENABLE or DISABLE
  147. \arg trans_fifo_order: ENABLE or DISABLE
  148. \arg prescaler: 0x0001 - 0x0400
  149. \param[out] none
  150. \retval ErrStatus: SUCCESS or ERROR
  151. */
  152. ErrStatus can_init(uint32_t can_periph, can_parameter_struct* can_parameter_init)
  153. {
  154. uint32_t timeout = CAN_TIMEOUT;
  155. ErrStatus flag = ERROR;
  156. /* disable sleep mode */
  157. CAN_CTL(can_periph) &= ~CAN_CTL_SLPWMOD;
  158. /* enable initialize mode */
  159. CAN_CTL(can_periph) |= CAN_CTL_IWMOD;
  160. /* wait ACK */
  161. while((CAN_STAT_IWS != (CAN_STAT(can_periph) & CAN_STAT_IWS)) && (0U != timeout)){
  162. timeout--;
  163. }
  164. /* check initialize working success */
  165. if(CAN_STAT_IWS != (CAN_STAT(can_periph) & CAN_STAT_IWS)){
  166. flag = ERROR;
  167. }else{
  168. /* set the bit timing register */
  169. CAN_BT(can_periph) = (BT_MODE((uint32_t)can_parameter_init->working_mode) | \
  170. BT_SJW((uint32_t)can_parameter_init->resync_jump_width) | \
  171. BT_BS1((uint32_t)can_parameter_init->time_segment_1) | \
  172. BT_BS2((uint32_t)can_parameter_init->time_segment_2) | \
  173. BT_BAUDPSC(((uint32_t)(can_parameter_init->prescaler) - 1U)));
  174. /* time trigger communication mode */
  175. if(ENABLE == can_parameter_init->time_triggered){
  176. CAN_CTL(can_periph) |= CAN_CTL_TTC;
  177. }else{
  178. CAN_CTL(can_periph) &= ~CAN_CTL_TTC;
  179. }
  180. /* automatic bus-off managment */
  181. if(ENABLE == can_parameter_init->auto_bus_off_recovery){
  182. CAN_CTL(can_periph) |= CAN_CTL_ABOR;
  183. }else{
  184. CAN_CTL(can_periph) &= ~CAN_CTL_ABOR;
  185. }
  186. /* automatic wakeup mode */
  187. if(ENABLE == can_parameter_init->auto_wake_up){
  188. CAN_CTL(can_periph) |= CAN_CTL_AWU;
  189. }else{
  190. CAN_CTL(can_periph) &= ~CAN_CTL_AWU;
  191. }
  192. /* automatic retransmission mode */
  193. if(ENABLE == can_parameter_init->auto_retrans){
  194. CAN_CTL(can_periph) &= ~CAN_CTL_ARD;
  195. }else{
  196. CAN_CTL(can_periph) |= CAN_CTL_ARD;
  197. }
  198. /* receive fifo overwrite mode */
  199. if(ENABLE == can_parameter_init->rec_fifo_overwrite){
  200. CAN_CTL(can_periph) &= ~CAN_CTL_RFOD;
  201. }else{
  202. CAN_CTL(can_periph) |= CAN_CTL_RFOD;
  203. }
  204. /* transmit fifo order */
  205. if(ENABLE == can_parameter_init->trans_fifo_order){
  206. CAN_CTL(can_periph) |= CAN_CTL_TFO;
  207. }else{
  208. CAN_CTL(can_periph) &= ~CAN_CTL_TFO;
  209. }
  210. /* disable initialize mode */
  211. CAN_CTL(can_periph) &= ~CAN_CTL_IWMOD;
  212. timeout = CAN_TIMEOUT;
  213. /* wait the ACK */
  214. while((CAN_STAT_IWS == (CAN_STAT(can_periph) & CAN_STAT_IWS)) && (0U != timeout)){
  215. timeout--;
  216. }
  217. /* check exit initialize mode */
  218. if(0U != timeout){
  219. flag = SUCCESS;
  220. }
  221. }
  222. return flag;
  223. }
  224. /*!
  225. \brief initialize CAN filter
  226. \param[in] can_filter_parameter_init: struct for CAN filter initialization
  227. \arg filter_list_high: 0x0000 - 0xFFFF
  228. \arg filter_list_low: 0x0000 - 0xFFFF
  229. \arg filter_mask_high: 0x0000 - 0xFFFF
  230. \arg filter_mask_low: 0x0000 - 0xFFFF
  231. \arg filter_fifo_number: CAN_FIFO0, CAN_FIFO1
  232. \arg filter_number: 0 - 27
  233. \arg filter_mode: CAN_FILTERMODE_MASK, CAN_FILTERMODE_LIST
  234. \arg filter_bits: CAN_FILTERBITS_32BIT, CAN_FILTERBITS_16BIT
  235. \arg filter_enable: ENABLE or DISABLE
  236. \param[out] none
  237. \retval none
  238. */
  239. void can_filter_init(can_filter_parameter_struct* can_filter_parameter_init)
  240. {
  241. uint32_t val = 0U;
  242. val = ((uint32_t)1) << (can_filter_parameter_init->filter_number);
  243. /* filter lock disable */
  244. CAN_FCTL(CAN0) |= CAN_FCTL_FLD;
  245. /* disable filter */
  246. CAN_FW(CAN0) &= ~(uint32_t)val;
  247. /* filter 16 bits */
  248. if(CAN_FILTERBITS_16BIT == can_filter_parameter_init->filter_bits){
  249. /* set filter 16 bits */
  250. CAN_FSCFG(CAN0) &= ~(uint32_t)val;
  251. /* first 16 bits list and first 16 bits mask or first 16 bits list and second 16 bits list */
  252. CAN_FDATA0(CAN0, can_filter_parameter_init->filter_number) = \
  253. FDATA_MASK_HIGH((can_filter_parameter_init->filter_mask_low) & CAN_FILTER_MASK_16BITS) | \
  254. FDATA_MASK_LOW((can_filter_parameter_init->filter_list_low) & CAN_FILTER_MASK_16BITS);
  255. /* second 16 bits list and second 16 bits mask or third 16 bits list and fourth 16 bits list */
  256. CAN_FDATA1(CAN0, can_filter_parameter_init->filter_number) = \
  257. FDATA_MASK_HIGH((can_filter_parameter_init->filter_mask_high) & CAN_FILTER_MASK_16BITS) | \
  258. FDATA_MASK_LOW((can_filter_parameter_init->filter_list_high) & CAN_FILTER_MASK_16BITS);
  259. }
  260. /* filter 32 bits */
  261. if(CAN_FILTERBITS_32BIT == can_filter_parameter_init->filter_bits){
  262. /* set filter 32 bits */
  263. CAN_FSCFG(CAN0) |= (uint32_t)val;
  264. /* 32 bits list or first 32 bits list */
  265. CAN_FDATA0(CAN0, can_filter_parameter_init->filter_number) = \
  266. FDATA_MASK_HIGH((can_filter_parameter_init->filter_list_high) & CAN_FILTER_MASK_16BITS) |
  267. FDATA_MASK_LOW((can_filter_parameter_init->filter_list_low) & CAN_FILTER_MASK_16BITS);
  268. /* 32 bits mask or second 32 bits list */
  269. CAN_FDATA1(CAN0, can_filter_parameter_init->filter_number) = \
  270. FDATA_MASK_HIGH((can_filter_parameter_init->filter_mask_high) & CAN_FILTER_MASK_16BITS) |
  271. FDATA_MASK_LOW((can_filter_parameter_init->filter_mask_low) & CAN_FILTER_MASK_16BITS);
  272. }
  273. /* filter mode */
  274. if(CAN_FILTERMODE_MASK == can_filter_parameter_init->filter_mode){
  275. /* mask mode */
  276. CAN_FMCFG(CAN0) &= ~(uint32_t)val;
  277. }else{
  278. /* list mode */
  279. CAN_FMCFG(CAN0) |= (uint32_t)val;
  280. }
  281. /* filter FIFO */
  282. if(CAN_FIFO0 == (can_filter_parameter_init->filter_fifo_number)){
  283. /* FIFO0 */
  284. CAN_FAFIFO(CAN0) &= ~(uint32_t)val;
  285. }else{
  286. /* FIFO1 */
  287. CAN_FAFIFO(CAN0) |= (uint32_t)val;
  288. }
  289. /* filter working */
  290. if(ENABLE == can_filter_parameter_init->filter_enable){
  291. CAN_FW(CAN0) |= (uint32_t)val;
  292. }
  293. /* filter lock enable */
  294. CAN_FCTL(CAN0) &= ~CAN_FCTL_FLD;
  295. }
  296. /*!
  297. \brief set CAN1 fliter start bank number
  298. \param[in] start_bank: CAN1 start bank number
  299. only one parameter can be selected which is shown as below:
  300. \arg (1..27)
  301. \param[out] none
  302. \retval none
  303. */
  304. void can1_filter_start_bank(uint8_t start_bank)
  305. {
  306. /* filter lock disable */
  307. CAN_FCTL(CAN0) |= CAN_FCTL_FLD;
  308. /* set CAN1 filter start number */
  309. CAN_FCTL(CAN0) &= ~(uint32_t)CAN_FCTL_HBC1F;
  310. CAN_FCTL(CAN0) |= FCTL_HBC1F(start_bank);
  311. /* filter lock enaable */
  312. CAN_FCTL(CAN0) &= ~CAN_FCTL_FLD;
  313. }
  314. /*!
  315. \brief enable CAN debug freeze
  316. \param[in] can_periph
  317. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  318. \param[out] none
  319. \retval none
  320. */
  321. void can_debug_freeze_enable(uint32_t can_periph)
  322. {
  323. /* set DFZ bit */
  324. CAN_CTL(can_periph) |= CAN_CTL_DFZ;
  325. #ifdef GD32F10x_CL
  326. if(CAN0 == can_periph){
  327. dbg_periph_enable(DBG_CAN0_HOLD);
  328. }else{
  329. dbg_periph_enable(DBG_CAN1_HOLD);
  330. }
  331. #else
  332. if(CAN0 == can_periph){
  333. dbg_periph_enable(DBG_CAN0_HOLD);
  334. }
  335. #endif
  336. }
  337. /*!
  338. \brief disable CAN debug freeze
  339. \param[in] can_periph
  340. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  341. \param[out] none
  342. \retval none
  343. */
  344. void can_debug_freeze_disable(uint32_t can_periph)
  345. {
  346. /* set DFZ bit */
  347. CAN_CTL(can_periph) &= ~CAN_CTL_DFZ;
  348. #ifdef GD32F10x_CL
  349. if(CAN0 == can_periph){
  350. dbg_periph_disable(DBG_CAN0_HOLD);
  351. }else{
  352. dbg_periph_disable(DBG_CAN1_HOLD);
  353. }
  354. #else
  355. if(CAN0 == can_periph){
  356. dbg_periph_enable(DBG_CAN0_HOLD);
  357. }
  358. #endif
  359. }
  360. /*!
  361. \brief enable CAN time trigger mode
  362. \param[in] can_periph
  363. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  364. \param[out] none
  365. \retval none
  366. */
  367. void can_time_trigger_mode_enable(uint32_t can_periph)
  368. {
  369. uint8_t mailbox_number;
  370. /* enable the tcc mode */
  371. CAN_CTL(can_periph) |= CAN_CTL_TTC;
  372. /* enable time stamp */
  373. for(mailbox_number = 0U; mailbox_number < 3U; mailbox_number++){
  374. CAN_TMP(can_periph, mailbox_number) |= CAN_TMP_TSEN;
  375. }
  376. }
  377. /*!
  378. \brief disable CAN time trigger mode
  379. \param[in] can_periph
  380. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  381. \param[out] none
  382. \retval none
  383. */
  384. void can_time_trigger_mode_disable(uint32_t can_periph)
  385. {
  386. uint8_t mailbox_number;
  387. /* disable the TCC mode */
  388. CAN_CTL(can_periph) &= ~CAN_CTL_TTC;
  389. /* reset TSEN bits */
  390. for(mailbox_number = 0U; mailbox_number < 3U; mailbox_number++){
  391. CAN_TMP(can_periph, mailbox_number) &= ~CAN_TMP_TSEN;
  392. }
  393. }
  394. /*!
  395. \brief transmit CAN message
  396. \param[in] can_periph
  397. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  398. \param[in] transmit_message: struct for CAN transmit message
  399. \arg tx_sfid: 0x00000000 - 0x000007FF
  400. \arg tx_efid: 0x00000000 - 0x1FFFFFFF
  401. \arg tx_ff: CAN_FF_STANDARD, CAN_FF_EXTENDED
  402. \arg tx_ft: CAN_FT_DATA, CAN_FT_REMOTE
  403. \arg tx_dlen: 0 - 8
  404. \arg tx_data[]: 0x00 - 0xFF
  405. \param[out] none
  406. \retval mailbox_number
  407. */
  408. uint8_t can_message_transmit(uint32_t can_periph, can_trasnmit_message_struct* transmit_message)
  409. {
  410. uint8_t mailbox_number = CAN_MAILBOX0;
  411. /* select one empty mailbox */
  412. if(CAN_TSTAT_TME0 == (CAN_TSTAT(can_periph)&CAN_TSTAT_TME0)){
  413. mailbox_number = CAN_MAILBOX0;
  414. }else if(CAN_TSTAT_TME1 == (CAN_TSTAT(can_periph)&CAN_TSTAT_TME1)){
  415. mailbox_number = CAN_MAILBOX1;
  416. }else if(CAN_TSTAT_TME2 == (CAN_TSTAT(can_periph)&CAN_TSTAT_TME2)){
  417. mailbox_number = CAN_MAILBOX2;
  418. }else{
  419. mailbox_number = CAN_NOMAILBOX;
  420. }
  421. /* return no mailbox empty */
  422. if(CAN_NOMAILBOX == mailbox_number){
  423. return CAN_NOMAILBOX;
  424. }
  425. CAN_TMI(can_periph, mailbox_number) &= CAN_TMI_TEN;
  426. if(CAN_FF_STANDARD == transmit_message->tx_ff){
  427. /* set transmit mailbox standard identifier */
  428. CAN_TMI(can_periph, mailbox_number) |= (uint32_t)(TMI_SFID(transmit_message->tx_sfid) | \
  429. transmit_message->tx_ft);
  430. }else{
  431. /* set transmit mailbox extended identifier */
  432. CAN_TMI(can_periph, mailbox_number) |= (uint32_t)(TMI_EFID(transmit_message->tx_efid) | \
  433. transmit_message->tx_ff | \
  434. transmit_message->tx_ft);
  435. }
  436. /* set the data length */
  437. CAN_TMP(can_periph, mailbox_number) &= ~CAN_TMP_DLENC;
  438. CAN_TMP(can_periph, mailbox_number) |= transmit_message->tx_dlen;
  439. /* set the data */
  440. CAN_TMDATA0(can_periph, mailbox_number) = TMDATA0_DB3(transmit_message->tx_data[3]) | \
  441. TMDATA0_DB2(transmit_message->tx_data[2]) | \
  442. TMDATA0_DB1(transmit_message->tx_data[1]) | \
  443. TMDATA0_DB0(transmit_message->tx_data[0]);
  444. CAN_TMDATA1(can_periph, mailbox_number) = TMDATA1_DB7(transmit_message->tx_data[7]) | \
  445. TMDATA1_DB6(transmit_message->tx_data[6]) | \
  446. TMDATA1_DB5(transmit_message->tx_data[5]) | \
  447. TMDATA1_DB4(transmit_message->tx_data[4]);
  448. /* enable transmission */
  449. CAN_TMI(can_periph, mailbox_number) |= CAN_TMI_TEN;
  450. return mailbox_number;
  451. }
  452. /*!
  453. \brief get CAN transmit state
  454. \param[in] can_periph
  455. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  456. \param[in] mailbox_number
  457. only one parameter can be selected which is shown as below:
  458. \arg CAN_MAILBOX(x=0,1,2)
  459. \param[out] none
  460. \retval can_transmit_state_enum
  461. */
  462. can_transmit_state_enum can_transmit_states(uint32_t can_periph, uint8_t mailbox_number)
  463. {
  464. can_transmit_state_enum state = CAN_TRANSMIT_FAILED;
  465. uint32_t val = 0U;
  466. /* check selected mailbox state */
  467. switch(mailbox_number){
  468. /* mailbox0 */
  469. case CAN_MAILBOX0:
  470. val = CAN_TSTAT(can_periph) & (CAN_TSTAT_MTF0 | CAN_TSTAT_MTFNERR0 | CAN_TSTAT_TME0);
  471. break;
  472. /* mailbox1 */
  473. case CAN_MAILBOX1:
  474. val = CAN_TSTAT(can_periph) & (CAN_TSTAT_MTF1 | CAN_TSTAT_MTFNERR1 | CAN_TSTAT_TME1);
  475. break;
  476. /* mailbox2 */
  477. case CAN_MAILBOX2:
  478. val = CAN_TSTAT(can_periph) & (CAN_TSTAT_MTF2 | CAN_TSTAT_MTFNERR2 | CAN_TSTAT_TME2);
  479. break;
  480. default:
  481. val = CAN_TRANSMIT_FAILED;
  482. break;
  483. }
  484. switch(val){
  485. /* transmit pending */
  486. case (CAN_STATE_PENDING):
  487. state = CAN_TRANSMIT_PENDING;
  488. break;
  489. /* mailbox0 transmit succeeded */
  490. case (CAN_TSTAT_MTF0 | CAN_TSTAT_MTFNERR0 | CAN_TSTAT_TME0):
  491. state = CAN_TRANSMIT_OK;
  492. break;
  493. /* mailbox1 transmit succeeded */
  494. case (CAN_TSTAT_MTF1 | CAN_TSTAT_MTFNERR1 | CAN_TSTAT_TME1):
  495. state = CAN_TRANSMIT_OK;
  496. break;
  497. /* mailbox2 transmit succeeded */
  498. case (CAN_TSTAT_MTF2 | CAN_TSTAT_MTFNERR2 | CAN_TSTAT_TME2):
  499. state = CAN_TRANSMIT_OK;
  500. break;
  501. /* transmit failed */
  502. default:
  503. state = CAN_TRANSMIT_FAILED;
  504. break;
  505. }
  506. return state;
  507. }
  508. /*!
  509. \brief stop CAN transmission
  510. \param[in] can_periph
  511. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  512. \param[in] mailbox_number
  513. only one parameter can be selected which is shown as below:
  514. \arg CAN_MAILBOXx(x=0,1,2)
  515. \param[out] none
  516. \retval none
  517. */
  518. void can_transmission_stop(uint32_t can_periph, uint8_t mailbox_number)
  519. {
  520. if(CAN_MAILBOX0 == mailbox_number){
  521. CAN_TSTAT(can_periph) |= CAN_TSTAT_MST0;
  522. while(CAN_TSTAT_MST0 == (CAN_TSTAT(can_periph) & CAN_TSTAT_MST0)){
  523. }
  524. }else if(CAN_MAILBOX1 == mailbox_number){
  525. CAN_TSTAT(can_periph) |= CAN_TSTAT_MST1;
  526. while(CAN_TSTAT_MST1 == (CAN_TSTAT(can_periph) & CAN_TSTAT_MST1)){
  527. }
  528. }else if(CAN_MAILBOX2 == mailbox_number){
  529. CAN_TSTAT(can_periph) |= CAN_TSTAT_MST2;
  530. while(CAN_TSTAT_MST2 == (CAN_TSTAT(can_periph) & CAN_TSTAT_MST2)){
  531. }
  532. }else{
  533. /* illegal parameters */
  534. }
  535. }
  536. /*!
  537. \brief CAN receive message
  538. \param[in] can_periph
  539. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  540. \param[in] fifo_number
  541. \arg CAN_FIFOx(x=0,1)
  542. \param[out] receive_message: struct for CAN receive message
  543. \arg rx_sfid: 0x00000000 - 0x000007FF
  544. \arg rx_efid: 0x00000000 - 0x1FFFFFFF
  545. \arg rx_ff: CAN_FF_STANDARD, CAN_FF_EXTENDED
  546. \arg rx_ft: CAN_FT_DATA, CAN_FT_REMOTE
  547. \arg rx_dlen: 0 - 8
  548. \arg rx_data[]: 0x00 - 0xFF
  549. \arg rx_fi: 0 - 27
  550. \retval none
  551. */
  552. void can_message_receive(uint32_t can_periph, uint8_t fifo_number, can_receive_message_struct* receive_message)
  553. {
  554. /* get the frame format */
  555. receive_message->rx_ff = (uint8_t)(CAN_RFIFOMI_FF & CAN_RFIFOMI(can_periph, fifo_number));
  556. if(CAN_FF_STANDARD == receive_message->rx_ff){
  557. /* get standard identifier */
  558. receive_message->rx_sfid = (uint32_t)(GET_RFIFOMI_SFID(CAN_RFIFOMI(can_periph, fifo_number)));
  559. }else{
  560. /* get extended identifier */
  561. receive_message->rx_efid = (uint32_t)(GET_RFIFOMI_EFID(CAN_RFIFOMI(can_periph, fifo_number)));
  562. }
  563. /* get frame type */
  564. receive_message->rx_ft = (uint8_t)(CAN_RFIFOMI_FT & CAN_RFIFOMI(can_periph, fifo_number));
  565. /* filtering index */
  566. receive_message->rx_fi = (uint8_t)(GET_RFIFOMP_FI(CAN_RFIFOMP(can_periph, fifo_number)));
  567. /* get recevie data length */
  568. receive_message->rx_dlen = (uint8_t)(GET_RFIFOMP_DLENC(CAN_RFIFOMP(can_periph, fifo_number)));
  569. /* receive data */
  570. receive_message -> rx_data[0] = (uint8_t)(GET_RFIFOMDATA0_DB0(CAN_RFIFOMDATA0(can_periph, fifo_number)));
  571. receive_message -> rx_data[1] = (uint8_t)(GET_RFIFOMDATA0_DB1(CAN_RFIFOMDATA0(can_periph, fifo_number)));
  572. receive_message -> rx_data[2] = (uint8_t)(GET_RFIFOMDATA0_DB2(CAN_RFIFOMDATA0(can_periph, fifo_number)));
  573. receive_message -> rx_data[3] = (uint8_t)(GET_RFIFOMDATA0_DB3(CAN_RFIFOMDATA0(can_periph, fifo_number)));
  574. receive_message -> rx_data[4] = (uint8_t)(GET_RFIFOMDATA1_DB4(CAN_RFIFOMDATA1(can_periph, fifo_number)));
  575. receive_message -> rx_data[5] = (uint8_t)(GET_RFIFOMDATA1_DB5(CAN_RFIFOMDATA1(can_periph, fifo_number)));
  576. receive_message -> rx_data[6] = (uint8_t)(GET_RFIFOMDATA1_DB6(CAN_RFIFOMDATA1(can_periph, fifo_number)));
  577. receive_message -> rx_data[7] = (uint8_t)(GET_RFIFOMDATA1_DB7(CAN_RFIFOMDATA1(can_periph, fifo_number)));
  578. /* release FIFO */
  579. if(CAN_FIFO0 == fifo_number){
  580. CAN_RFIFO0(can_periph) |= CAN_RFIFO0_RFD0;
  581. }else{
  582. CAN_RFIFO1(can_periph) |= CAN_RFIFO1_RFD1;
  583. }
  584. }
  585. /*!
  586. \brief release FIFO0
  587. \param[in] can_periph
  588. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  589. \param[in] fifo_number
  590. only one parameter can be selected which is shown as below:
  591. \arg CAN_FIFOx(x=0,1)
  592. \param[out] none
  593. \retval none
  594. */
  595. void can_fifo_release(uint32_t can_periph, uint8_t fifo_number)
  596. {
  597. if(CAN_FIFO0 == fifo_number){
  598. CAN_RFIFO0(can_periph) |= CAN_RFIFO0_RFD0;
  599. }else if(CAN_FIFO1 == fifo_number){
  600. CAN_RFIFO1(can_periph) |= CAN_RFIFO1_RFD1;
  601. }else{
  602. /* illegal parameters */
  603. CAN_ERROR_HANDLE("CAN FIFO NUM is invalid \r\n");
  604. }
  605. }
  606. /*!
  607. \brief CAN receive message length
  608. \param[in] can_periph
  609. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  610. \param[in] fifo_number
  611. only one parameter can be selected which is shown as below:
  612. \arg CAN_FIFOx(x=0,1)
  613. \param[out] none
  614. \retval message length
  615. */
  616. uint8_t can_receive_message_length_get(uint32_t can_periph, uint8_t fifo_number)
  617. {
  618. uint8_t val = 0U;
  619. if(CAN_FIFO0 == fifo_number){
  620. /* FIFO0 */
  621. val = (uint8_t)(CAN_RFIFO0(can_periph) & CAN_RFIF_RFL_MASK);
  622. }else if(CAN_FIFO1 == fifo_number){
  623. /* FIFO1 */
  624. val = (uint8_t)(CAN_RFIFO1(can_periph) & CAN_RFIF_RFL_MASK);
  625. }else{
  626. /* illegal parameters */
  627. }
  628. return val;
  629. }
  630. /*!
  631. \brief set CAN working mode
  632. \param[in] can_periph
  633. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  634. \param[in] can_working_mode
  635. only one parameter can be selected which is shown as below:
  636. \arg CAN_MODE_INITIALIZE
  637. \arg CAN_MODE_NORMAL
  638. \arg CAN_MODE_SLEEP
  639. \param[out] none
  640. \retval ErrStatus: SUCCESS or ERROR
  641. */
  642. ErrStatus can_working_mode_set(uint32_t can_periph, uint8_t working_mode)
  643. {
  644. ErrStatus flag = ERROR;
  645. /* timeout for IWS or also for SLPWS bits */
  646. uint32_t timeout = CAN_TIMEOUT;
  647. if(CAN_MODE_INITIALIZE == working_mode){
  648. /* disable sleep mode */
  649. CAN_CTL(can_periph) &= (~(uint32_t)CAN_CTL_SLPWMOD);
  650. /* set initialize mode */
  651. CAN_CTL(can_periph) |= (uint8_t)CAN_CTL_IWMOD;
  652. /* wait the acknowledge */
  653. while((CAN_STAT_IWS != (CAN_STAT(can_periph) & CAN_STAT_IWS)) && (0U != timeout)){
  654. timeout--;
  655. }
  656. if(CAN_STAT_IWS != (CAN_STAT(can_periph) & CAN_STAT_IWS)){
  657. flag = ERROR;
  658. }else{
  659. flag = SUCCESS;
  660. }
  661. }else if(CAN_MODE_NORMAL == working_mode){
  662. /* enter normal mode */
  663. CAN_CTL(can_periph) &= ~(uint32_t)(CAN_CTL_SLPWMOD | CAN_CTL_IWMOD);
  664. /* wait the acknowledge */
  665. while((0U != (CAN_STAT(can_periph) & (CAN_STAT_IWS | CAN_STAT_SLPWS))) && (0U != timeout)){
  666. timeout--;
  667. }
  668. if(0U != (CAN_STAT(can_periph) & (CAN_STAT_IWS | CAN_STAT_SLPWS))){
  669. flag = ERROR;
  670. }else{
  671. flag = SUCCESS;
  672. }
  673. }else if(CAN_MODE_SLEEP == working_mode){
  674. /* disable initialize mode */
  675. CAN_CTL(can_periph) &= (~(uint32_t)CAN_CTL_IWMOD);
  676. /* set sleep mode */
  677. CAN_CTL(can_periph) |= (uint8_t)CAN_CTL_SLPWMOD;
  678. /* wait the acknowledge */
  679. while((CAN_STAT_SLPWS != (CAN_STAT(can_periph) & CAN_STAT_SLPWS)) && (0U != timeout)){
  680. timeout--;
  681. }
  682. if(CAN_STAT_SLPWS != (CAN_STAT(can_periph) & CAN_STAT_SLPWS)){
  683. flag = ERROR;
  684. }else{
  685. flag = SUCCESS;
  686. }
  687. }else{
  688. flag = ERROR;
  689. }
  690. return flag;
  691. }
  692. /*!
  693. \brief wake up CAN
  694. \param[in] can_periph
  695. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  696. \param[out] none
  697. \retval ErrStatus: SUCCESS or ERROR
  698. */
  699. ErrStatus can_wakeup(uint32_t can_periph)
  700. {
  701. ErrStatus flag = ERROR;
  702. uint32_t timeout = CAN_TIMEOUT;
  703. /* wakeup */
  704. CAN_CTL(can_periph) &= ~CAN_CTL_SLPWMOD;
  705. while((0U != (CAN_STAT(can_periph) & CAN_STAT_SLPWS)) && (0x00U != timeout)){
  706. timeout--;
  707. }
  708. /* check state */
  709. if(0U != (CAN_STAT(can_periph) & CAN_STAT_SLPWS)){
  710. flag = ERROR;
  711. }else{
  712. flag = SUCCESS;
  713. }
  714. return flag;
  715. }
  716. /*!
  717. \brief get CAN error type
  718. \param[in] can_periph
  719. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  720. \param[out] none
  721. \retval can_error_enum
  722. \arg CAN_ERROR_NONE: no error
  723. \arg CAN_ERROR_FILL: fill error
  724. \arg CAN_ERROR_FORMATE: format error
  725. \arg CAN_ERROR_ACK: ACK error
  726. \arg CAN_ERROR_BITRECESSIVE: bit recessive
  727. \arg CAN_ERROR_BITDOMINANTER: bit dominant error
  728. \arg CAN_ERROR_CRC: CRC error
  729. \arg CAN_ERROR_SOFTWARECFG: software configure
  730. */
  731. can_error_enum can_error_get(uint32_t can_periph)
  732. {
  733. can_error_enum error;
  734. error = CAN_ERROR_NONE;
  735. /* get error type */
  736. error = (can_error_enum)(GET_ERR_ERRN(CAN_ERR(can_periph)));
  737. return error;
  738. }
  739. /*!
  740. \brief get CAN receive error number
  741. \param[in] can_periph
  742. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  743. \param[out] none
  744. \retval error number
  745. */
  746. uint8_t can_receive_error_number_get(uint32_t can_periph)
  747. {
  748. uint8_t val;
  749. /* get error count */
  750. val = (uint8_t)(GET_ERR_RECNT(CAN_ERR(can_periph)));
  751. return val;
  752. }
  753. /*!
  754. \brief get CAN transmit error number
  755. \param[in] can_periph
  756. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  757. \param[out] none
  758. \retval error number
  759. */
  760. uint8_t can_transmit_error_number_get(uint32_t can_periph)
  761. {
  762. uint8_t val;
  763. val = (uint8_t)(GET_ERR_TECNT(CAN_ERR(can_periph)));
  764. return val;
  765. }
  766. /*!
  767. \brief enable CAN interrupt
  768. \param[in] can_periph
  769. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  770. \param[in] interrupt
  771. one or more parameters can be selected which are shown as below:
  772. \arg CAN_INT_TME: transmit mailbox empty interrupt enable
  773. \arg CAN_INT_RFNE0: receive FIFO0 not empty interrupt enable
  774. \arg CAN_INT_RFF0: receive FIFO0 full interrupt enable
  775. \arg CAN_INT_RFO0: receive FIFO0 overfull interrupt enable
  776. \arg CAN_INT_RFNE1: receive FIFO1 not empty interrupt enable
  777. \arg CAN_INT_RFF1: receive FIFO1 full interrupt enable
  778. \arg CAN_INT_RFO1: receive FIFO1 overfull interrupt enable
  779. \arg CAN_INT_WERR: warning error interrupt enable
  780. \arg CAN_INT_PERR: passive error interrupt enable
  781. \arg CAN_INT_BO: bus-off interrupt enable
  782. \arg CAN_INT_ERRN: error number interrupt enable
  783. \arg CAN_INT_ERR: error interrupt enable
  784. \arg CAN_INT_WAKEUP: wakeup interrupt enable
  785. \arg CAN_INT_SLPW: sleep working interrupt enable
  786. \param[out] none
  787. \retval none
  788. */
  789. void can_interrupt_enable(uint32_t can_periph, uint32_t interrupt)
  790. {
  791. CAN_INTEN(can_periph) |= interrupt;
  792. }
  793. /*!
  794. \brief disable CAN interrupt
  795. \param[in] can_periph
  796. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  797. \param[in] interrupt
  798. one or more parameters can be selected which are shown as below:
  799. \arg CAN_INT_TME: transmit mailbox empty interrupt enable
  800. \arg CAN_INT_RFNE0: receive FIFO0 not empty interrupt enable
  801. \arg CAN_INT_RFF0: receive FIFO0 full interrupt enable
  802. \arg CAN_INT_RFO0: receive FIFO0 overfull interrupt enable
  803. \arg CAN_INT_RFNE1: receive FIFO1 not empty interrupt enable
  804. \arg CAN_INT_RFF1: receive FIFO1 full interrupt enable
  805. \arg CAN_INT_RFO1: receive FIFO1 overfull interrupt enable
  806. \arg CAN_INT_WERR: warning error interrupt enable
  807. \arg CAN_INT_PERR: passive error interrupt enable
  808. \arg CAN_INT_BO: bus-off interrupt enable
  809. \arg CAN_INT_ERRN: error number interrupt enable
  810. \arg CAN_INT_ERR: error interrupt enable
  811. \arg CAN_INT_WAKEUP: wakeup interrupt enable
  812. \arg CAN_INT_SLPW: sleep working interrupt enable
  813. \param[out] none
  814. \retval none
  815. */
  816. void can_interrupt_disable(uint32_t can_periph, uint32_t interrupt)
  817. {
  818. CAN_INTEN(can_periph) &= ~interrupt;
  819. }
  820. /*!
  821. \brief get CAN flag state
  822. \param[in] can_periph
  823. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  824. \param[in] flag: CAN flags, refer to can_flag_enum
  825. only one parameter can be selected which is shown as below:
  826. \arg CAN_FLAG_RXL: RX level
  827. \arg CAN_FLAG_LASTRX: last sample value of RX pin
  828. \arg CAN_FLAG_RS: receiving state
  829. \arg CAN_FLAG_TS: transmitting state
  830. \arg CAN_FLAG_SLPIF: status change flag of entering sleep working mode
  831. \arg CAN_FLAG_WUIF: status change flag of wakeup from sleep working mode
  832. \arg CAN_FLAG_ERRIF: error flag
  833. \arg CAN_FLAG_SLPWS: sleep working state
  834. \arg CAN_FLAG_IWS: initial working state
  835. \arg CAN_FLAG_TMLS2: transmit mailbox 2 last sending in Tx FIFO
  836. \arg CAN_FLAG_TMLS1: transmit mailbox 1 last sending in Tx FIFO
  837. \arg CAN_FLAG_TMLS0: transmit mailbox 0 last sending in Tx FIFO
  838. \arg CAN_FLAG_TME2: transmit mailbox 2 empty
  839. \arg CAN_FLAG_TME1: transmit mailbox 1 empty
  840. \arg CAN_FLAG_TME0: transmit mailbox 0 empty
  841. \arg CAN_FLAG_MTE2: mailbox 2 transmit error
  842. \arg CAN_FLAG_MTE1: mailbox 1 transmit error
  843. \arg CAN_FLAG_MTE0: mailbox 0 transmit error
  844. \arg CAN_FLAG_MAL2: mailbox 2 arbitration lost
  845. \arg CAN_FLAG_MAL1: mailbox 1 arbitration lost
  846. \arg CAN_FLAG_MAL0: mailbox 0 arbitration lost
  847. \arg CAN_FLAG_MTFNERR2: mailbox 2 transmit finished with no error
  848. \arg CAN_FLAG_MTFNERR1: mailbox 1 transmit finished with no error
  849. \arg CAN_FLAG_MTFNERR0: mailbox 0 transmit finished with no error
  850. \arg CAN_FLAG_MTF2: mailbox 2 transmit finished
  851. \arg CAN_FLAG_MTF1: mailbox 1 transmit finished
  852. \arg CAN_FLAG_MTF0: mailbox 0 transmit finished
  853. \arg CAN_FLAG_RFO0: receive FIFO0 overfull
  854. \arg CAN_FLAG_RFF0: receive FIFO0 full
  855. \arg CAN_FLAG_RFO1: receive FIFO1 overfull
  856. \arg CAN_FLAG_RFF1: receive FIFO1 full
  857. \arg CAN_FLAG_BOERR: bus-off error
  858. \arg CAN_FLAG_PERR: passive error
  859. \arg CAN_FLAG_WERR: warning error
  860. \param[out] none
  861. \retval FlagStatus: SET or RESET
  862. */
  863. FlagStatus can_flag_get(uint32_t can_periph, can_flag_enum flag)
  864. {
  865. /* get flag and interrupt enable state */
  866. if(RESET != (CAN_REG_VAL(can_periph, flag) & BIT(CAN_BIT_POS(flag)))){
  867. return SET;
  868. }else{
  869. return RESET;
  870. }
  871. }
  872. /*!
  873. \brief clear CAN flag state
  874. \param[in] can_periph
  875. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  876. \param[in] flag: CAN flags, refer to can_flag_enum
  877. only one parameter can be selected which is shown as below:
  878. \arg CAN_FLAG_SLPIF: status change flag of entering sleep working mode
  879. \arg CAN_FLAG_WUIF: status change flag of wakeup from sleep working mode
  880. \arg CAN_FLAG_ERRIF: error flag
  881. \arg CAN_FLAG_MTE2: mailbox 2 transmit error
  882. \arg CAN_FLAG_MTE1: mailbox 1 transmit error
  883. \arg CAN_FLAG_MTE0: mailbox 0 transmit error
  884. \arg CAN_FLAG_MAL2: mailbox 2 arbitration lost
  885. \arg CAN_FLAG_MAL1: mailbox 1 arbitration lost
  886. \arg CAN_FLAG_MAL0: mailbox 0 arbitration lost
  887. \arg CAN_FLAG_MTFNERR2: mailbox 2 transmit finished with no error
  888. \arg CAN_FLAG_MTFNERR1: mailbox 1 transmit finished with no error
  889. \arg CAN_FLAG_MTFNERR0: mailbox 0 transmit finished with no error
  890. \arg CAN_FLAG_MTF2: mailbox 2 transmit finished
  891. \arg CAN_FLAG_MTF1: mailbox 1 transmit finished
  892. \arg CAN_FLAG_MTF0: mailbox 0 transmit finished
  893. \arg CAN_FLAG_RFO0: receive FIFO0 overfull
  894. \arg CAN_FLAG_RFF0: receive FIFO0 full
  895. \arg CAN_FLAG_RFO1: receive FIFO1 overfull
  896. \arg CAN_FLAG_RFF1: receive FIFO1 full
  897. \param[out] none
  898. \retval none
  899. */
  900. void can_flag_clear(uint32_t can_periph, can_flag_enum flag)
  901. {
  902. if (flag == CAN_FLAG_RFO1){
  903. CAN_REG_VAL(can_periph, flag) = RFO1_CLEAR_VAL;
  904. } else if (flag == CAN_FLAG_RFF1){
  905. CAN_REG_VAL(can_periph, flag) = RFF1_CLEAR_VAL;
  906. } else {
  907. CAN_REG_VAL(can_periph, flag) = BIT(CAN_BIT_POS(flag));
  908. }
  909. }
  910. /*!
  911. \brief get CAN interrupt flag state
  912. \param[in] can_periph
  913. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  914. \param[in] flag: CAN interrupt flags, refer to can_interrupt_flag_enum
  915. only one parameter can be selected which is shown as below:
  916. \arg CAN_INT_FLAG_SLPIF: status change interrupt flag of sleep working mode entering
  917. \arg CAN_INT_FLAG_WUIF: status change interrupt flag of wakeup from sleep working mode
  918. \arg CAN_INT_FLAG_ERRIF: error interrupt flag
  919. \arg CAN_INT_FLAG_MTF2: mailbox 2 transmit finished interrupt flag
  920. \arg CAN_INT_FLAG_MTF1: mailbox 1 transmit finished interrupt flag
  921. \arg CAN_INT_FLAG_MTF0: mailbox 0 transmit finished interrupt flag
  922. \arg CAN_INT_FLAG_RFO0: receive FIFO0 overfull interrupt flag
  923. \arg CAN_INT_FLAG_RFF0: receive FIFO0 full interrupt flag
  924. \arg CAN_INT_FLAG_RFL0: receive FIFO0 not empty interrupt flag
  925. \arg CAN_INT_FLAG_RFO1: receive FIFO1 overfull interrupt flag
  926. \arg CAN_INT_FLAG_RFF1: receive FIFO1 full interrupt flag
  927. \arg CAN_INT_FLAG_RFL1: receive FIFO1 not empty interrupt flag
  928. \arg CAN_INT_FLAG_ERRN: error number interrupt flag
  929. \arg CAN_INT_FLAG_BOERR: bus-off error interrupt flag
  930. \arg CAN_INT_FLAG_PERR: passive error interrupt flag
  931. \arg CAN_INT_FLAG_WERR: warning error interrupt flag
  932. \param[out] none
  933. \retval FlagStatus: SET or RESET
  934. */
  935. FlagStatus can_interrupt_flag_get(uint32_t can_periph, can_interrupt_flag_enum flag)
  936. {
  937. uint32_t ret1 = RESET;
  938. uint32_t ret2 = RESET;
  939. /* get the staus of interrupt flag */
  940. if (flag == CAN_INT_FLAG_RFL0) {
  941. ret1 = can_receive_message_length_get(can_periph, CAN_FIFO0);
  942. } else if (flag == CAN_INT_FLAG_RFL1) {
  943. ret1 = can_receive_message_length_get(can_periph, CAN_FIFO1);
  944. } else if (flag == CAN_INT_FLAG_ERRN) {
  945. ret1 = can_error_get(can_periph);
  946. } else {
  947. ret1 = CAN_REG_VALS(can_periph, flag) & BIT(CAN_BIT_POS0(flag));
  948. }
  949. /* get the staus of interrupt enale bit */
  950. ret2 = CAN_INTEN(can_periph) & BIT(CAN_BIT_POS1(flag));
  951. if(ret1 && ret2){
  952. return SET;
  953. }else{
  954. return RESET;
  955. }
  956. }
  957. /*!
  958. \brief clear CAN interrupt flag state
  959. \param[in] can_periph
  960. \arg CANx(x=0,1),the CAN1 only for GD32F10x_CL
  961. \param[in] flag: CAN interrupt flags, refer to can_interrupt_flag_enum
  962. only one parameter can be selected which is shown as below:
  963. \arg CAN_INT_FLAG_SLPIF: status change interrupt flag of sleep working mode entering
  964. \arg CAN_INT_FLAG_WUIF: status change interrupt flag of wakeup from sleep working mode
  965. \arg CAN_INT_FLAG_ERRIF: error interrupt flag
  966. \arg CAN_INT_FLAG_MTF2: mailbox 2 transmit finished interrupt flag
  967. \arg CAN_INT_FLAG_MTF1: mailbox 1 transmit finished interrupt flag
  968. \arg CAN_INT_FLAG_MTF0: mailbox 0 transmit finished interrupt flag
  969. \arg CAN_INT_FLAG_RFO0: receive FIFO0 overfull interrupt flag
  970. \arg CAN_INT_FLAG_RFF0: receive FIFO0 full interrupt flag
  971. \arg CAN_INT_FLAG_RFO1: receive FIFO1 overfull interrupt flag
  972. \arg CAN_INT_FLAG_RFF1: receive FIFO1 full interrupt flag
  973. \param[out] none
  974. \retval none
  975. */
  976. void can_interrupt_flag_clear(uint32_t can_periph, can_interrupt_flag_enum flag)
  977. {
  978. if (flag == CAN_INT_FLAG_RFO1){
  979. CAN_REG_VALS(can_periph, flag) = RFO1_CLEAR_VAL;
  980. } else if (flag == CAN_INT_FLAG_RFF1){
  981. CAN_REG_VALS(can_periph, flag) = RFF1_CLEAR_VAL;
  982. } else {
  983. CAN_REG_VALS(can_periph, flag) = BIT(CAN_BIT_POS0(flag));
  984. }
  985. }