101 lines
3.2 KiB
C
101 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/queue.h"
|
|
#include "freertos/semphr.h"
|
|
#include "esp_err.h"
|
|
#include "esp_log.h"
|
|
#include "driver/twai.h"
|
|
#include "modbus.h"
|
|
|
|
#define TX_GPIO_NUM 42
|
|
#define RX_GPIO_NUM 41
|
|
#define RX_BUF_LEN (1024)
|
|
|
|
static const char *TAG = "twal";
|
|
|
|
static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL);
|
|
static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
|
|
static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
|
|
|
|
typedef struct
|
|
{
|
|
uint16_t res;
|
|
uint16_t angle;
|
|
uint16_t speed;
|
|
} twai_receive_data_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint32_t encoder_accumul; /* 编码器的累计值 修正:可能装不下,考虑用多少位的来扩展 */
|
|
uint32_t encoder_accumur;
|
|
} twai_send_data1_t;
|
|
|
|
|
|
twai_receive_data_t *twai_receive_data = (twai_receive_data_t *)&gWordVar[0];
|
|
twai_send_data1_t *twai_send_data1 = (twai_send_data1_t *)&gWordVar[12];
|
|
|
|
|
|
static void twai_receive_task(void *arg)
|
|
{
|
|
uint8_t rx_data[RX_BUF_LEN];
|
|
int rx_len = 0;
|
|
twai_message_t message;
|
|
|
|
ESP_LOGI(TAG, "twai_receive_task");
|
|
|
|
while (1)
|
|
{
|
|
/* 接受一个modbus帧 */
|
|
if (twai_receive(&message, portMAX_DELAY) == ESP_OK) {
|
|
/* 远程帧据不处理 */
|
|
if (message.rtr) {
|
|
ESP_LOGI(TAG, "remote frame, return");
|
|
}
|
|
else if (message.data_length_code != 4) {
|
|
ESP_LOGI(TAG, "Invalid data length");
|
|
}
|
|
else {
|
|
twai_receive_data->angle = (message.data[1] << 8) | message.data[0];
|
|
twai_receive_data->speed = (message.data[3] << 8) | message.data[2];
|
|
ESP_LOGI(TAG, "angle: %d, speed: %d", twai_receive_data->angle, twai_receive_data->speed);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(TAG, "Failed to receive message");
|
|
}
|
|
|
|
/* 发送 */
|
|
ESP_LOGI(TAG, "send");
|
|
twai_message_t messaget;
|
|
messaget.identifier = 0xAAAA;
|
|
messaget.extd = 1;
|
|
messaget.data_length_code = 8;
|
|
messaget.data[0] = twai_send_data1->encoder_accumul & 0xff;
|
|
messaget.data[1] = (twai_send_data1->encoder_accumul >> 8) & 0xff;
|
|
messaget.data[2] = (twai_send_data1->encoder_accumul >> 16) & 0xff;
|
|
messaget.data[3] = (twai_send_data1->encoder_accumul >> 24) & 0xff;
|
|
messaget.data[4] = twai_send_data1->encoder_accumur & 0xff;
|
|
messaget.data[5] = (twai_send_data1->encoder_accumur >> 8) & 0xff;
|
|
messaget.data[6] = (twai_send_data1->encoder_accumur >> 16) & 0xff;
|
|
messaget.data[7] = (twai_send_data1->encoder_accumur >> 24) & 0xff;
|
|
|
|
if (twai_transmit(&messaget, pdMS_TO_TICKS(1000)) != ESP_OK) {
|
|
printf("Failed to queue message for transmission\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void twai_init(void)
|
|
{
|
|
ESP_LOGI(TAG, "twai init");
|
|
|
|
ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
|
|
ESP_ERROR_CHECK(twai_start());
|
|
|
|
xTaskCreatePinnedToCore(twai_receive_task, "TWAI_rx", 4096, NULL, 8, NULL, tskNO_AFFINITY);
|
|
}
|