/**
* @file main.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#define HSE_VALUE (40000000)
#include <stdio.h>
#include "main.h"
#include "sys_var.h"
#include "key.h"
#include "n32g43x_gpio.h"
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* USART configuration */
USART_Configuration();
/* TIM Configuration */
TIM_Configuration();
DelayMs(200);//待上电稳定
ADC_Initial();
//检测上电类型
if(1)
{
/* Check if the system has resumed from solft reset */
if (RCC_GetFlagStatus(RCC_CTRLSTS_FLAG_SFTRSTF) != RESET)
{
/* solft reset flag set */
printf("\n\rsystem solft reset\n\r\n\r");
/* Clear reset flags */
RCC_ClrFlag();
}
else
{
/* WWDGRST flag is not set */
printf("\n\rnormal power on\n\r\n\r");
}
}
PrintSystemClock();
gpio_func_init();
printf("\n\r seat 8 io project\n\rbuild datetime : %s %s\n\r",__DATE__,__TIME__);
//printf("\n\rwhile\n\r");
strength_Led_state = 0;
while (1)
{
/* running led*/
{
if(IsTimeOut(&LedTimer))
{
TimeOutSet(&LedTimer,1000);
GpioToggle(LED);
}
}
#ifdef FY_1KEY_3LED
/* mode led */
set_led_mode(mode_state);
/* key led */
GpioSetOut(LED5,1);
// if(!GpioGet(KEY6))
// {
// GpioSetOut(LED5,1);
// }
// else
// {
// GpioSetOut(LED5,0);
// }
#endif
/* key func */
//if (IsTimeOut(&KeyScanTimer))
{
keyScan();
if( key_mode == KEY_MODE_ADC)
{
AdcKeyFunc();
//TimeOutSet(&KeyScanTimer,50);
}
if( key_mode == KEY_MODE_GPIO)
{
GpioKeyFunc();
//TimeOutSet(&KeyScanTimer,80);
}
}
}
}
/**
* @}
*/
/**
* @}
*/
#include "app.h"
#include "key.h"
#include "sys_var.h"
#ifdef FY_1KEY_3LED
void set_led_mode(uint8_t mode)
{
if(mode == 0x1)
{
GpioSetOut(LED4,1);
GpioSetOut(LED3,0);
GpioSetOut(LED2,0);
}
else if(mode == 0x2)
{
GpioSetOut(LED4,1);
GpioSetOut(LED3,1);
GpioSetOut(LED2,0);
}
else if(mode == 0x3)
{
GpioSetOut(LED4,1);
GpioSetOut(LED3,1);
GpioSetOut(LED2,1);
}
else
{
GpioSetOut(LED4,0);
GpioSetOut(LED3,0);
GpioSetOut(LED2,0);
}
}
#endif
void air_bag_init(void)
{
GpioInitOut( SW1 );
GpioInitOut( SW2 );
GpioInitOut( SW3 );
GpioInitOut( SW4 );
GpioInitOut( SW5 );
GpioInitOut( SW6 );
GpioInitOut( SW7 );
GpioInitOut( SW8 );
GpioInitOut( SW9 );
GpioInitOut( SW10 );
//GpioInitOut( SW11 );
//GpioInitOut( SW12 );
GpioInitOut( POWER_FB );
GpioInitOut( POWER_EN );
GpioInitOut( MOTOR_PWM);
GpioSetOut( SW1 ,0);
GpioSetOut( SW2 ,0);
GpioSetOut( SW3 ,0);
GpioSetOut( SW4 ,0);
GpioSetOut( SW5 ,0);
GpioSetOut( SW6 ,0);
GpioSetOut( SW7 ,0);
GpioSetOut( SW8 ,0);
GpioSetOut( SW9 ,0);
GpioSetOut( SW10 ,0);
//GpioSetOut( SW11 ,0);
//GpioSetOut( SW12 ,0);
GpioSetOut( POWER_FB ,1);
GpioSetOut( POWER_EN ,0);
GpioSetOut( MOTOR_PWM,0);
}
void air_bag_idle(void)
{
POWER_OFF;
MOTOR_OFF;
GpioSetOut( SW1 ,0);
GpioSetOut( SW2 ,0);
GpioSetOut( SW3 ,0);
GpioSetOut( SW4 ,0);
GpioSetOut( SW5 ,0);
GpioSetOut( SW6 ,0);
GpioSetOut( SW7 ,0);
GpioSetOut( SW8 ,0);
GpioSetOut( SW9 ,0);
GpioSetOut( SW10 ,0);
//GpioSetOut( SW11 ,0);
//GpioSetOut( SW12 ,0);
}
void air_bag_out(void)
{
GpioSetOut( SW1 ,0);
GpioSetOut( SW2 ,0);
GpioSetOut( SW3 ,0);
GpioSetOut( SW4 ,0);
GpioSetOut( SW5 ,0);
GpioSetOut( SW6 ,0);
GpioSetOut( SW7 ,0);
GpioSetOut( SW8 ,0);
GpioSetOut( SW9 ,0);
GpioSetOut( SW10 ,0);
}
void gpio_func_init(void)
{
#ifdef FY_1KEY_3LED
GpioInitIn(KEY6);
#elif defined(FY_1KEY_NOLED)
GpioInitIn(KEY4);
#endif
#ifdef FY_1KEY_3LED
GpioInitOut(LED);
GpioSetOut(LED,0);
GpioInitOut(LED2);
GpioSetOut(LED2,0);
GpioInitOut(LED3);
GpioSetOut(LED3,0);
GpioInitOut(LED4);
GpioSetOut(LED4,0);
GpioInitOut(LED5);
GpioSetOut(LED5,0);
#endif
air_bag_init();
}
void AdcKeyFunc(void)
{
static unsigned char last_adckey_code = 0;
static unsigned char last_adckey_type = 0;
if(( last_adckey_code != key.KeyEventCode || last_adckey_type != key.KeyEventType)
)
{
if(key.KeyEventCode > 0 && key.KeyEventType > 0 )
printf("\n\rADC key KeyEventCode : %d KeyEventType : %d \n\r",key.KeyEventCode , key.KeyEventType);
if( key.KeyEventCode == ADC_KEY_CODE_UP && key.KeyScanState >= KEYSCANSTATE_HOLD)
{
{
//上充下放
POWER_ON;
MOTOR_ON;
#ifdef FY_SEAT_10
WAIST_1_IN;
WAIST_2_OUT;
#endif
air_bag_out();
}
air_bag_ctrl.current_mode = SEAT_FUNC_MODE_IDLE;
TimeOutSet(&Out_15S_Before_Stop_Timer,0);
mode_state = 0;
}
else if( key.KeyEventCode == ADC_KEY_CODE_DOWN && key.KeyScanState >= KEYSCANSTATE_HOLD)
{
{
//下充上放
POWER_ON;
MOTOR_ON;
#ifdef FY_SEAT_10
WAIST_2_IN;
WAIST_1_OUT;
#endif
air_bag_out();
}
air_bag_ctrl.current_mode = SEAT_FUNC_MODE_IDLE;
TimeOutSet(&Out_15S_Before_Stop_Timer,0);
mode_state = 0;
}
else if( key.KeyEventCode == ADC_KEY_CODE_LEFT && key.KeyScanState >= KEYSCANSTATE_HOLD)
{
{
//全充
POWER_ON;
MOTOR_ON;
#ifdef FY_SEAT_10
WAIST_1_IN;
WAIST_2_IN;
#endif
air_bag_out();
}
air_bag_ctrl.current_mode = SEAT_FUNC_MODE_IDLE;
TimeOutSet(&Out_15S_Before_Stop_Timer,0);
mode_state = 0;
}
else if( key.KeyEventCode == ADC_KEY_CODE_RIGHT && key.KeyScanState >= KEYSCANSTATE_HOLD)
{
{
//全放
POWER_ON;
MOTOR_OFF;
#ifdef FY_SEAT_10
WAIST_1_OUT;
WAIST_2_OUT;
#endif
air_bag_out();
}
air_bag_ctrl.current_mode = SEAT_FUNC_MODE_IDLE;
TimeOutSet(&Out_15S_Before_Stop_Timer,0);
mode_state = 0;
}
else if( key.KeyEventCode == ADC_KEY_CODE_OK)
{
if( key.KeyEventType == KEYTYPE_HOLD)
{
printf("\n\rkey hold\n\r");
TimeOutSet(&ModeSwitch_Timer,1000);
if(mode_state)
{
mode_state = 0;
}
else
{
mode_state = 1;
}
air_bag_ctrl.current_mode = mode_state;
air_bag_ctrl.schedule_table_pntr = air_bag_schedule_table[air_bag_ctrl.current_mode];
air_bag_ctrl.current_schedule_index = 0;
air_bag_ctrl.last_schedule_index = 0;
air_bag_ctrl.current_schedule_duration = 0;
TimeOutSet(&StopWork_Timer,work_mode_total_duration[mode_state]);
statck_init();
air_bag_idle();
statck_push(&air_bag_ctrl.schedule_table_pntr[0]);
printf(" air_bag_ctrl.current_mode : %d\r\n",air_bag_ctrl.current_mode);
}
else if( key.KeyEventType == KEYTYPE_SINGLE)
{
//printf("\n\radc key down \n\r");
TimeOutSet(&ModeSwitch_Timer,1000);
if(mode_state)
{
mode_state = (mode_state + 1) % SEAT_FUNC_MODE_AMOUNT ;
if(mode_state == 0)
mode_state = 1;
}
air_bag_ctrl.current_mode = mode_state;
air_bag_ctrl.schedule_table_pntr = air_bag_schedule_table[air_bag_ctrl.current_mode];
air_bag_ctrl.current_schedule_index = 0;
air_bag_ctrl.last_schedule_index = 0;
air_bag_ctrl.current_schedule_duration = 0;
TimeOutSet(&StopWork_Timer,work_mode_total_duration[mode_state]);
statck_init();
air_bag_init();
statck_push(&air_bag_ctrl.schedule_table_pntr[0]);
printf(" air_bag_ctrl.current_mode : %d\r\n",air_bag_ctrl.current_mode);
}
#ifdef FY_SEAT_10
WAIST_1_IDLE;
WAIST_2_IDLE;
#endif
}
else
{
//无按键动作
if(!IsTimeOut(&Out_15S_Before_Stop_Timer))
{
{
//放气15S
POWER_ON;
MOTOR_OFF;
air_bag_idle();
}
}
else if(mode_state == 0)
{
//闭气
air_bag_idle();
POWER_OFF;
MOTOR_OFF;
#ifdef FY_SEAT_10
WAIST_1_IDLE;
WAIST_2_IDLE;
#endif
}
}
last_adckey_code = key.KeyEventCode;
last_adckey_type = key.KeyEventType;
}
// else if(mode_state) //循环工作中
// {
// if( key.KeyEventCode == ADC_KEY_CODE_OK && key.KeyEventType == KEYTYPE_LONGPRES)
// {
// //退出循环工作
// air_bag_ctrl.current_mode = SEAT_FUNC_MODE_IDLE;
// air_bag_ctrl.autoWorkFlag = 0;
// TimeOutSet(&Out_15S_Before_Stop_Timer,SECOND_15);
// }
// }
}
void GpioKeyFunc(void)
{
static unsigned char last_key_code = 0;
static unsigned char last_key_type = 0;
/* LED KEY */
if(last_key_code != key.KeyEventCode || last_key_type != key.KeyEventType)
{
if(key.KeyEventCode > 0 && key.KeyEventType > 0 )
printf("\n\rGPIO key KeyEventCode : %d KeyEventType : %d \n\r",key.KeyEventCode , key.KeyEventType);
if( key.KeyEventCode == GPIO_KEY_CODE_K6 && key.KeyEventType == KEYTYPE_SINGLE)
{
//printf("\n\rgpio key down \n\r");
TimeOutSet(&ModeSwitch_Timer,1000);
//if(mode_state)
{
mode_state = (mode_state + 1) % 4 ;
}
air_bag_ctrl.current_mode = mode_state;
air_bag_ctrl.schedule_table_pntr = air_bag_schedule_table[air_bag_ctrl.current_mode];
air_bag_ctrl.current_schedule_index = 0;
air_bag_ctrl.last_schedule_index = 0;
air_bag_ctrl.current_schedule_duration = 0;
TimeOutSet(&StopWork_Timer,work_mode_total_duration[mode_state]);
statck_init();
air_bag_init();
statck_push(&air_bag_ctrl.schedule_table_pntr[0]);
// printf(" air_bag_ctrl.current_mode : %d\r\n",air_bag_ctrl.current_mode);
}
last_key_code = key.KeyEventCode;
last_key_type = key.KeyEventType;
}
}
/**
* @file n32g43x_it.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include <stdio.h>
#include "n32g43x_it.h"
#include "bsp.h"
#include "sys_var.h"
#include "util.h"
extern uint8_t mode_state;
/** @addtogroup N32G43X_StdPeriph_Template
* @{
*/
/******************************************************************************/
/* Cortex-M4 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Memory Manage exception.
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
*/
void SysTick_Handler(void)
{
}
/**
* @brief This function handles TIM2 update interrupt request.
*/
void TIM2_IRQHandler(void)
{
if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) != RESET) //1ms 定时中断
{
TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);//重置定时器
//软定时器滴答
TimerTickHandler();
if( (air_bag_ctrl.current_mode != SEAT_FUNC_MODE_IDLE && !IsTimeOut(&StopWork_Timer) && IsTimeOut(&ModeSwitch_Timer) ))
{
int stack_idx;
for(stack_idx = 0;stack_idx < STACK_SIZE;stack_idx++)
{
if (schedule_stack[stack_idx].IsUsed)
{
if (schedule_stack[stack_idx].InDuration > 0)
{
//充气
POWER_ON;
air_bag_set_in(schedule_stack[stack_idx].OpAirBagGroup);
schedule_stack[stack_idx].InDuration --;
}
else
{
if (schedule_stack[stack_idx].OutDuration > 0)
{
//放气
POWER_ON;
air_bag_set_out(schedule_stack[stack_idx].OpAirBagGroup);
schedule_stack[stack_idx].OutDuration --;
if (schedule_stack[stack_idx].OutDuration == 0)
{
//停止放气
//放气完毕,出栈
//printf("out end idx %d\r\n",schedule_stack[stack_idx].sch_idx);
statck_pop(stack_idx);
if(0)
{
int i;
for (i = 0; i < STACK_SIZE; i++)
{
//printf(" st_idx : %d is_used : %d sch_idx : %d InD :%d OutD :%d NexD : %d \r\n",i,schedule_stack[i].IsUsed,schedule_stack[i].sch_idx,schedule_stack[i].InDuration,schedule_stack[i].OutDuration,schedule_stack[i].NextOpDuration);
}
}
}
}
}
if (schedule_stack[stack_idx].NextOpDuration > 0)
{
schedule_stack[stack_idx].NextOpDuration --;
//下一动作入栈
if (schedule_stack[stack_idx].NextOpDuration == 0)
{
if(schedule_stack[stack_idx].sch_idx + 1 <= schedule_stack[stack_idx].sch_idx_max)
{
statck_push(&air_bag_ctrl.schedule_table_pntr[schedule_stack[stack_idx].sch_idx+1]);
//printf("next op idx %d\r\n",schedule_stack[stack_idx].sch_idx + 1);
{
int i;
for (i = 0; i < STACK_SIZE; i++)
{
//printf(" st_idx : %d is_used : %d sch_idx : %d InD :%d OutD :%d NexD : %d \r\n",i,schedule_stack[i].IsUsed,schedule_stack[i].sch_idx,schedule_stack[i].InDuration,schedule_stack[i].OutDuration,schedule_stack[i].NextOpDuration);
}
}
}
else
{
statck_push(&air_bag_ctrl.schedule_table_pntr[0]);
//printf("cycle end return 0 idx %d\r\n",schedule_stack[stack_idx].sch_idx);
{
int i;
for (i = 0; i < STACK_SIZE; i++)
{
//printf(" st_idx : %d is_used : %d sch_idx : %d InD :%d OutD :%d NexD : %d \r\n",i,schedule_stack[i].IsUsed,schedule_stack[i].sch_idx,schedule_stack[i].InDuration,schedule_stack[i].OutDuration,schedule_stack[i].NextOpDuration);
}
}
}
}
}
if(schedule_stack[stack_idx].InDuration == 0 &&
schedule_stack[stack_idx].OutDuration == 0 &&
schedule_stack[stack_idx].NextOpDuration == 0)
{
statck_pop(stack_idx);
}
}
}
}
else if( (air_bag_ctrl.current_mode != SEAT_FUNC_MODE_IDLE && IsTimeOut(&StopWork_Timer)))
{
statck_init();
air_bag_idle();
mode_state = 0;
air_bag_ctrl.current_mode = SEAT_FUNC_MODE_IDLE;
printf(" timeout air_bag_ctrl.current_mode : %d\r\n",air_bag_ctrl.current_mode);
}
}
}
/**
* @brief This function handles lin usart interrupt request.
*/
void USART_LIN_S_IRQHandler(void)
{
}
void EXTI15_10_IRQHandler(void)
{
#ifdef CFG_LOW_POWER_MODE
if (EXTI_GetITStatus(LPM_WAKEUP_LINE) != RESET)
{
/* Clear the Key Button EXTI line pending bit */
EXTI_ClrITPendBit(LPM_WAKEUP_LINE);
}
#endif
}
/**
* @}
*/