Bootloader start work
This commit is contained in:
parent
43185a391c
commit
9396bc3fb2
51 changed files with 10145 additions and 9127 deletions
|
@ -44,7 +44,7 @@ void MX_CAN2_Init(void)
|
|||
hcan2.Init.TimeSeg1 = CAN_BS1_12TQ;
|
||||
hcan2.Init.TimeSeg2 = CAN_BS2_2TQ;
|
||||
hcan2.Init.TimeTriggeredMode = DISABLE;
|
||||
hcan2.Init.AutoBusOff = DISABLE;
|
||||
hcan2.Init.AutoBusOff = ENABLE;
|
||||
hcan2.Init.AutoWakeUp = DISABLE;
|
||||
hcan2.Init.AutoRetransmission = DISABLE;
|
||||
hcan2.Init.ReceiveFifoLocked = DISABLE;
|
||||
|
|
|
@ -17,18 +17,19 @@
|
|||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "flash.h"
|
||||
#include "main.h"
|
||||
#include "adc.h"
|
||||
#include "can.h"
|
||||
#include "spi.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
#include <stdbool.h>
|
||||
#include "can_reg.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "flash.h"
|
||||
#include "can_reg.h"
|
||||
#include <stdbool.h>
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
|
@ -80,7 +81,7 @@ void send_ack(uint8_t status) {
|
|||
uint8_t tx_data[1] = {status};
|
||||
uint32_t tx_mailbox;
|
||||
|
||||
tx_header.ExtId = ACK_CAN_ID; // id = 0x05
|
||||
tx_header.StdId = ACK_CAN_ID; // id = 0x05
|
||||
tx_header.IDE = CAN_ID_STD; //standart id
|
||||
tx_header.RTR = CAN_RTR_DATA; // data frame
|
||||
tx_header.DLC = 1; // data len = 1 byte
|
||||
|
@ -107,11 +108,11 @@ bool verify_firmware() {
|
|||
|
||||
|
||||
void process_can_message(CAN_RxHeaderTypeDef *header, uint8_t *data) {
|
||||
msg_id = header->ExtId;
|
||||
msg_id = header->StdId;
|
||||
/* 0x697
|
||||
69 - slave addr
|
||||
7 || 8 - REG_READ or REG_WRITE */
|
||||
id_x = (msg_id >> 4) & 0xFFFF; // get addr
|
||||
id_x = (msg_id >> 4) & 0xFFF; // get addr
|
||||
msg_ch = msg_id & 0xF; // check cmd
|
||||
|
||||
// Check addr
|
||||
|
@ -144,7 +145,6 @@ void process_can_message(CAN_RxHeaderTypeDef *header, uint8_t *data) {
|
|||
send_ack(0xAA);
|
||||
write_param(firmw, 0); // Reset firmware update
|
||||
fw_update = false;
|
||||
|
||||
HAL_Delay(500);
|
||||
NVIC_SystemReset();
|
||||
} else {
|
||||
|
@ -156,20 +156,33 @@ void process_can_message(CAN_RxHeaderTypeDef *header, uint8_t *data) {
|
|||
}
|
||||
}
|
||||
|
||||
void jump_to_app() {
|
||||
void jump_to_app(void) {
|
||||
HAL_RCC_DeInit();
|
||||
HAL_DeInit();
|
||||
__disable_irq();
|
||||
jump = *(volatile uint32_t*)(APP_ADDRESS + 4);
|
||||
void (*app_entry)(void);
|
||||
app_entry = (void (*)(void))jump;
|
||||
|
||||
uint32_t *app_vector_table = (uint32_t*)APP_ADDRESS;
|
||||
|
||||
|
||||
for (uint32_t i = 0; i < 8; i++) {
|
||||
// RESET ALL Interrupt
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
NVIC->ICER[i] = 0xFFFFFFFF;
|
||||
NVIC->ICPR[i] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
__set_MSP(*(volatile uint32_t*)APP_ADDRESS);
|
||||
// SCB->VTOR = (uint32_t)0x08008004;
|
||||
app_entry();
|
||||
// APP_ADDR
|
||||
__set_MSP(app_vector_table[0]);
|
||||
|
||||
// Point to go
|
||||
uint32_t app_entry = *(app_vector_table + 1); //APP_ADDR + 4
|
||||
void (*application)(void);
|
||||
application = (void (*)(void))app_entry;
|
||||
|
||||
// Go to application
|
||||
application();
|
||||
|
||||
// If we return go to infinity loop
|
||||
while(1);
|
||||
}
|
||||
|
||||
bool is_app_valid() {
|
||||
|
@ -178,11 +191,11 @@ bool is_app_valid() {
|
|||
|
||||
// Check stack pointer
|
||||
bool sp_valid = (app_vector[0] >= 0x20000000) &&
|
||||
(app_vector[0] <= (0x20000000 + 128*1024)); // Для STM32 с 128K RAM
|
||||
(app_vector[0] <= (0x20000000 + 128*1024));
|
||||
|
||||
// check reset_handler
|
||||
bool pc_valid = (app_vector[1] >= 0x08000000) &&
|
||||
(app_vector[1] <= (0x08000000 + 1024*1024)); // Для 1MB Flash
|
||||
(app_vector[1] <= (0x08000000 + 1024*1024));
|
||||
|
||||
// check two words on reset value
|
||||
bool not_erased = (app_vector[0] != 0xFFFFFFFF) &&
|
||||
|
@ -200,29 +213,7 @@ int main(void)
|
|||
{
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
// Настройка GPIO
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
|
||||
GPIOC->MODER |= GPIO_MODER_MODE10_0 | GPIO_MODER_MODE11_0;
|
||||
GPIOC->ODR &= ~GPIO_ODR_OD11;
|
||||
GPIOC->ODR |= GPIO_ODR_OD10;
|
||||
|
||||
flash_record = load_params();
|
||||
if(flash_record[firmw].value == UPDATE_FLAG) {
|
||||
fw_update = true;
|
||||
for(int i = 0; i < 5;i++){
|
||||
GPIOC->ODR ^= GPIO_ODR_OD10; // Indecate message
|
||||
HAL_Delay(100);
|
||||
}
|
||||
// write_param(firmw,0); //reset flasg
|
||||
erase_flash_pages();
|
||||
}
|
||||
else{
|
||||
// for st-link update, because he doesnt reset flag_update
|
||||
if(is_app_valid()) jump_to_app(); //firmware exist
|
||||
else fw_update = true; //firmware doesnt exist, but we in bootloader
|
||||
}
|
||||
|
||||
GPIOC->ODR |= GPIO_ODR_OD10;
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
@ -255,29 +246,82 @@ int main(void)
|
|||
MX_NVIC_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
// Настройка GPIO
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
|
||||
GPIOC->MODER |= GPIO_MODER_MODE10_0 | GPIO_MODER_MODE11_0;
|
||||
GPIOC->ODR &= ~GPIO_ODR_OD11;
|
||||
GPIOC->ODR |= GPIO_ODR_OD10;
|
||||
|
||||
flash_record = load_params();
|
||||
if(flash_record[firmw].value == UPDATE_FLAG) {
|
||||
fw_update = true;
|
||||
for(int i = 0; i < 5;i++){
|
||||
GPIOC->ODR ^= GPIO_ODR_OD10; // Indecate message
|
||||
HAL_Delay(100);
|
||||
}
|
||||
// write_param(firmw,0); //reset flasg
|
||||
erase_flash_pages();
|
||||
}
|
||||
else{
|
||||
// for st-link update, because he doesnt reset flag_update
|
||||
if(is_app_valid()) jump_to_app(); //firmware exist
|
||||
else fw_update = true; //firmware doesnt exist, but we in bootloader
|
||||
}
|
||||
|
||||
|
||||
CAN_FilterTypeDef can_filter = {
|
||||
.FilterBank = 14, // Bank 14-27 for CAN2
|
||||
.FilterMode = CAN_FILTERMODE_IDMASK,
|
||||
.FilterScale = CAN_FILTERSCALE_32BIT,
|
||||
.FilterIdHigh = 0x0000,
|
||||
.FilterIdLow = 0x0000,
|
||||
.FilterMaskIdHigh = 0x0000,
|
||||
.FilterMaskIdLow = 0x0000,
|
||||
.FilterFIFOAssignment = CAN_RX_FIFO0,
|
||||
.FilterActivation = ENABLE,
|
||||
.SlaveStartFilterBank = 14
|
||||
};
|
||||
|
||||
if (HAL_CAN_ConfigFilter(&hcan2, &can_filter) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
if (HAL_CAN_Start(&hcan2) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
GPIOC->ODR |= GPIO_ODR_OD10;
|
||||
|
||||
// CAN_TxHeaderTypeDef test_header = {
|
||||
// .StdId = 0x123,
|
||||
// .IDE = CAN_ID_STD,
|
||||
// .RTR = CAN_RTR_DATA,
|
||||
// .DLC = 1,
|
||||
// .TransmitGlobalTime = DISABLE
|
||||
// };
|
||||
// uint8_t test_data = 0x55;
|
||||
// uint32_t tx_mailbox;
|
||||
// HAL_CAN_AddTxMessage(&hcan2, &test_header, &test_data, &tx_mailbox);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1) {
|
||||
if(fw_update) {
|
||||
CAN_RxHeaderTypeDef rx_header;
|
||||
uint8_t rx_data[8];
|
||||
HAL_StatusTypeDef status;
|
||||
if (fw_update) {
|
||||
uint32_t rf0r = CAN2->RF0R;
|
||||
|
||||
// Check message
|
||||
if(HAL_CAN_GetRxFifoFillLevel(&hcan2, CAN_RX_FIFO0) > 0) {
|
||||
status = HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO0, &rx_header, rx_data);
|
||||
// Check count of FIFO
|
||||
if ((rf0r & CAN_RF0R_FMP0) > 0) {
|
||||
CAN_RxHeaderTypeDef rx_header;
|
||||
uint8_t rx_data[8];
|
||||
|
||||
if(status == HAL_OK) {
|
||||
// check message IDE standart
|
||||
if(rx_header.IDE == CAN_ID_STD) {
|
||||
process_can_message(&rx_header, rx_data);
|
||||
|
||||
if (HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO0, &rx_header, rx_data) == HAL_OK) {
|
||||
process_can_message(&rx_header, rx_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
HAL_Delay(1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
|
@ -285,9 +329,6 @@ int main(void)
|
|||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue