Skip to content

Commit

Permalink
added DroneCAN support
Browse files Browse the repository at this point in the history
support for droneCAN on L431 and F415
  • Loading branch information
tridge authored and AlkaMotors committed Nov 11, 2024
1 parent 4ec015e commit b219de9
Show file tree
Hide file tree
Showing 141 changed files with 14,207 additions and 402 deletions.
17 changes: 17 additions & 0 deletions Inc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ extern char ic_timer_prescaler;
extern uint8_t buffersize;
extern char output_timer_prescaler;
extern uint8_t compute_dshot_flag;
extern uint16_t battery_voltage;
extern int16_t actual_current;
extern uint16_t e_rpm;


#ifdef STMICRO
extern GPIO_TypeDef* current_GPIO_PORT;
#ifndef MCU_F031
Expand Down Expand Up @@ -73,3 +78,15 @@ typedef struct fastPID {
int32_t integral_limit;
int32_t output_limit;
} fastPID;

/*
input signal types
*/
enum inputType {
AUTO_IN = 0,
DSHOT_IN = 1,
SERVO_IN = 2,
SERIAL_IN = 3,
EDTARM_IN = 4,
DRONECAN_IN = 5,
};
14 changes: 12 additions & 2 deletions Inc/eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,18 @@ typedef union EEprom_u {
uint8_t auto_advance; // 47
uint8_t reserved_2[4]; //48-51
uint8_t tune[124]; // 52-175
struct {
uint8_t can_node; // 176
uint8_t esc_index; // 177
uint8_t require_arming; // 178
uint8_t telem_rate; // 179
uint8_t require_zero_throttle; // 180
uint8_t filter_hz; // 181
uint8_t debug_rate; // 182
uint8_t reserved[9]; // 183-191
} can;
};
uint8_t buffer[184];
uint8_t buffer[192];
} EEprom_t;

extern EEprom_t eepromBuffer;
Expand All @@ -59,4 +69,4 @@ extern EEprom_t eepromBuffer;
// void read_flash(uint8_t* data, uint32_t address);
// void save_to_flash_bin(uint8_t *data, int length, uint32_t add);
void read_flash_bin(uint8_t* data, uint32_t add, int out_buff_len);
void save_flash_nolib(uint8_t* data, int length, uint32_t add);
void save_flash_nolib(uint8_t* data, int length, uint32_t add);
110 changes: 106 additions & 4 deletions Inc/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
#define EEPROM_START_ADD (uint32_t)0x0800F800
#endif

#ifdef VIMDRONES_L431_CAN
#define FIRMWARE_NAME "VimdroneL431"
#define FILE_NAME "VIMDRONES_L431_CAN"
#define DRONECAN_SUPPORT 1
#define DEAD_TIME 45
#define HARDWARE_GROUP_L4_B
#define TARGET_VOLTAGE_DIVIDER 94
#define MILLIVOLT_PER_AMP 30
#define CURRENT_OFFSET 110
#define USE_SERIAL_TELEMETRY
#endif

#ifdef VIMDRONES_NANO_L431
#define FIRMWARE_NAME "VM_NANO"
#define FILE_NAME "VIMDRONES_NANO_L431"
Expand All @@ -89,7 +101,31 @@
#define TARGET_VOLTAGE_DIVIDER 110
#define MILLIVOLT_PER_AMP 20
#define USE_SERIAL_TELEMETRY
#define EEPROM_START_ADD (uint32_t)0x0800F800
#endif

#ifdef VIMDRONES_NANO_L431_CAN
#define FIRMWARE_NAME "VM_NANO_CAN"
#define FILE_NAME "VIMDRONES_NANO_L431_CAN"
#define DRONECAN_SUPPORT 1
#define USE_HSE
#undef HSE_VALUE
#define HSE_VALUE 24000000
#define DEAD_TIME 45
#define HARDWARE_GROUP_L4_B
#define TARGET_VOLTAGE_DIVIDER 110
#define MILLIVOLT_PER_AMP 20
#define USE_SERIAL_TELEMETRY
#endif

#ifdef SIERRA_PULSE1_L431_CAN
#define FIRMWARE_NAME "SierraPulse1"
#define FILE_NAME "SIERRA_PULSE1_L431_CAN"
#define DRONECAN_SUPPORT 1
#define DEAD_TIME 45
#define HARDWARE_GROUP_L4_C
#define TARGET_VOLTAGE_DIVIDER 100
#define MILLIVOLT_PER_AMP 100
#define USE_SERIAL_TELEMETRY
#endif

#ifdef REF_L431
Expand Down Expand Up @@ -634,6 +670,43 @@
#define LOOP_FREQUENCY_HZ 20000
#endif

#ifdef TBS_12S_F415
#define FIRMWARE_NAME "TBS 12S F415"
#define FILE_NAME "TBS_12S_F415"
#define DEAD_TIME 120
#define HARDWARE_GROUP_AT_D
#define HARDWARE_GROUP_AT_045
#define USE_SERIAL_TELEMETRY
#define ADC_CHANNEL_VOLTAGE ADC_CHANNEL_6
#define ADC_CHANNEL_CURRENT ADC_CHANNEL_3
#define TARGET_VOLTAGE_DIVIDER 210
#define MILLIVOLT_PER_AMP 25
#define RAMP_SPEED_LOW_RPM 1
#define RAMP_SPEED_HIGH_RPM 1
#define LOOP_FREQUENCY_HZ 20000
#define PA6_VOLTAGE
#define EEPROM_START_ADD (uint32_t)0x0801F800
#endif

#ifdef TBS_12S_F415_CAN
#define FIRMWARE_NAME "TBS 12S CAN"
#define FILE_NAME "TBS_12S_F415_CAN"
#define DEAD_TIME 120
#define HARDWARE_GROUP_AT_D
#define HARDWARE_GROUP_AT_045
#define DRONECAN_SUPPORT 1
#define USE_SERIAL_TELEMETRY
#define ADC_CHANNEL_VOLTAGE ADC_CHANNEL_6
#define ADC_CHANNEL_CURRENT ADC_CHANNEL_3
#define TARGET_VOLTAGE_DIVIDER 210
#define MILLIVOLT_PER_AMP 25
#define RAMP_SPEED_LOW_RPM 1
#define RAMP_SPEED_HIGH_RPM 1
#define LOOP_FREQUENCY_HZ 20000
#define PA6_VOLTAGE
#define EEPROM_START_ADD (uint32_t)0x0801F800
#endif

#ifdef AIKON_SINGLE_F421
#define FIRMWARE_NAME "AIKON S F4 "
#define FILE_NAME "AIKON_SINGLE_F421"
Expand Down Expand Up @@ -663,6 +736,23 @@
#define USE_SERIAL_TELEMETRY
#endif

#ifdef REF_F415_CAN
#define FIRMWARE_NAME "REF F415 CAN"
#define FILE_NAME "REF_F415_CAN"
#define DRONECAN_SUPPORT 1
#define DEAD_TIME 120
#define HARDWARE_GROUP_AT_D
#define HARDWARE_GROUP_AT_045
#define USE_SERIAL_TELEMETRY
#define ADC_CHANNEL_VOLTAGE ADC_CHANNEL_6
#define ADC_CHANNEL_CURRENT ADC_CHANNEL_3
#define TARGET_VOLTAGE_DIVIDER 210
#define MILLIVOLT_PER_AMP 25
#define RAMP_SPEED_LOW_RPM 1
#define RAMP_SPEED_HIGH_RPM 1
#define LOOP_FREQUENCY_HZ 20000
#endif

#ifdef DAKEFPV_35A_F415
#define FIRMWARE_NAME "DakeFPV 35A"
#define FILE_NAME "DAKEFPV_35A_F415" // DISABLE_BUILD
Expand All @@ -680,10 +770,11 @@
#define USE_SERIAL_TELEMETRY
#endif

#ifdef TBS_CAN_F415
#define FIRMWARE_NAME "TBS_CAN_F415"
#define FILE_NAME "TBS_CAN_F415"
#ifdef TBS_F415_CAN
#define FIRMWARE_NAME "TBS_F415_CAN"
#define FILE_NAME "TBS_F415_CAN"
#define DEAD_TIME 120
#define DRONECAN_SUPPORT 1
#define HARDWARE_GROUP_AT_H
#define HARDWARE_GROUP_AT_045
#define TARGET_VOLTAGE_DIVIDER 210
Expand Down Expand Up @@ -3387,7 +3478,9 @@
#ifdef MCU_AT415
#define ARTERY
#define CPU_FREQUENCY_MHZ 144
#ifndef EEPROM_START_ADD
#define EEPROM_START_ADD (uint32_t)0x08007C00
#endif
#define INTERVAL_TIMER TMR4
#define TEN_KHZ_TIMER TMR9
#define UTILITY_TIMER TMR10
Expand Down Expand Up @@ -3438,3 +3531,12 @@
#endif

#define PID_LOOP_DIVIDER (LOOP_FREQUENCY_HZ / 1000)

// default to no DroneCAN support
#ifndef DRONECAN_SUPPORT
#define DRONECAN_SUPPORT 0
#elif DRONECAN_SUPPORT == 1
// all DroneCAN ESCs use 128k flash layout
#undef EEPROM_START_ADD
#define EEPROM_START_ADD (uint32_t)0x0801F800
#endif
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FIRMWARE_VERSION := $(VERSION_MAJOR).$(VERSION_MINOR)
# Compiler options

CFLAGS_BASE := -fsingle-precision-constant -fomit-frame-pointer -ffast-math
CFLAGS_BASE += -I$(MAIN_INC_DIR) -g3 -O2 -ffunction-sections
CFLAGS_BASE += -I$(MAIN_INC_DIR) -g3 -O2 -ffunction-sections --specs=nosys.specs
CFLAGS_BASE += -Wall -Wundef -Wextra -Werror -Wno-unused-parameter -Wno-stringop-truncation

CFLAGS_COMMON := $(CFLAGS_BASE)
Expand All @@ -59,6 +59,9 @@ SRC_COMMON := $(foreach dir,$(SRC_DIRS_COMMON),$(wildcard $(dir)/*.[cs]))
OBJ := obj
BIN_DIR := $(ROOT)/$(OBJ)

# Function to check for _CAN suffix
has_can_suffix = $(findstring _CAN,$1)

# find the SVD files
$(foreach MCU,$(MCU_TYPES),$(eval SVD_$(MCU) := $(wildcard $(HAL_FOLDER_$(MCU))/*.svd)))

Expand Down Expand Up @@ -88,20 +91,26 @@ $$($(2)_BASENAME).bin: $$($(2)_BASENAME).elf
echo building BIN $$@
@$(ECHO) Generating $$(notdir $$@)
$(QUIET)$(OBJCOPY) -O binary $$(<) $$@
$(QUIET)python3 Src/DroneCAN/set_app_signature.py $$@ $$(<)
$(QUIET)$(OBJCOPY) $$(<) -O ihex $$(@:.bin=.hex)
$(QUIET)$(CP) -f $$(<) $(OBJ)$(DSEP)debug.elf > $(NUL)

# check for CAN support
$(eval xLDSCRIPT := $$(if $$(call has_can_suffix,$$(2)),$(LDSCRIPT_CAN_$(1)),$(LDSCRIPT_$(1))))
$(eval xCFLAGS := $$(if $$(call has_can_suffix,$$(2)),$(CFLAGS_CAN_$(1))))
$(eval xSRC := $$(if $$(call has_can_suffix,$$(2)),$(SRC_CAN_$(1))))

CFLAGS_$(2) = $(MCU_$(1)) -D$(2) $(CFLAGS_$(1)) $(CFLAGS_COMMON)
LDFLAGS_$(2) = $(LDFLAGS_COMMON) $(LDFLAGS_$(1)) -T$(LDSCRIPT_$(1))
CFLAGS_$(2) = -DAM32_MCU=\"$(MCU)\" $(MCU_$(1)) -D$(2) $(CFLAGS_$(1)) $(CFLAGS_COMMON) $(xCFLAGS)
LDFLAGS_$(2) = $(LDFLAGS_COMMON) $(LDFLAGS_$(1)) -T$(xLDSCRIPT)

-include $$($(2)_BASENAME).d

$$($(2)_BASENAME).elf: $(SRC_COMMON) $$(SRC_$(1))
$$($(2)_BASENAME).elf: $(SRC_COMMON) $$(SRC_$(1)) $(xSRC)
@$(ECHO) Compiling $$(notdir $$@)
$(QUIET)$(MKDIR) -p $(OBJ)
$(QUIET)$(CC) $$(CFLAGS_$(2)) $$(LDFLAGS_$(2)) -MMD -MP -MF $$(@:.elf=.d) -o $$(@) $(SRC_COMMON) $$(SRC_$(1))
$(QUIET)$(CC) $$(CFLAGS_$(2)) $$(LDFLAGS_$(2)) -MMD -MP -MF $$(@:.elf=.d) -o $$(@) $(SRC_COMMON) $$(SRC_$(1)) $(xSRC)
# we copy debug.elf to give us a constant debug target for vscode
# this means the debug button will always debug the last target built
$(QUIET)$(CP) -f $$(@) $(OBJ)$(DSEP)debug.elf > $(NUL)
$(QUIET)$(CP) -f $$(SVD_$(1)) $(OBJ)/debug.svd
# also copy the openocd.cfg from the MCU directory to obj/openocd.cfg for auto config of Cortex-Debug
# in vscode
Expand Down
1 change: 1 addition & 0 deletions Mcu/f415/Drivers/drivers/inc/at32f415_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ extern "C" {
* @{
*/

#define CAN1_GMUX_0000 IOMUX_MAKE_VALUE(0x2C, 0, 4, 0x00) /*!< can_rx(pa11), can_tx(pa12) */
#define CAN1_GMUX_0010 IOMUX_MAKE_VALUE(0x2C, 0, 4, 0x02) /*!< can_rx(pb8), can_tx(pb9) */
#define SDIO1_GMUX_0100 IOMUX_MAKE_VALUE(0x2C, 8, 4, 0x04) /*!< sdio1_ck(pc4), sdio1_cmd(pc5), sdio1_d0(pc0), sdio1_d1(pc1), sdio1_d2(pc2), sdio1_d3(pc3), sdio1_d4(pa4), sdio1_d5(pa5), sdio1_d6(pa6), sdio1_d7(pa7) */
#define SDIO1_GMUX_0101 IOMUX_MAKE_VALUE(0x2C, 8, 4, 0x05) /*!< sdio1_ck(pc4), sdio1_cmd(pc5), sdio1_d0(pa4), sdio1_d1(pa5), sdio1_d2(pa6), sdio1_d3(pa7) */
Expand Down
71 changes: 38 additions & 33 deletions Mcu/f415/Src/eeprom.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
#include "eeprom.h"
#include "targets.h"

#include <string.h>
#include <stdbool.h>

//#pragma GCC optimize("O0")

#include "at32f415_flash.h"

// #define APP_START (uint32_t)0x08001000
// #define FLASH_STORAGE 0x08005000 // at the 31kb mark
#define page_size 0x400 // 1 kb for f051
// uint32_t FLASH_FKEY1 =0x45670123;
// uint32_t FLASH_FKEY2 =0xCDEF89AB;
/*
the F415 can be either 1k or 2k sector size. The 256k flash part has
2k sector size. We only support ESCs with 128k flash or less, so 1k
sector size, but it is useful to work with 2k sector size when using
a F415 dev board like the AT-Start F415, so we detect here.
*/
static inline uint32_t sector_size()
{
const uint16_t *F_SIZE = (const uint16_t *)0x1FFFF7E0;
if (*F_SIZE <= 128) {
// 1k sectors for 128k flash or less
return 1024;
}
// 256k flash is 2k sectors
return 2048;
}

void save_flash_nolib(uint8_t* data, int length, uint32_t add)
{ /// todo

// fmc_wscnt_set(2);

// fmc_prefetch_enable();

uint32_t data_to_FLASH[length / 4];
memset(data_to_FLASH, 0, length / 4);
for (int i = 0; i < length / 4; i++) {
data_to_FLASH[i] = data[i * 4 + 3] << 24 | data[i * 4 + 2] << 16 | data[i * 4 + 1] << 8 | data[i * 4]; // make 16 bit
{
if ((add & 0x3) != 0 || (length & 0x3) != 0) {
return;
}
volatile uint32_t data_length = length / 4;
/*
we need the data to be 32 bit aligned
*/
const uint32_t word_length = length / 4;

// unlock flash

flash_unlock();

// erase page if address even divisable by 1024
if ((add % 1024) == 0) {
flash_sector_erase(add);
// erase page if address even divisable by sector size
if ((add % sector_size()) == 0) {
flash_sector_erase(add);
}

volatile uint32_t index = 0;
while (index < data_length) {
// fmc_word_program(add + (index*4),data_to_FLASH[index]);
flash_word_program(add + (index * 4), data_to_FLASH[index]);
// fmc_flag_clear(FMC_FLAG_END |
// FMC_FLAG_WPERR |
// FMC_FLAG_PGERR);
flash_flag_clear(FLASH_PROGRAM_ERROR | FLASH_EPP_ERROR | FLASH_OPERATE_DONE);
uint32_t index = 0;
while (index < word_length) {
uint32_t word;
memcpy(&word, &data[index*4], sizeof(word));
flash_word_program(add + (index * 4), word);
flash_flag_clear(FLASH_PROGRAM_ERROR | FLASH_EPP_ERROR | FLASH_OPERATE_DONE);
index++;
}
flash_lock();
}

void read_flash_bin(uint8_t* data, uint32_t add, int out_buff_len)
{
// volatile uint32_t read_data;
for (int i = 0; i < out_buff_len; i++) {
data[i] = *(uint8_t*)(add + i);
}
}
memcpy(data, (void*)add, out_buff_len);
}
11 changes: 8 additions & 3 deletions Mcu/f415/Src/system_at32f415.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@
*/

#include "at32f415.h"
#include "targets.h"
#include "string.h"

/** @addtogroup AT32F415_system_private_defines
* @{
*/
#define VECT_TAB_OFFSET \
0x1000 /*!< vector table base offset field. this value must be a multiple \
of 0x200. */
/*!< vector table base offset field. this value must be a multiple of 0x200. */
#if DRONECAN_SUPPORT
#define VECT_TAB_OFFSET 0x4000
#else
#define VECT_TAB_OFFSET 0x1000
#endif
/**
* @}
*/
Expand Down
Loading

0 comments on commit b219de9

Please sign in to comment.