当前位置:   article > 正文

RT-Thread基于AT32单片机的CAN应用_at32 workbench

at32 workbench

1 硬件电路

2 RT-Thread驱动配置

RT-Studio中没有CAN相关的图形配置,需要手动修改board.h。在board.h的末尾,增加相关的BSP配置。

  1. #define RT_CAN_USING_HDR
  2. #define BSP_USING_CAN1

3 IO配置

at32_msp.c中的IO配置是PB9和PB10,掌上实验室V9实际采用的是PD0和PD1,需要修改CAN1相关的IO配置代码。

IO配置代码可以采用AT32_workbench生成,如下图所示。

at32a403a_wk_config.c中找到相关代码,修改RT-Studio中的at32_msp.c的相关代码,如下所示:

  1. void at32_msp_can_init(void *instance)
  2. {
  3. #if defined (BSP_USING_CAN1) || defined (BSP_USING_CAN2)
  4. gpio_init_type gpio_init_struct;
  5. can_type *can_x = (can_type *)instance;
  6. gpio_default_para_init(&gpio_init_struct);
  7. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  8. #ifdef BSP_USING_CAN1
  9. if(CAN1 == can_x)
  10. {
  11. crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
  12. // crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  13. // crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  14. //
  15. // gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  16. // gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  17. // gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  18. // gpio_init_struct.gpio_pins = GPIO_PINS_9;
  19. // gpio_init(GPIOB, &gpio_init_struct);
  20. // gpio_pin_remap_config(CAN1_GMUX_0010, TRUE);
  21. //
  22. // gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  23. // gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  24. // gpio_init_struct.gpio_pins = GPIO_PINS_8;
  25. // gpio_init(GPIOB, &gpio_init_struct);
  26. crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
  27. /* configure the CAN1 TX pin */
  28. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  29. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  30. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  31. gpio_init_struct.gpio_pins = GPIO_PINS_1;
  32. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  33. gpio_init(GPIOD, &gpio_init_struct);
  34. /* configure the CAN1 RX pin */
  35. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  36. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  37. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  38. gpio_init_struct.gpio_pins = GPIO_PINS_0;
  39. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  40. gpio_init(GPIOD, &gpio_init_struct);
  41. /* GPIO PIN remap */
  42. gpio_pin_remap_config(CAN1_GMUX_0011, TRUE);
  43. }
  44. #endif
  45. #ifdef BSP_USING_CAN2
  46. if(CAN2 == can_x)
  47. {
  48. crm_periph_clock_enable(CRM_CAN2_PERIPH_CLOCK, TRUE);
  49. crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  50. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  51. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  52. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  53. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  54. gpio_init_struct.gpio_pins = GPIO_PINS_6;
  55. gpio_init(GPIOB, &gpio_init_struct);
  56. gpio_pin_remap_config(CAN2_GMUX_0001, TRUE);
  57. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  58. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  59. gpio_init_struct.gpio_pins = GPIO_PINS_5;
  60. gpio_init(GPIOB, &gpio_init_struct);
  61. }
  62. #endif
  63. #endif
  64. }
  65. void at32_msp_emac_init(void *instance)
  66. {
  67. #if defined (BSP_USING_EMAC)
  68. gpio_init_type gpio_init_struct;
  69. crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  70. crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  71. crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  72. crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
  73. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  74. gpio_pin_remap_config(EMAC_MUX, TRUE);
  75. gpio_default_para_init(&gpio_init_struct);
  76. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  77. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  78. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  79. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  80. gpio_init_struct.gpio_pins = GPIO_PINS_2;
  81. gpio_init(GPIOA, &gpio_init_struct);
  82. gpio_init_struct.gpio_pins = GPIO_PINS_11 | GPIO_PINS_12 | GPIO_PINS_13;
  83. gpio_init(GPIOB, &gpio_init_struct);
  84. gpio_init_struct.gpio_pins = GPIO_PINS_1;
  85. gpio_init(GPIOC, &gpio_init_struct);
  86. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  87. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  88. gpio_init_struct.gpio_pins = GPIO_PINS_1;
  89. gpio_init(GPIOA, &gpio_init_struct);
  90. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  91. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  92. gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10;
  93. gpio_init(GPIOD, &gpio_init_struct);
  94. #endif
  95. }

4 时钟配置

drv_can.c中给出了can的bitrate配置代码,如下所示:

  1. #ifdef SOC_SERIES_AT32F403A
  2. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 120 / ((1 + 8 + 3) * 10) = 1MHz*/
  3. /* attention !!! default apbclk 120 mhz */
  4. static const struct at32_baud_rate can_baud_rate_tab[] =
  5. {
  6. {CAN1MBaud, {10 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  7. {CAN800kBaud, {15 , CAN_RSAW_2TQ, CAN_BTS1_7TQ, CAN_BTS2_2TQ}},
  8. {CAN500kBaud, {20 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  9. {CAN250kBaud, {40 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  10. {CAN125kBaud, {80 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  11. {CAN100kBaud, {75 , CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  12. {CAN50kBaud, {150, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  13. {CAN20kBaud, {375, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  14. {CAN10kBaud, {750, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}}
  15. };

这里要特别注意的是,所有计算是基于apbclk=120MHz。要确认RT-Studio生成的代码的时钟正确,否则需重新配置时钟或修改at32_baud_rate can_baud_rate_tab表格内容。

5 RT-Thread应用示例

  1. #include <rtthread.h>
  2. #include "rtdevice.h"
  3. #ifdef RT_USING_CAN
  4. #define CAN_DEV_NAME "can1" /* CAN 设备名称 */
  5. static struct rt_semaphore rx_sem; /* 用于接收消息的信号量 */
  6. static rt_device_t can_dev; /* CAN 设备句柄 */
  7. #define THREAD_PRIORITY 25
  8. #define THREAD_STACK_SIZE 512
  9. #define THREAD_TIMESLICE 5
  10. static rt_thread_t tid1 = RT_NULL;
  11. static volatile int running = 0;
  12. static int data_buf[10];
  13. static uint32_t data_cnt = 0;
  14. rt_err_t lp40_recv(uint16_t id, uint8_t *msg)
  15. {
  16. if(crc_high_first(msg,6)){
  17. }
  18. return RT_EOK;
  19. }
  20. /* 接收数据回调函数 */
  21. static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size) {
  22. /* CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
  23. rt_sem_release(&rx_sem);
  24. return RT_EOK;
  25. }
  26. static void can_rx_thread(void *parameter) {
  27. int i;
  28. //rt_err_t res;
  29. struct rt_can_msg rxmsg = {0};
  30. /* 设置接收回调函数 */
  31. rt_device_set_rx_indicate(can_dev, can_rx_call);
  32. #ifdef RT_CAN_USING_HDR
  33. struct rt_can_filter_item items[5] = {
  34. RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 1, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,hdr 为 - 1,设置默认过滤表 */
  35. RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 1, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x300~0x3ff,hdr 为 - 1 */
  36. RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 1, 0x7ff, RT_NULL, RT_NULL), /* std,match ID:0x211,hdr 为 - 1 */
  37. RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), /* std,match ID:0x486,hdr 为 - 1 */
  38. {0x555, 0, 0, 1, 0x7ff, 7,} /* std,match ID:0x555,hdr 为 7,指定设置 7 号过滤表 */
  39. };
  40. struct rt_can_filter_config cfg = {5, 1, items}; /* 一共有 5 个过滤表 */
  41. /* 设置硬件过滤表 */
  42. res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
  43. RT_ASSERT(res == RT_EOK);
  44. #endif
  45. int rx_count = 0;
  46. while (running) {
  47. /* hdr 值为 - 1,表示直接从 uselist 链表读取数据 */
  48. rxmsg.hdr_index = -1;
  49. /* 阻塞等待接收信号量 */
  50. if(rt_sem_take(&rx_sem, RT_WAITING_FOREVER)==RT_EOK){
  51. /* 从 CAN 读取一帧数据 */
  52. rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
  53. /* 打印数据 ID 及内容 */
  54. rt_kprintf("recv %ld : id = %d, ide=%d :", ++rx_count, rxmsg.id, rxmsg.ide);
  55. for (i = 0; i < rxmsg.len; i++) {
  56. rt_kprintf(" %02x", rxmsg.data[i]);
  57. }
  58. rt_kprintf("\n");
  59. }
  60. }
  61. }
  62. /* 线程 1 的入口函数 */
  63. static void thread1_entry(void *parameter) {
  64. struct rt_can_msg msg = {0};
  65. int count = 0;
  66. msg.id = 0x123; /* ID 为 0x78 */
  67. msg.ide = RT_CAN_STDID; /* 标准格式 */
  68. //msg.ide = RT_CAN_EXTID; /* 标准格式 */
  69. msg.rtr = RT_CAN_DTR; /* 数据帧 */
  70. msg.len = 8; /* 数据长度为 8 */
  71. /* 待发送的 8 字节数据 */
  72. msg.data[0] = 0x00;
  73. msg.data[1] = 0x11;
  74. msg.data[2] = 0x22;
  75. msg.data[3] = 0x33;
  76. msg.data[4] = 0x44;
  77. msg.data[5] = 0x55;
  78. msg.data[6] = 0x66;
  79. msg.data[7] = 0x77;
  80. while(running) {
  81. /* 线程 1 采用低优先级运行,一直打印计数值 */
  82. rt_kprintf("send %d : id = %d, ide=%d :", ++count, msg.id, msg.ide);
  83. for(int i=0;i<msg.len;i++)
  84. rt_kprintf(" %02x", msg.data[i]);
  85. rt_kprintf("\n");
  86. rt_device_write(can_dev, 0, &msg, sizeof(msg));
  87. for(int i=0;i<100;i++){
  88. rt_thread_mdelay(50);
  89. if(!running)
  90. break;
  91. }
  92. }
  93. rt_device_close(can_dev);
  94. }
  95. int can_sample(int argc, char *argv[]) {
  96. rt_err_t res;
  97. rt_size_t size;
  98. rt_thread_t thread;
  99. char can_name[RT_NAME_MAX];
  100. if (argc == 2) {
  101. rt_strncpy(can_name, argv[1], RT_NAME_MAX);
  102. } else {
  103. rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
  104. }
  105. if(running){
  106. rt_kprintf("can_sample is running, stop it before restart!\n can_sample_stop\n", can_name);
  107. return RT_ERROR;
  108. }
  109. /* 查找 CAN 设备 */
  110. can_dev = rt_device_find(can_name);
  111. if (!can_dev) {
  112. rt_kprintf("find %s failed!\n", can_name);
  113. return RT_ERROR;
  114. }
  115. running = 1;
  116. res = rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  117. /* 以中断接收及发送方式打开 CAN 设备 */
  118. res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
  119. /* 初始化 CAN 接收信号量 */
  120. /* 设置 CAN 的工作模式为正常工作模式 */
  121. res = rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void *)RT_CAN_MODE_NORMAL);
  122. //res = rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void *)RT_CAN_MODE_LOOPBACK);
  123. res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN125kBaud);
  124. RT_ASSERT(res == RT_EOK);
  125. /* 创建数据接收线程 */
  126. thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
  127. if (thread != RT_NULL) {
  128. rt_thread_startup(thread);
  129. } else {
  130. rt_kprintf("create can_rx thread failed!\n");
  131. }
  132. if (size == 0) {
  133. rt_kprintf("can dev write data failed!\n");
  134. }
  135. /* 创建线程 1,名称是 thread1,入口是 thread1_entry*/
  136. tid1 = rt_thread_create("thread1",
  137. thread1_entry, RT_NULL,
  138. THREAD_STACK_SIZE,
  139. THREAD_PRIORITY, THREAD_TIMESLICE);
  140. /* 如果获得线程控制块,启动这个线程 */
  141. if (tid1 != RT_NULL)
  142. rt_thread_startup(tid1);
  143. else
  144. rt_kprintf("start can send fail\n");
  145. return res;
  146. }
  147. int can_sample_stop(int argc, char *argv[]) {
  148. if(running){
  149. running = 0;
  150. //rt_sem_release(&rx_sem);
  151. rt_sem_detach(&rx_sem);
  152. }
  153. return RT_EOK;
  154. }
  155. /* 导出到 msh 命令列表中 */
  156. MSH_CMD_EXPORT(can_sample, can device sample);
  157. MSH_CMD_EXPORT(can_sample_stop, can device sample stop);
  158. #endif

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/755617
推荐阅读
相关标签
  

闽ICP备14008679号