C++ refactoring, plus low-power support, plus software-based SPI CS

I'm backlogged.
This commit is contained in:
2019-04-13 15:08:02 -07:00
parent d5bfecedb2
commit 6747d6c831
54 changed files with 13193 additions and 7264 deletions

140
main.cpp Normal file
View File

@@ -0,0 +1,140 @@
/*
* 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 "ConcreteTaskScheduler.h"
#include "DisplayDriver.h"
#include "SpiDriver.h"
#include "BlinkTask.h"
#include "LowPowerDelay.h"
#include "DisplayTimeTask.h"
#include "stm32l0xx.h"
#include "macros.h"
using Common::Time;
static Common::Schedule::ConcreteTaskScheduler<10> g_sched;
static BSP::SpiDriver g_spi(g_sched);
static BSP::DisplayDriver g_display(g_sched, g_spi);
//static BlinkTask g_blink(Common::Time::seconds(2));
static DisplayTimeTask g_display_time(g_display);
//static LowPowerDelay g_lp_delay;
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();
BSP::SystemTimer::init(RTC);
g_spi.init();
g_display.init();
//g_blink.init();
g_display_time.init();
Common::Schedule::NextTime asap = Common::Schedule::NextTime::asap();
//g_sched.add_task(g_blink, asap);
//g_sched.add_task(g_lp_delay, asap);
g_sched.add_task(g_spi, asap);
g_sched.add_task(g_display, asap);
g_sched.add_task(g_display_time, asap);
g_sched.run();
}