Skip to content

Commit

Permalink
firmware: Add missing gem_bod.{h,c}
Browse files Browse the repository at this point in the history
  • Loading branch information
theacodes committed Apr 21, 2021
1 parent c9e5fc5 commit a4d07fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
40 changes: 40 additions & 0 deletions firmware/src/hw/gem_bod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright (c) 2021 Alethea Katherine Flowers.
Published under the standard MIT License.
Full text available at: https://opensource.org/licenses/MIT
*/

#include "gem_bod.h"
#include "sam.h"

void gem_wait_for_stable_voltage() {
/*
Configure the BOD to monitor for at least 3.0 volts and wait for
VDD to reach that level.
*/
SYSCTRL->BOD33.bit.ENABLE = 0;
while (!SYSCTRL->PCLKSR.bit.B33SRDY) {};

SYSCTRL->BOD33.reg = (
/* VBOD-: 3.0, VBOD+: 3.3 See datasheet Table 37-21. */
SYSCTRL_BOD33_LEVEL(48) |
/* Don't reset */
SYSCTRL_BOD33_ACTION_NONE |
/* Enable hysteresis */
SYSCTRL_BOD33_HYST);

/* Wait for the voltage to stabilize. */
SYSCTRL->BOD33.bit.ENABLE = 1;
while (!SYSCTRL->PCLKSR.bit.BOD33RDY) {}
while (SYSCTRL->PCLKSR.bit.BOD33DET) {}

/*
Everything is good, let the BOD33 reset the microcontroller if the
voltage falls below 3.0 volts.
*/

SYSCTRL->BOD33.bit.ENABLE = 0;
while (!SYSCTRL->PCLKSR.bit.B33SRDY) {};
SYSCTRL->BOD33.reg |= SYSCTRL_BOD33_ACTION_RESET;
SYSCTRL->BOD33.bit.ENABLE = 1;
}
11 changes: 11 additions & 0 deletions firmware/src/hw/gem_bod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright (c) 2021 Alethea Katherine Flowers.
Published under the standard MIT License.
Full text available at: https://opensource.org/licenses/MIT
*/

#pragma once

/* Routines for interacting with the SAM D21's brown out detector (BOD33) */

void gem_wait_for_stable_voltage();

0 comments on commit a4d07fb

Please sign in to comment.