Motion_EC_Stm32_archived/App/Src/th_exti.c

222 lines
6.8 KiB
C
Raw Normal View History

2021-03-14 23:24:59 +08:00
/*
* @Description:
* @Date: 2021-03-10 10:49:52
* @LastEditors: CK.Zh
* @LastEditTime: 2021-03-10 11:57:47
* @FilePath: \NaviKit_EC_stm32\App\Src\th_button.c
*/
#define LOG_TAG "TH-Exti"
#include <th_exti.h>
#include "main.h"
#include "navikit.h"
//Thread
const osThreadAttr_t ExtiServiceTask_attributes = {
.name = "ExtiServiceTask",
.priority = (osPriority_t) osPriorityBelowNormal,
.stack_size = 256 * 4
};
//Timer
const osTimerAttr_t PwrBtnLongPressTimer_attributes = {
.name = "PwrBtnLongPressTimer"
};
const osTimerAttr_t CustBtnLongPressTimer_attributes = {
.name = "CustBtnLongPressTimer"
};
const osTimerAttr_t PwrBtnShortPressTimer_attributes = {
.name = "PwrBtnShortPressTimer"
};
const osTimerAttr_t CustBtnShortPressTimer_attributes = {
.name = "CustBtnShortPressTimer"
};
//Event Flag
const osEventFlagsAttr_t ExtiEventFlags_attributes = {
.name = "ExtiEventFlags"
};
/* USER CODE BEGIN Header_StartExtiServiceTask */
/**
* @brief Function implementing the ExtiServiceTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartExtiServiceTask */
void StartExtiServiceTask(void *argument)
{
/* USER CODE BEGIN StartExtiServiceTask */
/* Infinite loop */
uint32_t thread_flag = 0;
log_d("Start EXTI Service Task");
for(;;)
{
thread_flag = osThreadFlagsGet();
if(thread_flag & SHUTDOWN_REQ){
}
if(thread_flag & MOD_SLEEP){
}
//powwer button
if((thread_flag & PWR_BTN) && !osTimerIsRunning(PwrBtnLongPressTimerHandle)){
osTimerStart(PwrBtnLongPressTimerHandle,LONG_PRESS_TIME);
osTimerStart(PwrBtnShortPressTimerHandle,SHORT_PRESS_TIME);
}else if(!(thread_flag & PWR_BTN) && osTimerIsRunning(PwrBtnLongPressTimerHandle)){
osTimerStop(PwrBtnLongPressTimerHandle);
osTimerStop(PwrBtnShortPressTimerHandle);
}
//custom button
if((thread_flag & CUS_BTN) && !osTimerIsRunning(CustBtnLongPressTimerHandle)) {
osTimerStart(CustBtnLongPressTimerHandle,LONG_PRESS_TIME);
osTimerStart(CustBtnShortPressTimerHandle,SHORT_PRESS_TIME);
}else if(!(thread_flag & CUS_BTN) && osTimerIsRunning(CustBtnLongPressTimerHandle)){
osTimerStop(CustBtnLongPressTimerHandle);
osTimerStop(CustBtnShortPressTimerHandle);
}
// //power button
// if(NaviKit.sys.power_btn && !osTimerIsRunning(PwrBtnLongPressTimerHandle)){
// osTimerStart(PwrBtnLongPressTimerHandle,LONG_PRESS_TIME);
// osTimerStart(PwrBtnShortPressTimerHandle,SHORT_PRESS_TIME);
// }
// osDelay(2);
// if(!NaviKit.sys.power_btn && osTimerIsRunning(PwrBtnLongPressTimerHandle)){
// osTimerStop(PwrBtnLongPressTimerHandle);
// osTimerStop(PwrBtnShortPressTimerHandle);
// }
// osDelay(2);
//custom button
// if(!NaviKit.sys.custom_btn && osTimerIsRunning(CustBtnLongPressTimerHandle)){
// }
// osDelay(2);
// if(NaviKit.sys.custom_btn && !osTimerIsRunning(CustBtnLongPressTimerHandle)){
// }
// osDelay(2);
osDelay(10);
}
/* USER CODE END StartExtiServiceTask */
}
void PwrBtnLongPressTimerCallback(void *argument)
{
log_v("power button long pressed.");
switch(NaviKit.sys.sta){
case run: {NaviKit.sys.next_sta = idle;log_v("change to idle"); }break;//system is run now, user request to idle
case idle: {NaviKit.sys.next_sta = run;log_v("change to run"); }break;//system is idle now , user request to power on
case dfu: {NaviKit.sys.next_sta = idle;log_v("change to idle"); }break;
case sleep:{NaviKit.sys.next_sta = run;log_v("change to run"); }break;
case isp: {NaviKit.sys.next_sta = idle;log_v("change to idle"); }break;
default : break;
}
}
void CustBtnLongPressTimerCallback(void *argument)
{
log_v("custom button long pressed.");
switch(NaviKit.sys.sta){
case run:{//system is run now, user request to reboot SOM
som_reboot(100);
}break;
case idle:{
}break;
case dfu:{
som_reboot(100);
}break;
case sleep:{
}break;
default : break;
}
}
void PwrBtnShortPressTimerCallback(void *argument)
{
log_v("power button short pressed.");
switch(NaviKit.sys.sta){
case run: {//som is running, send sleep requeset to operate system
log_v("Request operate system pop up the shutdown dialog.");
PWR_Enable(SOM_SLEEP,true,100);
PWR_Enable(SOM_SLEEP,false,0);
}break;
case idle: { }break;//system is idle now , user request to power on
case dfu: { }break;
case sleep:{ }break;
case isp: { }break;
default : break;
}
}
void CustBtnShortPressTimerCallback(void *argument)
{
log_v("custom button short pressed.");
switch(NaviKit.sys.sta){
case run:{
}break;
case idle:{
}break;
case dfu:{
}break;
case sleep:{
}break;
default : break;
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
switch (GPIO_Pin){
case SOM_SHUTDOWN_REQ_Pin:{
if(HAL_GPIO_ReadPin(SOM_SHUTDOWN_REQ_GPIO_Port, SOM_SHUTDOWN_REQ_Pin)==GPIO_PIN_RESET){//falling edge trigger
if(NaviKit.sys.sta == run){//if jetson nano shutdown output low,the power_en should be set low less than 10us
PWR_Enable(SOM_PWR_EN,false,0);
NaviKit.sys.next_sta = idle;
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)SHUTDOWN_REQ_ACTIVE);
// log_i("SOM's shutdown_req pin falling edge, SOM request to shutdown.");
}
}else{//Rising edge trigger
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)SHUTDOWN_REQ_INACTIVE);
}
}break;
case SOM_MOD_SLEEP_Pin:{
if(HAL_GPIO_ReadPin(SOM_MOD_SLEEP_GPIO_Port, SOM_MOD_SLEEP_Pin)==GPIO_PIN_SET){//Rising edge trigger
// log_i("SOM's sleep pin rising edge.");
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)MOD_SLEEP_ACTIVE);
}else{//falling edge trigger
// log_v("SOM's sleep pin falling edge.");
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)MOD_SLEEP_INACTIVE);
}
}break;
case SYS_POWER_BTN_Pin:{
if(HAL_GPIO_ReadPin(SYS_POWER_BTN_GPIO_Port, SYS_POWER_BTN_Pin)==GPIO_PIN_SET){//Rising edge trigger
NaviKit.sys.power_btn = true;
// log_v("power_btn status: pressed.");
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)PWR_BTN_ACTIVE);
}else{//falling edge trigger
NaviKit.sys.power_btn = false;
// log_v("power_btn status: released.");
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)PWR_BTN_INACTIVE);
}
}break;
case SYS_CUSTOM_BTN_Pin:{
if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port, SYS_CUSTOM_BTN_Pin)==GPIO_PIN_RESET){//falling edge trigger
// log_v("custom_btn status: pressed.");
NaviKit.sys.custom_btn = true;
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)CUS_BTN_ACTIVE);
}else{//Rising edge trigger
// log_v("custom_btn status: released.");
NaviKit.sys.custom_btn = false;
osThreadFlagsSet(ExtiServiceTaskHandle,(Exti_Flags_t)CUS_BTN_INACTIVE);
}
}break;
}
}