From bfc8f3b43a0f97c3c5b1a9a19891d4cd49da69ae Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Tue, 26 Oct 2021 19:37:49 +1000 Subject: [PATCH] Second uart and i2c test --- main/CMakeLists.txt | 2 ++ main/i2c.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ main/i2c.h | 2 ++ main/led.h | 1 + main/main.c | 46 +++++++------------------------ main/uart.c | 37 +++++++++++++++++++++++++ main/uart.h | 4 +++ 7 files changed, 121 insertions(+), 37 deletions(-) create mode 100644 main/i2c.c create mode 100644 main/i2c.h create mode 100644 main/uart.c create mode 100644 main/uart.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 79c3b0e..e605ff1 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,6 +4,8 @@ set(SOURCES "nvs.c" "wifi.c" "led.c" + "uart.c" + "i2c.c" ) set(INCLUDES diff --git a/main/i2c.c b/main/i2c.c new file mode 100644 index 0000000..f069319 --- /dev/null +++ b/main/i2c.c @@ -0,0 +1,66 @@ +#include + +static gpio_num_t i2c_gpio_sda = 18; +static gpio_num_t i2c_gpio_scl = 17; +static gpio_num_t i2c_pull_enable = 3; +static i2c_port_t i2c_port = I2C_NUM_0; +static uint32_t i2c_frequency = 100000; + +// I2C master doesn't need buffers +#define I2C_MASTER_TX_BUF_DISABLE 0 +#define I2C_MASTER_RX_BUF_DISABLE 0 + +#define WRITE_BIT I2C_MASTER_WRITE +#define ACK_CHECK_EN 0x1 + +void i2c_init(void) { + i2c_driver_install( + i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + + i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = i2c_gpio_sda, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = i2c_gpio_scl, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = i2c_frequency, + }; + ESP_ERROR_CHECK(i2c_param_config(i2c_port, &conf)); + + gpio_config_t io_conf = {}; + io_conf.intr_type = GPIO_INTR_DISABLE; + io_conf.mode = GPIO_MODE_OUTPUT; + io_conf.pin_bit_mask = (1ULL << i2c_pull_enable); + io_conf.pull_down_en = 0; + io_conf.pull_up_en = 0; + ESP_ERROR_CHECK(gpio_config(&io_conf)); + + // enable pullups + gpio_set_level(i2c_pull_enable, 0); +} + +void i2c_scan(void) { + uint8_t address; + printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); + for(int i = 0; i < 128; i += 16) { + printf("%02x: ", i); + for(int j = 0; j < 16; j++) { + fflush(stdout); + address = i + j; + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN); + i2c_master_stop(cmd); + esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + if(ret == ESP_OK) { + printf("%02x ", address); + } else if(ret == ESP_ERR_TIMEOUT) { + printf("UU "); + } else { + printf("-- "); + } + } + printf("\r\n"); + } +} \ No newline at end of file diff --git a/main/i2c.h b/main/i2c.h new file mode 100644 index 0000000..bec1db6 --- /dev/null +++ b/main/i2c.h @@ -0,0 +1,2 @@ +void i2c_init(void); +void i2c_scan(void); \ No newline at end of file diff --git a/main/led.h b/main/led.h index a7b5511..1b6f15b 100644 --- a/main/led.h +++ b/main/led.h @@ -1,3 +1,4 @@ +#pragma once #include void led_init(); diff --git a/main/main.c b/main/main.c index 898148a..ac4a5d4 100644 --- a/main/main.c +++ b/main/main.c @@ -5,6 +5,8 @@ #include "wifi.h" #include "gdb_main.h" #include "led.h" +#include "uart.h" +#include "i2c.h" #include #include #include @@ -38,42 +40,6 @@ void pins_init() { gpio_config(&io_conf); } -void led_thread(void* pvParameters) { - const uint8_t led_colors[8][3] = { - {153, 0, 255}, - {255, 0, 0}, - {0, 255, 0}, - {0, 0, 255}, - {0, 255, 136}, - {255, 238, 0}, - {255, 157, 0}, - {255, 255, 255}, - }; - - uint8_t led_color_counter = 0; - - while(1) { - ESP_LOGI( - "led_thread", - "Led set to %u, %u, %u", - led_colors[led_color_counter][0], - led_colors[led_color_counter][1], - led_colors[led_color_counter][2]); - - led_set( - led_colors[led_color_counter][0], - led_colors[led_color_counter][1], - led_colors[led_color_counter][2]); - led_color_counter++; - - if(led_color_counter >= (sizeof(led_colors) / sizeof(led_colors[1]))) { - led_color_counter = 0; - } - - vTaskDelay(1000 / portTICK_PERIOD_MS); - } -} - void app_main(void) { ESP_LOGI(TAG, "start"); @@ -86,7 +52,13 @@ void app_main(void) { led_init(); led_set(0, 16, 0); - //xTaskCreate(&led_thread, "led_thread", 4096, NULL, 5, NULL); + // TODO uart and i2c share the same pins, need switching mechanics + // uart_init(); + // uart_print("Uart inited"); + + // i2c_init(); + // i2c_scan(); + xTaskCreate(&gdb_application_thread, "gdb_thread", 16 * 4096, NULL, 5, NULL); ESP_LOGI(TAG, "end"); diff --git a/main/uart.c b/main/uart.c new file mode 100644 index 0000000..269c679 --- /dev/null +++ b/main/uart.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#define UART_PORT_NUM UART_NUM_1 +#define UART_TXD_PIN (17) +#define UART_RXD_PIN (18) +#define UART_RTS_PIN (UART_PIN_NO_CHANGE) +#define UART_CTS_PIN (UART_PIN_NO_CHANGE) +#define UART_BAUD_RATE (115200) +#define UART_BUF_SIZE (1024) + +void uart_init() { + uart_config_t uart_config = { + .baud_rate = UART_BAUD_RATE, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_APB, + }; + int intr_alloc_flags = 0; + +#if CONFIG_UART_ISR_IN_IRAM + intr_alloc_flags = ESP_INTR_FLAG_IRAM; +#endif + + ESP_ERROR_CHECK( + uart_driver_install(UART_PORT_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags)); + ESP_ERROR_CHECK(uart_param_config(UART_PORT_NUM, &uart_config)); + ESP_ERROR_CHECK( + uart_set_pin(UART_PORT_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_RTS_PIN, UART_CTS_PIN)); +} + +void uart_print(const char* str) { + uart_write_bytes(UART_PORT_NUM, str, strlen(str)); +} diff --git a/main/uart.h b/main/uart.h new file mode 100644 index 0000000..703f5ff --- /dev/null +++ b/main/uart.h @@ -0,0 +1,4 @@ +#pragma once + +void uart_init(); +void uart_print(const char* str); \ No newline at end of file