forked from Koxx3/SmartESC_STM32_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_pwr.c
216 lines (189 loc) · 6.39 KB
/
task_pwr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* m365
*
* Copyright (c) 2021 Francois Deslandes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "task_pwr.h"
#include "task_LED.h"
#include "task_init.h"
#include "main.h"
#include "task.h"
#include "task_cli.h"
#include <string.h>
#include "VescCommand.h"
#include "music.h"
#include "ninebot.h"
#include "conf_general.h"
#include "VescToSTM.h"
#include "app.h"
//Not a real Task... it's called from safety task. No delays allowed
#define EXECUTION_SPEED 40 //every 40 ticks (20ms)
extern m365Answer m365_to_display;
//extern const uint8_t m365_mode[3];
uint32_t shutdown_limit = 0;
uint8_t buttonState() {
static const uint32_t DEBOUNCE_MILLIS = 20 ;
bool buttonstate = HAL_GPIO_ReadPin( PWR_BTN_GPIO_Port, PWR_BTN_Pin ) == GPIO_PIN_SET ;
uint32_t buttonstate_ts = HAL_GetTick() ;
uint32_t now = HAL_GetTick() ;
if( now - buttonstate_ts > DEBOUNCE_MILLIS )
{
if( buttonstate != (HAL_GPIO_ReadPin( PWR_BTN_GPIO_Port, PWR_BTN_Pin ) == GPIO_PIN_SET))
{
buttonstate = !buttonstate ;
buttonstate_ts = now ;
}
}
return buttonstate ;
}
eButtonEvent getButtonEvent()
{
static const uint32_t DOUBLE_GAP_MILLIS_MAX = 250;
static const uint32_t SINGLE_PRESS_MILLIS_MAX = 300;
static const uint32_t LONG_PRESS_MILLIS_MAX = 5000;
static uint32_t button_down_ts = 0 ;
static uint32_t button_up_ts = 0 ;
static bool double_pending = false ;
static bool button_down = false ; ;
eButtonEvent button_event = NO_PRESS ;
uint32_t now = HAL_GetTick() ;
if( button_down != buttonState() ) {
button_down = !button_down ;
if( button_down ) {
button_down_ts = now ;
} else {
button_up_ts = now ;
if( double_pending ) {
button_event = DOUBLE_PRESS ;
double_pending = false ;
}
else {
double_pending = true ;
}
}
}
uint32_t diff = button_up_ts - button_down_ts;
if (!button_down && double_pending && now - button_up_ts > DOUBLE_GAP_MILLIS_MAX) {
double_pending = false ;
button_event = SINGLE_PRESS ;
} else if (!button_down && double_pending && diff >= SINGLE_PRESS_MILLIS_MAX && diff <= LONG_PRESS_MILLIS_MAX) {
double_pending = false ;
button_event = LONG_PRESS ;
} else if (button_down && now - button_down_ts > LONG_PRESS_MILLIS_MAX) {
double_pending = false ;
button_event = VERY_LONG_PRESS ;
}
return button_event ;
}
void PWR_set_shutdown_time(uint32_t seconds){
shutdown_limit = seconds * 2000;
}
//This is not a FreeRTOS Task... its called from safety task to safe some heap space every 500us
void task_PWR(void *argument) {
static uint8_t main_loop_counter = 0;
static uint32_t shutdown_timer = 0;
static uint16_t AWD=0;
if(SpeednTorqCtrlM1.SPD->hAvrMecSpeedUnit){
shutdown_timer = 0;
}else if (shutdown_limit > 0){
shutdown_timer++;
if(shutdown_timer>shutdown_limit){
shutdown_timer=0;
power_control(DEV_PWR_OFF);
}
}
if(main_loop_counter > 40){
main_loop_counter=0;
switch( getButtonEvent() ){
case NO_PRESS : break ;
case SINGLE_PRESS : {
m365_to_display.light = !m365_to_display.light;
if(m365_to_display.light){
task_LED_set_brake_light(BRAKE_LIGHT_ON);
}else{
task_LED_set_brake_light(BRAKE_LIGHT_OFF);
}
} break ;
case LONG_PRESS : {
power_control(DEV_PWR_OFF);
} break ;
case VERY_LONG_PRESS : {
AWD++;
if(AWD==1){
m365_to_display.beep=1;
VescToSTM_set_ADC1(0);
}
else {
AWD=0;
m365_to_display.beep=2;}
} break ;
case DOUBLE_PRESS : {
uint32_t kmh=0;
switch(m365_to_display.mode){
case M365_MODE_DRIVE:
app_adc_speed_mode(M365_MODE_SPORT);
kmh = mc_conf.modes_kmh_limits[2];
mc_conf.lo_current_max_scale = mc_conf.modes_curr_scale[2];
break;
case M365_MODE_SPORT:
app_adc_speed_mode(M365_MODE_SLOW);
kmh = mc_conf.modes_kmh_limits[0];
mc_conf.lo_current_max_scale = mc_conf.modes_curr_scale[0];
break;
case M365_MODE_SLOW:
app_adc_speed_mode(M365_MODE_DRIVE);
kmh = mc_conf.modes_kmh_limits[1];
mc_conf.lo_current_max_scale = mc_conf.modes_curr_scale[1];
break;
}
if(kmh==1337){
mc_conf.lo_max_erpm = mc_conf.l_max_erpm;
}else{
mc_conf.lo_max_erpm = (((float)kmh * 1000.0 / 60.0)/(mc_conf.si_wheel_diameter*M_PI)) * mc_conf.si_motor_poles * mc_conf.si_gear_ratio;
}
MCI_ExecSpeedRamp(pMCI[M1], VescToSTM_erpm_to_speed(mc_conf.lo_max_erpm), 0);
} break ;
}
}
main_loop_counter++;
}
void task_PWR_init(port_str * port) {
/* Check button pressed state at startup */
buttonState();
/* Power ON board temporarily, ultimate decision to keep hardware ON or OFF is made later */
power_control(DEV_PWR_ON);
}
void power_control(uint8_t pwr)
{
if(pwr == DEV_PWR_ON) {
/* Turn the PowerON line high to keep the board powered on even when
* the power button is released
*/
HAL_GPIO_WritePin(TPS_ENA_GPIO_Port, TPS_ENA_Pin, GPIO_PIN_SET);
} else if(pwr == DEV_PWR_OFF) {
vTaskDelay(1);
while(HAL_GPIO_ReadPin(PWR_BTN_GPIO_Port, PWR_BTN_Pin));
HAL_GPIO_WritePin(TPS_ENA_GPIO_Port, TPS_ENA_Pin, GPIO_PIN_RESET);
while(1);
} else if(pwr == DEV_PWR_RESTART) {
/* Restart the system */
NVIC_SystemReset();
}
}