2022-05-09 11:54 AM
2022-05-09 06:21 PM
What STM32 are you using?
What's in debug/production mode? The ESP32 ?
Perhaps you can output some diagnostic/telemetry detail via a serial port to some terminal application so you can understand what is happening, and debug things that way.
Using the STM32 System Loader via USART?
Gets stuck where, can you break with a debugger?
Present the problem with more thought and clarity, it might help to get your own understanding of the problem to a point you can solve it. And give the rest of us something to swing at..
2022-05-09 07:50 PM
Hello,
I am using STM32F407VET6. My requirement is that i have to do the ota update of the stm application code. For that i am using esp32 to connect with mobile phone.
I am using custom bootloader located at 0x08004000 and 0x08008000 (32k size).
When i put breakpoints and check the code works without any problem. But when i run the code normally ie without any breakpoints (indipendent)
The code is hanging. I am using crc 16 protocol to send data from mobile phone to esp32.
The recieved data ll be sent to stm32f using UART and ll be Stored in an array.
When i receive 128 block of 128bits I'll be writing the recieved data into the application space that starts from 0x0800C000.
2022-05-09 10:58 PM
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "internal_flash.h"
#include "lcd.h"
typedef void (*ptrF)(uint32_t dlyticks);
typedef void (*pFunction)(void);
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
#define FLASH_APP_ADDR 0x800C000
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);
int _write(int file, char *ptr, int len)
{
int DataIdx;
for(DataIdx = 0; DataIdx < len; DataIdx++)
{
HAL_UART_Transmit(&huart2, (uint8_t *)ptr++, 1, 100);
}
return len;
}
char bootloader_start[] = "BOOTLOADER Start";
char app_start[] = "APP Start";
char no_app_found[] = "No APP Found";
int erase_flag = 0;
int calcrc(char *ptr, int count)
{
int crc;
char i;
crc = 0;
while (--count >= 0)
{
crc = crc ^ (int) *ptr++ << 8;
i = 8;
do
{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
} while(--i);
}
return (crc);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
uint8_t nack = 0x15;
uint8_t ack = 0x06;
uint8_t eot = 0x04;
uint8_t soh = 0x01;
uint8_t c = 0x43;
uint8_t program_block_size = 132;
uint8_t program_block[132];
uint8_t check_crc[128];
uint16_t retrieve_crc;
uint16_t retrieve_calcrc;
uint8_t header = 0x00u;
long block_count = 0;
int block_flag = 0;
long i = 0;
char program_rec[65536];
int numofwords = 0;
int program_block_flag = 0;
int program_block_error_count = 0;
int program_c_flag = 0;
HAL_UART_Transmit(&huart3, &c, 1, 1000);
memset(program_block, '\0', sizeof(program_block));
memset(program_rec, '\0', sizeof(program_rec));
while(header != eot)
{
memset(program_block, '\0', sizeof(program_block));
HAL_UART_Receive(&huart3, (uint8_t*)&header, 1u, 3000);
HAL_UART_Receive(&huart3, (uint8_t*)&program_block, program_block_size, 3000);
program_c_flag = 1;
for(i = 0; i < 128; i++)
{
check_crc[i] = program_block[i + 2];
}
retrieve_crc = program_block[130] << 8;
retrieve_crc = retrieve_crc | program_block[131];
retrieve_calcrc = calcrc((char*)check_crc, 128) & 0xFFFF;
if(retrieve_calcrc == retrieve_crc)
{
program_block_flag = 1;
}
else
{
program_block_flag = 0;
}
if((header != soh) && (header!= eot) && (program_c_flag == 1))
{
program_block_flag = 0;
}
program_block_flag = 1;
if(program_block_flag == 1)
{
program_block_error_count = 0;
if(header!= eot)
{
for(i = 0; i < 128; i++)
{
program_rec[i + block_count] = program_block[i + 2];
}
block_count += 128;
}
else{}
if((block_flag == 0) && ((block_count >= 16384) || (header == eot)))
{
numofwords = 16384 / 4;
erase_flag = 0;
Flash_Write_Data((0x0800C000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 1;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 1) && ((block_count >= 65536) || (header == eot)))
{
numofwords = 65536 / 4;
erase_flag = 0;
Flash_Write_Data((0x08010000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 2;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 2) && ((block_count >= 65536) || (header == eot)))
{
numofwords = 65536 / 4;
erase_flag = 0;
Flash_Write_Data((0x08020000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 3;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 3) && ((block_count >= 65536) || (header == eot)))
{
erase_flag = 1;
numofwords = 65536 / 4;
Flash_Write_Data((0x08030000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 4;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 4) && ((block_count >= 65536) || (header == eot)))
{
erase_flag = 0;
numofwords = 65536 / 4;
Flash_Write_Data((0x08040000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 5;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 5) && ((block_count >= 65536) || (header == eot)))
{
erase_flag = 1;
numofwords = 65536 / 4;
Flash_Write_Data((0x08050000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 6;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 6) && ((block_count >= 65536) || (header == eot)))
{
erase_flag = 0;
numofwords = 65536 / 4;
Flash_Write_Data((0x08060000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 7;
memset(program_rec, '\0', sizeof(program_rec));
}
else if((block_flag == 7) && ((block_count >= 65536) || (header == eot)))
{
erase_flag = 1;
numofwords = 65536 / 4;
Flash_Write_Data((0x08070000), (uint32_t *)program_rec, numofwords);
block_count = 0;
block_flag = 0;
memset(program_rec, '\0', sizeof(program_rec));
}
HAL_UART_Transmit(&huart3, &ack, 1, 100);
}
else
{
HAL_UART_Transmit(&huart3, &nack, 1, 100);
program_block_error_count++;
}
if(program_block_error_count == 3)
{
program_block_error_count = 0;
program_c_flag = 0;
}
}
HAL_NVIC_SystemReset();
}
2022-05-10 03:30 AM
hello,
I solved the hanging problem but got stuck in other problem.
in the above code posted the file is getting flashed but when i do system reset the application code is not running.
do i have to enable interupts beefore jumping to main application?