ot_esp_cli.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "esp_netif.h"
  18. #include "esp_openthread.h"
  19. #include "esp_openthread_lock.h"
  20. #include "esp_openthread_types.h"
  21. #include "sdkconfig.h"
  22. #include "driver/uart.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/portmacro.h"
  25. #include "freertos/task.h"
  26. #include "hal/uart_types.h"
  27. #include "openthread/cli.h"
  28. #include "openthread/instance.h"
  29. #include "openthread/tasklet.h"
  30. #define TAG "ot_esp_cli"
  31. static void ot_task_worker(void *aContext)
  32. {
  33. esp_openthread_platform_config_t config = {
  34. .radio_config =
  35. {
  36. .radio_mode = RADIO_MODE_UART_RCP,
  37. .radio_uart_config =
  38. {
  39. .port = 1,
  40. .uart_config =
  41. {
  42. .baud_rate = 115200,
  43. .data_bits = UART_DATA_8_BITS,
  44. .parity = UART_PARITY_DISABLE,
  45. .stop_bits = UART_STOP_BITS_1,
  46. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  47. .rx_flow_ctrl_thresh = 0,
  48. .source_clk = UART_SCLK_APB,
  49. },
  50. .rx_pin = 4,
  51. .tx_pin = 5,
  52. },
  53. },
  54. .host_config =
  55. {
  56. .host_connection_mode = HOST_CONNECTION_MODE_UART,
  57. .host_uart_config =
  58. {
  59. .port = 0,
  60. .uart_config =
  61. {
  62. .baud_rate = 115200,
  63. .data_bits = UART_DATA_8_BITS,
  64. .parity = UART_PARITY_DISABLE,
  65. .stop_bits = UART_STOP_BITS_1,
  66. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  67. .rx_flow_ctrl_thresh = 0,
  68. .source_clk = UART_SCLK_APB,
  69. },
  70. .rx_pin = UART_PIN_NO_CHANGE,
  71. .tx_pin = UART_PIN_NO_CHANGE,
  72. },
  73. },
  74. };
  75. esp_openthread_mainloop_context_t mainloop;
  76. ESP_ERROR_CHECK(esp_openthread_platform_init(&config));
  77. otInstance *instance = otInstanceInitSingle();
  78. assert(instance != NULL);
  79. esp_openthread_lock_acquire(portMAX_DELAY);
  80. otCliUartInit(instance);
  81. esp_openthread_lock_release();
  82. while (true) {
  83. FD_ZERO(&mainloop.read_fds);
  84. FD_ZERO(&mainloop.write_fds);
  85. FD_ZERO(&mainloop.error_fds);
  86. mainloop.max_fd = -1;
  87. mainloop.timeout.tv_sec = 10;
  88. mainloop.timeout.tv_usec = 0;
  89. esp_openthread_lock_acquire(portMAX_DELAY);
  90. esp_openthread_platform_update(&mainloop);
  91. if (otTaskletsArePending(instance)) {
  92. mainloop.timeout.tv_sec = 0;
  93. mainloop.timeout.tv_usec = 0;
  94. }
  95. esp_openthread_lock_release();
  96. if (select(mainloop.max_fd + 1, &mainloop.read_fds, &mainloop.write_fds, &mainloop.error_fds,
  97. &mainloop.timeout) >= 0) {
  98. esp_openthread_lock_acquire(portMAX_DELAY);
  99. otTaskletsProcess(instance);
  100. if (esp_openthread_platform_process(instance, &mainloop)) {
  101. ESP_LOGE(TAG, "esp_openthread_platform_process failed");
  102. }
  103. esp_openthread_lock_release();
  104. } else {
  105. ESP_LOGE(TAG, "OpenThread system polling failed");
  106. break;
  107. }
  108. }
  109. otInstanceFinalize(instance);
  110. esp_openthread_platform_deinit();
  111. vTaskDelete(NULL);
  112. }
  113. void app_main(void)
  114. {
  115. xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
  116. }