The display currently shows the time, with hours and minutes, and is capable of receiving input with buttons (though does nothing). It sleeps during intervals where nothing is happening. The display task runs once per second, and RTC alarm A is used for periodic alarms to update the system time.
178 lines
5.8 KiB
C++
178 lines
5.8 KiB
C++
/*
|
|
* Copyright (C) 2019 Max Regan
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "LowPowerTaskScheduler.h"
|
|
#include "RtcDriver.h"
|
|
#include "DisplayDriver.h"
|
|
#include "SpiDriver.h"
|
|
#include "DisplayTimeTask.h"
|
|
#include "LptimPwm.h"
|
|
#include "ButtonManager.h"
|
|
|
|
#include "stm32l0xx.h"
|
|
|
|
#include "macros.h"
|
|
|
|
using Common::Time;
|
|
|
|
static Common::Schedule::LowPowerTaskScheduler<10> g_sched;
|
|
static BSP::SpiDriver g_spi(g_sched);
|
|
static BSP::DisplayDriver g_display(g_sched, g_spi);
|
|
static BSP::LptimPwm g_lptim_pwm(LPTIM1);
|
|
static BSP::ButtonManager g_btn_manager(0, 1, 3, Time::millis(1));
|
|
static DisplayTimeTask g_display_time(g_display);
|
|
|
|
extern "C" void __cxa_pure_virtual() { while(1) {} }
|
|
|
|
void SystemInit()
|
|
{
|
|
/**
|
|
* Use the MSI for the system clock, and disable all other clocks.
|
|
*/
|
|
|
|
/*!< Set MSION bit. Set by hardware to force the MSI oscillator ON
|
|
* when exiting from Stop or Standby mode, or in case of a failure
|
|
* of the HSE oscillator used directly or indirectly as system
|
|
* clock. This bit cannot be cleared if the MSI is used as system
|
|
* clock. */
|
|
SET(RCC->CR,
|
|
RCC_CR_MSION);
|
|
|
|
SET_TO(RCC->ICSCR,
|
|
RCC_ICSCR_MSIRANGE,
|
|
RCC_ICSCR_MSIRANGE_6);
|
|
|
|
/*!< Set internal representation of clock frequency to 4MHz */
|
|
// system_clk_freq = 4u << 22;
|
|
|
|
/*!< Reset
|
|
* SW[1:0] (use MSI oscillator as system clock),
|
|
* HPRE[3:0] (do not divide AHB clock in prescaler) ,
|
|
* PPRE1[2:0] (do not divide APB low-speed clock)
|
|
* PPRE2[2:0] (do not divide APB high-speed clock),
|
|
* MCOSEL[2:0] (disable MCO clock),
|
|
* MCOPRE[2:0] (disable MCO prescaler) */
|
|
CLR(RCC->CFGR,
|
|
RCC_CFGR_SW | ~RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 |
|
|
RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE);
|
|
|
|
/*!< Reset
|
|
* HSION (disable HSI),
|
|
* HSIDIVEN (disable 18MHz HSI division)
|
|
* HSEON (disable HSE clock)
|
|
* CSSHSEON (disable HSE clock monitoring)
|
|
* PLLON (disable PLL)
|
|
*/
|
|
CLR(RCC->CR,
|
|
RCC_CR_HSION | RCC_CR_HSIDIVEN | RCC_CR_HSEON |
|
|
RCC_CR_CSSHSEON | RCC_CR_PLLON);
|
|
|
|
/*!< Reset HSEBYP bit (disable HSE bypass) */
|
|
CLR(RCC->CR,
|
|
RCC_CR_HSEBYP);
|
|
|
|
/*!< Reset
|
|
* PLLSRC (HSI16 is the PLL source),
|
|
* PLLMUL[3:0] (3x PLL multiplication)
|
|
* Don't touch PLLDIV[1:0], since 0 is undefined
|
|
*/
|
|
CLR(RCC->CFGR,
|
|
RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV);
|
|
|
|
|
|
/*!< Disable all interrupts */
|
|
RCC->CIER = 0x00000000;
|
|
|
|
/* Vector Table Relocation in Internal FLASH */
|
|
SCB->VTOR = FLASH_BASE;
|
|
}
|
|
|
|
extern "C" {
|
|
|
|
typedef void (*func_ptr)(void);
|
|
extern func_ptr __init_array_start[], __init_array_end[];
|
|
|
|
static void _init(void)
|
|
{
|
|
|
|
for (func_ptr* func = __init_array_start; func != __init_array_end; func++) {
|
|
(*func)();
|
|
}
|
|
}
|
|
}
|
|
|
|
[[noreturn]] void main() {
|
|
|
|
_init();
|
|
|
|
// Set up the system clock
|
|
BSP::RtcDriver::init();
|
|
BSP::SystemTimer::set_timer(BSP::RtcDriver::get_system_timer());
|
|
BSP::LowPower::init();
|
|
|
|
// Initialize the tasks
|
|
g_lptim_pwm.init();
|
|
g_spi.init();
|
|
g_btn_manager.init();
|
|
g_display.init();
|
|
g_display_time.init();
|
|
|
|
// Enqueue each of the tasks
|
|
Common::Schedule::NextTime asap = Common::Schedule::NextTime::asap();
|
|
g_sched.add_task(g_spi, asap);
|
|
g_sched.add_task(g_btn_manager, asap);
|
|
g_sched.add_task(g_display, asap);
|
|
g_sched.add_task(g_display_time, asap);
|
|
|
|
// And we're off! This will never return
|
|
g_sched.run();
|
|
}
|
|
|
|
|
|
extern "C" void NMI_Handler() {while (1);}
|
|
extern "C" void HardFault_Handler() {while (1);}
|
|
extern "C" void SVC_Handler() {while (1);}
|
|
extern "C" void PendSV_Handler() {while (1);}
|
|
extern "C" void SysTick_Handler() {while (1);}
|
|
|
|
extern "C" void WWDG_IRQHandler() {while (1);}
|
|
extern "C" void PVD_IRQHandler() {while (1);}
|
|
extern "C" void WDT_IRQHandler() {while (1);}
|
|
//extern "C" void RTC_IRQHandler() {while (1);}
|
|
extern "C" void FLASH_IRQHandler() {while (1);}
|
|
extern "C" void RCC_CRS_IRQHandler() {while (1);}
|
|
// extern "C" void EXTI_1_0_IRQHandler() {while (1);}
|
|
// extern "C" void EXTI_3_2_IRQHandler() {while (1);}
|
|
// extern "C" void EXTI_15_4_IRQHandler() {while (1);}
|
|
extern "C" void DMA1_CHANNEL1_IRQHandler() {while (1);}
|
|
extern "C" void DMA1_CHANNEL3_2_IRQHandler() {while (1);}
|
|
extern "C" void DMA_CHANNEL_7_4_IRQHandler() {while (1);}
|
|
extern "C" void ADC_COMP_IRQHandler() {while (1);}
|
|
extern "C" void LPTIM1_IRQHandler() {while (1);}
|
|
extern "C" void USART4_USART5_IRQHandler() {while (1);}
|
|
extern "C" void TIM2_IRQHandler() {while (1);}
|
|
extern "C" void TIM3_IRQHandler() {while (1);}
|
|
extern "C" void TIM6_IRQHandler() {while (1);}
|
|
extern "C" void TIM7_IRQHandler() {while (1);}
|
|
extern "C" void TIM21_IRQHandler() {while (1);}
|
|
extern "C" void I2C3_IRQHandler() {while (1);}
|
|
extern "C" void TIM22_IRQHandler() {while (1);}
|