/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
*
© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.
*
* 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
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "iwdg.h"
#include "rtc.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include
#include "init.h"
#include "buffer.h"
#include "flash.h"
#include "usart.h"
#include "button.h"
#include "display.h"
#include "plan.h"
#include "syswatch.h"
#include
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define HARDWARE_VERSION "V1.0.0"
#define SOFTWARE_VERSION "V0.1.0"
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t inMenu = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
static rt_err_t exception_hook(void *context) {
extern long list_thread(void);
uint8_t _continue = 1;
rt_enter_critical();
#ifdef RT_USING_FINSH
list_thread();
#endif
/* 建立深度为 16 的函数调用栈缓冲区,深度大小不应该超过 CMB_CALL_STACK_MAX_DEPTH(默认16) */
uint32_t call_stack[16] = {0};
size_t i, depth = 0;
/* 获取当前环境下的函数调用栈,每个元素将会以 32 位地址形式存储, depth 为函数调用栈实际深度 */
depth = cm_backtrace_call_stack(call_stack, sizeof(call_stack), cmb_get_sp());
/* 输出当前函数调用栈信息
* 注意:查看函数名称及具体行号时,需要使用 addr2line 工具转换
*/
for (i = 0; i < depth; i++) {
printf("%08x ", call_stack[i]);
}
cm_backtrace_fault(*((uint32_t *)(cmb_get_sp() + sizeof(uint32_t) * 8)), cmb_get_sp() + sizeof(uint32_t) * 9);
while (_continue == 1);
return RT_EOK;
}
void fault_test_by_div0(void) {
volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
int x, y, z;
*SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */
x = 10;
y = 0;
z = x / y;
printf("z:%d\n", z);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
extern IWDG_HandleTypeDef hiwdg;
extern unsigned char XbeeConst_FrameNum;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* CmBacktrace initialize */
// cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, SOFTWARE_VERSION);
/* set exception hook */
// rt_hw_exception_install(exception_hook);
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
MX_GPIO_Init();
MX_IWDG_Init();
onbordflash_init();
HAL_IWDG_Refresh(&hiwdg);
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
MX_UART4_Init();
MX_UART5_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
MX_IWDG_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
rt_show_version();
/* 使能备份时钟:备份寄存器 */
__HAL_RCC_BKP_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
XbeeConst_FrameNum = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
if(READ_BIT(RCC->CSR, RCC_CSR_IWDGRSTF) != RESET) //看门狗重启不需要获取时间
{
uint16_t timeval1,timeval2;
timeval1 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
timeval2 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
TimeConst.Buffer[0]=timeval1 & 0xFF;
TimeConst.Buffer[1]=(timeval1>>8) & 0xFF;
TimeConst.Buffer[2]=timeval2 & 0xFF;
TimeConst.Buffer[3]=(timeval2>>8) & 0xFF;
}
thread_init();
WeightConst_Product_Run_time= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
WeightConst_DELAYON= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
WeightConst_DELAYTIME= HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
WeightConst_WeightTargetAllowMin = 32767; //目标重量的允许跳转范围
WeightConst_WeightTargetAllowMax = -32768; //目标重量的允许跳转范围
pf_comp.data.delaytime = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR6);
//WeightConst_WeightCurTem = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR7);
thread_beep_init();
output1_off();
output2_off();
pwr_lock_on();
AT_on();
rt_kprintf("\n \\ | /\n");
rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
// init_allflash();
// beep();
// rt_sem_take(thread_init_sem, RT_WAITING_FOREVER); //等待初始化进程完毕
if (WeightConst_SBType == 4)
{
if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
{
beep();
rt_thread_mdelay(1000);
if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)> 0 && HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
{
init_allflash();
__HAL_RCC_CLEAR_RESET_FLAGS();
beep();
rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
}
else if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0)
{
if (WeightConst_SBType == 6) WeightConst_SBType = 1;
inMenu = 1;
}
}
} else {
if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0 )
{
beep();
rt_thread_mdelay(1000);
if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)> 0 && HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
{
init_allflash();
__HAL_RCC_CLEAR_RESET_FLAGS();
beep();
rt_sem_take(thread_init_sem, RT_WAITING_FOREVER);
}
else if (HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0)
{
if (WeightConst_SBType == 6) WeightConst_SBType = 1;
inMenu = 1;
}
}
}
MX_IWDG_Init();
threadUart_init();
// threadUart5_init();
ReadWeight_thread();
if (inMenu==1)
{
beep();
MenuButtonthread();
threadMenuDisplay_init();
}
else {
Button_thread();
SendWifi_thread();
if (WeightConst_SBType != 2)
{
if (WeightConst_SBType == 4) threadDisplay_delay_init();
delay_key_thread();
threadDisplay_init();
setTIME_thread();
if (WeightConst_DELAYON==0)
setWeightConst_initProduct();
threadRemoteDisplay_init();
sendgetTime();
CheckWeight_thread();
pop_product_thread();
ReadEID_thread();
}
else
{
beep();
CanButton = 1;
}
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
rt_thread_mdelay(500);
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
// HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
// HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
uint8_t rDataBuffer;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == UART5 && isTag>0){
if (isTag>0) {
USART_Push(5, rDataBuffer);
rt_sem_release(thread_EmptyBuffer_sem);
}
HAL_UART_Receive_IT(&huart5, &rDataBuffer, 1);
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/