feat(Puya-PY32F040): add OTA dual-project codegen template

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-18 18:56:19 +08:00
co-authored by Cursor
commit 62eb0199ed
498 changed files with 329164 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
#include "application.h"
#include "iwdg.h"
#include "jb_port.h"
#include "jb_protocol.h"
/*******************状态机应用层开始********************/
void app_machine_handle(void)
{
}
/***************状态机应用层结束************************/
void version_printf(void)
{
appPrintf(LOG_NOTIC, "Developer:%s\r\n", __DEVELOPER__);
appPrintf(LOG_NOTIC, "Email:%s\r\n", __EMAIL__);
appPrintf(LOG_NOTIC, "Version:%s\r\n", __VERSION__);
appPrintf(LOG_NOTIC, "Compiler Date:%s\r\n", __DATE__);
appPrintf(LOG_NOTIC, "Compiler Time:%s\r\n", __TIME__);
}
void app_Init(void)
{
// 系统初始化
DEBUG_USART_Config(); /* 将串口配置成日志口 */
version_printf(); /* 打印版本信息 */
timerInit(); /* TIM6 1ms 节拍,供协议超时/重传 */
jbInit(); /* 协议初始化 */
userInit(); /* 用户初始化 */
uartInit(); /* USART3 PB10/PB119600 8N1 */
}
void app_lication(void)
{
while (1)
{
app_machine_handle(); /* 状态机处理函数 */
jb_port_uart_run(); /* 串口调试日志 */
userHandle(); /* 用户处理函数 */
jbProtocolHandle(&gDevData); /* 协议处理函数 */
iwdg_feed(); /* 看门狗喂狗 */
}
}
+115
View File
@@ -0,0 +1,115 @@
/**
******************************************************************************
* @file main.c
* @author MCU Application Team
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* This software component is licensed by Puya under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "application.h"
#include "app_boot.h"
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t lptim_flag = 0;
/**
* @brief Main program.
* @retval int
*/
int main(void)
{
app_startup();
app_lication();
}
/**
* @brief Period elapsed callback in non blocking mode
* @param htimTIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == JB_TIMER)
{
jbTimerIrq();
}
}
/**
* @brief 计算时间差
* @param meiosis:需要比较的时间戳,用于计算时间差
* @retval 时间差
*/
uint32_t HAL_GetTickDiff(uint32_t meiosis)
{
uint32_t temp = HAL_GetTick();
if (temp >= meiosis)
{
temp = temp - meiosis;
}
else
{
temp = 0xFFFFFFFFU - meiosis + temp;
}
return temp;
}
/**
* @brief 错误处理函数
* @param *file:文件名,line:行号
* @return None
*/
void Error_Handler(uint8_t *file, uint32_t line)
{
while (1)
{
appPrintf(LOG_ERROR, "%s %d\r\n", file, line);
}
}
#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 can add his own implementation to report the file name and line number,
for example: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
+131
View File
@@ -0,0 +1,131 @@
/**
******************************************************************************
* @file py32f040_hal_msp.c
* @author MCU Application Team
* @brief This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* This software component is licensed by Puya under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* External functions --------------------------------------------------------*/
/**
* @brief Initialize global MSP.
*/
void HAL_MspInit(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
}
/**
* @brief UART MSP 初始化
* @param huart UART 句柄
* @retval None
* @note USART3: PB10=TX(AF4), PB11=RX(AF4) — 见 PY32F040 手册 PortB AF 表
*/
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef gpio_init = {0};
if (huart->Instance == JB_USART)
{
JB_USART_CLK_ENABLE();
JB_USART_TX_GPIO_CLK_ENABLE();
JB_USART_RX_GPIO_CLK_ENABLE();
/* PB10 ------> USART3_TX */
gpio_init.Pin = JB_USART_TX_PIN;
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Pull = GPIO_PULLUP;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.Alternate = JB_USART_TX_AF;
HAL_GPIO_Init(JB_USART_TX_GPIO_PORT, &gpio_init);
/* PB11 ------> USART3_RX */
gpio_init.Pin = JB_USART_RX_PIN;
gpio_init.Alternate = JB_USART_RX_AF;
HAL_GPIO_Init(JB_USART_RX_GPIO_PORT, &gpio_init);
HAL_NVIC_SetPriority(JB_USART_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(JB_USART_IRQn);
}
}
/**
* @brief UART MSP 反初始化
* @param huart UART 句柄
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
if (huart->Instance == JB_USART)
{
__HAL_RCC_USART3_CLK_DISABLE();
HAL_GPIO_DeInit(JB_USART_TX_GPIO_PORT, JB_USART_TX_PIN);
HAL_GPIO_DeInit(JB_USART_RX_GPIO_PORT, JB_USART_RX_PIN);
HAL_NVIC_DisableIRQ(JB_USART_IRQn);
}
}
/**
* @brief TIM Base MSP 初始化
* @param htim TIM 句柄
* @retval None
* @note TIM6 用作协议层 1ms 节拍
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
if (htim->Instance == JB_TIMER)
{
JB_TIMER_CLK_ENABLE();
HAL_NVIC_SetPriority(JB_TIMER_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(JB_TIMER_IRQn);
}
}
/**
* @brief TIM Base MSP 反初始化
* @param htim TIM 句柄
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
{
if (htim->Instance == JB_TIMER)
{
__HAL_RCC_TIM6_CLK_DISABLE();
HAL_NVIC_DisableIRQ(JB_TIMER_IRQn);
}
}
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
+109
View File
@@ -0,0 +1,109 @@
/**
******************************************************************************
* @file py32f040_it.c
* @author MCU Application Team
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* This software component is licensed by Puya under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "py32f040_it.h"
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* External variables --------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M0+ Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
while (1)
{
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
HAL_IncTick();
}
/******************************************************************************/
/* PY32F040 Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file. */
/******************************************************************************/
/**
* @brief USART3/4 中断服务:协议口收发
*/
void USART3_4_IRQHandler(void)
{
HAL_UART_IRQHandler(&UartHandle);
}
/**
* @brief TIM6/LPTIM1 中断服务:1ms 节拍
*/
void TIM6_LPTIM1_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TimHandle);
}
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
+154
View File
@@ -0,0 +1,154 @@
/**
******************************************************************************
* @file system_py32f040.c
* @author MCU Application Team
* @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* This software component is licensed by Puya under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#include "py32f0xx.h"
#if !defined(HSE_VALUE)
#define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined(HSI_VALUE)
#define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
#if !defined(LSI_VALUE)
#define LSI_VALUE 32768U /*!< Value of LSI in Hz*/
#endif /* LSI_VALUE */
#if !defined(LSE_VALUE)
#define LSE_VALUE 32768U /*!< Value of LSE in Hz*/
#endif /* LSE_VALUE */
/************************* Miscellaneous Configuration ************************/
/*!< Uncomment the following line if you need to relocate your vector Table in
Internal SRAM. */
/* #define FORBID_VECT_TAB_MIGRATION */
/* #define VECT_TAB_SRAM */
#define VECT_TAB_OFFSET 0x3000 /*!< Vector Table base offset field. \
APP 链接在 0x08003000, 偏移须为 0x3000 (标准 CMSIS 用户配置项) \
This value must be a multiple of 0x100. */
/******************************************************************************/
/*----------------------------------------------------------------------------
Clock Variable definitions
*----------------------------------------------------------------------------*/
/* This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock; then there
is no need to call the 2 first functions listed above, since SystemCoreClock
variable is updated automatically.
*/
uint32_t SystemCoreClock = HSI_VALUE;
const uint32_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
const uint32_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
const uint32_t HSIFreqTable[8] = {4000000U, 8000000U, 16000000U, 22120000U, 24000000U, 4000000U, 4000000U, 4000000U};
/**
* @brief Clock functions.
* @param none
* @return none
*/
void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
{
uint32_t tmp;
uint32_t hsidiv;
uint32_t hsifs;
#if defined(RCC_PLL_SUPPORT)
uint32_t pllmul = 0;
const uint8_t pllMulValue[4] = {2, 3, 2, 2};
#endif /* RCC_PLL_SUPPORT */
/* Get SYSCLK source -------------------------------------------------------*/
switch (RCC->CFGR & RCC_CFGR_SWS)
{
case RCC_SYSCLKSOURCE_STATUS_HSE: /* HSE used as system clock */
SystemCoreClock = HSE_VALUE;
break;
case RCC_SYSCLKSOURCE_STATUS_LSI: /* LSI used as system clock */
SystemCoreClock = LSI_VALUE;
break;
#if defined(RCC_LSE_SUPPORT)
case RCC_SYSCLKSOURCE_STATUS_LSE: /* LSE used as system clock */
SystemCoreClock = LSE_VALUE;
break;
#endif /* RCC_LSE_SUPPORT */
#if defined(RCC_PLL_SUPPORT)
case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock */
pllmul = pllMulValue[((RCC->PLLCFGR & RCC_PLLCFGR_PLLMUL) >> RCC_PLLCFGR_PLLMUL_Pos)];
if ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI) /* HSI used as PLL clock source */
{
hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
SystemCoreClock = pllmul * (HSIFreqTable[hsifs]);
}
else /* HSE used as PLL clock source */
{
SystemCoreClock = pllmul * HSE_VALUE;
}
break;
#endif /* RCC_PLL_SUPPORT */
case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock */
default: /* HSI used as system clock */
hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
SystemCoreClock = (HSIFreqTable[hsifs] / hsidiv);
break;
}
/* Compute HCLK clock frequency --------------------------------------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
/* HCLK clock frequency */
SystemCoreClock >>= tmp;
}
/**
* @brief Setup the microcontroller system.
* Initialize the System.
* @param none
* @return none
*/
void SystemInit(void)
{
/* Set the HSI clock to 8MHz by default */
/* Set the LSI clock to 32.768KHz by default */
RCC->ICSCR = (RCC->ICSCR & 0xFE000000) | (0x1 << 13) | ((*(uint32_t *)(0x1fff3208)) & 0x0000FFFF) | ((*(uint32_t *)(0x1fff3348)) << 16);
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif /* VECT_TAB_SRAM */
}
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/