Skip to content

WiFi + MCP2515 (CAN over SPI) = nightmare #1670

@psykokwak-com

Description

@psykokwak-com

Hardware:

Board: Custom board based on ESP32 Wrower module
Core Installation/update date: IDF & Arduino up to date
IDE name: IDF Component (mingw)
Flash Frequency: 40Mhz
Upload Speed: 115200

Description:

I try to make the WiFi and my CAN (over SPI) communication working together but I have no succes :/
If I comment the "WiFi.softAP()" line, the CAN link works well.
If I uncomment this line, I have read and write error and after few seconds (random time), the ESP32 crashs.

I use this CAN library https://github.com/coryjfowler/MCP_CAN_lib but I tried with this other one https://github.com/sandeepmistry/arduino-CAN and I had the same results.
I tried to build the sketch with the ArduinoIDE... The same.

I have no more idea. Is it possible to have a conflict between the SPI and WiFi link or a memory leak on the WiFi stack ?

Here the link with the mcp_can library (I changed it a bit) :
modified_mcp_can.zip

No need to have CAN device attached to.

Sketch:

#include <Arduino.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiAP.h>

#include "mcp_can.h"


#define VSPI_CLOCK_PIN        4
#define VSPI_MOSI_PIN         15
#define VSPI_MISO_PIN         32
#define VSPI_CS_MCP2515_PIN   25

#define CANBUFFERSIZE         8
#define CAN_INT_MCP2515_PIN   4

#define DAMACCANID            0x00A

#define myTAG                 "SPIandWiFI"

void setup()
{
  esp_log_level_set("*", ESP_LOG_VERBOSE);

  SPIClass mffVSPI = SPIClass(VSPI);
  mffVSPI.begin(VSPI_CLOCK_PIN, VSPI_MISO_PIN, VSPI_MOSI_PIN);
  //pinMode(VSPI_MOSI_PIN, INPUT_PULLUP);
  //pinMode(VSPI_MISO_PIN, INPUT_PULLUP);

  MCP_CAN mffCAN(VSPI_CS_MCP2515_PIN);
  mffCAN.begin(mffVSPI, MCP_ANY, CAN_250KBPS, MCP_8MHZ);
  mffCAN.setMode(MCP_NORMAL);

  byte dataOperationMode[] = { 0x01, 0x00 };
  mffCAN.sendMsgBuf(0x00, 2, dataOperationMode);

  byte dataSetDisplay[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 };
  mffCAN.sendMsgBuf((0x20 << 4) + DAMACCANID, 8, dataSetDisplay);

  // If I comment this line, the CAN bus works well.
  WiFi.softAP("WIFI Sucks", "fuckingWiFi");

  unsigned long id = 0;
  byte len = CANBUFFERSIZE;
  byte buffer[CANBUFFERSIZE];

  unsigned long m = millis();

  while (42)
  {
    if (millis() / 100 != m / 100)
    {
      byte r;
      byte dataRequest[] = { 0x00 };

      r = mffCAN.sendMsgBuf(((0x28 << 4) + DAMACCANID) | 0x40000000, 0, dataRequest);
      if (r != CAN_OK)
        ESP_LOGW(myTAG, "CAN: Error Damac sending frame");

      m = millis();
    }

    if (mffCAN.checkReceive() != CAN_MSGAVAIL)
      continue;

    if (mffCAN.readMsgBuf(&id, &len, buffer) != CAN_OK)
    {
      ESP_LOGW(myTAG, "CAN: readMsgBuf returned an error");
      continue;
    }

    if (len != 8)
    {
      ESP_LOGW(myTAG, "CAN: wrong frame length : %d", len);
      continue;
    }

    //if (!damacProcessIn(id, len, buffer))
    {
      //ESP_LOGW(myTAG, "CAN: unknown Damac frame : %d", len);
      continue;
    }
  }
}


void loop()
{

}

Debug Messages:

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:756
load:0x40078000,len:5972
ho 0 tail 12 room 4
load:0x40080000,len:6704
0x40080000: _WindowOverflow4 at C:/msys32/home/Jyce/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

entry 0x400802e4
0x400802e4: _NMIExceptionVector at ??:?

I (276) cpu_start: Pro cpu up.
I (276) cpu_start: Starting app cpu, entry point is 0x40081030
0x40081030: call_start_cpu1 at C:/msys32/home/Jyce/esp/esp-idf/components/esp32/cpu_start.c:225

I (261) cpu_start: App cpu up.
I (277) heap_init: Initializing. RAM available for dynamic allocation:
D (282) heap_init: New heap initialised at 0x3ffae6e0
I (287) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
D (292) heap_init: New heap initialised at 0x3ffba488
I (297) heap_init: At 3FFBA488 len 00025B78 (150 KiB): DRAM
I (302) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (307) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
D (314) heap_init: New heap initialised at 0x40090ab4
I (318) heap_init: At 40090AB4 len 0000F54C (61 KiB): IRAM
I (323) cpu_start: Pro cpu start user code
D (334) clk: RTC_SLOW_CLK calibration value: 3291085
D (119) intr_alloc: Connected src 46 to int 2 (cpu 0)
D (120) intr_alloc: Connected src 57 to int 3 (cpu 0)
D (121) stack_chk: Intialize random stack guard
D (124) intr_alloc: Connected src 24 to int 9 (cpu 0)
I (129) cpu_start: Starting scheduler on PRO CPU.
D (0) intr_alloc: Connected src 25 to int 2 (cpu 1)
I (4) cpu_start: Starting scheduler on APP CPU.
D (150) heap_init: New heap initialised at 0x3ffe0440
D (155) heap_init: New heap initialised at 0x3ffe4350
D (160) intr_alloc: Connected src 16 to int 12 (cpu 0)
D (165) nvs: nvs_flash_init_custom partition=nvs start=9 count=5
D (195) nvs: nvs_open_from_partition misc 1
D (196) nvs: nvs_get_str_or_blob log
I (196) wifi: wifi driver task: 3ffc42e4, prio:23, stack:4096, core=0
I (198) wifi: wifi firmware version: a7a46e9
I (202) wifi: config NVS flash: enabled
I (205) wifi: config nano formating: disabled
I (209) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (218) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
D (226) nvs: nvs_open_from_partition nvs.net80211 1
D (231) nvs: nvs_get opmode 1
D (234) nvs: nvs_get_str_or_blob sta.ssid
D (238) nvs: nvs_get_str_or_blob sta.mac
D (241) nvs: nvs_get sta.authmode 1
D (244) nvs: nvs_get_str_or_blob sta.pswd
D (248) nvs: nvs_get_str_or_blob sta.pmk
D (252) nvs: nvs_get sta.chan 1
D (255) nvs: nvs_get auto.conn 1
D (258) nvs: nvs_get bssid.set 1
D (261) nvs: nvs_get_str_or_blob sta.bssid
D (265) nvs: nvs_get sta.lis_intval 2
D (268) nvs: nvs_get sta.phym 1
D (271) nvs: nvs_get sta.phybw 1
D (274) nvs: nvs_get_str_or_blob sta.apsw
D (277) nvs: nvs_get_str_or_blob sta.apinfo
D (283) nvs: nvs_get sta.scan_method 1
D (285) nvs: nvs_get sta.sort_method 1
D (288) nvs: nvs_get sta.minrssi 1
D (291) nvs: nvs_get sta.minauth 1
D (294) nvs: nvs_get_str_or_blob ap.ssid
D (298) nvs: nvs_get_str_or_blob ap.mac
D (302) nvs: nvs_get_str_or_blob ap.passwd
D (306) nvs: nvs_get_str_or_blob ap.pmk
D (309) nvs: nvs_get ap.chan 1
D (312) nvs: nvs_get ap.authmode 1
D (315) nvs: nvs_get ap.hidden 1
D (318) nvs: nvs_get ap.max.conn 1
D (321) nvs: nvs_get bcn.interval 2
D (324) nvs: nvs_get ap.phym 1
D (327) nvs: nvs_get ap.phybw 1
D (330) nvs: nvs_get ap.sndchan 1
D (333) nvs: nvs_get lorate 1
D (336) nvs: nvs_set_blob sta.mac 6
D (344) nvs: nvs_set_blob ap.mac 6
I (345) wifi: Init dynamic tx buffer num: 32
I (346) wifi: Init data frame dynamic rx buffer num: 64
I (351) wifi: Init management frame dynamic rx buffer num: 64
I (356) wifi: Init static tx buffer num: 16
I (361) wifi: Init static rx buffer size: 1600
I (364) wifi: Init static rx buffer num: 10
I (368) wifi: Init dynamic rx buffer num: 0
D (373) RTC_MODULE: Wi-Fi takes adc2 lock.
D (376) phy_init: loading PHY init data from application binary
D (382) nvs: nvs_open_from_partition phy 0
D (385) nvs: nvs_get cal_version 4
D (389) nvs: nvs_get_str_or_blob cal_mac
D (392) nvs: nvs_get_str_or_blob cal_data
D (400) nvs: nvs_close 3
I (496) phy: phy_version: 3910, c0c45a3, May 21 2018, 18:07:06, 0, 0
I (497) wifi: mode : softAP (30:ae:a4:3f:69:39)
D (501) _eventCallback: Event: 0 - WIFI_READY
D (502) event: SYSTEM_EVENT_AP_START
D (504) tcpip_adapter: dhcp server start:(ip: 192.168.4.1, mask: 255.255.255.0, gw: 192.168.4.1)
D (511) _eventCallback: Event: 13 - AP_START
W (531) SPIandWiFI: CAN: Error Damac sending frame
W (634) SPIandWiFI: CAN: Error Damac sending frame
W (708) SPIandWiFI: CAN: readMsgBuf returned an error
W (709) SPIandWiFI: CAN: readMsgBuf returned an error
W (731) SPIandWiFI: CAN: Error Damac sending frame
W (831) SPIandWiFI: CAN: Error Damac sending frame
W (913) SPIandWiFI: CAN: readMsgBuf returned an error
W (914) SPIandWiFI: CAN: wrong frame length : 14
W (915) SPIandWiFI: CAN: wrong frame length : 1
W (916) SPIandWiFI: CAN: wrong frame length : 0
W (922) SPIandWiFI: CAN: readMsgBuf returned an error
W (925) SPIandWiFI: CAN: wrong frame length : 0
W (931) SPIandWiFI: CAN: readMsgBuf returned an error
W (934) SPIandWiFI: CAN: wrong frame length : 0
W (938) SPIandWiFI: CAN: wrong frame length : 4
W (944) SPIandWiFI: CAN: readMsgBuf returned an error
W (948) SPIandWiFI: CAN: wrong frame length : 0
W (951) SPIandWiFI: CAN: wrong frame length : 4
W (957) SPIandWiFI: CAN: readMsgBuf returned an error
W (961) SPIandWiFI: CAN: wrong frame length : 0
W (964) SPIandWiFI: CAN: wrong frame length : 4
W (971) SPIandWiFI: CAN: readMsgBuf returned an error
W (974) SPIandWiFI: CAN: wrong frame length : 0
W (978) SPIandWiFI: CAN: wrong frame length : 4
W (984) SPIandWiFI: CAN: readMsgBuf returned an error
W (987) SPIandWiFI: CAN: wrong frame length : 0
W (991) SPIandWiFI: CAN: wrong frame length : 4
W (997) SPIandWiFI: CAN: readMsgBuf returned an error
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x40135bb2  PS      : 0x00060830  A0      : 0x800f0304  A1      : 0x3ffc07f0
0x40135bb2: spiGetClockDiv at C:/msys32/home/Jyce/esp/arduino/components/arduino/cores/esp32/esp32-hal-spi.c:290

A2      : 0x00008100  A3      : 0x3ffae944  A4      : 0x00000050  A5      : 0x00000000
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x80082e5a  A9      : 0x3ffc04b0
A10     : 0x3ffbe434  A11     : 0x00000004  A12     : 0x3ffc0734  A13     : 0x3ffae944
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00008118  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd

Backtrace: 0x40135bb2:0x3ffc07f0 0x400f0301:0x3ffc0810 0x400ea79e:0x3ffc0840 0x400eafa5:0x3ffc0870 0x400ea4f4:0x3ffc0890 0x400e2b0f:0x
3ffc0920
0x40135bb2: spiGetClockDiv at C:/msys32/home/Jyce/esp/arduino/components/arduino/cores/esp32/esp32-hal-spi.c:290

0x400f0301: SPIClass::beginTransaction(SPISettings) at C:/msys32/home/Jyce/esp/arduino/components/arduino/libraries/SPI/src/SPI.cpp:26
5

0x400ea79e: MCP_CAN::mcp2515_readStatus() at C:/msys32/home/Jyce/esp/arduino/main/mcp_can.cpp:998

0x400eafa5: MCP_CAN::checkReceive() at C:/msys32/home/Jyce/esp/arduino/main/mcp_can.cpp:998

0x400ea4f4: setup() at C:/msys32/home/Jyce/esp/arduino/main/SentinelSimpleFirmwareMain.cpp:79

0x400e2b0f: loopTask(void*) at C:/msys32/home/Jyce/esp/arduino/components/arduino/cores/esp32/main.cpp:15


Rebooting...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions