Question
firmware download over UART
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");
}