main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*!
  2. \file main.c
  3. \brief communication_among_Devices 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. FlagStatus receive_flag;
  44. uint8_t transmit_number = 0x0;
  45. can_receive_message_struct receive_message;
  46. can_trasnmit_message_struct transmit_message;
  47. void nvic_config(void);
  48. void led_config(void);
  49. void gpio_config(void);
  50. ErrStatus can_networking(void);
  51. void can_networking_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. uint8_t i = 0;
  61. uint32_t timeout = 0xFFFF;
  62. uint8_t transmit_mailbox = 0;
  63. receive_flag = RESET;
  64. /* configure Tamper key */
  65. gd_eval_key_init(KEY_TAMPER, KEY_MODE_GPIO);
  66. /* configure GPIO */
  67. gpio_config();
  68. /* configure USART */
  69. gd_eval_com_init(EVAL_COM0);
  70. /* configure NVIC */
  71. nvic_config();
  72. /* configure leds */
  73. led_config();
  74. /* set all leds off */
  75. gd_eval_led_off(LED2);
  76. gd_eval_led_off(LED3);
  77. gd_eval_led_off(LED4);
  78. gd_eval_led_off(LED5);
  79. /* initialize CAN */
  80. can_networking_init();
  81. /* enable CAN receive FIFO1 not empty interrupt */
  82. can_interrupt_enable(CANX, CAN_INT_RFNE1);
  83. /* initialize transmit message */
  84. can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message);
  85. transmit_message.tx_sfid = 0x00;
  86. transmit_message.tx_efid = 0xaabb;
  87. transmit_message.tx_ft = CAN_FT_DATA;
  88. transmit_message.tx_ff = CAN_FF_EXTENDED;
  89. transmit_message.tx_dlen = 8;
  90. transmit_message.tx_data[0] = 0xA0;
  91. transmit_message.tx_data[1] = 0xA1;
  92. transmit_message.tx_data[2] = 0xA2;
  93. transmit_message.tx_data[3] = 0xA3;
  94. transmit_message.tx_data[4] = 0xA4;
  95. transmit_message.tx_data[5] = 0xA5;
  96. transmit_message.tx_data[6] = 0xA6;
  97. transmit_message.tx_data[7] = 0xA7;
  98. printf("Please press the Tamper key to transmit data!\r\n");
  99. /* initialize receive message */
  100. can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &receive_message);
  101. while(1){
  102. /* waiting for the Tamper key pressed */
  103. while(0 == gd_eval_key_state_get(KEY_TAMPER)){
  104. /* if transmit_number is 0x10, set it to 0x00 */
  105. if(transmit_number == 0x10){
  106. transmit_number = 0x00;
  107. }else{
  108. transmit_message.tx_data[0] = transmit_number++;
  109. printf("\r\n can transmit data:");
  110. for(i = 0; i < transmit_message.tx_dlen; i++){
  111. printf(" %02x", transmit_message.tx_data[i]);
  112. }
  113. /* transmit message */
  114. transmit_mailbox = can_message_transmit(CANX, &transmit_message);
  115. /* waiting for transmit completed */
  116. timeout = 0xFFFF;
  117. while((CAN_TRANSMIT_OK != can_transmit_states(CANX, transmit_mailbox)) && (0 != timeout)){
  118. timeout--;
  119. }
  120. /* waiting for Tamper key up */
  121. while(0 == gd_eval_key_state_get(KEY_TAMPER));
  122. }
  123. }
  124. if(SET == receive_flag){
  125. gd_eval_led_toggle(LED2);
  126. receive_flag = RESET;
  127. printf("\r\n can receive data:");
  128. for(i = 0; i < receive_message.rx_dlen; i++){
  129. printf(" %02x", receive_message.rx_data[i]);
  130. }
  131. }
  132. }
  133. }
  134. /*!
  135. \brief initialize CAN and filter
  136. \param[in] can_parameter
  137. \arg can_parameter_struct
  138. \param[in] can_filter
  139. \arg can_filter_parameter_struct
  140. \param[out] none
  141. \retval none
  142. */
  143. void can_networking_init(void)
  144. {
  145. can_parameter_struct can_parameter;
  146. can_filter_parameter_struct can_filter;
  147. can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  148. can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
  149. /* initialize CAN register */
  150. can_deinit(CANX);
  151. /* initialize CAN */
  152. can_parameter.time_triggered = DISABLE;
  153. can_parameter.auto_bus_off_recovery = ENABLE;
  154. can_parameter.auto_wake_up = DISABLE;
  155. can_parameter.auto_retrans = ENABLE;
  156. can_parameter.rec_fifo_overwrite = DISABLE;
  157. can_parameter.trans_fifo_order = DISABLE;
  158. can_parameter.working_mode = CAN_NORMAL_MODE;
  159. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  160. can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
  161. can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
  162. /* baudrate 1Mbps */
  163. can_parameter.prescaler = 6;
  164. can_init(CANX, &can_parameter);
  165. /* initialize filter */
  166. #ifdef DEV_CAN0_USED
  167. /* CAN0 filter number */
  168. can_filter.filter_number = 0;
  169. #else
  170. /* CAN1 filter number */
  171. can_filter.filter_number = 15;
  172. #endif
  173. /* initialize filter */
  174. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  175. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  176. can_filter.filter_list_high = 0x0000;
  177. can_filter.filter_list_low = 0x0000;
  178. can_filter.filter_mask_high = 0x0000;
  179. can_filter.filter_mask_low = 0x0000;
  180. can_filter.filter_fifo_number = CAN_FIFO1;
  181. can_filter.filter_enable = ENABLE;
  182. can_filter_init(&can_filter);
  183. }
  184. /*!
  185. \brief configure the nested vectored interrupt controller
  186. \param[in] none
  187. \param[out] none
  188. \retval none
  189. */
  190. void nvic_config(void)
  191. {
  192. #ifdef DEV_CAN0_USED
  193. /* configure CAN0 NVIC */
  194. nvic_irq_enable(CAN0_RX1_IRQn,0,0);
  195. #else
  196. /* configure CAN1 NVIC */
  197. nvic_irq_enable(CAN1_RX1_IRQn,0,0);
  198. #endif
  199. }
  200. /*!
  201. \brief configure the leds
  202. \param[in] none
  203. \param[out] none
  204. \retval none
  205. */
  206. void led_config(void)
  207. {
  208. gd_eval_led_init(LED2);
  209. gd_eval_led_init(LED3);
  210. gd_eval_led_init(LED4);
  211. gd_eval_led_init(LED5);
  212. }
  213. /*!
  214. \brief configure GPIO
  215. \param[in] none
  216. \param[out] none
  217. \retval none
  218. */
  219. void gpio_config(void)
  220. {
  221. /* enable CAN clock */
  222. rcu_periph_clock_enable(RCU_CAN0);
  223. #ifdef GD32F30X_CL
  224. rcu_periph_clock_enable(RCU_CAN1);
  225. rcu_periph_clock_enable(RCU_GPIOB);
  226. #endif
  227. rcu_periph_clock_enable(RCU_GPIOD);
  228. rcu_periph_clock_enable(RCU_AF);
  229. /* configure CAN0 GPIO */
  230. gpio_init(GPIOD,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
  231. gpio_init(GPIOD,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  232. gpio_pin_remap_config(GPIO_CAN0_FULL_REMAP,ENABLE);
  233. #ifdef GD32F30X_CL
  234. /* configure CAN1 GPIO */
  235. gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_5);
  236. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  237. gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
  238. #endif
  239. }
  240. /* retarget the C library printf function to the usart */
  241. int fputc(int ch, FILE *f)
  242. {
  243. usart_data_transmit(EVAL_COM0, (uint8_t) ch);
  244. while (RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
  245. return ch;
  246. }