Skip to content

Commit 55b511a

Browse files
committed
Use a dedicated timer
1 parent 88e4019 commit 55b511a

11 files changed

Lines changed: 104 additions & 38 deletions

File tree

ports/atmel-samd/boards/pewpew70/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
#include "internal_flash.h"
1111

12-
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x014000)
12+
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

ports/atmel-samd/boards/pewpew70/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ LONGINT_IMPL = NONE
99

1010
CHIP_VARIANT = SAMD21E18A
1111
CHIP_FAMILY = samd21
12+
13+
FROZEN_MPY_DIRS += $(TOP)/frozen/pewpew70

ports/atmel-samd/boards/samd21x18-bootloader-crystalless.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
MEMORY
77
{
88
/* Leave 8KiB for the bootloader, 256b for persistent config (clock), 64k for the flash file system and 256b for the user config. */
9-
FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K - 256 - 80K - 256
9+
FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K - 256 - 64K - 256
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
1111
}
1212

ports/atmel-samd/supervisor/internal_flash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#endif
3838

3939
#ifdef SAMD21
40-
#define TOTAL_INTERNAL_FLASH_SIZE 0x014000
40+
#define TOTAL_INTERNAL_FLASH_SIZE 0x010000
4141
#endif
4242

4343
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)

ports/atmel-samd/supervisor/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#include "shared-module/gamepad/__init__.h"
7373
#endif
7474
#ifdef CIRCUITPY_PEWPEW_TICKS
75-
#include "shared-module/_pew/__init__.h"
75+
#include "shared-module/_pew/PewPew.h"
7676
#endif
7777

7878
extern volatile bool mp_msc_enabled;

ports/atmel-samd/tick.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ void SysTick_Handler(void) {
5555
gamepad_tick();
5656
}
5757
#endif
58-
#ifdef CIRCUITPY_PEWPEW_TICKS
59-
if (!(ticks_ms & CIRCUITPY_PEWPEW_TICKS)) {
60-
pew_tick();
61-
}
62-
#endif
6358
}
6459

6560
void tick_init() {

shared-bindings/_pew/PewPew.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args,
110110
pew->rows_size = rows_size;
111111
pew->cols = cols;
112112
pew->cols_size = cols_size;
113-
pew->col = 0;
114-
pew->turn = 0;
115113
pew_init();
116114

117115
return MP_OBJ_FROM_PTR(pew);

shared-module/_pew/PewPew.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,30 @@
2727
#include <stdbool.h>
2828

2929
#include "py/mpstate.h"
30+
#include "py/runtime.h"
3031
#include "__init__.h"
3132
#include "PewPew.h"
3233

3334
#include "shared-bindings/digitalio/Pull.h"
3435
#include "shared-bindings/digitalio/DigitalInOut.h"
3536
#include "shared-bindings/util.h"
37+
#include "samd/timers.h"
3638

3739

40+
static uint8_t pewpew_tc_index = 0xff;
41+
42+
43+
void pewpew_interrupt_handler(uint8_t index) {
44+
if (index != pewpew_tc_index) return;
45+
Tc* tc = tc_insts[index];
46+
if (!tc->COUNT16.INTFLAG.bit.MC0) return;
47+
48+
pew_tick();
49+
50+
// Clear the interrupt bit.
51+
tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
52+
}
53+
3854
void pew_init() {
3955
pew_obj_t* pew_singleton = MP_STATE_VM(pew_singleton);
4056
for (size_t i = 0; i < pew_singleton->rows_size; ++i) {
@@ -49,4 +65,57 @@ void pew_init() {
4965
common_hal_digitalio_digitalinout_switch_to_output(pin, true,
5066
DRIVE_MODE_OPEN_DRAIN);
5167
}
68+
if (pewpew_tc_index == 0xff) {
69+
// Find a spare timer.
70+
Tc *tc = NULL;
71+
int8_t index = TC_INST_NUM - 1;
72+
for (; index >= 0; index--) {
73+
if (tc_insts[index]->COUNT16.CTRLA.bit.ENABLE == 0) {
74+
tc = tc_insts[index];
75+
break;
76+
}
77+
}
78+
if (tc == NULL) {
79+
mp_raise_RuntimeError("All timers in use");
80+
}
81+
82+
pewpew_tc_index = index;
83+
84+
// We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run
85+
// at 48mhz making our math the same across the boards.
86+
#ifdef SAMD21
87+
turn_on_clocks(true, index, 0);
88+
#endif
89+
#ifdef SAMD51
90+
turn_on_clocks(true, index, 1);
91+
#endif
92+
93+
94+
#ifdef SAMD21
95+
tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 |
96+
TC_CTRLA_PRESCALER_DIV64 |
97+
TC_CTRLA_WAVEGEN_MFRQ;
98+
#endif
99+
#ifdef SAMD51
100+
tc_reset(tc);
101+
tc_set_enable(tc, false);
102+
tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16
103+
| TC_CTRLA_PRESCALER_DIV64;
104+
tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ;
105+
#endif
106+
107+
tc_set_enable(tc, true);
108+
//tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP;
109+
tc->COUNT16.CC[0].reg = 160;
110+
111+
// Clear our interrupt in case it was set earlier
112+
tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
113+
tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0;
114+
tc_enable_interrupts(pewpew_tc_index);
115+
}
116+
}
117+
118+
void pew_reset(void) {
119+
MP_STATE_VM(pew_singleton) = NULL;
120+
pewpew_tc_index = 0xff;
52121
}

shared-module/_pew/PewPew.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@
2929

3030
#include <stdint.h>
3131

32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
3432
typedef struct {
3533
mp_obj_base_t base;
3634
uint8_t* buffer;
3735
mp_obj_t* rows;
3836
mp_obj_t* cols;
39-
size_t rows_size;
40-
size_t cols_size;
41-
volatile uint8_t col;
42-
volatile uint8_t turn;
37+
uint8_t rows_size;
38+
uint8_t cols_size;
4339
} pew_obj_t;
4440

4541
void pew_init(void);
42+
void pewpew_interrupt_handler(uint8_t index);
43+
void pew_reset(void);
4644

4745
#endif // MICROPY_INCLUDED_PEW_PEWPEW_H

shared-module/_pew/__init__.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,44 @@
3434

3535

3636
void pew_tick(void) {
37+
static uint8_t col = 0;
38+
static uint8_t turn = 0;
3739
digitalio_digitalinout_obj_t *pin;
40+
3841
pew_obj_t* pew = MP_STATE_VM(pew_singleton);
3942
if (!pew) { return; }
4043

41-
pin = MP_OBJ_TO_PTR(pew->cols[pew->col]);
42-
common_hal_digitalio_digitalinout_set_value(pin, true);
43-
pew->col += 1;
44-
if (pew->col >= pew->cols_size) {
45-
pew->col = 0;
46-
pew->turn += 1;
47-
if (pew->turn >= 4) {
48-
pew->turn = 0;
44+
pin = MP_OBJ_TO_PTR(pew->cols[col]);
45+
++col;
46+
if (col >= pew->cols_size) {
47+
col = 0;
48+
++turn;
49+
if (turn >= 8) {
50+
turn = 0;
4951
}
5052
}
53+
common_hal_digitalio_digitalinout_set_value(pin, true);
5154
for (size_t x = 0; x < pew->rows_size; ++x) {
5255
pin = MP_OBJ_TO_PTR(pew->rows[x]);
53-
uint8_t color = pew->buffer[(pew->col) * (pew->rows_size) + x];
54-
bool value = true;
55-
switch (pew->turn) {
56-
case 0:
57-
if (color & 0x03) { value = true; }
56+
uint8_t color = pew->buffer[col * (pew->rows_size) + x];
57+
bool value = false;
58+
switch (color & 0x03) {
59+
case 3:
60+
value = true;
5861
break;
59-
case 1:
6062
case 2:
61-
if (color & 0x02) { value = true; }
63+
if (turn == 2 || turn == 4 || turn == 6) {
64+
value = true;
65+
}
66+
case 1:
67+
if (turn == 0) {
68+
value = true;
69+
}
70+
case 0:
6271
break;
6372
}
6473
common_hal_digitalio_digitalinout_set_value(pin, value);
6574
}
66-
pin = MP_OBJ_TO_PTR(pew->cols[pew->col]);
75+
pin = MP_OBJ_TO_PTR(pew->cols[col]);
6776
common_hal_digitalio_digitalinout_set_value(pin, false);
6877
}
69-
70-
void pew_reset(void) {
71-
MP_STATE_VM(pew_singleton) = NULL;
72-
}

0 commit comments

Comments
 (0)