Second uart and i2c test

This commit is contained in:
DrZlo13 2021-10-26 19:37:49 +10:00
parent 7c12863ef7
commit bfc8f3b43a
7 changed files with 121 additions and 37 deletions

View File

@ -4,6 +4,8 @@ set(SOURCES
"nvs.c"
"wifi.c"
"led.c"
"uart.c"
"i2c.c"
)
set(INCLUDES

66
main/i2c.c Normal file
View File

@ -0,0 +1,66 @@
#include <driver/i2c.h>
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");
}
}

2
main/i2c.h Normal file
View File

@ -0,0 +1,2 @@
void i2c_init(void);
void i2c_scan(void);

View File

@ -1,3 +1,4 @@
#pragma once
#include <stdint.h>
void led_init();

View File

@ -5,6 +5,8 @@
#include "wifi.h"
#include "gdb_main.h"
#include "led.h"
#include "uart.h"
#include "i2c.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <rom/ets_sys.h>
@ -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");

37
main/uart.c Normal file
View File

@ -0,0 +1,37 @@
#include <driver/uart.h>
#include <driver/gpio.h>
#include <string.h>
#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));
}

4
main/uart.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void uart_init();
void uart_print(const char* str);