cancel
Showing results for 
Search instead for 
Did you mean: 

firmware download over UART

ortegar23
Associate

I am using STMF103CBT6 the problem is nothing gets written to the specified address                                                           

void SIM800C_HTTP_GetAndWrite(char *url, uint32_t flashAddress) {

SIM800C_SendCommand("AT+HTTPINIT\r\n");

HAL_Delay(500);

SIM800C_SendCommand("AT+HTTPPARA=\"CID\",1\r\n");

HAL_Delay(500);



char httpParaUrl[100];

sprintf(httpParaUrl, "AT+HTTPPARA=\"URL\",\"%s\"\r\n", url);

SIM800C_SendCommand(httpParaUrl);

HAL_Delay(500);



SIM800C_SendCommand("AT+HTTPACTION=0\r\n"); // Start GET

HAL_Delay(5000);



// Read the +HTTPACTION result

char actionResponse[64] = {0};

HAL_UART_Receive(&huart1, (uint8_t *)actionResponse, sizeof(actionResponse), 2000);



// Extract data length

int dataLen = 0;

sscanf(actionResponse, "+HTTPACTION: 0,200,%d", &dataLen);



if (dataLen <= 0 || dataLen > MAX_FILE_SIZE) {

Error_Handler(); // Invalid size

}



HAL_FLASH_Unlock();



for (uint32_t offset = 0; offset < dataLen; offset += CHUNK_SIZE) {

int thisChunkSize = (dataLen - offset > CHUNK_SIZE) ? CHUNK_SIZE : (dataLen - offset);



char readCmd[32];

sprintf(readCmd, "AT+HTTPREAD=%lu,%d\r\n", offset, thisChunkSize);

SIM800C_SendCommand(readCmd);

HAL_Delay(1000);



memset(chunkBuffer, 0xFF, sizeof(chunkBuffer));

HAL_UART_Receive(&huart1, chunkBuffer, thisChunkSize, HAL_MAX_DELAY);



// Write chunk to flash

for (int i = 0; i < thisChunkSize; i += 4) {

uint32_t word = 0xFFFFFFFF;

memcpy(&word, chunkBuffer + i, (thisChunkSize - i >= 4) ? 4 : (thisChunkSize - i));

if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flashAddress + offset + i, word) != HAL_OK) {

Error_Handler();

}

}

}



HAL_FLASH_Lock();



SIM800C_SendCommand("AT+HTTPTERM\r\n");

}
4 REPLIES 4
mƎALLEm
ST Employee

Hello @ortegar23 and welcome to the ST community,

Please review how to post a thread in this community

Thank you.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
MM..1
Chief III
uint32_t word = 0xFFFFFFFF;

memcpy(&word, chunkBuffer + i, (thisChunkSize - i >= 4) ? 4 : (thisChunkSize - i));

this maybe dont work. word in compiler is placed into reg no mem , then memcpy ... and try ask better we dont see error result or question ...

For this type of unique set up, expect to have to instrument and debug your own work.

Understand what's happening. 

Don't expect single stepping to work helpfully. 

Memory must be erased, you get one chance to write. Address must be within Flash and at word aligned address.

Test by writing into RAM or dumping to console. Test Flash writing code independently. 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Super User

There's a lot in that code about SIM800, HTTP, etc.

Get rid of such extra complications - just focus on getting the Flash writes working.

Once that's thoroughly tested - and only then - move on adding the comms stuff.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.