Skip to content

Commit 048017e

Browse files
committed
Add theoretical-ish ALARM handling
1 parent 06f1cf4 commit 048017e

6 files changed

Lines changed: 60 additions & 18 deletions

File tree

fw/rbcx-coprocessor/include/Bsp_v10.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ inline void reinitEspStrappingPins() {
178178
}
179179

180180
inline void pinsInit() {
181-
pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
181+
// pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
182182

183-
pinWrite(powerPin, 1);
184-
pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
183+
// pinWrite(powerPin, 1);
184+
// pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
185185

186186
for (auto button : buttonPin)
187187
pinInit(button, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_SPEED_FREQ_LOW);

fw/rbcx-coprocessor/include/Bsp_v11.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ inline void reinitEspStrappingPins() {
175175
}
176176

177177
inline void pinsInit() {
178-
pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
178+
// pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
179179

180-
pinWrite(powerPin, 1);
181-
pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
180+
// pinWrite(powerPin, 1);
181+
// pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
182182

183183
for (auto button : buttonPin)
184184
pinInit(button, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_SPEED_FREQ_LOW);

fw/rbcx-coprocessor/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ platform = ststm32@~7.0.0
1717
board = genericSTM32F103VC
1818
framework = stm32cube
1919
lib_deps =
20-
https://github.com/RoboticsBrno/RB3204-RBCX-coproc-comm/archive/fa79ee92b39527ac0ff988277efaa71a09e9cad3.zip
20+
https://github.com/RoboticsBrno/RB3204-RBCX-coproc-comm/archive/81dae0effc201174b9e55c9702d58f80d51b720b.zip
2121

2222
build_flags =
2323
-Iinclude

fw/rbcx-coprocessor/src/Power.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,52 @@ extern "C" void PVD_IRQHandler() {
107107
pinWrite(powerPin, 0);
108108

109109
// Blink red LED "pretty fast"
110+
// Only blink for a limited time because we don't want to cause a denial of service
111+
// if we're powered from a different source (which?)
110112
uint32_t leds = 0;
111-
while (true) {
113+
for (int blink = 0; blink < 10; blink++) {
112114
leds ^= CoprocReq_LedsEnum_L3;
113115
setLeds(leds);
114116

115117
for (volatile int i = 0; i < 200000; i++)
116118
;
117119
}
120+
121+
// Reset program if power still present (powered from elsewhere)
122+
HAL_NVIC_SystemReset();
118123
}
119124

120125
void powerEarlyInit() {
126+
pinWrite(powerPin, true);
121127
__HAL_RCC_PWR_CLK_ENABLE();
122128
__HAL_RCC_BKP_CLK_ENABLE();
123129

124-
// Enable PVD - will generate EXTI16(PVD_IRQn) when VDD drops.
125-
// Done before disabling ALARM output to eliminate race
130+
// Enable PVD - we want EXTI16(PVD_IRQn) interrupt when VDD drops.
131+
// Enabled before disabling ALARM output to eliminate race
126132
// that could result in ALARM deactivation.
127133
LL_PWR_EnableBkUpAccess();
128-
LL_PWR_SetPVDLevel(LL_PWR_PVDLEVEL_7);
134+
// Level 7 is the highest and seems too intolerant.
135+
LL_PWR_SetPVDLevel(LL_PWR_PVDLEVEL_4);
129136
LL_PWR_EnablePVD();
130137

138+
setLeds(0x1);
139+
140+
// Attempt to survive a transient power-up period when PVD output may fluctuate.
141+
while (LL_PWR_IsActiveFlag_PVDO())
142+
;
143+
__HAL_PWR_PVD_EXTI_CLEAR_FLAG();
144+
__HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
145+
__HAL_PWR_PVD_EXTI_ENABLE_IT();
146+
147+
HAL_NVIC_ClearPendingIRQ(PVD_IRQn);
131148
HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
132149
HAL_NVIC_EnableIRQ(PVD_IRQn);
133150

151+
setLeds(0x3);
152+
134153
// Disable potential RTC ALARM -> powerPin override.
135154
// This way we keep VCC powered upon board power-up.
136-
// This must be done very early at power-up.
155+
// This must be done early at power-up.
137156
LL_RTC_SetOutputSource(BKP, LL_RTC_CALIB_OUTPUT_NONE);
138157
}
139158

fw/rbcx-coprocessor/src/RtcController.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@
88
#include "stm32f1xx_ll_rtc.h"
99

1010
static void ensureInitialized() {
11-
// Ensure RTC is running (noop in case it is)
11+
// We use DR3 to indicate that RTC was already initialized in a prior life.
12+
bool initialized = LL_RTC_BKP_GetRegister(BKP, LL_RTC_BKP_DR3);
13+
if (initialized) {
14+
// RTC clock selected signifies RTC already initialized.
15+
return;
16+
}
17+
18+
while (!LL_RCC_LSE_IsReady()) {
19+
}
20+
1221
LL_RTC_InitTypeDef init = {
1322
.AsynchPrescaler = 0x7FFF, // 32kHz crystal -> 1s tick
1423
.OutPutSource = LL_RTC_CALIB_OUTPUT_NONE,
1524
};
1625

26+
LL_RCC_EnableRTC();
27+
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
1728
LL_RTC_Init(RTC, &init);
29+
LL_RTC_BKP_SetRegister(BKP, LL_RTC_BKP_DR3, 0x1);
1830
}
1931

2032
void rtcInit() {
@@ -27,13 +39,17 @@ void rtcInit() {
2739

2840
bool rtcInitReady() { return LL_RCC_LSE_IsReady(); }
2941

42+
uint32_t rtcFlags() {
43+
return (!rtcInitReady()) | (LL_RTC_IsActiveFlag_ALR(RTC) << 1);
44+
}
45+
3046
static void sendStatus() {
3147
CoprocStat status;
3248
status.which_payload = CoprocStat_rtcStat_tag,
3349
status.payload.rtcStat = CoprocStat_RtcStat {
3450
.time = rtcGetTime(),
3551
.alarm = rtcGetAlarm(),
36-
.notReady = !rtcInitReady(),
52+
.flags = CoprocStat_RtcFlags(rtcFlags()),
3753
};
3854
controlLinkTx(status);
3955
}
@@ -74,6 +90,7 @@ uint32_t rtcGetAlarm() {
7490

7591
void rtcSetAlarm(uint32_t seconds) {
7692
ensureInitialized();
93+
LL_RTC_ClearFlag_ALR(RTC);
7794
LL_RTC_ALARM_SetCounter(RTC, seconds);
7895

7996
// We mirror the value into DR1 and DR2 16-bit regs

fw/rbcx-coprocessor/src/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@
2323
static TaskWrapper<3072> mainTask;
2424

2525
int main() {
26-
powerEarlyInit();
27-
clocksInit();
28-
HAL_Init();
29-
3026
#ifdef RBCX_VECT_TAB_OFFSET
3127
SCB->VTOR = FLASH_BASE | RBCX_VECT_TAB_OFFSET;
3228
#endif
3329

30+
// Allow POWER and LEDs drive in powerEarlyInit
31+
__HAL_RCC_GPIOA_CLK_ENABLE();
32+
__HAL_RCC_GPIOC_CLK_ENABLE();
33+
pinInit(powerPin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
34+
pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
35+
36+
powerEarlyInit();
37+
clocksInit();
38+
HAL_Init();
39+
3440
pinsInit();
3541

3642
mainTask.start("main", 1, []() {

0 commit comments

Comments
 (0)