Problem is, some hardware vendors require RTR's if you want to communicate with their equipment.
Guess they failed to read the linked post
![Smile :)](./images/smilies/icon_e_smile.gif)
RTR's are a nuisance.
Cheers
Code: Select all
#include "CAN_config.h"
CAN_device_t CAN_cfg = {
.speed=CAN_SPEED_1000KBPS, // CAN Node baudrade
.tx_pin_id = GPIO_NUM_5, // CAN TX pin
.rx_pin_id = GPIO_NUM_4, // CAN RX pin
.rx_queue=NULL, // FreeRTOS queue for RX frames
};
Code: Select all
CAN_frame_t __TX_frame;
Code: Select all
__TX_frame.MsgId
and build your own CAN project codeto clone version 0.1_third_parts and the menuconfig includes the options and saves them correctly.
and if you cpy the driver pack to your ESP-IDF components folderyourProject ->
yourProject -> Makefile
yourProject -> main
yourProject -> main -> myAPP.c
yourProject -> main -> component.mk
yourProject -> components
yourProject -> components -> can
yourProject -> components -> can -> CAN.c
yourProject -> components -> can -> kconfig
yourProject -> components -> can -> component.mk
yourProject -> components -> can -> include
yourProject -> components -> can -> include -> CAN.h
yourProject -> components -> can -> include -> CAN_config.h
yourProject -> components -> can -> include -> CAN_regdef.h
and use in the myAPP.cyourProject ->
yourProject -> Makefile
yourProject -> main
yourProject -> main -> myAPP.c
yourProject -> main -> component.mk
Code: Select all
#include "CAN.h"
#include "CAN_config.h"
Code: Select all
menuconfig config defines that you can use in the code:
* ESPCan activated:
* - CONFIG_ESPCAN ( bool )
* baudrade 100,125, 250, 500, 800, 1000 and user:
* - CONFIG_CAN_SPEED_100KBPS
* - CONFIG_CAN_SPEED_125KBPS
* - CONFIG_CAN_SPEED_250KBPS
* - CONFIG_CAN_SPEED_500KBPS
* - CONFIG_CAN_SPEED_800KBPS
* - CONFIG_CAN_SPEED_1000KBPS
* - CONFIG_CAN_SPEED_USER_KBPS
* user can speed
* - CONFIG_CAN_SPEED_USER_KBPS ( bool )
* - CONFIG_CAN_SPEED_USER_KBPS_VAL (value)
* gpio pins for CANRx and CANTx
* - CONFIG_ESP_CAN_RXD_PIN_NUM
* - CONFIG_ESP_CAN_TXD_PIN_NUM
* node id ( CAN identifier )
* - CONFIG_ESP_CAN_NODE_ITSELF
* enbable/disable send test frames
* - CONFIG_CAN_TEST_SENDING_ENABLED
* - CONFIG_CAN_TEST_SENDING_DISABLED
Code: Select all
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
Code: Select all
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "CAN.h"
#include "CAN_config.h"
Code: Select all
#ifndef CONFIG_ESPCAN
#error for this demo you must enable and configure ESPCan in menuconfig
#endif
#ifdef CONFIG_CAN_SPEED_100KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_100KBPS
#endif
#ifdef CONFIG_CAN_SPEED_125KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_125KBPS
#endif
#ifdef CONFIG_CAN_SPEED_250KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_250KBPS
#endif
#ifdef CONFIG_CAN_SPEED_500KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_500KBPS
#endif
#ifdef CONFIG_CAN_SPEED_800KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_800KBPS
#endif
#ifdef CONFIG_CAN_SPEED_1000KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_1000KBPS
#endif
#ifdef CONFIG_CAN_SPEED_USER_KBPS
#define CONFIG_SELECTED_CAN_SPEED CONFIG_CAN_SPEED_USER_KBPS_VAL /* per menuconfig */
#endif
Code: Select all
/* brief: rudi
* over menuconfig you can set the cfg
* defines are used in the head of code then
* if you change to user cfg
* you can change here too by your self
* how you need this.
*/
CAN_device_t CAN_cfg = {
.speed = CONFIG_SELECTED_CAN_SPEED, // CAN Node baudrade
.tx_pin_id = CONFIG_ESP_CAN_TXD_PIN_NUM, // CAN TX pin example menuconfif GPIO_NUM_5
.rx_pin_id = CONFIG_ESP_CAN_RXD_PIN_NUM, // CAN RX pin example menuconfig GPIO_NUM_4
.rx_queue = NULL, // FreeRTOS queue for RX frames
};
Code: Select all
void task_CAN_TX(void* pvParameters) {
CAN_frame_t __TX_frame;
uint32_t counter = 0;
__TX_frame.MsgID = CONFIG_ESP_CAN_NODE_ITSELF;
__TX_frame.DLC = 8;
__TX_frame.data.u8[0] = 'E';
__TX_frame.data.u8[1] = 'S';
__TX_frame.data.u8[2] = 'P';
__TX_frame.data.u8[3] = '-';
__TX_frame.data.u8[4] = 'C';
__TX_frame.data.u8[5] = 'A';
__TX_frame.data.u8[6] = 'N';
__TX_frame.data.u8[7] = counter;
while(1) {
__TX_frame.data.u8[7] = counter;
CAN_write_frame(&__TX_frame);
vTaskDelay( 1000 / portTICK_PERIOD_MS); // to see ( printf on receiver side ) what happend..
counter++;
if (counter >= 256) counter = 0;
}
}
Code: Select all
#ifdef CONFIG_CAN_TEST_SENDING_ENABLED
vTaskDelay( 1000 / portTICK_PERIOD_MS);
xTaskCreate(&task_CAN_TX, "task_CAN_TX", 2048, NULL, 5, NULL);
#endif
Code: Select all
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "CAN.h"
#include "CAN_config.h"
/* brief: rudi
* you can use menuconfig for this
* and you can expand like you need
* you can also use your own
* cfg - look in the main/cfg folder
* you have then change by self
*/
#ifndef CONFIG_ESPCAN
#error for this demo you must enable and configure ESPCan in menuconfig
#endif
#ifdef CONFIG_CAN_SPEED_100KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_100KBPS
#endif
#ifdef CONFIG_CAN_SPEED_125KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_125KBPS
#endif
#ifdef CONFIG_CAN_SPEED_250KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_250KBPS
#endif
#ifdef CONFIG_CAN_SPEED_500KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_500KBPS
#endif
#ifdef CONFIG_CAN_SPEED_800KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_800KBPS
#endif
#ifdef CONFIG_CAN_SPEED_1000KBPS
#define CONFIG_SELECTED_CAN_SPEED CAN_SPEED_1000KBPS
#endif
#ifdef CONFIG_CAN_SPEED_USER_KBPS
#define CONFIG_SELECTED_CAN_SPEED CONFIG_CAN_SPEED_USER_KBPS_VAL /* per menuconfig */
#endif
/* brief: rudi
* over menuconfig you can set the cfg
* defines are used in the head of code then
* if you change to user cfg
* you can change here too by your self
* how you need this.
*/
CAN_device_t CAN_cfg = {
.speed = CONFIG_SELECTED_CAN_SPEED, // CAN Node baudrade
.tx_pin_id = CONFIG_ESP_CAN_TXD_PIN_NUM, // CAN TX pin example menuconfif GPIO_NUM_5
.rx_pin_id = CONFIG_ESP_CAN_RXD_PIN_NUM, // CAN RX pin example menuconfig GPIO_NUM_4
.rx_queue = NULL, // FreeRTOS queue for RX frames
};
esp_err_t event_handler(void *ctx, system_event_t *event)
{
return ESP_OK;
}
void task_CAN( void *pvParameters ){
(void)pvParameters;
//frame buffer
CAN_frame_t __RX_frame;
//create CAN RX Queue
CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t));
//start CAN Module
CAN_init();
while (1){
//receive next CAN frame from queue
if(xQueueReceive(CAN_cfg.rx_queue,&__RX_frame, 3*portTICK_PERIOD_MS)==pdTRUE){
//do stuff!
/* brief: rudi
* new we switch to NODE Identifier here
* only if we are interest to this comming node
* we print / use the node date where commes in from
*
*/
// if ( __RX_frame.MsgID == YOUR_CHOICE) {
// printf("New Frame form 0x%08x, DLC %d, dataL: 0x%08x, dataH: 0x%08x \r\n",__RX_frame.MsgID, __RX_frame.DLC, __RX_frame.data.u32[0],__RX_frame.data.u32[1]);
printf("Frame from : 0x%08x, DLC %d \n", __RX_frame.MsgID, __RX_frame.DLC);
printf("D0: 0x%02x, ", __RX_frame.data.u8[0]);
printf("D1: 0x%02x, ", __RX_frame.data.u8[1]);
printf("D2: 0x%02x, ", __RX_frame.data.u8[2]);
printf("D3: 0x%02x, ", __RX_frame.data.u8[3]);
printf("D4: 0x%02x, ", __RX_frame.data.u8[4]);
printf("D5: 0x%02x, ", __RX_frame.data.u8[5]);
printf("D6: 0x%02x, ", __RX_frame.data.u8[6]);
printf("D7: 0x%02x\n", __RX_frame.data.u8[7]);
printf("==============================================================================\n");
//loop back frame
CAN_write_frame(&__RX_frame);
// } /* comment out if you use YOUR_CHOICE */
}
}
}
void task_CAN_TX(void* pvParameters) {
CAN_frame_t __TX_frame;
uint32_t counter = 0;
__TX_frame.MsgID = CONFIG_ESP_CAN_NODE_ITSELF;
__TX_frame.DLC = 8;
__TX_frame.data.u8[0] = 'E';
__TX_frame.data.u8[1] = 'S';
__TX_frame.data.u8[2] = 'P';
__TX_frame.data.u8[3] = '-';
__TX_frame.data.u8[4] = 'C';
__TX_frame.data.u8[5] = 'A';
__TX_frame.data.u8[6] = 'N';
__TX_frame.data.u8[7] = counter;
while(1) {
__TX_frame.data.u8[7] = counter;
CAN_write_frame(&__TX_frame);
vTaskDelay( 1000 / portTICK_PERIOD_MS); // to see ( printf on receiver side ) what happend..
counter++;
if (counter >= 256) counter = 0;
}
}
void app_main(void)
{
nvs_flash_init();
tcpip_adapter_init();
/*
* Station Mode:
*/
/*
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config = {
.sta = {
.ssid = "access_point_name",
.password = "password",
.bssid_set = false
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
*/
/*
* Soft AP Mode
*/
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); // esp32_wifi_eventHandler
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
wifi_config_t apConfig = {
.ap = {
.ssid="OpenCAN-home",
.ssid_len=0,
.password="not4upublic",
.channel=0,
.authmode=WIFI_AUTH_WPA2_PSK,
.ssid_hidden=0,
.max_connection=4,
.beacon_interval=100
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &apConfig) );
ESP_ERROR_CHECK( esp_wifi_start() );
/*brief: rudi
* if you have "activate ESPCan"
* then the code here runs..
*
*/
#ifdef CONFIG_ESPCAN
printf("ESPCan configured by this Data:\n");
printf("Node : 0x%03x\n", CONFIG_ESP_CAN_NODE_ITSELF);
printf("CAN RXD PIN NUM: %d\n", CONFIG_ESP_CAN_RXD_PIN_NUM);
printf("CAN TXD PIN NUM: %d\n", CONFIG_ESP_CAN_TXD_PIN_NUM);
printf("CAN SPEED : %d KBit/s\n", CONFIG_SELECTED_CAN_SPEED);
#ifdef CONFIG_CAN_SPEED_USER_KBPS
printf("kBit/s setting was done by User\n");
#endif
//Create CAN receive task
xTaskCreate(&task_CAN, "CAN", 2048, NULL, 5, NULL);
#ifdef CONFIG_CAN_TEST_SENDING_ENABLED
vTaskDelay( 1000 / portTICK_PERIOD_MS);
xTaskCreate(&task_CAN_TX, "task_CAN_TX", 2048, NULL, 5, NULL);
#endif
#endif
/*brief: rudi
* you can use the menuconfig ESPCan configs
* like you need..
* cause we have stdder if ESPCan is not enabled in this demo
* this will never be called cause compilation will break
* see #error message on top of the code
*/
#ifndef CONFIG_ESPCAN
printf("Hello World without ESPCan ;-)\n");
#endif
}
Code: Select all
#include "CAN.h"
#include "CAN_config.h"
Code: Select all
//disable listen only mode
MODULE_CAN->MOD.B.LOM = 0;
Code: Select all
// loop back frame
CAN_write_frame(&__RX_frame);
Code: Select all
// loop back frame
// CAN_write_frame(&__RX_frame);
`so i think you use an CAN bus transceiver between, can you point me to the Type of this please.got the ESP32 and a CAN bus tranceiver hooked up to a Kvaser Leaf Light, with termination resistance.
Users browsing this forum: Google [Bot] and 30 guests