Compare commits

...

4 Commits

Author SHA1 Message Date
Thomas Klaehn
ba83d9cdea Add virtual timer 2020-04-18 10:36:01 +02:00
Thomas Klaehn
e3f57f80df Accelerate clear screen in st7789 driver 2020-04-17 07:35:36 +02:00
Thomas Klaehn
eecbba48d2 Spi driver uses interrupt mode 2020-04-17 07:35:08 +02:00
Thomas Klaehn
17d4ee8ff9 Add memory pool 2020-04-14 12:57:01 +02:00
22 changed files with 534 additions and 150 deletions

3
.vscode/tasks.json vendored
View File

@ -2,10 +2,9 @@
"version": "2.0.0", "version": "2.0.0",
"options": { "options": {
"env": { "env": {
// "APPLICATION": "blinky", "APPLICATION": "blinky",
// "APPLICATION": "spi", // "APPLICATION": "spi",
// "APPLICATION": "st7789_lcd", // "APPLICATION": "st7789_lcd",
"APPLICATION": "sys_tick",
}, },
}, },
"presentation": { "presentation": {

View File

@ -33,6 +33,7 @@ CC_SRCS = $(wildcard $(SRC_DIR)/*.cc)
CC_SRCS += $(wildcard $(SRC_DIR)/platform/$(CORE)/*.cc) CC_SRCS += $(wildcard $(SRC_DIR)/platform/$(CORE)/*.cc)
CC_SRCS += $(wildcard $(SRC_DIR)/platform/$(PLATFORM)/*.cc) CC_SRCS += $(wildcard $(SRC_DIR)/platform/$(PLATFORM)/*.cc)
CC_SRCS += $(wildcard $(SRC_DIR)/application/$(APPLICATION)/*.cc) CC_SRCS += $(wildcard $(SRC_DIR)/application/$(APPLICATION)/*.cc)
CC_SRCS += $(wildcard $(SRC_DIR)/virtual_timer/*.cc)
CC_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.cc,%.cc.o,$(CC_SRCS))) CC_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.cc,%.cc.o,$(CC_SRCS)))
OBJS = $(A_OBJS) $(C_OBJS) $(CC_OBJS) OBJS = $(A_OBJS) $(C_OBJS) $(CC_OBJS)

View File

@ -0,0 +1,19 @@
#ifndef __INTERFACES_HWTIMERINTERFACE_H__
#define __INTERFACES_HWTIMERINTERFACE_H__
#include <cstdint>
namespace pinetime::interfaces
{
class HwTimerInterface
{
public:
virtual void enable_timer() = 0;
virtual void disable_timer() = 0;
virtual uint64_t tick() = 0;
};
}
#endif

View File

@ -0,0 +1,19 @@
#ifndef __INTERFACES_VIRTUALTIMERINTERFACE_H__
#define __INTERFACES_VIRTUALTIMERINTERFACE_H__
#include <cstdint>
namespace pinetime::interfaces
{
class VirtualTimerInterface
{
public:
virtual void timer_notification(uint64_t) = 0;
virtual void timer_enable() = 0;
virtual void timer_disable() = 0;
};
}
#endif

56
src/Pool.h Normal file
View File

@ -0,0 +1,56 @@
#ifndef __POOL_H__
#define __POOL_H__
#include <cstddef>
#include <cstdint>
#include "platform/cm4/InterruptLock.h"
namespace pinetime
{
template <typename T, std::size_t sizeT>
class Pool
{
union Item
{
uint8_t item[sizeof(T)];
Item * next;
};
Item items[sizeT];
uint8_t map[sizeT];
public:
Pool()
{
for(unsigned int i = 0; i < sizeT; i++) {
map[i] = 0;
}
}
void *alloc()
{
pinetime::platform::cm4::InterruptLock lock;
void * res = nullptr;
for(unsigned int i = 0; i < sizeT; i++) {
if(map[i] == 0) {
map[i] = 0xff;
res = static_cast<void *>(items + i);
break;
}
}
return res;
}
void free(T * ptr)
{
pinetime::platform::cm4::InterruptLock lock;
// ptr is not assumed to be aligned, since the integer based aritmetic provides always the correct index and omits the ramainder.
std::size_t index = (reinterpret_cast<std::uintptr_t>(ptr) - reinterpret_cast<std::uintptr_t>(items)) / sizeof(Item);
map[index] = 0;
}
};
}
#endif

View File

@ -1,15 +1,19 @@
#include <array> #include <array>
#include "delay.h" #include "virtual_timer/VirtualTimerDistributor.h"
#include "platform/cm4/InterruptHandler.h" #include "platform/cm4/InterruptHandler.h"
#include "platform/cm4/InterruptGuardian.h" #include "platform/cm4/InterruptGuardian.h"
#include "platform/cm4/SystemTick.h"
#include "platform/nrf52/gpio.h" #include "platform/nrf52/gpio.h"
#include "platform/nrf52/InterruptHandler.h" #include "platform/nrf52/InterruptHandler.h"
#include "platform/nrf52/InterruptGuardian.h" #include "platform/nrf52/InterruptGuardian.h"
#include "delay.h"
using namespace pinetime::platform; using namespace pinetime::platform;
using namespace pinetime::virtual_timer;
enum { enum {
PIN_NUMBER_LED_1 = 17, PIN_NUMBER_LED_1 = 17,
@ -18,23 +22,30 @@ enum {
PIN_NUMBER_LED_4 = 20 PIN_NUMBER_LED_4 = 20
}; };
// LEDs
nrf52::Gpio led_1(PIN_NUMBER_LED_1); nrf52::Gpio led_1(PIN_NUMBER_LED_1);
nrf52::Gpio led_2(PIN_NUMBER_LED_2); nrf52::Gpio led_2(PIN_NUMBER_LED_2);
nrf52::Gpio led_3(PIN_NUMBER_LED_3); nrf52::Gpio led_3(PIN_NUMBER_LED_3);
nrf52::Gpio led_4(PIN_NUMBER_LED_4); nrf52::Gpio led_4(PIN_NUMBER_LED_4);
std::array<nrf52::Gpio *, 4> leds = {&led_1, &led_2, &led_3, &led_4}; std::array<nrf52::Gpio *, 4> leds = {&led_1, &led_2, &led_3, &led_4};
cm4::InterruptGuardian cm4::InterruptGuardian::instance; // IRQs
nrf52::InterruptGuardian nrf52::InterruptGuardian::instance; nrf52::InterruptGuardian nrf52::InterruptGuardian::instance;
cm4::InterruptGuardian cm4::InterruptGuardian::instance;
// Timer
cm4::SystemTick system_tick;
VirtualTimerDistributor virtual_timer_distributor(system_tick.instance());
pinetime::Delay delay;
int main(void) int main(void)
{ {
cm4::InterruptGuardian::enable_interrupts();
while(true) { while(true) {
for(auto it = std::begin(leds); it != std::end(leds); ++it) { for(auto it = std::begin(leds); it != std::end(leds); ++it) {
nrf52::Gpio * tmp = *it; nrf52::Gpio * tmp = *it;
tmp->toggle(); tmp->toggle();
delay_ms(500); delay.delay(200);
} }
} }
return 0; return 0;

View File

@ -1,6 +1,4 @@
#include "delay.h"
#include "platform/cm4/InterruptHandler.h" #include "platform/cm4/InterruptHandler.h"
#include "platform/cm4/InterruptGuardian.h" #include "platform/cm4/InterruptGuardian.h"
@ -11,19 +9,19 @@
using namespace pinetime::platform; using namespace pinetime::platform;
const uint8_t buf[] = "Test"; uint8_t buf[] = "Test";
nrf52::Gpio led_1(17); nrf52::Gpio led_1(17);
nrf52::Gpio lcd_chip_select(25); nrf52::Gpio lcd_chip_select(25);
nrf52::Spi spi_0(0, 2, 3, 4, lcd_chip_select);
cm4::InterruptGuardian cm4::InterruptGuardian::instance; cm4::InterruptGuardian cm4::InterruptGuardian::instance;
nrf52::InterruptGuardian nrf52::InterruptGuardian::instance; nrf52::InterruptGuardian nrf52::InterruptGuardian::instance;
int main(void) int main(void)
{ {
cm4::InterruptGuardian::enable_interrupts();
nrf52::Spi spi_0(0, 2, 3, 4, lcd_chip_select);
while(1) { while(1) {
delay_ms(200);
spi_0.send(buf, sizeof(buf)); spi_0.send(buf, sizeof(buf));
led_1.toggle(); led_1.toggle();
} }

View File

@ -1,5 +1,3 @@
#include "delay.h"
#include "platform/cm4/InterruptHandler.h" #include "platform/cm4/InterruptHandler.h"
#include "platform/cm4/InterruptGuardian.h" #include "platform/cm4/InterruptGuardian.h"
@ -26,10 +24,10 @@ nrf52::InterruptGuardian nrf52::InterruptGuardian::instance;
int main(void) int main(void)
{ {
cm4::InterruptGuardian::enable_interrupts();
lcd.init(); lcd.init();
lcd.clear(0); lcd.clear(0);
while(true) { while(true) {
delay_ms(200);
led_1.toggle(); led_1.toggle();
} }
} }

View File

@ -1,49 +0,0 @@
#include <array>
#include "platform/cm4/InterruptHandler.h"
#include "platform/cm4/InterruptGuardian.h"
#include "platform/cm4/SystemTick.h"
#include "platform/nrf52/gpio.h"
#include "platform/nrf52/InterruptHandler.h"
#include "platform/nrf52/InterruptGuardian.h"
using namespace pinetime::platform;
enum {
PIN_NUMBER_LED_1 = 17,
PIN_NUMBER_LED_2 = 18,
PIN_NUMBER_LED_3 = 19,
PIN_NUMBER_LED_4 = 20
};
nrf52::Gpio led_1(PIN_NUMBER_LED_1);
nrf52::Gpio led_2(PIN_NUMBER_LED_2);
nrf52::Gpio led_3(PIN_NUMBER_LED_3);
nrf52::Gpio led_4(PIN_NUMBER_LED_4);
std::array<nrf52::Gpio *, 4> leds = {&led_1, &led_2, &led_3, &led_4};
nrf52::InterruptGuardian nrf52::InterruptGuardian::instance;
cm4::InterruptGuardian cm4::InterruptGuardian::instance;
int main(void)
{
cm4::SystemTick ticker;
ticker.enable();
uint32_t last_tick = 0;
cm4::InterruptGuardian::enable_interrupts();
while(true) {
for(auto it = std::begin(leds); it != std::end(leds); ++it) {
volatile uint32_t tick = ticker.tick() / 1000;
if(tick != last_tick) {
nrf52::Gpio * tmp = *it;
tmp->toggle();
last_tick = tick;
}
}
}
return 0;
}

27
src/delay.cc Normal file
View File

@ -0,0 +1,27 @@
#include "delay.h"
using namespace pinetime;
using namespace pinetime::virtual_timer;
Delay::Delay()
: VirtualTimer(0)
, pause(false)
{
}
void Delay::delay(uint64_t time_ms)
{
this->pause = true;
this->timer_set_period_ms(time_ms);
this->timer_enable();
while(this->pause) {
// FIXME: Low power
asm volatile("nop");
}
this->timer_disable();
}
void Delay::notification()
{
this->pause = false;
}

25
src/delay.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __DELAY_H__
#define __DELAY_H__
#include "virtual_timer/VirtualTimer.h"
namespace pinetime
{
class Delay
: public pinetime::virtual_timer::VirtualTimer
{
public:
Delay();
void delay(uint64_t);
void notification();
private:
bool pause;
};
}
#endif

View File

@ -2,11 +2,13 @@ extern "C" {
#include "nrf52.h" #include "nrf52.h"
} }
#include "virtual_timer/VirtualTimerDistributor.h"
#include "platform/cm4/InterruptHandler.h" #include "platform/cm4/InterruptHandler.h"
#include "platform/cm4/InterruptGuardian.h" #include "platform/cm4/InterruptGuardian.h"
#include "platform/cm4/SystemTick.h" #include "platform/cm4/SystemTick.h"
using namespace pinetime::platform::cm4; using namespace pinetime::platform::cm4;
using namespace pinetime::virtual_timer;
SystemTick::SystemTick() SystemTick::SystemTick()
: InterruptHandler(InterruptGuardian::Cm4IrqN::SYS_TICK_IRQ) : InterruptHandler(InterruptGuardian::Cm4IrqN::SYS_TICK_IRQ)
@ -17,19 +19,20 @@ SystemTick::SystemTick()
void SystemTick::handle() void SystemTick::handle()
{ {
this->sys_tick++; this->sys_tick++;
VirtualTimerDistributor::instance().notify(this->sys_tick);
} }
void SystemTick::enable() void SystemTick::enable_timer()
{ {
SysTick_Config(SystemCoreClock / 1000); SysTick_Config(SystemCoreClock / 1000);
} }
void SystemTick::disable() void SystemTick::disable_timer()
{ {
SysTick->CTRL = 0; SysTick->CTRL = 0;
} }
uint32_t SystemTick::tick() uint64_t SystemTick::tick()
{ {
return this->sys_tick; return this->sys_tick;
} }

View File

@ -1,21 +1,34 @@
#ifndef __PLATFORM_CM4_SYSTEMTICK_H__ #ifndef __PLATFORM_CM4_SYSTEMTICK_H__
#define __PLATFORM_CM4_SYSTEMTICK_H__ #define __PLATFORM_CM4_SYSTEMTICK_H__
#include "HwTimerInterface.h"
#include "platform/cm4/InterruptHandler.h" #include "platform/cm4/InterruptHandler.h"
namespace pinetime::platform::cm4 { namespace pinetime::platform::cm4
{
class SystemTick;
}
extern pinetime::platform::cm4::SystemTick system_tick;
class SystemTick : public pinetime::platform::cm4::InterruptHandler namespace pinetime::platform::cm4
{
class SystemTick
: public pinetime::platform::cm4::InterruptHandler
, public pinetime::interfaces::HwTimerInterface
{ {
public: public:
SystemTick(); SystemTick();
void handle();
void enable(); static inline SystemTick & instance() { return system_tick; }
void disable();
uint32_t tick(); void enable_timer() override;
void disable_timer() override;
uint64_t tick() override;
void handle() override;
private: private:
uint32_t sys_tick; uint64_t sys_tick;
}; };
} }

View File

@ -15,43 +15,43 @@ public:
InterruptGuardian(); InterruptGuardian();
enum Nrf52IrqN { enum Nrf52IrqN {
POWER_CLOCK_IRQn, //!< 0 POWER_CLOCK POWER_CLOCK_IRQ, //!< 0 POWER_CLOCK
RADIO_IRQn, //!< 1 RADIO RADIO_IRQ, //!< 1 RADIO
UARTE0_UART0_IRQn, //!< 2 UARTE0_UART0 UARTE0_UART0_IRQ, //!< 2 UARTE0_UART0
SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, //!< 3 SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQ, //!< 3 SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, //!< 4 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQ, //!< 4 SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1
NFCT_IRQn, //!< 5 NFCT NFCT_IRQ, //!< 5 NFCT
GPIOTE_IRQn, //!< 6 GPIOTE GPIOTE_IRQ, //!< 6 GPIOTE
SAADC_IRQn, //!< 7 SAADC SAADC_IRQ, //!< 7 SAADC
TIMER0_IRQn, //!< 8 TIMER0 TIMER0_IRQ, //!< 8 TIMER0
TIMER1_IRQn, //!< 9 TIMER1 TIMER1_IRQ, //!< 9 TIMER1
TIMER2_IRQn, //!< 10 TIMER2 TIMER2_IRQ, //!< 10 TIMER2
RTC0_IRQn, //!< 11 RTC0 RTC0_IRQ, //!< 11 RTC0
TEMP_IRQn, //!< 12 TEMP TEMP_IRQ, //!< 12 TEMP
RNG_IRQn, //!< 13 RNG RNG_IRQ, //!< 13 RNG
ECB_IRQn, //!< 14 ECB ECB_IRQ, //!< 14 ECB
CCM_AAR_IRQn, //!< 15 CCM_AAR CCM_AAR_IRQ, //!< 15 CCM_AAR
WDT_IRQn, //!< 16 WDT WDT_IRQ, //!< 16 WDT
RTC1_IRQn, //!< 17 RTC1 RTC1_IRQ, //!< 17 RTC1
QDEC_IRQn, //!< 18 QDEC QDEC_IRQ, //!< 18 QDEC
COMP_LPCOMP_IRQn, //!< 19 COMP_LPCOMP COMP_LPCOMP_IRQ, //!< 19 COMP_LPCOMP
SWI0_EGU0_IRQn, //!< 20 SWI0_EGU0 SWI0_EGU0_IRQ, //!< 20 SWI0_EGU0
SWI1_EGU1_IRQn, //!< 21 SWI1_EGU1 SWI1_EGU1_IRQ, //!< 21 SWI1_EGU1
SWI2_EGU2_IRQn, //!< 22 SWI2_EGU2 SWI2_EGU2_IRQ, //!< 22 SWI2_EGU2
SWI3_EGU3_IRQn, //!< 23 SWI3_EGU3 SWI3_EGU3_IRQ, //!< 23 SWI3_EGU3
SWI4_EGU4_IRQn, //!< 24 SWI4_EGU4 SWI4_EGU4_IRQ, //!< 24 SWI4_EGU4
SWI5_EGU5_IRQn, //!< 25 SWI5_EGU5 SWI5_EGU5_IRQ, //!< 25 SWI5_EGU5
TIMER3_IRQn, //!< 26 TIMER3 TIMER3_IRQ, //!< 26 TIMER3
TIMER4_IRQn, //!< 27 TIMER4 TIMER4_IRQ, //!< 27 TIMER4
PWM0_IRQn, //!< 28 PWM0 PWM0_IRQ, //!< 28 PWM0
PDM_IRQn, //!< 29 PDM PDM_IRQ, //!< 29 PDM
MWU_IRQn, //!< 32 MWU MWU_IRQ, //!< 32 MWU
PWM1_IRQn, //!< 33 PWM1 PWM1_IRQ, //!< 33 PWM1
PWM2_IRQn, //!< 34 PWM2 PWM2_IRQ, //!< 34 PWM2
SPIM2_SPIS2_SPI2_IRQn, //!< 35 SPIM2_SPIS2_SPI2 SPIM2_SPIS2_SPI2_IRQ, //!< 35 SPIM2_SPIS2_SPI2
RTC2_IRQn, //!< 36 RTC2 RTC2_IRQ, //!< 36 RTC2
I2S_IRQn, //!< 37 I2S I2S_IRQ, //!< 37 I2S
FPU_IRQn //!< 38 FPU FPU_IRQ //!< 38 FPU
}; };
private: private:

View File

@ -44,3 +44,13 @@ extern "C" {
} }
using namespace pinetime::platform::nrf52; using namespace pinetime::platform::nrf52;
void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void)
{
uint32_t irq_nr = InterruptGuardian::Nrf52IrqN::SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQ;
InterruptHandler *h = InterruptGuardian::instance.nrf52_vector[irq_nr];
assert(h != nullptr);
h->handle();
}

View File

@ -1,70 +1,130 @@
#include <assert.h> #include <assert.h>
#include "platform/nrf52/InterruptHandler.h"
#include "platform/nrf52/InterruptGuardian.h"
#include "platform/nrf52/spi.h" #include "platform/nrf52/spi.h"
extern "C" { extern "C" {
#include "nrf52.h" #include "nrf52.h"
#include "nrf52_bitfields.h" #include "nrf52_bitfields.h"
NRF_SPI_Type *SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI0_BASE); NRF_GPIO_Type *const GPIO_REGS = reinterpret_cast<NRF_GPIO_Type *>(NRF_P0_BASE);
NRF_SPIM_Type *SPIM_REGS = reinterpret_cast<NRF_SPIM_Type *>(NRF_SPIM0_BASE);
} }
using namespace pinetime::platform::nrf52; using namespace pinetime::platform::nrf52;
Spi::Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, pinetime::interfaces::GpioInterface & cs) Spi::Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, pinetime::interfaces::GpioInterface & cs)
: chip_select(cs) : InterruptHandler(InterruptGuardian::Nrf52IrqN::SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQ)
, chip_select(cs)
, xfer_active(false)
{ {
assert(instance < 3); assert(instance < 3);
if(instance == 0) { // sck pin
SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI0_BASE); GPIO_REGS->OUTCLR = 1UL << sck;
GPIO_REGS->PIN_CNF[mosi] = GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos
| GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos
| GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos
| GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos
| GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos;
// mosi pin
GPIO_REGS->OUTCLR = 1UL << mosi;
GPIO_REGS->PIN_CNF[mosi] = GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos
| GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos
| GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos
| GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos
| GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos;
// miso pin
GPIO_REGS->PIN_CNF[miso] = GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos
| GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos
| GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos
| GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos
| GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos;
// ss already configured by gpio driver
IRQn_Type irq = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn;
if(instance == 2) {
SPIM_REGS = reinterpret_cast<NRF_SPIM_Type *>(NRF_SPIM2_BASE);
irq = SPIM2_SPIS2_SPI2_IRQn;
} else if(instance == 1) { } else if(instance == 1) {
SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI1_BASE); SPIM_REGS = reinterpret_cast<NRF_SPIM_Type *>(NRF_SPIM1_BASE);
irq = SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn;
} else { } else {
SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI2_BASE); SPIM_REGS = reinterpret_cast<NRF_SPIM_Type *>(NRF_SPIM0_BASE);
irq = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn;
} }
this->chip_select.set(); this->chip_select.set();
SPI_REGS->ENABLE = 0; SPIM_REGS->ENABLE = 0;
SPI_REGS->PSEL.SCK = sck; SPIM_REGS->PSEL.SCK = sck;
SPI_REGS->PSEL.MOSI = mosi; SPIM_REGS->PSEL.MOSI = mosi;
SPI_REGS->PSEL.MISO = miso; SPIM_REGS->PSEL.MISO = miso;
SPI_REGS->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M8; SPIM_REGS->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M8;
SPI_REGS->CONFIG = (0x03 << 1); //Sample on trailing edge of clock, shift serial data on leading edge, SCK polarity Active low SPIM_REGS->CONFIG = (0x03 << 1); //Sample on trailing edge of clock, shift serial data on leading edge, SCK polarity Active low
SPI_REGS->EVENTS_READY = 0; SPIM_REGS->EVENTS_ENDRX = 0;
SPI_REGS->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); SPIM_REGS->EVENTS_ENDTX = 0;
SPIM_REGS->EVENTS_END = 0;
SPIM_REGS->INTENSET = (1 << 1) // Stopped
| (1 << 6) // End
| (1 << 19); // Started
SPIM_REGS->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
NVIC_SetPriority(irq, 2);
NVIC_EnableIRQ(irq);
} }
Spi::~Spi() Spi::~Spi()
{ {
SPI_REGS->ENABLE = 0; SPIM_REGS->ENABLE = 0;
} }
void Spi::send(const uint8_t * buffer, uint32_t len) void Spi::send(const uint8_t * buffer, uint32_t len)
{ {
this->chip_select.clear(); this->chip_select.clear();
for(unsigned int i = 0; i < len; i++) { this->xfer_active = true;
this->transfer(buffer[i]);
} SPIM_REGS->TXD.PTR = reinterpret_cast<uint32_t>(buffer);
SPIM_REGS->TXD.MAXCNT = len;
SPIM_REGS->TXD.LIST = 0;
SPIM_REGS->RXD.PTR = 0;
SPIM_REGS->RXD.MAXCNT = 0;
SPIM_REGS->RXD.LIST = 0;
SPIM_REGS->EVENTS_END = 0;
SPIM_REGS->TASKS_START = 1;
// FIXME: LOW POWER???
while(this->xfer_active);
this->chip_select.set(); this->chip_select.set();
} }
void Spi::recv(uint8_t * buffer, uint32_t len) void Spi::recv(uint8_t * buffer, uint32_t len)
{ {
this->chip_select.clear(); // FIXME: not implemented yet
for(unsigned int i = 0; i < len; i++) {
buffer[i] = this->transfer(buffer[i]);
}
this->chip_select.set();
} }
uint32_t Spi::transfer(uint32_t data) void Spi::handle()
{ {
volatile uint32_t ret; if((SPIM_REGS->INTENSET & (1 << 6)) && (SPIM_REGS->EVENTS_END == 1)) {
SPI_REGS->EVENTS_READY = 0; SPIM_REGS->EVENTS_END = 0;
SPI_REGS->TXD = data; this->xfer_active = false;
while(SPI_REGS->EVENTS_READY == 0) {;} }
ret = SPI_REGS->RXD;
return ret; if((SPIM_REGS->INTENSET & (1 << 19)) && (SPIM_REGS->EVENTS_STARTED == 1)) {
SPIM_REGS->EVENTS_STARTED = 0;
}
if((SPIM_REGS->INTENSET & (1 << 1)) && (SPIM_REGS->EVENTS_STOPPED == 1)) {
SPIM_REGS->EVENTS_STOPPED = 0;
this->xfer_active = false;
}
} }

View File

@ -4,9 +4,13 @@
#include "gpio_interface.h" #include "gpio_interface.h"
#include "spi_interface.h" #include "spi_interface.h"
#include "platform/nrf52/InterruptHandler.h"
namespace pinetime::platform::nrf52 { namespace pinetime::platform::nrf52 {
class Spi : public pinetime::interfaces::SpiInterface class Spi
: public pinetime::platform::nrf52::InterruptHandler
, public pinetime::interfaces::SpiInterface
{ {
public: public:
Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, pinetime::interfaces::GpioInterface & cs); Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, pinetime::interfaces::GpioInterface & cs);
@ -17,7 +21,10 @@ public:
private: private:
uint32_t transfer(uint32_t); uint32_t transfer(uint32_t);
void handle() override;
pinetime::interfaces::GpioInterface & chip_select; pinetime::interfaces::GpioInterface & chip_select;
volatile bool xfer_active;
}; };
} }

View File

@ -1,3 +1,4 @@
#include <array>
#include <cstdint> #include <cstdint>
#include "st7789.h" #include "st7789.h"
@ -115,17 +116,21 @@ void St7789::send_data(uint8_t data)
void St7789::clear(uint16_t Color) void St7789::clear(uint16_t Color)
{ {
unsigned int i,j; unsigned int odd = 0;
std::array<uint8_t, 240> line;
for(auto it = line.begin(); it != line.end(); ++it) {
if(odd % 2 == 0) {
*it = static_cast<uint8_t>((Color >> 8) & 0xff);
} else {
*it = static_cast<uint8_t>(Color & 0xff);
}
odd++;
}
set_windows(0, 0, 240, 240); set_windows(0, 0, 240, 240);
this->data_command.set(); this->data_command.set();
for(i = 0; i < 240; i++) { for(unsigned int i = 0; i < 240 * 2; i++) {
for(j = 0; j < 240; j++){ this->spi.send(line.data(), line.size());
uint8_t c = (Color >> 8) & 0xff;
this->spi.send(&c, 1);
c = Color & 0xff;
this->spi.send(&c, 1);
}
} }
} }

View File

@ -0,0 +1,38 @@
#include "virtual_timer/VirtualTimerDistributor.h"
#include "virtual_timer/VirtualTimer.h"
using namespace pinetime::virtual_timer;
VirtualTimer::VirtualTimer(uint64_t period_ms)
: period(period_ms)
{
}
void VirtualTimer::timer_enable()
{
VirtualTimerDistributor & distri = VirtualTimerDistributor::instance();
this->start_time = distri.tick();
distri.register_timer(this);
}
void VirtualTimer::timer_disable()
{
VirtualTimerDistributor::instance().unregister_timer(this);
}
void VirtualTimer::timer_notification(uint64_t tick_ms)
{
if((tick_ms - this->start_time) >= (this->period)) {
this->notification();
}
}
void VirtualTimer::timer_set_period_ms(uint64_t period_ms)
{
this->period = period_ms;
}
// void VirtualTimer::notify()
// {
// asm volatile("nop");
// }

View File

@ -0,0 +1,30 @@
#ifndef __VIRTUAL_TIMER_VIRTUALTIMER_H__
#define __VIRTUAL_TIMER_VIRTUALTIMER_H__
#include "VirtualTimerInterface.h"
namespace pinetime::virtual_timer
{
class VirtualTimer
: public pinetime::interfaces::VirtualTimerInterface
{
public:
VirtualTimer(uint64_t);
void timer_enable() override;
void timer_disable() override;
void timer_notification(uint64_t) override;
void timer_set_period_ms(uint64_t);
virtual void notification() = 0;
private:
uint64_t period;
uint64_t start_time;
};
}
#endif

View File

@ -0,0 +1,73 @@
#include "virtual_timer/VirtualTimer.h"
#include "virtual_timer/VirtualTimerDistributor.h"
using namespace pinetime::virtual_timer;
using namespace pinetime::interfaces;
VirtualTimerDistributor::VirtualTimerDistributor(pinetime::interfaces::HwTimerInterface & timer)
: hw_timer(timer)
, num_registered_timers(0)
{
for(auto it = this->virtual_timer_list.begin(); it != this->virtual_timer_list.end(); ++it) {
*it = nullptr;
}
}
void VirtualTimerDistributor::register_timer(VirtualTimerInterface *timer)
{
volatile bool enable = false;
if(num_registered_timers == MAX_TIMERS) {
// FIXME: Error notification
return;
}
if(num_registered_timers == 0) {
// Timer list is empty - so - hw timer isn't running
enable = true;
} else {
// Check if timer already registered
for(auto it = this->virtual_timer_list.begin(); it != this->virtual_timer_list.end(); ++it) {
if(*it == timer) {
return;
}
}
}
for(auto it = this->virtual_timer_list.begin(); it != this->virtual_timer_list.end(); ++it) {
if(*it == nullptr) {
*it = timer;
num_registered_timers++;
break;
}
}
if(enable) {
hw_timer.enable_timer();
}
}
void VirtualTimerDistributor::unregister_timer(VirtualTimerInterface *timer)
{
for(auto it = this->virtual_timer_list.begin(); it != this->virtual_timer_list.end(); ++it) {
if(*it == timer) {
*it = nullptr;
num_registered_timers--;
break;
}
}
if(num_registered_timers == 0) {
hw_timer.disable_timer();
}
}
void VirtualTimerDistributor::notify(uint64_t time_ms)
{
for(auto it = this->virtual_timer_list.begin(); it != this->virtual_timer_list.end(); ++it) {
if(*it != nullptr) {
VirtualTimerInterface *vt = *it;
vt->timer_notification(time_ms);
}
}
}
uint64_t VirtualTimerDistributor::tick()
{
return hw_timer.tick();
}

View File

@ -0,0 +1,41 @@
#ifndef __VIRTUAL_TIMER_VIRTUALTIMERDISTRIBUTOR_H__
#define __VIRTUAL_TIMER_VIRTUALTIMERDISTRIBUTOR_H__
#include <array>
#include "HwTimerInterface.h"
#include "VirtualTimerInterface.h"
namespace pinetime::virtual_timer
{
class VirtualTimerDistributor;
}
extern pinetime::virtual_timer::VirtualTimerDistributor virtual_timer_distributor;
namespace pinetime::virtual_timer
{
class VirtualTimerDistributor
{
public:
VirtualTimerDistributor(pinetime::interfaces::HwTimerInterface &);
static inline VirtualTimerDistributor& instance() { return virtual_timer_distributor; }
void register_timer(pinetime::interfaces::VirtualTimerInterface *);
void unregister_timer(pinetime::interfaces::VirtualTimerInterface *);
void notify(uint64_t);
uint64_t tick();
private:
static const uint32_t MAX_TIMERS = 8;
std::array<pinetime::interfaces::VirtualTimerInterface *, MAX_TIMERS> virtual_timer_list;
pinetime::interfaces::HwTimerInterface & hw_timer;
uint32_t num_registered_timers;
};
}
#endif