Skip to content

Commit 4f87d35

Browse files
committed
Implement LED PWM control
1 parent 3f11f23 commit 4f87d35

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

main.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,51 @@
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+
// XXX: The real address in use here is 0x80010000
11+
// There appears to be a compiler bug where ctable entries with the MSB set
12+
// do not get optimized correctly. We lie to the PRU compiler here, but the
13+
// hardware indeed contains the correct address we *actually* want.
14+
static volatile shared_ram * const SHARED = (volatile shared_ram *)0x7f010000;
15+
#pragma ctable_entry 30 0x7f010000
16+
17+
// LED definitions
18+
// LEDx corresponds to DIODEx in the EV3 schematics
19+
// LEDx defined as n ==> pin is PRU1_R30[n]
20+
#define LED0 12
21+
#define LED1 10
22+
#define LED2 13
23+
#define LED3 11
24+
25+
static inline void update_pwm(uint8_t val, uint8_t time_now, uint32_t gpio_bit) {
26+
// We want to force generation of the optimized set/clr opcodes
27+
if (time_now < val) {
28+
asm volatile("set r30, r30, %0"::"I"(gpio_bit));
29+
} else {
30+
asm volatile("clr r30, r30, %0"::"I"(gpio_bit));
31+
}
32+
}
33+
534
void main() {
6-
__halt();
35+
uint32_t pwms = 0;
36+
while (1) {
37+
// 24 MHz / 256 ==> 93.75 kHz tick rate for this counter
38+
uint8_t time_now = (*TIMER0_TIM34) >> 8;
39+
40+
if (time_now == 0) {
41+
// 24 MHz / 256 / 256 ~= 366 Hz update rate
42+
pwms = SHARED->pwms;
43+
}
44+
45+
update_pwm(pwms >> 0, time_now, LED0);
46+
update_pwm(pwms >> 8, time_now, LED1);
47+
// Intentional swap of 2 and 3
48+
// This puts the pins into the order R, G, R, G
49+
update_pwm(pwms >> 16, time_now, LED3);
50+
update_pwm(pwms >> 24, time_now, LED2);
51+
}
752
}

0 commit comments

Comments
 (0)