M26/app/user/appTask.c

528 lines
10 KiB
C
Raw Permalink Normal View History

2024-01-08 17:02:46 +08:00
/**
* @file appTask.c
* @author sunbeam
* @brief
* @version 0.1
* @date 2023-11-14
*
* @copyright Copyright (c) 2023
*
*/
#include "appTask.h"
#include "iodefine.h"
#include "r_cg_adc.h"
#include "PINdef.h"
#include "hwCtrl.h"
#include "MotorCtrl.h"
#include "r_cg_wdt.h"
#include "r_cg_timer.h"
2024-01-20 20:02:40 +08:00
#include "buzzer.h"
2024-01-08 17:02:46 +08:00
static uint8_t EEL_BUF[50];
unsigned char IGN_Voltage_error_flag, IGN_Voltage_error_count;
unsigned char Timer_1ms_flag;
unsigned char Timer_5ms_flag;
unsigned char Timer_10ms_flag;
unsigned char Timer_20ms_flag;
unsigned char Timer_50ms_flag;
unsigned char Timer_1000ms_flag;
/***************************************************/
static unsigned int EEL_SAVE_CNT_DOWN_TIMER;
static uint8_t EEL_SAVE_ENABLE;
MotorStateEE_Type MotorStateEE;
MotorStateEE_Type *pEE;
void MotorCtrl(void);
void HallDetecte(void);
2024-10-25 23:30:26 +08:00
void fw_tp_ctrl(void);
void js_ctrl(void);
2024-06-02 21:12:07 +08:00
2024-01-08 17:02:46 +08:00
extern uint8_t OC1flag,OC2flag,OC3flag;
uint8_t TfState,JrState;
unsigned char keybyte1,keybyte2,keybyte3;
2024-10-25 23:30:26 +08:00
uint8_t fw_tp_pressed,fw_tp_stop;
uint16_t fw_tp_cnt;
uint8_t js_pressed;
uint16_t js_cnt;
#define PLAN1 0
#define PLAN2 1
2024-11-16 20:20:41 +08:00
#define M26B_PLAN PLAN2
2024-10-25 23:30:26 +08:00
2024-01-08 17:02:46 +08:00
void Apply_task(void)
{
//static uint8_t temp;
if (Timer_1ms_flag == 1)
{
Timer_1ms_flag = 0;
KeyScan();
HallDetecte();
CurrentDetecte();
if (EEL_SAVE_CNT_DOWN_TIMER > 0)
{
EEL_SAVE_CNT_DOWN_TIMER--;
}
}
if (Timer_5ms_flag == 1)
{
Timer_5ms_flag = 0;
KeyPro();
}
if (Timer_10ms_flag == 1)
{
Timer_10ms_flag = 0;
MotorCtrl();
2024-01-20 20:02:40 +08:00
buzzer_task();
2024-01-08 17:02:46 +08:00
}
if (Timer_20ms_flag == 1)
{
Timer_20ms_flag = 0;
2024-06-02 21:12:07 +08:00
R_WDT_Restart();
2024-01-08 17:02:46 +08:00
}
if (Timer_50ms_flag == 1)
{
Timer_50ms_flag = 0;
2024-10-25 23:30:26 +08:00
fw_tp_ctrl();
js_ctrl();
2024-01-08 17:02:46 +08:00
}
if (Timer_1000ms_flag == 1)
{
Timer_1000ms_flag = 0;
//temp = !temp;
//MOTOR1Ctrl(temp);
}
}
2024-01-20 20:02:40 +08:00
2024-01-08 17:02:46 +08:00
void KeyPressLogic(uint8_t keyid)
{
switch (keyid)
{
2024-05-27 22:12:50 +08:00
case KEYID_KB_F:
setMotorState(MOTOR1,ACT_XQ);
break;
case KEYID_KB_R:
setMotorState(MOTOR1,ACT_XH);
break;
case KEYID_HG_F:
2024-01-08 17:02:46 +08:00
setMotorState(MOTOR2,ACT_XQ);
break;
2024-05-27 22:12:50 +08:00
case KEYID_HG_R:
2024-01-08 17:02:46 +08:00
setMotorState(MOTOR2,ACT_XH);
break;
2024-11-16 20:56:29 +08:00
case KEYID_ZD_UP:
setMotorState(MOTOR1,ACT_XQ);
setMotorState(MOTOR2,ACT_XQ);
break;
case KEYID_ZD_DOWN:
setMotorState(MOTOR1,ACT_XH);
setMotorState(MOTOR2,ACT_XH);
break;
2024-05-27 22:12:50 +08:00
case KEYID_TT_UP:
2024-10-24 08:29:57 +08:00
setMotorState(MOTOR3,ACT_XQ);
2024-01-08 17:02:46 +08:00
break;
2024-05-27 22:12:50 +08:00
case KEYID_TT_DOWN:
2024-10-24 08:29:57 +08:00
setMotorState(MOTOR3,ACT_XH);
2024-01-08 17:02:46 +08:00
break;
2024-10-24 08:29:57 +08:00
case KEYID_JS:
2024-10-25 23:30:26 +08:00
js_pressed = 1;
2024-06-04 07:31:17 +08:00
//buzzer_start();
2024-10-24 08:29:57 +08:00
//TP_act();
2024-05-27 22:12:50 +08:00
break;
case KEYID_TP:
2024-06-04 07:31:17 +08:00
//buzzer_start();
2024-10-25 23:30:26 +08:00
//FW_act();
fw_tp_pressed = 1;
2024-05-27 22:12:50 +08:00
break;
case KEYID_JY_SET:
2024-01-08 17:02:46 +08:00
MotorMemoryKeyMMPress();
break;
2024-05-27 22:12:50 +08:00
case KEYID_JY_1:
2024-01-08 17:02:46 +08:00
break;
2024-05-27 22:12:50 +08:00
case KEYID_JY_2:
2024-01-08 17:02:46 +08:00
break;
2024-05-27 22:12:50 +08:00
case KEYID_JY_3:
2024-01-08 17:02:46 +08:00
break;
default:
break;
}
}
void KeyReleaseLogic(uint8_t keyid)
{
switch (keyid)
{
2024-05-27 22:12:50 +08:00
case KEYID_KB_F:
setMotorState(MOTOR1,ACT_NOACT);
break;
case KEYID_KB_R:
setMotorState(MOTOR1,ACT_NOACT);
break;
case KEYID_HG_F:
2024-01-08 17:02:46 +08:00
setMotorState(MOTOR2,ACT_NOACT);
break;
2024-05-27 22:12:50 +08:00
case KEYID_HG_R:
2024-01-08 17:02:46 +08:00
setMotorState(MOTOR2,ACT_NOACT);
break;
2024-11-16 20:56:29 +08:00
case KEYID_ZD_UP:
setMotorState(MOTOR1,ACT_NOACT);
setMotorState(MOTOR2,ACT_NOACT);
break;
case KEYID_ZD_DOWN:
setMotorState(MOTOR1,ACT_NOACT);
setMotorState(MOTOR2,ACT_NOACT);
break;
2024-05-27 22:12:50 +08:00
case KEYID_TT_UP:
2024-10-24 08:29:57 +08:00
setMotorState(MOTOR3,ACT_NOACT);
2024-01-08 17:02:46 +08:00
break;
2024-05-27 22:12:50 +08:00
case KEYID_TT_DOWN:
2024-10-24 08:29:57 +08:00
setMotorState(MOTOR3,ACT_NOACT);
2024-01-08 17:02:46 +08:00
break;
2024-10-24 08:29:57 +08:00
case KEYID_JS:
2024-10-25 23:30:26 +08:00
js_pressed = 0;
2024-05-27 22:12:50 +08:00
break;
case KEYID_TP:
2024-10-25 23:30:26 +08:00
fw_tp_pressed = 0;
2024-05-27 22:12:50 +08:00
break;
2024-01-20 20:02:40 +08:00
2024-05-27 22:12:50 +08:00
case KEYID_JY_SET:
2024-01-08 17:02:46 +08:00
MotorMemoryKeyMMRelease();
break;
2024-05-27 22:12:50 +08:00
case KEYID_JY_1:
2024-01-08 17:02:46 +08:00
MotorMemoryKeyM1Release();
break;
2024-05-27 22:12:50 +08:00
case KEYID_JY_2:
2024-01-08 17:02:46 +08:00
MotorMemoryKeyM2Release();
break;
2024-05-27 22:12:50 +08:00
case KEYID_JY_3:
2024-01-08 17:02:46 +08:00
MotorMemoryKeyM3Release();
break;
default:
break;
}
}
void KeyPro(void)
{
uint8_t keyid;
for (keyid = 0; keyid < KEY_NUM; keyid++)
{
if (getKeyPressFlag(keyid))
{
2024-01-20 20:02:40 +08:00
KeyPressLogic(keyid);
2024-01-08 17:02:46 +08:00
StopAutoCal();
2024-10-25 23:30:26 +08:00
fw_tp_stop = 1;
2024-01-08 17:02:46 +08:00
}
if (getKeyReleaseFlag(keyid))
{
2024-01-20 20:02:40 +08:00
KeyReleaseLogic(keyid);
2024-01-08 17:02:46 +08:00
}
}
}
void EEL_READ(void)
{
uint16_t checksum;
uint8_t *src,*des,i;
2024-06-02 21:12:07 +08:00
ReadFlashData(EEL_BUF, 0x000F1000, sizeof(MotorStateEE));
2024-01-08 17:02:46 +08:00
pEE = (MotorStateEE_Type *)EEL_BUF;
checksum = pEE->kbsoft1 + pEE->kbsoft2 + pEE->kbnow + pEE->zysoft1 + pEE->zysoft2 + pEE->zynow;
if (pEE ->start == 0x55AA && pEE->stop == 0xAA55 && checksum == pEE->checksum)
{
des = &MotorStateEE;
src = EEL_BUF;
for (i = 0; i < sizeof(MotorStateEE); i++)
{
des[i] = src[i];
}
}
}
uint16_t SupplyVoltage;
void IGN_Voltage_Detect(void)
{
uint32_t adval;
adval = getAdval(ADCH_BAT);
SupplyVoltage = (adval*57*5)>>10;
if (adval <= 305 )
{
IGN_Voltage_error_count++;
if (IGN_Voltage_error_count >= 250)
{
IGN_Voltage_error_count = 0;
IGN_Voltage_error_flag = 1;
}
}
else if (adval >= 592 )//16.5
{
IGN_Voltage_error_count++;
if (IGN_Voltage_error_count >= 250)
{
IGN_Voltage_error_count = 0;
IGN_Voltage_error_flag = 2;
}
}
else if (adval >= 323 && adval <= 574)
{
IGN_Voltage_error_count = 0;
IGN_Voltage_error_flag = 0;
}
if (IGN_Voltage_error_flag != 0)
{
// TAU0_Channel1_ChangeDuty(0);
}
}
void value_init(void)
{
R_TAU0_Channel0_Start();
2024-05-27 22:12:50 +08:00
2024-01-08 17:02:46 +08:00
R_ADC_Start();
R_ADC_Set_OperationOn();
2024-05-27 22:12:50 +08:00
R_TAU0_Channel1_Start();
2024-01-08 17:02:46 +08:00
MotorValueInit();
2024-05-27 22:11:54 +08:00
buzzer_init();
//buzzer_start();
2024-01-08 17:02:46 +08:00
}
void Timer_Pro(void)
{
static unsigned int Timer_1ms_tick_count;
Timer_1ms_tick_count++;
Timer_1ms_flag=1;
if(Timer_1ms_tick_count%5==0)
{
Timer_5ms_flag=1;
}
if(Timer_1ms_tick_count%10==0)
{
Timer_10ms_flag=1;
}
if(Timer_1ms_tick_count%20==0)
{
Timer_20ms_flag=1;
}
if(Timer_1ms_tick_count%50==0)
{
Timer_50ms_flag=1;
}
if(Timer_1ms_tick_count%1000==0)
{
Timer_1000ms_flag = 1;
}
if(Timer_1ms_tick_count>=5000)
{
Timer_1ms_tick_count=0;
}
}
2024-10-25 23:30:26 +08:00
#define FW_TP_STATE_IDLE 0
#define FW_TP_STATE_START 1
#define FW_TP_STATE_RUNNING 2
#define FW_TP_STATE_STOP 3
void fw_tp_ctrl(void)//50ms
{
static uint8_t fw_tp_state,fw_tp_switch;
if (fw_tp_pressed)
{
fw_tp_cnt++;
2024-10-27 15:57:10 +08:00
if (fw_tp_cnt >= 60)//5s
2024-10-25 23:30:26 +08:00
{
fw_tp_cnt = 0;
fw_tp_pressed = 0;
fw_tp_state = FW_TP_STATE_START;
}
}
else
{
fw_tp_cnt = 0;
}
switch (fw_tp_state)
{
case FW_TP_STATE_IDLE:
break;
case FW_TP_STATE_START:
if (fw_tp_switch)
{
2024-10-27 15:57:10 +08:00
FW_act();
2024-10-25 23:30:26 +08:00
}
else
{
2024-10-27 15:57:10 +08:00
TP_act();
2024-10-25 23:30:26 +08:00
}
fw_tp_switch = !fw_tp_switch;
fw_tp_stop = 0;
2024-10-27 15:57:10 +08:00
fw_tp_state = FW_TP_STATE_RUNNING;
2024-10-25 23:30:26 +08:00
break;
case FW_TP_STATE_RUNNING:
2024-10-27 15:57:10 +08:00
if (fw_tp_stop == 1 || fw_tp_pressed == 1)
2024-10-25 23:30:26 +08:00
{
fw_tp_state = FW_TP_STATE_STOP;
fw_tp_stop = 0;
2024-10-27 15:57:10 +08:00
fw_tp_pressed = 0;
2024-10-25 23:30:26 +08:00
}
2024-10-27 15:57:10 +08:00
if (getMotorTarget(MOTOR1) == 0 &&
getMotorTarget(MOTOR2) == 0 &&
getMotorTarget(MOTOR3) == 0 )
{
fw_tp_state = FW_TP_STATE_STOP;
}
2024-10-25 23:30:26 +08:00
break;
case FW_TP_STATE_STOP:
2024-10-27 15:57:10 +08:00
fw_tp_cnt = 0;
fw_tp_pressed = 0;
//setMotorState(MOTOR1,ACT_NOACT);
//setMotorState(MOTOR2,ACT_NOACT);
//setMotorState(MOTOR3,ACT_NOACT);
setMotorTarget(MOTOR1,0);
setMotorTarget(MOTOR2,0);
setMotorTarget(MOTOR3,0);
2024-10-25 23:30:26 +08:00
fw_tp_state = FW_TP_STATE_IDLE;
break;
default:
break;
}
}
2024-01-08 17:02:46 +08:00
2024-10-25 23:30:26 +08:00
#define JS_STATE_IDLE 0
#define JS_STATE_START 1
#define JS_STATE_RUNNING 2
#define JS_STATE_STOP 3
void js_ctrl(void)//50ms
{
static uint8_t js_state=JS_STATE_IDLE;
static uint16_t js_running_cnt;
2024-10-27 15:57:10 +08:00
static uint8_t motor1_flag = 0;
2024-10-25 23:30:26 +08:00
switch (js_state)
{
case JS_STATE_IDLE:
if (js_pressed == 1)
{
if (js_cnt < 0xFFFF)
{
js_cnt++;
}
if (js_cnt >= 60)//3s
{
js_pressed = 0;
//todo
#if M26B_PLAN == PLAN2
setJSMomory();
#endif
}
}
else
{
if (js_cnt <= 20 && js_cnt > 0)
{
js_state = JS_STATE_START;
//setMotorState(MOTOR4,ACT_XQ);
js_running_cnt = 0;
}
js_cnt = 0;
}
break;
case JS_STATE_START:
#if M26B_PLAN == PLAN1
//setMotorState(MOTOR1,ACT_XQ);
2024-10-27 15:57:10 +08:00
motor1_flag = 0;
2024-10-25 23:30:26 +08:00
setMotorState(MOTOR2,ACT_XQ);
2024-10-27 15:57:10 +08:00
setMotorState(MOTOR3,ACT_XQ);
2024-10-25 23:30:26 +08:00
#else
gotoJSLoc();
#endif
js_state = JS_STATE_RUNNING;
js_running_cnt = 0;
break;
case JS_STATE_RUNNING:
#if M26B_PLAN == PLAN1
2024-10-27 15:57:10 +08:00
if (getMotorState(MOTOR2) == ACT_NOACT && motor1_flag == 0)
2024-10-25 23:30:26 +08:00
{
setMotorState(MOTOR1,ACT_XQ);
2024-10-27 15:57:10 +08:00
motor1_flag = 1;
2024-10-25 23:30:26 +08:00
}
#endif
js_running_cnt++;
if (js_running_cnt < 40)//2s
{
MOTOR4Ctrl(ACT_XQ);
}
2024-10-27 15:57:10 +08:00
else if (js_running_cnt >= 40 && js_running_cnt < 1000)//2-50s
2024-10-25 23:30:26 +08:00
{
MOTOR4Ctrl(ACT_NOACT);
}
2024-10-27 15:57:10 +08:00
else if (js_running_cnt >= 1000 && js_running_cnt < 1020)//50-52s
2024-10-25 23:30:26 +08:00
{
MOTOR4Ctrl(ACT_XH);
}
else
{
MOTOR4Ctrl(ACT_NOACT);
js_state = JS_STATE_STOP;
}
if (js_pressed == 1)
{
2024-10-27 15:57:10 +08:00
js_pressed = 0;
js_running_cnt = 999;
//js_state = JS_STATE_STOP;
2024-10-25 23:30:26 +08:00
setMotorState(MOTOR1,ACT_NOACT);
setMotorState(MOTOR2,ACT_NOACT);
setMotorState(MOTOR3,ACT_NOACT);
}
break;
case JS_STATE_STOP:
MOTOR4Ctrl(ACT_NOACT);
js_state = JS_STATE_IDLE;
break;
default:
break;
}
}
2024-01-08 17:02:46 +08:00