Advertisement
Guest User

esp-idf uart to spp

a guest
Aug 5th, 2021
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/uart.h"
  5. #include "driver/gpio.h"
  6.  
  7. #include "nvs_flash.h"
  8. #include "esp_log.h"
  9. #include "esp_bt.h"
  10. #include "esp_bt_defs.h"
  11. #include "esp_bt_main.h"
  12. #include "esp_gap_bt_api.h"
  13. #include "esp_bt_device.h"
  14. #include "esp_spp_api.h"
  15.  
  16. #define ECHO_TEST_TXD (GPIO_NUM_4)
  17. #define ECHO_TEST_RXD (GPIO_NUM_5)
  18. #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
  19. #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
  20.  
  21. #define BUF_SIZE (1024)
  22. #define JOB_TAG "MAIN"
  23.  
  24. typedef struct t_my_data
  25. {
  26.     esp_spp_cb_param_t spp_cb_param;
  27.     bool spp_conn;
  28. } t_my_data;
  29. t_my_data gs_my_data;
  30.  
  31. static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  32. {
  33.     ESP_LOGI(JOB_TAG, "esp_bt_gap_cb - event: %d", event);
  34.     switch (event)
  35.     {
  36.     case ESP_BT_GAP_AUTH_CMPL_EVT:
  37.         if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS)
  38.         {
  39.             ESP_LOGI(JOB_TAG, "authentication success: %s", param->auth_cmpl.device_name);
  40.         }
  41.         else
  42.         {
  43.             ESP_LOGE(JOB_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
  44.         }
  45.         break;
  46.     case ESP_BT_GAP_PIN_REQ_EVT:
  47.         ESP_LOGI(JOB_TAG, "ESP_BT_GAP_PIN_REQ_EVT");
  48.         break;
  49.     case ESP_BT_GAP_CFM_REQ_EVT:
  50.         ESP_LOGI(JOB_TAG, "ESP_BT_GAP_CFM_REQ_EVT Compare the numeric value: %d", param->cfm_req.num_val);
  51.         esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  52.         break;
  53.     case ESP_BT_GAP_KEY_NOTIF_EVT:
  54.         ESP_LOGI(JOB_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  55.         break;
  56.     case ESP_BT_GAP_KEY_REQ_EVT:
  57.         ESP_LOGI(JOB_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  58.         break;
  59.     default:
  60.         break;
  61.     }
  62. }
  63.  
  64. static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
  65. {
  66.     ESP_LOGI(JOB_TAG, "esp_spp_cb - event:%d", event);
  67.     switch (event)
  68.     {
  69.     case ESP_SPP_INIT_EVT:
  70.  
  71.         esp_bt_dev_set_device_name("Point");
  72.         esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  73.         esp_spp_start_srv(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_SLAVE, 0, "SPP server");
  74.         break;
  75.     case ESP_SPP_OPEN_EVT:
  76.         break;
  77.     case ESP_SPP_CLOSE_EVT:
  78.         gs_my_data.spp_conn = false;
  79.         break;
  80.     case ESP_SPP_START_EVT:
  81.         break;
  82.     case ESP_SPP_CL_INIT_EVT:
  83.         break;
  84.     case ESP_SPP_DATA_IND_EVT:
  85.         break;
  86.     case ESP_SPP_CONG_EVT:
  87.         break;
  88.     case ESP_SPP_WRITE_EVT:
  89.         break;
  90.     case ESP_SPP_SRV_OPEN_EVT:
  91.         ESP_LOGI(JOB_TAG, "ESP_SPP_SRV_OPEN_EVT");
  92.         gs_my_data.spp_conn = true;
  93.         gs_my_data.spp_cb_param = *param;
  94.         break;
  95.     default:
  96.         break;
  97.     }
  98. }
  99.  
  100. void app_main(void)
  101. {
  102.     esp_err_t ret = nvs_flash_init();
  103.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  104.     {
  105.         ESP_ERROR_CHECK(nvs_flash_erase());
  106.         ret = nvs_flash_init();
  107.     }
  108.     ESP_ERROR_CHECK(ret);
  109.  
  110.     ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
  111.  
  112.     uart_config_t uart_config = {
  113.         .baud_rate = 115200,
  114.         .data_bits = UART_DATA_8_BITS,
  115.         .parity = UART_PARITY_DISABLE,
  116.         .stop_bits = UART_STOP_BITS_1,
  117.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  118.         .source_clk = UART_SCLK_APB,
  119.     };
  120.     uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0);
  121.     uart_param_config(UART_NUM_0, &uart_config);
  122.     uart_set_pin(UART_NUM_0, GPIO_NUM_1, GPIO_NUM_3, ECHO_TEST_RTS, ECHO_TEST_CTS);
  123.  
  124.     // Configure a temporary buffer for the incoming data
  125.     uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
  126.  
  127.     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  128.     if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK)
  129.     {
  130.         ESP_LOGE(JOB_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
  131.         return;
  132.     }
  133.  
  134.     if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK)
  135.     {
  136.         ESP_LOGE(JOB_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  137.         return;
  138.     }
  139.  
  140.     if ((ret = esp_bluedroid_init()) != ESP_OK)
  141.     {
  142.         ESP_LOGE(JOB_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  143.         return;
  144.     }
  145.  
  146.     if ((ret = esp_bluedroid_enable()) != ESP_OK)
  147.     {
  148.         ESP_LOGE(JOB_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  149.         return;
  150.     }
  151.  
  152.     if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK)
  153.     {
  154.         ESP_LOGE(JOB_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
  155.         return;
  156.     }
  157.  
  158.     if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK)
  159.     {
  160.         ESP_LOGE(JOB_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret));
  161.         return;
  162.     }
  163.  
  164.     if ((ret = esp_spp_init(ESP_SPP_MODE_CB)) != ESP_OK)
  165.     {
  166.         ESP_LOGE(JOB_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
  167.         return;
  168.     }
  169.  
  170.     /* Set default parameters for Secure Simple Pairing */
  171.     esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  172.     esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  173.     esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  174.  
  175.     /*
  176.      * Set default parameters for Legacy Pairing
  177.      * Use variable pin, input pin code when pairing
  178.      */
  179.     esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
  180.     esp_bt_pin_code_t pin_code;
  181.     esp_bt_gap_set_pin(pin_type, 0, pin_code);
  182.  
  183.     while (1)
  184.     {
  185.         int len = uart_read_bytes(UART_NUM_0, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  186.         if (len > 0 && gs_my_data.spp_conn)
  187.         {
  188.             ESP_LOGI(JOB_TAG, "UART data recieved:%s LENGTH:%d", (const char *)data, len);
  189.  
  190.             esp_spp_write(gs_my_data.spp_cb_param.write.handle, len, data);
  191.         }
  192.     }
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement