main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*!
  2. \file main.c
  3. \brief communication_among_CANS 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 baudrate */
  36. /* 1MBps */
  37. #define CAN_BAUDRATE 1000
  38. /* 500kBps */
  39. /* #define CAN_BAUDRATE 500 */
  40. /* 250kBps */
  41. /* #define CAN_BAUDRATE 250 */
  42. /* 125kBps */
  43. /* #define CAN_BAUDRATE 125 */
  44. /* 100kBps */
  45. /* #define CAN_BAUDRATE 100 */
  46. /* 50kBps */
  47. /* #define CAN_BAUDRATE 50 */
  48. /* 20kBps */
  49. /* #define CAN_BAUDRATE 20 */
  50. FlagStatus can0_receive_flag;
  51. FlagStatus can1_receive_flag;
  52. FlagStatus can0_error_flag;
  53. FlagStatus can1_error_flag;
  54. can_trasnmit_message_struct transmit_message;
  55. can_receive_message_struct receive_message;
  56. void nvic_config(void);
  57. void led_config(void);
  58. void can_gpio_config(void);
  59. void can_config(void);
  60. /*!
  61. \brief main function
  62. \param[in] none
  63. \param[out] none
  64. \retval none
  65. */
  66. int main(void)
  67. {
  68. uint8_t i = 0;
  69. uint32_t timeout = 0xFFFF;
  70. uint8_t transmit_mailbox = 0;
  71. can0_receive_flag = RESET;
  72. can1_receive_flag = RESET;
  73. can0_error_flag = RESET;
  74. can1_error_flag = RESET;
  75. /* configure GPIO */
  76. can_gpio_config();
  77. /* configure NVIC */
  78. nvic_config();
  79. /* configure USART */
  80. gd_eval_com_init(EVAL_COM0);
  81. /* configure Wakeup key or Tamper key */
  82. gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
  83. gd_eval_key_init(KEY_TAMPER, KEY_MODE_GPIO);
  84. printf("\r\nGD32F30x dual CAN test, please press Wakeup key or Tamper key to start communication!\r\n");
  85. /* configure leds */
  86. led_config();
  87. gd_eval_led_off(LED2);
  88. gd_eval_led_off(LED3);
  89. gd_eval_led_off(LED4);
  90. gd_eval_led_off(LED5);
  91. /* initialize CAN and filter */
  92. can_config();
  93. /* enable can receive FIFO0 not empty interrupt */
  94. can_interrupt_enable(CAN0, CAN_INT_RFNE0);
  95. can_interrupt_enable(CAN1, CAN_INT_RFNE0);
  96. /* initialize transmit message */
  97. transmit_message.tx_sfid = 0x7ab;
  98. transmit_message.tx_efid = 0x00;
  99. transmit_message.tx_ft = CAN_FT_DATA;
  100. transmit_message.tx_ff = CAN_FF_STANDARD;
  101. transmit_message.tx_dlen = 8;
  102. transmit_message.tx_data[0] = 0x00;
  103. transmit_message.tx_data[1] = 0xA1;
  104. transmit_message.tx_data[2] = 0xA2;
  105. transmit_message.tx_data[3] = 0xA3;
  106. transmit_message.tx_data[4] = 0xA4;
  107. transmit_message.tx_data[5] = 0xA5;
  108. transmit_message.tx_data[6] = 0xA6;
  109. transmit_message.tx_data[7] = 0xA7;
  110. while(1){
  111. /* test whether the Tamper key is pressed */
  112. if(0 == gd_eval_key_state_get(KEY_TAMPER)){
  113. transmit_message.tx_data[0] = 0x55;
  114. transmit_message.tx_data[1] = 0xAA;
  115. printf("\r\n can0 transmit data:");
  116. for(i = 0; i < transmit_message.tx_dlen; i++){
  117. printf(" %02x", transmit_message.tx_data[i]);
  118. }
  119. /* transmit message */
  120. transmit_mailbox = can_message_transmit(CAN0, &transmit_message);
  121. /* waiting for transmit completed */
  122. timeout = 0xFFFF;
  123. while((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && (0 != timeout)){
  124. timeout--;
  125. }
  126. /* waiting for the Tamper key up */
  127. while(0 == gd_eval_key_state_get(KEY_TAMPER));
  128. }
  129. /* test whether the Wakeup key is pressed */
  130. if(0 == gd_eval_key_state_get(KEY_WAKEUP)){
  131. transmit_message.tx_data[0] = 0xAA;
  132. transmit_message.tx_data[1] = 0x55;
  133. printf("\r\n can1 transmit data:");
  134. for(i = 0; i < transmit_message.tx_dlen; i++){
  135. printf(" %02x", transmit_message.tx_data[i]);
  136. }
  137. /* transmit message */
  138. transmit_mailbox = can_message_transmit(CAN1, &transmit_message);
  139. /* waiting for transmit completed */
  140. timeout = 0xFFFF;
  141. while((CAN_TRANSMIT_OK != can_transmit_states(CAN1, transmit_mailbox)) && (0 != timeout)){
  142. timeout--;
  143. }
  144. /* waiting for the Wakeup key up */
  145. while(0 == gd_eval_key_state_get(KEY_WAKEUP));
  146. }
  147. /* CAN0 receive data correctly, the received data is printed */
  148. if(SET == can0_receive_flag){
  149. can0_receive_flag = RESET;
  150. printf("\r\n can0 receive data:");
  151. for(i = 0; i < receive_message.rx_dlen; i++){
  152. printf(" %02x", receive_message.rx_data[i]);
  153. }
  154. gd_eval_led_toggle(LED4);
  155. }
  156. /* CAN1 receive data correctly, the received data is printed */
  157. if(SET == can1_receive_flag){
  158. can1_receive_flag = RESET;
  159. gd_eval_led_toggle(LED5);
  160. printf("\r\n can1 receive data:");
  161. for(i = 0; i < receive_message.rx_dlen; i++){
  162. printf(" %02x", receive_message.rx_data[i]);
  163. }
  164. }
  165. /* CAN0 error */
  166. if(SET == can0_error_flag){
  167. can0_error_flag = RESET;
  168. printf("\r\n can0 communication error");
  169. }
  170. /* CAN1 error */
  171. if(SET == can1_error_flag){
  172. can1_error_flag = RESET;
  173. printf("\r\n can1 communication error");
  174. }
  175. }
  176. }
  177. /*!
  178. \brief initialize CAN and filter
  179. \param[in] can_parameter
  180. \arg can_parameter_struct
  181. \param[in] can_filter
  182. \arg can_filter_parameter_struct
  183. \param[out] none
  184. \retval none
  185. */
  186. void can_config()
  187. {
  188. can_parameter_struct can_parameter;
  189. can_filter_parameter_struct can_filter;
  190. can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  191. can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
  192. /* initialize CAN register */
  193. can_deinit(CAN0);
  194. can_deinit(CAN1);
  195. /* initialize CAN parameters */
  196. can_parameter.time_triggered = DISABLE;
  197. can_parameter.auto_bus_off_recovery = ENABLE;
  198. can_parameter.auto_wake_up = DISABLE;
  199. can_parameter.auto_retrans = ENABLE;
  200. can_parameter.rec_fifo_overwrite = DISABLE;
  201. can_parameter.trans_fifo_order = DISABLE;
  202. can_parameter.working_mode = CAN_NORMAL_MODE;
  203. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  204. can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
  205. can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
  206. /* 1MBps */
  207. #if CAN_BAUDRATE == 1000
  208. can_parameter.prescaler = 6;
  209. /* 500KBps */
  210. #elif CAN_BAUDRATE == 500
  211. can_parameter.prescaler = 12;
  212. /* 250KBps */
  213. #elif CAN_BAUDRATE == 250
  214. can_parameter.prescaler = 24;
  215. /* 125KBps */
  216. #elif CAN_BAUDRATE == 125
  217. can_parameter.prescaler = 48;
  218. /* 100KBps */
  219. #elif CAN_BAUDRATE == 100
  220. can_parameter.prescaler = 60;
  221. /* 50KBps */
  222. #elif CAN_BAUDRATE == 50
  223. can_parameter.prescaler = 120;
  224. /* 20KBps */
  225. #elif CAN_BAUDRATE == 20
  226. can_parameter.prescaler = 300;
  227. #else
  228. #error "please select list can baudrate in private defines in main.c "
  229. #endif
  230. /* initialize CAN */
  231. can_init(CAN0, &can_parameter);
  232. can_init(CAN1, &can_parameter);
  233. /* initialize filter */
  234. can_filter.filter_number=0;
  235. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  236. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  237. can_filter.filter_list_high = 0x0000;
  238. can_filter.filter_list_low = 0x0000;
  239. can_filter.filter_mask_high = 0x0000;
  240. can_filter.filter_mask_low = 0x0000;
  241. can_filter.filter_fifo_number = CAN_FIFO0;
  242. can_filter.filter_enable = ENABLE;
  243. can_filter_init(&can_filter);
  244. /* CAN1 filter number */
  245. can_filter.filter_number = 15;
  246. can_filter_init(&can_filter);
  247. }
  248. /*!
  249. \brief configure the nested vectored interrupt controller
  250. \param[in] none
  251. \param[out] none
  252. \retval none
  253. */
  254. void nvic_config(void)
  255. {
  256. /* configure CAN0 NVIC */
  257. nvic_irq_enable(CAN0_RX0_IRQn,0,0);
  258. /* configure CAN1 NVIC */
  259. nvic_irq_enable(CAN1_RX0_IRQn,1,1);
  260. }
  261. /*!
  262. \brief configure the leds
  263. \param[in] none
  264. \param[out] none
  265. \retval none
  266. */
  267. void led_config(void)
  268. {
  269. gd_eval_led_init(LED2);
  270. gd_eval_led_init(LED3);
  271. gd_eval_led_init(LED4);
  272. gd_eval_led_init(LED5);
  273. }
  274. /*!
  275. \brief configure GPIO
  276. \param[in] none
  277. \param[out] none
  278. \retval none
  279. */
  280. void can_gpio_config(void)
  281. {
  282. /* enable CAN clock */
  283. rcu_periph_clock_enable(RCU_CAN0);
  284. rcu_periph_clock_enable(RCU_CAN1);
  285. rcu_periph_clock_enable(RCU_GPIOB);
  286. rcu_periph_clock_enable(RCU_GPIOD);
  287. rcu_periph_clock_enable(RCU_AF);
  288. /* configure CAN0 GPIO */
  289. gpio_init(GPIOD,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
  290. gpio_init(GPIOD,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  291. gpio_pin_remap_config(GPIO_CAN0_FULL_REMAP,ENABLE);
  292. /* configure CAN1 GPIO */
  293. gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_5);
  294. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  295. gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
  296. }
  297. /* retarget the C library printf function to the usart */
  298. int fputc(int ch, FILE *f)
  299. {
  300. usart_data_transmit(EVAL_COM0, (uint8_t)ch);
  301. while (RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
  302. return ch;
  303. }