main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*!
  2. \file main.c
  3. \brief communication_Loopback in normal mode
  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 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include <stdio.h>
  34. #include "gd32f307c_eval.h"
  35. /* select CAN */
  36. #define DEV_CAN0_USED
  37. //#define DEV_CAN1_USED
  38. #ifdef DEV_CAN0_USED
  39. #define CANX CAN0
  40. #else
  41. #define CANX CAN1
  42. #endif
  43. #define DEV_CAN_POLLING_ID 0x0AA
  44. #define DEV_CAN_INTERRUPT_ID 0x1234
  45. volatile ErrStatus test_flag;
  46. volatile ErrStatus test_flag_interrupt;
  47. void nvic_config(void);
  48. void led_config(void);
  49. ErrStatus can_loopback(void);
  50. ErrStatus can_loopback_interrupt(void);
  51. void can_loopback_init(void);
  52. /*!
  53. \brief main function
  54. \param[in] none
  55. \param[out] none
  56. \retval none
  57. */
  58. int main(void)
  59. {
  60. /* enable CAN clock */
  61. rcu_periph_clock_enable(RCU_CAN0);
  62. rcu_periph_clock_enable(RCU_CAN1);
  63. /* configure NVIC */
  64. nvic_config();
  65. /* configure leds */
  66. led_config();
  67. /* set all the leds off */
  68. gd_eval_led_off(LED2);
  69. gd_eval_led_off(LED3);
  70. gd_eval_led_off(LED4);
  71. gd_eval_led_off(LED5);
  72. /* loopback of polling */
  73. test_flag = can_loopback();
  74. if(SUCCESS == test_flag){
  75. /* loopback test is success */
  76. gd_eval_led_on(LED2);
  77. gd_eval_led_on(LED3);
  78. }else{
  79. /* loopback test is failed */
  80. gd_eval_led_off(LED2);
  81. gd_eval_led_off(LED3);
  82. }
  83. /* loopback of interrupt */
  84. test_flag_interrupt = can_loopback_interrupt();
  85. if(SUCCESS == test_flag_interrupt){
  86. /* interrupt loopback test is success */
  87. gd_eval_led_on(LED4);
  88. gd_eval_led_on(LED5);
  89. }else{
  90. /* interrupt loopback test is failed */
  91. gd_eval_led_off(LED4);
  92. gd_eval_led_off(LED5);
  93. }
  94. while (1);
  95. }
  96. /*!
  97. \brief function for CAN loopback communication
  98. \param[in] none
  99. \param[out] none
  100. \retval ErrStatus
  101. */
  102. ErrStatus can_loopback(void)
  103. {
  104. can_trasnmit_message_struct transmit_message;
  105. can_receive_message_struct receive_message;
  106. uint32_t timeout = 0xFFFF;
  107. uint8_t transmit_mailbox = 0;
  108. /* initialize CAN */
  109. can_loopback_init();
  110. /* initialize transmit message */
  111. can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message);
  112. transmit_message.tx_sfid = DEV_CAN_POLLING_ID;
  113. transmit_message.tx_ft = CAN_FT_DATA;
  114. transmit_message.tx_ff = CAN_FF_STANDARD;
  115. transmit_message.tx_dlen = 2;
  116. transmit_message.tx_data[0] = 0xAB;
  117. transmit_message.tx_data[1] = 0xCD;
  118. /* initialize receive message */
  119. can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &receive_message);
  120. /* transmit message */
  121. transmit_mailbox = can_message_transmit(CANX, &transmit_message);
  122. /* waiting for transmit completed */
  123. while((CAN_TRANSMIT_OK != can_transmit_states(CANX, transmit_mailbox)) && (0 != timeout)){
  124. timeout--;
  125. }
  126. timeout = 0xFFFF;
  127. /* waiting for receive completed */
  128. while((can_receive_message_length_get(CANX, CAN_FIFO1) < 1) && (0 != timeout)){
  129. timeout--;
  130. }
  131. can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &receive_message);
  132. can_message_receive(CANX, CAN_FIFO1, &receive_message);
  133. /* check the receive message */
  134. if((DEV_CAN_POLLING_ID == receive_message.rx_sfid) && (CAN_FF_STANDARD == receive_message.rx_ff)
  135. && (2 == receive_message.rx_dlen) && (0xCDAB == (receive_message.rx_data[1]<<8|receive_message.rx_data[0]))){
  136. return SUCCESS;
  137. }else{
  138. return ERROR;
  139. }
  140. }
  141. /*!
  142. \brief function for CAN loopback interrupt communication
  143. \param[in] none
  144. \param[out] none
  145. \retval ErrStatus
  146. */
  147. ErrStatus can_loopback_interrupt(void)
  148. {
  149. can_trasnmit_message_struct transmit_message;
  150. uint32_t timeout = 0x0000FFFF;
  151. /* initialize CAN and filter */
  152. can_loopback_init();
  153. /* enable CAN receive FIFO1 not empty interrupt */
  154. can_interrupt_enable(CANX, CAN_INT_RFNE1);
  155. /* initialize transmit message */
  156. transmit_message.tx_sfid = 0;
  157. transmit_message.tx_efid = DEV_CAN_INTERRUPT_ID;
  158. transmit_message.tx_ff = CAN_FF_EXTENDED;
  159. transmit_message.tx_ft = CAN_FT_DATA;
  160. transmit_message.tx_dlen = 2;
  161. transmit_message.tx_data[0] = 0xDE;
  162. transmit_message.tx_data[1] = 0xCA;
  163. /* transmit a message */
  164. can_message_transmit(CANX, &transmit_message);
  165. /* waiting for receive completed */
  166. while((SUCCESS != test_flag_interrupt) && (0 != timeout)){
  167. timeout--;
  168. }
  169. if(0 == timeout){
  170. test_flag_interrupt = ERROR;
  171. }
  172. /* disable CAN receive FIFO1 not empty interrupt */
  173. can_interrupt_disable(CANX, CAN_INTEN_RFNEIE1);
  174. return test_flag_interrupt;
  175. }
  176. /*!
  177. \brief initialize CAN and filter
  178. \param[in] can_parameter
  179. \arg can_parameter_struct
  180. \param[in] can_filter
  181. \arg can_filter_parameter_struct
  182. \param[out] none
  183. \retval none
  184. */
  185. void can_loopback_init(void)
  186. {
  187. can_parameter_struct can_parameter;
  188. can_filter_parameter_struct can_filter;
  189. can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  190. can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
  191. /* initialize CAN register */
  192. can_deinit(CANX);
  193. /* initialize CAN */
  194. can_parameter.time_triggered = DISABLE;
  195. can_parameter.auto_bus_off_recovery = ENABLE;
  196. can_parameter.auto_wake_up = DISABLE;
  197. can_parameter.auto_retrans = ENABLE;
  198. can_parameter.rec_fifo_overwrite = DISABLE;
  199. can_parameter.trans_fifo_order = DISABLE;
  200. can_parameter.working_mode = CAN_LOOPBACK_MODE;
  201. /* configure baudrate to 125kbps */
  202. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  203. can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
  204. can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
  205. can_parameter.prescaler = 48;
  206. can_init(CANX, &can_parameter);
  207. /* initialize filter */
  208. #ifdef DEV_CAN0_USED
  209. /* CAN0 filter number */
  210. can_filter.filter_number = 0;
  211. #else
  212. /* CAN1 filter number */
  213. can_filter.filter_number = 14;
  214. #endif
  215. /* initialize filter */
  216. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  217. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  218. can_filter.filter_list_high = (uint16_t)(DEV_CAN_POLLING_ID << 5);
  219. can_filter.filter_list_low = 0x0000;
  220. /* ID and standard frame matched */
  221. can_filter.filter_mask_high = (uint16_t)(0x7FF << 5);
  222. can_filter.filter_mask_low = (uint16_t)(1U << 2);
  223. can_filter.filter_fifo_number = CAN_FIFO1;
  224. can_filter.filter_enable=ENABLE;
  225. can_filter_init(&can_filter);
  226. /* initialize filter */
  227. #ifdef DEV_CAN0_USED
  228. /* CAN0 filter number */
  229. can_filter.filter_number = 1;
  230. #else
  231. /* CAN1 filter number */
  232. can_filter.filter_number = 15;
  233. #endif
  234. /* initialize filter */
  235. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  236. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  237. /* ID and extend frame matched */
  238. can_filter.filter_list_high = (uint16_t)(DEV_CAN_INTERRUPT_ID >> 13);
  239. can_filter.filter_list_low = (uint16_t)(((uint16_t)DEV_CAN_INTERRUPT_ID << 3) | (1U << 2));
  240. can_filter.filter_mask_high = (uint16_t)(0x1FFFFFFF >> 13);
  241. can_filter.filter_mask_low = (uint16_t)(((uint16_t)0x1FFFFFFF << 3) | (1U << 2));
  242. can_filter.filter_fifo_number = CAN_FIFO1;
  243. can_filter.filter_enable=ENABLE;
  244. can_filter_init(&can_filter);
  245. }
  246. /*!
  247. \brief configure the nested vectored interrupt controller
  248. \param[in] none
  249. \param[out] none
  250. \retval none
  251. */
  252. void nvic_config(void)
  253. {
  254. /* configure CAN0 NVIC */
  255. nvic_irq_enable(CAN0_RX1_IRQn,0,0);
  256. /* configure CAN1 NVIC */
  257. nvic_irq_enable(CAN1_RX1_IRQn,0,0);
  258. }
  259. /*!
  260. \brief configure the leds
  261. \param[in] none
  262. \param[out] none
  263. \retval none
  264. */
  265. void led_config(void)
  266. {
  267. gd_eval_led_init(LED2);
  268. gd_eval_led_init(LED3);
  269. gd_eval_led_init(LED4);
  270. gd_eval_led_init(LED5);
  271. }