diff --git a/examples/esp32can_basic/esp32can_basic.ino b/examples/esp32can_basic/esp32can_basic.ino index 1c9a9ca..205c7cd 100644 --- a/examples/esp32can_basic/esp32can_basic.ino +++ b/examples/esp32can_basic/esp32can_basic.ino @@ -1,65 +1,65 @@ -#include -#include -#include - -CAN_device_t CAN_cfg; // CAN Config -unsigned long previousMillis = 0; // will store last time a CAN Message was send -const int interval = 1000; // interval at which send CAN Messages (milliseconds) -const int rx_queue_size = 10; // Receive Queue size - -void setup() { - Serial.begin(115200); - Serial.println("Basic Demo - ESP32-Arduino-CAN"); - CAN_cfg.speed = CAN_SPEED_125KBPS; - CAN_cfg.tx_pin_id = GPIO_NUM_5; - CAN_cfg.rx_pin_id = GPIO_NUM_4; - CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); - // Init CAN Module - ESP32Can.CANInit(); -} - -void loop() { - - CAN_frame_t rx_frame; - - unsigned long currentMillis = millis(); - - // Receive next CAN frame from queue - if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { - - if (rx_frame.FIR.B.FF == CAN_frame_std) { - printf("New standard frame"); - } - else { - printf("New extended frame"); - } - - if (rx_frame.FIR.B.RTR == CAN_RTR) { - printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); - } - else { - printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); - for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { - printf("0x%02X ", rx_frame.data.u8[i]); - } - printf("\n"); - } - } - // Send CAN Message - if (currentMillis - previousMillis >= interval) { - previousMillis = currentMillis; - CAN_frame_t tx_frame; - tx_frame.FIR.B.FF = CAN_frame_std; - tx_frame.MsgID = 0x001; - tx_frame.FIR.B.DLC = 8; - tx_frame.data.u8[0] = 0x00; - tx_frame.data.u8[1] = 0x01; - tx_frame.data.u8[2] = 0x02; - tx_frame.data.u8[3] = 0x03; - tx_frame.data.u8[4] = 0x04; - tx_frame.data.u8[5] = 0x05; - tx_frame.data.u8[6] = 0x06; - tx_frame.data.u8[7] = 0x07; - ESP32Can.CANWriteFrame(&tx_frame); - } -} +#include +#include +#include + +CAN_device_t CAN_cfg; // CAN Config +unsigned long previousMillis = 0; // will store last time a CAN Message was send +const int interval = 1000; // interval at which send CAN Messages (milliseconds) +const int rx_queue_size = 10; // Receive Queue size + +void setup() { + Serial.begin(115200); + Serial.println("Basic Demo - ESP32-Arduino-CAN"); + CAN_cfg.speed = CAN_SPEED_125KBPS; + CAN_cfg.tx_pin_id = GPIO_NUM_5; + CAN_cfg.rx_pin_id = GPIO_NUM_4; + CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); + // Init CAN Module + ESP32Can.CANInit(CAN_cfg); +} + +void loop() { + + CAN_frame_t rx_frame; + + unsigned long currentMillis = millis(); + + // Receive next CAN frame from queue + if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { + + if (rx_frame.FIR.B.FF == CAN_frame_std) { + printf("New standard frame"); + } + else { + printf("New extended frame"); + } + + if (rx_frame.FIR.B.RTR == CAN_RTR) { + printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); + } + else { + printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); + for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { + printf("0x%02X ", rx_frame.data.u8[i]); + } + printf("\n"); + } + } + // Send CAN Message + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + CAN_frame_t tx_frame; + tx_frame.FIR.B.FF = CAN_frame_std; + tx_frame.MsgID = 0x001; + tx_frame.FIR.B.DLC = 8; + tx_frame.data.u8[0] = 0x00; + tx_frame.data.u8[1] = 0x01; + tx_frame.data.u8[2] = 0x02; + tx_frame.data.u8[3] = 0x03; + tx_frame.data.u8[4] = 0x04; + tx_frame.data.u8[5] = 0x05; + tx_frame.data.u8[6] = 0x06; + tx_frame.data.u8[7] = 0x07; + ESP32Can.CANWriteFrame(&tx_frame); + } +} diff --git a/examples/esp32can_filter/esp32can_filter.ino b/examples/esp32can_filter/esp32can_filter.ino index c13c2e8..1ebedf1 100644 --- a/examples/esp32can_filter/esp32can_filter.ino +++ b/examples/esp32can_filter/esp32can_filter.ino @@ -1,66 +1,65 @@ -#include -#include -#include - -CAN_device_t CAN_cfg; // CAN Config -const int rx_queue_size = 10; // Receive Queue size - -void setup() { - Serial.begin(115200); - Serial.println("Filter Demo - ESP32-Arduino-CAN"); - CAN_cfg.speed = CAN_SPEED_125KBPS; - CAN_cfg.tx_pin_id = GPIO_NUM_5; - CAN_cfg.rx_pin_id = GPIO_NUM_4; - CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); - - // Set CAN Filter - // See in the SJA1000 Datasheet chapter "6.4.15 Acceptance filter" - // and the APPLICATION NOTE AN97076 chapter "4.1.2 Acceptance Filter" - // for PeliCAN Mode - CAN_filter_t p_filter; - p_filter.FM = Single_Mode; - - p_filter.ACR0 = 0x29; - p_filter.ACR1 = 0; - p_filter.ACR2 = 0; - p_filter.ACR3 = 0; - - p_filter.AMR0 = 0; - p_filter.AMR1 = 0xFF; - p_filter.AMR2 = 0xFF; - p_filter.AMR3 = 0xFF; - ESP32Can.CANConfigFilter(&p_filter); - - // Init CAN Module - ESP32Can.CANInit(); -} - -void loop() { - - CAN_frame_t rx_frame; - - unsigned long currentMillis = millis(); - - // Receive next CAN frame from queue - if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { - - if (rx_frame.FIR.B.FF == CAN_frame_std) { - printf("New standard frame"); - } - else { - printf("New extended frame"); - } - - if (rx_frame.FIR.B.RTR == CAN_RTR) { - printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); - } - else { - printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); - for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { - printf("0x%02X ", rx_frame.data.u8[i]); - } - printf("\n"); - } - } -} - +#include +#include +#include + +CAN_device_t CAN_cfg; // CAN Config +const int rx_queue_size = 10; // Receive Queue size + +void setup() { + Serial.begin(115200); + Serial.println("Filter Demo - ESP32-Arduino-CAN"); + CAN_cfg.speed = CAN_SPEED_125KBPS; + CAN_cfg.tx_pin_id = GPIO_NUM_5; + CAN_cfg.rx_pin_id = GPIO_NUM_4; + CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); + + // Set CAN Filter + // See in the SJA1000 Datasheet chapter "6.4.15 Acceptance filter" + // and the APPLICATION NOTE AN97076 chapter "4.1.2 Acceptance Filter" + // for PeliCAN Mode + CAN_filter_t p_filter; + p_filter.FM = Single_Mode; + + p_filter.ACR0 = 0x29; + p_filter.ACR1 = 0; + p_filter.ACR2 = 0; + p_filter.ACR3 = 0; + + p_filter.AMR0 = 0; + p_filter.AMR1 = 0xFF; + p_filter.AMR2 = 0xFF; + p_filter.AMR3 = 0xFF; + ESP32Can.CANConfigFilter(&p_filter); + + // Init CAN Module + ESP32Can.CANInit(CAN_cfg); +} + +void loop() { + + CAN_frame_t rx_frame; + + unsigned long currentMillis = millis(); + + // Receive next CAN frame from queue + if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { + + if (rx_frame.FIR.B.FF == CAN_frame_std) { + printf("New standard frame"); + } + else { + printf("New extended frame"); + } + + if (rx_frame.FIR.B.RTR == CAN_RTR) { + printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); + } + else { + printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); + for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { + printf("0x%02X ", rx_frame.data.u8[i]); + } + printf("\n"); + } + } +} diff --git a/examples/esp32can_mirror/esp32can_mirror.ino b/examples/esp32can_mirror/esp32can_mirror.ino index 0b95c0e..022183d 100644 --- a/examples/esp32can_mirror/esp32can_mirror.ino +++ b/examples/esp32can_mirror/esp32can_mirror.ino @@ -1,46 +1,46 @@ -#include -#include -#include - -CAN_device_t CAN_cfg; // CAN Config -const int rx_queue_size = 10; // Receive Queue size - -void setup() { - Serial.begin(115200); - Serial.println("Mirror Demo - ESP32-Arduino-CAN"); - CAN_cfg.speed = CAN_SPEED_125KBPS; - CAN_cfg.tx_pin_id = GPIO_NUM_5; - CAN_cfg.rx_pin_id = GPIO_NUM_4; - CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); - // Init CAN Module - ESP32Can.CANInit(); -} - -void loop() { - CAN_frame_t rx_frame; - //receive next CAN frame from queue - if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { - - if (rx_frame.FIR.B.FF == CAN_frame_std) { - printf("New standard frame"); - } - else { - printf("New extended frame"); - } - - if (rx_frame.FIR.B.RTR == CAN_RTR) { - printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); - } - else { - printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); - for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { - printf("0x%02X ", rx_frame.data.u8[i]); - } - printf("\n"); - } - - //respond to sender - ESP32Can.CANWriteFrame(&rx_frame); - } - -} +#include +#include +#include + +CAN_device_t CAN_cfg; // CAN Config +const int rx_queue_size = 10; // Receive Queue size + +void setup() { + Serial.begin(115200); + Serial.println("Mirror Demo - ESP32-Arduino-CAN"); + CAN_cfg.speed = CAN_SPEED_125KBPS; + CAN_cfg.tx_pin_id = GPIO_NUM_5; + CAN_cfg.rx_pin_id = GPIO_NUM_4; + CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); + // Init CAN Module + ESP32Can.CANInit(CAN_cfg); +} + +void loop() { + CAN_frame_t rx_frame; + //receive next CAN frame from queue + if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { + + if (rx_frame.FIR.B.FF == CAN_frame_std) { + printf("New standard frame"); + } + else { + printf("New extended frame"); + } + + if (rx_frame.FIR.B.RTR == CAN_RTR) { + printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); + } + else { + printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); + for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { + printf("0x%02X ", rx_frame.data.u8[i]); + } + printf("\n"); + } + + //respond to sender + ESP32Can.CANWriteFrame(&rx_frame); + } + +} diff --git a/src/CAN.c b/src/CAN.c index 826f9db..0f51f4b 100644 --- a/src/CAN.c +++ b/src/CAN.c @@ -45,6 +45,7 @@ // CAN Filter - no acceptance filter static CAN_filter_t __filter = { Dual_Mode, 0, 0, 0, 0, 0Xff, 0Xff, 0Xff, 0Xff }; +static CAN_device_t __CAN_cfg; static void CAN_read_frame_phy(); static void CAN_isr(void *arg_p); @@ -90,7 +91,7 @@ static void CAN_read_frame_phy(BaseType_t *higherPriorityTaskWoken) { CAN_frame_t __frame; // check if we have a queue. If not, operation is aborted. - if (CAN_cfg.rx_queue == NULL) { + if (__CAN_cfg.rx_queue == NULL) { // Let the hardware know the frame has been read. MODULE_CAN->CMR.B.RRB = 1; return; @@ -122,7 +123,7 @@ static void CAN_read_frame_phy(BaseType_t *higherPriorityTaskWoken) { } // send frame to input queue - xQueueSendToBackFromISR(CAN_cfg.rx_queue, &__frame, higherPriorityTaskWoken); + xQueueSendToBackFromISR(__CAN_cfg.rx_queue, &__frame, higherPriorityTaskWoken); // Let the hardware know the frame has been read. MODULE_CAN->CMR.B.RRB = 1; @@ -164,8 +165,8 @@ static int CAN_write_frame_phy(const CAN_frame_t *p_frame) { return 0; } -int CAN_init() { - +int CAN_init(const CAN_device_t p_CAN_cfg) { + __CAN_cfg = p_CAN_cfg; // Time quantum double __tq; @@ -174,15 +175,15 @@ int CAN_init() { DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_CAN_RST); // configure TX pin - gpio_set_level(CAN_cfg.tx_pin_id, 1); - gpio_set_direction(CAN_cfg.tx_pin_id, GPIO_MODE_OUTPUT); - gpio_matrix_out(CAN_cfg.tx_pin_id, CAN_TX_IDX, 0, 0); - gpio_pad_select_gpio(CAN_cfg.tx_pin_id); + gpio_set_level(__CAN_cfg.tx_pin_id, 1); + gpio_set_direction(__CAN_cfg.tx_pin_id, GPIO_MODE_OUTPUT); + gpio_matrix_out(__CAN_cfg.tx_pin_id, CAN_TX_IDX, 0, 0); + gpio_pad_select_gpio(__CAN_cfg.tx_pin_id); // configure RX pin - gpio_set_direction(CAN_cfg.rx_pin_id, GPIO_MODE_INPUT); - gpio_matrix_in(CAN_cfg.rx_pin_id, CAN_RX_IDX, 0); - gpio_pad_select_gpio(CAN_cfg.rx_pin_id); + gpio_set_direction(__CAN_cfg.rx_pin_id, GPIO_MODE_INPUT); + gpio_matrix_in(__CAN_cfg.rx_pin_id, CAN_RX_IDX, 0); + gpio_pad_select_gpio(__CAN_cfg.rx_pin_id); // set to PELICAN mode MODULE_CAN->CDR.B.CAN_M = 0x1; @@ -194,7 +195,7 @@ int CAN_init() { MODULE_CAN->BTR1.B.TSEG2 = 0x1; // select time quantum and set TSEG1 - switch (CAN_cfg.speed) { + switch (__CAN_cfg.speed) { case CAN_SPEED_1000KBPS: MODULE_CAN->BTR1.B.TSEG1 = 0x4; __tq = 0.125; @@ -213,7 +214,7 @@ int CAN_init() { default: MODULE_CAN->BTR1.B.TSEG1 = 0xc; - __tq = ((float) 1000 / CAN_cfg.speed) / 16; + __tq = ((float) 1000 / __CAN_cfg.speed) / 16; } // set baud rate prescaler diff --git a/src/CAN.h b/src/CAN.h index aebde46..9743054 100644 --- a/src/CAN.h +++ b/src/CAN.h @@ -98,7 +98,7 @@ typedef struct { * * \return 0 CAN Module had been initialized */ -int CAN_init(void); +int CAN_init(const CAN_device_t p_CAN_cfg); /** * \brief Send a can frame diff --git a/src/CAN_config.h b/src/CAN_config.h index 2840a3c..f3d87f0 100644 --- a/src/CAN_config.h +++ b/src/CAN_config.h @@ -61,9 +61,6 @@ typedef struct { TaskHandle_t rx_handle; /**< \brief Handler to FreeRTOS RX task. */ } CAN_device_t; -/** \brief CAN configuration reference */ -extern CAN_device_t CAN_cfg; - #ifdef __cplusplus } #endif diff --git a/src/ESP32CAN.cpp b/src/ESP32CAN.cpp index 1412b4a..66d3cbe 100644 --- a/src/ESP32CAN.cpp +++ b/src/ESP32CAN.cpp @@ -1,8 +1,8 @@ #include "ESP32CAN.h" -int ESP32CAN::CANInit() +int ESP32CAN::CANInit(const CAN_device_t p_CAN_cfg) { - return CAN_init(); + return CAN_init(p_CAN_cfg); } int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame) { diff --git a/src/ESP32CAN.h b/src/ESP32CAN.h index 0cbe156..744beeb 100644 --- a/src/ESP32CAN.h +++ b/src/ESP32CAN.h @@ -7,8 +7,8 @@ class ESP32CAN { public: - int CANInit(); - int CANConfigFilter(const CAN_filter_t* p_filter); + int CANInit(const CAN_device_t p_CAN_cfg); + int CANConfigFilter(const CAN_filter_t* p_filter); int CANWriteFrame(const CAN_frame_t* p_frame); int CANStop(); };