cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U0_OTA mode

sreedharan1196
Associate II
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<stdint.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #define GETCHAR_PROTOTYPE int __io_getchar(void) #define FLASH_PART1 0x08000000 #define FLASH_PART2 0x08020000 #define BUFFER_SIZE 15354 char rx_data[BUFFER_SIZE]; typedef void (*pFunction)(void); int fileSize = 0; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart1; UART_HandleTypeDef huart3; /* USER CODE BEGIN PV */ PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } GETCHAR_PROTOTYPE { uint8_t ch = 0; __HAL_UART_CLEAR_OREFLAG(&huart3); HAL_UART_Receive(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); static void MX_USART3_UART_Init(void); static void MX_FLASH_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void Enable_Sections(void) { printf("FP N-Channel Enabled.....\r\n"); HAL_GPIO_WritePin(GPIOD, FP_NCH_Pin,GPIO_PIN_SET); HAL_Delay(1000); printf("Mic P-Channel Enabled.....\r\n"); HAL_GPIO_WritePin(GPIOD, MIC_PCH_Pin ,GPIO_PIN_SET); HAL_Delay(1000); printf("Mic N-Channel Enbaled....\r\n"); HAL_GPIO_WritePin(GPIOD, MIC_NCH_Pin,GPIO_PIN_SET); HAL_Delay(1000); printf("Power key enabled.....\r\n"); HAL_GPIO_WritePin(GPIOD, PWR_KEY_Pin,GPIO_PIN_SET); HAL_Delay(1000); printf("SIM Enbaled....\r\n"); HAL_GPIO_WritePin(GPIOD, SIM_EN_Pin, GPIO_PIN_SET); HAL_Delay(1000); printf("Power key enabled.....\r\n"); HAL_GPIO_WritePin(GPIOD, PWR_KEY_Pin,GPIO_PIN_RESET); HAL_Delay(1000); printf("Initializing HTTP connection...\r\n"); HAL_Delay(10000); } void Disable_Sections() { printf("FP N-Channel disabled.....\r\n"); HAL_GPIO_WritePin(GPIOD, FP_NCH_Pin,GPIO_PIN_RESET); HAL_Delay(2000); printf("Mic P-Channel disabled.....\r\n"); HAL_GPIO_WritePin(GPIOD, MIC_PCH_Pin ,GPIO_PIN_RESET); HAL_Delay(500); printf("Mic N-Channel disabled....\r\n"); HAL_GPIO_WritePin(GPIOD, MIC_NCH_Pin,GPIO_PIN_RESET); HAL_Delay(2000); printf("SIM disabaled....\r\n"); HAL_GPIO_WritePin(GPIOD, SIM_EN_Pin, GPIO_PIN_RESET); HAL_Delay(500); } void Clear_UART_Buffer(UART_HandleTypeDef *huart) { __HAL_UART_FLUSH_DRREGISTER(huart); while(__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE)) { (void)(huart->Instance->RDR & (uint8_t)0x00FF); } __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); __HAL_UART_CLEAR_OREFLAG(huart); } void parseCmd(char *command, int delay, char *rx_data) { char rxdata[BUFFER_SIZE]; memset(rxdata, 0, sizeof(rxdata)); HAL_UART_Transmit(&huart1, (uint8_t*)command, strlen(command), delay); HAL_UART_Receive(&huart1, (uint8_t*)rxdata, BUFFER_SIZE, delay); memcpy(rx_data, rxdata, sizeof(rxdata)); HAL_UART_Transmit(&huart3, (uint8_t*)rxdata, sizeof(rxdata), 1000); Clear_UART_Buffer(&huart1); Clear_UART_Buffer(&huart3); } uint32_t WriteToFlash(uint32_t startAddress, uint8_t *data, uint32_t dataSize) { printf("\n\nWriting to Flash...\r\n"); uint32_t newAddress = startAddress; if(startAddress == FLASH_PART2 ) { printf("\n\nEnter\n\n"); data +=10; // Skip the first 10 bytes dataSize -=10; // Reduce the size by 10 bytes } else { printf("\n\nnew......\n\n"); data += 3; dataSize -= 3; } // Ensure the starting address is aligned to 8 bytes if (startAddress % 8 != 0) { startAddress += 8 - (startAddress % 8); data += 8 - (startAddress % 8); dataSize -= 8 - (startAddress % 8); } HAL_FLASH_Unlock(); printf("Flash unlocked.\r\n"); HAL_Delay(100); // Program the user Flash area double word by double word for (uint32_t i = 0; i < dataSize; i += 8) { uint64_t dataToWrite = 0; // Copy 8 bytes of data or less if at the end of data if (i + 8 <= dataSize) { memcpy(&dataToWrite, data + i, 8); } else { memcpy(&dataToWrite, data + i, dataSize - i); } if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, startAddress + i, dataToWrite) != HAL_OK) { printf("Flash programming failed at address 0x%08X\r\n", startAddress + i); HAL_FLASH_Lock(); return newAddress; } else { printf("Data written to address 0x%08X\r\n", startAddress + i); newAddress = startAddress + i; } uint64_t readBackData = *(volatile uint64_t *)(startAddress + i); if (readBackData != dataToWrite) { printf("Flash verification failed at address 0x%08X. Expected: 0x%016llX, Found: 0x%016llX\r\n", startAddress + i, dataToWrite, readBackData); HAL_FLASH_Lock(); return; } } HAL_FLASH_Lock(); printf("Flash locked.\r\n"); printf("Writing to Flash completed successfully.\r\n"); return newAddress; } void DownloadFile(void) { printf("Entering Download mode...\r\n"); HAL_Delay(5000); char ATcommand[512]; const char *host = "104.237.9.39"; char DeviceID[] = "Zenner"; char bin[256]; snprintf(bin, sizeof(bin), "test/%s.bin", DeviceID); const int port = 80; printf("The bin : %s\r\n", bin); parseCmd("AT\r\n", 2000, rx_data); parseCmd("ATE0\r\n", 2000, rx_data); snprintf(ATcommand, sizeof(ATcommand), "AT+QHTTPURL=%d,80\r\n", snprintf(NULL, 0, "http://%s:%d/%s", host, port, bin)); parseCmd(ATcommand, 10000, rx_data); snprintf(ATcommand, sizeof(ATcommand), "http://%s:%d/%s\r\n", host, port, bin); parseCmd(ATcommand, 10000, rx_data); parseCmd("AT+QHTTPGET=60\r\n", 10000, rx_data); uint32_t fileSize = 0; char *sizeStart = strstr(rx_data, "+QHTTPGET: 0,200,"); if (sizeStart) { sizeStart += strlen("+QHTTPGET: 0,200,"); if (sscanf(sizeStart, "%u", &fileSize) == 1) { printf("File size: %u bytes\r\n", fileSize); } else { printf("Failed to extract file size. Response: %s\r\n", rx_data); return; } } else { printf("Failed to find file size in response.\r\n"); return; } uint32_t flashAddress = FLASH_PART2; FLASH_EraseInitTypeDef EraseInitStruct; uint32_t PageError = 0; EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.Page = (flashAddress - FLASH_BASE) / FLASH_PAGE_SIZE; EraseInitStruct.NbPages = (fileSize + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE; if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) { printf("Flash erase failed at page %lu. Error code: %lu\r\n", EraseInitStruct.Page, PageError); HAL_FLASH_Lock(); return; } else { printf("Flash erase completed.\r\n"); } uint32_t remainingSize = fileSize; uint32_t new_data = 1; while (remainingSize > 0) { printf("\n\n\n"); uint32_t chunkSize = (remainingSize > BUFFER_SIZE) ? BUFFER_SIZE : remainingSize; snprintf(ATcommand, sizeof(ATcommand), "AT+QHTTPGETEX=60,%u,%u\r\n",new_data,chunkSize); parseCmd(ATcommand, 10000, rx_data); memset(rx_data, 0, sizeof(rx_data)); parseCmd("AT+QHTTPREAD=80\r\n",10000,rx_data); printf("\n\n\n"); new_data = new_data + chunkSize-11; remainingSize -= chunkSize; if (strstr(rx_data, "+QHTTPREAD: 0") != NULL) { printf("\n\nDownload completed or no more data.\r\n"); break; } else if (strstr(rx_data, "ERROR") != NULL) { printf("\n\nError on downloading the message.\r\n"); break; } uint32_t newAddress = WriteToFlash(flashAddress,rx_data,chunkSize); flashAddress = newAddress + 1; memset(rx_data, 0, sizeof(rx_data)); printf("Flash New address : 0x%08X\r\n", newAddress); printf("Flash address : 0x%08X\r\n", flashAddress); } } void jumpToApplication(uint32_t address) { printf("Entered jump...\r\n"); printf("Jumping to address: 0x%08X\r\n", address); typedef void (*pFunction)(void); __disable_irq(); pFunction jumpToApp; SCB->VTOR = address; uint32_t stackPointer = *(volatile uint32_t *)address; printf("Setting stack pointer to: 0x%08X\r\n", stackPointer); // Set stack pointer __set_MSP(*(volatile uint32_t *)address); void (*appResetHandler)(void) = (void (*)(void))(*(volatile uint32_t*)(address + 4)); // Get application entry point jumpToApp = (pFunction)(*(volatile uint32_t *)(address + 4)); printf("Application entry point: 0x%08X\r\n", (uint32_t)jumpToApp); appResetHandler(); printf("Enter to deint....\r\n"); HAL_DeInit(); // Jump to application jumpToApp(); } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); MX_FLASH_Init(); /* USER CODE BEGIN 2 */ printf("Welcome to stm32.....\r\n"); HAL_Delay(2000); Enable_Sections(); DownloadFile(); HAL_Delay(2000); Disable_Sections(); jumpToApplication(FLASH_PART2); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } } /** * @brief FLASH Initialization Function * @PAram None * @retval None */ static void MX_FLASH_Init(void) { /* USER CODE BEGIN FLASH_Init 0 */ /* USER CODE END FLASH_Init 0 */ /* USER CODE BEGIN FLASH_Init 1 */ /* USER CODE END FLASH_Init 1 */ if (HAL_FLASH_Unlock() != HAL_OK) { Error_Handler(); } if (HAL_FLASH_Lock() != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN FLASH_Init 2 */ /* USER CODE END FLASH_Init 2 */ } /** * @brief USART1 Initialization Function * @PAram None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief USART3 Initialization Function * @PAram None * @retval None */ static void MX_USART3_UART_Init(void) { /* USER CODE BEGIN USART3_Init 0 */ /* USER CODE END USART3_Init 0 */ /* USER CODE BEGIN USART3_Init 1 */ /* USER CODE END USART3_Init 1 */ huart3.Instance = USART3; huart3.Init.BaudRate = 115200; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; huart3.Init.Mode = UART_MODE_TX_RX; huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart3.Init.OverSampling = UART_OVERSAMPLING_16; huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1; huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart3) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART3_Init 2 */ /* USER CODE END USART3_Init 2 */ } /** * @brief GPIO Initialization Function * @PAram None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOD, PWR_KEY_Pin|MIC_PCH_Pin|SIM_EN_Pin|MIC_NCH_Pin |FP_NCH_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : PWR_KEY_Pin MIC_PCH_Pin SIM_EN_Pin MIC_NCH_Pin FP_NCH_Pin */ GPIO_InitStruct.Pin = PWR_KEY_Pin|MIC_PCH_Pin|SIM_EN_Pin|MIC_NCH_Pin |FP_NCH_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @PAram file: pointer to the source file name * @PAram line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */
View more
1 REPLY 1
sreedharan1196
Associate II

i am working on ota mode like stm32 with ec200 module, inneed to run the ota mode on stm32u083mct6 by partition the flash memory when i try to run the another firmware on partition 2 by jumping to it the firmware in partition 2 is not running when i try to reset jump parttion 1 its running , what is ths issues i am facing her