-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_rp2040.cpp
66 lines (42 loc) · 1.94 KB
/
led_rp2040.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
////////////////////////////////////////////////////////////////////////////////
// https://cec-code-lab.aps.edu/robotics/resources/pico-c-api/systick_8h_source.html
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// INCLUDE FILES
////////////////////////////////////////////////////////////////////////////////
#include <arduino.h>
#include "led.h"
////////////////////////////////////////////////////////////////////////////////
// THIS FILE ONLY APPLIES TO THE RASPBERRY PI RO2040 BASED CHIPS/BOARDS
////////////////////////////////////////////////////////////////////////////////
#if defined(ARDUINO_ARCH_RP2040)
////////////////////////////////////////////////////////////////////////////////
// DYNAMICALLY CALCULATE THE CLOCK DELAYS
////////////////////////////////////////////////////////////////////////////////
void led::clock() {
float clockspeed;
clockspeed = static_cast<float>(rp2040.f_cpu());
CYCLES_T0H = static_cast<uint32_t>(clockspeed * 0.000000350f);
CYCLES_T1H = static_cast<uint32_t>(clockspeed * 0.000000700f);
CYCLES = static_cast<uint32_t>(clockspeed * 0.000001250f);
}
////////////////////////////////////////////////////////////////////////////////
// SEND A FULL PIXEL TO THE LED STRIP
////////////////////////////////////////////////////////////////////////////////
void led::pixel(const color_t &color) {
uint32_t mask = 1 << _pin;
uint32_t data = (_mode==LED_RGB) ? ((uint32_t)color) : (color.grb());
uint32_t start = 0;
uint32_t pause = 0;
for (int32_t i=23; i>=0; i--) {
pause = (data & (1<<i)) ? CYCLES_T1H : CYCLES_T0H;
start = systick_hw->cvr;
//WRITE HIGH VALUE AND PAUSE
sio_hw->gpio_set = mask;
while ((start - systick_hw->cvr) < pause) {}
//WRITE LOW VALUE AND PAUSE
sio_hw->gpio_clr = mask;
while ((start - systick_hw->cvr) < CYCLES) {}
}
}
#endif //defined(ARDUINO_ARCH_RP2040)