ESP32 S3 If I2S is set to 96kHz and I2S_MCLK_MULTIPLE_192 Web browser don't load pages

CatNoir
Posts: 3
Joined: Wed Apr 24, 2024 3:52 pm

ESP32 S3 If I2S is set to 96kHz and I2S_MCLK_MULTIPLE_192 Web browser don't load pages

Postby CatNoir » Wed Apr 24, 2024 9:40 pm

Hello everyone,

In Visual Studio Code
ESP32 S3 Mini-1 chip revision: v0.1 ESP-IDF v5.0.1

After i2s_example_init_std();
[if set I2S_MCLK_MULTIPLE_192 and I2S_Sample_Freq_Hz 96000Hz]
Web browser don't load simple Http Server /hello page (like in this example belowe) .


If set I2S_MCLK_MULTIPLE_384 and I2S_Sample_Freq_Hz 48000Hz all is fine.

Why?
The multiple of mclk to sample rate I2S_MCLK_MULTIPLE_192 is a not accepted value for ESP32 S3?

Any advice?

  1. /* Simple HTTP Server Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9.  
  10. #include <esp_wifi.h>
  11. #include <esp_event.h>
  12. #include <esp_log.h>
  13. #include <esp_system.h>
  14. #include <nvs_flash.h>
  15. #include <sys/param.h>
  16. #include "nvs_flash.h"
  17. #include "esp_netif.h"
  18. #include "esp_eth.h"
  19. #include "protocol_examples_common.h"
  20. #include "esp_tls_crypto.h"
  21. #include <esp_http_server.h>
  22. #include "driver/i2c.h"
  23. #include "driver/i2s_common.h"
  24. #include "driver/i2s_std.h"
  25.  
  26. #define I2S_MCLK_MULTIPLE_192       192
  27.  
  28. #define I2S_Sample_Freq_Hz              96000
  29. #define I2S_Sample_Samples_DMA_FRAME_NUM    1020
  30. #define I2S_Sample_Samples_DMA_DESC_NUM     4
  31. #define I2S_Sample_Samples_Buffor_Len       3*(I2S_Sample_Samples_DMA_FRAME_NUM+(I2S_Sample_Samples_DMA_FRAME_NUM/3))
  32.  
  33. #define EXAMPLE_STD_MCLK_IO1 GPIO_NUM_36
  34. #define EXAMPLE_STD_BCLK_IO1 GPIO_NUM_34 // I2S bit clock io number
  35. #define EXAMPLE_STD_WS_IO1 GPIO_NUM_33   // I2S word select io number
  36. #define EXAMPLE_STD_DOUT_IO1 GPIO_NUM_35 // I2S data out io number
  37. #define EXAMPLE_STD_DIN_IO1 GPIO_NUM_19  // I2S data in io number
  38.  
  39. static i2s_chan_handle_t tx_chan; // I2S tx channel handler
  40. /* A simple example that demonstrates how to create GET and POST
  41.  * handlers for the web server.
  42.  */
  43.  
  44. static const char *TAG = "example";
  45.  
  46. #if CONFIG_EXAMPLE_BASIC_AUTH
  47.  
  48. typedef struct {
  49.     char    *username;
  50.     char    *password;
  51. } basic_auth_info_t;
  52.  
  53. #define HTTPD_401      "401 UNAUTHORIZED"           /*!< HTTP Response 401 */
  54.  
  55. static char *http_auth_basic(const char *username, const char *password)
  56. {
  57.     int out;
  58.     char *user_info = NULL;
  59.     char *digest = NULL;
  60.     size_t n = 0;
  61.     asprintf(&user_info, "%s:%s", username, password);
  62.     if (!user_info) {
  63.         ESP_LOGE(TAG, "No enough memory for user information");
  64.         return NULL;
  65.     }
  66.     esp_crypto_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
  67.  
  68.     /* 6: The length of the "Basic " string
  69.      * n: Number of bytes for a base64 encode format
  70.      * 1: Number of bytes for a reserved which be used to fill zero
  71.     */
  72.     digest = calloc(1, 6 + n + 1);
  73.     if (digest) {
  74.         strcpy(digest, "Basic ");
  75.         esp_crypto_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
  76.     }
  77.     free(user_info);
  78.     return digest;
  79. }
  80.  
  81. /* An HTTP GET handler */
  82. static esp_err_t basic_auth_get_handler(httpd_req_t *req)
  83. {
  84.     char *buf = NULL;
  85.     size_t buf_len = 0;
  86.     basic_auth_info_t *basic_auth_info = req->user_ctx;
  87.  
  88.     buf_len = httpd_req_get_hdr_value_len(req, "Authorization") + 1;
  89.     if (buf_len > 1) {
  90.         buf = calloc(1, buf_len);
  91.         if (!buf) {
  92.             ESP_LOGE(TAG, "No enough memory for basic authorization");
  93.             return ESP_ERR_NO_MEM;
  94.         }
  95.  
  96.         if (httpd_req_get_hdr_value_str(req, "Authorization", buf, buf_len) == ESP_OK) {
  97.             ESP_LOGI(TAG, "Found header => Authorization: %s", buf);
  98.         } else {
  99.             ESP_LOGE(TAG, "No auth value received");
  100.         }
  101.  
  102.         char *auth_credentials = http_auth_basic(basic_auth_info->username, basic_auth_info->password);
  103.         if (!auth_credentials) {
  104.             ESP_LOGE(TAG, "No enough memory for basic authorization credentials");
  105.             free(buf);
  106.             return ESP_ERR_NO_MEM;
  107.         }
  108.  
  109.         if (strncmp(auth_credentials, buf, buf_len)) {
  110.             ESP_LOGE(TAG, "Not authenticated");
  111.             httpd_resp_set_status(req, HTTPD_401);
  112.             httpd_resp_set_type(req, "application/json");
  113.             httpd_resp_set_hdr(req, "Connection", "keep-alive");
  114.             httpd_resp_set_hdr(req, "WWW-Authenticate", "Basic realm=\"Hello\"");
  115.             httpd_resp_send(req, NULL, 0);
  116.         } else {
  117.             ESP_LOGI(TAG, "Authenticated!");
  118.             char *basic_auth_resp = NULL;
  119.             httpd_resp_set_status(req, HTTPD_200);
  120.             httpd_resp_set_type(req, "application/json");
  121.             httpd_resp_set_hdr(req, "Connection", "keep-alive");
  122.             asprintf(&basic_auth_resp, "{\"authenticated\": true,\"user\": \"%s\"}", basic_auth_info->username);
  123.             if (!basic_auth_resp) {
  124.                 ESP_LOGE(TAG, "No enough memory for basic authorization response");
  125.                 free(auth_credentials);
  126.                 free(buf);
  127.                 return ESP_ERR_NO_MEM;
  128.             }
  129.             httpd_resp_send(req, basic_auth_resp, strlen(basic_auth_resp));
  130.             free(basic_auth_resp);
  131.         }
  132.         free(auth_credentials);
  133.         free(buf);
  134.     } else {
  135.         ESP_LOGE(TAG, "No auth header received");
  136.         httpd_resp_set_status(req, HTTPD_401);
  137.         httpd_resp_set_type(req, "application/json");
  138.         httpd_resp_set_hdr(req, "Connection", "keep-alive");
  139.         httpd_resp_set_hdr(req, "WWW-Authenticate", "Basic realm=\"Hello\"");
  140.         httpd_resp_send(req, NULL, 0);
  141.     }
  142.  
  143.     return ESP_OK;
  144. }
  145.  
  146. static httpd_uri_t basic_auth = {
  147.     .uri       = "/basic_auth",
  148.     .method    = HTTP_GET,
  149.     .handler   = basic_auth_get_handler,
  150. };
  151.  
  152. static void httpd_register_basic_auth(httpd_handle_t server)
  153. {
  154.     basic_auth_info_t *basic_auth_info = calloc(1, sizeof(basic_auth_info_t));
  155.     if (basic_auth_info) {
  156.         basic_auth_info->username = CONFIG_EXAMPLE_BASIC_AUTH_USERNAME;
  157.         basic_auth_info->password = CONFIG_EXAMPLE_BASIC_AUTH_PASSWORD;
  158.  
  159.         basic_auth.user_ctx = basic_auth_info;
  160.         httpd_register_uri_handler(server, &basic_auth);
  161.     }
  162. }
  163. #endif
  164.  
  165. /* An HTTP GET handler */
  166. static esp_err_t hello_get_handler(httpd_req_t *req)
  167. {
  168.     char*  buf;
  169.     size_t buf_len;
  170.  
  171.     /* Get header value string length and allocate memory for length + 1,
  172.      * extra byte for null termination */
  173.     buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
  174.     if (buf_len > 1) {
  175.         buf = malloc(buf_len);
  176.         /* Copy null terminated value string into buffer */
  177.         if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
  178.             ESP_LOGI(TAG, "Found header => Host: %s", buf);
  179.         }
  180.         free(buf);
  181.     }
  182.  
  183.     buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
  184.     if (buf_len > 1) {
  185.         buf = malloc(buf_len);
  186.         if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
  187.             ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
  188.         }
  189.         free(buf);
  190.     }
  191.  
  192.     buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
  193.     if (buf_len > 1) {
  194.         buf = malloc(buf_len);
  195.         if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
  196.             ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
  197.         }
  198.         free(buf);
  199.     }
  200.  
  201.     /* Read URL query string length and allocate memory for length + 1,
  202.      * extra byte for null termination */
  203.     buf_len = httpd_req_get_url_query_len(req) + 1;
  204.     if (buf_len > 1) {
  205.         buf = malloc(buf_len);
  206.         if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
  207.             ESP_LOGI(TAG, "Found URL query => %s", buf);
  208.             char param[32];
  209.             /* Get value of expected key from query string */
  210.             if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
  211.                 ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
  212.             }
  213.             if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
  214.                 ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
  215.             }
  216.             if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
  217.                 ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
  218.             }
  219.         }
  220.         free(buf);
  221.     }
  222.  
  223.     /* Set some custom headers */
  224.     httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
  225.     httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");
  226.  
  227.     /* Send response with custom headers and body set as the
  228.      * string passed in user context*/
  229.     const char* resp_str = (const char*) req->user_ctx;
  230.     httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  231.  
  232.     /* After sending the HTTP response the old HTTP request
  233.      * headers are lost. Check if HTTP request headers can be read now. */
  234.     if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
  235.         ESP_LOGI(TAG, "Request headers lost");
  236.     }
  237.     return ESP_OK;
  238. }
  239.  
  240. static const httpd_uri_t hello = {
  241.     .uri       = "/hello",
  242.     .method    = HTTP_GET,
  243.     .handler   = hello_get_handler,
  244.     /* Let's pass response string in user
  245.      * context to demonstrate it's usage */
  246.     .user_ctx  = "Hello World!"
  247. };
  248.  
  249. /* An HTTP POST handler */
  250. static esp_err_t echo_post_handler(httpd_req_t *req)
  251. {
  252.     char buf[100];
  253.     int ret, remaining = req->content_len;
  254.  
  255.     while (remaining > 0) {
  256.         /* Read the data for the request */
  257.         if ((ret = httpd_req_recv(req, buf,
  258.                         MIN(remaining, sizeof(buf)))) <= 0) {
  259.             if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  260.                 /* Retry receiving if timeout occurred */
  261.                 continue;
  262.             }
  263.             return ESP_FAIL;
  264.         }
  265.  
  266.         /* Send back the same data */
  267.         httpd_resp_send_chunk(req, buf, ret);
  268.         remaining -= ret;
  269.  
  270.         /* Log data received */
  271.         ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
  272.         ESP_LOGI(TAG, "%.*s", ret, buf);
  273.         ESP_LOGI(TAG, "====================================");
  274.     }
  275.  
  276.     // End response
  277.     httpd_resp_send_chunk(req, NULL, 0);
  278.     return ESP_OK;
  279. }
  280.  
  281. static const httpd_uri_t echo = {
  282.     .uri       = "/echo",
  283.     .method    = HTTP_POST,
  284.     .handler   = echo_post_handler,
  285.     .user_ctx  = NULL
  286. };
  287.  
  288. /* This handler allows the custom error handling functionality to be
  289.  * tested from client side. For that, when a PUT request 0 is sent to
  290.  * URI /ctrl, the /hello and /echo URIs are unregistered and following
  291.  * custom error handler http_404_error_handler() is registered.
  292.  * Afterwards, when /hello or /echo is requested, this custom error
  293.  * handler is invoked which, after sending an error message to client,
  294.  * either closes the underlying socket (when requested URI is /echo)
  295.  * or keeps it open (when requested URI is /hello). This allows the
  296.  * client to infer if the custom error handler is functioning as expected
  297.  * by observing the socket state.
  298.  */
  299. esp_err_t http_404_error_handler(httpd_req_t *req, httpd_err_code_t err)
  300. {
  301.     if (strcmp("/hello", req->uri) == 0) {
  302.         httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "/hello URI is not available");
  303.         /* Return ESP_OK to keep underlying socket open */
  304.         return ESP_OK;
  305.     } else if (strcmp("/echo", req->uri) == 0) {
  306.         httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "/echo URI is not available");
  307.         /* Return ESP_FAIL to close underlying socket */
  308.         return ESP_FAIL;
  309.     }
  310.     /* For any other URI send 404 and close socket */
  311.     httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Some 404 error message");
  312.     return ESP_FAIL;
  313. }
  314.  
  315. /* An HTTP PUT handler. This demonstrates realtime
  316.  * registration and deregistration of URI handlers
  317.  */
  318. static esp_err_t ctrl_put_handler(httpd_req_t *req)
  319. {
  320.     char buf;
  321.     int ret;
  322.  
  323.     if ((ret = httpd_req_recv(req, &buf, 1)) <= 0) {
  324.         if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
  325.             httpd_resp_send_408(req);
  326.         }
  327.         return ESP_FAIL;
  328.     }
  329.  
  330.     if (buf == '0') {
  331.         /* URI handlers can be unregistered using the uri string */
  332.         ESP_LOGI(TAG, "Unregistering /hello and /echo URIs");
  333.         httpd_unregister_uri(req->handle, "/hello");
  334.         httpd_unregister_uri(req->handle, "/echo");
  335.         /* Register the custom error handler */
  336.         httpd_register_err_handler(req->handle, HTTPD_404_NOT_FOUND, http_404_error_handler);
  337.     }
  338.     else {
  339.         ESP_LOGI(TAG, "Registering /hello and /echo URIs");
  340.         httpd_register_uri_handler(req->handle, &hello);
  341.         httpd_register_uri_handler(req->handle, &echo);
  342.         /* Unregister custom error handler */
  343.         httpd_register_err_handler(req->handle, HTTPD_404_NOT_FOUND, NULL);
  344.     }
  345.  
  346.     /* Respond with empty body */
  347.     httpd_resp_send(req, NULL, 0);
  348.     return ESP_OK;
  349. }
  350.  
  351. static const httpd_uri_t ctrl = {
  352.     .uri       = "/ctrl",
  353.     .method    = HTTP_PUT,
  354.     .handler   = ctrl_put_handler,
  355.     .user_ctx  = NULL
  356. };
  357.  
  358. static httpd_handle_t start_webserver(void)
  359. {
  360.     httpd_handle_t server = NULL;
  361.     httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  362.     config.lru_purge_enable = true;
  363.  
  364.     // Start the httpd server
  365.     ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  366.     if (httpd_start(&server, &config) == ESP_OK) {
  367.         // Set URI handlers
  368.         ESP_LOGI(TAG, "Registering URI handlers");
  369.         httpd_register_uri_handler(server, &hello);
  370.         httpd_register_uri_handler(server, &echo);
  371.         httpd_register_uri_handler(server, &ctrl);
  372.         #if CONFIG_EXAMPLE_BASIC_AUTH
  373.         httpd_register_basic_auth(server);
  374.         #endif
  375.         return server;
  376.     }
  377.  
  378.     ESP_LOGI(TAG, "Error starting server!");
  379.     return NULL;
  380. }
  381.  
  382. static esp_err_t stop_webserver(httpd_handle_t server)
  383. {
  384.     // Stop the httpd server
  385.     return httpd_stop(server);
  386. }
  387.  
  388. static void disconnect_handler(void* arg, esp_event_base_t event_base,
  389.                                int32_t event_id, void* event_data)
  390. {
  391.     httpd_handle_t* server = (httpd_handle_t*) arg;
  392.     if (*server) {
  393.         ESP_LOGI(TAG, "Stopping webserver");
  394.         if (stop_webserver(*server) == ESP_OK) {
  395.             *server = NULL;
  396.         } else {
  397.             ESP_LOGE(TAG, "Failed to stop http server");
  398.         }
  399.     }
  400. }
  401.  
  402. static void connect_handler(void* arg, esp_event_base_t event_base,
  403.                             int32_t event_id, void* event_data)
  404. {
  405.     httpd_handle_t* server = (httpd_handle_t*) arg;
  406.     if (*server == NULL) {
  407.         ESP_LOGI(TAG, "Starting webserver");
  408.         *server = start_webserver();
  409.     }
  410. }
  411.  
  412. void i2s_example_init_std(void)
  413. {
  414.     // Setp 1: Determine the I2S channel configuration and allocate both channels
  415.     // The default configuration can be generated by the helper macro,
  416.     // it only requires the I2S controller id and I2S role
  417.     i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_1, I2S_ROLE_MASTER);
  418.     chan_cfg.dma_desc_num = I2S_Sample_Samples_DMA_DESC_NUM;
  419.     chan_cfg.dma_frame_num = I2S_Sample_Samples_DMA_FRAME_NUM;
  420.  
  421.     ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_chan, NULL));
  422.  
  423.     // Step 2: Setting the configurations of standard mode, and initialize rx & tx channels
  424.     // The slot configuration and clock configuration can be generated by the macros
  425.     //  These two helper macros is defined in 'i2s_std.h' which can only be used in STD mode.
  426.     //  They can help to specify the slot and clock configurations for initialization or re-configuring
  427.  
  428.     i2s_std_config_t std_cfg = {
  429.         .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(I2S_Sample_Freq_Hz),
  430.         .clk_cfg.mclk_multiple  = I2S_MCLK_MULTIPLE_192,
  431.         .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_24BIT, I2S_SLOT_MODE_MONO),
  432.         .slot_cfg.slot_mask=I2S_STD_SLOT_LEFT,
  433.         .gpio_cfg = {
  434.             .mclk = EXAMPLE_STD_MCLK_IO1, // some codecs may require mclk signal, this example doesn't need it
  435.             .bclk = EXAMPLE_STD_BCLK_IO1,
  436.             .ws = EXAMPLE_STD_WS_IO1,
  437.             .dout = EXAMPLE_STD_DOUT_IO1,
  438.             .din = I2S_GPIO_UNUSED, // In duplex mode, bind output and input to a same gpio can loopback internally
  439.             .invert_flags = {
  440.                 .mclk_inv = false,
  441.                 .bclk_inv = false,
  442.                 .ws_inv = false,
  443.             },
  444.         },
  445.     };
  446.    
  447.     // Initialize the channels
  448.     ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_chan, &std_cfg));
  449. }
  450. void app_main(void)
  451. {
  452.     static httpd_handle_t server = NULL;
  453.  
  454.     ESP_ERROR_CHECK(nvs_flash_init());
  455.     ESP_ERROR_CHECK(esp_netif_init());
  456.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  457.  
  458.     /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  459.      * Read "Establishing Wi-Fi or Ethernet Connection" section in
  460.      * examples/protocols/README.md for more information about this function.
  461.      */
  462.     ESP_ERROR_CHECK(example_connect());
  463.  
  464.     /* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
  465.      * and re-start it upon connection.
  466.      */
  467. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  468.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  469.     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  470. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  471. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  472.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
  473.     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
  474. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  475.  
  476.     /* Start the server for the first time */
  477.     server = start_webserver();
  478.  
  479.     i2s_example_init_std();
  480. }
Attachments
WIFI_I2S.ZIP
VSCode Project Example
CONFIG_EXAMPLE_WIFI_SSID=myssid
CONFIG_EXAMPLE_WIFI_PASSWORD=mypassword
(37.92 KiB) Downloaded 67 times

Who is online

Users browsing this forum: ESP_Sprite and 52 guests