Skip to content

Commit

Permalink
Add theoretical-ish ALARM handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dzarda committed Apr 24, 2021
1 parent 06f1cf4 commit 048017e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
6 changes: 3 additions & 3 deletions fw/rbcx-coprocessor/include/Bsp_v10.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ inline void reinitEspStrappingPins() {
}

inline void pinsInit() {
pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
// pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);

pinWrite(powerPin, 1);
pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
// pinWrite(powerPin, 1);
// pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);

for (auto button : buttonPin)
pinInit(button, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_SPEED_FREQ_LOW);
Expand Down
6 changes: 3 additions & 3 deletions fw/rbcx-coprocessor/include/Bsp_v11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ inline void reinitEspStrappingPins() {
}

inline void pinsInit() {
pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
// pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);

pinWrite(powerPin, 1);
pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
// pinWrite(powerPin, 1);
// pinInit(powerPin, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);

for (auto button : buttonPin)
pinInit(button, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_SPEED_FREQ_LOW);
Expand Down
2 changes: 1 addition & 1 deletion fw/rbcx-coprocessor/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ platform = ststm32@~7.0.0
board = genericSTM32F103VC
framework = stm32cube
lib_deps =
https://github.com/RoboticsBrno/RB3204-RBCX-coproc-comm/archive/fa79ee92b39527ac0ff988277efaa71a09e9cad3.zip
https://github.com/RoboticsBrno/RB3204-RBCX-coproc-comm/archive/81dae0effc201174b9e55c9702d58f80d51b720b.zip

build_flags =
-Iinclude
Expand Down
29 changes: 24 additions & 5 deletions fw/rbcx-coprocessor/src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,52 @@ extern "C" void PVD_IRQHandler() {
pinWrite(powerPin, 0);

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

for (volatile int i = 0; i < 200000; i++)
;
}

// Reset program if power still present (powered from elsewhere)
HAL_NVIC_SystemReset();
}

void powerEarlyInit() {
pinWrite(powerPin, true);
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_RCC_BKP_CLK_ENABLE();

// Enable PVD - will generate EXTI16(PVD_IRQn) when VDD drops.
// Done before disabling ALARM output to eliminate race
// Enable PVD - we want EXTI16(PVD_IRQn) interrupt when VDD drops.
// Enabled before disabling ALARM output to eliminate race
// that could result in ALARM deactivation.
LL_PWR_EnableBkUpAccess();
LL_PWR_SetPVDLevel(LL_PWR_PVDLEVEL_7);
// Level 7 is the highest and seems too intolerant.
LL_PWR_SetPVDLevel(LL_PWR_PVDLEVEL_4);
LL_PWR_EnablePVD();

setLeds(0x1);

// Attempt to survive a transient power-up period when PVD output may fluctuate.
while (LL_PWR_IsActiveFlag_PVDO())
;
__HAL_PWR_PVD_EXTI_CLEAR_FLAG();
__HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE();
__HAL_PWR_PVD_EXTI_ENABLE_IT();

HAL_NVIC_ClearPendingIRQ(PVD_IRQn);
HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(PVD_IRQn);

setLeds(0x3);

// Disable potential RTC ALARM -> powerPin override.
// This way we keep VCC powered upon board power-up.
// This must be done very early at power-up.
// This must be done early at power-up.
LL_RTC_SetOutputSource(BKP, LL_RTC_CALIB_OUTPUT_NONE);
}

Expand Down
21 changes: 19 additions & 2 deletions fw/rbcx-coprocessor/src/RtcController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
#include "stm32f1xx_ll_rtc.h"

static void ensureInitialized() {
// Ensure RTC is running (noop in case it is)
// We use DR3 to indicate that RTC was already initialized in a prior life.
bool initialized = LL_RTC_BKP_GetRegister(BKP, LL_RTC_BKP_DR3);
if (initialized) {
// RTC clock selected signifies RTC already initialized.
return;
}

while (!LL_RCC_LSE_IsReady()) {
}

LL_RTC_InitTypeDef init = {
.AsynchPrescaler = 0x7FFF, // 32kHz crystal -> 1s tick
.OutPutSource = LL_RTC_CALIB_OUTPUT_NONE,
};

LL_RCC_EnableRTC();
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
LL_RTC_Init(RTC, &init);
LL_RTC_BKP_SetRegister(BKP, LL_RTC_BKP_DR3, 0x1);
}

void rtcInit() {
Expand All @@ -27,13 +39,17 @@ void rtcInit() {

bool rtcInitReady() { return LL_RCC_LSE_IsReady(); }

uint32_t rtcFlags() {
return (!rtcInitReady()) | (LL_RTC_IsActiveFlag_ALR(RTC) << 1);
}

static void sendStatus() {
CoprocStat status;
status.which_payload = CoprocStat_rtcStat_tag,
status.payload.rtcStat = CoprocStat_RtcStat {
.time = rtcGetTime(),
.alarm = rtcGetAlarm(),
.notReady = !rtcInitReady(),
.flags = CoprocStat_RtcFlags(rtcFlags()),
};
controlLinkTx(status);
}
Expand Down Expand Up @@ -74,6 +90,7 @@ uint32_t rtcGetAlarm() {

void rtcSetAlarm(uint32_t seconds) {
ensureInitialized();
LL_RTC_ClearFlag_ALR(RTC);
LL_RTC_ALARM_SetCounter(RTC, seconds);

// We mirror the value into DR1 and DR2 16-bit regs
Expand Down
14 changes: 10 additions & 4 deletions fw/rbcx-coprocessor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@
static TaskWrapper<3072> mainTask;

int main() {
powerEarlyInit();
clocksInit();
HAL_Init();

#ifdef RBCX_VECT_TAB_OFFSET
SCB->VTOR = FLASH_BASE | RBCX_VECT_TAB_OFFSET;
#endif

// Allow POWER and LEDs drive in powerEarlyInit
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
pinInit(powerPin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);
pinInit(ledPins, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW);

powerEarlyInit();
clocksInit();
HAL_Init();

pinsInit();

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

0 comments on commit 048017e

Please sign in to comment.