Skip to content

Commit 0896e56

Browse files
committed
vaguely working all the embedded bits
1 parent 6e18330 commit 0896e56

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

firmware/.gdbinit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tar ext /dev/cu.usbmodemDDC9D6D11
2-
mon tpwr en
2+
# mon tpwr en
33
mon sw
44
at 1
55
set mem inaccessible-by-default off

firmware/led.c

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "led.h"
22
#include "registers.h"
33

4+
uint32_t led_data[NUM_SECTORS];
5+
int led_current_sector;
6+
47
static inline void set_hi(int pin) {
58
GPIOA_BSRR = 1 << pin;
69
GPIOA_MODER &= ~(0b10 << (pin * 2));
@@ -48,10 +51,29 @@ const uint8_t led_pins[NUM_LEDS * 2] = {
4851
2, 3,
4952
3, 2,
5053
};
51-
void turn_on_led(int led) {
52-
// make everything (relevant) tristate
53-
GPIOA_MODER |= 0xfff;
54+
static inline void turn_on_led(int led) {
5455
// turn on *just* this LED
5556
set_hi(led_pins[led * 2 + 0]);
5657
set_lo(led_pins[led * 2 + 1]);
5758
}
59+
60+
uint32_t current_led;
61+
void SysTick_Handler() {
62+
uint32_t current_word = led_data[led_current_sector];
63+
64+
// turn off leds
65+
GPIOA_MODER |= 0xfff;
66+
67+
if (current_word & (1 << current_led))
68+
turn_on_led(current_led);
69+
70+
current_led++;
71+
if (current_led == NUM_LEDS) {
72+
current_led = 0;
73+
74+
// // test
75+
// led_current_sector++;
76+
// if (led_current_sector == NUM_SECTORS)
77+
// led_current_sector = 0;
78+
}
79+
}

firmware/led.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#pragma once
2+
#include <stdint.h>
23

34
#define NUM_LEDS 30
4-
void turn_on_led(int led);
5+
6+
#define NUM_SECTORS 128
7+
extern uint32_t led_data[NUM_SECTORS];
8+
extern int led_current_sector;

firmware/main.c

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
uint8_t debug_accel_whoami;
77
uint8_t debug_accel_ctrl_readback[6];
8-
#define DEBUG_BUF_NENTS 1500
8+
#define DEBUG_BUF_NENTS 1200
99
int16_t debug_accel_buffer[DEBUG_BUF_NENTS];
1010
uint32_t debug_accel_buffer_idx;
1111

@@ -41,11 +41,22 @@ void main() {
4141
accel_write_reg(0x25, 0b00000000);
4242
accel_read_multi_reg(0x20, 6, debug_accel_ctrl_readback);
4343

44+
/// xxx dummy data
45+
for (int i = 0; i < NUM_SECTORS; i++) {
46+
led_data[i] = (1 << (i % (NUM_LEDS + 1))) - 1;
47+
}
48+
49+
/// systick setup, 24 kHz = 30 LEDs * 400 Hz * 2x doubling
50+
SYST_CVR = 0;
51+
SYST_RVR = 999;
52+
SYST_CSR = 0b111;
53+
4454
/// PB3 = accel interrupt
4555
GPIOB_MODER &= ~(0b11 << 6);
4656
EXTI_EXTICR1 = 0b01 << 24;
4757
EXTI_RTSR = 1 << 3;
4858
EXTI_IMR |= 1 << 3;
59+
NVIC_IPR1 = 0xc0 << 16; // lower prio than led
4960

5061
// initial read to make sure interrupt pin is cleared
5162
while (!(GPIOB_IDR & (1 << 3))) {}
@@ -76,10 +87,9 @@ void EXTI2_3_IRQHandler() {
7687

7788
// maths!
7889
float angle = atan2f(-y, x);
79-
80-
float xxx_led_f = angle * 15.0f / (float)(M_PI);
81-
int xxx_led = floorf(xxx_led_f);
82-
if (xxx_led < 0)
83-
xxx_led += 30;
84-
turn_on_led(xxx_led);
90+
float led_sector_f = angle * ((float)(NUM_SECTORS / 2) / (float)(M_PI));
91+
int led_sector = floorf(led_sector_f);
92+
if (led_sector < 0)
93+
led_sector += NUM_SECTORS;
94+
led_current_sector = led_sector;
8595
}

firmware/registers.h

+13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
#define GPIOF_AFRH (*(volatile uint32_t*)0x50001424)
9292
#define GPIOF_BRR (*(volatile uint32_t*)0x50001428)
9393

94+
#define SYST_CSR (*(volatile uint32_t*)0xe000e010)
95+
#define SYST_RVR (*(volatile uint32_t*)0xe000e014)
96+
#define SYST_CVR (*(volatile uint32_t*)0xe000e018)
97+
#define SYST_CALIB (*(volatile uint32_t*)0xe000e01c)
98+
9499
#define NVIC_ISER (*(volatile uint32_t*)0xe000e100)
95100
#define NVIC_ICER (*(volatile uint32_t*)0xe000e180)
96101
#define NVIC_ISPR (*(volatile uint32_t*)0xe000e200)
@@ -103,3 +108,11 @@
103108
#define NVIC_IPR5 (*(volatile uint32_t*)0xe000e414)
104109
#define NVIC_IPR6 (*(volatile uint32_t*)0xe000e418)
105110
#define NVIC_IPR7 (*(volatile uint32_t*)0xe000e41c)
111+
112+
#define SCB_CPUID (*(volatile uint32_t*)0xe000ed00)
113+
#define SCB_ICSR (*(volatile uint32_t*)0xe000ed04)
114+
#define SCB_AIRCR (*(volatile uint32_t*)0xe000ed0c)
115+
#define SCB_SCR (*(volatile uint32_t*)0xe000ed10)
116+
#define SCB_CCR (*(volatile uint32_t*)0xe000ed14)
117+
#define SCB_SHPR2 (*(volatile uint32_t*)0xe000ed1c)
118+
#define SCB_SHPR3 (*(volatile uint32_t*)0xe000ed20)

0 commit comments

Comments
 (0)