soft_uart_main.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include "sdkconfig.h"
  8. #include "esp_log.h"
  9. #include "esp_check.h"
  10. #include "soft_uart.h"
  11. #define READ_COUNT 16
  12. const char* EXAMPLE_TAG = "dedicated_gpio_example";
  13. void app_main(void)
  14. {
  15. esp_err_t ret = ESP_OK;
  16. uint8_t read_buffer[READ_COUNT] = { 0 };
  17. const uint8_t write_buffer[] = "Hello, world! This is a message.\r\n";
  18. soft_uart_port_t port = NULL;
  19. soft_uart_config_t config = {
  20. .tx_pin = CONFIG_EMULATE_UART_GPIO_TX,
  21. .rx_pin = CONFIG_EMULATE_UART_GPIO_RX,
  22. .baudrate = SOFT_UART_115200
  23. };
  24. /* Initialize and configure the software UART port */
  25. ret = soft_uart_new(&config, &port);
  26. ESP_GOTO_ON_ERROR(ret, error, EXAMPLE_TAG, "Error configuring software UART");
  27. /* Write few bytes to the UART */
  28. ret = soft_uart_send(port, write_buffer, sizeof(write_buffer));
  29. ESP_GOTO_ON_ERROR(ret, error, EXAMPLE_TAG, "Error writing to the software UART");
  30. /* Read from the UART */
  31. ret = soft_uart_receive(port, read_buffer, sizeof(read_buffer));
  32. ESP_GOTO_ON_ERROR(ret, error, EXAMPLE_TAG, "Error reading from the software UART");
  33. printf("UART transfers succeeded, received bytes: { ");
  34. for (int i = 0; i < sizeof(read_buffer); i++) {
  35. printf("0x%02x ", read_buffer[i]);
  36. }
  37. printf("}\n");
  38. error:
  39. if (port != NULL) {
  40. soft_uart_del(port);
  41. }
  42. if (ret != ESP_OK) {
  43. ESP_LOGE(EXAMPLE_TAG, "An error occurred while communicating through the UART");
  44. }
  45. }