refactor tree, add ecad, mcad

This commit is contained in:
2019-08-28 23:23:48 -07:00
parent d5ddd76bef
commit ae837eef12
45 changed files with 4629 additions and 169 deletions

View File

@@ -27,8 +27,8 @@
namespace BSP {
using Common::ReturnCode;
using Common::Schedule::NextTime;
using BSP::ReturnCode;
using BSP::Schedule::NextTime;
ButtonManager *ButtonManager::m_instance = nullptr;
@@ -67,8 +67,8 @@ ReturnCode ButtonManager::init()
NextTime ButtonManager::execute()
{
Common::time_t systime;
Common::time_t endtime = 0;
BSP::time_t systime;
BSP::time_t endtime = 0;
BSP::SystemTimer::get_time(systime);
@@ -121,7 +121,7 @@ void ButtonManager::remove_callback(Button btn)
void ButtonManager::irq()
{
uint32_t idr = GPIOA->IDR;
static Common::time_t systime;
static BSP::time_t systime;
// TODO: is this call too expensive for an interrupt handler?
BSP::SystemTimer::get_time(systime);

View File

@@ -29,13 +29,13 @@
namespace BSP {
class ButtonManager : public Common::Schedule::Task {
class ButtonManager : public BSP::Schedule::Task {
public:
ButtonManager(Common::Schedule::TaskScheduler &scheduler,
ButtonManager(BSP::Schedule::TaskScheduler &scheduler,
uint8_t up_gpio_idx,
uint8_t mid_gpio_idx,
uint8_t down_gpio_idx,
Common::time_t debounce_time)
BSP::time_t debounce_time)
: m_scheduler(scheduler)
, m_buttons
{
@@ -66,9 +66,9 @@ public:
using ChangeCallback = std::function<void(ButtonState)>;
Common::Schedule::NextTime execute() override;
BSP::Schedule::NextTime execute() override;
Common::ReturnCode init();
BSP::ReturnCode init();
void set_callback(Button btn, ChangeCallback callback);
void remove_callback(Button btn);
@@ -79,7 +79,7 @@ private:
struct button_state {
button_state(uint8_t gpio_index,
Common::time_t debounce_time,
BSP::time_t debounce_time,
ChangeCallback callback)
: m_gpio_idx(gpio_index)
, m_debounce_time(debounce_time)
@@ -88,15 +88,15 @@ private:
, m_callback(callback)
{}
uint8_t const m_gpio_idx;
Common::time_t const m_debounce_time;
BSP::time_t const m_debounce_time;
ButtonState m_prev_call_state; /*<! The last state the button was in when it the callback was called */
ButtonState m_state; /*<! The state the button was in during the last iteration */
Common::time_t m_state_change_ts; /*<! The system time when the button entered its current state */
BSP::time_t m_state_change_ts; /*<! The system time when the button entered its current state */
ChangeCallback m_callback; /*<! The callback to call when the button has changed states (post-debounce) */
};
Common::Schedule::TaskScheduler &m_scheduler;
BSP::Schedule::TaskScheduler &m_scheduler;
button_state m_buttons[Button::Count];
static void nop_callback(ButtonState) {};

View File

@@ -24,11 +24,11 @@
// TODO: Basically all of the error checking
using Common::Schedule::Task;
using Common::Schedule::NextTime;
using Common::ReturnCode;
using BSP::Schedule::Task;
using BSP::Schedule::NextTime;
using BSP::ReturnCode;
ScreenManager::ScreenManager(Common::Schedule::TaskScheduler &scheduler,
ScreenManager::ScreenManager(BSP::Schedule::TaskScheduler &scheduler,
BSP::DisplayDriver &display,
BSP::ButtonManager &buttons)
: m_scheduler(scheduler)

View File

@@ -31,21 +31,21 @@
#include "Application/ButtonManager.h"
#include "Application/Screens/Screen.h"
class ScreenManager : public Common::Schedule::Task {
class ScreenManager : public BSP::Schedule::Task {
public:
ScreenManager(Common::Schedule::TaskScheduler &scheduler,
ScreenManager(BSP::Schedule::TaskScheduler &scheduler,
BSP::DisplayDriver &display,
BSP::ButtonManager &buttons);
Common::ReturnCode init();
Common::ReturnCode set_root_screen(Screen &screen);
BSP::ReturnCode init();
BSP::ReturnCode set_root_screen(Screen &screen);
Common::Schedule::NextTime execute() override;
BSP::Schedule::NextTime execute() override;
Common::ReturnCode pop_screen();
Common::ReturnCode push_screen(Screen &screen);
Common::ReturnCode set_screen(Screen &screen);
BSP::ReturnCode pop_screen();
BSP::ReturnCode push_screen(Screen &screen);
BSP::ReturnCode set_screen(Screen &screen);
private:
@@ -58,7 +58,7 @@ private:
static constexpr std::size_t MAX_SCREEN_STACK = 5;
Common::Schedule::TaskScheduler &m_scheduler;
BSP::Schedule::TaskScheduler &m_scheduler;
std::array<Screen *, MAX_SCREEN_STACK> m_screen_stack;
std::size_t m_screen_stack_depth;

View File

@@ -0,0 +1,191 @@
/*
* 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 <algorithm>
#include <cmath>
#include "Application/Screens/AnalogTimeScreen.h"
#include "Application/SystemFonts.h"
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/SystemTime.h"
#include "Bsp/Drivers/LowPower.h"
using BSP::ReturnCode;
using BSP::Time;
using BSP::Schedule::NextTime;
using Color = BSP::DisplayDriver::Color;
// TODO: remove all calls to std::cos and std::sin?
AnalogTimeScreen::AnalogTimeScreen(BSP::DisplayDriver &driver,
ScreenManager &manager,
Screen &menu_screen)
: m_driver(driver)
, m_last_time()
, m_manager(manager)
, m_menu_screen(menu_screen)
, m_display_seconds(true)
{}
ReturnCode AnalogTimeScreen::init()
{
return ReturnCode::OK;
}
void AnalogTimeScreen::draw_ticks() {
// After profiling, using lookup tables is 6-10% faster than using std::sin & std::cos.
static constexpr float sin_tbl[8] = {
0.0,
0.10452539173303049,
0.20790564888802265,
0.30900818248165035,
0.4067253572759511,
0.4999866265466325,
0.5877702605258084,
0.6691145400274635,
};
static constexpr float cos_tbl[8] = {
1.0,
0.9945222181947755,
0.9781488849661131,
0.9510593794077146,
0.9135504823209005,
0.8660331248136633,
0.8090278863187741,
0.7431592913526923,
};
const uint32_t y_center = m_driver.get_height() / 2;
const uint32_t x_center = m_driver.get_width() / 2;
const uint32_t radius = std::min(m_driver.get_width(), m_driver.get_height()) / 2;
// This only works for square screens, but saves a a bunch of float math and calls to trig functions
for (int i = 0; i < 8; i++) {
const uint32_t len = (i % 5 == 0) ? 10 : 5;
const uint32_t width = (i % 5 == 0) ? 3 : 1;
const uint32_t end1_offset = sin_tbl[i] * radius;
const uint32_t end2_offset = cos_tbl[i] * radius;
const uint32_t start1_offset = sin_tbl[i] * (radius - len);
const uint32_t start2_offset = cos_tbl[i] * (radius - len);
m_driver.draw_line(x_center + start1_offset, y_center + start2_offset,
x_center + end1_offset, y_center + end2_offset,
Color::BLACK, width);
m_driver.draw_line(x_center - start1_offset, y_center + start2_offset,
x_center - end1_offset, y_center + end2_offset,
Color::BLACK, width);
m_driver.draw_line(x_center + start1_offset, y_center - start2_offset,
x_center + end1_offset, y_center - end2_offset,
Color::BLACK, width);
m_driver.draw_line(x_center - start1_offset, y_center - start2_offset,
x_center - end1_offset, y_center - end2_offset,
Color::BLACK, width);
m_driver.draw_line(x_center + start2_offset, y_center + start1_offset,
x_center + end2_offset, y_center + end1_offset,
Color::BLACK, width);
m_driver.draw_line(x_center - start2_offset, y_center + start1_offset,
x_center - end2_offset, y_center + end1_offset,
Color::BLACK, width);
m_driver.draw_line(x_center + start2_offset, y_center - start1_offset,
x_center + end2_offset, y_center - end1_offset,
Color::BLACK, width);
m_driver.draw_line(x_center - start2_offset, y_center - start1_offset,
x_center - end2_offset, y_center - end1_offset,
Color::BLACK, width);
}
}
void AnalogTimeScreen::draw_hand(uint32_t ticks, uint32_t len, int32_t width, Color color) {
const uint32_t y_center = m_driver.get_height() / 2;
const uint32_t x_center = m_driver.get_width() / 2;
uint32_t x_end = x_center + std::sin(ticks * TURNS_PER_TICK) * len;
uint32_t y_end = y_center - std::cos(ticks * TURNS_PER_TICK) * len;
m_driver.draw_line(x_center, y_center, x_end, y_end, color, width);
}
void AnalogTimeScreen::display_time()
{
BSP::WallClockTime time;
BSP::RtcDriver::get_time(time);
m_driver.clear(Color::WHITE);
draw_ticks();
const uint32_t seconds_len = std::min(m_driver.get_width(), m_driver.get_height()) * 9 / 20;
const uint32_t minutes_len = std::min(m_driver.get_width(), m_driver.get_height()) * 17 / 40;
const uint32_t hours_len = std::min(m_driver.get_width(), m_driver.get_height()) * 6 / 20;
draw_hand(time.get_hours_12() * 5, hours_len, 3, Color::BLACK);
draw_hand(time.get_minutes(), minutes_len, 3, Color::BLACK);
if (m_display_seconds) {
draw_hand(time.get_seconds(), seconds_len, 2, Color::RED);
}
m_last_time = time;
m_driver.refresh();
}
NextTime AnalogTimeScreen::execute()
{
display_time();
BSP::time_t now;
BSP::SystemTimer::get_time(now);
if (m_display_seconds) {
return NextTime::in(Time::seconds(1));
} else {
BSP::WallClockTime wall_time;
BSP::RtcDriver::get_time(wall_time);
return NextTime::in(Time::seconds(61 - wall_time.get_seconds()));
}
}
void AnalogTimeScreen::enable() {
m_last_time = {};
display_time();
}
void AnalogTimeScreen::disable() {
}
void AnalogTimeScreen::notify_up_button() {
/* TODO: This should open a menu first */
m_manager.push_screen(m_menu_screen);
}
void AnalogTimeScreen::notify_middle_button() {
}
void AnalogTimeScreen::notify_down_button() {
}

View File

@@ -0,0 +1,61 @@
/*
* 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.
*/
#pragma once
#include "Bsp/macros.h"
#include "Application/ScreenManager.h"
#include "Application/Screens/Screen.h"
class AnalogTimeScreen : public Screen {
public:
AnalogTimeScreen(BSP::DisplayDriver &display,
ScreenManager &m_manager,
Screen &m_menu_screen);
BSP::ReturnCode init();
BSP::Schedule::NextTime execute() override;
void enable() override;
void disable() override;
void notify_up_button() override;
void notify_middle_button() override;
void notify_down_button() override;
private:
static constexpr float TURNS_PER_TICK = 2 * 3.1415 / 60;
void draw_hand(uint32_t ticks, uint32_t len, int32_t width, BSP::DisplayDriver::Color color);
void draw_ticks();
void display_time();
BSP::DisplayDriver &m_driver;
BSP::WallClockTime m_last_time;
ScreenManager &m_manager;
Screen &m_menu_screen;
const bool m_display_seconds;
};

View File

@@ -19,18 +19,18 @@
* THE SOFTWARE.
*/
#include "Application/Screens/DisplayTimeScreen.h"
#include "Application/Screens/BigDigitalTimeScreen.h"
#include "Application/SystemFonts.h"
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/SystemTime.h"
#include "Bsp/Drivers/LowPower.h"
using Common::ReturnCode;
using Common::Time;
using Common::Schedule::NextTime;
using BSP::ReturnCode;
using BSP::Time;
using BSP::Schedule::NextTime;
using Color = BSP::DisplayDriver::Color;
DisplayTimeScreen::DisplayTimeScreen(BSP::DisplayDriver &driver,
BigDigitalTimeScreen::BigDigitalTimeScreen(BSP::DisplayDriver &driver,
ScreenManager &manager,
Screen &menu_screen)
: m_driver(driver)
@@ -48,14 +48,12 @@ static char get_char_for_digit(uint8_t bcd_digit)
return bcd_digit + '0';
}
ReturnCode DisplayTimeScreen::init()
ReturnCode BigDigitalTimeScreen::init()
{
SET(RCC->CFGR, RCC_CFGR_SW_HSI);
return ReturnCode::OK;
}
void DisplayTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const struct font &f)
void BigDigitalTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const struct font &f)
{
char time_str[3] = { 0 };
@@ -63,12 +61,12 @@ void DisplayTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, u
time_str[1] = get_char_for_digit(ones);
time_str[2] = '\0';
m_driver.string_at(x, y, time_str, &f, Color::WHITE);
m_driver.string_at(x, y, time_str, &f, Color::BLACK);
}
void DisplayTimeScreen::display_time()
void BigDigitalTimeScreen::display_time()
{
Common::WallClockTime time;
BSP::WallClockTime time;
BSP::RtcDriver::get_time(time);
const struct font &font = font_large_digits;
const uint32_t y_space = (m_driver.get_height() - (2 * font.height)) / 3;
@@ -76,7 +74,7 @@ void DisplayTimeScreen::display_time()
uint32_t x = 0;
m_driver.clear(Color::BLACK);
m_driver.clear(Color::WHITE);
x = x_space;
display_number(&x, y_space,
@@ -90,40 +88,40 @@ void DisplayTimeScreen::display_time()
m_driver.refresh();
}
NextTime DisplayTimeScreen::execute()
NextTime BigDigitalTimeScreen::execute()
{
display_time();
Common::time_t now;
BSP::time_t now;
BSP::SystemTimer::get_time(now);
if (m_display_seconds) {
return NextTime::in(Time::seconds(1));
} else {
Common::WallClockTime wall_time;
BSP::WallClockTime wall_time;
BSP::RtcDriver::get_time(wall_time);
return NextTime::in(Time::seconds(61 - wall_time.get_seconds()));
}
}
void DisplayTimeScreen::enable() {
void BigDigitalTimeScreen::enable() {
m_last_time = {};
display_time();
}
void DisplayTimeScreen::disable() {
void BigDigitalTimeScreen::disable() {
}
void DisplayTimeScreen::notify_up_button() {
void BigDigitalTimeScreen::notify_up_button() {
/* TODO: This should open a menu first */
m_manager.push_screen(m_menu_screen);
}
void DisplayTimeScreen::notify_middle_button() {
void BigDigitalTimeScreen::notify_middle_button() {
}
void DisplayTimeScreen::notify_down_button() {
void BigDigitalTimeScreen::notify_down_button() {
}

View File

@@ -12,7 +12,7 @@
*
* 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
* 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
@@ -26,15 +26,15 @@
#include "Application/ScreenManager.h"
#include "Application/Screens/Screen.h"
class DisplayTimeScreen : public Screen {
class BigDigitalTimeScreen : public Screen {
public:
DisplayTimeScreen(BSP::DisplayDriver &display,
ScreenManager &m_manager,
Screen &m_menu_screen);
BigDigitalTimeScreen(BSP::DisplayDriver &display,
ScreenManager &m_manager,
Screen &m_menu_screen);
Common::ReturnCode init();
Common::Schedule::NextTime execute() override;
BSP::ReturnCode init();
BSP::Schedule::NextTime execute() override;
void enable() override;
void disable() override;
@@ -49,7 +49,7 @@ private:
void display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const font &f);
BSP::DisplayDriver &m_driver;
Common::WallClockTime m_last_time;
BSP::WallClockTime m_last_time;
ScreenManager &m_manager;
Screen &m_menu_screen;

View File

@@ -39,8 +39,8 @@ public:
, m_manager(manager)
{}
Common::ReturnCode init() {
return Common::ReturnCode::OK;
BSP::ReturnCode init() {
return BSP::ReturnCode::OK;
}
void render() {
@@ -58,9 +58,9 @@ public:
m_driver.refresh();
}
Common::Schedule::NextTime execute() override {
BSP::Schedule::NextTime execute() override {
render();
return Common::Schedule::NextTime::never();
return BSP::Schedule::NextTime::never();
}
void enable() override {

View File

@@ -124,12 +124,12 @@ public:
m_driver.refresh();
}
Common::ReturnCode init() {
return Common::ReturnCode::OK;
BSP::ReturnCode init() {
return BSP::ReturnCode::OK;
}
Common::Schedule::NextTime execute() override {
return Common::Schedule::NextTime::never();
BSP::Schedule::NextTime execute() override {
return BSP::Schedule::NextTime::never();
}
void enable() override {

View File

@@ -24,7 +24,7 @@
#include "Bsp/ReturnCode.h"
#include "Bsp/Task.h"
class Screen : public Common::Schedule::Task {
class Screen : public BSP::Schedule::Task {
public:
virtual void enable() = 0;

View File

@@ -26,9 +26,9 @@
#include "Bsp/SystemTime.h"
#include "Bsp/Drivers/RtcDriver.h"
using Common::ReturnCode;
using Common::Time;
using Common::Schedule::NextTime;
using BSP::ReturnCode;
using BSP::Time;
using BSP::Schedule::NextTime;
SetDateScreen::SetDateScreen(BSP::DisplayDriver &display,
ScreenManager &manager)

View File

@@ -36,8 +36,8 @@ public:
SetDateScreen(BSP::DisplayDriver &display,
ScreenManager &m_manager);
Common::ReturnCode init();
Common::Schedule::NextTime execute() override;
BSP::ReturnCode init();
BSP::Schedule::NextTime execute() override;
void enable() override;
void disable() override;
@@ -70,7 +70,7 @@ private:
SetState m_state;
bool m_is_acked = false;
Common::WallClockTime m_time;
BSP::WallClockTime m_time;
const struct font &m_font;
const uint32_t m_row_spacing;
const uint32_t m_row_0_y;

View File

@@ -26,9 +26,9 @@
#include "Bsp/SystemTime.h"
#include "Bsp/Drivers/RtcDriver.h"
using Common::ReturnCode;
using Common::Time;
using Common::Schedule::NextTime;
using BSP::ReturnCode;
using BSP::Time;
using BSP::Schedule::NextTime;
SetTimeScreen::SetTimeScreen(BSP::DisplayDriver &display,
ScreenManager &manager)

View File

@@ -36,8 +36,8 @@ public:
SetTimeScreen(BSP::DisplayDriver &display,
ScreenManager &m_manager);
Common::ReturnCode init();
Common::Schedule::NextTime execute() override;
BSP::ReturnCode init();
BSP::Schedule::NextTime execute() override;
void enable() override;
void disable() override;
@@ -70,7 +70,7 @@ private:
SetState m_state;
bool m_is_acked = false;
Common::WallClockTime m_time;
BSP::WallClockTime m_time;
const struct font &m_font;
const uint32_t m_row_spacing;
const uint32_t m_row_0_y;

View File

@@ -28,7 +28,8 @@
#include "Application/ButtonManager.h"
#include "Application/ScreenManager.h"
#include "Application/Screens/DisplayTimeScreen.h"
#include "Application/Screens/AnalogTimeScreen.h"
#include "Application/Screens/BigDigitalTimeScreen.h"
#include "Application/Screens/DebugScreen.h"
#include "Application/Screens/MenuScreen.h"
#include "Application/Screens/SetTimeScreen.h"
@@ -39,13 +40,13 @@
// TODO: Don't include this here.
#include "stm32l0xx.h"
using Common::Time;
using BSP::Time;
static Common::Schedule::LowPowerTaskScheduler<5> g_sched;
static BSP::Schedule::LowPowerTaskScheduler<5> 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(g_sched, 2, 1, 0, Time::millis(1));
static BSP::ButtonManager g_btn_manager(g_sched, 0, 1, 2, Time::millis(1));
static ScreenManager g_screen_manager(g_sched, g_display, g_btn_manager);
static SetTimeScreen g_set_time_screen(g_display, g_screen_manager);
@@ -72,7 +73,7 @@ static MenuScreen g_main_menu_screen(g_display,
std::initializer_list<MenuScreenItem>({MenuScreenItem("Apps", g_settings_menu_screen),
MenuScreenItem("Settings", g_settings_menu_screen)}));
static DisplayTimeScreen g_display_time_screen(g_display, g_screen_manager, g_main_menu_screen);
static AnalogTimeScreen g_display_time_screen(g_display, g_screen_manager, g_main_menu_screen);
extern "C" void __cxa_pure_virtual() { while(1) {} }
@@ -121,7 +122,45 @@ void SystemInit()
/*!< Reset HSEBYP bit (disable HSE bypass) */
CLR(RCC->CR,
RCC_CR_HSEBYP);
RCC_CR_HSEBYP)
;
/* (1) Test if PLL is used as System clock */
/* (2) Select HSI as system clock */
/* (3) Wait for HSI switched */
/* (4) Disable the PLL */
/* (5) Wait until PLLRDY is cleared */
/* (6) Set latency to 1 wait state */
/* (7) Set the PLL multiplier to 24 and divider by 3 */
/* (8) Enable the PLL */
/* (9) Wait until PLLRDY is set */
/* (10) Select PLL as system clock */
/* (11) Wait until the PLL is switched on */
// SET(RCC->CR, RCC_CR_HSION);
// while((RCC->CR & RCC_CR_HSIRDY) != 0) {}
// if ((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_PLL) /* (1) */
// {
// RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI; /* (2) */
// while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI) /* (3) */
// { /* For robust implementation, add here time-out management */ }
// }
// RCC->CR &= (uint32_t)(~RCC_CR_PLLON);/* (4) */
// while((RCC->CR & RCC_CR_PLLRDY) != 0) /* (5) */
// { /* For robust implementation, add here time-out management */ }
// FLASH->ACR |= FLASH_ACR_LATENCY; /* (6) */
// RCC->CFGR = RCC->CFGR & ((~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV )) | (RCC_CFGR_PLLMUL24 | RCC_CFGR_PLLDIV2)); /* (7) */
// RCC->CR |= RCC_CR_PLLON; /* (8) */
// while ((RCC->CR & RCC_CR_PLLRDY) == 0) /* (9) */
// { /* For robust implementation, add here time-out management */ }
// RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL); /* (10) */
// while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) /* (11) */
// { /* For robust implementation, add here time-out management */ }
/*!< Reset
* PLLSRC (HSI16 is the PLL source),
@@ -129,7 +168,7 @@ void SystemInit()
* Don't touch PLLDIV[1:0], since 0 is undefined
*/
CLR(RCC->CFGR,
RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV);
RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV);
/*!< Disable all interrupts */
RCC->CIER = 0x00000000;
@@ -170,7 +209,7 @@ static void _init(void)
g_screen_manager.set_root_screen(g_display_time_screen);
// Enqueue each of the tasks
Common::Schedule::NextTime asap = Common::Schedule::NextTime::asap();
BSP::Schedule::NextTime asap = BSP::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);

View File

@@ -20,6 +20,7 @@
*/
#include <cstring>
#include <cstdlib>
#include "Bsp/Drivers/DisplayDriver.h"
#include "Bsp/macros.h"
@@ -27,10 +28,10 @@
namespace BSP {
using Common::Schedule::NextTime;
using Common::ReturnCode;
using BSP::Schedule::NextTime;
using BSP::ReturnCode;
DisplayDriver::DisplayDriver(Common::Schedule::TaskScheduler &scheduler, SpiDriver &spi)
DisplayDriver::DisplayDriver(BSP::Schedule::TaskScheduler &scheduler, SpiDriver &spi)
: m_scheduler(scheduler)
, m_spi(spi)
, m_is_dirty(true)
@@ -42,7 +43,7 @@ DisplayDriver::DisplayDriver(Common::Schedule::TaskScheduler &scheduler, SpiDriv
ReturnCode DisplayDriver::init()
{
return Common::ReturnCode::OK;
return BSP::ReturnCode::OK;
}
NextTime DisplayDriver::execute()
@@ -98,6 +99,45 @@ void DisplayDriver::set_dirty(unsigned int y)
}
}
void DisplayDriver::draw_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, Color color, int32_t width)
{
const int32_t dx = abs(x1 - x0);
const int32_t sx = (x0 < x1) ? 1 : -1;
const int32_t dy = -abs(y1 - y0);
const int32_t sy = (y0 < y1) ? 1 : -1;
int32_t err = dx + dy;
uint32_t x = x0;
uint32_t y = y0;
while (true) {
for (int32_t i = -width / 2; i < (-width / 2) + width; i++) {
uint32_t xp, yp;
if (dx > -dy) {
xp = x;
yp = y + i;
} else {
xp = x + i;
yp = y;
}
set_pixel(xp, yp, color);
}
if (x == x1 && y == y1)
break;
const int32_t e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x += sx;
}
if (e2 <= dx) {
err += dx;
y += sy;
}
}
}
// TODO: write my own implementation
#define R2(n) n, n + 2*64, n + 1*64, n + 3*64
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)

View File

@@ -27,15 +27,15 @@
namespace BSP {
class DisplayDriver final : public Common::Schedule::Task {
class DisplayDriver final : public BSP::Schedule::Task {
public:
DisplayDriver(Common::Schedule::TaskScheduler &scheduler, SpiDriver &spi);
DisplayDriver(BSP::Schedule::TaskScheduler &scheduler, SpiDriver &spi);
/**
* Common::Schedule::Task
* BSP::Schedule::Task
*/
Common::ReturnCode init();
Common::Schedule::NextTime execute() override;
BSP::ReturnCode init();
BSP::Schedule::NextTime execute() override;
static constexpr uint32_t BITS_PER_PIXEL = 3;
@@ -62,6 +62,7 @@ public:
const char *string, const struct font *font,
Color color=DEFAULT_COLOR);
void draw_hline(uint32_t x, uint32_t y, uint32_t width, Color color=DEFAULT_COLOR);
void draw_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, Color color=DEFAULT_COLOR, int32_t width=1);
void refresh();
void clear(Color color=Color::WHITE);
@@ -98,7 +99,7 @@ private:
};
Common::Schedule::TaskScheduler &m_scheduler;
BSP::Schedule::TaskScheduler &m_scheduler;
SpiDriver &m_spi;
struct display_buffer m_buffer;

View File

@@ -29,7 +29,7 @@ uint32_t wakeups = 0;
namespace BSP {
using Common::ReturnCode;
using BSP::ReturnCode;
ReturnCode LowPower::init()
{
@@ -40,12 +40,12 @@ ReturnCode LowPower::init()
ReturnCode LowPower::enable_debug()
{
/* Enable Clocks */
SET(RCC->APB2ENR, RCC_APB2ENR_DBGEN);
SET(RCC->APB2SMENR, RCC_APB2SMENR_DBGSMEN);
// SET(RCC->APB2ENR, RCC_APB2ENR_DBGEN);
// SET(RCC->APB2SMENR, RCC_APB2SMENR_DBGSMEN);
SET(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
SET(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
SET(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
// SET(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
// SET(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
// SET(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
return ReturnCode::OK;
}

View File

@@ -31,11 +31,11 @@ class LowPower {
public:
LowPower() = delete;
static Common::ReturnCode init();
static Common::ReturnCode sleep();
static Common::ReturnCode stop();
static Common::ReturnCode enable_debug();
static Common::ReturnCode disable_debug();
static BSP::ReturnCode init();
static BSP::ReturnCode sleep();
static BSP::ReturnCode stop();
static BSP::ReturnCode enable_debug();
static BSP::ReturnCode disable_debug();
};
}

View File

@@ -24,7 +24,7 @@
namespace BSP {
using Common::ReturnCode;
using BSP::ReturnCode;
LptimPwm::LptimPwm(LPTIM_TypeDef *lptim)
: m_lptim(lptim)

View File

@@ -32,7 +32,7 @@ public:
LptimPwm() = delete;
LptimPwm(LPTIM_TypeDef *lptim);
Common::ReturnCode init();
BSP::ReturnCode init();
private:
void init_lptim();

View File

@@ -26,8 +26,8 @@
namespace BSP {
using Common::ReturnCode;
using Common::time_t;
using BSP::ReturnCode;
using BSP::time_t;
RtcDriver::RtcSystemTimer RtcDriver::m_sys_timer;
@@ -106,11 +106,11 @@ ReturnCode RtcDriver::init_hw()
uint32_t time = 0;
SET(time, RTC_TR_PM);
SET_TO(time, RTC_TR_HT, 1 << RTC_TR_HT_Pos);
SET_TO(time, RTC_TR_HU, 2 << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_MNT, 5 << RTC_TR_MNT_Pos);
SET_TO(time, RTC_TR_HU, 0 << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_MNT, 0 << RTC_TR_MNT_Pos);
SET_TO(time, RTC_TR_MNU, 9 << RTC_TR_MNU_Pos);
SET_TO(time, RTC_TR_ST, 0 << RTC_TR_ST_Pos);
SET_TO(time, RTC_TR_SU, 0 << RTC_TR_SU_Pos);
SET_TO(time, RTC_TR_ST, 3 << RTC_TR_ST_Pos);
SET_TO(time, RTC_TR_SU, 6 << RTC_TR_SU_Pos);
RTC->TR = time;
CLR(RTC->ISR, RTC_ISR_INIT);
@@ -119,7 +119,7 @@ ReturnCode RtcDriver::init_hw()
SET(EXTI->EMR, EXTI_EMR_EM20);
SET(EXTI->RTSR, EXTI_RTSR_RT20);
// Enable Wakeup interrupts, we may/will use them later
// Enable Wakeup irq, we may/will use them later
SET(RTC->CR, RTC_CR_WUTIE);
NVIC_EnableIRQ(RTC_IRQn);
NVIC_SetPriority(RTC_IRQn, 0);
@@ -131,7 +131,7 @@ ReturnCode RtcDriver::init_hw()
return ReturnCode::OK;
}
ReturnCode RtcDriver::get_time(Common::WallClockTime &wall_time)
ReturnCode RtcDriver::get_time(BSP::WallClockTime &wall_time)
{
/*<! The value of TR in the shadow register is locked when SSR is
read (by the system timer), until the date register is read. We're
@@ -155,12 +155,12 @@ ReturnCode RtcDriver::get_time(Common::WallClockTime &wall_time)
hours += 12;
}
new (&wall_time) Common::WallClockTime(hours, minutes, seconds);
new (&wall_time) BSP::WallClockTime(hours, minutes, seconds);
return ReturnCode::OK;
}
ReturnCode RtcDriver::set_time(const Common::WallClockTime &wall_time)
ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
{
enable_rtc_write();
@@ -191,13 +191,13 @@ ReturnCode RtcDriver::set_time(const Common::WallClockTime &wall_time)
return ReturnCode::OK;
}
ReturnCode RtcDriver::set_wakeup_in(Common::time_t wakeup_delay)
ReturnCode RtcDriver::set_wakeup_in(BSP::time_t wakeup_delay)
{
/*<! 2^64 / (1000000 * 32768) / 60 / 60 / 24 / 365 = ~17.85 This
value will only overflow for wakeup_delays > 17.85 years, so
this is fine. */
uint64_t delay_cycles = Common::Time::to_micros(wakeup_delay) * LSE_CLOCK_FREQ /
Common::Time::MICROS_PER_SEC;
uint64_t delay_cycles = BSP::Time::to_micros(wakeup_delay) * LSE_CLOCK_FREQ /
BSP::Time::MICROS_PER_SEC;
enable_rtc_write();
@@ -244,25 +244,29 @@ ReturnCode RtcDriver::set_wakeup_in(Common::time_t wakeup_delay)
return ReturnCode::OK;
}
Common::time_t RtcDriver::RtcSystemTimer::get_time()
BSP::time_t RtcDriver::RtcSystemTimer::get_time()
{
uint32_t new_secs, old_secs, ssr, subsecond;
uint32_t new_secs, old_secs, ssr;
uint64_t new_timer_ticks, new_millis;
do {
__disable_irq();
old_secs = m_seconds;
ssr = RTC->SSR & 0xFFFF;
new_secs = m_seconds;
__enable_irq();
} while (new_secs != old_secs);
new_secs = new_secs * LSE_CLOCK_FREQ;
new_timer_ticks = (uint64_t) new_secs * LSE_CLOCK_FREQ;
/** SSR is a countdown register */
subsecond = (new_secs + LSE_CLOCK_FREQ - 1 - ssr) * Common::Time::MILLIS_PER_SEC / LSE_CLOCK_FREQ;
return Common::Time::millis(subsecond);
new_millis = (new_timer_ticks + LSE_CLOCK_FREQ - 1 - ssr) * BSP::Time::MILLIS_PER_SEC / LSE_CLOCK_FREQ;
return BSP::Time::millis(new_millis);
}
void RtcDriver::RtcSystemTimer::increment_seconds()
{
/** TODO: Atomic increment */
__disable_irq();
m_seconds++;
__enable_irq();
}
void RtcDriver::increment_seconds()
@@ -274,7 +278,7 @@ static uint32_t wakeup_alarms = 0;
extern "C" void RTC_IRQHandler()
{
// Clear the wakeup and alarm interrupts in the EXTI
// Clear the wakeup and alarm irq in the EXTI
SET(EXTI->PR, EXTI_PR_PIF20 | EXTI_PR_PIF17);
if (RTC->ISR & RTC_ISR_ALRAF) {

View File

@@ -40,15 +40,15 @@ public:
static SystemTimerImpl& get_system_timer() {
return m_sys_timer;
};
static Common::ReturnCode init();
static BSP::ReturnCode init();
static void increment_seconds();
static Common::ReturnCode get_time(Common::WallClockTime &tm_bcd);
static Common::ReturnCode set_time(const Common::WallClockTime &tm_bcd);
static Common::ReturnCode set_wakeup_in(Common::time_t wakeup_delay);
static BSP::ReturnCode get_time(BSP::WallClockTime &tm_bcd);
static BSP::ReturnCode set_time(const BSP::WallClockTime &tm_bcd);
static BSP::ReturnCode set_wakeup_in(BSP::time_t wakeup_delay);
private:
static Common::ReturnCode init_hw();
static BSP::ReturnCode init_hw();
static void enable_rtc_write();
static void disable_rtc_write();
static void enable_rtc_wakeup_interrupt();
@@ -67,7 +67,7 @@ private:
~RtcSystemTimer() {}
Common::time_t get_time() override;
BSP::time_t get_time() override;
void increment_seconds();
private:

View File

@@ -24,10 +24,10 @@
namespace BSP {
using RC = Common::ReturnCode;
using Common::Schedule::TaskScheduler;
using Common::Schedule::NextTime;
using Common::Time;
using RC = BSP::ReturnCode;
using BSP::Schedule::TaskScheduler;
using BSP::Schedule::NextTime;
using BSP::Time;
SpiDriver::SpiDriver(TaskScheduler &scheduler)
: m_scheduler(scheduler)

View File

@@ -29,18 +29,18 @@
namespace BSP {
class SpiDriver : public Common::Schedule::Task {
class SpiDriver : public BSP::Schedule::Task {
public:
// TODO: Add configurability / provide a real abstraction
SpiDriver(Common::Schedule::TaskScheduler &scheduler);
SpiDriver(BSP::Schedule::TaskScheduler &scheduler);
void init();
Common::Schedule::NextTime execute() override;
Common::ReturnCode tx_blocking(const uint8_t *data, size_t len);
BSP::Schedule::NextTime execute() override;
BSP::ReturnCode tx_blocking(const uint8_t *data, size_t len);
private:
Common::Schedule::TaskScheduler &m_scheduler;
BSP::Schedule::TaskScheduler &m_scheduler;
SPI_TypeDef *m_spi;
};

View File

@@ -28,7 +28,7 @@
#include "Drivers/LowPower.h"
#include "Drivers/RtcDriver.h"
namespace Common {
namespace BSP {
namespace Schedule {
template <uint32_t MAX_TASKS>
@@ -100,13 +100,13 @@ private:
void inline cycle()
{
Common::time_t time = 0;
BSP::time_t time = 0;
BSP::SystemTimer::get_time(time);
bool task_died = false;
/* Keep state for when the next task will execute. */
bool execed = false;
Common::time_t next_time = ~0;
BSP::time_t next_time = ~0;
for (size_t i = 0; i < m_task_count; i++) {
TaskEvent &event = m_tasks[i];
@@ -128,13 +128,13 @@ private:
}
if (m_task_count == 0) {
Common::ReturnCode rc = BSP::RtcDriver::set_wakeup_in(Time::seconds(5));
if (rc == Common::ReturnCode::OK) {
BSP::ReturnCode rc = BSP::RtcDriver::set_wakeup_in(Time::seconds(5));
if (rc == BSP::ReturnCode::OK) {
BSP::LowPower::stop();
}
} else if (!execed && (next_time - time > Time::millis(2))) {
Common::ReturnCode rc = BSP::RtcDriver::set_wakeup_in(next_time - time);
if (rc == Common::ReturnCode::OK) {
BSP::ReturnCode rc = BSP::RtcDriver::set_wakeup_in(next_time - time);
if (rc == BSP::ReturnCode::OK) {
BSP::LowPower::stop();
}
}

View File

@@ -21,7 +21,7 @@
#pragma once
namespace Common {
namespace BSP {
enum class ReturnCode : int {
OK = 0,

View File

@@ -24,8 +24,8 @@
namespace BSP {
using Common::ReturnCode;
using Common::time_t;
using BSP::ReturnCode;
using BSP::time_t;
SystemTimerImpl *SystemTimer::m_impl = nullptr;

View File

@@ -32,7 +32,7 @@ namespace BSP {
class SystemTimerImpl {
public:
virtual Common::time_t get_time() = 0;
virtual BSP::time_t get_time() = 0;
};
class SystemTimer final {
@@ -40,7 +40,7 @@ public:
SystemTimer() = delete;
~SystemTimer() = delete;
static Common::ReturnCode get_time(Common::time_t &time);
static BSP::ReturnCode get_time(BSP::time_t &time);
static void set_timer(SystemTimerImpl& timer);
private:
static SystemTimerImpl *m_impl;

View File

@@ -19,6 +19,6 @@
* THE SOFTWARE.
*/
namespace Common {
namespace BSP {
}

View File

@@ -25,7 +25,7 @@
#include "Time.h"
#include "SystemTime.h"
namespace Common {
namespace BSP {
namespace Schedule {
enum class ScheduleType {
@@ -112,4 +112,4 @@ public:
};
} // namespace Schedule
} // namespace Common
} // namespace BSP

View File

@@ -26,7 +26,7 @@
#include "Task.h"
#include "system.h"
namespace Common {
namespace BSP {
namespace Schedule {
class TaskScheduler {

View File

@@ -23,7 +23,7 @@
#include <stdint.h>
namespace Common {
namespace BSP {
using time_t = uint64_t;

View File

@@ -32,6 +32,7 @@ AS = $(TOOL_PREFIX)as
LD = $(TOOL_PREFIX)g++
OBJCOPY = $(TOOL_PREFIX)objcopy
STM32_PROG = STM32_Programmer.sh
#
# Device Variables
#
@@ -114,6 +115,7 @@ LDFLAGS += $(CPU_FLAGS)
LDFLAGS += -Wl,--gc-sections -Wl,--build-id=none -static
LDFLAGS += -Wl,--wrap=malloc -Wl,--wrap=free # Fail to link if dynamic allocation is sneaking through
LDFLAGS += -Wl,-print-memory-usage
# LDFLAGS += -lstdc++ -latomic
LDFLAGS += -nostartfiles
#
@@ -127,23 +129,22 @@ build: $(OUTPUT_BIN)
#
%.o: %.c $(FONT_H_FILES)
@echo "CC $@"
@echo "CC $@"
@$(CC) $(CFLAGS) -c $< -o $@
%.o: %.S $(FONT_H_FILES)
@echo "AS $@"
@echo "AS $@"
@$(CC) $(ASFLAGS) -c $< -o $@
%.o: %.cpp $(FONT_H_FILES)
@echo "CXX $@"
@$(CXX) $(CXX_FLAGS) $(CFLAGS) -c $< -o $@
SMALL_FONT=ThirdParty/fonts/roboto_mono/RobotoMono-Medium.ttf
SMALL_FONT=ThirdParty/fonts/roboto_mono/RobotoMono-Regular.ttf
$(FONT_GEN_DIR)/small.h $(FONT_GEN_DIR)/small.c: Gen/fixedfont-to-c.py Gen/font.py
@echo "GEN $@"
@mkdir -p $(FONT_GEN_DIR)
@Gen/fixedfont-to-c.py $(patsubst .%,%,$(suffix $@)) $(SMALL_FONT) "$@" -s 18 --header-dir "Bsp/" --name font_small
@$(call gen_font,$(FONT),29,small)
@Gen/fixedfont-to-c.py $(patsubst .%,%,$(suffix $@)) $(SMALL_FONT) "$@" -s 24 --header-dir "Bsp/" --name font_small
LARGE_FONT=ThirdParty/fonts/roboto_mono/RobotoMono-Bold.ttf
$(FONT_GEN_DIR)/large_digits.h $(FONT_GEN_DIR)/large_digits.c: Gen/fixedfont-to-c.py Gen/font.py
@@ -156,7 +157,7 @@ $(OUTPUT_BIN): $(OUTPUT_ELF)
@$(OBJCOPY) -O binary $(OUTPUT_ELF) $(OUTPUT_BIN)
$(OUTPUT_MAP) $(OUTPUT_ELF): $(LINKER_SCRIPT) $(OBJS)
@echo "LD $@"
@echo "LD $@"
@$(LD) -T $(LINKER_SCRIPT) $(LDFLAGS) -o $(OUTPUT_ELF) $(OBJS) -Wl,-Map=$(OUTPUT_MAP)
#
@@ -172,6 +173,4 @@ flash: $(OUTPUT_BIN)
.PHONY: clean
clean:
@echo "RM $(OBJS)"
@rm -f $(OBJS) $(OUTPUT_BIN) $(OUTPUT_ELF) $(FONT_C_FILES) $(OUTPUT_MAP) ./*.su
@rm -rf build/
rm -f $(OBJS) $(OUTPUT_BIN) $(OUTPUT_ELF) $(FONT_C_FILES) $(FONT_H_FILES) $(OUTPUT_MAP) $(addsuffix .su,$(basename $(OBJS)))