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

@@ -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;
};