summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2023-10-01 08:44:30 +0200
committerThomas White <taw@physics.org>2023-10-01 08:44:30 +0200
commit683703a4720681107d17f44eae774d8c50797af7 (patch)
tree8fab916d51a0eeaad5b0e0a6bc0901f77168867d
parent632dedc23ed394e6a83cef2e5f0cdc3ae6aae443 (diff)
Backup before messing with DMA
-rw-r--r--pixelhub.cpp129
1 files 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<<dma_chan ) {
+ dma_hw->ints0 = 1<<dma_chan;
+ add_alarm_in_us(400, next_strip, NULL, true);
+ }
+}
+
+
static void init_pixel_strip(struct pixel_strip *p)
{
int i;
@@ -62,66 +104,79 @@ static void init_pixel_strip(struct pixel_strip *p)
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] = 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<p1.num_pixels; i++) {
- p1.pixelbuf[i] = urgb_u32(dmxbuf[dmxpos++],
- dmxbuf[dmxpos++],
- dmxbuf[dmxpos++]);
- }
- for ( i=0; i<p2.num_pixels; i++) {
- p2.pixelbuf[i] = wrgb_u32(dmxbuf[dmxpos++],
- dmxbuf[dmxpos++],
- dmxbuf[dmxpos++],
- dmxbuf[dmxpos++]);
- }
+ //dmx_in.read(dmxbuf);
- for ( i=0; i<npx; i++) {
- put_pixel(&p1, p1.pixelbuf[i]);
- put_pixel(&p2, p2.pixelbuf[i]);
- }
+ //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<strips[1].num_pixels; i++) {
+ // strips[1].pixelbuf[i] = wrgb_u32(0, 64, 0, 0);
+ //}
gpio_put(PICO_DEFAULT_LED_PIN, blink);
- blink = 1 - blink;
+ loop++;
+ if ( loop > 1000000 ) {
+ loop = 0;
+ blink = 1 - blink;
+ }
}
}