77#include "esp_log.h"
88#include "driver/sdmmc_host.h"
99#include "vfs_fat_internal.h"
10- #include "esp_wifi.h"
1110#include "esp_event.h"
1211#include "nvs_flash.h"
1312#include "esp_gmf_oal_mem.h"
2524#include "esp_codec_dev_defaults.h"
2625#endif /* USE_ESP_GMF_ESP_CODEC_DEV_IO */
2726
28- #define WIFI_CONNECTED_BIT BIT0
29- #define WIFI_FAIL_BIT BIT1
30- #define WIFI_RECONNECT_RETRIES 30
27+ typedef enum {
28+ I2S_CREATE_MODE_TX_ONLY = 0 ,
29+ I2S_CREATE_MODE_RX_ONLY = 1 ,
30+ I2S_CREATE_MODE_TX_AND_RX = 2 ,
31+ } i2s_create_mode_t ;
3132
3233#ifdef USE_ESP_GMF_ESP_CODEC_DEV_IO
3334i2s_chan_handle_t rx_handle = NULL ;
@@ -43,22 +44,8 @@ const audio_codec_if_t *out_codec_if = NULL;
4344const audio_codec_gpio_if_t * gpio_if = NULL ;
4445#endif /* USE_ESP_GMF_ESP_CODEC_DEV_IO */
4546
46- #ifndef CONFIG_EXAMPLE_ESP_GMF_WIFI
47- #define CONFIG_EXAMPLE_ESP_GMF_WIFI_SSID "****"
48- #define CONFIG_EXAMPLE_ESP_GMF_WIFI_PASS "****"
49- #endif /* CONFIG_EXAMPLE_ESP_GMF_WIFI */
50-
51- typedef enum {
52- I2S_CREATE_MODE_TX_ONLY = 0 ,
53- I2S_CREATE_MODE_RX_ONLY = 1 ,
54- I2S_CREATE_MODE_TX_AND_RX = 2 ,
55- } i2s_create_mode_t ;
56-
57- static const char * TAG = "SETUP_PERIPH" ;
58- static esp_netif_t * s_esp_gmf_setup_sta_netif ;
59- static EventGroupHandle_t s_wifi_event_group ;
60- static int s_retry_num = 0 ;
61- i2c_master_bus_handle_t i2c_handle = NULL ;
47+ static const char * TAG = "SETUP_PERIPH" ;
48+ i2c_master_bus_handle_t i2c_handle = NULL ;
6249
6350#ifdef USE_ESP_GMF_ESP_CODEC_DEV_IO
6451static esp_err_t setup_periph_i2s_tx_init (esp_gmf_setup_periph_aud_info * aud_info )
@@ -342,92 +329,6 @@ void esp_gmf_teardown_periph_sdmmc(void *card)
342329 esp_vfs_fat_sdcard_unmount ("/sdcard" , card );
343330}
344331
345- static void event_handler (void * arg , esp_event_base_t event_base ,
346- int32_t event_id , void * event_data )
347- {
348- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START ) {
349- esp_wifi_connect ();
350- } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED ) {
351- if (s_retry_num < WIFI_RECONNECT_RETRIES ) {
352- esp_wifi_connect ();
353- s_retry_num ++ ;
354- ESP_LOGI (TAG , "Retry to connect to the AP" );
355- } else {
356- xEventGroupSetBits (s_wifi_event_group , WIFI_FAIL_BIT );
357- }
358- ESP_LOGI (TAG , "connect to the AP fail" );
359- } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP ) {
360- ip_event_got_ip_t * event = (ip_event_got_ip_t * )event_data ;
361- ESP_LOGI (TAG , "got ip:" IPSTR , IP2STR (& event -> ip_info .ip ));
362- s_retry_num = 0 ;
363- xEventGroupSetBits (s_wifi_event_group , WIFI_CONNECTED_BIT );
364- }
365- }
366-
367- void esp_gmf_setup_periph_wifi (void )
368- {
369- esp_err_t ret = nvs_flash_init ();
370- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND ) {
371- ESP_ERROR_CHECK (nvs_flash_erase ());
372- ret = nvs_flash_init ();
373- }
374- ESP_ERROR_CHECK (ret );
375- s_wifi_event_group = xEventGroupCreate ();
376- ESP_ERROR_CHECK (esp_netif_init ());
377- ESP_ERROR_CHECK (esp_event_loop_create_default ());
378- s_esp_gmf_setup_sta_netif = esp_netif_create_default_wifi_sta ();
379- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
380- ESP_ERROR_CHECK (esp_wifi_init (& cfg ));
381- esp_event_handler_instance_t instance_any_id ;
382- esp_event_handler_instance_t instance_got_ip ;
383- ESP_ERROR_CHECK (esp_event_handler_instance_register (WIFI_EVENT ,
384- ESP_EVENT_ANY_ID ,
385- & event_handler ,
386- NULL ,
387- & instance_any_id ));
388- ESP_ERROR_CHECK (esp_event_handler_instance_register (IP_EVENT ,
389- IP_EVENT_STA_GOT_IP ,
390- & event_handler ,
391- NULL ,
392- & instance_got_ip ));
393- wifi_config_t wifi_config = {
394- .sta = {
395- .ssid = CONFIG_EXAMPLE_ESP_GMF_WIFI_SSID ,
396- .password = CONFIG_EXAMPLE_ESP_GMF_WIFI_PASS ,
397- },
398- };
399- ESP_ERROR_CHECK (esp_wifi_set_mode (WIFI_MODE_STA ));
400- ESP_ERROR_CHECK (esp_wifi_set_config (WIFI_IF_STA , & wifi_config ));
401- ESP_ERROR_CHECK (esp_wifi_start ());
402- ESP_LOGI (TAG , "wifi_init_sta finished." );
403- EventBits_t bits = xEventGroupWaitBits (s_wifi_event_group ,
404- WIFI_CONNECTED_BIT | WIFI_FAIL_BIT , pdFALSE , pdFALSE , portMAX_DELAY );
405- if (bits & WIFI_CONNECTED_BIT ) {
406- ESP_LOGI (TAG , "connected to ap SSID:%s password:%s" ,
407- CONFIG_EXAMPLE_ESP_GMF_WIFI_SSID , CONFIG_EXAMPLE_ESP_GMF_WIFI_PASS );
408- } else if (bits & WIFI_FAIL_BIT ) {
409- ESP_LOGI (TAG , "Failed to connect to SSID:%s, password:%s" ,
410- CONFIG_EXAMPLE_ESP_GMF_WIFI_SSID , CONFIG_EXAMPLE_ESP_GMF_WIFI_PASS );
411- } else {
412- ESP_LOGE (TAG , "UNEXPECTED EVENT" );
413- }
414- }
415-
416- void esp_gmf_teardown_periph_wifi (void )
417- {
418- esp_wifi_disconnect ();
419- esp_err_t err = esp_wifi_stop ();
420- if (err == ESP_ERR_WIFI_NOT_INIT ) {
421- ESP_LOGE (TAG , "WIFI stop got an error" );
422- return ;
423- }
424- ESP_ERROR_CHECK (esp_wifi_deinit ());
425- esp_event_loop_delete_default ();
426- ESP_ERROR_CHECK (esp_wifi_clear_default_wifi_driver_and_handlers (s_esp_gmf_setup_sta_netif ));
427- esp_netif_destroy (s_esp_gmf_setup_sta_netif );
428- s_esp_gmf_setup_sta_netif = NULL ;
429- }
430-
431332void esp_gmf_setup_periph_i2c (int port )
432333{
433334 i2c_master_bus_config_t i2c_config = {
0 commit comments