1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include <stdio.h>
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/queue.h"
- #include "driver/uart.h"
- #include "esp_log.h"
- #include "config.h"
- #include "uart.h"
- #ifdef SERIAL_ENABLED
- /************************************************************************************************
- * On the ESP-WROOM-32 Card, the UART2-Pins are avaialable.
- * TXD: GPIO17
- * RXD: GPIO16
- * https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf
- * http://esp32.net/images/MH-ET-LIVE/ESP32-DevKit/MH-ET-LIVE_ESP32-DevKit_DiagramPinout.png
- * https://i.imgur.com/ctokhuk.jpg (Schematic of the MH-ET-LIVE)
- *
- ************************************************************************************************/
- #define UART_NUMBER UART_NUM_2
- void initUart() {
-
- uart_config_t uart_config = {
- .baud_rate = 19200,
- .data_bits = UART_DATA_8_BITS,
- .parity = UART_PARITY_DISABLE,
- .stop_bits = UART_STOP_BITS_1,
- .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
- };
- ESP_ERROR_CHECK(uart_param_config(UART_NUMBER, &uart_config));
- ESP_ERROR_CHECK(uart_set_pin(UART_NUMBER, UART_TX_PIN, UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
- const int uart_buffer_size = (1024 * 2);
- QueueHandle_t uart_queue;
- ESP_ERROR_CHECK(uart_driver_install(UART_NUMBER, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
- }
- unsigned char serialRead(void) {
- unsigned char data;
- const int length = uart_read_bytes(UART_NUMBER, (uint8_t*)&data, 1, 0);
- if( length > 0 )
- return data;
- else
- return '\0';
- }
- int serialAvailable() {
- size_t size;
- uart_get_buffered_data_len(UART_NUMBER, &size);
- return (size == 0)? 0 : 1;
- }
- void writeUARTData(const char *str) {
- uart_tx_chars(UART_NUMBER, (const char*)str, strlen(str));
- }
- void readData() {
- uint8_t data[2048];
- int length = 0;
- printf("readData()a\n");
- ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUMBER, (size_t*)&length));
- printf("readData()b %d\n",length);
- length = uart_read_bytes(UART_NUMBER, data, length, 100);
- printf("readData()c %d\n",length);
- data[length] = '\0';
- printf("Read:%d :%s\n",length,(char *)data);
- }
- #endif
|