forked from dangiu/PicoMemcard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,72 @@ | ||
#include "led.h" | ||
#include "config.h" | ||
#include "hardware/gpio.h" | ||
#include "hardware/pio.h" | ||
#ifdef RP2040ZERO | ||
#include "ws2812.pio.h" | ||
#endif | ||
|
||
#ifdef PICO | ||
#define PICO_LED_PIN 25 | ||
#endif | ||
|
||
void led_init() { | ||
static uint smWs2813; | ||
static uint offsetWs2813; | ||
|
||
#ifdef RP2040ZERO | ||
void ws2812_put_pixel(uint32_t pixel_grb) { | ||
pio_sm_put_blocking(pio1, smWs2813, pixel_grb << 8u); | ||
} | ||
void ws2812_put_rgb(uint8_t red, uint8_t green, uint8_t blue) { | ||
uint32_t mask = (green << 16) | (red << 8) | (blue << 0); | ||
ws2812_put_pixel(mask); | ||
} | ||
#endif | ||
|
||
void led_init() { | ||
#ifdef RP2040ZERO | ||
offsetWs2813 = pio_add_program(pio1, &ws2812_program); | ||
smWs2813 = pio_claim_unused_sm(pio1, true); | ||
ws2812_program_init(pio1, smWs2813, offsetWs2813, 16, 800000, true); | ||
#endif | ||
} | ||
|
||
void led_output_sync_status(bool out_of_sync) { | ||
#ifdef PICO | ||
gpio_put(PICO_LED_PIN, !out_of_sync); | ||
#endif | ||
#ifdef RP2040ZERO | ||
if(out_of_sync) | ||
ws2812_put_rgb(255, 255, 0); | ||
else | ||
ws2812_put_rgb(0, 255, 0); | ||
#endif | ||
} | ||
|
||
void led_blink(int amount, int on_ms, int off_ms) { | ||
bool initial_state = gpio_get(PICO_LED_PIN); | ||
if(initial_state) { | ||
/* turn led off if already on */ | ||
gpio_put(PICO_LED_PIN, false); | ||
sleep_ms(off_ms); | ||
--amount; // last blink is performed while restoring led state | ||
} | ||
void led_blink_error(int amount) { | ||
/* ensure led is off */ | ||
#ifdef PICO | ||
gpio_put(PICO_LED_PIN, false); | ||
#endif | ||
#ifdef RP2040ZERO | ||
ws2812_put_rgb(0, 0, 0); | ||
#endif | ||
sleep_ms(500); | ||
/* start blinking */ | ||
for(int i = 0; i < amount; ++i) { | ||
#ifdef PICO | ||
gpio_put(PICO_LED_PIN, true); | ||
sleep_ms(on_ms); | ||
#endif | ||
#ifdef RP2040ZERO | ||
ws2812_put_rgb(255, 0, 0); | ||
#endif | ||
sleep_ms(500); | ||
#ifdef PICO | ||
gpio_put(PICO_LED_PIN, false); | ||
sleep_ms(off_ms); | ||
#endif | ||
#ifdef RP2040ZERO | ||
ws2812_put_rgb(0, 0, 0); | ||
#endif | ||
sleep_ms(500); | ||
} | ||
if(initial_state) { | ||
/* restore led state */ | ||
gpio_put(PICO_LED_PIN, true); | ||
} | ||
} | ||
|
||
void led_blink_fast(int amount) { | ||
led_blink(amount, 250, 250); | ||
} | ||
|
||
void led_blink_normal(int amount) { | ||
led_blink(amount, 500, 500); | ||
} | ||
|
||
void led_blink_slow(int amount) { | ||
led_blink(amount, 1000, 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
; | ||
; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. | ||
; | ||
; SPDX-License-Identifier: BSD-3-Clause | ||
; | ||
|
||
.program ws2812 | ||
.side_set 1 | ||
|
||
.define public T1 2 | ||
.define public T2 5 | ||
.define public T3 3 | ||
|
||
.lang_opt python sideset_init = pico.PIO.OUT_HIGH | ||
.lang_opt python out_init = pico.PIO.OUT_HIGH | ||
.lang_opt python out_shiftdir = 1 | ||
|
||
.wrap_target | ||
bitloop: | ||
out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls | ||
jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse | ||
do_one: | ||
jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse | ||
do_zero: | ||
nop side 0 [T2 - 1] ; Or drive low, for a short pulse | ||
.wrap | ||
|
||
% c-sdk { | ||
#include "hardware/clocks.h" | ||
|
||
static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) { | ||
|
||
pio_gpio_init(pio, pin); | ||
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); | ||
|
||
pio_sm_config c = ws2812_program_get_default_config(offset); | ||
sm_config_set_sideset_pins(&c, pin); | ||
sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24); | ||
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); | ||
|
||
int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3; | ||
float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); | ||
sm_config_set_clkdiv(&c, div); | ||
|
||
pio_sm_init(pio, sm, offset, &c); | ||
pio_sm_set_enabled(pio, sm, true); | ||
} | ||
%} |