servo/controller/fw/embed/bootloader/main.cpp

110 lines
2.2 KiB
C++

#include <Arduino.h>
#include <STM32_CAN.h>
//#include "CRC32.h"
#include "flash.h"
//CRC
// put function declarations here:
STM32_CAN Can(CAN2, DEF);
static CAN_message_t CAN_TX_msg;
static CAN_message_t CAN_inMsg;
void buff_data_can(size_t len,size_t len_frame) {
uint8_t buff[len] = {0};
uint32_t buff_32[len / 4] = {0};
uint32_t bias = 0;
while(Can.read(CAN_inMsg)) {
for(size_t i = 0; i < len_frame; i++,bias++) {
buff[bias] = CAN_inMsg.buf[i];
/* if data from len no end blink led 5 count and stop programm */
if(bias >= len){
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
GPIOC->MODER |= GPIO_MODER_MODE10_0;
for(int i = 0;i < 5;i++){
GPIOC->ODR ^= GPIO_ODR_OD10;
HAL_Delay(500);
}
return;
}
}
}
/* from 8byte to 32 byte */
for(size_t i = 0; i < len / 4; i++) {
buff_32[i] = (buff[i*4] << 24) |
(buff[i*4 + 1] << 16) |
(buff[i*4 + 2] << 8) |
buff[i*4 + 3];
}
/* CRC check */
/* Work with FLASH */
FLASH_UNLOCK;
uint32_t address = (uint32_t)APP_ADDR;
FLASH_32BYTE;
for(size_t i = 0; i < len; i++) {
if(i == 0) {
flash_erase_sector(1); // Erase sector 1 (APP_ADDR)
while(FLASH_BUSY);
}
flash_program_word(address + (i * 4), buff_32[i],0);
while(FLASH_BUSY);
}
flash_lock();
}
void setup() {
Serial.setRx(HARDWARE_SERIAL_RX_PIN);
Serial.setTx(HARDWARE_SERIAL_TX_PIN);
Serial.begin(115200);
Can.begin();
Can.setBaudRate(1000000);
// put your setup code here, to run once:
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
GPIOC->MODER |= GPIO_MODER_MODE10_0 | GPIO_MODER_MODE11_0;;
GPIOC->ODR |= GPIO_ODR_OD11;
}
void loop() {
flash_program_word(FLAG_BOOT,1,1);
for(int i = 0; i < 4; i++ ) {
GPIOC->ODR ^= GPIO_ODR_OD11;
HAL_Delay(500);
}
if(FLAG_BOOT == 1){
while (Can.read(CAN_inMsg)){
// buff_data_can(32000,8);
}
/* Go to application */
volatile uint32_t* appjump = (volatile uint32_t*)APP_ADDR;
uint32_t msp_start = *(appjump);
uint32_t reset_handler = *(appjump + 1);
pFunction start = (pFunction)reset_handler;
__set_MSP(msp_start);
start();
}
// put your main code here, to run repeatedly:
}