| | |
| | | /* Private define ------------------------------------------------------------*/ |
| | | /* USER CODE BEGIN PD */ |
| | | //#define DEBUG_MODE |
| | | #define Period (uint32_t) 32768 |
| | | #define Period (uint32_t) 32768 - 1 |
| | | #define Timeout (uint32_t) 32768 - 1//(32768 - 1) |
| | | /* USER CODE END PD */ |
| | | |
| | |
| | | /* Private user code ---------------------------------------------------------*/ |
| | | /* USER CODE BEGIN 0 */ |
| | | uint32_t dev_id; |
| | | extern uint8_t g_start_sync_flag; |
| | | uint16_t heartbeat_timer,poll_timer,sync_timer; |
| | | uint8_t aRxBuffer[1]; |
| | | |
| | | void IdleTask(void) |
| | | uint8_t aRxBuffer[1],group_id; |
| | | uint8_t bat_percent=0,g_start_send_flag; |
| | | extern u8 motor_state; |
| | | uint16_t tyncpoll_time,lpsettime; |
| | | uint16_t slottime,max_slotpos; |
| | | uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time; |
| | | extern uint8_t module_power; |
| | | uint8_t imu_enable,motor_enable; |
| | | u16 GetLPTime(void) |
| | | { |
| | | g_start_sync_flag=0; |
| | | u16 count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | return count*LPTIMER_LSB/1000; |
| | | } |
| | | void IdleTask(void) |
| | | {u16 current_time; |
| | | |
| | | UART_CheckReceive(); |
| | | UART_CheckSend(); |
| | | |
| | |
| | | { |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | if(!HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin)) |
| | | { |
| | | LED_LR_ON; |
| | | }else{ |
| | | LED_LR_OFF; |
| | | //if(!HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin)) |
| | | //{ |
| | | // LED_TR_ON; |
| | | // LED_TB_OFF; |
| | | //}else{ |
| | | // LED_TR_OFF; |
| | | current_time=GetLPTime(); |
| | | //motor_state=2; |
| | | switch(motor_state) |
| | | {case 0: |
| | | MOTOR_OFF; |
| | | break; |
| | | case 1: |
| | | if(current_time<MOTOR_ONTIME) |
| | | { |
| | | MOTOR_ON; |
| | | }else{ |
| | | MOTOR_OFF; |
| | | } |
| | | |
| | | break; |
| | | case 2: |
| | | MOTOR_ON; |
| | | break; |
| | | case 3: |
| | | MOTOR_OFF; |
| | | break; |
| | | } |
| | | if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin)) |
| | | { |
| | | if(bat_percent>99) |
| | | { |
| | | LED_TR_OFF; |
| | | LED_TB_ON; |
| | | }else{ |
| | | LED_TR_ON; |
| | | LED_TB_OFF; |
| | | } |
| | | }else{ |
| | | LED_TR_OFF; |
| | | LED_TB_OFF; |
| | | // } |
| | | } |
| | | if(g_com_map[CNT_UPDATE]==1) |
| | | { |
| | |
| | | // Delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | if(g_com_map[CNT_REBOOT]==1) |
| | | { |
| | | g_com_map[CNT_REBOOT]=0; |
| | | g_com_map[MAP_SIGN_INDEX]=0; |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | if(g_com_map[CNT_RESTART]==1) |
| | | { |
| | | g_com_map[CNT_RESTART]=0; |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | |
| | | } |
| | | |
| | | uint16_t tyncpoll_time,lpsettime; |
| | | uint16_t slottime,max_slotpos; |
| | | uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency; |
| | | |
| | | u16 total_slotnum,current_slotnum; |
| | | extern int32_t offsettimeus; |
| | | |
| | | void SetNextPollTime(u16 time) |
| | | { |
| | | int32_t lpcount,lptime,target_time; |
| | | |
| | | time=0; |
| | | if(current_slotnum>=total_slotnum) |
| | | current_slotnum-=total_slotnum; |
| | | |
| | | target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000; |
| | | //if(target_time<90000) |
| | | { |
| | | lptime=target_time-offsettimeus; |
| | | lpcount = lptime/LPTIMER_LSB; |
| | | if(lpcount>LPTIMER_1S_COUNT) |
| | | lpcount-=LPTIMER_1S_COUNT; |
| | | if(lpcount<0) |
| | | { |
| | | lpcount+=LPTIMER_1S_COUNT; |
| | | } |
| | | __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount); |
| | | } |
| | | } |
| | | void Program_Init(void) |
| | | { float temp; |
| | | uint16_t temp2; |
| | |
| | | Usart1ParseDataCallback = UsartParseDataHandler; |
| | | parameter_init(); |
| | | //deca_sleep(1000); |
| | | g_com_map[VERSION] = 0x0107; |
| | | g_com_map[VERSION] = 0x010A; |
| | | #ifdef DEBUG_MODE |
| | | g_com_map[DEV_ROLE]=1; |
| | | g_com_map[DEV_ID]=1; |
| | | g_com_map[COM_INTERVAL]=1000; |
| | | g_com_map[MAX_REPORT_ANC_NUM]=1; |
| | | // g_com_map[DEV_ROLE]=1; |
| | | // g_com_map[DEV_ID]=1; |
| | | g_com_map[COM_INTERVAL]=100; |
| | | // g_com_map[MAX_REPORT_ANC_NUM]=1; |
| | | g_com_map[NOMOVESLEEP_TIME]=10; |
| | | #endif |
| | | |
| | | |
| | | module_power = g_com_map[POWER]; |
| | | imu_enable=g_com_map[IMU_ENABLE]; |
| | | motor_enable=g_com_map[MOTOR_ENABLE]; |
| | | |
| | | |
| | | group_id=g_com_map[GROUP_ID]; |
| | | dev_id = g_com_map[DEV_ID]; |
| | | slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*4/3); |
| | | slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]/4)+4; |
| | | max_slotpos=g_com_map[COM_INTERVAL]/slottime; |
| | | tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime; |
| | | |
| | | slot_startcount=(float)(tyncpoll_time+10)*1000/LPTIMER_LSB; |
| | | lastpoll_count=slot_startcount; |
| | | interval_count=(float)g_com_map[COM_INTERVAL]*1000/LPTIMER_LSB; |
| | | slot_startcount=tyncpoll_time*1000/LPTIMER_LSB; |
| | | lastpoll_time=tyncpoll_time; |
| | | tag_frequency = 1000/g_com_map[COM_INTERVAL]; |
| | | total_slotnum = 1000/g_com_map[COM_INTERVAL]; |
| | | current_slotnum=1; |
| | | current_time=GetLPTime(); |
| | | if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, slot_startcount) != HAL_OK) //system time is 1010ms 1 puls=30.518us |
| | | { |
| | | Error_Handler(); |
| | |
| | | /* Enable the fast wake up from Ultra low power mode */ |
| | | HAL_PWREx_EnableFastWakeUp(); |
| | | } |
| | | void mcu_sleep(void); |
| | | uint32_t temp; |
| | | extern uint8_t sleep_flag,m_bEUARTTxEn; |
| | | uint16_t waitusart_timer; |
| | | uint8_t bat_percent=0; |
| | | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) |
| | | { |
| | | /* Set transmission flag: trasfer complete*/ |
| | | waitusart_timer = tag_frequency*USART_KEEPWAKE_TIME; |
| | | |
| | | if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE)) |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | } |
| | | //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) |
| | | //{ |
| | | // /* Set transmission flag: trasfer complete*/ |
| | | // waitusart_timer = tag_frequency*USART_KEEPWAKE_TIME; |
| | | // |
| | | |
| | | //} |
| | | void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) |
| | | { |
| | | //SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | |
| | | dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN); |
| | | dwt_entersleep(); |
| | | waitusart_timer=1; |
| | | DW_DISABLE; |
| | | |
| | | // HAL_Delay(2000); |
| | | // mcu_sleep(); |
| | | //LED0_BLINK; |
| | | // SystemPower_Config(); |
| | | //HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); |
| | |
| | | { static uint16_t blink_count=0; |
| | | SystemClock_Config(); |
| | | g_start_send_flag = 0; |
| | | |
| | | // if(current_count>slot_startcount&¤t_count<slot_startcount+interval_count) |
| | | // { |
| | | // HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET); |
| | | // }else{ |
| | | // HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_RESET); |
| | | // } |
| | | // LED_TR_BLINK; |
| | | |
| | | bat_percent=Get_Battary(); |
| | | if(bat_percent>15) |
| | | { LED0_ON; |
| | |
| | | } |
| | | if(m_bEUARTTxEn==0&&waitusart_timer==0) |
| | | { |
| | | HAL_UART_DMAStop(&huart1); |
| | | if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 1) != HAL_OK) |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | // HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); |
| | | } |
| | | |
| | |
| | | }else{ |
| | | IdleTask(); |
| | | } |
| | | //HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); |
| | | |
| | | // HAL_Delay(100); |
| | | // Get_Battary(); |
| | | |
| | |
| | | Error_Handler(); |
| | | } |
| | | /* USER CODE BEGIN LPTIM1_Init 2 */ |
| | | if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, Period, Timeout) != HAL_OK) |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | // if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, Period, Timeout) != HAL_OK) |
| | | // { |
| | | // Error_Handler(); |
| | | // } |
| | | /* USER CODE END LPTIM1_Init 2 */ |
| | | |
| | | } |
| | |
| | | } |
| | | /* USER CODE BEGIN USART1_Init 2 */ |
| | | |
| | | if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 1) != HAL_OK) |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | // if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 1) != HAL_OK) |
| | | // { |
| | | // Error_Handler(); |
| | | // } |
| | | if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE)) |
| | | { |
| | | Error_Handler(); |
| | | } |
| | | Usart1InitVariables(); |
| | | /* USER CODE END USART1_Init 2 */ |
| | | |
| | |
| | | __HAL_RCC_GPIOB_CLK_ENABLE(); |
| | | |
| | | /*Configure GPIO pin Output Level */ |
| | | HAL_GPIO_WritePin(GPIOA, SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET); |
| | | HAL_GPIO_WritePin(GPIOA, DW_CTRL_Pin|SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET); |
| | | |
| | | /*Configure GPIO pin Output Level */ |
| | | HAL_GPIO_WritePin(GPIOB, MOTOR_Pin, GPIO_PIN_SET); |
| | | |
| | | /*Configure GPIO pin Output Level */ |
| | | HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|LED1_G_Pin|LED1_R_Pin|LED2_R_Pin, GPIO_PIN_RESET); |
| | | |
| | | /*Configure GPIO pin Output Level */ |
| | | HAL_GPIO_WritePin(MOTOR_GPIO_Port, MOTOR_Pin, GPIO_PIN_SET); |
| | | |
| | | /*Configure GPIO pin Output Level */ |
| | | HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin, GPIO_PIN_RESET); |
| | | HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|LED2_G_Pin, GPIO_PIN_RESET); |
| | | |
| | | /*Configure GPIO pin : PA0 */ |
| | | GPIO_InitStruct.Pin = GPIO_PIN_0; |
| | |
| | | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| | | |
| | | /*Configure GPIO pin : SPI_CS_Pin */ |
| | | GPIO_InitStruct.Pin = SPI_CS_Pin; |
| | | GPIO_InitStruct.Pin = SPI_CS_Pin|GPIO_PIN_9; |
| | | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| | | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| | | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| | |
| | | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| | | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| | | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| | | |
| | | /*Configure GPIO pin : INPUT_5V_Pin */ |
| | | GPIO_InitStruct.Pin = INPUT_5V_Pin; |
| | | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| | | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
| | | HAL_GPIO_Init(INPUT_5V_GPIO_Port, &GPIO_InitStruct); |
| | | |
| | | /*Configure GPIO pins : DW_WKUP_Pin DW_CTRL_Pin LED2_G_Pin */ |
| | | GPIO_InitStruct.Pin = DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin; |
| | |
| | | static uint8_t i=0; |
| | | i++; |
| | | g_start_send_flag=1; |
| | | lastpoll_count+=interval_count; |
| | | if(lastpoll_count>LPTIMER_1S_COUNT-30) |
| | | { |
| | | lastpoll_count = slot_startcount; |
| | | } |
| | | __HAL_LPTIM_COMPARE_SET(&hlptim1, lastpoll_count); |
| | | |
| | | current_slotnum++; |
| | | SetNextPollTime(tyncpoll_time); |
| | | nomove_count+=(float)g_com_map[COM_INTERVAL]/1000; |
| | | if(nomove_count>g_com_map[NOMOVESLEEP_TIME]) |
| | | if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable) |
| | | { |
| | | // mcu_sleep(); |
| | | mcu_sleep(); |
| | | } |
| | | } |
| | | /* USER CODE END 4 */ |