Skip to content

Commit

Permalink
uart added
Browse files Browse the repository at this point in the history
  • Loading branch information
ta2tf committed May 30, 2023
1 parent fbdb6d0 commit c1e71ee
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 6 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(gatt_server_service_table_demo)


target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/aws/client.crt" TEXT)
target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/aws/client.key" TEXT)
target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/aws/awsrootca.crt" TEXT)
target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/https_ota/google.crt" TEXT)
# target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/aws/client.crt" TEXT)
# target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/aws/client.key" TEXT)
# target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/aws/awsrootca.crt" TEXT)
# target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/https_ota/google.crt" TEXT)


1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(COMPONENT_SRCS "main.c"
"board/led.c"
"board/IP5306.c"
"board/battery.c"
"board/uart.c"
"aws/aws.c"
"https_ota/httpsota.c"
"wifi/wifi_connect.c"
Expand Down
89 changes: 89 additions & 0 deletions main/board/uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* UART Echo Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "esp_log.h"

/**
* This is an example which echos any data it receives on configured UART back to the sender,
* with hardware flow control turned off. It does not use UART driver event queue.
*
* - Port: configured UART
* - Receive (Rx) buffer: on
* - Transmit (Tx) buffer: off
* - Flow control: off
* - Event queue: off
* - Pin assignment: see defines below (See Kconfig)
*/

#define ECHO_TEST_TXD 1
#define ECHO_TEST_RXD 3
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)

#define ECHO_UART_PORT_NUM 1
#define ECHO_UART_BAUD_RATE 115200
#define ECHO_TASK_STACK_SIZE 2048

static const char *TAG = "UART TEST";

#define BUF_SIZE (1024)

static void echo_task(void *arg)
{
/* Configure parameters of an UART driver,
* communication pins and install the driver */
uart_config_t uart_config = {
.baud_rate = ECHO_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_DEFAULT,
};
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(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));

// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);



while (1) {
// Read data from the UART
int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
// Write data back to the UART
uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);

if (len) {
data[len] = '\0';
ESP_LOGI(TAG, "Recv str: %s", (char *) data);

}
}
}

void uart_echo_test(void)
{
esp_log_level_set(TAG, ESP_LOG_INFO);
ESP_LOGI(TAG, "ECHO UART start");

xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
}
15 changes: 15 additions & 0 deletions main/board/uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* uart.h
*
* Created on: 30 May 2023
* Author: MertechArge014
*/

#ifndef MAIN_BOARD_UART_H_
#define MAIN_BOARD_UART_H_


void uart_echo_test(void);


#endif /* MAIN_BOARD_UART_H_ */
11 changes: 9 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ota_1,app,ota_1,0xc00000,4M,
#include "aws.h"
#include "httpsota.h"
#include "wifi_connect.h"
#include "uart.h"

#include "nvs.h"
#include "nvs_flash.h"
Expand All @@ -70,6 +71,7 @@ static const char *TAG = "MAIN";




//==========================================================================================================
//==========================================================================================================
//
Expand Down Expand Up @@ -98,8 +100,12 @@ void app_main(void)
char issid[32];
char ipass[32];

sprintf(issid,"%s","MERLIN");
sprintf(ipass,"%s","narnia1523");
// sprintf(issid,"%s","MERLIN");
// sprintf(ipass,"%s","narnia1523");

sprintf(issid,"%s","Mertech_2_4");
sprintf(ipass,"%s","MeR0TecH_2");


nvs_flash_init();
nvs_handle_t nvs;
Expand All @@ -108,6 +114,7 @@ void app_main(void)
nvs_set_str(nvs, "pass", ipass);
nvs_close(nvs);

// uart_echo_test();

LED_Init();

Expand Down

0 comments on commit c1e71ee

Please sign in to comment.