diff --git a/controller/fw/embed/src/main.cpp b/controller/fw/embed/src/main.cpp index b342add..0a7074b 100644 --- a/controller/fw/embed/src/main.cpp +++ b/controller/fw/embed/src/main.cpp @@ -11,9 +11,24 @@ #include "wiring_analog.h" #include "wiring_constants.h" // clang-format on +#include "reg_cah.h" + + + + STM32_CAN Can(CAN2, DEF); +/* for FLASH */ +uint32_t flash_flag; +uint8_t flag_can = 0; +uint32_t flash_error; +FLASH_EraseInitTypeDef pEraseInit; +uint32_t SectorError; + + + + static CAN_message_t CAN_TX_msg; static CAN_message_t CAN_inMsg; @@ -34,6 +49,7 @@ struct MotorControlInputs { float target_angle = 0.0; float target_velocity = 0.0; bool motor_enabled = false; + bool foc_state = false; }; MotorControlInputs motor_control_inputs; @@ -109,39 +125,128 @@ void send_motor_enabled() { Can.write(CAN_TX_msg); } +void send_foc_state(){ + CAN_TX_msg.id = 1; + CAN_TX_msg.buf[0] = 'F'; + memcpy(&CAN_TX_msg.buf[1], &motor_control_inputs.foc_state, + sizeof(motor_control_inputs.foc_state)); +Can.write(CAN_TX_msg); +} + +void send_id(){ + /* data for reading of firmware */ + FLASH->KEYR = 0x45670123; // Первый ключ + FLASH->KEYR = 0xCDEF89AB; // Второй ключ + uint8_t id = *(volatile uint32_t*)ADDR_VAR_ID; + FLASH->CR |= FLASH_CR_LOCK; + CAN_TX_msg.id = id; + CAN_TX_msg.buf[0] = 0x22; + memcpy(&CAN_TX_msg.buf[1], &id,sizeof(id)); +Can.write(CAN_TX_msg); +} + +void setup_id(uint8_t my_id){ + /* Чтобы разблокировать флэш память для записи*/ + FLASH->KEYR = 0x45670123; // Первый ключ + FLASH->KEYR = 0xCDEF89AB; // Второй ключ + while (FLASH->SR & FLASH_SR_BSY); + FLASH->CR &= ~FLASH_CR_PSIZE_0 & ~FLASH_CR_PSIZE_1; //Data write = 8bit + FLASH->CR |= FLASH_CR_SNB_1 | FLASH_CR_SNB_2; //6 SECTOR FOR ERASE + FLASH->CR |= FLASH_CR_SER; + FLASH->CR |= FLASH_CR_STRT; + while((FLASH->SR & FLASH_SR_BSY) != 0) + __NOP(); + FLASH->SR |= (FLASH_SR_EOP | FLASH_SR_WRPERR | FLASH_SR_PGAERR); + FLASH->CR |= FLASH_CR_PG; + *(volatile uint8_t*)ADDR_VAR_ID = my_id; + + FLASH->CR |= FLASH_CR_LOCK; + +} + + + void send_data() { send_velocity(); send_angle(); send_motor_enabled(); + // read_temperature(); digitalWrite(PC11, !digitalRead(PC11)); } -void read_can_step() { - char flag = CAN_inMsg.buf[0]; + + +/*void read_can_step() { + char flag = CAN_inMsg.buf[1]; if (flag == 'V') { motor.enable(); - memcpy(&motor_control_inputs.target_velocity, &CAN_inMsg.buf[1], + memcpy(&motor_control_inputs.target_velocity, &CAN_inMsg.buf[2], sizeof(motor_control_inputs.target_velocity)); } else if (flag == 'A') { motor.enable(); - memcpy(&motor_control_inputs.target_angle, &CAN_inMsg.buf[1], + memcpy(&motor_control_inputs.target_angle, &CAN_inMsg.buf[2], sizeof(motor_control_inputs.target_angle)); } else if (flag == 'E') { - bool enable_flag = CAN_inMsg.buf[1]; + bool enable_flag = CAN_inMsg.buf[2]; if (enable_flag == 1) { - memcpy(&motor_control_inputs.motor_enabled, &CAN_inMsg.buf[1], + memcpy(&motor_control_inputs.motor_enabled, &CAN_inMsg.buf[2], sizeof(motor_control_inputs.motor_enabled)); motor.enable(); } else if (enable_flag == 0) { - memcpy(&motor_control_inputs.motor_enabled, &CAN_inMsg.buf[1], + memcpy(&motor_control_inputs.motor_enabled, &CAN_inMsg.buf[2], sizeof(motor_control_inputs.motor_enabled)); motor.disable(); } } - digitalWrite(PC10, !digitalRead(PC10)); + GPIOC->ODR ^= GPIO_ODR_OD10; //digitalWrite(PC10, !digitalRead(PC10)); +}*/ + + + + +void listen_can(){ + + uint8_t reg_id = CAN_inMsg.id; + if(CAN_inMsg.buf[0] == REG_WRITE){ + switch (reg_id) + { + case REG_ID: + /* setup new id */ + setup_id(CAN_inMsg.buf[1]); + break; + + case REG_LED_BLINK: + for(int i = 0;i < 10;i++){ + GPIOC->ODR ^= GPIO_ODR_OD10; + HAL_Delay(100); + } + break; + + default: + break; + } + } + + else if(CAN_inMsg.buf[0] == REG_READ){ + switch (reg_id) + { + case REG_ID: + send_id(); + + break; + + default: + break; + } + } + } + + + + void foc_step(BLDCMotor *motor, Commander *commander) { if (motor_control_inputs.target_velocity != 0 || motor->controller == MotionControlType::velocity) { @@ -163,7 +268,12 @@ void foc_step(BLDCMotor *motor, Commander *commander) { commander->run(); } -void setup() { + + + + + +void setup(){ Serial.setRx(HARDWARE_SERIAL_RX_PIN); Serial.setTx(HARDWARE_SERIAL_TX_PIN); Serial.begin(115200); @@ -180,11 +290,21 @@ void setup() { SendTimer->attachInterrupt(send_data); SendTimer->resume(); setup_foc(&encoder, &motor, &driver, ¤t_sense, &command, doMotor); + + /* Настройка параметров стирания */ + /*pEraseInit.TypeErase = TYPEERASE_SECTORS; // Стирание страниц + pEraseInit.Sector = ADDR_VAR; // Начальная страница + pEraseInit.NbSectors = 1; + pEraseInit.VoltageRange = VOLTAGE_RANGE_3;*/ +} + void loop(){ + foc_step(&motor, &command); + GPIOC->ODR ^= GPIO_ODR_OD10; + HAL_Delay(1000); + while (Can.read(CAN_inMsg)) { + listen_can(); + + } } -void loop() { - foc_step(&motor, &command); - while (Can.read(CAN_inMsg)) { - read_can_step(); - } -} +