improve driver, remove duplicated code by using toggle gpio driver capability.
authorNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 19 Nov 2020 18:42:11 +0000 (19:42 +0100)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Tue, 8 Nov 2022 16:03:05 +0000 (17:03 +0100)
extdrv/ws2812.c

index 615031f..809c764 100644 (file)
@@ -71,7 +71,7 @@ static void ws2812_bit_sender(struct ws2812_conf* ws2812)
 
                if (bit == 0) {
                        /* "high" time : 350ns */
-                       gpio_port->set = gpio_bit;
+                       gpio_port->toggle = gpio_bit;
                        nop();
                        nop();
                        nop();
@@ -79,7 +79,7 @@ static void ws2812_bit_sender(struct ws2812_conf* ws2812)
                        nop();
                        nop();
                        /* "low" time : 800ns */
-                       gpio_port->clear = gpio_bit;
+                       gpio_port->toggle = gpio_bit;
                        nop();
                        nop();
                        nop();
@@ -89,7 +89,7 @@ static void ws2812_bit_sender(struct ws2812_conf* ws2812)
                        nop();
                } else {
                        /* "high" time : 800ns */
-                       gpio_port->set = gpio_bit;
+                       gpio_port->toggle = gpio_bit;
                        nop();
                        nop();
                        nop();
@@ -109,7 +109,7 @@ static void ws2812_bit_sender(struct ws2812_conf* ws2812)
                        nop();
                        nop();
                        /* "low" time : 600ns */
-                       gpio_port->clear = gpio_bit;
+                       gpio_port->toggle = gpio_bit;
                        nop();
                }
 
@@ -126,79 +126,6 @@ static void ws2812_bit_sender(struct ws2812_conf* ws2812)
        lpc_enable_irq();
 }
 
-static void ws2812_inverted_bit_sender(struct ws2812_conf* ws2812)
-{
-       struct lpc_gpio* gpio_port = LPC_GPIO_REGS(ws2812->gpio.port);
-       uint32_t gpio_bit = (1 << ws2812->gpio.pin);
-       uint32_t byte_idx = 0;
-       uint8_t bit_idx = 7;
-       uint8_t bit = 0;
-       uint8_t data = (ws2812->led_data[0] * ws2812->brightness) >> 8;
-
-       lpc_disable_irq();
-
-       /* Send data */
-       while (byte_idx < ws2812->nb_bytes) {
-               bit = (data & (0x01 << bit_idx));
-
-               if (bit == 0) {
-                       /* "high" time : 350ns */
-                       gpio_port->clear = gpio_bit;
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       /* "low" time : 800ns */
-                       gpio_port->set = gpio_bit;
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-               } else {
-                       /* "high" time : 700ns */
-                       gpio_port->clear = gpio_bit;
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       nop();
-                       /* "low" time : 600ns */
-                       gpio_port->set = gpio_bit;
-                       nop();
-                       nop();
-               }
-
-               /* Move to the next bit */
-               if (bit_idx == 0) {
-                       bit_idx = 7;
-                       byte_idx++;
-                       data = (ws2812->led_data[byte_idx] * ws2812->brightness) >> 8;
-               } else {
-                       bit_idx--;
-               }
-       }
-
-       lpc_enable_irq();
-}
 
 /* Send led data from internal buffer (set leds to the selected color).
  * If nb_leds is 0 then all led data set using ws2812_set_pixel() since the last call
@@ -215,11 +142,7 @@ int ws2812_send_frame(struct ws2812_conf* ws2812, uint16_t nb_leds)
                nb_leds = ws2812->max_led + 1;
        }
        ws2812->nb_bytes = nb_leds * 3;
-       if (ws2812->inverted == 0) {
-               ws2812_bit_sender(ws2812);
-       } else {
-               ws2812_inverted_bit_sender(ws2812);
-       }
+       ws2812_bit_sender(ws2812);
        return 0;
 }