Added Basic Uart Setup
This commit is contained in:
parent
871bc4876e
commit
10d6898e43
39
main/main.c
39
main/main.c
@ -1,11 +1,14 @@
|
||||
#include "assert.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/uart.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_phy_init.h"
|
||||
#include "esp_rom_gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "hal/uart_types.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "portmacro.h"
|
||||
#include <esp_event.h>
|
||||
#include <esp_now.h>
|
||||
#include <esp_wifi.h>
|
||||
@ -20,6 +23,11 @@
|
||||
|
||||
#define MASTER_MODE_PIN GPIO_NUM_23 // Jumper-Erkennungspin
|
||||
#define BROADCAST_INTERVAL_MS 500
|
||||
|
||||
#define BUF_SIZE (1024)
|
||||
#define TXD_PIN (GPIO_NUM_17)
|
||||
#define RXD_PIN (GPIO_NUM_16)
|
||||
|
||||
uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF};
|
||||
#define IS_BROADCAST_ADDR(addr) \
|
||||
@ -126,7 +134,6 @@ void master_broadcast_task(void *param) {
|
||||
|
||||
void master_ping_task(void *param) {
|
||||
while (1) {
|
||||
ESP_LOGI(TAG, "SENDING PING TO CLIENTS");
|
||||
for (size_t i = 0; i < numClients; ++i) {
|
||||
if (clients[i].isAvailable) {
|
||||
PingPayload payload = {};
|
||||
@ -241,6 +248,35 @@ void client_data_sending_task(void *param) {
|
||||
}
|
||||
}
|
||||
|
||||
void uart_read_task(void *param) {
|
||||
uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
|
||||
int len = 0;
|
||||
while (1) {
|
||||
len = 0;
|
||||
len =
|
||||
uart_read_bytes(UART_NUM_2, data, BUF_SIZE, (20 / portTICK_PERIOD_MS));
|
||||
if (len > 0) {
|
||||
data[len] = '\0';
|
||||
ESP_LOGI(TAG, "GOT UART DATA %s", data);
|
||||
uart_write_bytes(UART_NUM_2, data, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_uart() {
|
||||
uart_config_t uart_config = {.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
|
||||
|
||||
uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
uart_param_config(UART_NUM_2, &uart_config);
|
||||
uart_set_pin(UART_NUM_2, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
||||
UART_PIN_NO_CHANGE);
|
||||
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||
@ -287,6 +323,7 @@ void app_main(void) {
|
||||
add_peer(broadcast_address);
|
||||
xTaskCreate(master_broadcast_task, "MasterBroadcast", 4096, NULL, 1, NULL);
|
||||
xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
||||
init_uart();
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Started in Slavemode");
|
||||
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user