Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ESP32 hardware targets #23

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5eab89c
add esp32s3 and esp32 support
Feb 16, 2024
60f92a1
Merge branch 'vedderb:main' into hardware-targets
contactsimonwilson Feb 16, 2024
2c44fd7
Fixed build for esp32c3 and added a nice script to build all targets …
Relys Feb 16, 2024
22ec19d
Good enough for now - will fix later
simonnztg Feb 18, 2024
14866a4
Make cmake commands all functional
contactsimonwilson Feb 18, 2024
b913d29
Update readme, improve target switching, remove previous change
contactsimonwilson Feb 18, 2024
0c4a252
Increase task stack sizes that were causing boot issues
contactsimonwilson Feb 19, 2024
2460d4f
Improve build process
contactsimonwilson Feb 21, 2024
343d3c5
disable uart for mini
contactsimonwilson Feb 21, 2024
290beec
More stack size for lbm
contactsimonwilson Feb 23, 2024
1316227
Merge branch 'main' of https://github.com/contactsimonwilson/vesc_exp…
contactsimonwilson Feb 29, 2024
5cb2d69
Fix BLE on ESP32
contactsimonwilson Mar 7, 2024
78bbfa0
Remove duplicate cmakelists line
contactsimonwilson Mar 7, 2024
9c5a366
Add avaspark rgb hardware target
contactsimonwilson Mar 8, 2024
699637a
Merge branch 'main' of https://github.com/contactsimonwilson/vesc_exp…
contactsimonwilson Mar 14, 2024
6ae8b1c
Merge branch 'main' of https://github.com/contactsimonwilson/vesc_exp…
contactsimonwilson Apr 17, 2024
09733f9
avaspark RGB update
contactsimonwilson Apr 17, 2024
d062202
Merge branch 'main' of https://github.com/vedderb/vesc_express into h…
contactsimonwilson Aug 2, 2024
bd6add5
Merge usb serial jtag changes
contactsimonwilson Aug 2, 2024
7083f4f
Update gitignore and readme
contactsimonwilson Aug 2, 2024
0cee935
Merge branch 'main' of https://github.com/contactsimonwilson/vesc_exp…
contactsimonwilson Aug 2, 2024
29ced00
Merge branch 'main' of https://github.com/contactsimonwilson/vesc_exp…
contactsimonwilson Aug 17, 2024
d43a109
Update sdkconfigs for additional hw targets
contactsimonwilson Aug 21, 2024
5f15bf5
Merge branch 'main' of https://github.com/vedderb/vesc_express into h…
contactsimonwilson Sep 7, 2024
624d765
Remove psram
contactsimonwilson Sep 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
build
esp_idf_components
sdkconfig.old
.vscode
sdkconfig
.vscode
managed_components/
dependencies.lock
88 changes: 85 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
# 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)

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
set(PROJECT_VER "6.0")

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
# Continued at the bottom of this file (after custom target logic)

# Default build target if not provided
if(NOT DEFINED HW_NAME)
set(HW_NAME "VESC Express T")
endif()

file(GLOB_RECURSE HW_HEADER_PATHS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
main/hwconf/hw_*.h
)

foreach(HW_HEADER_PATH ${HW_HEADER_PATHS})
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/${HW_HEADER_PATH}" FILE_CONTENTS)
set(IS_HW_MATCH false)

foreach(LINE IN LISTS FILE_CONTENTS)
string(REGEX MATCH "^#define[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]+(.*)$" MATCHES "${LINE}")
if(MATCHES)
set(DEF_KEY ${CMAKE_MATCH_1})
set(DEF_VAL ${CMAKE_MATCH_2})

# Remove surrounding quotes
if(DEF_VAL MATCHES "^\".*\"$")
string(REGEX REPLACE "^\"(.*)\"$" "\\1" DEF_VAL ${DEF_VAL})
endif()

if (DEF_KEY STREQUAL "HW_NAME" AND DEF_VAL STREQUAL HW_NAME)
set(IS_HW_MATCH true)
endif()

# We've found the hardware target (must be below HW_NAME) - exit early
if (IS_HW_MATCH AND DEF_KEY STREQUAL "HW_TARGET")
set(HW_TARGET ${DEF_VAL})
message(${HW_HEADER_PATH})
set(HW_FILE_NAME ${HW_HEADER_PATH})
break()
endif()
endif()
endforeach()
endforeach()


if(DEFINED HW_NAME AND DEFINED HW_TARGET AND DEFINED HW_FILE_NAME)
# Get file name without extension
string(REGEX MATCH "([^/]+)$" HW_FILE ${HW_FILE_NAME})
string(REGEX REPLACE ".h" "" HW_FILE ${HW_FILE})

function(set_hw_target)
file(REMOVE sdkconfig)
configure_file(sdkconfig.${HW_TARGET} ${CMAKE_CURRENT_SOURCE_DIR}/sdkconfig COPYONLY)
execute_process(COMMAND "idf.py reconfigure" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endfunction()

set(TARGET_NAME_CACHE_LOCATION ${CMAKE_BINARY_DIR}/last_target)

# Set our sdkconfig based on hw target
if(EXISTS last_target)
file(READ ${TARGET_NAME_CACHE_LOCATION} STRING)
if(NOT HW_TARGET EQUAL LAST_HW_TARGET)
message("HW_TARGET has changed from ${LAST_HW_TARGET} to ${HW_TARGET}")
file(REMOVE sdkconfig)
set_hw_target()
else()
message("HW_TARGET matches last build of ${HW_TARGET}")
endif()
else()
message("HW_TARGET cache missing. Adding ${HW_TARGET}")
set_hw_target()
endif()

file(WRITE ${TARGET_NAME_CACHE_LOCATION} ${HW_TARGET})
SET(IDF_TARGET ${HW_TARGET})

# Pass through hw file paths
add_compile_definitions(HW_HEADER="${HW_FILE}.h" HW_SOURCE="${HW_FILE}.c")
else ()
message(FATAL_ERROR "Build target info missing.\nHW_NAME: ${HW_NAME}\nHW_TARGET: ${HW_TARGET}\nHW_FILE: ${HW_FILE_NAME}")
endif()

message("Processing target ${HW_NAME} on ${HW_TARGET} for file ${HW_FILE_NAME}")


# ESP-IDF remainder boilerplate
get_filename_component(ProjectId ${CMAKE_CURRENT_LIST_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId})
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ The instructions linked above will install the master branch of ESP-IDF. To inst
```bash
git clone -b v5.2.2 --recursive https://github.com/espressif/esp-idf.git esp-idf-v5.2.2
cd esp-idf-v5.2.2/
./install.sh esp32c3
./install.sh esp32c3 esp32 esp32s3
```

At the moment development is done using the stable 5.2.2-release.

Additionally you may want to add export.sh to your .bashrc file.
```echo "source /path/to/esp-idf-v5.2.2/export.sh" >> ~/.bashrc```

## Compiling

Once the toolchain is set up in the current path, the project can be built with
Expand All @@ -30,4 +33,14 @@ Once the toolchain is set up in the current path, the project can be built with
idf.py build
```

That will create vesc_express.bin in the build directory, which can be used with the bootloader in VESC Tool. If the ESP32c3 does not come with firmware preinstalled, the USB-port can be used for flashing firmware using the built-in bootloader. That also requires bootloader.bin and partition-table.bin which also can be found in the build directory. This can be done from VESC Tool or using idf.py.
To build a specific target
```bash
idf.py build -DHW_NAME="VESC Express T"
```

That will create vesc_express.$target.bin in the bin directory, which can be used with the bootloader in VESC Tool. If the ESP32, ESP32c3 or ESP32s3 does not come with firmware preinstalled, the USB-port can be used for flashing firmware using the built-in bootloader. That also requires bootloader.bin and partition-table.bin which also can be found in the build directory. This can be done from VESC Tool or using idf.py.

When switching targets, you need to clean the build directory
```bash
idf.py fullclean
```
2 changes: 2 additions & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ set(COMPONENT_ADD_INCLUDEDIRS
"hwconf"
"hwconf/trampa"
"hwconf/trampa/bms_rb"
"hwconf/rescue"
"hwconf/avaspark"
"hwconf/vesc"
"hwconf/vesc/vbms32"
"hwconf/vesc/vdisp_dual"
Expand Down
30 changes: 18 additions & 12 deletions main/ble/custom_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
#define ADV_CFG_FLAG (1 << 0)
#define SCAN_RSP_CFG_FLAG (1 << 1)

#if CONFIG_IDF_TARGET_ESP32
#define ESP_PWR_LVL ESP_PWR_LVL_P9
#else
#define ESP_PWR_LVL ESP_PWR_LVL_P18
#endif

typedef uint8_t custom_ble_id_t;

typedef enum {
Expand Down Expand Up @@ -444,12 +450,12 @@ static void gatts_event_handler(

LED_BLUE_ON();

esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL);

// TODO: Should investigate if this is necessary for IOS.
// esp_ble_conn_update_params_t conn_params = {0};
Expand Down Expand Up @@ -590,12 +596,12 @@ custom_ble_result_t custom_ble_start() {
esp_bluedroid_init();
esp_bluedroid_enable();

esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL);

esp_bt_dev_set_device_name(device_name);

Expand Down
36 changes: 23 additions & 13 deletions main/comm_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@
#include "conf_general.h"
#include "main.h"

#define GATTS_CHAR_VAL_LEN_MAX 255
#if CONFIG_IDF_TARGET_ESP32
#define GATTS_CHAR_VAL_LEN_MAX 23
#else
#define GATTS_CHAR_VAL_LEN_MAX 255
#endif
#define DEFAULT_BLE_MTU 20 // 23 for default mtu and 3 bytes for ATT headers
#define BLE_CHAR_COUNT 2
#define BLE_SERVICE_HANDLE_NUM (1 + (3 * BLE_CHAR_COUNT))
#define ADV_CFG_FLAG (1 << 0)
#define SCAN_RSP_CFG_FLAG (1 << 1)

#if CONFIG_IDF_TARGET_ESP32
#define ESP_PWR_LVL ESP_PWR_LVL_P9
#else
#define ESP_PWR_LVL ESP_PWR_LVL_P18
#endif

static bool is_connected = false;
static uint16_t ble_current_mtu = DEFAULT_BLE_MTU;

Expand Down Expand Up @@ -590,12 +600,12 @@ static void gatts_event_handler(
is_connected = true;
LED_BLUE_ON();

esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL);
break;

case ESP_GATTS_DISCONNECT_EVT:
Expand Down Expand Up @@ -670,12 +680,12 @@ void comm_ble_init(void) {
esp_bluedroid_init();
esp_bluedroid_enable();

esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P18);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL0, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL1, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_CONN_HDL2, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL);

esp_bt_dev_set_device_name((char *)backup.config.ble_name);

Expand Down
21 changes: 15 additions & 6 deletions main/comm_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,18 +711,27 @@ static void update_baud(CAN_BAUD baudrate) {
} break;

case CAN_BAUD_1M: {
twai_timing_config_t t_config2 = TWAI_TIMING_CONFIG_1MBITS();
t_config = t_config2;
#if CONFIG_IDF_TARGET_ESP32
#else
twai_timing_config_t t_config2 = TWAI_TIMING_CONFIG_1MBITS();
t_config = t_config2;
#endif
} break;

case CAN_BAUD_10K: {
twai_timing_config_t t_config2 = TWAI_TIMING_CONFIG_10KBITS();
t_config = t_config2;
#if CONFIG_IDF_TARGET_ESP32
#else
twai_timing_config_t t_config2 = TWAI_TIMING_CONFIG_10KBITS();
t_config = t_config2;
#endif
} break;

case CAN_BAUD_20K: {
twai_timing_config_t t_config2 = TWAI_TIMING_CONFIG_20KBITS();
t_config = t_config2;
#if CONFIG_IDF_TARGET_ESP32
#else
twai_timing_config_t t_config2 = TWAI_TIMING_CONFIG_20KBITS();
t_config = t_config2;
#endif
} break;

case CAN_BAUD_50K: {
Expand Down
9 changes: 8 additions & 1 deletion main/comm_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <stdbool.h>
#include <stdatomic.h>
#include "esp_log.h"
#include "hal/usb_serial_jtag_ll.h"
#if !CONFIG_IDF_TARGET_ESP32
#include "hal/usb_serial_jtag_ll.h"
#endif
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/ringbuf.h"
Expand Down Expand Up @@ -75,6 +77,11 @@ static void send_packet_raw(unsigned char *buffer, unsigned int len) {
}

void comm_usb_init(void) {
#if CONFIG_IDF_TARGET_ESP32
return;
#endif


usb_serial_jtag_driver_config_t usb_serial_jtag_config;
usb_serial_jtag_config.rx_buffer_size = 1024;
usb_serial_jtag_config.tx_buffer_size = 256;
Expand Down
2 changes: 1 addition & 1 deletion main/comm_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void comm_wifi_init(void) {
xTaskCreatePinnedToCore(tcp_task_hub, "tcp_hub", 3500, NULL, 8, NULL, tskNO_AFFINITY);
}

xTaskCreatePinnedToCore(broadcast_task, "udp_multicast", 1024, NULL, 8, NULL, tskNO_AFFINITY);
xTaskCreatePinnedToCore(broadcast_task, "udp_multicast", 2048, NULL, 8, NULL, tskNO_AFFINITY);
}

WIFI_MODE comm_wifi_get_mode(void) {
Expand Down
41 changes: 0 additions & 41 deletions main/conf_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,6 @@
// Set to 0 for building a release and iterate during beta test builds
#define FW_TEST_VERSION_NUMBER 0

#if !defined(HW_SOURCE) && !defined(HW_HEADER)
#define HW_HEADER "hw_xp_t.h"
#define HW_SOURCE "hw_xp_t.c"

//#define HW_HEADER "hw_str365.h"
//#define HW_SOURCE "hw_str365.c"

//#define HW_HEADER "hw_bms_rb.h"
//#define HW_SOURCE "hw_bms_rb.c"

//#define HW_HEADER "hw_disp_v1.h"
//#define HW_SOURCE "hw_disp_v1.c"

//#define HW_HEADER "hw_devkit_c3.h"
//#define HW_SOURCE "hw_devkit_c3.c"

//#define HW_HEADER "hw_vbms32.h"
//#define HW_SOURCE "hw_vbms32.c"

//#define HW_HEADER "hw_vdisp_dual.h"
//#define HW_SOURCE "hw_vdisp_dual.c"

//#define HW_HEADER "hw_lifan.h"
//#define HW_SOURCE "hw_lifan.c"

//#define HW_HEADER "hw_lb_if.h"
//#define HW_SOURCE "hw_lb_if.c"

//#define HW_HEADER "hw_lb_bms_wifi.h"
//#define HW_SOURCE "hw_lb_bms_wifi.c"

//#define HW_HEADER "hw_lb_ant.h"
//#define HW_SOURCE "hw_lb_ant.c"

//#define HW_HEADER "hw_lb_hc.h"
//#define HW_SOURCE "hw_lb_hc.c"

//#define HW_HEADER "hw_lb_chg.h"
//#define HW_SOURCE "hw_lb_chg.c"
#endif

#if !defined(HW_SOURCE) && !defined(HW_SOURCE_ALT)
#error "No hardware source file set"
#endif
Expand Down
Loading