Skip to content

Instantly share code, notes, and snippets.

@cpldcpu
Created February 7, 2021 02:10
Show Gist options
  • Save cpldcpu/603bf71c9c6afcffa46dd7c459e27efc to your computer and use it in GitHub Desktop.
Save cpldcpu/603bf71c9c6afcffa46dd7c459e27efc to your computer and use it in GitHub Desktop.
Low Power LED Flasher on PFS154
/* ---------------------------------------------------
Ultra Low Power LED flasher
LED is connected to PA4 and is high active.
Jan 16th, 2021, CPLDCPU - Initial version
---------------------------------------------------- */
#include <stdint.h>
#include <pdk/device.h>
#include <pdk/delay.h>
unsigned char _sdcc_external_startup(void)
{
PDK_SET_FUSE(FUSE_IO_DRV_LOW); // Set output driving strength to low
CLKMD = CLKMD_ILRC | CLKMD_ENABLE_ILRC | CLKMD_ENABLE_IHRC;
CLKMD = CLKMD_ILRC | CLKMD_ENABLE_ILRC ;
// Note: it is important to turn off IHRC only after clock settings
// have been updated. Otherwise the CPU stalls.
return 0; // perform normal initialization
}
void main(void)
{
// Toggling PA4 at ILRC (53kHz): 86 µA
// Toggling PA4 at ILRC_dev16 (53kHz): 66.7 µA
// Stopsys with 5V: 0.5 µA
// The watchdog cannot wake up from stopexe!!
PADIER = 0; // disable pins as wakeup source
PBDIER = 0; // Also port B needs to be disabled even if it is not connected
// to the outside of the package. touching the package can introduce
// glitches and wake up the device
INTEN = 0; // Make sure all interrupts are disabled
INTRQ = 0;
MISC = MISC_FAST_WAKEUP_ENABLE; // Enable faster wakeup (45 clocks instead of 3000)
// This is important to not waste energy, as 40µA bias is already added during wakeup time
// Configure timer two for output on PA3
// --> Works, timer2 and 2 can be used for PWM while CPU is in STOPEXE mode
// It appears that also timer 2 can wake up the CPU. This is not maskable?
TM2C = TM2C_CLK_ILRC | TM2C_MODE_PWM; // Oscilator source for timer 2 is LRC (53 kHZ)
TM2CT = 0;
TM2S = TM2S_PRESCALE_DIV16 | TM2S_SCALE_DIV8; // Divide clock by 16*7=112 -> 53 kHz / 122 = 414 Hz
TM2B = 1; // PWM threshold set to 1. The PWM event will trigger the wakeup. Wakeup occurs with 414 Hz / 256 = 1.66 Hz
PA = 1<<4; // LED is on PA4, set all other output to zero.
PAPH = 0; // Disable all pull up resistors
PAC = 0; // Disable all outputs
// Note: There is no series resistor for the LED
// The LED current is limited to 2-4 mA by LOW IO driving setting
// See Setction 4.14 (p24) in PFS154 manual
for (;;) {
PAC |=1<<4; // Enable LED output (It's set to High)
__nop();
__nop();
__nop();
PAC &=~(1<<4); // Disable LED output after 4 cycles => 4/53 kHz = 75.5 µS
__stopexe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment