@jcsbanks
the
http://www.barth-dev.de/wp-content/uplo ... N_demo.zip is all
- demo
- driver
so if you take this like it is, then you can compile and start CAN project.
if you want use the drv folder for next project
you copy this structure in your next project and set in cfg/CAN_config.c your setup for CAN manually.
but if you want to use a general CAN Driver pack with menuconfig and ESP-IDF structure
( this was the goal to do the things with it )
then you can use the version 0.1_third_parts, that include the driver pack and menuconfig structure
like in ESP-IDF.
if you want to "install" / use for next can driver project the CAN Driver Pack by setting over menuconfig
then you take the version 0.1_third_parts component pack like you have try
to clone version 0.1_third_parts and the menuconfig includes the options and saves them correctly.
and build your own CAN project code
or you can take the demo code and
must change things for include, component.mk in it to point to component/can instead to the included drv folder, thats normal
.....
btw you can copy this CAN component in your ESP-IDF
under components, then you have allways this CAN Driver Pack available.
this is the sense of the driver pack with menuconfig
adapted to the component structure of esp-idf
and sure
you can copy this in the project folder
instead of esp-idf components folder
If you do not want to have it in esp-idf
and you have then this structure
yourProject ->
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 if you cpy the driver pack to your ESP-IDF components folder
you need only this in your project
yourProject ->
yourProject -> Makefile
yourProject -> main
yourProject -> main -> myAPP.c
yourProject -> main -> component.mk
and use in the myAPP.c
Code: Select all
#include "CAN.h"
#include "CAN_config.h"
thats all.
make menuconfig
makes then the
sdkconfig file and config file for the project.
if you did your step, you have now config vars like this:
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
and you can use this in your "own" code in a new project
if you want use the demo code and the Driver Pack with menuconfig
then you can delete the drv folder in the demo project ( is replaced with components/can from Driver Pack with menuconfig )
the component.mk file then looks like this then in the main
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.
#
empty.
now you can use in your main.c code like this:
inlcudes
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"
you can use the CAN Speed Config Define from menuconfig like this
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
now you can work with the define var
CONFIG_SELECTED_CAN_SPEED
CONFIG_ESP_CAN_TXD_PIN_NUM
CONFIG_ESP_CAN_RXD_PIN_NUM
like this ( this is a replacement for CAN_config.c and not more need )
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
};
a test frame send looks like this
and you can use the menuconfig defined var
CONFIG_ESP_CAN_NODE_ITSELF
for your CAN node unit ID itself
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;
}
}
and you can ( not must ) use for this example then the menuconfig define var
CONFIG_CAN_TEST_SENDING_ENABLED
like this
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
an example code looks then like 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"
/* 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
}
hope now is clear and helps,
if you use demo.zip - there is no need for the driver pack
and if you use driver pack with menuconfig - you do not need the drv folder from demo code and must clear main component.mk
and use only
Code: Select all
#include "CAN.h"
#include "CAN_config.h"
btw
the demos code for a unit and a controll unit comes here in a few days
then
there are websocket demos too, to monitoring CAN messages on webbrowser and
send CAN messages by link
( just in time project sceleton )
and sure - there comes animations too - but this comes easter....with an esp32 eclipse neon3 project --
best wishes
rudi