/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * *

© Copyright (c) 2019 STMicroelectronics. * All rights reserved.

* * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* 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 ---------------------------------------------------------*/ #define ApplicationAddress 0x8010000 #define VECT_TAB_OFFSET 0x10000 int i; /* USER CODE BEGIN PV */ uint8_t msg[7]={0xFF,0xFF,0x00,0x00,0x00,0x00,0x0D}; uint8_t NACK[1]={0x79}; int rf=0; int row=0; int data_count=0; uint32_t temp[10000]; uint32_t FlashData1[10000]; uint8_t FlashData[16]; uint8_t com[7]={0xFF,0xFF,0x00,0x00,0x00,0x01,0x0D}; uint8_t com1[7]={0xFF,0xFF,0x00,0x00,0x00,0x02,0x0D}; uint8_t start[7]={0xFF,0xFF,0x00,0x00,0x00,0x03,0x0D}; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void UserAppStart(void); void writeFlashTest(void); void printFlashTest(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* 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_USART2_UART_Init(); MX_UART4_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /*------Zigbee setup-----------------------------------------------*/ HAL_UART_Transmit(&huart4,(uint8_t*)start,sizeof(start),10); HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET); /*-----------------------------------------------------*/ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ if(rf==0) { HAL_UART_Receive_IT(&huart4, (uint8_t *)msg,sizeof(msg)); } else { HAL_UART_Receive_IT(&huart4, (uint8_t *)FlashData,sizeof(FlashData)); } //HAL_UART_Receive_IT(&huart4, (uint8_t *)msg,sizeof(msg)); /* 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_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 72; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 3; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == UART4) { if(rf==1) { //printf("row:%d\r\n",row); for(i=0;i<4;i++) { FlashData1[(4*row)+i]=(FlashData[4*i]<<24)+(FlashData[1+(4*i)]<<16)+(FlashData[2+(4*i)]<<8)+FlashData[3+(4*i)]; //printf("%x\r\n",FlashData1[(4*row)+i]); } if(row==(data_count-1)) { writeFlashTest(); printFlashTest(); UserAppStart(); rf=0; data_count=0; row=0; } row=row+1; } else if(rf==0) { if(msg[0]==0xFF && msg[1]==0x01 && msg[6]==0x0D) { HAL_UART_Transmit(&huart4, (uint8_t *)NACK,1,0xFFFF); rf=1; data_count=msg[2]*100+msg[3]; } if(msg[0]==0xFF && msg[1]==0x02 && msg[6]==0x0D) { HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET); HAL_UART_Transmit(&huart4,(uint8_t*)com,sizeof(com),10); } if(msg[0]==0xFF && msg[1]==0x03 && msg[6]==0x0D) { HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_RESET); HAL_UART_Transmit(&huart4,(uint8_t*)com1,sizeof(com1),10); } } } } void UserAppStart(void) { typedef void (*pFunction)(void); pFunction Jump_To_Application; uint32_t JumpAddress; if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000) { /* Jump to user application */ JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4); Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) ApplicationAddress); Jump_To_Application(); } } void writeFlashTest(void) { FLASH_EraseInitTypeDef pEraseInit; HAL_FLASH_Unlock(); pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS; pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3; pEraseInit.Sector = FLASH_SECTOR_4; pEraseInit.NbSectors = 1; uint32_t PageError=0; HAL_FLASHEx_Erase(&pEraseInit,&PageError); for(i=0;i<(data_count*4);i++) { HAL_FLASH_Program(TYPEPROGRAM_WORD,ApplicationAddress+(i*4),FlashData1[i]); } HAL_FLASH_Lock(); } void printFlashTest(void) { for(i=0;i<(data_count*4);i++) { temp[i] = *(__IO uint32_t*)(ApplicationAddress+(i*4)); //printf("addr:0x%x, data:0x%x\r\n",ApplicationAddress+(i*4), temp[i]); //printf("%x\r\n", temp[i]); } } /* 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 */ /* 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, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/