-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpower.cpp
More file actions
306 lines (254 loc) · 7.59 KB
/
power.cpp
File metadata and controls
306 lines (254 loc) · 7.59 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Authors (alphabetical order)
* - Andre Bernet <bernet.andre@gmail.com>
* - Bertrand Songis <bsongis@gmail.com>
* - Bryan J. Rentoul (Gruvin) <gruvin@gmail.com>
* - Cameron Weeks <th9xer@gmail.com>
* - Erez Raviv
* - Jean-Pierre Parisy
* - Karl Szmutny <shadow@privy.de>
* - Michael Blandford
* - Michal Hlavinka
* - Pat Mackenzie
* - Philip Moss
* - Rob Thomson
* - Romolo Manfredini <romolo.manfredini@gmail.com>
* - Thomas Husterer
*
* open9x is based on code named
* gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/,
* er9x by Erez Raviv: http://code.google.com/p/er9x/,
* and the original (and ongoing) project by
* Thomas Husterer, th9x: http://code.google.com/p/th9x/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <stdint.h>
#ifdef PCBSKY
#include "AT91SAM3S4.h"
#endif
#if ( defined(PCBX9D) || defined(PCB9XT) )
#include "stm32f2xx_gpio.h"
#include "stm32f2xx_rcc.h"
#include "hal.h"
#endif
#include "radio.h"
#include "logicio.h"
#if (defined(REV9E) || defined(PCBX7) || defined(PCBXLITE) || defined(PCBX9LITE))
#define POWER_STATE_OFF 0
#define POWER_STATE_START 1
#define POWER_STATE_RUNNING 2
#define POWER_STATE_STOPPING 3
#define POWER_STATE_STOPPED 4
uint8_t PowerState = POWER_STATE_OFF ;
#ifdef PCBT12
uint8_t PowerCount ;
#endif
#endif
#ifdef PCBSKY
void init_soft_power()
{
// Configure RF_power (PC17)
configure_pins( PIO_PC17, PIN_ENABLE | PIN_INPUT | PIN_PORTC | PIN_NO_PULLUP | PIN_PULLDOWN ) ;
configure_pins( PIO_PA8, PIN_ENABLE | PIN_INPUT | PIN_PORTA | PIN_PULLUP ) ;
}
// Returns zero if power is switched off
// 1 if power switch is on
// 2 if power switch off, trainer power on
uint32_t check_soft_power()
{
if ( PIOC->PIO_PDSR & PIO_PC17 ) // Power on
{
return POWER_ON ;
}
return POWER_OFF ;
}
// turn off soft power
void soft_power_off()
{
configure_pins( PIO_PA8, PIN_ENABLE | PIN_OUTPUT | PIN_LOW | PIN_PORTA | PIN_NO_PULLUP ) ;
}
#endif
#ifdef PCBX9D
void soft_power_off()
{
GPIO_ResetBits(GPIOPWR,PIN_MCU_PWR);
}
uint32_t check_soft_power()
{
#if (defined(REV9E) || defined(PCBX7) || defined(PCBXLITE) || defined(PCBX9LITE))
uint32_t switchValue ;
#endif
#if (defined(REV9E) || defined(PCBX7) || defined(PCBXLITE) || defined(PCBX9LITE))
#if defined(PCBXLITE) || defined(PCBX9LITE)
switchValue = GPIO_ReadInputDataBit(GPIOPWRSENSE, PIN_PWR_STATUS) == Bit_RESET ;
#else
switchValue = GPIO_ReadInputDataBit(GPIOPWR, PIN_PWR_STATUS) == Bit_RESET ;
#endif
switch ( PowerState )
{
case POWER_STATE_OFF :
default :
PowerState = POWER_STATE_START ;
#ifdef PCBT12
PowerCount = 0 ;
#endif
return POWER_ON ;
break ;
case POWER_STATE_START :
if ( !switchValue )
{
PowerState = POWER_STATE_RUNNING ;
}
return POWER_ON ;
break ;
case POWER_STATE_RUNNING :
if ( switchValue )
{
#ifdef PCBT12
if ( ++PowerCount > 20 )
{
PowerState = POWER_STATE_STOPPING ;
return POWER_X9E_STOP ;
}
else
{
return POWER_ON ;
}
#endif
PowerState = POWER_STATE_STOPPING ;
return POWER_X9E_STOP ;
}
#ifdef PCBT12
else
{
PowerCount = 0 ;
}
#endif
return POWER_ON ;
break ;
case POWER_STATE_STOPPING :
if ( !switchValue )
{
PowerState = POWER_STATE_STOPPED ;
}
return POWER_OFF ;
break ;
case POWER_STATE_STOPPED :
return POWER_OFF ;
break ;
}
#else // REV9E
if (GPIO_ReadInputDataBit(GPIOPWR, PIN_PWR_STATUS) == Bit_RESET)
return POWER_ON;
else
return POWER_OFF;
#endif
}
void init_soft_power()
{
#if defined(PCBXLITE)
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN ; // Enable portA clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN ; // Enable portD clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOEEN ; // Enable portE clock
GPIO_ResetBits(GPIOPWRINT, PIN_INT_RF_PWR );
GPIO_ResetBits(GPIOPWREXT, PIN_EXT_RF_PWR);
GPIO_ResetBits(GPIOPWRSPORT, PIN_SPORT_PWR);
configure_pins( PIN_INT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTD ) ;
configure_pins( PIN_EXT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTD ) ;
configure_pins( PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTE ) ;
configure_pins( PIN_PWR_STATUS, PIN_INPUT | PIN_PORTA ) ;
#else
#ifdef PCBX9LITE
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN ; // Enable portA clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN ; // Enable portD clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOEEN ; // Enable portE clock
GPIO_ResetBits(GPIOPWRINT, PIN_INT_RF_PWR );
GPIO_ResetBits(GPIOPWREXT, PIN_EXT_RF_PWR);
GPIO_ResetBits(GPIOPWRSPORT, PIN_SPORT_PWR);
configure_pins( PIN_INT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTA ) ;
configure_pins( PIN_EXT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTA ) ;
configure_pins( PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTA ) ;
configure_pins( PIN_SPORT_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTE ) ;
configure_pins( PIN_PWR_STATUS, PIN_INPUT | PIN_PORTA ) ;
#else // X3
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN ; // Enable portD clock
#ifdef REVPLUS
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN ; // Enable portD clock
#endif
#ifdef PCBX7
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN ; // Enable portC clock
#endif
GPIO_ResetBits(GPIOPWRINT, PIN_INT_RF_PWR );
GPIO_ResetBits(GPIOPWREXT, PIN_EXT_RF_PWR);
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOPWR, ENABLE);
/* GPIO Configuration*/
#ifdef REVPLUS
configure_pins( PIN_INT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTC ) ;
configure_pins( PIN_EXT_RF_PWR | PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTD ) ;
#else
#ifdef PCBX7
configure_pins( PIN_INT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTC ) ;
configure_pins( PIN_EXT_RF_PWR | PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTD ) ;
#else
configure_pins( PIN_INT_RF_PWR | PIN_EXT_RF_PWR | PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTD ) ;
#endif
#endif
configure_pins( PIN_PWR_STATUS, PIN_INPUT | PIN_PULLUP | PIN_PORTD ) ;
configure_pins( PIN_TRNDET, PIN_INPUT | PIN_PULLUP | PIN_PORTA ) ;
// Soft power ON
#endif
#endif // X3
GPIO_SetBits(GPIOPWR,PIN_MCU_PWR);
#if (defined(REV9E) || defined(PCBX7) || defined(PCBXLITE) || defined(PCBX9LITE))
PowerState = POWER_STATE_START ;
#endif
}
#endif
#ifdef PCB9XT
void soft_power_off()
{
configure_pins( PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTC ) ;
GPIO_ResetBits(GPIOPWR,PIN_MCU_PWR) ;
}
uint32_t check_soft_power()
{
static uint32_t c1 = 0 ;
// static uint32_t c2 = 0 ;
uint16_t value = GPIOC->IDR ;
if ( value & PIN_PWR_STATUS )
{
c1 = 0 ;
return POWER_ON ;
}
else
{
c1 += 1 ;
if ( c1 > 50 )
{
return POWER_OFF;
}
return POWER_ON ;
}
}
void init_soft_power()
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN ; // Enable portD clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN ; // Enable portC clock
GPIO_ResetBits(GPIOPWRINT, PIN_INT_RF_PWR );
GPIO_ResetBits(GPIOPWREXT, PIN_EXT_RF_PWR);
/* GPIO Configuration*/
configure_pins( PIN_INT_RF_PWR | PIN_MCU_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTC ) ;
configure_pins( PIN_EXT_RF_PWR, PIN_OUTPUT | PIN_PUSHPULL | PIN_OS25 | PIN_PORTD ) ;
configure_pins( PIN_PWR_STATUS, PIN_INPUT | PIN_PORTC ) ;
configure_pins( PIN_MCU_PWR, PIN_INPUT | PIN_PULLUP | PIN_PORTC ) ;
}
#endif