diff options
| author | Radomir Dopieralski <openstack@sheep.art.pl> | 2018-08-09 22:35:26 +0200 |
|---|---|---|
| committer | Radomir Dopieralski <openstack@sheep.art.pl> | 2019-02-28 23:33:37 +0100 |
| commit | 55b511a5d887c5e02031d40c661b31fce336c488 (patch) | |
| tree | 6d11de0516b96fe012ae1cc9a543a96c51c9f7f2 /shared-module/_pew | |
| parent | 88e40193ae92a390ff0cc8872ec2b7cd953c83cb (diff) | |
Use a dedicated timer
Diffstat (limited to 'shared-module/_pew')
| -rw-r--r-- | shared-module/_pew/PewPew.c | 69 | ||||
| -rw-r--r-- | shared-module/_pew/PewPew.h | 10 | ||||
| -rw-r--r-- | shared-module/_pew/__init__.c | 45 | ||||
| -rw-r--r-- | shared-module/_pew/__init__.h | 1 |
4 files changed, 98 insertions, 27 deletions
diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c index ede8ed9bb..de1224a37 100644 --- a/shared-module/_pew/PewPew.c +++ b/shared-module/_pew/PewPew.c @@ -27,14 +27,30 @@ #include <stdbool.h> #include "py/mpstate.h" +#include "py/runtime.h" #include "__init__.h" #include "PewPew.h" #include "shared-bindings/digitalio/Pull.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/util.h" +#include "samd/timers.h" +static uint8_t pewpew_tc_index = 0xff; + + +void pewpew_interrupt_handler(uint8_t index) { + if (index != pewpew_tc_index) return; + Tc* tc = tc_insts[index]; + if (!tc->COUNT16.INTFLAG.bit.MC0) return; + + pew_tick(); + + // Clear the interrupt bit. + tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0; +} + void pew_init() { pew_obj_t* pew_singleton = MP_STATE_VM(pew_singleton); for (size_t i = 0; i < pew_singleton->rows_size; ++i) { @@ -49,4 +65,57 @@ void pew_init() { common_hal_digitalio_digitalinout_switch_to_output(pin, true, DRIVE_MODE_OPEN_DRAIN); } + if (pewpew_tc_index == 0xff) { + // Find a spare timer. + Tc *tc = NULL; + int8_t index = TC_INST_NUM - 1; + for (; index >= 0; index--) { + if (tc_insts[index]->COUNT16.CTRLA.bit.ENABLE == 0) { + tc = tc_insts[index]; + break; + } + } + if (tc == NULL) { + mp_raise_RuntimeError("All timers in use"); + } + + pewpew_tc_index = index; + + // We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run + // at 48mhz making our math the same across the boards. + #ifdef SAMD21 + turn_on_clocks(true, index, 0); + #endif + #ifdef SAMD51 + turn_on_clocks(true, index, 1); + #endif + + + #ifdef SAMD21 + tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | + TC_CTRLA_PRESCALER_DIV64 | + TC_CTRLA_WAVEGEN_MFRQ; + #endif + #ifdef SAMD51 + tc_reset(tc); + tc_set_enable(tc, false); + tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 + | TC_CTRLA_PRESCALER_DIV64; + tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ; + #endif + + tc_set_enable(tc, true); + //tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP; + tc->COUNT16.CC[0].reg = 160; + + // Clear our interrupt in case it was set earlier + tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0; + tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0; + tc_enable_interrupts(pewpew_tc_index); + } +} + +void pew_reset(void) { + MP_STATE_VM(pew_singleton) = NULL; + pewpew_tc_index = 0xff; } diff --git a/shared-module/_pew/PewPew.h b/shared-module/_pew/PewPew.h index b21ff0137..2f25f9ce2 100644 --- a/shared-module/_pew/PewPew.h +++ b/shared-module/_pew/PewPew.h @@ -29,19 +29,17 @@ #include <stdint.h> -#include "shared-bindings/digitalio/DigitalInOut.h" - typedef struct { mp_obj_base_t base; uint8_t* buffer; mp_obj_t* rows; mp_obj_t* cols; - size_t rows_size; - size_t cols_size; - volatile uint8_t col; - volatile uint8_t turn; + uint8_t rows_size; + uint8_t cols_size; } pew_obj_t; void pew_init(void); +void pewpew_interrupt_handler(uint8_t index); +void pew_reset(void); #endif // MICROPY_INCLUDED_PEW_PEWPEW_H diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c index 92637a04d..2e09932f3 100644 --- a/shared-module/_pew/__init__.c +++ b/shared-module/_pew/__init__.c @@ -34,39 +34,44 @@ void pew_tick(void) { + static uint8_t col = 0; + static uint8_t turn = 0; digitalio_digitalinout_obj_t *pin; + pew_obj_t* pew = MP_STATE_VM(pew_singleton); if (!pew) { return; } - pin = MP_OBJ_TO_PTR(pew->cols[pew->col]); - common_hal_digitalio_digitalinout_set_value(pin, true); - pew->col += 1; - if (pew->col >= pew->cols_size) { - pew->col = 0; - pew->turn += 1; - if (pew->turn >= 4) { - pew->turn = 0; + pin = MP_OBJ_TO_PTR(pew->cols[col]); + ++col; + if (col >= pew->cols_size) { + col = 0; + ++turn; + if (turn >= 8) { + turn = 0; } } + common_hal_digitalio_digitalinout_set_value(pin, true); for (size_t x = 0; x < pew->rows_size; ++x) { pin = MP_OBJ_TO_PTR(pew->rows[x]); - uint8_t color = pew->buffer[(pew->col) * (pew->rows_size) + x]; - bool value = true; - switch (pew->turn) { - case 0: - if (color & 0x03) { value = true; } + uint8_t color = pew->buffer[col * (pew->rows_size) + x]; + bool value = false; + switch (color & 0x03) { + case 3: + value = true; break; - case 1: case 2: - if (color & 0x02) { value = true; } + if (turn == 2 || turn == 4 || turn == 6) { + value = true; + } + case 1: + if (turn == 0) { + value = true; + } + case 0: break; } common_hal_digitalio_digitalinout_set_value(pin, value); } - pin = MP_OBJ_TO_PTR(pew->cols[pew->col]); + pin = MP_OBJ_TO_PTR(pew->cols[col]); common_hal_digitalio_digitalinout_set_value(pin, false); } - -void pew_reset(void) { - MP_STATE_VM(pew_singleton) = NULL; -} diff --git a/shared-module/_pew/__init__.h b/shared-module/_pew/__init__.h index 30c9c8c93..f85dec749 100644 --- a/shared-module/_pew/__init__.h +++ b/shared-module/_pew/__init__.h @@ -28,6 +28,5 @@ #define MICROPY_INCLUDED_PEW_H void pew_tick(void); -void pew_reset(void); #endif // MICROPY_INCLUDED_PEW_H |
