Skip to content

Commit 268609f

Browse files
committed
Implement LED PWM control
1 parent 67af118 commit 268609f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

main.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@
22

33
#include "am1808.h"
44

5+
// Shared RAM (control input)
6+
typedef struct shared_ram {
7+
// Declare this as a u32 to force more-efficient codegen
8+
uint32_t pwms;
9+
} shared_ram;
10+
static volatile shared_ram * const SHARED = (volatile shared_ram *)0x80010000;
11+
// XXX this doesn't seem to work (compiler won't generate code to use it)
12+
// #pragma ctable_entry 30 0x80010000
13+
14+
// LED definitions (all on GP6)
15+
#define LED0 (1 << 13)
16+
#define LED1 (1 << 7)
17+
#define LED2 (1 << 14)
18+
#define LED3 (1 << 12)
19+
20+
static inline void update_pwm(uint8_t val, uint8_t time_now, uint32_t gpio_bit) {
21+
if (time_now < val) {
22+
GPIO->set_data67 = gpio_bit;
23+
} else {
24+
GPIO->clr_data67 = gpio_bit;
25+
}
26+
}
27+
528
void main() {
6-
__halt();
29+
uint32_t pwms = 0;
30+
while (1) {
31+
// 24 MHz / 256 ==> 93.75 kHz tick rate for this counter
32+
uint8_t time_now = (*TIMER0_TIM34) >> 8;
33+
34+
if (time_now == 0) {
35+
// 24 MHz / 256 / 256 ~= 366 Hz update rate
36+
pwms = SHARED->pwms;
37+
}
38+
39+
update_pwm(pwms >> 0, time_now, LED0);
40+
update_pwm(pwms >> 8, time_now, LED1);
41+
update_pwm(pwms >> 16, time_now, LED2);
42+
update_pwm(pwms >> 24, time_now, LED3);
43+
}
744
}

0 commit comments

Comments
 (0)