diff --git a/Inc/targets.h b/Inc/targets.h index 062f48fc..53d5cd5c 100644 --- a/Inc/targets.h +++ b/Inc/targets.h @@ -93,6 +93,30 @@ #define EEPROM_START_ADD (uint32_t)0x0801F800 #endif +#ifdef VIMDRONES_NANO_L431 +#define FIRMWARE_NAME "VM_NANO" +#define FILE_NAME "VIMDRONES_NANO_L431" +#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 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 2 +#define USE_SERIAL_TELEMETRY +#endif + #ifdef SIERRA_PULSE1_L431_CAN #define FIRMWARE_NAME "SierraPulse1" #define FILE_NAME "SIERRA_PULSE1_L431_CAN" @@ -102,18 +126,7 @@ #define TARGET_VOLTAGE_DIVIDER 100 #define MILLIVOLT_PER_AMP 100 #define USE_SERIAL_TELEMETRY -#define EEPROM_START_ADD (uint32_t)0x0800F800 -#endif - -#ifdef VIMDRONES_NANO_L431 -#define FIRMWARE_NAME "VM_NANO" -#define FILE_NAME "VIMDRONES_NANO_L431" -#define DEAD_TIME 45 -#define HARDWARE_GROUP_L4_B -#define TARGET_VOLTAGE_DIVIDER 110 -#define MILLIVOLT_PER_AMP 20 -#define USE_SERIAL_TELEMETRY -#define EEPROM_START_ADD (uint32_t)0x0800F800 +#define EEPROM_START_ADD (uint32_t)0x0801F800 #endif #ifdef REF_L431 diff --git a/Mcu/l431/Src/peripherals.c b/Mcu/l431/Src/peripherals.c index 83bc3f32..f0429fa6 100644 --- a/Mcu/l431/Src/peripherals.c +++ b/Mcu/l431/Src/peripherals.c @@ -60,6 +60,17 @@ void SystemClock_Config(void) while (LL_PWR_IsActiveFlag_VOS() != 0) { } + +#ifdef USE_HSE + LL_RCC_HSE_EnableBypass(); + LL_RCC_HSE_Enable(); +#if HSE_VALUE == 24000000 + LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_3, 20, LL_RCC_PLLR_DIV_2); +#else +#error "Unsupported HSE_VALUE" +#endif + +#else LL_RCC_MSI_Enable(); /* Wait till MSI is ready */ @@ -71,6 +82,8 @@ void SystemClock_Config(void) LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_6); LL_RCC_MSI_SetCalibTrimming(0); LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_MSI, LL_RCC_PLLM_DIV_1, 40, LL_RCC_PLLR_DIV_2); +#endif + LL_RCC_PLL_EnableDomain_SYS(); LL_RCC_PLL_Enable(); diff --git a/Src/DroneCAN/DroneCAN.c b/Src/DroneCAN/DroneCAN.c index 742dd4f9..2be4a57a 100644 --- a/Src/DroneCAN/DroneCAN.c +++ b/Src/DroneCAN/DroneCAN.c @@ -34,6 +34,10 @@ #define CANARD_POOL_SIZE 4096 #endif +#define EEPROM_MOTOR_KV_INDEX 26 +#define EEPROM_TUNE_INDEX 48 +#define EEPROM_TUNE_MAX_LEN 128 + static CanardInstance canard; static uint8_t canard_memory_pool[CANARD_POOL_SIZE]; @@ -80,6 +84,8 @@ static struct enum VarType { T_BOOL = 0, T_UINT8, + T_UINT16, + T_STRING, }; static void can_printf(const char *fmt, ...); @@ -96,6 +102,7 @@ extern char dir_reversed; extern char bi_direction; extern char advance_level; extern char motor_poles; +extern uint16_t motor_kv; extern char VARIABLE_PWM; extern char use_sin_start; extern char comp_pwm; @@ -119,6 +126,7 @@ static struct parameter { { "CAN_NODE", T_UINT8, 0, 127, &settings.can_node, 176}, { "ESC_INDEX", T_UINT8, 0, 32, &settings.esc_index, 177}, { "DIR_REVERSED", T_BOOL, 0, 1, &dir_reversed, 0 }, + { "MOTOR_KV", T_UINT16,0, 10220, &motor_kv, EEPROM_MOTOR_KV_INDEX}, { "BI_DIRECTIONAL", T_BOOL, 0, 1, &bi_direction, 0 }, { "MOTOR_POLES", T_UINT8, 0, 64, &motor_poles, 27 }, { "REQUIRE_ARMING", T_BOOL, 0, 1, &settings.require_arming, 178 }, @@ -128,6 +136,7 @@ static struct parameter { { "COMP_PWM", T_BOOL, 0, 1, &comp_pwm, 0}, { "STUCK_ROTOR_PROTECTION", T_BOOL, 0, 1, &stuck_rotor_protection, 0}, { "ADVANCE_LEVEL", T_UINT8, 0, 4, &advance_level, 0}, + { "TUNE", T_STRING,0, 4, NULL, EEPROM_TUNE_INDEX}, }; /* @@ -273,12 +282,31 @@ static void handle_param_GetSet(CanardInstance* ins, CanardRxTransfer* transfer) eepromBuffer[p->eeprom_index] = *(uint8_t *)p->ptr; } break; + case T_UINT16: + *(uint16_t *)p->ptr = req.value.integer_value; + if (p->eeprom_index == EEPROM_MOTOR_KV_INDEX) { + eepromBuffer[EEPROM_MOTOR_KV_INDEX] = (uint8_t)((*(uint16_t *)p->ptr - 20) / 40); + } + break; case T_BOOL: *(uint8_t *)p->ptr = req.value.boolean_value?1:0; if (p->eeprom_index != 0) { eepromBuffer[p->eeprom_index] = *(uint8_t *)p->ptr; } break; + case T_STRING: + if (req.value.union_tag == UAVCAN_PROTOCOL_PARAM_VALUE_STRING_VALUE) { + if (p->eeprom_index == EEPROM_TUNE_INDEX) { + for (size_t i = 0; i < EEPROM_TUNE_MAX_LEN; i++) { + if (i < req.value.string_value.len) { + eepromBuffer[EEPROM_TUNE_INDEX + i] = req.value.string_value.data[i]; + } else { + eepromBuffer[EEPROM_TUNE_INDEX + i] = 0xFF; + } + } + } + } + break; default: return; } @@ -299,6 +327,19 @@ static void handle_param_GetSet(CanardInstance* ins, CanardRxTransfer* transfer) pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE; pkt.value.integer_value = *(uint8_t *)p->ptr; break; + case T_UINT16: + pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE; + pkt.value.integer_value = *(uint16_t *)p->ptr; + break; + case T_STRING: + pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_STRING_VALUE; + if (p->eeprom_index == EEPROM_TUNE_INDEX) { + pkt.value.string_value.len = EEPROM_TUNE_MAX_LEN; + for (size_t i=0; i < EEPROM_TUNE_MAX_LEN; i++) { + pkt.value.string_value.data[i] = eepromBuffer[EEPROM_TUNE_INDEX + i]; + } + } + break; case T_BOOL: pkt.value.union_tag = UAVCAN_PROTOCOL_PARAM_VALUE_BOOLEAN_VALUE; pkt.value.boolean_value = (*(uint8_t *)p->ptr)?true:false; @@ -852,7 +893,7 @@ static void send_ESCStatus(void) // make up some synthetic status data pkt.error_count = 0; pkt.voltage = battery_voltage * 0.01; - pkt.current = actual_current * 0.02; + pkt.current = actual_current * 0.001; pkt.temperature = C_TO_KELVIN(degrees_celsius); pkt.rpm = (e_rpm * 100) / ((uint8_t)motor_poles); pkt.power_rating_pct = 0; // how do we get this?