Skip to content
Snippets Groups Projects
Commit 6353aedf authored by Jakob's avatar Jakob
Browse files

fix: uart init and config, removing printf as it will make the board to not...

fix: uart init and config, removing printf as it will make the board to not work as expected, refactored led scripts
parent deb91e5e
No related branches found
No related tags found
1 merge request!1copied and adapted example from mdk, added init and config uart
SOURCES = main.c
SOURCES = main.c esp32-c3-uart-interface.c esp32-c3-led-helpers.c
include $(MDK)/$(ARCH)/build.mk
#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
#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
#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
......@@ -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
......
......@@ -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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment