ESP32 TWAI(can总线)设备通信

#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_log.h"
#include "driver/twai.h"

#define EXAMPLE_TAG "TWAI master"
#define SENDMSG 0
#define RECEIVEMSG 1
#define RX_GPIO_PIN    36
#define TX_GPIO_PIN    37

/* --------------------------- Tasks and Functions -------------------------- */

void printf_msg(int flag, twai_message_t *msg) // flag:0-send 1-receive
{
    int j;
    if (flag)
        printf("Receive: ");
    else
        printf("Send   : ");
    if (msg->extd)
        printf("Extended ");
    else
        printf("Standard ");
    if (msg->rtr)
        printf("Remote Frame, ");
    else
        printf("Data  Frame,  ");
    // printf("ID: 0x%lx", (unsigned long)msg->identifier);
    if (msg->rtr == 0)
    {
        for (j = 0; j < msg->data_length_code; j++)
        {
            printf("D%d: %d\t", j, msg->data[j]);
        }
        printf("\n");
    }
    else
        printf("\n");
}

static void twai_transmit_task(void *arg)
{

      int i;
      
    //   twai_message_t s1 = {
    //       .extd = 0,                         // 0-标准帧; 1-扩展帧
    //       .rtr = 0,                          // 0-数据帧; 1-远程帧
    //       .ss = 1,                           // 0-错误重发; 1-单次发送(仲裁或丢失时消息不会被重发),对接收消息无效
    //       .self = 0,                         // 0-不接收自己发送的消息,1-接收自己发送的消息,对接收消息无效
    //       .dlc_non_comp = 0,                 // 0-数据长度不大于8(ISO 11898-1); 1-数据长度大于8(非标);
    //       .identifier = 0x55b,                  // 11/29位ID
    //       .data_length_code = 4,             // DLC数据长度4bit位宽
    //       .data = {0, 0, 0, 0, 0, 0, 0, 0}}; //发送数据,对远程帧无效
      
  
  
      twai_message_t s1;
      s1.identifier = 0x55b;
      s1.extd = 1;
      s1.self = 1;
      s1.data_length_code = 4;
      for (int i = 0; i < 8; i++) {
          s1.data[i] = 1;
      }
  
      vTaskDelay(pdMS_TO_TICKS(1000));
      for (i = 0; i < 3; i++) //发送3个标准数据帧
      {
          s1.data[0] = i;
          ESP_ERROR_CHECK(twai_transmit(&s1, portMAX_DELAY));
          printf_msg(SENDMSG, &s1);
      }
  
    //   s1.rtr = 1;
    //   s1.data_length_code = 6;
    //   vTaskDelay(pdMS_TO_TICKS(1000));
    //   for (i = 0; i < 1; i++) //发送1个标准远程帧
    //   {
    //       ESP_ERROR_CHECK(twai_transmit(&s1, portMAX_DELAY));
    //       printf_msg(SENDMSG, &s1);
    //   }
  
      s1.extd = 1;
      s1.rtr = 0;
      s1.identifier = 0x55e;
      s1.data_length_code = 5;
      vTaskDelay(pdMS_TO_TICKS(1000));
      for (i = 0; i < 3; i++) //发送3个扩展数据帧
      {
          s1.data[0] = i;
          ESP_ERROR_CHECK(twai_transmit(&s1, portMAX_DELAY));
          printf_msg(SENDMSG, &s1);
      }
  
    //   s1.rtr = 1;
    //   s1.data_length_code = 3;
    //   vTaskDelay(pdMS_TO_TICKS(1000));
    //   for (i = 0; i < 1; i++) //发送1个扩展远程帧
    //   {
    //       ESP_ERROR_CHECK(twai_transmit(&s1, portMAX_DELAY));
    //       printf_msg(SENDMSG, &s1);
    //   }

      vTaskDelete(NULL);
}

static void twai_receive_task(void *arg)
{
    twai_message_t r1;
    for (int i = 0; i < 8; i++) //接受
    {
        ESP_ERROR_CHECK(twai_receive(&r1, portMAX_DELAY));
        printf_msg(RECEIVEMSG, &r1);
    }
    vTaskDelete(NULL);
}

void app_main(void)
{
    /*
    // CAN接口基本配置
    // twai_general_config_t g_config = {
    //     .mode = TWAI_MODE_NORMAL,            // TWAI_MODE_NORMAL / TWAI_MODE_NO_ACK / TWAI_MODE_LISTEN_ONLY
    //     .tx_io = 2,                          // IO号
    //     .rx_io = 3,                          // IO号
    //     .clkout_io = TWAI_IO_UNUSED,         // io号,不用为-1
    //     .bus_off_io = TWAI_IO_UNUSED,        // io号,不用为-1
    //     .tx_queue_len = 5,                   //发送队列长度,0-禁用发送队列
    //     .rx_queue_len = 5,                   //接收队列长度
    //     .alerts_enabled = TWAI_ALERT_NONE,   //警告标志 TWAI_ALERT_ALL 可开启所有警告
    //     .clkout_divider = 0,                 // 1 to 14 , 0-不用
    //     .intr_flags = ESP_INTR_FLAG_LEVEL1}; //中断优先级
    // */

    // CAN接口基本配置
    twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_PIN, RX_GPIO_PIN, TWAI_MODE_NO_ACK);
    // CAN接口时序配置官方提供了1K to 1Mbps的常用配置
    // twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS(); // TWAI_TIMING_CONFIG_500KBITS()
    twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); // TWAI_TIMING_CONFIG_500KBITS()

    //过滤器配置
    twai_filter_config_t f_config = {
        .acceptance_code = 0,          //验证代码
        .acceptance_mask = 0xFFFFFFFF, //验证掩码 0xFFFFFFFF表示全部接收
        .single_filter = true};        // true:单过滤器模式 false:双过滤器模式

    ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
    ESP_LOGI(EXAMPLE_TAG, "Driver installed");
    ESP_ERROR_CHECK(twai_start());
    ESP_LOGI(EXAMPLE_TAG, "Driver started");

    xTaskCreatePinnedToCore(twai_receive_task, "TWAI_rx", 4096, NULL, 8, NULL, tskNO_AFFINITY);
    xTaskCreatePinnedToCore(twai_transmit_task, "TWAI_tx", 4096, NULL, 9, NULL, tskNO_AFFINITY);

    vTaskDelay(pdMS_TO_TICKS(10000)); //运行10s

    twai_status_info_t status_info;
    twai_get_status_info(&status_info);
    while (status_info.msgs_to_tx != 0)
    {
        ESP_ERROR_CHECK(twai_get_status_info(&status_info));
    }
    ESP_ERROR_CHECK(twai_stop()); // Stop the TWAI Driver
    ESP_LOGI(EXAMPLE_TAG, "Driver stopped");
    ESP_ERROR_CHECK(twai_driver_uninstall());
    ESP_LOGI(EXAMPLE_TAG, "Driver uninstalled");
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值