diff --git a/src/uart-transmit/Makefile b/src/uart-transmit/Makefile index 333c4d41aed88bd61ea6c6782508a2c9255846ef..1cabc439ec8717e71de36f741d00668386a3863b 100644 --- a/src/uart-transmit/Makefile +++ b/src/uart-transmit/Makefile @@ -1,3 +1,3 @@ -SOURCES = main.c +SOURCES = main.c esp32-c3-uart-interface.c esp32-c3-led-helpers.c include $(MDK)/$(ARCH)/build.mk diff --git a/src/uart-transmit/esp32-c3-led-helpers.c b/src/uart-transmit/esp32-c3-led-helpers.c new file mode 100644 index 0000000000000000000000000000000000000000..7195205c6b7476e572c07c80ed2779a9d7ea6e6d --- /dev/null +++ b/src/uart-transmit/esp32-c3-led-helpers.c @@ -0,0 +1,31 @@ +#include "esp32-c3-led-helpers.h" +#include <mdk.h> + +// used for debugging + +// On the ESP32C3 dev boards, the WS2812 LED is connected to GPIO 8 +static int ws_2812_pin = 8; + +void setupLED(){ + wdt_disable(); + gpio_output(ws_2812_pin); +} + +void blinkGreen(int count, unsigned long delay_millis) { + uint8_t green[3] = {1, 0, 0}, black[3] = {0, 0, 0}; + for (int i = 0; i < count; i++) { + ws2812_show(ws_2812_pin, green, sizeof(green)); + delay_ms(delay_millis); + ws2812_show(ws_2812_pin, black, sizeof(black)); + delay_ms(delay_millis); + } +} + +void showColorForDuration(uint8_t grb[3], uint32_t durationInMs){ + ws2812_show(ws_2812_pin, grb, sizeof(uint8_t)*3); + delay_ms(durationInMs); +} + +void showColor(uint8_t grb[3]){ + ws2812_show(ws_2812_pin, grb, sizeof(uint8_t)*3); +} \ No newline at end of file diff --git a/src/uart-transmit/esp32-c3-led-helpers.h b/src/uart-transmit/esp32-c3-led-helpers.h new file mode 100644 index 0000000000000000000000000000000000000000..d51f91a48d8a7d77cb84e852a4b6045c7b2bf9d5 --- /dev/null +++ b/src/uart-transmit/esp32-c3-led-helpers.h @@ -0,0 +1,9 @@ +#ifndef ESP32_C3_LED_HELPERS_H +#define ESP32_C3_LED_HELPERS_H + +void showColorForDuration(); +void showColor(); +void blinkGreen(); +void setupLED(); + +#endif \ No newline at end of file diff --git a/src/uart-transmit/esp32-c3-uart-interface.c b/src/uart-transmit/esp32-c3-uart-interface.c index d9ffad59c311c410f79c131c132b0dee7cb50540..ba50bcf2ca86ee1d65151a70c3a3d35b2f056eef 100644 --- a/src/uart-transmit/esp32-c3-uart-interface.c +++ b/src/uart-transmit/esp32-c3-uart-interface.c @@ -1,9 +1,12 @@ #include "esp32-c3-uart-interface.h" +#include "esp32-c3-led-helpers.h" +#include <stdint.h> +#include <stdbool.h> static void clearBits(volatile uint32_t *registerAddress, uint32_t bitPositions, uint32_t numBits) { - uint32_t mask = (uint32_t)(1 << numBits) - 1; - mask <<= bitPositions; - *registerAddress &= ~mask; + uint32_t mask = (uint32_t)(1 << numBits) - 1; // Create a mask with the specified number of bits set to 1 + mask <<= bitPositions; // Shift the mask to the correct bit positions + *registerAddress &= ~mask; // Clear the bits in the register using bitwise AND with the inverse of the mask } static void setBit(volatile uint32_t *registerAddress, uint32_t bitPositions) { @@ -110,14 +113,12 @@ static void set_max_data_lenght(){ } void config_uart(){ - printf("Try to config %d\n", 1); // wait for UART_REG_UART0_UPDATE to become 0, which indicates the completion of the last synchronization; uint32_t reg_update = *C3_UART_ID_REG_UART0; - if ((bool)(reg_update & (uint32_t)(1<<31))){ - return; - } - printf("Ready to config %d\n", 1); + while((bool)(reg_update & (uint32_t)(1<<31))){ + __asm__("ADDI x0, x0, 0"); + } // configure static registers (if any) following Section 26.5.1.2; @@ -156,10 +157,10 @@ void enable_uart_transmitter(){ set_65_to_rd_byte(); // clear UART_TXFIFO_EMPTY_INT interrupt by setting UART_TXFIFO_EMPTY_INT_CLR; - setBit(C3_UART_INT_CLR_REG_UART0, 1) + setBit(C3_UART_INT_CLR_REG_UART0, 1); // enable UART_TXFIFO_EMPTY_INT interrupt by setting UART_TXFIFO_EMPTY_INT_ENA; - setBit(C3_UART_INT_ENA_REG_UART0, 1) + setBit(C3_UART_INT_ENA_REG_UART0, 1); // detect UART_TXFIFO_EMPTY_INT and wait for the completion of data transmission. } @@ -171,8 +172,16 @@ void reset_uart(){ } void init_uart(){ + uint8_t red[3] = {0,1,0}; + uint8_t blue[3] = {0,0,1}; + uint8_t yellow[3] = {1,1,0}; + uint8_t violet[3] = {0,1,1}; // from technical reference manual 543 f + showColorForDuration(red,1000); init_uart_enable_clk(); + showColorForDuration(blue,1000); init_uart_toggle_rst(); + showColorForDuration(violet,1000); init_uart_clear_update(); + showColorForDuration(yellow,1000); } \ No newline at end of file diff --git a/src/uart-transmit/esp32-c3-uart-interface.h b/src/uart-transmit/esp32-c3-uart-interface.h index 29b02d2cc1f3de6e844a828179ffb7bebd0b8ba1..7dfe002d3fb0292099c0833d2109bd55b9ae8466 100644 --- a/src/uart-transmit/esp32-c3-uart-interface.h +++ b/src/uart-transmit/esp32-c3-uart-interface.h @@ -19,7 +19,7 @@ #define C3_UART_INT_CLR_REG_UART0 ((volatile uint32_t *)C3_BASE_OFFSET_UART_CONTROLLER_0+0x0010) #define C3_UART_INT_CLR_REG_UART1 ((volatile uint32_t *)C3_BASE_OFFSET_UART_CONTROLLER_1+0x0010) -// UART_TXFIFO_EMPTY_INT_CLR is on 1 +// UART_TXFIFO_EMPTY_INT_CLR #define C3_SYSTEM_PERIP_CLK_EN0_REG ((volatile uint32_t *)C3_BASE_OFFSET_SYSTEM_REGISTERS+0x0010) // SYSTEM_UART_MEM_CLK_EN is on 24 diff --git a/src/uart-transmit/main.c b/src/uart-transmit/main.c index c249ba76bbd1d9dd018e601a347e545a9bfe2252..937885a419b2931e55a3c56c108d490b00fe9f5f 100644 --- a/src/uart-transmit/main.c +++ b/src/uart-transmit/main.c @@ -2,57 +2,21 @@ // All rights reserved #include <mdk.h> -#include "esp32-c3-uart-interface.c" - -// On the ESP32C3 dev boards, the WS2812 LED is connected to GPIO 8 -static int ws_2812_pin = 8; - -// Simple hue function for generation of smooth rainbow. -static uint8_t hueval(int value) { - value = value % 1536; - if (value < 256) { - return (uint8_t) value; - } else if (value < 768) { - return 255; - } else if (value < 1024) { - return (uint8_t) (1023 - value); - } else { - return 0; - } -} - -static void blink(int count, unsigned long delay_millis) { - uint8_t green[3] = {1, 0, 0}, black[3] = {0, 0, 0}; - for (int i = 0; i < count; i++) { - ws2812_show(ws_2812_pin, green, sizeof(green)); - delay_ms(delay_millis); - ws2812_show(ws_2812_pin, black, sizeof(black)); - delay_ms(delay_millis); - } -} - -static void rainbow(int count) { - for (int i = 0; i < count; i++) { - uint8_t val = hueval(i); - val = (uint8_t) (val/100); - uint8_t rgb[3] = {0, val, val}; - ws2812_show(ws_2812_pin, rgb, sizeof(rgb)); - delay_ms(1); - } -} +#include "esp32-c3-uart-interface.h" +#include "esp32-c3-led-helpers.h" int main(void) { - wdt_disable(); - gpio_output(ws_2812_pin); + uint8_t green[3] = {1,0,0}; + uint8_t black[3] = {0,0,0}; + setupLED(); + showColor(black); + // for some reason the first time we show a color, it's always a bright green... + showColorForDuration(green,1000); + init_uart(); config_uart(); - uint8_t cycle = 0; - for (;;) { - printf("Cycle %d\n", cycle++); - blink(2, 200); - rainbow(2500); - } + showColor(black); return 0; }