From 52d880375e7cec9287efa70def6d77a6317c37f4 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 25 Feb 2023 11:10:01 +0100 Subject: [PATCH] Initialisation des WS2812 (Neopixel) --- CMakeLists.txt | 9 ++++- main.c | 22 +++++++++--- ws2812.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ ws2812.h | 5 +++ ws2812.pio | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 ws2812.c create mode 100644 ws2812.h create mode 100644 ws2812.pio diff --git a/CMakeLists.txt b/CMakeLists.txt index cd64b91..8e89e8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,17 +15,24 @@ add_executable(Detection2023 vl53l1_platform.c VL53L1X_api.c VL53L1X_calibration.c + ws2812.c SelectionCapteur.c ) +# generate the header file into the source tree as it is included in the RP2040 datasheet +pico_generate_pio_header(Detection2023 ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio) + + target_link_libraries(Detection2023 hardware_i2c hardware_uart + hardware_pio pico_stdlib + ) pico_enable_stdio_usb(Detection2023 1) -pico_enable_stdio_uart(Detection2023 0) +pico_enable_stdio_uart(Detection2023 1) pico_add_extra_outputs(Detection2023) diff --git a/main.c b/main.c index e9051f5..9d1885b 100644 --- a/main.c +++ b/main.c @@ -4,18 +4,25 @@ #include "VL53L1X_api.h" #include "VL53L1X_calibration.h" #include "SelectionCapteur.h" +#include "hardware/pio.h" +#include "ws2812.h" +#include "ws2812.pio.h" #define I2C_SDA_PIN 0 #define I2C_SCL_PIN 1 + #define TEST_TIMEOUT_US 10000000 void i2c_master_init(void); int continuous_reading(uint8_t device); int calibration(uint8_t device); int change_address(uint8_t * device, uint8_t new_i2c_7bits_address); +void initialise_adresses(void); void display_menu(); + + void main(void) { int status; @@ -27,8 +34,8 @@ void main(void) Selection_capteur_init(); Selection_capteur_select(1); - printf("Waiting to allow user to connect USB serial\n"); - sleep_ms(3000); + ws2812_init(); + printf("End waiting\n"); while(1){ @@ -36,9 +43,7 @@ void main(void) display_menu(); do{ - stdio_flush(); keycode = getchar_timeout_us(TEST_TIMEOUT_US); - stdio_flush(); if(!answer_at_least_once){ display_menu(); } @@ -61,12 +66,21 @@ void main(void) answer_at_least_once=1; while(continuous_reading(VL53L1X_device)); break; + default : + break; } } } +void initialise_adresses(void){ + for(uint capteur=1; capteur<=12; capteur++){ + Selection_capteur_select(1); + } + +} + void display_menu(){ printf("Select action :\n"); printf("A - Change I2C address\n"); diff --git a/ws2812.c b/ws2812.c new file mode 100644 index 0000000..b07638e --- /dev/null +++ b/ws2812.c @@ -0,0 +1,91 @@ +#include "pico/stdlib.h" +#include "hardware/pio.h" +#include "ws2812.h" +#include "ws2812.pio.h" + +#define WS2812_PIN 7 +#define IS_RGBW false + + +uint32_t couleur[13]; +uint32_t buffer_couleur[12]; + +void ws2812_init(){ + /*couleur[0]=0x000200; + couleur[1]=0x010200; + couleur[2]=0x020200; + couleur[3]=0x020100; + couleur[4]=0x020000; + couleur[5]=0x020001; + couleur[6]=0x020002; + couleur[7]=0x010002; + couleur[8]=0x000002; + couleur[9]=0x000102; + couleur[10]=0x000202; + couleur[11]=0x000201; + couleur[12]=0x000200;*/ + + couleur[0]=0x002000; + couleur[1]=0x102000; + couleur[2]=0x202000; + couleur[3]=0x201000; + couleur[4]=0x200000; + couleur[5]=0x200010; + couleur[6]=0x200020; + couleur[7]=0x100020; + couleur[8]=0x000020; + couleur[9]=0x001020; + couleur[10]=0x002020; + couleur[11]=0x002010; + couleur[12]=0x002000; + + /*couleur[0]=0x00FF00; + couleur[1]=0x80FF00; + couleur[2]=0xFFFF00; + couleur[3]=0xFF8000; + couleur[4]=0xFF0000; + couleur[5]=0xFF0080; + couleur[6]=0xFF00FF; + couleur[7]=0x8000FF; + couleur[8]=0x0000FF; + couleur[9]=0x0080FF; + couleur[10]=0x00FFFF; + couleur[11]=0x00FF80; + couleur[12]=0x00FF00;*/ + + // initialisation du PIO + PIO pio = pio0; + int sm = 0; + uint offset = pio_add_program(pio, &ws2812_program); + + ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW); + + // Tout rouge ! + for(uint32_t i = 0; i<12; i++){ + ws2812_set_buffer_rgb(0x10, 0, 0, i); + } + ws2812_affiche_buffer(); +} + +static inline void put_pixel(uint32_t pixel_grb) { + pio_sm_put_blocking(pio0, 0, pixel_grb << 8u); +} + +static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) { + return + ((uint32_t) (r) << 8) | + ((uint32_t) (g) << 16) | + (uint32_t) (b); +} + +void ws2812_affiche_buffer(){ + for(uint32_t i = 0; i<12; i++){ + put_pixel(buffer_couleur[i]); + } +} + +void ws2812_set_buffer_rgb(uint8_t rouge, uint8_t vert, uint8_t bleu, uint8_t index_led){ + if(index_led <12){ + buffer_couleur[index_led] = urgb_u32(rouge, vert,bleu); + } +} \ No newline at end of file diff --git a/ws2812.h b/ws2812.h new file mode 100644 index 0000000..40cdf19 --- /dev/null +++ b/ws2812.h @@ -0,0 +1,5 @@ +#include "pico/stdlib.h" + +void ws2812_init(void); +void ws2812_affiche_buffer(void); +void ws2812_set_buffer_rgb(uint8_t rouge, uint8_t vert, uint8_t bleu, uint8_t index_led); \ No newline at end of file diff --git a/ws2812.pio b/ws2812.pio new file mode 100644 index 0000000..3c31fd6 --- /dev/null +++ b/ws2812.pio @@ -0,0 +1,85 @@ +; +; 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); +} +%} + +.program ws2812_parallel + +.define public T1 2 +.define public T2 5 +.define public T3 3 + +.wrap_target + out x, 32 + mov pins, !null [T1-1] + mov pins, x [T2-1] + mov pins, null [T3-2] +.wrap + +% c-sdk { +#include "hardware/clocks.h" + +static inline void ws2812_parallel_program_init(PIO pio, uint sm, uint offset, uint pin_base, uint pin_count, float freq) { + for(uint i=pin_base; i