ot_esp_cli.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. extern void otAppCliInit(otInstance *instance);
  32. static void ot_task_worker(void *aContext)
  33. {
  34. esp_openthread_platform_config_t config = {
  35. .radio_config =
  36. {
  37. .radio_mode = RADIO_MODE_UART_RCP,
  38. .radio_uart_config =
  39. {
  40. .port = 1,
  41. .uart_config =
  42. {
  43. .baud_rate = 115200,
  44. .data_bits = UART_DATA_8_BITS,
  45. .parity = UART_PARITY_DISABLE,
  46. .stop_bits = UART_STOP_BITS_1,
  47. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  48. .rx_flow_ctrl_thresh = 0,
  49. .source_clk = UART_SCLK_APB,
  50. },
  51. .rx_pin = 4,
  52. .tx_pin = 5,
  53. },
  54. },
  55. .host_config =
  56. {
  57. .host_connection_mode = HOST_CONNECTION_MODE_UART,
  58. .host_uart_config =
  59. {
  60. .port = 0,
  61. .uart_config =
  62. {
  63. .baud_rate = 115200,
  64. .data_bits = UART_DATA_8_BITS,
  65. .parity = UART_PARITY_DISABLE,
  66. .stop_bits = UART_STOP_BITS_1,
  67. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  68. .rx_flow_ctrl_thresh = 0,
  69. .source_clk = UART_SCLK_APB,
  70. },
  71. .rx_pin = UART_PIN_NO_CHANGE,
  72. .tx_pin = UART_PIN_NO_CHANGE,
  73. },
  74. },
  75. };
  76. esp_openthread_mainloop_context_t mainloop;
  77. ESP_ERROR_CHECK(esp_openthread_platform_init(&config));
  78. otInstance *instance = otInstanceInitSingle();
  79. assert(instance != NULL);
  80. esp_openthread_lock_acquire(portMAX_DELAY);
  81. otAppCliInit(instance);
  82. esp_openthread_lock_release();
  83. while (true) {
  84. FD_ZERO(&mainloop.read_fds);
  85. FD_ZERO(&mainloop.write_fds);
  86. FD_ZERO(&mainloop.error_fds);
  87. mainloop.max_fd = -1;
  88. mainloop.timeout.tv_sec = 10;
  89. mainloop.timeout.tv_usec = 0;
  90. esp_openthread_lock_acquire(portMAX_DELAY);
  91. esp_openthread_platform_update(&mainloop);
  92. if (otTaskletsArePending(instance)) {
  93. mainloop.timeout.tv_sec = 0;
  94. mainloop.timeout.tv_usec = 0;
  95. }
  96. esp_openthread_lock_release();
  97. if (select(mainloop.max_fd + 1, &mainloop.read_fds, &mainloop.write_fds, &mainloop.error_fds,
  98. &mainloop.timeout) >= 0) {
  99. esp_openthread_lock_acquire(portMAX_DELAY);
  100. otTaskletsProcess(instance);
  101. if (esp_openthread_platform_process(instance, &mainloop)) {
  102. ESP_LOGE(TAG, "esp_openthread_platform_process failed");
  103. }
  104. esp_openthread_lock_release();
  105. } else {
  106. ESP_LOGE(TAG, "OpenThread system polling failed");
  107. break;
  108. }
  109. }
  110. otInstanceFinalize(instance);
  111. esp_openthread_platform_deinit();
  112. vTaskDelete(NULL);
  113. }
  114. void app_main(void)
  115. {
  116. xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
  117. }