diff options
author | Thomas White <taw@physics.org> | 2023-10-01 09:03:44 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2023-10-01 09:03:44 +0200 |
commit | f32a4f9e24711e20390977503068c42a9f0b92a2 (patch) | |
tree | 01d7510e19a352e495c4488629b26046d5416b66 | |
parent | 683703a4720681107d17f44eae774d8c50797af7 (diff) |
Working with DMA
-rw-r--r-- | pixelhub.cpp | 68 |
1 files changed, 33 insertions, 35 deletions
diff --git a/pixelhub.cpp b/pixelhub.cpp index 660ecc7..6a1e36b 100644 --- a/pixelhub.cpp +++ b/pixelhub.cpp @@ -59,14 +59,14 @@ static inline uint32_t wrgb_u32(uint8_t r, uint8_t g, uint8_t b, uint8_t w) uint dma_chan; dma_channel_config dma_conf; -struct pixel_strip strips[8]; -int n_strips; +struct pixel_strip pxs[8]; +int n_pxs; int cur_strip = 0; static void set_dma() { - struct pixel_strip *p = &strips[cur_strip]; + struct pixel_strip *p = &pxs[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, @@ -80,7 +80,7 @@ static void set_dma() static int64_t next_strip(alarm_id_t id, void *vp) { cur_strip++; - if ( cur_strip >= n_strips ) cur_strip = 0; + if ( cur_strip >= n_pxs ) cur_strip = 0; set_dma(); return 0; } @@ -95,17 +95,23 @@ static void dma_complete() } -static void init_pixel_strip(struct pixel_strip *p) +static void init_pixel_strip(struct pixel_strip *p, PIO pio, int sm) { int i; - p->pio_sm = pio_claim_unused_sm(p->pio, true); + p->pio = pio; + p->pio_sm = sm; p->offset = pio_add_program(p->pio, &ws2812_program); ws2812_program_init(p->pio, p->pio_sm, p->offset, p->pin, 800000, p->rgbw); for ( i=0; i<p->num_pixels; i++ ) { - p->pixelbuf[i] = 127; + p->pixelbuf[i] = urgb_u32(32, 0, 32); } + + pio_sm_set_enabled(pio, sm, false); + pio_sm_clear_fifos(pio, sm); + pio_sm_restart(pio, sm); + pio_sm_set_enabled(pio, sm, true); } @@ -116,43 +122,34 @@ int main() int i; int blink = 1; - strips[0].pin = 2; - strips[0].num_pixels = 8; - strips[0].rgbw = false; - strips[0].pio = pio0; + pxs[0].pin = 2; + pxs[0].num_pixels = 8; + pxs[0].rgbw = false; - strips[1].pin = 3; - strips[1].num_pixels = 1; - strips[1].rgbw = true; - strips[1].pio = pio0; + pxs[1].pin = 3; + pxs[1].num_pixels = 1; + pxs[1].rgbw = true; - n_strips = 1; + n_pxs = 1; /* Initialise DMX */ dmx_in.begin(dmx_rx_pin, 1, 512); /* Initialise Neopixels */ - init_pixel_strip(&strips[0]); - init_pixel_strip(&strips[1]); + init_pixel_strip(&pxs[0], pio0, 0); + //init_pixel_strip(&pxs[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, + pio_get_dreq(pxs[0].pio, pxs[0].pio_sm, true)); + dma_channel_configure(dma_chan, &dma_conf, + &pxs[0].pio->txf[pxs[0].pio_sm], /* Dest */ + pxs[0].pixelbuf, /* Source */ + pxs[0].num_pixels, true); /* Status LED */ @@ -165,16 +162,17 @@ int main() //dmx_in.read(dmxbuf); //int dmxpos = 1; /* Numbering starts at 1 */ - //for ( i=0; i<strips[0].num_pixels; i++) { - // strips[0].pixelbuf[i] = urgb_u32(0, 0, 32); + //for ( i=0; i<pxs[0].num_pixels; i++) { + // put_pixel(&pxs[0], urgb_u32(32, 0, 0)); //} - //for ( i=0; i<strips[1].num_pixels; i++) { - // strips[1].pixelbuf[i] = wrgb_u32(0, 64, 0, 0); + //sleep_ms(10); + //for ( i=0; i<pxs[1].num_pixels; i++) { + // pxs[1].pixelbuf[i] = wrgb_u32(0, 64, 0, 0); //} gpio_put(PICO_DEFAULT_LED_PIN, blink); loop++; - if ( loop > 1000000 ) { + if ( loop > 3000000 ) { loop = 0; blink = 1 - blink; } |