#include "bsp.h" #include "led.h" #include "gw_ctrl.h" #include "lora.h" #include "encoder.h" const char PowerOn_Info[][64]={ {" _ooOoo_"}, {" o8888888o"}, {" 88\" . \"88"}, {" (| -_- |)"}, {" O\\ = /O"}, {" ____/`---'\\____"}, {" . ' \\| |// `."}, {" / \\\\||| : |||// \\"}, {" / _||||| -:- |||||- \\"}, {" | | \\\\ - /// | |"}, {" | \\_| ''\\---/'' | |"}, {" \\ .-\\__ `-` ___/-. /"}, {" ___`. .' /--.--\\ `. . __"}, {" ."" '< `.___\\_<|>_/___.' >'""."}, {" | | : `- \\`.;`\\ _ /`;.`/ - ` : | |"}, {" \\ \\ `-. \\_ __\\ /__ _/ .-` / /"}, {"======`-.____`-.___\\_____/___.-`____.-'======"}, {" `=---='"}, {""}, {"............................................."} }; OS_TMR watch_tmr; task_count_t task_count;//任务运行次数 timeout_t tt_watchdog={0xff,0,(1000)}; device_info_t device_info; device_fixed_info_t device_fixed_info; union data_u Voltage,Temperature; FunctionalState irq_state = ENABLE; timeout_t tt_real_time = { 0xff, 0, REAL_TIME_TIMEOUT, }; uint8_t watchdog_num = 0; //定时喂狗 void watchdog_feed(void) { if(timeout_isOut(&tt_watchdog)) { IWDG_ReloadCounter(); watchdog_num++; if(watchdog_num > 10) { watchdog_num = 0; if(task_count.uart_count_cur != task_count.uart_count_prev) { task_count.uart_count_prev = task_count.uart_count_cur; } else { NVIC_SystemReset(); } if(task_count.lora_count_cur != task_count.lora_count_prev) { task_count.lora_count_prev = task_count.lora_count_cur; } else { NVIC_SystemReset(); } if(task_count.prog_count_cur != task_count.prog_count_prev) { task_count.prog_count_prev = task_count.prog_count_cur; } else { NVIC_SystemReset(); } //离线升级时不判断 if(device_fixed_info.Work_State == DEV_WORK_STATE_UPDATING &&\ ota_state.update_mode == OTA_WORK_STATE_MASTER) {} else { if(task_count.ota_count_cur != task_count.ota_count_prev) { task_count.ota_count_prev = task_count.ota_count_cur; } else { NVIC_SystemReset(); } } if(hd_id == PCB_V1) { if(task_count.encoder_count_cur != task_count.encoder_count_prev) { task_count.encoder_count_prev = task_count.encoder_count_cur; } else { NVIC_SystemReset(); } } } } } static void watchdog_feed_timer(void *p_tmr, void *p_arg) { // static uint8_t led_flag = 0; #if 1 IWDG_ReloadCounter(); #endif // RTC_Get(); } void watchdog_init(uint8_t pre,uint16_t rlr) { OS_ERR err; #if 0 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); IWDG_SetPrescaler(pre); IWDG_SetReload(rlr); IWDG_ReloadCounter(); IWDG_Enable(); #endif OSTmrCreate(&watch_tmr, "watch_tmr", 1, 1, OS_OPT_TMR_PERIODIC, watchdog_feed_timer, NULL, &err); OSTmrStart(&watch_tmr, &err); } void GPIO_COMM_Init(u32 clk, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIOMode_TypeDef mode, GPIOSpeed_TypeDef speed) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(clk, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin; GPIO_InitStructure.GPIO_Mode = mode; GPIO_InitStructure.GPIO_Speed = speed; GPIO_Init(GPIOx, &GPIO_InitStructure); GPIO_SetBits(GPIOx, GPIO_Pin); } void BSP_Init(void) { SystemInit(); SysTick_init(); // watchdog_init(IWDG_Prescaler_256, 4000); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断优先级分组 RTC_Init(); adc_init(); power_on_read();//上电读取数据 HD_ID_Init();//pcb版本引脚初始化 hd_id = get_HD_ID();//读取pcb版本 key_init(); LED_Init();//led初始化 sw_gpio_init();//切换开关初始化 pos_init(); uart_init(UART1_ID, 115200, USART_WordLength_8b, USART_Parity_No); uart_init(UART2_ID, 115200, USART_WordLength_9b, USART_Parity_Even);//加密模块 uart_init(UART3_ID, 115200, USART_WordLength_8b, USART_Parity_No);//显示屏 uart_init(UART4_ID, 9600, USART_WordLength_9b, USART_Parity_Even); uart_init(UART5_ID, 9600, USART_WordLength_9b, USART_Parity_Even); //控制引脚初始化 //RS485_CTL(PA1) GPIO_COMM_Init(RCC_APB2Periph_GPIOA, GPIOA, GPIO_Pin_1, GPIO_Mode_Out_PP, GPIO_Speed_50MHz); RS485_RX(); } void test_flash_crc(void) { // int i; // uint8_t flash_buf[64]={0X01,0X00,0X00,0X00,0X01,0X02,0X00,0X00,0X62,0X00,0X00,0X00,0X01,0X00,0X00,0X00,0X01,0X04,0X00,0X00,0XEC,0X96,0X98,0X00,0X18,0X00,0X00,0X00,0XEC,0X9B,0X06,0X08,0X00,0X00,0X00,0X00,0X4C,0X50,0X53,0X58}; // uint32_t getcrc; // Flash_BufferRead(OTA_UPDATE_APP3_FLASH_START_ADDR,(uint8_t *)flash_buf,64); // Flash_RangeErase(OTA_UPDATE_PT_FLASH_START_ADDR, OTA_UPDATE_PT_FLASH_SIZE); // Flash_BufferWrite(OTA_UPDATE_PT_FLASH_START_ADDR,(uint32_t *)flash_buf,40); // Flash_BufferWrite(OTA_UPDATE_PT_FLASH_START_ADDR+64,(uint32_t *)flash_buf,40); // Flash_BufferWrite(OTA_UPDATE_PT_FLASH_START_ADDR+128,(uint32_t *)flash_buf,64); // Flash_BufferWrite(OTA_UPDATE_PT_FLASH_START_ADDR+2048,(uint32_t *)flash_buf,64); // Flash_BufferWrite(OTA_UPDATE_PT_FLASH_START_ADDR+2048+64,(uint32_t *)flash_buf,64); // data_dump("sd",(uint8_t *)flash_buf,64); // memset(flash_buf,0,64); // Flash_BufferRead(OTA_UPDATE_PT_FLASH_START_ADDR,(uint8_t *)flash_buf,64); // data_dump("addr1",(uint8_t *)flash_buf,64); // memset(flash_buf,0,64); // Flash_BufferRead(OTA_UPDATE_PT_FLASH_START_ADDR+64,(uint8_t *)flash_buf,64); // data_dump("addr2",(uint8_t *)flash_buf,64); // // memset(flash_buf,0,64); // Flash_BufferRead(OTA_UPDATE_PT_FLASH_START_ADDR+2048,(uint8_t *)flash_buf,64); // data_dump("addr2",(uint8_t *)flash_buf,64); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_PT_FLASH_START_ADDR, 40/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc1=%08x\n",getcrc); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)(OTA_UPDATE_PT_FLASH_START_ADDR+64), 40/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc2=%08x\n",getcrc); // // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_PT_FLASH_START_ADDR, 64/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc1=%08x\n",getcrc); // // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)(OTA_UPDATE_PT_FLASH_START_ADDR+128), 64/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc2=%08x\n",getcrc); // // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)(OTA_UPDATE_PT_FLASH_START_ADDR+2048), 64/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc3=%08x\n",getcrc); // // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)(OTA_UPDATE_PT_FLASH_START_ADDR+2048+64), 64/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc4=%08x\n",getcrc); // // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC_ResetDR(); // CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, 64/4); // getcrc = CRC_GetCRC(); // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); // printf("crc5=%08x\n",getcrc); } //按键任务处理函数 void key_task_handle(uint8_t num) { switch(num) { case 1://显示lora信道 loraindex_led_flag = 1; timeout_setValue(&led_info.tt_loraindex_led_state,LED_LORA_INDEX_TIME,1);//设置loraindex灯计时参数 close_all_led(); Data_lastNbit_led(lora_para_choice+1,4,1);//显示Lora组号 key_info[KEY0_PRES-1].execute = 1; break; case 2: // device_fixed_info.Work_State = DEV_WORK_STATE_XSP_UPDATING; // ota_xsp_info.xsp_ota_begin = 1; // ota_xsp_info.xsp_count = 0; // ota_xsp_info.error_num = 0; // ota_xsp_init(); // ota_xsp_info_read_test();//升级显示屏 // test_flash_crc(); break; case 3: break; case 4: break; case 5://离线升级 device_fixed_info.master_updata_start = 1; ota_state.offline_mode = KEY_TRIGGER; break; default: break; } } //比较设备类型 uint8_t device_type_cmp(uint32_t device_fixed, uint16_t device_rcv) { uint8_t ret = 0; uint16_t temp = 0; temp = (device_fixed>>16)&0x0000FFFF; if(temp == device_rcv) { ret = 1; } return ret; } //比较接收设备的类型和sn(sn不能是0xffffffff) int device_type_and_id_cmp_NoBroadcast(uint16_t device_type, uint32_t device_sn) { int ret = 0; if((device_type == device_info.device_type) && (device_sn == device_info.device_sn)) { ret = 1; } return ret; } float Temp_test(void); //读取温度 void Temp_get(void) { Temperature.dataf = Temp_test(); device_fixed_info.Temperature = Temperature.datai; // printf("temperature uint32:%08x\n",device_fixed_info.Temperature); } #define ADC_READ_TIMES 3 u16 get_adc(uint8_t channel, uint8_t rank, uint8_t sample_time); //读电压 float adc_get(void) { OS_ERR err; float vol,temp_vol_sum = 0; uint16_t temp=0,times=0; times = ADC_READ_TIMES; while(times--) { temp = get_adc(ADC_Channel_0, 1, ADC_SampleTime_239Cycles5); vol = (float)temp*3300/4096; //电压值(mV) temp_vol_sum += vol; // printf("ADC_Channel_0 Voltage measurement is : %f mV(%d)\r\n",vol, temp); //串口打印电压值 OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_DLY, &err); } vol = temp_vol_sum / ADC_READ_TIMES; vol /= 1000; // printf("ADC_Channel_0 Voltage measurement is : %f V\r\n",vol); //串口打印电压值 return vol*2; //实际电压应乘2 } //读电压 void Voltage_get(void) { Voltage.dataf = adc_get(); device_fixed_info.Voltage = Voltage.datai; // printf("voltage u32:%08x\n",device_fixed_info.Voltage); } //实时参数读取 void device_real_time_para_read(void) { if(timeout_isOut(&tt_real_time)) { Temp_get();//读温度 Voltage_get();//读电压 } } //中断设置 void device_irq_set(FunctionalState state) { EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //上升沿和下降沿触发 EXTI_InitStructure.EXTI_LineCmd = state; EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器 if(hd_id == PCB_V1) { EXTI_InitStructure.EXTI_Line=EXTI_Line6|EXTI_Line7|EXTI_Line9|EXTI_Line10|EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //上升沿和下降沿触发 EXTI_InitStructure.EXTI_LineCmd = state; EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器 } irq_state = state; if(irq_state==ENABLE) { printf("irq enable\n"); } else { printf("irq disable\n"); } } //运行时中断控制 void run_irq_set(void) { //进入产测关闭中断,退出产测打开中断 //升级时主设备和从设备时关闭中断,进入旁观者和升级状态上报时,打开中断 if(device_fixed_info.Work_State==DEV_WORK_STATE_PT || ((device_fixed_info.Work_State==DEV_WORK_STATE_UPDATING) && ((ota_state.update_mode!=OTA_WORK_STATE_ONLOOKER)&&(ota_state.update_mode!=OTA_WORK_STATE_UPLOAD)))) { if(irq_state==ENABLE) { device_irq_set(DISABLE); } } else { if(irq_state==DISABLE) { device_irq_set(ENABLE); } } } //设备模块复位判断 void device_module_reset(void) { ciphertext_state_isout();//加密模块死机判断 //判断Lora是否超时重启 if(timeout_isOut(&tt_lora_rcv_state)) { timeout_start(&tt_lora_rst_rcv_state,1); printf("lora一小时未收到数据\n"); lora_init();//lora初始化 } //Lora复位后一段时间依旧未接收到数据 if(timeout_isOut(&tt_lora_rst_rcv_state)) { printf("lora复位5分钟后还未收到数据\n"); if(Reset_encoder_judge())//编码器在工作不能复位 device_fixed_info.reset_flag = 1; else NVIC_SystemReset(); } if(device_fixed_info.reset_flag) { if(Reset_encoder_judge()==0) { printf("采集器复位\n"); device_fixed_info.reset_flag = 0; NVIC_SystemReset(); } } //读编码器后2s定时 if(timeout_isOut(&read_encoder_para.tt_encoder)) { read_encoder_para.flag = 0; } } /* ********************************************************************************************************* * BSP_CPU_ClkFreq() * * Description : Read CPU registers to determine the CPU clock frequency of the chip. * 读取cpu寄存器确定时钟频率 * Argument(s) : none. * * Return(s) : The CPU clock frequency, in Hz. 返回CPU始终频率 * * Caller(s) : Application. * * Note(s) : none. ********************************************************************************************************* */ CPU_INT32U BSP_CPU_ClkFreq (void) { RCC_ClocksTypeDef rcc_clocks; RCC_GetClocksFreq(&rcc_clocks); return ((CPU_INT32U)rcc_clocks.HCLK_Frequency); } #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED) void CPU_TS_TmrInit (void) { CPU_INT32U cpu_clk_freq_hz; cpu_clk_freq_hz = BSP_CPU_ClkFreq(); CPU_TS_TmrFreqSet(cpu_clk_freq_hz); } #endif /*$PAGE*/ /* ********************************************************************************************************* * CPU_TS_TmrRd() * * Description : Get current CPU timestamp timer count value. * * Argument(s) : none. * * Return(s) : Timestamp timer count (see Notes #2a & #2b). * * Caller(s) : CPU_TS_Init(), * CPU_TS_Get32(), * CPU_TS_Get64(), * CPU_IntDisMeasStart(), * CPU_IntDisMeasStop(). * * This function is an INTERNAL CPU module function & MUST be implemented by application/ * BSP function(s) [see Note #1] but SHOULD NOT be called by application function(s). * * Note(s) : (1) CPU_TS_TmrRd() is an application/BSP function that MUST be defined by the developer * if either of the following CPU features is enabled : * * (a) CPU timestamps * (b) CPU interrupts disabled time measurements * * See 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #1' * & 'cpu_cfg.h CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION Note #1a'. * * (2) (a) Timer count values MUST be returned via word-size-configurable 'CPU_TS_TMR' * data type. * * (1) If timer has more bits, truncate timer values' higher-order bits greater * than the configured 'CPU_TS_TMR' timestamp timer data type word size. * * (2) Since the timer MUST NOT have less bits than the configured 'CPU_TS_TMR' * timestamp timer data type word size; 'CPU_CFG_TS_TMR_SIZE' MUST be * configured so that ALL bits in 'CPU_TS_TMR' data type are significant. * * In other words, if timer size is not a binary-multiple of 8-bit octets * (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple * octet word size SHOULD be configured (e.g. to 16-bits). However, the * minimum supported word size for CPU timestamp timers is 8-bits. * * See also 'cpu_cfg.h CPU TIMESTAMP CONFIGURATION Note #2' * & 'cpu_core.h CPU TIMESTAMP DATA TYPES Note #1'. * * (b) Timer SHOULD be an 'up' counter whose values increase with each time count. * * (1) If timer is a 'down' counter whose values decrease with each time count, * then the returned timer value MUST be ones-complemented. * * (c) (1) When applicable, the amount of time measured by CPU timestamps is * calculated by either of the following equations : * * (A) Time measured = Number timer counts * Timer period * * where * * Number timer counts Number of timer counts measured * Timer period Timer's period in some units of * (fractional) seconds * Time measured Amount of time measured, in same * units of (fractional) seconds * as the Timer period * * Number timer counts * (B) Time measured = --------------------- * Timer frequency * * where * * Number timer counts Number of timer counts measured * Timer frequency Timer's frequency in some units * of counts per second * Time measured Amount of time measured, in seconds * * (2) Timer period SHOULD be less than the typical measured time but MUST be less * than the maximum measured time; otherwise, timer resolution inadequate to * measure desired times. ********************************************************************************************************* */ //#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED) //CPU_TS_TMR CPU_TS_TmrRd (void) //{ // return (CPU_TS_TMR)0; //} //#endif //#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED) //void CPU_TS_TmrInit2 (void) //{ // CPU_INT32U fclk_freq; // // // fclk_freq = BSP_CPU_ClkFreq(); // // BSP_REG_DEM_CR |= (CPU_INT32U)BSP_BIT_DEM_CR_TRCENA; // BSP_REG_DWT_CYCCNT = (CPU_INT32U)0u; // BSP_REG_DWT_CR |= (CPU_INT32U)BSP_BIT_DWT_CR_CYCCNTENA; // // CPU_TS_TmrFreqSet((CPU_TS_TMR_FREQ)fclk_freq); //} //#endif #if (CPU_CFG_TS_TMR_EN == DEF_ENABLED) CPU_TS_TMR CPU_TS_TmrRd (void) { extern volatile uint32_t TickCounter; return (TickCounter); } #endif //打印系统时钟 void system_clk_read(void) { RCC_ClocksTypeDef get_rcc_Clock; RCC_GetClocksFreq(&get_rcc_Clock); printf("\r\nSYSCLK parameter\r\n"); printf("SSYSCLK_F = %dHz\r\n",get_rcc_Clock.SYSCLK_Frequency); /*!< returns SYSCLK clock frequency expressed in Hz */ printf("HCLK_F = %dHz\r\n",get_rcc_Clock.HCLK_Frequency); /*!< returns HCLK clock frequency expressed in Hz */ printf("PCLK1_F = %dHz\r\n",get_rcc_Clock.PCLK1_Frequency); /*!< returns PCLK1 clock frequency expressed in Hz */ printf("PCLK2_F = %dHz\r\n",get_rcc_Clock.PCLK2_Frequency); /*!< returns PCLK2 clock frequency expressed in Hz */ printf("ADCCLK_F = %dHz\r\n",get_rcc_Clock.ADCCLK_Frequency); /*!< returns ADCCLK clock frequency expressed in Hz */ }