Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3,954 changes: 3,954 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

Binary file modified HARDWARE/REV-I/ESP32-EVB_Rev_I-BOM.ods
Binary file not shown.
Binary file added HARDWARE/REV-I/ESP32-EVB_Rev_I.dip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
11 changes: 11 additions & 0 deletions SOFTWARE/ESP-IDF_examples/ESP32-EVB_CAN/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"idf.adapterTargetName": "esp32",
"files.associations": {
"can.h": "c",
"twai.h": "c",
"can_types.h": "c",
"twai_types.h": "c"
},
"idf.flashType": "UART",
"idf.portWin": "COM6"
}
6 changes: 6 additions & 0 deletions SOFTWARE/ESP-IDF_examples/ESP32-EVB_CAN/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(can_basic)
8 changes: 8 additions & 0 deletions SOFTWARE/ESP-IDF_examples/ESP32-EVB_CAN/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := can_basic

include $(IDF_PATH)/make/project.mk
76 changes: 76 additions & 0 deletions SOFTWARE/ESP-IDF_examples/ESP32-EVB_CAN/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
| Supported Targets | ESP32 |
| ----------------- | ----- |

# CAN Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)

## Overview

This example demonstrates basic usage of `CAN driver`. The work flow of the example could be as follows:

1. Install CAN driver
2. Receive CAN messages
3. Send CAN message
4. Enable filter and receive only filtered messages (not implemented)

Code was heavily based on https://docs.espressif.com/projects/esp-idf/en/release-v3.3/api-reference/peripherals/can.html

If you have a new CAN application to create (for example, J1939 communication to a tank), try this as a basic template, then add your own code.

## How to use example
See Build, Flash, and Run below.

### Hardware Required

This example assumes you have an Olimex ESP32-EVB and was tested with a REV I version.

#### Pin Assignment

This example uses the embedded ESP32 CAN/TWAI controller. On the ESP32-EVB the pins are assigned as

* CAN-TX is on GPIO5
* CAN-RX is on GPI35

In the example code, these assignments are hard-coded.

### Configure the project

```
idf.py menuconfig
```
No configuration options have been set up.

The CAN transceiver is a MCP2562-E/SN. By default on the ESP32-EVB it operates on the +5V sourced from the PWR2 connector or USB connector.


### Build, Flash, and Run

Build the project and flash it to the board, then run monitor tool to view serial output. Use a CAN monitoring tool to see the transmitted message.

```
idf.py -p PORT build flash monitor
```

(Replace PORT with the name of the serial port to use.)

(To exit the serial monitor, type ``Ctrl-]``.)

See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.

## Example Output

```bash
Driver installed
Driver started
Message received
Message is in Extended Format
ID:1CEBFFFA, data:2 90 E7 FF FF FF FF FF
Message queued for transmission
ESP_ERR_TIMEOUT
```

## Troubleshooting

See common troubleshooting for examples from [upper level](../README.md#common-troubleshooting).

(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)
2 changes: 2 additions & 0 deletions SOFTWARE/ESP-IDF_examples/ESP32-EVB_CAN/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "can_example_main.c"
INCLUDE_DIRS ".")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

122 changes: 122 additions & 0 deletions SOFTWARE/ESP-IDF_examples/ESP32-EVB_CAN/main/can_example_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Code based on example at
// https://docs.espressif.com/projects/esp-idf/en/release-v3.3/api-reference/peripherals/can.html
// which is Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
//
// All other example code Copyright 2022 Brian Alano and
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.#include <stdio.h>

#include "driver/gpio.h"
// #include "driver/can.h"
#include "driver/twai.h"

//#include "sdkconfig.h"

static const char *TAG = "can_example";

esp_err_t recv() {
//Wait for message to be received
twai_message_t message;
if (twai_receive(&message, pdMS_TO_TICKS(10000)) == ESP_OK) {
printf("Message received\n");
} else {
printf("Failed to receive message\n");
return;
}

//Process received message
if (message.flags & TWAI_MSG_FLAG_EXTD) {
printf("Message is in Extended Format\n");
} else {
printf("Message is in Standard Format\n");
}
printf("ID:%X, data:", message.identifier);
if (!(message.flags & TWAI_MSG_FLAG_RTR)) {
for (int i = 0; i < message.data_length_code; i++) {
printf("%X ", message.data[i]);
}
printf("\n");
}
}

esp_err_t transmit(twai_message_t *message) {
//Queue message for transmission
if (twai_transmit(message, pdMS_TO_TICKS(1000)) == ESP_OK) {
printf("Message queued for transmission\n");
return ESP_OK;
} else {
printf("Failed to queue message for transmission\n");
return ESP_FAIL;
}
}

void app_main()
{
uint32_t my_alerts = 0;
esp_err_t ret;
twai_message_t message;
message.identifier = 0x18FF1720;
message.flags = TWAI_MSG_FLAG_EXTD;
message.data_length_code = 8;
for (int i = 0; i < 8; i++) {
message.data[i] = 0xff;
}
uint32_t cnt = 0;

//Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_35, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

ret = twai_reconfigure_alerts(TWAI_ALERT_ALL || TWAI_ALERT_AND_LOG, &my_alerts);
if (ret == ESP_OK) {
printf("Alerts reconfigured\n");
} else {
printf("Failed to reconfigure alerts. Err:%d", ret);
}

//Install CAN driver
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
printf("Driver installed\n");
} else {
printf("Failed to install driver\n");
return;
}

//Start CAN driver
if (twai_start() == ESP_OK) {
printf("Driver started\n");
} else {
printf("Failed to start driver\n");
return;
}

while(true) {
recv();
message.data[0] = (uint8_t)(cnt >>24);
message.data[1] = (uint8_t)(cnt >>16);
message.data[2] = (uint8_t)(cnt >>8);
message.data[3] = (uint8_t)(cnt);
transmit(&message);
ret = twai_read_alerts(&my_alerts, 10);
if ( ret == ESP_OK) {
printf("ESP_OK. alerts:%x\n", my_alerts);
} else if (ret == ESP_ERR_TIMEOUT) {
printf("ESP_ERR_TIMEOUT\n");
} else {
printf("unexpected twai alert: %d\n", ret);
}
cnt++;
};


}
Loading