From 683703a4720681107d17f44eae774d8c50797af7 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 1 Oct 2023 08:44:30 +0200 Subject: Backup before messing with DMA --- pixelhub.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 37 deletions(-) diff --git a/pixelhub.cpp b/pixelhub.cpp index 506e1a1..660ecc7 100644 --- a/pixelhub.cpp +++ b/pixelhub.cpp @@ -35,24 +35,66 @@ struct pixel_strip uint offset; uint32_t pixelbuf[256]; int rgbw; + uint dma_chan; }; -static inline void put_pixel(struct pixel_strip *p, uint32_t pixel_grb) { +static inline void put_pixel(struct pixel_strip *p, uint32_t pixel_grb) +{ pio_sm_put_blocking(p->pio, p->pio_sm, pixel_grb); } -static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) { +static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) +{ return ((uint32_t)(g)<<24) | ((uint32_t)(r)<<16) | ((uint32_t)(b)<<8); } -static inline uint32_t wrgb_u32(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { +static inline uint32_t wrgb_u32(uint8_t r, uint8_t g, uint8_t b, uint8_t w) +{ return ((uint32_t)(g)<<24) | ((uint32_t)(r)<<16) | ((uint32_t)(b)<<8) | (uint32_t)(w); } +uint dma_chan; +dma_channel_config dma_conf; +struct pixel_strip strips[8]; +int n_strips; +int cur_strip = 0; + + +static void set_dma() +{ + struct pixel_strip *p = &strips[cur_strip]; + channel_config_set_dreq(&dma_conf, + pio_get_dreq(p->pio, p->pio_sm, true)); + dma_channel_configure(p->dma_chan, &dma_conf, + &p->pio->txf[p->pio_sm], /* Dest */ + &p->pixelbuf, /* Source */ + p->num_pixels, + true); +} + + +static int64_t next_strip(alarm_id_t id, void *vp) +{ + cur_strip++; + if ( cur_strip >= n_strips ) cur_strip = 0; + set_dma(); + return 0; +} + + +static void dma_complete() +{ + if ( dma_hw->ints0 & 1<ints0 = 1<pio, p->pio_sm, p->offset, p->pin, 800000, p->rgbw); for ( i=0; inum_pixels; i++ ) { - p->pixelbuf[i] = 0; + p->pixelbuf[i] = 127; } - } int main() { DmxInput dmx_in; - struct pixel_strip p1; - struct pixel_strip p2; uint8_t dmxbuf[512]; int i; int blink = 1; - p1.pin = 2; - p1.num_pixels = 8; - p1.rgbw = false; - p1.pio = pio0; + strips[0].pin = 2; + strips[0].num_pixels = 8; + strips[0].rgbw = false; + strips[0].pio = pio0; - p2.pin = 3; - p2.num_pixels = 1; - p2.rgbw = true; - p2.pio = pio0; + strips[1].pin = 3; + strips[1].num_pixels = 1; + strips[1].rgbw = true; + strips[1].pio = pio0; + + n_strips = 1; /* Initialise DMX */ dmx_in.begin(dmx_rx_pin, 1, 512); /* Initialise Neopixels */ - init_pixel_strip(&p1); - init_pixel_strip(&p2); - int npx = 8; + init_pixel_strip(&strips[0]); + init_pixel_strip(&strips[1]); + + /* Start DMA */ + dma_chan = dma_claim_unused_channel(true); + dma_conf = dma_channel_get_default_config(dma_chan); + channel_config_set_read_increment(&dma_conf, true); + channel_config_set_write_increment(&dma_conf, false); + //irq_set_exclusive_handler(DMA_IRQ_0, dma_complete); + //dma_channel_set_irq0_enabled(dma_chan, true); + //irq_set_enabled(DMA_IRQ_0, true); + cur_strip = 0; + //set_dma(); + + struct pixel_strip *p = &strips[0]; + channel_config_set_dreq(&dma_conf, + pio_get_dreq(p->pio, p->pio_sm, true)); + dma_channel_configure(p->dma_chan, &dma_conf, + &p->pio->txf[p->pio_sm], /* Dest */ + p->pixelbuf, /* Source */ + p->num_pixels, + true); /* Status LED */ gpio_init(PICO_DEFAULT_LED_PIN); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + int loop = 0; while (1) { - dmx_in.read(dmxbuf); - - int dmxpos = 1; /* Numbering starts at 1 */ - for ( i=0; i 1000000 ) { + loop = 0; + blink = 1 - blink; + } } } -- cgit v1.2.3