From: kyz Date: Mon, 4 Aug 2025 10:24:20 +0000 (+0800) Subject: add udp/spiflash/bootloader X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=91d680365c95f9b591e88461dd70c74468d28c65;p=yn%2Falarmmonitor_mcu.git add udp/spiflash/bootloader --- diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 386c51b..6847dbb 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -80,12 +80,20 @@ void Error_Handler(void); #define PD15_LORA_M1_GPIO_Port GPIOD #define PC8_LORA_AUX_Pin GPIO_PIN_8 #define PC8_LORA_AUX_GPIO_Port GPIOC +#define PA11_RS485_DIR1_Pin GPIO_PIN_11 +#define PA11_RS485_DIR1_GPIO_Port GPIOA +#define PA12_CTL_POWER_Pin GPIO_PIN_12 +#define PA12_CTL_POWER_GPIO_Port GPIOA #define LORA_RST_Pin GPIO_PIN_4 #define LORA_RST_GPIO_Port GPIOD #define DM222_PAPWR_EN_Pin GPIO_PIN_7 #define DM222_PAPWR_EN_GPIO_Port GPIOD #define FAN_EN_Pin GPIO_PIN_5 #define FAN_EN_GPIO_Port GPIOB +#define PB6_EN_BEIDOU_PWN_Pin GPIO_PIN_6 +#define PB6_EN_BEIDOU_PWN_GPIO_Port GPIOB +#define PB9_SPI2_NSS_Pin GPIO_PIN_9 +#define PB9_SPI2_NSS_GPIO_Port GPIOB #define SYS_LED_Pin GPIO_PIN_1 #define SYS_LED_GPIO_Port GPIOE diff --git a/Core/Inc/stm32f4xx_hal_conf.h b/Core/Inc/stm32f4xx_hal_conf.h index 4d0e728..a44b8a1 100644 --- a/Core/Inc/stm32f4xx_hal_conf.h +++ b/Core/Inc/stm32f4xx_hal_conf.h @@ -62,7 +62,7 @@ /* #define HAL_SAI_MODULE_ENABLED */ /* #define HAL_SD_MODULE_ENABLED */ /* #define HAL_MMC_MODULE_ENABLED */ -/* #define HAL_SPI_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED /* #define HAL_USART_MODULE_ENABLED */ diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index 7f3391c..7d02dc6 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -54,6 +54,7 @@ void UsageFault_Handler(void); void DebugMon_Handler(void); void EXTI3_IRQHandler(void); void TIM1_UP_TIM10_IRQHandler(void); +void SPI2_IRQHandler(void); void USART1_IRQHandler(void); void USART2_IRQHandler(void); void USART3_IRQHandler(void); diff --git a/Core/Src/main.c b/Core/Src/main.c index daa2dfb..fd89c48 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -47,6 +47,8 @@ DMA_HandleTypeDef hdma_adc1; I2C_HandleTypeDef hi2c3; +SPI_HandleTypeDef hspi2; + TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim3; @@ -82,6 +84,7 @@ static void MX_USART3_UART_Init(void); static void MX_USART6_UART_Init(void); static void MX_TIM2_Init(void); static void MX_TIM3_Init(void); +static void MX_SPI2_Init(void); void main_task(void *argument); /* USER CODE BEGIN PFP */ @@ -132,6 +135,7 @@ int main(void) MX_USART6_UART_Init(); MX_TIM2_Init(); MX_TIM3_Init(); + MX_SPI2_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ @@ -313,6 +317,44 @@ static void MX_I2C3_Init(void) } +/** + * @brief SPI2 Initialization Function + * @param None + * @retval None + */ +static void MX_SPI2_Init(void) +{ + + /* USER CODE BEGIN SPI2_Init 0 */ + + /* USER CODE END SPI2_Init 0 */ + + /* USER CODE BEGIN SPI2_Init 1 */ + + /* USER CODE END SPI2_Init 1 */ + /* SPI2 parameter configuration*/ + hspi2.Instance = SPI2; + hspi2.Init.Mode = SPI_MODE_MASTER; + hspi2.Init.Direction = SPI_DIRECTION_2LINES; + hspi2.Init.DataSize = SPI_DATASIZE_8BIT; + hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi2.Init.NSS = SPI_NSS_SOFT; + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; + hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi2.Init.TIMode = SPI_TIMODE_DISABLE; + hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi2.Init.CRCPolynomial = 10; + if (HAL_SPI_Init(&hspi2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN SPI2_Init 2 */ + + /* USER CODE END SPI2_Init 2 */ + +} + /** * @brief TIM2 Initialization Function * @param None @@ -485,7 +527,7 @@ static void MX_USART1_UART_Init(void) /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; - huart1.Init.BaudRate = 1000000; + huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; @@ -649,7 +691,10 @@ static void MX_GPIO_Init(void) HAL_GPIO_WritePin(GPIOD, LAKI_RESET_Pin|PD15_LORA_M1_Pin|LORA_RST_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(FAN_EN_GPIO_Port, FAN_EN_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOA, PA11_RS485_DIR1_Pin|PA12_CTL_POWER_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, FAN_EN_Pin|PB6_EN_BEIDOU_PWN_Pin|PB9_SPI2_NSS_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(SYS_LED_GPIO_Port, SYS_LED_Pin, GPIO_PIN_RESET); @@ -660,12 +705,12 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(DQ_IN_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pin : PA4_BEIDOU_RST_EN_Pin */ - GPIO_InitStruct.Pin = PA4_BEIDOU_RST_EN_Pin; + /*Configure GPIO pins : PA4_BEIDOU_RST_EN_Pin PA11_RS485_DIR1_Pin PA12_CTL_POWER_Pin */ + GPIO_InitStruct.Pin = PA4_BEIDOU_RST_EN_Pin|PA11_RS485_DIR1_Pin|PA12_CTL_POWER_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(PA4_BEIDOU_RST_EN_GPIO_Port, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pin : PB14_RMII_INT_Pin */ GPIO_InitStruct.Pin = PB14_RMII_INT_Pin; @@ -673,8 +718,8 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(PB14_RMII_INT_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pins : PB15_ETH_RESET_Pin FAN_EN_Pin */ - GPIO_InitStruct.Pin = PB15_ETH_RESET_Pin|FAN_EN_Pin; + /*Configure GPIO pins : PB15_ETH_RESET_Pin FAN_EN_Pin PB6_EN_BEIDOU_PWN_Pin PB9_SPI2_NSS_Pin */ + GPIO_InitStruct.Pin = PB15_ETH_RESET_Pin|FAN_EN_Pin|PB6_EN_BEIDOU_PWN_Pin|PB9_SPI2_NSS_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; diff --git a/Core/Src/stm32f4xx_hal_msp.c b/Core/Src/stm32f4xx_hal_msp.c index 1c9d975..90865a0 100644 --- a/Core/Src/stm32f4xx_hal_msp.c +++ b/Core/Src/stm32f4xx_hal_msp.c @@ -239,6 +239,88 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c) } +/** +* @brief SPI MSP Initialization +* This function configures the hardware resources used in this example +* @param hspi: SPI handle pointer +* @retval None +*/ +void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(hspi->Instance==SPI2) + { + /* USER CODE BEGIN SPI2_MspInit 0 */ + + /* USER CODE END SPI2_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_SPI2_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**SPI2 GPIO Configuration + PC2 ------> SPI2_MISO + PC3 ------> SPI2_MOSI + PB10 ------> SPI2_SCK + */ + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* SPI2 interrupt Init */ + HAL_NVIC_SetPriority(SPI2_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(SPI2_IRQn); + /* USER CODE BEGIN SPI2_MspInit 1 */ + + /* USER CODE END SPI2_MspInit 1 */ + } + +} + +/** +* @brief SPI MSP De-Initialization +* This function freeze the hardware resources used in this example +* @param hspi: SPI handle pointer +* @retval None +*/ +void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) +{ + if(hspi->Instance==SPI2) + { + /* USER CODE BEGIN SPI2_MspDeInit 0 */ + + /* USER CODE END SPI2_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_SPI2_CLK_DISABLE(); + + /**SPI2 GPIO Configuration + PC2 ------> SPI2_MISO + PC3 ------> SPI2_MOSI + PB10 ------> SPI2_SCK + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2|GPIO_PIN_3); + + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10); + + /* SPI2 interrupt DeInit */ + HAL_NVIC_DisableIRQ(SPI2_IRQn); + /* USER CODE BEGIN SPI2_MspDeInit 1 */ + + /* USER CODE END SPI2_MspDeInit 1 */ + } + +} + /** * @brief TIM_Base MSP Initialization * This function configures the hardware resources used in this example diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index 2c20109..a12bc82 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -57,6 +57,7 @@ /* External variables --------------------------------------------------------*/ extern ETH_HandleTypeDef heth; extern DMA_HandleTypeDef hdma_adc1; +extern SPI_HandleTypeDef hspi2; extern UART_HandleTypeDef huart4; extern UART_HandleTypeDef huart5; extern UART_HandleTypeDef huart1; @@ -195,6 +196,20 @@ void TIM1_UP_TIM10_IRQHandler(void) /* USER CODE END TIM1_UP_TIM10_IRQn 1 */ } +/** + * @brief This function handles SPI2 global interrupt. + */ +void SPI2_IRQHandler(void) +{ + /* USER CODE BEGIN SPI2_IRQn 0 */ + + /* USER CODE END SPI2_IRQn 0 */ + HAL_SPI_IRQHandler(&hspi2); + /* USER CODE BEGIN SPI2_IRQn 1 */ + + /* USER CODE END SPI2_IRQn 1 */ +} + /** * @brief This function handles USART1 global interrupt. */ diff --git a/Core/Src/system_stm32f4xx.c b/Core/Src/system_stm32f4xx.c index 3bd40f7..f443a29 100644 --- a/Core/Src/system_stm32f4xx.c +++ b/Core/Src/system_stm32f4xx.c @@ -105,7 +105,7 @@ #else #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. +#define VECT_TAB_OFFSET 0x00020000U /*!< Vector Table base offset field. This value must be a multiple of 0x200. */ #endif /* VECT_TAB_SRAM */ #endif /* USER_VECT_TAB_ADDRESS */ diff --git a/LWIP/Target/ethernetif.c b/LWIP/Target/ethernetif.c index 16a8e08..7acea8f 100644 --- a/LWIP/Target/ethernetif.c +++ b/LWIP/Target/ethernetif.c @@ -165,7 +165,7 @@ void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *handlerEth) } /* USER CODE BEGIN 4 */ - +extern void user_eth_mac_get( uint8_t *mac ); /* USER CODE END 4 */ /******************************************************************************* @@ -204,7 +204,7 @@ static void low_level_init(struct netif *netif) heth.Init.RxBuffLen = 1536; /* USER CODE BEGIN MACADDRESS */ - + user_eth_mac_get(MACAddr); /* USER CODE END MACADDRESS */ hal_eth_init_status = HAL_ETH_Init(&heth); diff --git a/MDK-ARM/bootloader.uvprojx b/MDK-ARM/bootloader.uvprojx new file mode 100644 index 0000000..cf02a94 --- /dev/null +++ b/MDK-ARM/bootloader.uvprojx @@ -0,0 +1,644 @@ + + + + 2.1 + +
### uVision Project, (C) Keil Software
+ + + + bootloader + 0x4 + ARM-ADS + 5060960::V5.06 update 7 (build 960)::.\ARMCC + 0 + + + STM32F407VETx + STMicroelectronics + Keil.STM32F4xx_DFP.2.16.0 + http://www.keil.com/pack/ + IRAM(0x20000000-0x2001BFFF) IRAM2(0x2001C000-0x2001FFFF) IROM(0x8000000-0x807FFFF) CLOCK(25000000) FPU2 CPUTYPE("Cortex-M4") TZ + + + + 0 + + + + + + + + + + + $$Device:STM32F407VETx$CMSIS\SVD\STM32F40x.svd + 0 + 0 + + + + + + + 0 + 0 + 0 + 0 + 1 + + bootloader\ + bootloader + 1 + 0 + 1 + 1 + 1 + + 1 + 0 + 0 + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 1 + 0 + fromelf --bin --output=$L@L.bin !L + + 0 + 0 + 0 + 0 + + 1 + + + + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 3 + + + 0 + + + SARMCM3.DLL + -REMAP -MPU + DCM.DLL + -pCM4 + SARMCM3.DLL + -MPU + TCM.DLL + -pCM4 + + + + 1 + 0 + 0 + 0 + 16 + + + + + 1 + 0 + 0 + 1 + 1 + 4101 + + 1 + BIN\UL2CM3.DLL + + + + + + 0 + + + + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 0 + 1 + 1 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + "Cortex-M4" + + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 2 + 0 + 0 + 1 + 0 + 8 + 1 + 0 + 0 + 0 + 3 + 4 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 1 + 0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x20000000 + 0x1c000 + + + 1 + 0x8000000 + 0x80000 + + + 0 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x8000000 + 0x8000 + + + 1 + 0x0 + 0x0 + + + 0 + 0x10000000 + 0x10000 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x20000000 + 0x1c000 + + + 0 + 0x2001c000 + 0x4000 + + + + + + 1 + 3 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 2 + 0 + 0 + 1 + 0 + 0 + 5 + 3 + 1 + 1 + 0 + 0 + 0 + + + USE_HAL_DRIVER,STM32F407xx,ARM_MATH_CM4,BOOTLOADER + + ../Core/Inc;../LWIP/App;../LWIP/Target;../Middlewares/Third_Party/LwIP/src/include;../Middlewares/Third_Party/LwIP/system;../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Middlewares/Third_Party/FreeRTOS/Source/include;../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2;../Middlewares/Third_Party/FreeRTOS/Source/portable/RVDS/ARM_CM4F;../Drivers/BSP/Components/lan8742;../Middlewares/Third_Party/LwIP/src/include/netif/ppp;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Middlewares/Third_Party/LwIP/src/include/lwip;../Middlewares/Third_Party/LwIP/src/include/lwip/apps;../Middlewares/Third_Party/LwIP/src/include/lwip/priv;../Middlewares/Third_Party/LwIP/src/include/lwip/prot;../Middlewares/Third_Party/LwIP/src/include/netif;../Middlewares/Third_Party/LwIP/src/include/compat/posix;../Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa;../Middlewares/Third_Party/LwIP/src/include/compat/posix/net;../Middlewares/Third_Party/LwIP/src/include/compat/posix/sys;../Middlewares/Third_Party/LwIP/src/include/compat/stdc;../Middlewares/Third_Party/LwIP/system/arch;../Drivers/CMSIS/Include;../USB_DEVICE/App;../USB_DEVICE/Target;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc;../user;../user_debug + + + + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + + + BOOTLOADER + + ../Core/Inc + + + + 1 + 0 + 0 + 0 + 1 + 0 + + + + + + + + + + + + + + + Application/MDK-ARM + + + startup_stm32f407xx.s + 2 + startup_stm32f407xx.s + + + + + Application/User/Core + + + freertos.c + 1 + ../Core/Src/freertos.c + + + stm32f4xx_it.c + 1 + ../Core/Src/stm32f4xx_it.c + + + stm32f4xx_hal_msp.c + 1 + ../Core/Src/stm32f4xx_hal_msp.c + + + stm32f4xx_hal_timebase_tim.c + 1 + ../Core/Src/stm32f4xx_hal_timebase_tim.c + + + + + Drivers/STM32F4xx_HAL_Driver + + + stm32f4xx_hal_rcc.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c + + + stm32f4xx_hal_rcc_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c + + + stm32f4xx_hal_flash.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c + + + stm32f4xx_hal_flash_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c + + + stm32f4xx_hal_flash_ramfunc.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c + + + stm32f4xx_hal_gpio.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c + + + stm32f4xx_hal_dma_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c + + + stm32f4xx_hal_dma.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c + + + stm32f4xx_hal_pwr.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c + + + stm32f4xx_hal_pwr_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c + + + stm32f4xx_hal_cortex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c + + + stm32f4xx_hal.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c + + + stm32f4xx_hal_exti.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c + + + stm32f4xx_hal_eth.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c + + + stm32f4xx_hal_adc.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c + + + stm32f4xx_hal_adc_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c + + + stm32f4xx_ll_adc.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c + + + stm32f4xx_hal_i2c.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c + + + stm32f4xx_hal_i2c_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c + + + stm32f4xx_hal_spi.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c + + + 2 + 0 + 0 + 0 + 0 + 1 + 2 + 2 + 2 + 2 + 11 + + + 1 + + + + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 0 + 0 + 2 + 2 + 2 + 2 + 2 + + + + + + + + + + + + stm32f4xx_hal_tim.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c + + + stm32f4xx_hal_tim_ex.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c + + + stm32f4xx_hal_uart.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c + + + + + Drivers/CMSIS + + + system_stm32f4xx.c + 1 + ../Core/Src/system_stm32f4xx.c + + + + + user + + + mbcrc.c + 1 + ..\user\mbcrc.c + + + user_nv.c + 1 + ..\user\user_nv.c + + + user_sflash.c + 1 + ..\user\user_sflash.c + + + bootloader_main.c + 1 + ..\user\bootloader_main.c + + + + + ::CMSIS + + + + + + + + + + + + + + + + + + +
diff --git a/MDK-ARM/mcu.uvprojx b/MDK-ARM/mcu.uvprojx index 199fd44..1b5ddf4 100644 --- a/MDK-ARM/mcu.uvprojx +++ b/MDK-ARM/mcu.uvprojx @@ -275,8 +275,8 @@ 1 - 0x8000000 - 0x80000 + 0x8020000 + 0x60000 1 @@ -337,7 +337,7 @@ 0 - USE_HAL_DRIVER,STM32F407xx,ARM_MATH_CM4 + USE_HAL_DRIVER,STM32F407xx,ARM_MATH_CM4,USER_VECT_TAB_ADDRESS ../Core/Inc;../LWIP/App;../LWIP/Target;../Middlewares/Third_Party/LwIP/src/include;../Middlewares/Third_Party/LwIP/system;../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Middlewares/Third_Party/FreeRTOS/Source/include;../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2;../Middlewares/Third_Party/FreeRTOS/Source/portable/RVDS/ARM_CM4F;../Drivers/BSP/Components/lan8742;../Middlewares/Third_Party/LwIP/src/include/netif/ppp;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Middlewares/Third_Party/LwIP/src/include/lwip;../Middlewares/Third_Party/LwIP/src/include/lwip/apps;../Middlewares/Third_Party/LwIP/src/include/lwip/priv;../Middlewares/Third_Party/LwIP/src/include/lwip/prot;../Middlewares/Third_Party/LwIP/src/include/netif;../Middlewares/Third_Party/LwIP/src/include/compat/posix;../Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa;../Middlewares/Third_Party/LwIP/src/include/compat/posix/net;../Middlewares/Third_Party/LwIP/src/include/compat/posix/sys;../Middlewares/Third_Party/LwIP/src/include/compat/stdc;../Middlewares/Third_Party/LwIP/system/arch;../Drivers/CMSIS/Include;../USB_DEVICE/App;../USB_DEVICE/Target;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc;../user;../user_debug @@ -604,6 +604,62 @@ 1 ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c + + stm32f4xx_hal_spi.c + 1 + ../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c + + + 2 + 0 + 0 + 0 + 0 + 1 + 2 + 2 + 2 + 2 + 11 + + + 1 + + + + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 0 + 0 + 2 + 2 + 2 + 2 + 2 + + + + + + + + + + stm32f4xx_hal_tim.c 1 @@ -1149,6 +1205,21 @@ 1 ..\user\user_wind_sensor.c + + user_nv.c + 1 + ..\user\user_nv.c + + + user_sflash.c + 1 + ..\user\user_sflash.c + + + user_udp.c + 1 + ..\user\user_udp.c + diff --git a/MDK-ARM/startup_stm32f407xx.s b/MDK-ARM/startup_stm32f407xx.s index b0722a1..564303f 100644 --- a/MDK-ARM/startup_stm32f407xx.s +++ b/MDK-ARM/startup_stm32f407xx.s @@ -163,6 +163,22 @@ __Vectors_End __Vectors_Size EQU __Vectors_End - __Vectors + IMPORT SW_VER + IMPORT |Region$$Table$$Limit| + IMPORT |Region$$Table$$Base| + ;ALIGN 0x200 + SPACE 0x200 - __Vectors_Size ;ALIGN 0x200 + IF :DEF:BOOTLOADER + DCD 0x544F4F42 ; 'BOOT' + ELSE + DCD 0x00505041 ; 'APP\0' + ENDIF +SW_VER_ADDR + DCD SW_VER + DCD |Region$$Table$$Base| + DCD |Region$$Table$$Limit| + DCD __Vectors + SPACE 44 AREA |.text|, CODE, READONLY ; Reset handler diff --git a/mcu.ioc b/mcu.ioc index 2351b23..b9e28b0 100644 --- a/mcu.ioc +++ b/mcu.ioc @@ -52,76 +52,84 @@ Mcu.CPN=STM32F407VET6 Mcu.Family=STM32F4 Mcu.IP0=ADC1 Mcu.IP1=DMA -Mcu.IP10=TIM3 -Mcu.IP11=UART4 -Mcu.IP12=UART5 -Mcu.IP13=USART1 -Mcu.IP14=USART2 -Mcu.IP15=USART3 -Mcu.IP16=USART6 +Mcu.IP10=TIM2 +Mcu.IP11=TIM3 +Mcu.IP12=UART4 +Mcu.IP13=UART5 +Mcu.IP14=USART1 +Mcu.IP15=USART2 +Mcu.IP16=USART3 +Mcu.IP17=USART6 Mcu.IP2=ETH Mcu.IP3=FREERTOS Mcu.IP4=I2C3 Mcu.IP5=LWIP Mcu.IP6=NVIC Mcu.IP7=RCC -Mcu.IP8=SYS -Mcu.IP9=TIM2 -Mcu.IPNb=17 +Mcu.IP8=SPI2 +Mcu.IP9=SYS +Mcu.IPNb=18 Mcu.Name=STM32F407V(E-G)Tx Mcu.Package=LQFP100 Mcu.Pin0=PE3 Mcu.Pin1=PC14-OSC32_IN -Mcu.Pin10=PA6 -Mcu.Pin11=PA7 -Mcu.Pin12=PC4 -Mcu.Pin13=PC5 -Mcu.Pin14=PB11 -Mcu.Pin15=PB12 -Mcu.Pin16=PB13 -Mcu.Pin17=PB14 -Mcu.Pin18=PB15 -Mcu.Pin19=PD8 +Mcu.Pin10=PA4 +Mcu.Pin11=PA5 +Mcu.Pin12=PA6 +Mcu.Pin13=PA7 +Mcu.Pin14=PC4 +Mcu.Pin15=PC5 +Mcu.Pin16=PB10 +Mcu.Pin17=PB11 +Mcu.Pin18=PB12 +Mcu.Pin19=PB13 Mcu.Pin2=PC15-OSC32_OUT -Mcu.Pin20=PD9 -Mcu.Pin21=PD10 -Mcu.Pin22=PD11 -Mcu.Pin23=PD12 -Mcu.Pin24=PD13 -Mcu.Pin25=PD14 -Mcu.Pin26=PD15 -Mcu.Pin27=PC6 -Mcu.Pin28=PC7 -Mcu.Pin29=PC8 +Mcu.Pin20=PB14 +Mcu.Pin21=PB15 +Mcu.Pin22=PD8 +Mcu.Pin23=PD9 +Mcu.Pin24=PD10 +Mcu.Pin25=PD11 +Mcu.Pin26=PD12 +Mcu.Pin27=PD13 +Mcu.Pin28=PD14 +Mcu.Pin29=PD15 Mcu.Pin3=PH0-OSC_IN -Mcu.Pin30=PC9 -Mcu.Pin31=PA8 -Mcu.Pin32=PA9 -Mcu.Pin33=PA10 -Mcu.Pin34=PA13 -Mcu.Pin35=PA14 -Mcu.Pin36=PC10 -Mcu.Pin37=PC11 -Mcu.Pin38=PC12 -Mcu.Pin39=PD2 +Mcu.Pin30=PC6 +Mcu.Pin31=PC7 +Mcu.Pin32=PC8 +Mcu.Pin33=PC9 +Mcu.Pin34=PA8 +Mcu.Pin35=PA9 +Mcu.Pin36=PA10 +Mcu.Pin37=PA11 +Mcu.Pin38=PA12 +Mcu.Pin39=PA13 Mcu.Pin4=PH1-OSC_OUT -Mcu.Pin40=PD4 -Mcu.Pin41=PD5 -Mcu.Pin42=PD6 -Mcu.Pin43=PD7 -Mcu.Pin44=PB5 -Mcu.Pin45=PE1 -Mcu.Pin46=VP_FREERTOS_VS_CMSIS_V2 -Mcu.Pin47=VP_LWIP_VS_Enabled -Mcu.Pin48=VP_SYS_VS_tim1 -Mcu.Pin49=VP_TIM2_VS_ClockSourceINT +Mcu.Pin40=PA14 +Mcu.Pin41=PC10 +Mcu.Pin42=PC11 +Mcu.Pin43=PC12 +Mcu.Pin44=PD2 +Mcu.Pin45=PD4 +Mcu.Pin46=PD5 +Mcu.Pin47=PD6 +Mcu.Pin48=PD7 +Mcu.Pin49=PB5 Mcu.Pin5=PC1 -Mcu.Pin50=VP_TIM3_VS_ClockSourceINT -Mcu.Pin6=PA1 -Mcu.Pin7=PA2 -Mcu.Pin8=PA4 -Mcu.Pin9=PA5 -Mcu.PinsNb=51 +Mcu.Pin50=PB6 +Mcu.Pin51=PB9 +Mcu.Pin52=PE1 +Mcu.Pin53=VP_FREERTOS_VS_CMSIS_V2 +Mcu.Pin54=VP_LWIP_VS_Enabled +Mcu.Pin55=VP_SYS_VS_tim1 +Mcu.Pin56=VP_TIM2_VS_ClockSourceINT +Mcu.Pin57=VP_TIM3_VS_ClockSourceINT +Mcu.Pin6=PC2 +Mcu.Pin7=PC3 +Mcu.Pin8=PA1 +Mcu.Pin9=PA2 +Mcu.PinsNb=58 Mcu.ThirdParty0=STMicroelectronics.X-CUBE-ALGOBUILD.1.3.0 Mcu.ThirdPartyNb=1 Mcu.UserConstants= @@ -139,6 +147,7 @@ NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\: NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:false\:false NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 +NVIC.SPI2_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false\:false NVIC.SavedPendsvIrqHandlerGenerated=true NVIC.SavedSvcallIrqHandlerGenerated=true @@ -160,6 +169,14 @@ PA1.Signal=ETH_REF_CLK PA10.Locked=true PA10.Mode=Asynchronous PA10.Signal=USART1_RX +PA11.GPIOParameters=GPIO_Label +PA11.GPIO_Label=PA11_RS485_DIR1 +PA11.Locked=true +PA11.Signal=GPIO_Output +PA12.GPIOParameters=GPIO_Label +PA12.GPIO_Label=PA12_CTL_POWER +PA12.Locked=true +PA12.Signal=GPIO_Output PA13.Locked=true PA13.Mode=Serial_Wire PA13.Signal=SYS_JTMS-SWDIO @@ -185,6 +202,9 @@ PA8.Signal=I2C3_SCL PA9.Locked=true PA9.Mode=Asynchronous PA9.Signal=USART1_TX +PB10.Locked=true +PB10.Mode=Full_Duplex_Master +PB10.Signal=SPI2_SCK PB11.Mode=RMII PB11.Signal=ETH_TX_EN PB12.Mode=RMII @@ -204,6 +224,14 @@ PB5.GPIOParameters=GPIO_Label PB5.GPIO_Label=FAN_EN PB5.Locked=true PB5.Signal=GPIO_Output +PB6.GPIOParameters=GPIO_Label +PB6.GPIO_Label=PB6_EN_BEIDOU_PWN +PB6.Locked=true +PB6.Signal=GPIO_Output +PB9.GPIOParameters=GPIO_Label +PB9.GPIO_Label=PB9_SPI2_NSS +PB9.Locked=true +PB9.Signal=GPIO_Output PC1.Mode=RMII PC1.Signal=ETH_MDC PC10.Locked=true @@ -221,6 +249,12 @@ PC14-OSC32_IN.Signal=RCC_OSC32_IN PC15-OSC32_OUT.Locked=true PC15-OSC32_OUT.Mode=LSE-External-Oscillator PC15-OSC32_OUT.Signal=RCC_OSC32_OUT +PC2.Locked=true +PC2.Mode=Full_Duplex_Master +PC2.Signal=SPI2_MISO +PC3.Locked=true +PC3.Mode=Full_Duplex_Master +PC3.Signal=SPI2_MOSI PC4.Mode=RMII PC4.Signal=ETH_RXD0 PC5.Mode=RMII @@ -320,7 +354,7 @@ ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=0 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false @@ -380,6 +414,13 @@ SH.GPXTI14.0=GPIO_EXTI14 SH.GPXTI14.ConfNb=1 SH.GPXTI3.0=GPIO_EXTI3 SH.GPXTI3.ConfNb=1 +SPI2.CLKPhase=SPI_PHASE_1EDGE +SPI2.CLKPolarity=SPI_POLARITY_LOW +SPI2.CalculateBaudRate=21.0 MBits/s +SPI2.Direction=SPI_DIRECTION_2LINES +SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,CLKPolarity,CLKPhase +SPI2.Mode=SPI_MODE_MASTER +SPI2.VirtualType=VM_MASTER STMicroelectronics.X-CUBE-ALGOBUILD.1.3.0_SwParameter=LibraryCcDSPOoLibraryJjDSPOoLibrary\:true; TIM2.IPParameters=TIM_MasterOutputTrigger,Prescaler,Period TIM2.Period=100 diff --git a/user/bootloader_main.c b/user/bootloader_main.c new file mode 100644 index 0000000..8004095 --- /dev/null +++ b/user/bootloader_main.c @@ -0,0 +1,968 @@ +/** + ****************************************************************************** + * @file Templates/Src/main.c + * @author MCD Application Team + * @version V1.0.2 + * @date 13-November-2015 + * @brief Main program body + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2015 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "cmsis_os.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include "user.h" +#include "user_sflash.h" + +#define BOOT_VERSION "V2.0.0" + +#define SFLASH_IMAGE_BASE 0x000000 //addr in spiflash, offset:0M +#define APP_IMAGE_ADDR 0x08020000 + +enum{ + IMG_BOOT = 0x544F4F42, //"BOOT" + IMG_APP = 0x00505041, //"APP\0" +}; + +typedef struct { + uint32_t sign; + uint32_t addr; + uint32_t sz; + uint32_t crc16; + uint32_t reserve[12]; +}image_header_t; + +const char SW_VER[]= BOOT_VERSION ; +static image_header_t img_hdr; +static void go( uint32_t entry, uint32_t sp ); + +static uint8_t buf[5000]; + +extern uint32_t flash_write( uint32_t addr, uint8_t *data, uint32_t sz ); +extern uint32_t flash_erase( uint32_t addr, uint32_t sz ); +extern uint32_t sFLASH_crc16( uint32_t addr, uint32_t sz ); + +//------------------------------------------------------------------------------------------------------------------- +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 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" +#include "cmsis_os.h" + +ADC_HandleTypeDef hadc1; +DMA_HandleTypeDef hdma_adc1; + +I2C_HandleTypeDef hi2c3; + +SPI_HandleTypeDef hspi2; + +TIM_HandleTypeDef htim2; +TIM_HandleTypeDef htim3; + +UART_HandleTypeDef huart4; +UART_HandleTypeDef huart5; +UART_HandleTypeDef huart1; +UART_HandleTypeDef huart2; +UART_HandleTypeDef huart3; +UART_HandleTypeDef huart6; +ETH_HandleTypeDef heth; +/* Definitions for defaultTask */ +osThreadId_t defaultTaskHandle; +const osThreadAttr_t defaultTask_attributes = { + .name = "defaultTask", + .stack_size = 512 * 4, + .priority = (osPriority_t) osPriorityNormal, +}; +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +static void MX_GPIO_Init(void); +static void MX_DMA_Init(void); +static void MX_ADC1_Init(void); +static void MX_I2C3_Init(void); +static void MX_UART4_Init(void); +static void MX_UART5_Init(void); +static void MX_USART1_UART_Init(void); +static void MX_USART2_UART_Init(void); +static void MX_USART3_UART_Init(void); +static void MX_USART6_UART_Init(void); +static void MX_TIM2_Init(void); +static void MX_TIM3_Init(void); +static void MX_SPI2_Init(void); + +int32_t found_ota_image( void ); +int32_t update_ota_image( 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_DMA_Init(); + MX_ADC1_Init(); + MX_I2C3_Init(); + MX_UART4_Init(); + MX_UART5_Init(); + MX_USART1_UART_Init(); + MX_USART2_UART_Init(); + MX_USART3_UART_Init(); + MX_USART6_UART_Init(); + MX_TIM2_Init(); + MX_TIM3_Init(); + MX_SPI2_Init(); + + uint32_t entry; + uint32_t app_sp; + + printf( "bootloader, ver:%s\n", SW_VER ); + if( sFLASH_Init() == 0 ){ + //检查升级文件 + if( found_ota_image() == 0 ){ + for( int i=0; i<3; i++ ){ + //执行升级 + if( update_ota_image()== 0 ){ + break; + }else{ + printf( "update image fail[%d]!\n", i ); + } + } + } + } + + entry = 0; + if( *(uint32_t*)(APP_IMAGE_ADDR+0x200) == 0x00505041 ) + entry = *(uint32_t*)(APP_IMAGE_ADDR+4); + + if( entry > APP_IMAGE_ADDR && entry < APP_IMAGE_ADDR + 0x100000 ){ + app_sp = *(uint32_t*)(APP_IMAGE_ADDR); + go( entry, app_sp ); + } + 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_RCC_PWR_CLK_ENABLE(); + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLM = 8; + RCC_OscInitStruct.PLL.PLLN = 336; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + RCC_OscInitStruct.PLL.PLLQ = 7; + 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_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) + { + Error_Handler(); + } +} + +/** + * @brief ADC1 Initialization Function + * @param None + * @retval None + */ +static void MX_ADC1_Init(void) +{ + + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + */ + sConfig.Channel = ADC_CHANNEL_6; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ + +} + +/** + * @brief I2C3 Initialization Function + * @param None + * @retval None + */ +static void MX_I2C3_Init(void) +{ + + /* USER CODE BEGIN I2C3_Init 0 */ + + /* USER CODE END I2C3_Init 0 */ + + /* USER CODE BEGIN I2C3_Init 1 */ + + /* USER CODE END I2C3_Init 1 */ + hi2c3.Instance = I2C3; + hi2c3.Init.ClockSpeed = 100000; + hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c3.Init.OwnAddress1 = 0; + hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c3.Init.OwnAddress2 = 0; + hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c3) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN I2C3_Init 2 */ + + /* USER CODE END I2C3_Init 2 */ + +} + +/** + * @brief SPI2 Initialization Function + * @param None + * @retval None + */ +static void MX_SPI2_Init(void) +{ + + /* USER CODE BEGIN SPI2_Init 0 */ + + /* USER CODE END SPI2_Init 0 */ + + /* USER CODE BEGIN SPI2_Init 1 */ + + /* USER CODE END SPI2_Init 1 */ + /* SPI2 parameter configuration*/ + hspi2.Instance = SPI2; + hspi2.Init.Mode = SPI_MODE_MASTER; + hspi2.Init.Direction = SPI_DIRECTION_2LINES; + hspi2.Init.DataSize = SPI_DATASIZE_8BIT; + hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi2.Init.NSS = SPI_NSS_SOFT; + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; + hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi2.Init.TIMode = SPI_TIMODE_DISABLE; + hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi2.Init.CRCPolynomial = 10; + if (HAL_SPI_Init(&hspi2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN SPI2_Init 2 */ + + /* USER CODE END SPI2_Init 2 */ + +} + +/** + * @brief TIM2 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM2_Init(void) +{ + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + /* USER CODE BEGIN TIM2_Init 1 */ + + /* USER CODE END TIM2_Init 1 */ + htim2.Instance = TIM2; + htim2.Init.Prescaler = (84-1); + htim2.Init.CounterMode = TIM_COUNTERMODE_UP; + htim2.Init.Period = 100; + htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM2_Init 2 */ + + /* USER CODE END TIM2_Init 2 */ + +} + +/** + * @brief TIM3 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM3_Init(void) +{ + + /* USER CODE BEGIN TIM3_Init 0 */ + + /* USER CODE END TIM3_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + /* USER CODE BEGIN TIM3_Init 1 */ + + /* USER CODE END TIM3_Init 1 */ + htim3.Instance = TIM3; + htim3.Init.Prescaler = 0; + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 65535; + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM3_Init 2 */ + + /* USER CODE END TIM3_Init 2 */ + +} + +/** + * @brief UART4 Initialization Function + * @param None + * @retval None + */ +static void MX_UART4_Init(void) +{ + + /* USER CODE BEGIN UART4_Init 0 */ + + /* USER CODE END UART4_Init 0 */ + + /* USER CODE BEGIN UART4_Init 1 */ + + /* USER CODE END UART4_Init 1 */ + huart4.Instance = UART4; + huart4.Init.BaudRate = 115200; + huart4.Init.WordLength = UART_WORDLENGTH_8B; + huart4.Init.StopBits = UART_STOPBITS_1; + huart4.Init.Parity = UART_PARITY_NONE; + huart4.Init.Mode = UART_MODE_TX_RX; + huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart4.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart4) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN UART4_Init 2 */ + + /* USER CODE END UART4_Init 2 */ + +} + +/** + * @brief UART5 Initialization Function + * @param None + * @retval None + */ +static void MX_UART5_Init(void) +{ + + /* USER CODE BEGIN UART5_Init 0 */ + + /* USER CODE END UART5_Init 0 */ + + /* USER CODE BEGIN UART5_Init 1 */ + + /* USER CODE END UART5_Init 1 */ + huart5.Instance = UART5; + huart5.Init.BaudRate = 115200; + huart5.Init.WordLength = UART_WORDLENGTH_8B; + huart5.Init.StopBits = UART_STOPBITS_1; + huart5.Init.Parity = UART_PARITY_NONE; + huart5.Init.Mode = UART_MODE_TX_RX; + huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart5.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart5) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN UART5_Init 2 */ + + /* USER CODE END UART5_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; + if (HAL_UART_Init(&huart1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN USART1_Init 2 */ + + /* USER CODE END USART1_Init 2 */ + +} + +/** + * @brief USART2 Initialization Function + * @param None + * @retval None + */ +static void MX_USART2_UART_Init(void) +{ + + /* USER CODE BEGIN USART2_Init 0 */ + + /* USER CODE END USART2_Init 0 */ + + /* USER CODE BEGIN USART2_Init 1 */ + + /* USER CODE END USART2_Init 1 */ + huart2.Instance = USART2; + huart2.Init.BaudRate = 115200; + huart2.Init.WordLength = UART_WORDLENGTH_8B; + huart2.Init.StopBits = UART_STOPBITS_1; + huart2.Init.Parity = UART_PARITY_NONE; + huart2.Init.Mode = UART_MODE_TX_RX; + huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart2.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN USART2_Init 2 */ + + /* USER CODE END USART2_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; + if (HAL_UART_Init(&huart3) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN USART3_Init 2 */ + + /* USER CODE END USART3_Init 2 */ + +} + +/** + * @brief USART6 Initialization Function + * @param None + * @retval None + */ +static void MX_USART6_UART_Init(void) +{ + + /* USER CODE BEGIN USART6_Init 0 */ + + /* USER CODE END USART6_Init 0 */ + + /* USER CODE BEGIN USART6_Init 1 */ + + /* USER CODE END USART6_Init 1 */ + huart6.Instance = USART6; + huart6.Init.BaudRate = 9600; + huart6.Init.WordLength = UART_WORDLENGTH_8B; + huart6.Init.StopBits = UART_STOPBITS_1; + huart6.Init.Parity = UART_PARITY_NONE; + huart6.Init.Mode = UART_MODE_TX_RX; + huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart6.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart6) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN USART6_Init 2 */ + + /* USER CODE END USART6_Init 2 */ + +} + +/** + * Enable DMA controller clock + */ +static void MX_DMA_Init(void) +{ + + /* DMA controller clock enable */ + __HAL_RCC_DMA2_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA2_Stream0_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn); + +} + +/** + * @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_GPIOE_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOH_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(PA4_BEIDOU_RST_EN_GPIO_Port, PA4_BEIDOU_RST_EN_Pin, GPIO_PIN_SET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin, GPIO_PIN_SET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOD, LAKI_WAKE_UP_Pin|PD13_GPS_RST_EN_Pin|PD14_LORA_M0_Pin|DM222_PAPWR_EN_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOD, LAKI_RESET_Pin|PD15_LORA_M1_Pin|LORA_RST_Pin, GPIO_PIN_SET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOA, PA11_RS485_DIR1_Pin|PA12_CTL_POWER_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, FAN_EN_Pin|PB6_EN_BEIDOU_PWN_Pin|PB9_SPI2_NSS_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(SYS_LED_GPIO_Port, SYS_LED_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin : DQ_IN_Pin */ + GPIO_InitStruct.Pin = DQ_IN_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(DQ_IN_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pins : PA4_BEIDOU_RST_EN_Pin PA11_RS485_DIR1_Pin PA12_CTL_POWER_Pin */ + GPIO_InitStruct.Pin = PA4_BEIDOU_RST_EN_Pin|PA11_RS485_DIR1_Pin|PA12_CTL_POWER_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pin : PB14_RMII_INT_Pin */ + GPIO_InitStruct.Pin = PB14_RMII_INT_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(PB14_RMII_INT_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pins : PB15_ETH_RESET_Pin FAN_EN_Pin PB6_EN_BEIDOU_PWN_Pin PB9_SPI2_NSS_Pin */ + GPIO_InitStruct.Pin = PB15_ETH_RESET_Pin|FAN_EN_Pin|PB6_EN_BEIDOU_PWN_Pin|PB9_SPI2_NSS_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : LAKI_WAKE_UP_Pin LAKI_RESET_Pin PD13_GPS_RST_EN_Pin PD14_LORA_M0_Pin + PD15_LORA_M1_Pin LORA_RST_Pin DM222_PAPWR_EN_Pin */ + GPIO_InitStruct.Pin = LAKI_WAKE_UP_Pin|LAKI_RESET_Pin|PD13_GPS_RST_EN_Pin|PD14_LORA_M0_Pin + |PD15_LORA_M1_Pin|LORA_RST_Pin|DM222_PAPWR_EN_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); + + /*Configure GPIO pin : PD12_GPS_1PPS_Pin */ + GPIO_InitStruct.Pin = PD12_GPS_1PPS_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(PD12_GPS_1PPS_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pin : PC8_LORA_AUX_Pin */ + GPIO_InitStruct.Pin = PC8_LORA_AUX_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + HAL_GPIO_Init(PC8_LORA_AUX_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pin : SYS_LED_Pin */ + GPIO_InitStruct.Pin = SYS_LED_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(SYS_LED_GPIO_Port, &GPIO_InitStruct); + + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI3_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(EXTI3_IRQn); + +/* USER CODE BEGIN MX_GPIO_Init_2 */ +/* USER CODE END MX_GPIO_Init_2 */ +} + +/** + * @brief Period elapsed callback in non blocking mode + * @note This function is called when TIM1 interrupt took place, inside + * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment + * a global variable "uwTick" used as application time base. + * @param htim : TIM handle + * @retval None + */ +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) +{ + /* USER CODE BEGIN Callback 0 */ + + /* USER CODE END Callback 0 */ + if (htim->Instance == TIM1) { + HAL_IncTick(); + } + /* USER CODE BEGIN Callback 1 */ + + /* USER CODE END Callback 1 */ +} + +/** + * @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 */ + + +int32_t update_ota_image( void ) +{ + uint32_t offset; + int32_t ret; + uint32_t sflash_img_addr = SFLASH_IMAGE_BASE + sizeof( image_header_t ); + uint32_t blk_sz = 0; + + ret = flash_erase( APP_IMAGE_ADDR, img_hdr.sz ); + + if( ret==0 ){ + for(offset = 0; offset< img_hdr.sz;offset+=4096 ){ + blk_sz = img_hdr.sz - offset; + if( blk_sz > 4096 ) + blk_sz = 4096; + sFLASH_ReadBuffer(buf, sflash_img_addr+offset, blk_sz ); + ret = flash_write(APP_IMAGE_ADDR+offset,buf, blk_sz ); + if( ret ){ + printf( "wirte flash fail! addr:%08X, size:%d\n", APP_IMAGE_ADDR+offset, blk_sz ); + break; + }else{ + printf( "wirte flash addr:%08X, size:%d, OK!\n", APP_IMAGE_ADDR+offset, blk_sz ); + } + } + } + + if( !ret ){ + uint32_t read_crc16 = usMBCRC16( (uint8_t*)APP_IMAGE_ADDR, img_hdr.sz ); + if( read_crc16 == img_hdr.crc16 ){ + sFLASH_EraseSector( SFLASH_IMAGE_BASE ); //erase ota image header + printf( "update img success! erase sflash\n" ); + } + } + return ret; +} + +int32_t found_ota_image( void ) +{ + int32_t ret = -1; + + sFLASH_ReadBuffer( (uint8_t*)&img_hdr, SFLASH_IMAGE_BASE, sizeof( image_header_t ) ); + + if( img_hdr.sign == IMG_APP ){ + if( sFLASH_crc16( SFLASH_IMAGE_BASE+sizeof( image_header_t ), img_hdr.sz ) == img_hdr.crc16 ){ + printf( "found ota image, size:%08X, crc32:%08X, addr:%08X\n", img_hdr.sz, img_hdr.crc16, img_hdr.addr ); + ret = 0; + } + } + return ret; +} + +int fputc( int c, FILE* fp ) +{ +// HAL_UART_Transmit( &huart1, (uint8_t*)&c, 1, 10 ); + return 0; +} + +int32_t xxxmain(void) +{ + + + + ///boot_download(); + + while(1){ + //led flash + //user_led_on(); HAL_Delay(50); user_led_off(); HAL_Delay(50); + //user_led_on(); HAL_Delay(50); user_led_off(); HAL_Delay(50); + HAL_Delay( 1000 ); + } +} + + +void go( uint32_t entry, uint32_t sp ) +{ + uint32_t i; + + __disable_irq(); + HAL_NVIC_DisableIRQ( SysTick_IRQn ); + for( i=0; i +void debug_printf(char* str , ...) +{ + char buffer[100] = {0}; + va_list aptr; + va_start(aptr, str); + if ( vsnprintf(buffer,sizeof(buffer)-1, str, aptr) > 0) + { +// HAL_UART_Transmit( &huart1, (uint8_t*)buffer, strlen( buffer), strlen( buffer)*10 ); + } + va_end(aptr); +} + +int32_t osDelay( uint32_t ms ) +{ + HAL_Delay(ms); + return 0; +} + +//void UART1_RxCpltCallback(UART_HandleTypeDef *huart){} diff --git a/user/user.h b/user/user.h index a3843a6..8d7be70 100644 --- a/user/user.h +++ b/user/user.h @@ -4,14 +4,14 @@ #include "user_noise_sensor.h" -#define SW_VER_MAJOR 1 -#define SW_VER_MINOR 0 +#define VERSION "V1.0" + #define USER_SHELL_USE_UART1 0 #define WIND_SENSOR_USE_UART1 1 typedef osMutexId_t user_lock_t; - +typedef struct udp_pcb udp_pcb_t; typedef enum{ BD_ST_ERROR = -1, diff --git a/user/user_app.c b/user/user_app.c index 1400551..9136e97 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -1,6 +1,8 @@ #include "user.h" +const char SW_VER[16]= VERSION ; + extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart2; @@ -18,12 +20,10 @@ extern void user_rs485_rx_cb( void ); extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len); extern uint32_t adc_finished; -extern void Tcp_Server_Init(void); +extern void tcp_server_init(void); dev_t g_dev; -uint8_t app_ver[2] = { SW_VER_MAJOR, SW_VER_MINOR }; - void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if( huart->Instance == USART1 ){ @@ -141,7 +141,9 @@ void user_dev_init( void ) memset( &g_dev, 0, sizeof(g_dev ) ); g_dev.lock = user_lock_new(); } - +extern uint32_t code_size( void ); +extern uint32_t code_crc16( void ); +uint32_t code_sz,code_crc; void main_task(void *argument) { uint32_t delay_ticks; @@ -158,8 +160,9 @@ void main_task(void *argument) MX_LWIP_Init(); - Tcp_Server_Init(); - + tcp_server_init(); + udp_server_init(); + user_shell_start(); user_noise_task_start(); user_sound_task_start(); @@ -167,9 +170,12 @@ void main_task(void *argument) user_bd_task_start(); user_wind_task_start(); +code_sz = code_size(); +code_crc = code_crc16(); delay_ticks = xTaskGetTickCount(); for(;;) { + delay_ticks += 500; osDelayUntil( delay_ticks ); @@ -178,3 +184,65 @@ void main_task(void *argument) } /* USER CODE END 5 */ } + + +uint16_t user_crc16( uint8_t *data, uint32_t sz ) +{ + return usMBCRC16( data, sz ); +} + +uint32_t get_b_u32( uint8_t *data ) +{ + return (data[0]<<24)|(data[1]<<16)|(data[2]<<8)|(data[3]<<0); +} + +void put_b_u32( uint32_t value, uint8_t *data ) +{ + data[0] = (value>>24)&0xff; + data[1] = (value>>16)&0xff; + data[2] = (value>>8)&0xff; + data[3] = (value>>0)&0xff; +} + +uint32_t get_b_u24( uint8_t *data ) +{ + return (data[0]<<16)|(data[1]<<8)|(data[2]<<0); +} + +void put_b_u24( uint32_t value, uint8_t *data ) +{ + data[0] = (value>>16)&0xff; + data[1] = (value>>8)&0xff; + data[2] = (value>>0)&0xff; +} + +uint32_t get_b_u16( uint8_t *data ) +{ + return (data[0]<<8)|(data[1]<<0); +} + +void put_b_u16( uint16_t value, uint8_t *data ) +{ + data[0] = (value>>8)&0xff; + data[1] = (value>>0)&0xff; +} + +void user_eth_mac_get( uint8_t *mac ) +{ + //ÀûÓÃÓÃÉ豸UUIDÉú³É ²»Í¬eth mac ·ÀÖ¹³åÍ»¡£ + uint32_t uuid[3]; + uint16_t crc; + + uuid[0] = HAL_GetUIDw0(); + uuid[1] = HAL_GetUIDw1(); + uuid[2] = HAL_GetUIDw2(); + crc = usMBCRC16( (uint8_t*)&uuid[0], 4 ); + mac[0] = (crc>>8)&0xFC; + mac[1] = crc&0xFF; + crc = usMBCRC16( (uint8_t*)&uuid[1], 4 ); + mac[2] = (crc>>8)&0xFF; + mac[3] = crc&0xFF; + crc = usMBCRC16( (uint8_t*)&uuid[2], 4 ); + mac[4] = (crc>>8)&0xFF; + mac[5] = crc&0xFF; +} diff --git a/user/user_nv.c b/user/user_nv.c new file mode 100644 index 0000000..ea44ad4 --- /dev/null +++ b/user/user_nv.c @@ -0,0 +1,298 @@ +#include "user.h" + + +//sect 0 - 7 : 16K + 16K + 16K + 16K + 64K + 128K * 3 +/* Base address of the Flash sectors */ + +#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 11, 128 Kbytes */ + +//sect 4 0x0801 0000 -- 0x0801 FFFF + +#define NV_ADDR ADDR_FLASH_SECTOR_4 +#define NV_DATA_SIZE (2*1024) //2K + +void user_nv_init( void ) +{ + + return; +} + +static int32_t nv_erase( void ) +{ + + /*Variable used for Erase procedure*/ + FLASH_EraseInitTypeDef EraseInitStruct; + uint32_t SectorError = 0; + + /* Unlock the Flash to enable the flash control register access *************/ + ///HAL_FLASH_Unlock(); + + /* Erase the user Flash area + (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/ + + + /* Fill EraseInit structure*/ + EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; + EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; + EraseInitStruct.Sector = FLASH_SECTOR_4; + EraseInitStruct.NbSectors = 1; + + printf( "erase sect %d ...\r\n", EraseInitStruct.Sector ); + + if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) + { + /* + Error occurred while sector erase. + User can add here some code to deal with this error. + SectorError will contain the faulty sector and then to know the code error on this sector, + user can call function 'HAL_FLASH_GetError()' + */ + /* + FLASH_ErrorTypeDef errorcode = HAL_FLASH_GetError(); + */ + ///Error_Handler(); + printf( "erase fail!, errorcode:%08x\n", HAL_FLASH_GetError() ); + return -1; + } + printf( "erase ok!\n" ); + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, + you have to make sure that these data are rewritten before they are accessed during code + execution. If this cannot be done safely, it is recommended to flush the caches by setting the + DCRST and ICRST bits in the FLASH_CR register. */ + __HAL_FLASH_DATA_CACHE_DISABLE(); + __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); + + __HAL_FLASH_DATA_CACHE_RESET(); + __HAL_FLASH_INSTRUCTION_CACHE_RESET(); + + __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); + __HAL_FLASH_DATA_CACHE_ENABLE(); + + return 0; +} + +typedef struct{ + uint32_t data[NV_DATA_SIZE/4]; + uint16_t crc16; +}nv_data_t; + +static nv_data_t s_user_nv; + +extern uint16_t usMBCRC16( uint8_t * pucFrame, uint32_t usLen ); +int32_t nv_write( uint16_t off, uint8_t *data, uint16_t wr_size ) +{ + int32_t i; + uint32_t err = 0; + uint32_t nv_addr; + uint32_t nv_data_size; + + nv_addr = NV_ADDR; + nv_data_size = NV_DATA_SIZE; + + if( off+wr_size > nv_data_size ) + return -1; + + memcpy( &s_user_nv, (void*)nv_addr, sizeof( s_user_nv ) ); + if( usMBCRC16( (uint8_t*)&s_user_nv.data[0], nv_data_size ) != s_user_nv.crc16 ){ + memset( &s_user_nv, 0xff, sizeof( s_user_nv ) ); + } + + if( memcmp( (void*)(off+&s_user_nv.data[0]), data, wr_size ) ){ + + memcpy( (void*)(off+&s_user_nv.data[0]), data, wr_size ); + s_user_nv.crc16 = usMBCRC16( (uint8_t*)&s_user_nv.data[0], nv_data_size ); + + HAL_FLASH_Unlock(); + err = 0; + while( nv_erase() < 0 && err < 3){ + err++ ; + //DBG_PRINTF( "nv erase fail! retry:%d\r\n", err ); + osDelay(100); + //todo retry... + } + if( err >= 3 ){ + + + }else{ + for( i=0; i= sect_addr && addr < sect_addr+sector_size[i] ){ + erase_start = i; + } + if( addr+sz >= sect_addr && addr+sz < sect_addr+sector_size[i] ){ + erase_end = i; + break; + } + sect_addr += sector_size[i]; + } + + if( erase_start<4 || erase_end < erase_start || erase_end > 11 ){ + return -1; + } + + /* Unlock the Flash to enable the flash control register access *************/ + HAL_FLASH_Unlock(); + + /* Fill EraseInit structure*/ + EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; + EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; + EraseInitStruct.Sector = erase_start; + EraseInitStruct.NbSectors = erase_end-erase_start+1; + + //printf( "erase sect %d ...\r\n", EraseInitStruct.Sector ); + for( i=0; i<5; i++ ){ + if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){ + printf( "erase fail[%d]!, errorcode:%08x\n", i, HAL_FLASH_GetError() ); + ret = -1; + }else{ + printf( "erase ok!\n" ); + ret = 0; + break; + } + } + if( ret ){ + printf( "erase fail!\n" ); + } + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, + you have to make sure that these data are rewritten before they are accessed during code + execution. If this cannot be done safely, it is recommended to flush the caches by setting the + DCRST and ICRST bits in the FLASH_CR register. */ + __HAL_FLASH_DATA_CACHE_DISABLE(); + __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); + + __HAL_FLASH_DATA_CACHE_RESET(); + __HAL_FLASH_INSTRUCTION_CACHE_RESET(); + + __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); + __HAL_FLASH_DATA_CACHE_ENABLE(); + + return ret; +} + +int32_t flash_write( uint32_t addr, uint8_t *data, uint32_t sz ) +{ + int32_t err=0; + int32_t i; + uint32_t wr_word; + + err = 0; + + HAL_FLASH_Unlock(); + for( i=0; i+4<=sz; i+=4 ){ + memcpy( &wr_word, data+i, 4 ); + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr+i, wr_word ) != HAL_OK){ + err++; + break; + } + } + if( !err ){ + for( ; i nv_data_size ) + return -1; + + memcpy( &s_user_nv, (void*)nv_addr, sizeof( s_user_nv ) ); + if( usMBCRC16( (uint8_t*)&s_user_nv.data[0], nv_data_size ) != s_user_nv.crc16 ){ + memset( &s_user_nv, 0, sizeof( s_user_nv ) ); + }else{ + memcpy( data, (void*)(nv_addr+off), rd_size ); + } + return 0; +} + +int32_t user_nv_write( uint16_t off, uint8_t *data, uint16_t wr_size ) +{ + return nv_write( off, data, wr_size ); +} + +int32_t user_nv_read( uint16_t off, uint8_t *data, uint16_t rd_size ) +{ + return nv_read( off, data, rd_size ); +} + +uint32_t user_nv_size( void ) +{ + return NV_DATA_SIZE; +} + diff --git a/user/user_sflash.c b/user/user_sflash.c new file mode 100644 index 0000000..a6fe0c4 --- /dev/null +++ b/user/user_sflash.c @@ -0,0 +1,826 @@ +#include "user.h" +#include "user_sflash.h" +#include "stdio.h" +#include "string.h" +#include "stdlib.h" + +#define DBG_PRINTF( x... ) +/* +W25Q64JVSSIQ -- 64J -> 64M bit/8M Byte +FLASH_CS / PB.9 extern pullup +SPI2_SCK / PB.10 extern pullup +SPI2_MISO / PC.2 extern pullup +SPI2_MOSI / PC.3 extern pullup +FLASH_WP / vcc +FLASH_HOLD / vcc +*/ + + +#define SPI_TIMOUT 1000 //1000ms +#define SPI_OK ((uint8_t)0x00) +#define SPI_ERROR ((uint8_t)0x01) + +extern SPI_HandleTypeDef hspi2; + +//spiflash +static uint8_t buf_1k[1024]; +static uint32_t sFLASH_ready = 0; + +/** + * @brief Select sFLASH: Chip Select pin low + */ + +void sFLASH_GPIO_Init( void ) +{ + /* + //PB15_SPI2_MOSI --> INPUT + //PI3_SPI2_MOSI --> MODSI + GPIO_InitTypeDef GPIO_InitStruct = {0}; + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOI_CLK_ENABLE(); + + GPIO_InitStruct.Pin = PB15_SPI2_MOSI_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init(PB15_SPI2_MOSI_GPIO_Port, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init( GPIOI, &GPIO_InitStruct); + */ +} + +void sFLASH_GPIO_Deinit( void ) +{ +/* + //PB15_SPI2_MOSI --> MOSI + //PI3_SPI2_MOSI --> INPUT + GPIO_InitTypeDef GPIO_InitStruct = {0}; + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOI_CLK_ENABLE(); + + GPIO_InitStruct.Pin = PB15_SPI2_MOSI_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init(PB15_SPI2_MOSI_GPIO_Port, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; + HAL_GPIO_Init( GPIOI, &GPIO_InitStruct); +*/ +} + +void sFLASH_CS_LOW( void ) +{ + //spi2 require ... to lock + //sFLASH_GPIO_Init(); + PIN_LOW( PB9_SPI2_NSS ); +} + +/** + * @brief Deselect sFLASH: Chip Select pin high + */ +void sFLASH_CS_HIGH( void ) +{ + PIN_HIGH( PB9_SPI2_NSS ); + //sFLASH_GPIO_Deinit(); + //spi2 release ... to unlock} +} + +void sFLASH_DeInit(void) +{ + +} + +/** + * @brief Initializes the peripherals used by the SPI FLASH driver. + * @param None + * @retval None + */ + +int32_t sFLASH_Init(void) +{ + + //spi2_init(); + sFLASH_PowerOn(); + if( sFLASH_ReadID() == sFLASH_W25Q64JV_ID ){ + sFLASH_ready = 1; + }else{ + sFLASH_ready = 0; + } + + return sFLASH_ready?0:-1; +} + +/** + * @brief Erases the specified FLASH sector. + * @param SectorAddr: address of the sector to erase. + * @retval None + */ +void sFLASH_EraseSector(uint32_t SectorAddr) +{ + uint32_t ret; + uint8_t cmd[8]; + + if( !sFLASH_ready ) + return; + + /*!< Send write enable instruction */ + sFLASH_WriteEnable(); + + cmd[0] = sFLASH_CMD_SE; + cmd[1] = (SectorAddr>>16)& 0xFF; + cmd[2] = (SectorAddr>>8) & 0xFF; + cmd[3] = (SectorAddr) & 0xFF; + /*!< Sector Erase */ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 4, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); +} + +int32_t sFLASH_EraseBlock(uint32_t SectorAddr, uint32_t sect_sz ) +{ + int32_t ret; + uint8_t cmd[8]; + + /*!< Send write enable instruction */ + sFLASH_WriteEnable(); + + if( sect_sz == 64*1024 ) + cmd[0] = sFLASH_CMD_BE_64K; + else if( sect_sz == 32*1024 ) + cmd[0] = sFLASH_CMD_BE_32K; + else + cmd[0] = sFLASH_CMD_SE; + cmd[1] = (SectorAddr>>16)& 0xFF; + cmd[2] = (SectorAddr>>8) & 0xFF; + cmd[3] = (SectorAddr) & 0xFF; + /*!< Sector Erase */ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 4, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); + + return ret==HAL_OK?0:-1; +} + +/** + * @brief Erases the entire FLASH. + * @param None + * @retval None + */ +void sFLASH_EraseBulk(void) +{ + /*!< Send write enable instruction */ + sFLASH_WriteEnable(); + + /*!< Bulk Erase */ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + /*!< Send Bulk Erase instruction */ + sFLASH_SendByte(sFLASH_CMD_BE); + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); +} + +/** + * @brief Writes more than one byte to the FLASH with a single WRITE cycle + * (Page WRITE sequence). + * @note The number of byte can't exceed the FLASH page size. + * @param pBuffer: pointer to the buffer containing the data to be written + * to the FLASH. + * @param WriteAddr: FLASH's internal address to write to. + * @param NumByteToWrite: number of bytes to write to the FLASH, must be equal + * or less than "sFLASH_PAGESIZE" value. + * @retval None + */ +void sFLASH_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) +{ + uint32_t ret; + uint8_t cmd[8]; + + cmd[0] = sFLASH_CMD_WRITE; + cmd[1] = (WriteAddr>>16) & 0xFF; + cmd[2] = (WriteAddr>> 8) & 0xFF; + cmd[3] = (WriteAddr>> 0) & 0xFF; + /*!< Enable the write access to the FLASH */ + sFLASH_WriteEnable(); + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 4, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + ret = HAL_SPI_Transmit( &hspi2, pBuffer, NumByteToWrite, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); +} + +/** + * @brief Writes block of data to the FLASH. In this function, the number of + * WRITE cycles are reduced, using Page WRITE sequence. + * @param pBuffer: pointer to the buffer containing the data to be written + * to the FLASH. + * @param WriteAddr: FLASH's internal address to write to. + * @param NumByteToWrite: number of bytes to write to the FLASH. + * @retval None + */ +void sFLASH_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) +{ + uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0; + + if( !sFLASH_ready ) + return; + + Addr = WriteAddr % sFLASH_SPI_PAGESIZE; + count = sFLASH_SPI_PAGESIZE - Addr; + NumOfPage = NumByteToWrite / sFLASH_SPI_PAGESIZE; + NumOfSingle = NumByteToWrite % sFLASH_SPI_PAGESIZE; + + if (Addr == 0) /*!< WriteAddr is sFLASH_PAGESIZE aligned */ + { + if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */ + { + sFLASH_WritePage(pBuffer, WriteAddr, NumByteToWrite); + } + else /*!< NumByteToWrite > sFLASH_PAGESIZE */ + { + while (NumOfPage--) + { + sFLASH_WritePage(pBuffer, WriteAddr, sFLASH_SPI_PAGESIZE); + WriteAddr += sFLASH_SPI_PAGESIZE; + pBuffer += sFLASH_SPI_PAGESIZE; + } + if (NumOfSingle != 0) + sFLASH_WritePage(pBuffer, WriteAddr, NumOfSingle); + } + } + else /*!< WriteAddr is not sFLASH_PAGESIZE aligned */ + { + if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */ + { + if (NumOfSingle > count) /*!< (NumByteToWrite + WriteAddr) > sFLASH_PAGESIZE */ + { + temp = NumOfSingle - count; + + sFLASH_WritePage(pBuffer, WriteAddr, count); + WriteAddr += count; + pBuffer += count; + + sFLASH_WritePage(pBuffer, WriteAddr, temp); + } + else + { + sFLASH_WritePage(pBuffer, WriteAddr, NumByteToWrite); + } + } + else /*!< NumByteToWrite > sFLASH_PAGESIZE */ + { + NumByteToWrite -= count; + NumOfPage = NumByteToWrite / sFLASH_SPI_PAGESIZE; + NumOfSingle = NumByteToWrite % sFLASH_SPI_PAGESIZE; + + sFLASH_WritePage(pBuffer, WriteAddr, count); + WriteAddr += count; + pBuffer += count; + + while (NumOfPage--) + { + sFLASH_WritePage(pBuffer, WriteAddr, sFLASH_SPI_PAGESIZE); + WriteAddr += sFLASH_SPI_PAGESIZE; + pBuffer += sFLASH_SPI_PAGESIZE; + } + + if (NumOfSingle != 0) + { + sFLASH_WritePage(pBuffer, WriteAddr, NumOfSingle); + } + } + } +} + +/** + * @brief Reads a block of data from the FLASH. + * @param pBuffer: pointer to the buffer that receives the data read from the FLASH. + * @param ReadAddr: FLASH's internal address to read from. + * @param NumByteToRead: number of bytes to read from the FLASH. + * @retval None + */ +void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) +{ + uint32_t ret; + uint8_t cmd[8]; + + cmd[0] = sFLASH_CMD_READ; + cmd[1] = (ReadAddr>>16) & 0xFF; + cmd[2] = (ReadAddr>> 8) & 0xFF; + cmd[3] = (ReadAddr>> 0) & 0xFF; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 4, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + ret = HAL_SPI_Receive( &hspi2, pBuffer, NumByteToRead , SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +/** + * @brief Reads FLASH identification. + * @param None + * @retval FLASH identification + */ +//HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) + +uint32_t sFLASH_ReadID(void) +{ + uint8_t buf[8]; + uint32_t ret; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + /*!< Send "RDID " instruction */ + sFLASH_SendByte(sFLASH_CMD_RDID); + + ret = HAL_SPI_Receive( &hspi2, buf, 3, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + return (buf[0]<<16) | ( buf[1] <<8 ) | buf[2]; +} + +/** + * @brief Sends a byte through the SPI interface and return the byte received + * from the SPI bus. + * @param byte: byte to send. + * @retval The value of the received byte. + */ +uint8_t sFLASH_SendByte(uint8_t byte) +{ + uint8_t buf; + uint32_t ret; + + buf = byte; + ret = HAL_SPI_Transmit( &hspi2, &buf, 1, SPI_TIMOUT ); + ///HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE, 5000) + if( ret == HAL_OK ) + ; + return ret; +} + +/** + * @brief Enables the write access to the FLASH. + * @param None + * @retval None + */ +void sFLASH_WriteEnable(void) +{ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Write Enable" instruction */ + sFLASH_SendByte(sFLASH_CMD_WREN); + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +/** + * @brief Polls the status of the Write In Progress (WIP) flag in the FLASH's + * status register and loop until write opertaion has completed. + * @param None + * @retval None + */ +void sFLASH_WaitForWriteEnd(void) +{ + uint8_t flashstatus = 0; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Read Status Register" instruction */ + sFLASH_SendByte(sFLASH_CMD_RDSR); + + /*!< Loop as long as the memory is busy with a write cycle */ + do + { + /*!< Send a dummy byte to generate the clock needed by the FLASH + and put the value of the status register in FLASH_Status variable */ + //flashstatus = sFLASH_SendByte(sFLASH_DUMMY_BYTE); + if( HAL_SPI_Receive( &hspi2, &flashstatus, 1 , SPI_TIMOUT ) != HAL_OK ) + { + //Error_Handler(); + } + + } + while ((flashstatus & sFLASH_WIP_FLAG) == SET); /* Write in progress */ + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +void sFLASH_PowerOff(void) +{ + volatile uint8_t id; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Write Enable" instruction */ + sFLASH_SendByte(sFLAsh_CMD_POWERDOWN); + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + +} + +void sFLASH_PowerOn(void) +{ + uint32_t ret; + uint8_t cmd[8]={ sFLAsh_CMD_POWERUP, 0xA5, 0xA5, 0xA5, 0xA5, }; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 5, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +void sFlash_PowerTest( void ) +{ + volatile uint32_t id; + uint32_t i; + + id = sFLASH_ReadID(); + DBG_PRINTF( "Read Flas Id: %08X\n", id ); + sFLASH_PowerOff(); + for( i=0; i<0xffff; i++ ); + id = sFLASH_ReadID(); + DBG_PRINTF( "Pwr OFF -- Read Flas Id: %08X\n", id ); + sFLASH_PowerOn(); + for( i=0; i<0xffff; i++ ); + id = sFLASH_ReadID(); + DBG_PRINTF( "Pwr ON -- Read Flas Id: %08X\n", id ); + for( i=0; i<0xffff; i++ ); + +} + +/** + * @brief Reads a block of data from the FLASH. + * @param pBuffer: pointer to the buffer that receives the data read from the FLASH. + * @param ReadAddr: FLASH's internal address to read from. + * @param NumByteToRead: number of bytes to read from the FLASH. + * @retval None + */ +uint32_t sFLASH_ReadSum( void ) +{ + uint32_t ret; + uint8_t cmd[8]; + + uint32_t sum=0; + uint32_t count=0; +// uint8_t data; + uint32_t i; + + cmd[0] = sFLASH_CMD_READ; + cmd[1] = 0; + cmd[2] = 0; + cmd[3] = 0; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 4, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + for( count=0; count<0x800000/1024 - 256; count++ ){ + /*!< Read a byte from the FLASH */ + ret = HAL_SPI_Receive( &hspi2, buf_1k, 1024, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + for( i=0; i<1024; i++ ) + sum += buf_1k[i]; + } + sum += 0xff * 256 *1024; + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + return sum; +} + +extern uint16_t mbcrc16_update( uint16_t code, uint8_t *data, uint32_t sz ); +uint32_t sFLASH_crc16( uint32_t addr, uint32_t sz ) +{ + uint32_t ret; + uint8_t cmd[8]; + uint32_t count=0; + uint32_t n; + uint8_t page_buf[256]; + uint16_t crc16; + + cmd[0] = sFLASH_CMD_READ; + cmd[1] = (addr>>16) & 0xFF; + cmd[2] = (addr>> 8) & 0xFF; + cmd[3] = (addr>> 0) & 0xFF; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + ret = HAL_SPI_Transmit( &hspi2, cmd, 4, SPI_TIMOUT ); + if( ret == HAL_OK ) + ; + + crc16=0xFFFF; + for( count=0; count sizeof(page_buf) ) + n = sizeof(page_buf); + ret = HAL_SPI_Receive( &hspi2, page_buf, n, SPI_TIMOUT ); + if( ret != HAL_OK ) + break; + crc16 = mbcrc16_update( crc16, page_buf, n); + } + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + //crc16^=0xFFFFFFFF; + + return crc16; +} + +int32_t user_sflash_erase( uint32_t addr, uint32_t sz ) +{ + uint32_t erase_sz; + int32_t ret = 0; + + if( !sFLASH_ready ){ + sFLASH_Init(); + } + + if( addr & 64*1024 ){ + ret = -1; + }else{ + erase_sz = 0; + while( erase_sz < sz ){ + ret = sFLASH_EraseBlock( addr+erase_sz, 64*1024 ); + if( ret ) + break; //err + else + erase_sz += 64*1024; + } + + } + + return ret; +} + +int32_t user_sflash_write( uint32_t addr, uint8_t *data, uint32_t sz ) +{ + sFLASH_WriteBuffer( data, addr, sz ); + return 0; +} + +uint32_t user_sflash_crc16( uint32_t addr, uint32_t sz ) +{ + return sFLASH_crc16( addr, sz ); +} + +#if 0 +static uint8_t test_buffer[1024*8]; +void sFLASH_Test( void ) +{ + uint32_t id, i; + char buff[32]; + uint8_t * ptr; + + //uint32_t s; + +// sFlash_PowerTest(); + sFLASH_Init(); + + + sFLASH_ReadBuffer(test_buffer, 0x700000, sizeof(test_buffer) ); //from 7M addr, read 4k + + + + for( i=0; i
© COPYRIGHT 2011 STMicroelectronics
+ ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USER_SFLASH_H +#define __USER_SFLASH_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include + +/** + * @brief M25P SPI Flash supported commands + */ +#define sFLASH_CMD_WRITE 0x02 /*!< Write to Memory instruction */ +#define sFLASH_CMD_WRSR 0x01 /*!< Write Status Register instruction */ +#define sFLASH_CMD_WREN 0x06 /*!< Write enable instruction */ +#define sFLASH_CMD_READ 0x03 /*!< Read from Memory instruction */ +#define sFLASH_CMD_RDSR 0x05 /*!< Read Status Register instruction */ +#define sFLASH_CMD_RDID 0x9F /*!< Read identification */ +#define sFLASH_CMD_SE_4K 0x20 /*0x20:4K < Sector Erase instruction */ +#define sFLASH_CMD_BE_32K 0x52 /*0x52:32K !< Block Erase instruction */ +#define sFLASH_CMD_BE_64K 0xD8 /*0xD8:64K !< Block Erase instruction */ +#define sFLASH_CMD_SE sFLASH_CMD_SE_4K /*0x20:4K, 0x52:32K, 0xD8:64K !< Sector Erase instruction */ +#define sFLASH_SECT_SZ 0x1000 //4k + +#define sFLASH_CMD_BE 0xC7 /*!< Bulk Erase instruction */ +#define sFLAsh_CMD_POWERDOWN 0xB9 +#define sFLAsh_CMD_POWERUP 0xAB + +#define sFLASH_WIP_FLAG 0x01 /*!< Write In Progress (WIP) flag */ + +#define sFLASH_DUMMY_BYTE 0xA5 +#define sFLASH_SPI_PAGESIZE 0x100 + +#define sFLASH_M25P128_ID 0x202018 +#define sFLASH_M25P64_ID 0x202017 +#define sFLASH_W25Q64JV_ID 0xEF4017 + +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Functions + * @{ + */ +/** + * @brief High layer functions + */ +void sFLASH_DeInit(void); +int32_t sFLASH_Init(void); +void sFLASH_EraseSector(uint32_t SectorAddr); +void sFLASH_EraseBulk(void); +int32_t sFLASH_EraseBlock(uint32_t SectorAddr, uint32_t sect_sz ); +void sFLASH_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); +void sFLASH_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); +void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead); +uint32_t sFLASH_ReadID(void); +void sFLASH_StartReadSequence(uint32_t ReadAddr); + +/** + * @brief Low layer functions + */ +uint8_t sFLASH_ReadByte(void); +uint8_t sFLASH_SendByte(uint8_t byte); +uint16_t sFLASH_SendHalfWord(uint16_t HalfWord); +void sFLASH_WriteEnable(void); +void sFLASH_WaitForWriteEnd(void); + + +uint32_t sFLASH_ReadSum( void ); + +void sFLASH_PowerOff(void); +void sFLASH_PowerOn(void); + +int32_t user_sflash_erase( uint32_t addr, uint32_t sz ); +int32_t user_sflash_write( uint32_t addr, uint8_t *data, uint32_t sz ); +uint32_t user_sflash_crc16( uint32_t addr, uint32_t sz ); + +#ifdef __cplusplus +} +#endif + +#endif /* __USER_SFLASH_H */ diff --git a/user/user_tcp.c b/user/user_tcp.c index 7c69d29..6272c6b 100644 --- a/user/user_tcp.c +++ b/user/user_tcp.c @@ -7,6 +7,7 @@ #include "user.h" #include "user_tcp.h" +#include "user_sflash.h" /* @@ -23,7 +24,7 @@ static struct tcp_pcb *tcp_connected_pcb = NULL; static uint16_t tx_pkt_sn = 0; static uint16_t rx_pkt_sn; -void Tcp_Server_Init(void) +void tcp_server_init(void) { /* Ϊtcp·þÎñÆ÷·ÖÅäÒ»¸ötcp_pcb½á¹¹Ìå */ @@ -113,17 +114,24 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ); FLAG SN CMD LEN DATA CRC 55AA 0002 0001 0008 XX... XXXX CMD: - 0001: query LEN 0 query period:5s - 0002: resp LEN 32 , DATA: noise + alarm + pos + wind - 1001: Lora Tx - 1002: Lora Rx - 2001: BD Tx - 2002: BD Rx - 3001: Laki Tx - 3002: Laki Rx - - FFFF: Debug get adc raw data - + 0001: query LEN 0 query period:5s + 0002: resp LEN 32 , DATA: noise + alarm + pos + wind + 1001: Lora Tx + 1002: Lora Rx + 2001: BD Tx + 2002: BD Rx + 3001: Laki Tx + 3002: Laki Rx + + FFFF: Debug get adc raw data +*/ +/* + //TODO: + F001: fw_ver/16B fw_size/4 fw_crc16/4 + F002: sfash_erase + F003: sflash_write + F004: sflash_read + F005: sflash_read_crc */ int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint32_t data_sz ) @@ -154,6 +162,12 @@ extern int lora_uart_tx( uint8_t *tx_data, uint16_t tx_size ); int g_adc_debug = 0; extern int32_t g_alarm; +extern const char SW_VER[16]; +extern void put_b_u32( uint32_t value, uint8_t *data ); +extern uint32_t get_b_u32( uint8_t *data ); +extern uint32_t code_size( void ); +extern uint32_t code_crc16( void ); +extern uint16_t user_crc16( uint8_t *data, uint32_t sz ); int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) { @@ -161,7 +175,7 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) uint16_t recv_pkt_sn; uint16_t cmd; uint16_t pkt_data_sz; - //uint8_t *pkt_data; + uint8_t *pkt_data; uint16_t pkt_crc; uint8_t resp[64]; uint16_t resp_sz = 0; @@ -180,7 +194,7 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) recv_pkt_sn = (p[2]<<8)|p[3]; cmd = (p[4]<<8)|p[5]; pkt_data_sz = (p[6]<<8)|p[7]; - //pkt_data = p+8; + pkt_data = p+8; pkt_crc = (p[8+pkt_data_sz]<<8)|p[8+pkt_data_sz+1]; if( (recv_pkt_sn != rx_pkt_sn) && pkt_crc == usMBCRC16(p, 8+pkt_data_sz) ){ @@ -217,6 +231,76 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) g_adc_debug = p[8]; } break; + /*------------------- + //TODO: + F001: fw_ver/16B fw_size/4 fw_crc16/4 + F002: sfash_erase + F003: sflash_write + F004: sflash_read + F005: sflash_read_crc + F006: system restart + -------------------*/ + case 0xF001: + memset( resp_data, 0, sizeof(resp_data ) ); + strncpy( (char*)resp_data, &SW_VER[0], 16 ); + put_b_u32( code_size(), &resp_data[16] ); + put_b_u32( code_crc16(), &resp_data[20] ); + resp_sz = wrap_pkt( resp, rx_pkt_sn, 0xF001, resp_data, 24 ); + break; + case 0xF002: + { + uint32_t erase_addr = get_b_u32( &pkt_data[0] ); + uint32_t erase_sz = get_b_u32( &pkt_data[4] ); + resp_data[0] = user_sflash_erase( erase_addr, erase_sz )==0?1:0; + resp_sz = wrap_pkt( resp, rx_pkt_sn, 0xF002, resp_data, 1 ); + } + break; + case 0xF003: + { + uint32_t wr_addr = get_b_u32( &pkt_data[0] ); + uint32_t wr_sz = pkt_data_sz - 4; + uint32_t wr_crc; + uint32_t rd_crc; + + user_sflash_write( wr_addr, &pkt_data[4], wr_sz ); + wr_crc = user_crc16( &pkt_data[4], wr_sz ); + rd_crc = user_sflash_crc16( wr_addr, wr_sz ); + resp_data[0] = wr_crc == rd_crc ? 1:0; + resp_sz = wrap_pkt( resp, rx_pkt_sn, 0xF003, resp_data, 1 ); + //debug + if( wr_crc != rd_crc ){ + //DBG_PRINTF( "[%X-%X] write crc:%04X, read rcr:%04X\n",wr_addr, wr_sz, wr_crc, rd_crc ); + }else{ + //DBG_PRINTF( "[%X-%X] write/read crc:%04X\n",wr_addr, wr_sz, wr_crc ); + } + break; + } + case 0xF004: + break; + case 0xF005: + { + uint32_t addr = get_b_u32( &pkt_data[0] ); + uint32_t sz = get_b_u32( &pkt_data[4] ); + uint32_t crc = get_b_u32( &pkt_data[8] ); + uint32_t crc_read = user_sflash_crc16( addr, sz ); + if( crc == crc_read ){ + resp_data[0] = 1; + //debug_printf( "sflash %d--%d, crc32:%08X\n", addr, sz, crc ); + }else{ + resp_data[0] = 0; + //debug_printf( "sflash %d--%d, crc32:[%08X | %08X]\n", addr, sz, crc, crc_read ); + } + resp_sz = wrap_pkt( resp, rx_pkt_sn, 0xF005, resp_data, 1 ); + break; + } + case 0xF006: + //systewm restart + resp_data[0] = 1; + resp_sz = wrap_pkt( resp, rx_pkt_sn, 0xF006, resp_data, 1 ); + tcp_write( pcb, resp, resp_sz, TCP_WRITE_FLAG_COPY ); + osDelay(1000); + HAL_NVIC_SystemReset(); + return 0; } if( resp_sz ){ tcp_write( pcb, resp, resp_sz, TCP_WRITE_FLAG_COPY ); diff --git a/user/user_tcp.h b/user/user_tcp.h index 7c8fdef..32f74ab 100644 --- a/user/user_tcp.h +++ b/user/user_tcp.h @@ -56,7 +56,7 @@ struct name ********************************************************************************************************* */ -void Tcp_Server_Init(void); +void tcp_server_init(void); /* ******************************************************************************************************** diff --git a/user/user_udp.c b/user/user_udp.c new file mode 100644 index 0000000..b6359e7 --- /dev/null +++ b/user/user_udp.c @@ -0,0 +1,372 @@ +#include "lwip/debug.h" +#include "lwip/stats.h" +#include "lwip/udp.h" +#include +#include +#include + +#include "user.h" +#include "user_udp.h" +#include "user_sflash.h" + + +/* +********************************************************************************************************* +* LOCAL TABLES +********************************************************************************************************* +*/ + +#define PKT_HEADER_LEN 10 //flag + sn + cmd + dlen + crc = 10 + +typedef struct{ + uint16_t flag; //FF00-²éѯ/FF01-Ó¦´ð' + uint16_t sn; //°üÐòÁкŠ+ uint16_t cmd; //ÃüÁîÂë + uint16_t dlen; //Êý¾Ý¶Î³¤¶È ºóÃæÊý¾Ý¶Î³¤¶È£¬¿ÉÒÔΪ0£» + uint8_t* pdata; //Ö¸ÏòÊý¾Ý×Ö¶Î, Êý¾Ý³¤¶È0µ½ffff; + uint16_t crc; //×ܸöÊý¾Ý°üµÄУÑéÖµ£¬crc-16/modbus +}udp_pkt_t; + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +#define UDP_SERVER_PORT 10000 /* define the UDP local connection port */ +#define UDP_CLIENT_PORT 10000 /* define the UDP remote connection port */ + +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +extern int32_t udp_recv_cb( udp_pcb_t *upcb, ip_addr_t * tx_ip, uint16_t tx_port, uint8_t *rx_data, uint32_t rx_sz ); + +typedef struct { + struct udp_pcb *upcb; + ip_addr_t remote_addr; + uint16_t remote_port; +}udp_senssion_t; + +static udp_senssion_t g_udp={NULL, NULL, 0 }; + + +/* Private function prototypes -----------------------------------------------*/ +void udp_server_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); +int32_t udp_tx( udp_pcb_t *upcb, ip_addr_t * tx_ip, uint16_t tx_port, uint8_t *tx_data, uint32_t tx_sz ); + +extern uint16_t usMBCRC16( uint8_t * pucFrame, uint32_t usLen ); + +extern const char SW_VER[16]; +extern void put_b_u32( uint32_t value, uint8_t *data ); +extern uint32_t get_b_u32( uint8_t *data ); +extern uint32_t code_size( void ); +extern uint32_t code_crc16( void ); +extern uint16_t user_crc16( uint8_t *data, uint32_t sz ); + +/** + * @brief This function is called when an UDP datagrm has been received on the port UDP_PORT. + * @param arg user supplied argument (udp_pcb.recv_arg) + * @param pcb the udp_pcb which received data + * @param p the packet buffer that was received + * @param addr the remote IP address from which the packet was received + * @param port the remote port from which the packet was received + * @retval None + */ +int32_t udp_tx( udp_pcb_t *upcb, ip_addr_t * tx_ip, uint16_t tx_port, uint8_t *tx_data, uint32_t tx_sz ) +{ + struct pbuf *p; + + //tick = osKernelGetTickCount()/portTICK_PERIOD_MS; + //DBG_PRINTF( "[%d.%03d]UDP TX: [", tick/1000, tick%1000 ); + + for( int i=0; i 40 ){ + //DBG_PRINTF( " ... " ); + break; + } + } + //DBG_PRINTF( "]\r\n" ); + + p = pbuf_alloc(PBUF_TRANSPORT, tx_sz, PBUF_RAM); + if( p ){ + pbuf_take( p, tx_data, tx_sz ); + udp_sendto( upcb, p, tx_ip, tx_port ); + pbuf_free( p ); + } + return 0; +} + +#define debug_printf printf +#define DBG_PRINTF printf + +void print_rx_pkt( char * str, udp_pkt_t *pkt ) +{ + uint32_t tick; + if( !pkt ) + return; + debug_printf( "----------------------------------------------------\n" ); + tick = osKernelGetTickCount()/portTICK_PERIOD_MS; + debug_printf( "[%d.%03d]", tick/1000, tick%1000 ); + debug_printf( "%s -- get rx pkt, cmd:%02X, datalen:%d \n", str?str:"", pkt->cmd, pkt->dlen ); + if( pkt->dlen ){ + int i = 0; + debug_printf( "data:[" ); + for( i=0; idlen; i++ ){ + debug_printf( "%02X ", pkt->pdata[i] ); + if( i>40 ){ + DBG_PRINTF( " ... " ); + break; + } + } + debug_printf( " ]\n" ); + } + debug_printf( "----------------------------------------------------\n" ); +} + +int32_t get_rx_pkt( uint8_t * data, uint32_t data_size, udp_pkt_t *out_pkt ) +{ + uint16_t data_len; + uint16_t crc16, crc16_recv; + + if( data_size < PKT_HEADER_LEN ){ + //timeout++; + return 0; + } + + data_len = (data[6]<<8)|data[7]; + if( data_size < data_len+PKT_HEADER_LEN ){ + //timeout + return 0; + } + + crc16 = user_crc16( data, data_len+PKT_HEADER_LEN - 2 ); + crc16_recv = (data[data_len+PKT_HEADER_LEN-2]<<8)|data[data_len+PKT_HEADER_LEN-1]; + if( crc16 != crc16_recv ){ + return -1; + } + out_pkt->flag = (data[0]<<8)|data[1]; + out_pkt->sn = (data[2]<<8)|data[3]; + out_pkt->cmd = (data[4]<<8)|data[5]; + out_pkt->dlen = data_len; + out_pkt->pdata = &data[8]; + return 1; +} + +uint16_t wrap_tx_pkt( uint16_t cmd, uint8_t *pkt_buf, uint16_t sn, uint8_t *data, uint16_t data_len ) +{ + uint16_t crc=0; + uint16_t n = 0; + + debug_printf( "wrap_tx_pkt, cmd:%02X, sn:%d, dlen:%d\n", cmd, sn, data_len ); + + pkt_buf[n++] = 0xFF; //flag:ff00 + pkt_buf[n++] = 0x00; + pkt_buf[n++] = (sn>>8)&0xff; //sn + pkt_buf[n++] = (sn>>0)&0xff; + pkt_buf[n++] = (cmd>>8)&0xff; //cmd + pkt_buf[n++] = (cmd>>0)&0xff; + pkt_buf[n++] = (data_len>>8)&0xff; //dlen + pkt_buf[n++] = (data_len>>0)&0xff; + memcpy( &pkt_buf[n], data, data_len ); + n += data_len; + crc = user_crc16( pkt_buf, n); + pkt_buf[n++] = (crc>>8)&0xff; + pkt_buf[n++] = (crc)&0xff; + return n; +} +static uint8_t txbuf[512]; +int32_t handle_rx_pkt( udp_pcb_t *upcb, udp_pkt_t *pkt, ip_addr_t* ip, uint16_t port ) +{ + static udp_pkt_t last_pkt_header; + uint32_t pkt_retransmitted = 0; + + uint8_t buf[250]; + uint16_t n = 0; +// uint16_t tx_n = 0; + uint32_t off; + uint32_t i; + uint8_t can_bus; + uint8_t ch; + + if( pkt->flag != 0xff00 ){ + return -1; + } + + if( last_pkt_header.cmd == pkt->cmd && last_pkt_header.sn == pkt->sn ){ + pkt_retransmitted = 1; + //DBG_PRINTF( "RX retransmitted pkt!\n" ); + }else{ + pkt_retransmitted = 0; + memcpy( &last_pkt_header, pkt, sizeof( last_pkt_header ) ); + } + + switch( pkt->cmd ){ + case 0xFF82: + //read fw version + memset( buf, 0, 24 ); + strncpy( (char*)buf, VERSION, 16 ); + //version 16B + SZ 4B + crc32 4B + put_b_u32( code_size(), &buf[16] ); + put_b_u32( code_crc16(), &buf[20] ); + n = wrap_tx_pkt( pkt->cmd, txbuf, pkt->sn, buf, 24 ); + udp_tx( upcb, ip, port, txbuf, n ); + break; + case 0xFF83: + //systewm restart + buf[0] = 1; + n = wrap_tx_pkt( pkt->cmd, txbuf, pkt->sn, buf, 1 ); + udp_tx( upcb, ip, port, txbuf, n ); + osDelay(1000); + HAL_NVIC_SystemReset(); + break; + case 0xFFA0: + //sflash erase addr, size + { + uint32_t erase_addr = get_b_u32( &pkt->pdata[0] ); + uint32_t erase_sz = get_b_u32( &pkt->pdata[4] ); + buf[0] = user_sflash_erase( erase_addr, erase_sz )==0?1:0; + n = wrap_tx_pkt( pkt->cmd, txbuf, pkt->sn, buf, 1 ); + udp_tx( upcb, ip, port, txbuf, n ); + } + break; + case 0xFFA1: + //sflash write addr + { + uint32_t wr_addr = get_b_u32( &pkt->pdata[0] ); + uint32_t wr_sz = pkt->dlen - 4; + uint32_t wr_crc; + uint32_t rd_crc; + + user_sflash_write( wr_addr, &pkt->pdata[4], wr_sz ); + wr_crc = user_crc16( &pkt->pdata[4], wr_sz ); + rd_crc = user_sflash_crc16( wr_addr, wr_sz ); + buf[0] = wr_crc == rd_crc ? 1:0; + n = wrap_tx_pkt( pkt->cmd, txbuf, pkt->sn, buf, 1 ); + udp_tx( upcb, ip, port, txbuf, n ); + //debug + if( wr_crc != rd_crc ){ + DBG_PRINTF( "[%X-%X] write crc:%04X, read rcr:%04X\n",wr_addr, wr_sz, wr_crc, rd_crc ); + }else{ + DBG_PRINTF( "[%X-%X] write/read crc:%04X\n",wr_addr, wr_sz, wr_crc ); + } + } + break; + case 0xFFA2: + //sflash read addr + { + //uint32_t wr_addr = get_b_u32( &pkt->pdata[0] ); + //uint32_t wr_sz = pkt->dlen - 4; + //buf[0] = user_sflash_write( wr_addr, &pkt->pdata[4], wr_sz )==0?1:0; + //n = wrap_tx_pkt( pkt->cmd, txbuf, pkt->sn, buf, 1 ); + //udp_tx( upcb, ip, port, txbuf, n ); + } + break; + case 0xFFA3: + //sflash read crc + { + uint32_t addr = get_b_u32( &pkt->pdata[0] ); + uint32_t sz = get_b_u32( &pkt->pdata[4] ); + uint32_t crc = get_b_u32( &pkt->pdata[8] ); + uint32_t crc_read = user_sflash_crc16( addr, sz ); + if( crc == crc_read ){ + buf[0] = 1; + debug_printf( "sflash %d--%d, crc16:%08X\n", addr, sz, crc ); + }else{ + buf[0] = 0; + debug_printf( "sflash %d--%d, crc16:[%08X | %08X]\n", addr, sz, crc, crc_read ); + } + n = wrap_tx_pkt( pkt->cmd, txbuf, pkt->sn, buf, 1 ); + udp_tx( upcb, ip, port, txbuf, n ); + } + break; + default: + break; + } +} + +int32_t handle_rx_udp_data( udp_pcb_t *upcb, ip_addr_t * tx_ip, uint16_t tx_port, uint8_t *rx_data, uint32_t rx_sz ) +{ + udp_pkt_t pkt; + int32_t ret = 0; + + ret = get_rx_pkt( rx_data, rx_sz, &pkt ); + if( ret == 1 ){ + print_rx_pkt( "rx:", &pkt ); + handle_rx_pkt( upcb, &pkt, tx_ip, tx_port ); + }else if( ret < 0 ){ + //udp_rx_count = 0; + }else{ + } + return 0; +} + +int32_t udp_recv_cb( udp_pcb_t *upcb, ip_addr_t * tx_ip, uint16_t tx_port, uint8_t *rx_data, uint32_t rx_sz ) +{ + handle_rx_udp_data( upcb, tx_ip, tx_port, rx_data, rx_sz ); + return 0; +} + +void udp_server_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *remote_addr, u16_t remote_port) +{ + g_udp.upcb = upcb; + memcpy( &g_udp.remote_addr, remote_addr, sizeof( ip_addr_t ) ); + g_udp.remote_port = remote_port; + + /* Connect to the remote client */ + udp_connect(upcb, remote_addr, remote_port); + + /* Tell the client that we have accepted it */ + + #if 1 + //todo handle rx_data + uint8_t *rx_data = pvPortMalloc( p->tot_len ); + if( rx_data ){ + if( pbuf_copy_partial( p, rx_data, p->tot_len, 0 ) == p->tot_len ){ + udp_recv_cb(upcb, (ip_addr_t*)remote_addr, remote_port, rx_data, p->tot_len ); + } + vPortFree( rx_data ); + } + #else + //echo client test + udp_send(upcb, p); + #endif + + /* free the UDP connection, so we can accept new clients */ + udp_disconnect(upcb); + + /* Free the p buffer */ + pbuf_free(p); + +} + +void udp_server_start(void) +{ + struct udp_pcb *upcb; + err_t err; + uint16_t udp_port; + + + /* Create a new UDP control block */ + upcb = udp_new(); + + if (upcb) + { + udp_port = 8888; + /* Bind the upcb to the UDP_PORT port */ + /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */ + err = udp_bind(upcb, IP_ADDR_ANY, udp_port); + + if(err == ERR_OK) + { + /* Set a receive callback for the upcb */ + udp_recv(upcb, udp_server_receive_callback, NULL); + //DBG_PRINTF( "start udp server, port:%d\r\n", udp_port ); + } + else + { + udp_remove(upcb); + } + } +} + +void udp_server_init(void) +{ + udp_server_start(); +} + diff --git a/user/user_udp.h b/user/user_udp.h new file mode 100644 index 0000000..2de5f75 --- /dev/null +++ b/user/user_udp.h @@ -0,0 +1,8 @@ +#ifndef __UDP_SERVER_H__ +#define __UDP_SERVER_H__ + + +void udp_server_init(void); + + +#endif /* TCP_SERVER_H */ diff --git a/user/user_wind_sensor.c b/user/user_wind_sensor.c index c487fdb..811988a 100644 --- a/user/user_wind_sensor.c +++ b/user/user_wind_sensor.c @@ -41,15 +41,21 @@ void user_rs485_rx_cb( void ) rs485_rx_flag = 1; } +void rs485_tx_enable( int enabled ) +{ + HAL_GPIO_WritePin( PA11_RS485_DIR1_GPIO_Port, PA11_RS485_DIR1_Pin, enabled?GPIO_PIN_SET:GPIO_PIN_RESET ); +} + int32_t rs485_tx( uint8_t *data, uint16_t sz ) { uint8_t dummy; while( HAL_UART_Receive( &huart1, &dummy, 1, 0 ) == HAL_OK){ }; - + rs485_tx_enable(1); HAL_UART_Transmit( &huart1, data, sz, 100 ); - + osDelay(1); + rs485_tx_enable(0); return 0; } diff --git a/user_debug/test_main.c b/user_debug/test_main.c index c07d22e..9dda25d 100644 --- a/user_debug/test_main.c +++ b/user_debug/test_main.c @@ -2,6 +2,7 @@ #include "shell.h" extern UART_HandleTypeDef huart1; +extern const char SW_VER[16]; #define huart_shell huart1 @@ -59,7 +60,7 @@ short shell_uart_read(char *data, unsigned short len) void version( void ) { - printf( "Version: V%d.%d\n", SW_VER_MAJOR, SW_VER_MINOR ); + printf( "Version: %s\n", SW_VER ); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), version, version, version ); @@ -114,7 +115,7 @@ void user_shell_task(void *argument) shellInit(&shell, shellBuffer, sizeof( shellBuffer)); - printf( "Version:%d.%d\n", SW_VER_MAJOR, SW_VER_MINOR ); + printf( "Version:%s\n", SW_VER ); shell_uart_rx_start(); for(;;){