Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
WIP different bootloader for TSDZ2 and BAfang
Browse files Browse the repository at this point in the history
  • Loading branch information
casainho committed May 17, 2022
1 parent 12f252b commit 7f402f4
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 14 deletions.
19 changes: 15 additions & 4 deletions firmware/EBike_wireless_bootloader/firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@ OUTPUT_DIRECTORY := _build
SDK_ROOT := ../../common_firmware/nRF5_SDK_16.0.0

PROJ_DIR := .

ifdef MOTOR_TSDZ2
$(warning )
$(warning MOTOR: TSDZ2)
MOTOR = TSDZ2
CFLAGS = -DMOTOR_TSDZ2
endif

ifdef MOTOR_BAFANG
$(warning )
$(warning MOTOR: BAFANG)
MOTOR = BAFANG_M500_M600
CFLAGS = -DMOTOR_BAFANG
endif

# project build flags
BOOTLOADER_VERSION:= 10 # integer 10's digit MAJOR VERSION , 1's digit MINOR VERSION ie: 10 = Version 1.0
DEBUG ?= 0
SD_REQ := 0xB9 #s340 sd is required
NRF_DFU_BL_ALLOW_DOWNGRADE := 1 #allow the bootloader to be downgraded
BOARD_USE_SF_CLOCK := 1 #use the softdevice LFCLK



ifeq ($(DEBUG), 1)
#SOFT_DEVICE = $(PROJ_DIR)/include/S340_and_TSDZ2_remote.hex
SOFT_DEVICE = $(SDK_ROOT)/components/softdevice/s340/hex/ANT_s340_nrf52840_6.1.1.hex
Expand All @@ -24,8 +37,6 @@ endif
$(OUTPUT_DIRECTORY)/nrf52840_xxaa.out: \
LINKER_SCRIPT := secure_bootloader_gcc_nrf52.ld



# Source files common to all targets
SRC_FILES += \
$(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52840.S \
Expand Down
8 changes: 8 additions & 0 deletions firmware/EBike_wireless_bootloader/firmware/include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ typedef enum {
// NRF52840 NORDIC Dongle
#if defined(BOARD_PCA10059)

#ifdef MOTOR_TSDZ2
typedef enum {
PLUS__PIN = 13,
MINUS__PIN = 15,
BUTTON_PIN_ELEMENTS = 2 // must be updated when added or removed an element
} button_pins_t;
#elif defined(MOTOR_BAFANG)
typedef enum {
PLUS__PIN = 13,
BUTTON_PIN_ELEMENTS = 1 // must be updated when added or removed an element
} button_pins_t;
#endif

#endif

// NRF52840 Dongle (The Blue One)
Expand Down
20 changes: 20 additions & 0 deletions firmware/EBike_wireless_bootloader/firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,14 @@ static bool gpio_init(void)
err_code = nrf_drv_gpiote_init();
APP_ERROR_CHECK(err_code);

#ifdef MOTOR_TSDZ2
bootloader_pin_pressed = check_pin_and_enable_interrupt(PLUS__PIN);
bootloader_pin_pressed |= check_pin_and_enable_interrupt(MINUS__PIN);
#elif defined(MOTOR_BAFANG)
bootloader_pin_pressed = check_pin_and_enable_interrupt(PLUS__PIN);
#else
#error MOTOR_TSDZ2 or MOTOR_BAFANG must be defined
#endif

return bootloader_pin_pressed;
}
Expand All @@ -280,8 +286,12 @@ static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
if (int_type == NRF_DRV_RTC_INT_COMPARE0)
{
#ifdef MOTOR_TSDZ2
if ((read_pin(PLUS__PIN) == 0) &&
(read_pin(MINUS__PIN) == 0))
#elif defined(MOTOR_BAFANG)
if (read_pin(PLUS__PIN) == 0)
#endif
{
g_start_bootloader = true;
}
Expand All @@ -308,7 +318,12 @@ static void rtc_config(void)
APP_ERROR_CHECK(err_code);

//Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
#ifdef MOTOR_TSDZ2
err_code = nrf_drv_rtc_cc_set(&rtc, 0, 10 * 8, true);
#elif defined(MOTOR_BAFANG)
err_code = nrf_drv_rtc_cc_set(&rtc, 0, 10 * 3, true);
#endif

APP_ERROR_CHECK(err_code);

//Power on RTC instance
Expand Down Expand Up @@ -363,10 +378,15 @@ int main(void)
lfclk_config();

// disable buttons
#ifdef MOTOR_TSDZ2
nrf_drv_gpiote_in_uninit(PLUS__PIN);
nrf_drv_gpiote_in_event_disable(PLUS__PIN);
nrf_drv_gpiote_in_uninit(MINUS__PIN);
nrf_drv_gpiote_in_event_disable(MINUS__PIN);
#elif defined(MOTOR_BAFANG)
nrf_drv_gpiote_in_uninit(PLUS__PIN);
nrf_drv_gpiote_in_event_disable(PLUS__PIN);
#endif

// RTC uninit because it will be used by the softdevice
rtc_uninit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"settings": {
"git.ignoreLimitWarning": true,
"files.associations": {
"nrf_assert.h": "c"
}
"nrf_assert.h": "c",
"pins.h": "c"
}
}
}
8 changes: 7 additions & 1 deletion firmware/display/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,13 @@ for_development_only: display_firmware bl_settings final


TSDZ2_bootloader:
cd $(BOOTLOADER_DIR) && make -f ./Makefile
ifdef MOTOR_TSDZ2
cd $(BOOTLOADER_DIR) && make -f ./Makefile MOTOR_TSDZ2=1
endif

ifdef MOTOR_BAFANG
cd $(BOOTLOADER_DIR) && make -f ./Makefile DMOTOR_BAFANG=1
endif

# We use srec_cat here but if you want to be able to use the combined hex file with nRF Connect you should use Nordic's mergehex tool
final:
Expand Down
5 changes: 5 additions & 0 deletions firmware/display/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "buttons.h"
#include "state.h"

#ifdef MOTOR_TSDZ2

#elif defined(MOTOR_BAFANG)

static rt_vars_t *mp_rt_vars = NULL;
static ui_vars_t *mp_ui_vars = NULL;

Expand Down Expand Up @@ -264,3 +268,4 @@ void can_set_max_wheel_speed(uint16_t value) {
CANSPI_Transmit(&canMessage);
nrf_delay_ms(100);
}
#endif
4 changes: 3 additions & 1 deletion firmware/display/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,9 @@ int main(void)
}
}

#ifdef MOTOR_BAFANG
#ifdef MOTOR_TSDZ2

#elif defined(MOTOR_BAFANG)
// process the CAN messages
// call as fast as possible
ui32_time_now = get_time_base_counter_1ms();
Expand Down
16 changes: 10 additions & 6 deletions firmware/display/mainscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,16 @@ Field custom1 = FIELD_CUSTOMIZABLE_PTR(&ui_vars.field_selectors[0], customizable
custom6 = FIELD_CUSTOMIZABLE_PTR(&ui_vars.field_selectors[5], customizables);


Field bootHeading = FIELD_DRAWTEXT_RO("EasyDIY"),
bootURL_1 = FIELD_DRAWTEXT_RO("Bafang"),
bootURL_2 = FIELD_DRAWTEXT_RO("M500/M600"),

#ifdef MOTOR_TSDZ2
Field bootHeading = FIELD_DRAWTEXT_RO("EasyDIY"),
bootURL_1 = FIELD_DRAWTEXT_RO("TSDZ2"),
bootURL_2 = FIELD_DRAWTEXT_RO(""),
bootVersion = FIELD_DRAWTEXT_RO(VERSION_STRING),
bootStatus2 = FIELD_DRAWTEXT_RW(.msg = "");
#elif defined(MOTOR_BAFANG)
Field bootHeading = FIELD_DRAWTEXT_RO("EasyDIY"),
bootURL_1 = FIELD_DRAWTEXT_RO("Bafang"),
bootURL_2 = FIELD_DRAWTEXT_RO("M500/M600"),
bootVersion = FIELD_DRAWTEXT_RO(VERSION_STRING);
#endif

Expand Down Expand Up @@ -569,7 +571,8 @@ void mainscreen_clock(void) {
DisplayResetToDefaults();
TripMemoriesReset();
DisplayResetBluetoothPeers();
#ifdef MOTOR_BAFANG
#ifdef MOTOR_TSDZ2
#elif defined(MOTOR_BAFANG)
updateAssistLevels();
torqueSensorCalibration();
positionSensorCalibration();
Expand Down Expand Up @@ -1028,7 +1031,8 @@ void onSetConfigurationBatterySOCUsedWh(uint32_t v) {
ui_vars.ui32_wh_x10_offset = v;
}

#ifdef MOTOR_BAFANG
#ifdef MOTOR_TSDZ2
#elif defined(MOTOR_BAFANG)
void onSetConfigurationChangeMaxWheelSpeed(uint32_t v) {
can_set_max_wheel_speed(v);
}
Expand Down
1 change: 1 addition & 0 deletions firmware/display/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "state.h"
#include "ledalert.h"
#include "display.h"
#include "buttons.h"

typedef enum {
FRAME_TYPE_ALIVE = 0,
Expand Down

0 comments on commit 7f402f4

Please sign in to comment.