فهرست منبع

examples: Standardise naming of files, symbols, etc. in examples

* Use "example" in all example function & variable names,
  ie use i2c_example_xxx instead of i2c_xxx for example functions.
  Closes #198 https://github.com/espressif/esp-idf/issues/198
* Mark example functions, etc. static
* Replace uses of "test" & "demo" with "example"
* Split the UART example into two
* Rename "main" example files to end with "_main.c" for disambiguation
Angus Gratton 8 سال پیش
والد
کامیت
821c70f5d7
40فایلهای تغییر یافته به همراه623 افزوده شده و 597 حذف شده
  1. 3 0
      examples/README.md
  2. 4 8
      examples/bluetooth/blufi/main/blufi_example.h
  3. 18 18
      examples/bluetooth/blufi/main/blufi_example_main.c
  4. 1 1
      examples/bluetooth/blufi/main/blufi_security.c
  5. 1 1
      examples/ethernet/ethernet/main/ethernet_example_main.c
  6. 3 3
      examples/peripherals/gpio/main/gpio_example_main.c
  7. 39 35
      examples/peripherals/i2c/main/i2c_example_main.c
  8. 0 0
      examples/peripherals/i2s/main/i2s_example_main.c
  9. 0 0
      examples/peripherals/ledc/main/ledc_example_main.c
  10. 4 4
      examples/peripherals/pcnt/main/pcnt_example_main.c
  11. 3 1
      examples/peripherals/rmt_nec_tx_rx/README.md
  12. 0 358
      examples/peripherals/rmt_nec_tx_rx/main/infrared_nec.c
  13. 347 6
      examples/peripherals/rmt_nec_tx_rx/main/infrared_nec_main.c
  14. 2 2
      examples/peripherals/sigmadelta/main/sigmadelta_example_main.c
  15. 3 3
      examples/peripherals/spi_master/main/spi_master_example_main.c
  16. 6 6
      examples/peripherals/timer_group/main/timer_group_example_main.c
  17. 10 10
      examples/peripherals/touch_pad_interrupt/main/tp_interrupt_main.c
  18. 2 2
      examples/peripherals/touch_pad_read/main/tp_read_main.c
  19. 1 1
      examples/peripherals/uart_echo/Makefile
  20. 0 0
      examples/peripherals/uart_echo/main/component.mk
  21. 73 0
      examples/peripherals/uart_echo/main/uart_echo_example_main.c
  22. 9 0
      examples/peripherals/uart_events/Makefile
  23. 3 0
      examples/peripherals/uart_events/main/component.mk
  24. 25 72
      examples/peripherals/uart_events/main/uart_events_example_main.c
  25. 2 2
      examples/protocols/coap_client/main/coap_client_example_main.c
  26. 2 2
      examples/protocols/coap_server/main/coap_server_example_main.c
  27. 0 0
      examples/protocols/http_request/main/http_request_example_main.c
  28. 0 0
      examples/protocols/https_request/main/https_request_example_main.c
  29. 2 2
      examples/protocols/mdns/main/mdns_example_main.c
  30. 12 12
      examples/protocols/openssl_client/main/openssl_client_example.h
  31. 21 21
      examples/protocols/openssl_client/main/openssl_client_example_main.c
  32. 5 5
      examples/protocols/openssl_server/main/openssl_server_example.h
  33. 16 16
      examples/protocols/openssl_server/main/openssl_server_example_main.c
  34. 0 0
      examples/protocols/sntp/main/sntp_example_main.c
  35. 0 0
      examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c
  36. 0 0
      examples/storage/nvs_rw_value/main/nvs_value_example_main.c
  37. 0 0
      examples/storage/sd_card/main/sd_card_example_main.c
  38. 0 0
      examples/system/deep_sleep/main/deep_sleep_example_main.c
  39. 4 4
      examples/system/ota/main/ota_example_main.c
  40. 2 2
      examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c

+ 3 - 0
examples/README.md

@@ -32,4 +32,7 @@ In addition, here are some tips for creating good examples:
 
 * A good example is documented and the basic options can be configured.
 * A good example does not contain a lot of code. If there is a lot of generic code in the example, consider refactoring that code into a standalone component and then use the component's API in your example.
+* Names (of files, functions, variables, etc.) inside examples should be distinguishable from names of other parts of IDF (ideally, use `example` in names.)
+* Functions and variables used inside examples should be declared static where possible.
+* Examples should demonstrate one distinct thing each. Avoid multi-purposed "demo" examples, split these into multiple examples instead.
 * Examples must be licensed under the Apache License 2.0 or (preferably for examples) if possible you can declare the example to be Public Domain / Creative Commons Zero.

+ 4 - 8
examples/bluetooth/blufi/main/blufi_demo.h → examples/bluetooth/blufi/main/blufi_example.h

@@ -1,10 +1,8 @@
-#ifndef __BLUFI_DEMO_H__
-#define __BLUFI_DEMO_H__
+#pragma once
 
-
-#define BLUFI_DEMO_TAG "BLUFI_DEMO"
-#define BLUFI_INFO(fmt, ...)   ESP_LOGI(BLUFI_DEMO_TAG, fmt, ##__VA_ARGS__) 
-#define BLUFI_ERROR(fmt, ...)  ESP_LOGE(BLUFI_DEMO_TAG, fmt, ##__VA_ARGS__) 
+#define BLUFI_EXAMPLE_TAG "BLUFI_EXAMPLE"
+#define BLUFI_INFO(fmt, ...)   ESP_LOGI(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__) 
+#define BLUFI_ERROR(fmt, ...)  ESP_LOGE(BLUFI_EXAMPLE_TAG, fmt, ##__VA_ARGS__) 
 
 void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
 int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
@@ -13,5 +11,3 @@ uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len);
 
 int blufi_security_init(void);
 void blufi_security_deinit(void);
-
-#endif /* __BLUFI_DEMO_H__ */

+ 18 - 18
examples/bluetooth/blufi/main/blufi_main.c → examples/bluetooth/blufi/main/blufi_example_main.c

@@ -30,19 +30,19 @@
 #include "esp_gap_ble_api.h"
 #include "esp_bt_main.h"
 #include "esp_bt_device.h"
-#include "blufi_demo.h"
+#include "blufi_example.h"
 
-static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
+static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
 
 #define BLUFI_DEVICE_NAME            "BLUFI_DEVICE"
-static uint8_t blufi_service_uuid128[32] = {
+static uint8_t example_service_uuid128[32] = {
     /* LSB <--------------------------------------------------------------------------------> MSB */
     //first uuid, 16bit, [12],[13] is the value
     0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
 };
 
 //static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] =  {0x12, 0x23, 0x45, 0x56};
-static esp_ble_adv_data_t blufi_adv_data = {
+static esp_ble_adv_data_t example_adv_data = {
     .set_scan_rsp = false,
     .include_name = true,
     .include_txpower = true,
@@ -54,11 +54,11 @@ static esp_ble_adv_data_t blufi_adv_data = {
     .service_data_len = 0,
     .p_service_data = NULL,
     .service_uuid_len = 16,
-    .p_service_uuid = blufi_service_uuid128,
+    .p_service_uuid = example_service_uuid128,
     .flag = 0x6,
 };
 
-static esp_ble_adv_params_t blufi_adv_params = {
+static esp_ble_adv_params_t example_adv_params = {
     .adv_int_min        = 0x100,
     .adv_int_max        = 0x100,
     .adv_type           = ADV_TYPE_IND,
@@ -88,7 +88,7 @@ static uint8_t gl_sta_bssid[6];
 static uint8_t gl_sta_ssid[32];
 static int gl_sta_ssid_len;
 
-static esp_err_t event_handler(void *ctx, system_event_t *event)
+static esp_err_t example_net_event_handler(void *ctx, system_event_t *event)
 {
     wifi_mode_t mode;
 
@@ -146,7 +146,7 @@ static void initialise_wifi(void)
 {
     tcpip_adapter_init();
     wifi_event_group = xEventGroupCreate();
-    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
+    ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) );
     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
@@ -154,24 +154,24 @@ static void initialise_wifi(void)
     ESP_ERROR_CHECK( esp_wifi_start() );
 }
 
-static esp_blufi_callbacks_t blufi_callbacks = {
-    .event_cb = blufi_event_callback,
+static esp_blufi_callbacks_t example_callbacks = {
+    .event_cb = example_event_callback,
     .negotiate_data_handler = blufi_dh_negotiate_data_handler,
     .encrypt_func = blufi_aes_encrypt,
     .decrypt_func = blufi_aes_decrypt,
     .checksum_func = blufi_crc_checksum,
 };
 
-static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
+static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
 {
     /* actually, should post to blufi_task handle the procedure,
-     * now, as a demo, we do simplely */
+     * now, as a example, we do it more simply */
     switch (event) {
     case ESP_BLUFI_EVENT_INIT_FINISH:
         BLUFI_INFO("BLUFI init finish\n");
 
         esp_ble_gap_set_device_name(BLUFI_DEVICE_NAME);
-        esp_ble_gap_config_adv_data(&blufi_adv_data);
+        esp_ble_gap_config_adv_data(&example_adv_data);
         break;
     case ESP_BLUFI_EVENT_DEINIT_FINISH:
         BLUFI_INFO("BLUFI init finish\n");
@@ -184,7 +184,7 @@ static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_
         break;
     case ESP_BLUFI_EVENT_BLE_DISCONNECT:
         BLUFI_INFO("BLUFI ble disconnect\n");
-        esp_ble_gap_start_advertising(&blufi_adv_params);
+        esp_ble_gap_start_advertising(&example_adv_params);
         break;
     case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:
         BLUFI_INFO("BLUFI Set WIFI opmode %d\n", param->wifi_mode.op_mode);
@@ -297,11 +297,11 @@ static void blufi_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_
     }
 }
 
-static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
+static void example_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
 {
     switch (event) {
     case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
-        esp_ble_gap_start_advertising(&blufi_adv_params);
+        esp_ble_gap_start_advertising(&example_adv_params);
         break;
     default:
         break;
@@ -340,8 +340,8 @@ void app_main()
     BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version());
 
     blufi_security_init();
-    esp_ble_gap_register_callback(gap_event_handler);
+    esp_ble_gap_register_callback(example_gap_event_handler);
 
-    esp_blufi_register_callbacks(&blufi_callbacks);
+    esp_blufi_register_callbacks(&example_callbacks);
     esp_blufi_profile_init();
 }

+ 1 - 1
examples/bluetooth/blufi/main/blufi_security.c

@@ -29,7 +29,7 @@
 #include "esp_bt_defs.h"
 #include "esp_gap_ble_api.h"
 #include "esp_bt_main.h"
-#include "blufi_demo.h"
+#include "blufi_example.h"
 
 #include "mbedtls/aes.h"
 #include "mbedtls/dhm.h"

+ 1 - 1
examples/ethernet/ethernet/main/ethernet_main.c → examples/ethernet/ethernet/main/ethernet_example_main.c

@@ -34,7 +34,7 @@
 #include "driver/gpio.h"
 #include "tlk110_phy.h"
 
-static const char *TAG = "eth_demo";
+static const char *TAG = "eth_example";
 
 #define DEFAULT_PHY_CONFIG (AUTO_MDIX_ENABLE|AUTO_NEGOTIATION_ENABLE|AN_1|AN_0|LED_CFG)
 #define PIN_PHY_POWER 17

+ 3 - 3
examples/peripherals/gpio/main/gpio_test.c → examples/peripherals/gpio/main/gpio_example_main.c

@@ -41,13 +41,13 @@
 
 static xQueueHandle gpio_evt_queue = NULL;
 
-void IRAM_ATTR gpio_isr_handler(void* arg)
+static void IRAM_ATTR gpio_isr_handler(void* arg)
 {
     uint32_t gpio_num = (uint32_t) arg;
     xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
 }
 
-void gpio_task_example(void* arg)
+static void gpio_task_example(void* arg)
 {
     uint32_t io_num;
     for(;;) {
@@ -62,7 +62,7 @@ void app_main()
     gpio_config_t io_conf;
     //disable interrupt
     io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
-    //set as output mode        
+    //set as output mode
     io_conf.mode = GPIO_MODE_OUTPUT;
     //bit mask of the pins that you want to set,e.g.GPIO18/19
     io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;

+ 39 - 35
examples/peripherals/i2c/main/i2c_test.c → examples/peripherals/i2c/main/i2c_example_main.c

@@ -47,18 +47,18 @@
 #define RW_TEST_LENGTH       129  /*!<Data length for r/w test, any value from 0-DATA_LENGTH*/
 #define DELAY_TIME_BETWEEN_ITEMS_MS   1234 /*!< delay time between different test items */
 
-#define I2C_SLAVE_SCL_IO     26    /*!<gpio number for i2c slave clock  */
-#define I2C_SLAVE_SDA_IO     25    /*!<gpio number for i2c slave data */
-#define I2C_SLAVE_NUM I2C_NUM_0    /*!<I2C port number for slave dev */
-#define I2C_SLAVE_TX_BUF_LEN  (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
-#define I2C_SLAVE_RX_BUF_LEN  (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
+#define I2C_EXAMPLE_SLAVE_SCL_IO     26    /*!<gpio number for i2c slave clock  */
+#define I2C_EXAMPLE_SLAVE_SDA_IO     25    /*!<gpio number for i2c slave data */
+#define I2C_EXAMPLE_SLAVE_NUM I2C_NUM_0    /*!<I2C port number for slave dev */
+#define I2C_EXAMPLE_SLAVE_TX_BUF_LEN  (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
+#define I2C_EXAMPLE_SLAVE_RX_BUF_LEN  (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
 
-#define I2C_MASTER_SCL_IO    19    /*!< gpio number for I2C master clock */
-#define I2C_MASTER_SDA_IO    18    /*!< gpio number for I2C master data  */
-#define I2C_MASTER_NUM I2C_NUM_1   /*!< I2C port number for master dev */
-#define I2C_MASTER_TX_BUF_DISABLE   0   /*!< I2C master do not need buffer */
-#define I2C_MASTER_RX_BUF_DISABLE   0   /*!< I2C master do not need buffer */
-#define I2C_MASTER_FREQ_HZ    100000     /*!< I2C master clock frequency */
+#define I2C_EXAMPLE_MASTER_SCL_IO    19    /*!< gpio number for I2C master clock */
+#define I2C_EXAMPLE_MASTER_SDA_IO    18    /*!< gpio number for I2C master data  */
+#define I2C_EXAMPLE_MASTER_NUM I2C_NUM_1   /*!< I2C port number for master dev */
+#define I2C_EXAMPLE_MASTER_TX_BUF_DISABLE   0   /*!< I2C master do not need buffer */
+#define I2C_EXAMPLE_MASTER_RX_BUF_DISABLE   0   /*!< I2C master do not need buffer */
+#define I2C_EXAMPLE_MASTER_FREQ_HZ    100000     /*!< I2C master clock frequency */
 
 #define BH1750_SENSOR_ADDR  0x23    /*!< slave address for BH1750 sensor */
 #define BH1750_CMD_START    0x23    /*!< Command to set measure mode */
@@ -81,7 +81,7 @@ xSemaphoreHandle print_mux;
  * --------|--------------------------|----------------------|--------------------|------|
  *
  */
-esp_err_t i2c_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
+static esp_err_t i2c_example_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
 {
     if (size == 0) {
         return ESP_OK;
@@ -110,7 +110,7 @@ esp_err_t i2c_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t siz
  * --------|---------------------------|----------------------|------|
  *
  */
-esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t size)
+static esp_err_t i2c_example_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t size)
 {
     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
     i2c_master_start(cmd);
@@ -135,7 +135,7 @@ esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t si
  * | start | slave_addr + rd_bit + ack | read 1 byte + ack  | read 1 byte + nack | stop |
  * --------|---------------------------|--------------------|--------------------|------|
  */
-esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* data_l)
+static esp_err_t i2c_example_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* data_l)
 {
     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
     i2c_master_start(cmd);
@@ -166,42 +166,46 @@ esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* d
 /**
  * @brief i2c master initialization
  */
-void i2c_master_init()
+static void i2c_example_master_init()
 {
-    int i2c_master_port = I2C_MASTER_NUM;
+    int i2c_master_port = I2C_EXAMPLE_MASTER_NUM;
     i2c_config_t conf;
     conf.mode = I2C_MODE_MASTER;
-    conf.sda_io_num = I2C_MASTER_SDA_IO;
+    conf.sda_io_num = I2C_EXAMPLE_MASTER_SDA_IO;
     conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
-    conf.scl_io_num = I2C_MASTER_SCL_IO;
+    conf.scl_io_num = I2C_EXAMPLE_MASTER_SCL_IO;
     conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
-    conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
+    conf.master.clk_speed = I2C_EXAMPLE_MASTER_FREQ_HZ;
     i2c_param_config(i2c_master_port, &conf);
-    i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
+    i2c_driver_install(i2c_master_port, conf.mode,
+                       I2C_EXAMPLE_MASTER_RX_BUF_DISABLE,
+                       I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0);
 }
 
 /**
  * @brief i2c slave initialization
  */
-void i2c_slave_init()
+static void i2c_example_slave_init()
 {
-    int i2c_slave_port = I2C_SLAVE_NUM;
+    int i2c_slave_port = I2C_EXAMPLE_SLAVE_NUM;
     i2c_config_t conf_slave;
-    conf_slave.sda_io_num = I2C_SLAVE_SDA_IO;
+    conf_slave.sda_io_num = I2C_EXAMPLE_SLAVE_SDA_IO;
     conf_slave.sda_pullup_en = GPIO_PULLUP_ENABLE;
-    conf_slave.scl_io_num = I2C_SLAVE_SCL_IO;
+    conf_slave.scl_io_num = I2C_EXAMPLE_SLAVE_SCL_IO;
     conf_slave.scl_pullup_en = GPIO_PULLUP_ENABLE;
     conf_slave.mode = I2C_MODE_SLAVE;
     conf_slave.slave.addr_10bit_en = 0;
     conf_slave.slave.slave_addr = ESP_SLAVE_ADDR;
     i2c_param_config(i2c_slave_port, &conf_slave);
-    i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
+    i2c_driver_install(i2c_slave_port, conf_slave.mode,
+                       I2C_EXAMPLE_SLAVE_RX_BUF_LEN,
+                       I2C_EXAMPLE_SLAVE_TX_BUF_LEN, 0);
 }
 
 /**
  * @brief test function to show buffer
  */
-void disp_buf(uint8_t* buf, int len)
+static void disp_buf(uint8_t* buf, int len)
 {
     int i;
     for (i = 0; i < len; i++) {
@@ -213,7 +217,7 @@ void disp_buf(uint8_t* buf, int len)
     printf("\n");
 }
 
-void i2c_test_task(void* arg)
+static void i2c_test_task(void* arg)
 {
     int i = 0;
     int ret;
@@ -224,7 +228,7 @@ void i2c_test_task(void* arg)
     uint8_t sensor_data_h, sensor_data_l;
 
     while (1) {
-        ret = i2c_master_sensor_test( I2C_MASTER_NUM, &sensor_data_h, &sensor_data_l);
+        ret = i2c_example_master_sensor_test( I2C_EXAMPLE_MASTER_NUM, &sensor_data_h, &sensor_data_l);
         xSemaphoreTake(print_mux, portMAX_DELAY);
         printf("*******************\n");
         printf("TASK[%d]  MASTER READ SENSOR( BH1750 )\n", task_idx);
@@ -243,12 +247,12 @@ void i2c_test_task(void* arg)
         for (i = 0; i < DATA_LENGTH; i++) {
             data[i] = i;
         }
-        size_t d_size = i2c_slave_write_buffer(I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
+        size_t d_size = i2c_slave_write_buffer(I2C_EXAMPLE_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
         if (d_size == 0) {
             printf("i2c slave tx buffer full\n");
-            ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, DATA_LENGTH);
+            ret = i2c_example_master_read_slave(I2C_EXAMPLE_MASTER_NUM, data_rd, DATA_LENGTH);
         } else {
-            ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, RW_TEST_LENGTH);
+            ret = i2c_example_master_read_slave(I2C_EXAMPLE_MASTER_NUM, data_rd, RW_TEST_LENGTH);
         }
         xSemaphoreTake(print_mux, portMAX_DELAY);
         printf("*******************\n");
@@ -270,9 +274,9 @@ void i2c_test_task(void* arg)
             data_wr[i] = i + 10;
         }
         //we need to fill the slave buffer so that master can read later
-        ret = i2c_master_write_slave( I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
+        ret = i2c_example_master_write_slave( I2C_EXAMPLE_MASTER_NUM, data_wr, RW_TEST_LENGTH);
         if (ret == ESP_OK) {
-            size = i2c_slave_read_buffer( I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
+            size = i2c_slave_read_buffer( I2C_EXAMPLE_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
         }
         xSemaphoreTake(print_mux, portMAX_DELAY);
         printf("*******************\n");
@@ -294,8 +298,8 @@ void i2c_test_task(void* arg)
 void app_main()
 {
     print_mux = xSemaphoreCreateMutex();
-    i2c_slave_init();
-    i2c_master_init();
+    i2c_example_slave_init();
+    i2c_example_master_init();
 
     xTaskCreate(i2c_test_task, "i2c_test_task_0", 1024 * 2, (void* ) 0, 10, NULL);
     xTaskCreate(i2c_test_task, "i2c_test_task_1", 1024 * 2, (void* ) 1, 10, NULL);

+ 0 - 0
examples/peripherals/i2s/main/app_main.c → examples/peripherals/i2s/main/i2s_example_main.c


+ 0 - 0
examples/peripherals/ledc/main/ledc_fade.c → examples/peripherals/ledc/main/ledc_example_main.c


+ 4 - 4
examples/peripherals/pcnt/main/pcnt_test.c → examples/peripherals/pcnt/main/pcnt_example_main.c

@@ -53,7 +53,7 @@ typedef struct {
     uint32_t status; /*pulse counter internal status*/
 } pcnt_evt_t;
 
-void IRAM_ATTR pcnt_intr_handler(void* arg)
+static void IRAM_ATTR pcnt_example_intr_handler(void* arg)
 {
     uint32_t intr_status = PCNT.int_st.val;
     int i;
@@ -122,7 +122,7 @@ static void ledc_init(void)
     ledc_timer_config(&ledc_timer);
 }
 
-static void pcnt_init(void)
+static void pcnt_example_init(void)
 {
     pcnt_config_t pcnt_config = {
         /*Set PCNT_INPUT_SIG_IO as pulse input gpio */
@@ -175,7 +175,7 @@ static void pcnt_init(void)
     /*Reset counter value*/
     pcnt_counter_clear(PCNT_TEST_UNIT);
     /*Register ISR handler*/
-    pcnt_isr_register(pcnt_intr_handler, NULL, 0, NULL);
+    pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, NULL);
     /*Enable interrupt for PCNT unit*/
     pcnt_intr_enable(PCNT_TEST_UNIT);
     /*Resume counting*/
@@ -189,7 +189,7 @@ void app_main()
     /*Init PCNT event queue */
     pcnt_evt_queue = xQueueCreate(10, sizeof(pcnt_evt_t));
     /*Init PCNT functions*/
-    pcnt_init();
+    pcnt_example_init();
 
     int16_t count = 0;
     pcnt_evt_t evt;

+ 3 - 1
examples/peripherals/rmt_nec_tx_rx/README.md

@@ -2,5 +2,7 @@
 
 This example uses the remote control (RMT) peripheral to transmit and receive codes for the NEC infrared remote protocol.
 
-Configuration (pin numbers, etc.) can be modified in top of the main/infrared_nec.c file.
+Configuration (pin numbers, etc.) can be modified in top of the main/infrared_nec_main.c file.
+
+By default, this example runs a self test which assumes the TX and RX GPIO pins are connected together. To disable this, comment RMT_RX_SELF_TEST in infrared_nec_main.c.
 

+ 0 - 358
examples/peripherals/rmt_nec_tx_rx/main/infrared_nec.c

@@ -1,358 +0,0 @@
-/* NEC remote infrared RMT example
-
-   This example code is in the Public Domain (or CC0 licensed, at your option.)
-
-   Unless required by applicable law or agreed to in writing, this
-   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-   CONDITIONS OF ANY KIND, either express or implied.
-*/
-#include <stdio.h>
-#include <string.h>
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "freertos/queue.h"
-#include "freertos/semphr.h"
-#include "esp_err.h"
-#include "esp_log.h"
-#include "driver/rmt.h"
-#include "driver/periph_ctrl.h"
-#include "soc/rmt_reg.h"
-
-static const char* NEC_TAG = "NEC";
-
-//CHOOSE SELF TEST OR NORMAL TEST
-#define RMT_RX_SELF_TEST   1
-
-/******************************************************/
-/*****                SELF TEST:                  *****/
-/*Connect RMT_TX_GPIO_NUM with RMT_RX_GPIO_NUM        */
-/*TX task will send NEC data with carrier disabled    */
-/*RX task will print NEC data it receives.            */
-/******************************************************/
-#if RMT_RX_SELF_TEST
-#define RMT_RX_ACTIVE_LEVEL  1   /*!< Data bit is active high for self test mode */
-#define RMT_TX_CARRIER_EN    0   /*!< Disable carrier for self test mode  */
-#else
-//Test with infrared LED, we have to enable carrier for transmitter
-//When testing via IR led, the receiver waveform is usually active-low.
-#define RMT_RX_ACTIVE_LEVEL  0   /*!< If we connect with a IR receiver, the data is active low */
-#define RMT_TX_CARRIER_EN    1   /*!< Enable carrier for IR transmitter test with IR led */
-#endif
-
-#define RMT_TX_CHANNEL    1     /*!< RMT channel for transmitter */
-#define RMT_TX_GPIO_NUM  16     /*!< GPIO number for transmitter signal */
-#define RMT_RX_CHANNEL    0     /*!< RMT channel for receiver */
-#define RMT_RX_GPIO_NUM  19     /*!< GPIO number for receiver */
-#define RMT_CLK_DIV      100    /*!< RMT counter clock divider */
-#define RMT_TICK_10_US    (80000000/RMT_CLK_DIV/100000)   /*!< RMT counter value for 10 us.(Source clock is APB clock) */
-
-#define NEC_HEADER_HIGH_US    9000                         /*!< NEC protocol header: positive 9ms */
-#define NEC_HEADER_LOW_US     4500                         /*!< NEC protocol header: negative 4.5ms*/
-#define NEC_BIT_ONE_HIGH_US    560                         /*!< NEC protocol data bit 1: positive 0.56ms */
-#define NEC_BIT_ONE_LOW_US    (2250-NEC_BIT_ONE_HIGH_US)   /*!< NEC protocol data bit 1: negative 1.69ms */
-#define NEC_BIT_ZERO_HIGH_US   560                         /*!< NEC protocol data bit 0: positive 0.56ms */
-#define NEC_BIT_ZERO_LOW_US   (1120-NEC_BIT_ZERO_HIGH_US)  /*!< NEC protocol data bit 0: negative 0.56ms */
-#define NEC_BIT_END            560                         /*!< NEC protocol end: positive 0.56ms */
-#define NEC_BIT_MARGIN         20                          /*!< NEC parse margin time */
-
-#define NEC_ITEM_DURATION(d)  ((d & 0x7fff)*10/RMT_TICK_10_US)  /*!< Parse duration time from memory register value */
-#define NEC_DATA_ITEM_NUM   34  /*!< NEC code item number: header + 32bit data + end */
-#define RMT_TX_DATA_NUM  100    /*!< NEC tx test data number */
-#define rmt_item32_tIMEOUT_US  9500   /*!< RMT receiver timeout value(us) */
-
-/*
- * @brief Build register value of waveform for NEC one data bit
- */
-inline void nec_fill_item_level(rmt_item32_t* item, int high_us, int low_us)
-{
-    item->level0 = 1;
-    item->duration0 = (high_us) / 10 * RMT_TICK_10_US;
-    item->level1 = 0;
-    item->duration1 = (low_us) / 10 * RMT_TICK_10_US;
-}
-
-/*
- * @brief Generate NEC header value: active 9ms + negative 4.5ms
- */
-static void nec_fill_item_header(rmt_item32_t* item)
-{
-    nec_fill_item_level(item, NEC_HEADER_HIGH_US, NEC_HEADER_LOW_US);
-}
-
-/*
- * @brief Generate NEC data bit 1: positive 0.56ms + negative 1.69ms
- */
-static void nec_fill_item_bit_one(rmt_item32_t* item)
-{
-    nec_fill_item_level(item, NEC_BIT_ONE_HIGH_US, NEC_BIT_ONE_LOW_US);
-}
-
-/*
- * @brief Generate NEC data bit 0: positive 0.56ms + negative 0.56ms
- */
-static void nec_fill_item_bit_zero(rmt_item32_t* item)
-{
-    nec_fill_item_level(item, NEC_BIT_ZERO_HIGH_US, NEC_BIT_ZERO_LOW_US);
-}
-
-/*
- * @brief Generate NEC end signal: positive 0.56ms
- */
-static void nec_fill_item_end(rmt_item32_t* item)
-{
-    nec_fill_item_level(item, NEC_BIT_END, 0x7fff);
-}
-
-/*
- * @brief Check whether duration is around target_us
- */
-inline bool nec_check_in_range(int duration_ticks, int target_us, int margin_us)
-{
-    if(( NEC_ITEM_DURATION(duration_ticks) < (target_us + margin_us))
-        && ( NEC_ITEM_DURATION(duration_ticks) > (target_us - margin_us))) {
-        return true;
-    } else {
-        return false;
-    }
-}
-
-/*
- * @brief Check whether this value represents an NEC header
- */
-static bool nec_header_if(rmt_item32_t* item)
-{
-    if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
-        && nec_check_in_range(item->duration0, NEC_HEADER_HIGH_US, NEC_BIT_MARGIN)
-        && nec_check_in_range(item->duration1, NEC_HEADER_LOW_US, NEC_BIT_MARGIN)) {
-        return true;
-    }
-    return false;
-}
-
-/*
- * @brief Check whether this value represents an NEC data bit 1
- */
-static bool nec_bit_one_if(rmt_item32_t* item)
-{
-    if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
-        && nec_check_in_range(item->duration0, NEC_BIT_ONE_HIGH_US, NEC_BIT_MARGIN)
-        && nec_check_in_range(item->duration1, NEC_BIT_ONE_LOW_US, NEC_BIT_MARGIN)) {
-        return true;
-    }
-    return false;
-}
-
-/*
- * @brief Check whether this value represents an NEC data bit 0
- */
-static bool nec_bit_zero_if(rmt_item32_t* item)
-{
-    if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
-        && nec_check_in_range(item->duration0, NEC_BIT_ZERO_HIGH_US, NEC_BIT_MARGIN)
-        && nec_check_in_range(item->duration1, NEC_BIT_ZERO_LOW_US, NEC_BIT_MARGIN)) {
-        return true;
-    }
-    return false;
-}
-
-
-/*
- * @brief Parse NEC 32 bit waveform to address and command.
- */
-static int nec_parse_items(rmt_item32_t* item, int item_num, uint16_t* addr, uint16_t* data)
-{
-    int w_len = item_num;
-    if(w_len < NEC_DATA_ITEM_NUM) {
-        return -1;
-    }
-    int i = 0, j = 0;
-    if(!nec_header_if(item++)) {
-        return -1;
-    }
-    uint16_t addr_t = 0;
-    for(j = 0; j < 16; j++) {
-        if(nec_bit_one_if(item)) {
-            addr_t |= (1 << j);
-        } else if(nec_bit_zero_if(item)) {
-            addr_t |= (0 << j);
-        } else {
-            return -1;
-        }
-        item++;
-        i++;
-    }
-    uint16_t data_t = 0;
-    for(j = 0; j < 16; j++) {
-        if(nec_bit_one_if(item)) {
-            data_t |= (1 << j);
-        } else if(nec_bit_zero_if(item)) {
-            data_t |= (0 << j);
-        } else {
-            return -1;
-        }
-        item++;
-        i++;
-    }
-    *addr = addr_t;
-    *data = data_t;
-    return i;
-}
-
-/*
- * @brief Build NEC 32bit waveform.
- */
-static int nec_build_items(int channel, rmt_item32_t* item, int item_num, uint16_t addr, uint16_t cmd_data)
-{
-    int i = 0, j = 0;
-    if(item_num < NEC_DATA_ITEM_NUM) {
-        return -1;
-    }
-    nec_fill_item_header(item++);
-    i++;
-    for(j = 0; j < 16; j++) {
-        if(addr & 0x1) {
-            nec_fill_item_bit_one(item);
-        } else {
-            nec_fill_item_bit_zero(item);
-        }
-        item++;
-        i++;
-        addr >>= 1;
-    }
-    for(j = 0; j < 16; j++) {
-        if(cmd_data & 0x1) {
-            nec_fill_item_bit_one(item);
-        } else {
-            nec_fill_item_bit_zero(item);
-        }
-        item++;
-        i++;
-        cmd_data >>= 1;
-    }
-    nec_fill_item_end(item);
-    i++;
-    return i;
-}
-
-/*
- * @brief RMT transmitter initialization
- */
-static void rmt_tx_init()
-{
-    rmt_config_t rmt_tx;
-    rmt_tx.channel = RMT_TX_CHANNEL;
-    rmt_tx.gpio_num = RMT_TX_GPIO_NUM;
-    rmt_tx.mem_block_num = 1;
-    rmt_tx.clk_div = RMT_CLK_DIV;
-    rmt_tx.tx_config.loop_en = false;
-    rmt_tx.tx_config.carrier_duty_percent = 50;
-    rmt_tx.tx_config.carrier_freq_hz = 38000;
-    rmt_tx.tx_config.carrier_level = 1;
-    rmt_tx.tx_config.carrier_en = RMT_TX_CARRIER_EN;
-    rmt_tx.tx_config.idle_level = 0;
-    rmt_tx.tx_config.idle_output_en = true;
-    rmt_tx.rmt_mode = 0;
-    rmt_config(&rmt_tx);
-    rmt_driver_install(rmt_tx.channel, 0, 0);
-}
-
-/*
- * @brief RMT receiver initialization
- */
-void rmt_rx_init()
-{
-    rmt_config_t rmt_rx;
-    rmt_rx.channel = RMT_RX_CHANNEL;
-    rmt_rx.gpio_num = RMT_RX_GPIO_NUM;
-    rmt_rx.clk_div = RMT_CLK_DIV;
-    rmt_rx.mem_block_num = 1;
-    rmt_rx.rmt_mode = RMT_MODE_RX;
-    rmt_rx.rx_config.filter_en = true;
-    rmt_rx.rx_config.filter_ticks_thresh = 100;
-    rmt_rx.rx_config.idle_threshold = rmt_item32_tIMEOUT_US / 10 * (RMT_TICK_10_US);
-    rmt_config(&rmt_rx);
-    rmt_driver_install(rmt_rx.channel, 1000, 0);
-}
-
-/**
- * @brief RMT receiver demo, this task will print each received NEC data.
- *
- */
-void rmt_nec_rx_task()
-{
-    int channel = RMT_RX_CHANNEL;
-    rmt_rx_init();
-    RingbufHandle_t rb = NULL;
-    //get RMT RX ringbuffer
-    rmt_get_ringbuf_handler(channel, &rb);
-    rmt_rx_start(channel, 1);
-    while(rb) {
-        size_t rx_size = 0;
-        //try to receive data from ringbuffer.
-        //RMT driver will push all the data it receives to its ringbuffer.
-        //We just need to parse the value and return the spaces of ringbuffer.
-        rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 1000);
-        if(item) {
-            uint16_t rmt_addr;
-            uint16_t rmt_cmd;
-            int offset = 0;
-            while(1) {
-                //parse data value from ringbuffer.
-                int res = nec_parse_items(item + offset, rx_size / 4 - offset, &rmt_addr, &rmt_cmd);
-                if(res > 0) {
-                    offset += res + 1;
-                    ESP_LOGI(NEC_TAG, "RMT RCV --- addr: 0x%04x cmd: 0x%04x", rmt_addr, rmt_cmd);
-                } else {
-                    break;
-                }
-            }
-            //after parsing the data, return spaces to ringbuffer.
-            vRingbufferReturnItem(rb, (void*) item);
-        } else {
-            break;
-        }
-    }
-    vTaskDelete(NULL);
-}
-
-/**
- * @brief RMT transmitter demo, this task will periodically send NEC data. (100 * 32 bits each time.)
- *
- */
-void rmt_nec_tx_task()
-{
-    vTaskDelay(10);
-    rmt_tx_init();
-    esp_log_level_set(NEC_TAG, ESP_LOG_INFO);
-    int channel = RMT_TX_CHANNEL;
-    uint16_t cmd = 0x0;
-    uint16_t addr = 0x11;
-    int nec_tx_num = RMT_TX_DATA_NUM;
-    for(;;) {
-        ESP_LOGI(NEC_TAG, "RMT TX DATA");
-        size_t size = (sizeof(rmt_item32_t) * NEC_DATA_ITEM_NUM * nec_tx_num);
-        //each item represent a cycle of waveform.
-        rmt_item32_t* item = (rmt_item32_t*) malloc(size);
-        int item_num = NEC_DATA_ITEM_NUM * nec_tx_num;
-        memset((void*) item, 0, size);
-        int i, offset = 0;
-        while(1) {
-            //To build a series of waveforms.
-            i = nec_build_items(channel, item + offset, item_num - offset, ((~addr) << 8) | addr, cmd);
-            if(i < 0) {
-                break;
-            }
-            cmd++;
-            addr++;
-            offset += i;
-        }
-        //To send data according to the waveform items.
-        rmt_write_items(channel, item, item_num, true);
-        //Wait until sending is done.
-        rmt_wait_tx_done(channel);
-        //before we free the data, make sure sending is already done.
-        free(item);
-        vTaskDelay(2000 / portTICK_PERIOD_MS);
-    }
-    vTaskDelete(NULL);
-}

+ 347 - 6
examples/peripherals/rmt_nec_tx_rx/main/infrared_nec_main.c

@@ -7,17 +7,358 @@
    CONDITIONS OF ANY KIND, either express or implied.
 */
 #include <stdio.h>
+#include <string.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
-#include "esp_system.h"
-#include "nvs_flash.h"
+#include "freertos/queue.h"
+#include "freertos/semphr.h"
+#include "esp_err.h"
+#include "esp_log.h"
 #include "driver/rmt.h"
 #include "driver/periph_ctrl.h"
-extern void rmt_nec_tx_task();
-extern void rmt_nec_rx_task();
+#include "soc/rmt_reg.h"
+
+static const char* NEC_TAG = "NEC";
+
+//CHOOSE SELF TEST OR NORMAL TEST
+#define RMT_RX_SELF_TEST   1
+
+/******************************************************/
+/*****                SELF TEST:                  *****/
+/*Connect RMT_TX_GPIO_NUM with RMT_RX_GPIO_NUM        */
+/*TX task will send NEC data with carrier disabled    */
+/*RX task will print NEC data it receives.            */
+/******************************************************/
+#if RMT_RX_SELF_TEST
+#define RMT_RX_ACTIVE_LEVEL  1   /*!< Data bit is active high for self test mode */
+#define RMT_TX_CARRIER_EN    0   /*!< Disable carrier for self test mode  */
+#else
+//Test with infrared LED, we have to enable carrier for transmitter
+//When testing via IR led, the receiver waveform is usually active-low.
+#define RMT_RX_ACTIVE_LEVEL  0   /*!< If we connect with a IR receiver, the data is active low */
+#define RMT_TX_CARRIER_EN    1   /*!< Enable carrier for IR transmitter test with IR led */
+#endif
+
+#define RMT_TX_CHANNEL    1     /*!< RMT channel for transmitter */
+#define RMT_TX_GPIO_NUM  16     /*!< GPIO number for transmitter signal */
+#define RMT_RX_CHANNEL    0     /*!< RMT channel for receiver */
+#define RMT_RX_GPIO_NUM  19     /*!< GPIO number for receiver */
+#define RMT_CLK_DIV      100    /*!< RMT counter clock divider */
+#define RMT_TICK_10_US    (80000000/RMT_CLK_DIV/100000)   /*!< RMT counter value for 10 us.(Source clock is APB clock) */
+
+#define NEC_HEADER_HIGH_US    9000                         /*!< NEC protocol header: positive 9ms */
+#define NEC_HEADER_LOW_US     4500                         /*!< NEC protocol header: negative 4.5ms*/
+#define NEC_BIT_ONE_HIGH_US    560                         /*!< NEC protocol data bit 1: positive 0.56ms */
+#define NEC_BIT_ONE_LOW_US    (2250-NEC_BIT_ONE_HIGH_US)   /*!< NEC protocol data bit 1: negative 1.69ms */
+#define NEC_BIT_ZERO_HIGH_US   560                         /*!< NEC protocol data bit 0: positive 0.56ms */
+#define NEC_BIT_ZERO_LOW_US   (1120-NEC_BIT_ZERO_HIGH_US)  /*!< NEC protocol data bit 0: negative 0.56ms */
+#define NEC_BIT_END            560                         /*!< NEC protocol end: positive 0.56ms */
+#define NEC_BIT_MARGIN         20                          /*!< NEC parse margin time */
+
+#define NEC_ITEM_DURATION(d)  ((d & 0x7fff)*10/RMT_TICK_10_US)  /*!< Parse duration time from memory register value */
+#define NEC_DATA_ITEM_NUM   34  /*!< NEC code item number: header + 32bit data + end */
+#define RMT_TX_DATA_NUM  100    /*!< NEC tx test data number */
+#define rmt_item32_tIMEOUT_US  9500   /*!< RMT receiver timeout value(us) */
+
+/*
+ * @brief Build register value of waveform for NEC one data bit
+ */
+static inline void nec_fill_item_level(rmt_item32_t* item, int high_us, int low_us)
+{
+    item->level0 = 1;
+    item->duration0 = (high_us) / 10 * RMT_TICK_10_US;
+    item->level1 = 0;
+    item->duration1 = (low_us) / 10 * RMT_TICK_10_US;
+}
+
+/*
+ * @brief Generate NEC header value: active 9ms + negative 4.5ms
+ */
+static void nec_fill_item_header(rmt_item32_t* item)
+{
+    nec_fill_item_level(item, NEC_HEADER_HIGH_US, NEC_HEADER_LOW_US);
+}
+
+/*
+ * @brief Generate NEC data bit 1: positive 0.56ms + negative 1.69ms
+ */
+static void nec_fill_item_bit_one(rmt_item32_t* item)
+{
+    nec_fill_item_level(item, NEC_BIT_ONE_HIGH_US, NEC_BIT_ONE_LOW_US);
+}
+
+/*
+ * @brief Generate NEC data bit 0: positive 0.56ms + negative 0.56ms
+ */
+static void nec_fill_item_bit_zero(rmt_item32_t* item)
+{
+    nec_fill_item_level(item, NEC_BIT_ZERO_HIGH_US, NEC_BIT_ZERO_LOW_US);
+}
+
+/*
+ * @brief Generate NEC end signal: positive 0.56ms
+ */
+static void nec_fill_item_end(rmt_item32_t* item)
+{
+    nec_fill_item_level(item, NEC_BIT_END, 0x7fff);
+}
+
+/*
+ * @brief Check whether duration is around target_us
+ */
+inline bool nec_check_in_range(int duration_ticks, int target_us, int margin_us)
+{
+    if(( NEC_ITEM_DURATION(duration_ticks) < (target_us + margin_us))
+        && ( NEC_ITEM_DURATION(duration_ticks) > (target_us - margin_us))) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+/*
+ * @brief Check whether this value represents an NEC header
+ */
+static bool nec_header_if(rmt_item32_t* item)
+{
+    if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
+        && nec_check_in_range(item->duration0, NEC_HEADER_HIGH_US, NEC_BIT_MARGIN)
+        && nec_check_in_range(item->duration1, NEC_HEADER_LOW_US, NEC_BIT_MARGIN)) {
+        return true;
+    }
+    return false;
+}
+
+/*
+ * @brief Check whether this value represents an NEC data bit 1
+ */
+static bool nec_bit_one_if(rmt_item32_t* item)
+{
+    if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
+        && nec_check_in_range(item->duration0, NEC_BIT_ONE_HIGH_US, NEC_BIT_MARGIN)
+        && nec_check_in_range(item->duration1, NEC_BIT_ONE_LOW_US, NEC_BIT_MARGIN)) {
+        return true;
+    }
+    return false;
+}
+
+/*
+ * @brief Check whether this value represents an NEC data bit 0
+ */
+static bool nec_bit_zero_if(rmt_item32_t* item)
+{
+    if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
+        && nec_check_in_range(item->duration0, NEC_BIT_ZERO_HIGH_US, NEC_BIT_MARGIN)
+        && nec_check_in_range(item->duration1, NEC_BIT_ZERO_LOW_US, NEC_BIT_MARGIN)) {
+        return true;
+    }
+    return false;
+}
+
+
+/*
+ * @brief Parse NEC 32 bit waveform to address and command.
+ */
+static int nec_parse_items(rmt_item32_t* item, int item_num, uint16_t* addr, uint16_t* data)
+{
+    int w_len = item_num;
+    if(w_len < NEC_DATA_ITEM_NUM) {
+        return -1;
+    }
+    int i = 0, j = 0;
+    if(!nec_header_if(item++)) {
+        return -1;
+    }
+    uint16_t addr_t = 0;
+    for(j = 0; j < 16; j++) {
+        if(nec_bit_one_if(item)) {
+            addr_t |= (1 << j);
+        } else if(nec_bit_zero_if(item)) {
+            addr_t |= (0 << j);
+        } else {
+            return -1;
+        }
+        item++;
+        i++;
+    }
+    uint16_t data_t = 0;
+    for(j = 0; j < 16; j++) {
+        if(nec_bit_one_if(item)) {
+            data_t |= (1 << j);
+        } else if(nec_bit_zero_if(item)) {
+            data_t |= (0 << j);
+        } else {
+            return -1;
+        }
+        item++;
+        i++;
+    }
+    *addr = addr_t;
+    *data = data_t;
+    return i;
+}
+
+/*
+ * @brief Build NEC 32bit waveform.
+ */
+static int nec_build_items(int channel, rmt_item32_t* item, int item_num, uint16_t addr, uint16_t cmd_data)
+{
+    int i = 0, j = 0;
+    if(item_num < NEC_DATA_ITEM_NUM) {
+        return -1;
+    }
+    nec_fill_item_header(item++);
+    i++;
+    for(j = 0; j < 16; j++) {
+        if(addr & 0x1) {
+            nec_fill_item_bit_one(item);
+        } else {
+            nec_fill_item_bit_zero(item);
+        }
+        item++;
+        i++;
+        addr >>= 1;
+    }
+    for(j = 0; j < 16; j++) {
+        if(cmd_data & 0x1) {
+            nec_fill_item_bit_one(item);
+        } else {
+            nec_fill_item_bit_zero(item);
+        }
+        item++;
+        i++;
+        cmd_data >>= 1;
+    }
+    nec_fill_item_end(item);
+    i++;
+    return i;
+}
+
+/*
+ * @brief RMT transmitter initialization
+ */
+static void nec_tx_init()
+{
+    rmt_config_t rmt_tx;
+    rmt_tx.channel = RMT_TX_CHANNEL;
+    rmt_tx.gpio_num = RMT_TX_GPIO_NUM;
+    rmt_tx.mem_block_num = 1;
+    rmt_tx.clk_div = RMT_CLK_DIV;
+    rmt_tx.tx_config.loop_en = false;
+    rmt_tx.tx_config.carrier_duty_percent = 50;
+    rmt_tx.tx_config.carrier_freq_hz = 38000;
+    rmt_tx.tx_config.carrier_level = 1;
+    rmt_tx.tx_config.carrier_en = RMT_TX_CARRIER_EN;
+    rmt_tx.tx_config.idle_level = 0;
+    rmt_tx.tx_config.idle_output_en = true;
+    rmt_tx.rmt_mode = 0;
+    rmt_config(&rmt_tx);
+    rmt_driver_install(rmt_tx.channel, 0, 0);
+}
+
+/*
+ * @brief RMT receiver initialization
+ */
+static void nec_rx_init()
+{
+    rmt_config_t rmt_rx;
+    rmt_rx.channel = RMT_RX_CHANNEL;
+    rmt_rx.gpio_num = RMT_RX_GPIO_NUM;
+    rmt_rx.clk_div = RMT_CLK_DIV;
+    rmt_rx.mem_block_num = 1;
+    rmt_rx.rmt_mode = RMT_MODE_RX;
+    rmt_rx.rx_config.filter_en = true;
+    rmt_rx.rx_config.filter_ticks_thresh = 100;
+    rmt_rx.rx_config.idle_threshold = rmt_item32_tIMEOUT_US / 10 * (RMT_TICK_10_US);
+    rmt_config(&rmt_rx);
+    rmt_driver_install(rmt_rx.channel, 1000, 0);
+}
+
+/**
+ * @brief RMT receiver demo, this task will print each received NEC data.
+ *
+ */
+static void rmt_example_nec_rx_task()
+{
+    int channel = RMT_RX_CHANNEL;
+    nec_rx_init();
+    RingbufHandle_t rb = NULL;
+    //get RMT RX ringbuffer
+    rmt_get_ringbuf_handler(channel, &rb);
+    rmt_rx_start(channel, 1);
+    while(rb) {
+        size_t rx_size = 0;
+        //try to receive data from ringbuffer.
+        //RMT driver will push all the data it receives to its ringbuffer.
+        //We just need to parse the value and return the spaces of ringbuffer.
+        rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 1000);
+        if(item) {
+            uint16_t rmt_addr;
+            uint16_t rmt_cmd;
+            int offset = 0;
+            while(1) {
+                //parse data value from ringbuffer.
+                int res = nec_parse_items(item + offset, rx_size / 4 - offset, &rmt_addr, &rmt_cmd);
+                if(res > 0) {
+                    offset += res + 1;
+                    ESP_LOGI(NEC_TAG, "RMT RCV --- addr: 0x%04x cmd: 0x%04x", rmt_addr, rmt_cmd);
+                } else {
+                    break;
+                }
+            }
+            //after parsing the data, return spaces to ringbuffer.
+            vRingbufferReturnItem(rb, (void*) item);
+        } else {
+            break;
+        }
+    }
+    vTaskDelete(NULL);
+}
+
+/**
+ * @brief RMT transmitter demo, this task will periodically send NEC data. (100 * 32 bits each time.)
+ *
+ */
+static void rmt_example_nec_tx_task()
+{
+    vTaskDelay(10);
+    nec_tx_init();
+    esp_log_level_set(NEC_TAG, ESP_LOG_INFO);
+    int channel = RMT_TX_CHANNEL;
+    uint16_t cmd = 0x0;
+    uint16_t addr = 0x11;
+    int nec_tx_num = RMT_TX_DATA_NUM;
+    for(;;) {
+        ESP_LOGI(NEC_TAG, "RMT TX DATA");
+        size_t size = (sizeof(rmt_item32_t) * NEC_DATA_ITEM_NUM * nec_tx_num);
+        //each item represent a cycle of waveform.
+        rmt_item32_t* item = (rmt_item32_t*) malloc(size);
+        int item_num = NEC_DATA_ITEM_NUM * nec_tx_num;
+        memset((void*) item, 0, size);
+        int i, offset = 0;
+        while(1) {
+            //To build a series of waveforms.
+            i = nec_build_items(channel, item + offset, item_num - offset, ((~addr) << 8) | addr, cmd);
+            if(i < 0) {
+                break;
+            }
+            cmd++;
+            addr++;
+            offset += i;
+        }
+        //To send data according to the waveform items.
+        rmt_write_items(channel, item, item_num, true);
+        //Wait until sending is done.
+        rmt_wait_tx_done(channel);
+        //before we free the data, make sure sending is already done.
+        free(item);
+        vTaskDelay(2000 / portTICK_PERIOD_MS);
+    }
+    vTaskDelete(NULL);
+}
 
 void app_main()
 {
-    xTaskCreate(rmt_nec_rx_task, "rmt_nec_rx_task", 2048, NULL, 10, NULL);
-    xTaskCreate(rmt_nec_tx_task, "rmt_nec_tx_task", 2048, NULL, 10, NULL);
+    xTaskCreate(rmt_example_nec_rx_task, "rmt_nec_rx_task", 2048, NULL, 10, NULL);
+    xTaskCreate(rmt_example_nec_tx_task, "rmt_nec_tx_task", 2048, NULL, 10, NULL);
 }

+ 2 - 2
examples/peripherals/sigmadelta/main/sigmadelta_test.c → examples/peripherals/sigmadelta/main/sigmadelta_example_main.c

@@ -20,7 +20,7 @@
 /**
  * @brief Sigma-delta initialization.
  */
-static void sigmadelta_init(void)
+static void sigmadelta_example_init(void)
 {
    sigmadelta_config_t sigmadelta_cfg = {
        /* Sigma-delta channel0*/
@@ -40,7 +40,7 @@ static void sigmadelta_init(void)
  */
 void app_main()
 {
-    sigmadelta_init();
+    sigmadelta_example_init();
     int8_t duty = 0;
     int inc = 1;
     while(1) {

+ 3 - 3
examples/peripherals/spi_master/main/spi_master.c → examples/peripherals/spi_master/main/spi_master_example_main.c

@@ -147,7 +147,7 @@ void ili_init(spi_device_handle_t spi)
 //before sending the line data itself; a total of 6 transactions. (We can't put all of this in just one transaction
 //because the D/C line needs to be toggled in the middle.)
 //This routine queues these commands up so they get sent as quickly as possible.
-void send_line(spi_device_handle_t spi, int ypos, uint16_t *line) 
+static void send_line(spi_device_handle_t spi, int ypos, uint16_t *line) 
 {
     esp_err_t ret;
     int x;
@@ -198,7 +198,7 @@ void send_line(spi_device_handle_t spi, int ypos, uint16_t *line)
 }
 
 
-void send_line_finish(spi_device_handle_t spi) 
+static void send_line_finish(spi_device_handle_t spi) 
 {
     spi_transaction_t *rtrans;
     esp_err_t ret;
@@ -214,7 +214,7 @@ void send_line_finish(spi_device_handle_t spi)
 //Simple routine to generate some patterns and send them to the LCD. Don't expect anything too
 //impressive. Because the SPI driver handles transactions in the background, we can calculate the next line 
 //while the previous one is being sent.
-void display_pretty_colors(spi_device_handle_t spi) 
+static void display_pretty_colors(spi_device_handle_t spi) 
 {
     uint16_t line[2][320];
     int x, y, frame=0;

+ 6 - 6
examples/peripherals/timer_group/main/timer_group.c → examples/peripherals/timer_group/main/timer_group_example_main.c

@@ -44,7 +44,7 @@ static void inline print_u64(uint64_t val)
     printf("0x%08x%08x\n", (uint32_t) (val >> 32), (uint32_t) (val));
 }
 
-void timer_evt_task(void *arg)
+static void timer_example_evt_task(void *arg)
 {
     while(1) {
         timer_event_t evt;
@@ -135,7 +135,7 @@ void IRAM_ATTR timer_group0_isr(void *para)
 /*
  * @brief timer group0 hardware timer0 init
  */
-void tg0_timer0_init()
+static void example_tg0_timer0_init()
 {
     int timer_group = TIMER_GROUP_0;
     int timer_idx = TIMER_0;
@@ -165,7 +165,7 @@ void tg0_timer0_init()
 /*
  * @brief timer group0 hardware timer1 init
  */
-void tg0_timer1_init()
+static void example_tg0_timer1_init()
 {
     int timer_group = TIMER_GROUP_0;
     int timer_idx = TIMER_1;
@@ -198,8 +198,8 @@ void tg0_timer1_init()
 void app_main()
 {
     timer_queue = xQueueCreate(10, sizeof(timer_event_t));
-    tg0_timer0_init();
-    tg0_timer1_init();
-    xTaskCreate(timer_evt_task, "timer_evt_task", 2048, NULL, 5, NULL);
+    example_tg0_timer0_init();
+    example_tg0_timer1_init();
+    xTaskCreate(timer_example_evt_task, "timer_evt_task", 2048, NULL, 5, NULL);
 }
 

+ 10 - 10
examples/peripherals/touch_pad_interrupt/main/tp_interrupt_main.c

@@ -17,7 +17,7 @@
 
 static const char* TAG = "Touch pad";
 
-static bool touch_pad_activated[TOUCH_PAD_MAX];
+static bool s_pad_activated[TOUCH_PAD_MAX];
 
 
 /*
@@ -29,7 +29,7 @@ static bool touch_pad_activated[TOUCH_PAD_MAX];
   Do not touch any pads when this routine
   is running (on application start).
  */
-static void touch_pad_set_thresholds(void)
+static void tp_example_set_thresholds(void)
 {
     uint16_t touch_value;
     for (int i=0; i<TOUCH_PAD_MAX; i++) {
@@ -44,17 +44,17 @@ static void touch_pad_set_thresholds(void)
   If so, then print it out on a serial monitor.
   Clear related entry in the table afterwards
  */
-static void touch_pad_read_task(void *pvParameter)
+static void tp_example_read_task(void *pvParameter)
 {
     static int show_message;
     while (1) {
         for (int i=0; i<TOUCH_PAD_MAX; i++) {
-            if (touch_pad_activated[i] == true) {
+            if (s_pad_activated[i] == true) {
                 ESP_LOGI(TAG, "T%d activated!", i);
                 // Wait a while for the pad being released
                 vTaskDelay(200 / portTICK_PERIOD_MS);
                 // Clear information on pad activation
-                touch_pad_activated[i] = false;
+                s_pad_activated[i] = false;
                 // Reset the counter triggering a message
                 // that application is running
                 show_message = 1;
@@ -73,7 +73,7 @@ static void touch_pad_read_task(void *pvParameter)
   Handle an interrupt triggered when a pad is touched.
   Recognize what pad has been touched and save it in a table.
  */
-static void touch_pad_rtc_intr(void * arg)
+static void tp_example_rtc_intr(void * arg)
 {
     uint32_t pad_intr = READ_PERI_REG(SENS_SAR_TOUCH_CTRL2_REG) & 0x3ff;
     uint32_t rtc_intr = READ_PERI_REG(RTC_CNTL_INT_ST_REG);
@@ -84,7 +84,7 @@ static void touch_pad_rtc_intr(void * arg)
     if (rtc_intr & RTC_CNTL_TOUCH_INT_ST) {
         for (int i = 0; i < TOUCH_PAD_MAX; i++) {
             if ((pad_intr >> i) & 0x01) {
-                touch_pad_activated[i] = true;
+                s_pad_activated[i] = true;
             }
         }
     }
@@ -96,9 +96,9 @@ void app_main()
     // Initialize touch pad peripheral
     ESP_LOGI(TAG, "Initializing touch pad");
     touch_pad_init();
-    touch_pad_set_thresholds();
-    touch_pad_isr_handler_register(touch_pad_rtc_intr, NULL, 0, NULL);
+    tp_example_set_thresholds();
+    touch_pad_isr_handler_register(tp_example_rtc_intr, NULL, 0, NULL);
 
     // Start a task to show what pads have been touched
-    xTaskCreate(&touch_pad_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
+    xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
 }

+ 2 - 2
examples/peripherals/touch_pad_read/main/tp_read_main.c

@@ -17,7 +17,7 @@
   Read values sensed at all available touch pads.
   Print out values in a loop on a serial monitor.
  */
-void touch_pad_read_task(void *pvParameter)
+static void tp_example_read_task(void *pvParameter)
 {
     while (1) {
         uint16_t touch_value;
@@ -36,6 +36,6 @@ void app_main()
     touch_pad_init();
 
     // Start task to read values sensed by pads
-    xTaskCreate(&touch_pad_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
+    xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
 
 }

+ 1 - 1
examples/peripherals/uart/Makefile → examples/peripherals/uart_echo/Makefile

@@ -3,7 +3,7 @@
 # project subdirectory.
 #
 
-PROJECT_NAME := uart
+PROJECT_NAME := uart_echo
 
 include $(IDF_PATH)/make/project.mk
 

+ 0 - 0
examples/peripherals/uart/main/component.mk → examples/peripherals/uart_echo/main/component.mk


+ 73 - 0
examples/peripherals/uart_echo/main/uart_echo_example_main.c

@@ -0,0 +1,73 @@
+/* Uart Events Example
+
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "esp_system.h"
+#include "nvs_flash.h"
+#include "driver/uart.h"
+#include "freertos/queue.h"
+#include "esp_log.h"
+#include "soc/uart_struct.h"
+
+/**
+ * This is a example exaple which echos any data it receives on UART1 back to the sender, with hardware flow control
+ * turned on. It does not use UART driver event queue.
+ *
+ * - port: UART1
+ * - rx buffer: on
+ * - tx buffer: off
+ * - flow control: on
+ * - event queue: off
+ * - pin assignment: txd(io4), rxd(io5), rts(18), cts(19)
+ */
+
+#define ECHO_TEST_TXD  (4)
+#define ECHO_TEST_RXD  (5)
+#define ECHO_TEST_RTS  (18)
+#define ECHO_TEST_CTS  (19)
+
+#define BUF_SIZE (1024)
+
+//an example of echo test with hardware flow control on UART1
+static void echo_task()
+{
+    const int uart_num = UART_NUM_1;
+    uart_config_t uart_config = {
+        .baud_rate = 115200,
+        .data_bits = UART_DATA_8_BITS,
+        .parity = UART_PARITY_DISABLE,
+        .stop_bits = UART_STOP_BITS_1,
+        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
+        .rx_flow_ctrl_thresh = 122,
+    };
+    //Configure UART1 parameters
+    uart_param_config(uart_num, &uart_config);
+    //Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
+    uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
+    //Install UART driver (we don't need an event queue here)
+    //In this example we don't even use a buffer for sending data.
+    uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
+
+    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
+    while(1) {
+        //Read data from UART
+        int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
+        //Write data back to UART
+        uart_write_bytes(uart_num, (const char*) data, len);
+    }
+}
+
+void app_main()
+{
+    //A uart read/write example without event queue;
+    xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
+}

+ 9 - 0
examples/peripherals/uart_events/Makefile

@@ -0,0 +1,9 @@
+#
+# This is a project Makefile. It is assumed the directory this Makefile resides in is a
+# project subdirectory.
+#
+
+PROJECT_NAME := uart_events
+
+include $(IDF_PATH)/make/project.mk
+

+ 3 - 0
examples/peripherals/uart_events/main/component.mk

@@ -0,0 +1,3 @@
+#
+# Main Makefile. This is basically the same as a component makefile.
+#

+ 25 - 72
examples/peripherals/uart/main/uart_test.c → examples/peripherals/uart_events/main/uart_events_example_main.c

@@ -17,46 +17,36 @@
 #include "freertos/queue.h"
 #include "esp_log.h"
 #include "soc/uart_struct.h"
-static const char *TAG = "uart_example";
+
+static const char *TAG = "uart_events";
 
 /**
- * Test code brief
- * This example shows how to configure uart settings and install uart driver.
+ * This example shows how to use the UART driver to handle special UART events.
+ *
+ * It also reads data from UART0 directly, and echoes it to console.
  *
- * uart_evt_test() is an example that read and write data on UART0, and handler some of the special events.
  * - port: UART0
  * - rx buffer: on
  * - tx buffer: on
  * - flow control: off
  * - event queue: on
  * - pin assignment: txd(default), rxd(default)
- *
- * uart_echo_test() is an example that read and write data on UART1, with hardware flow control turning on.
- * - port: UART1
- * - rx buffer: on
- * - tx buffer: off
- * - flow control: on
- * - event queue: off
- * - pin assignment: txd(io4), rxd(io5), rts(18), cts(19)
  */
 
+#define EX_UART_NUM UART_NUM_0
+
 #define BUF_SIZE (1024)
-#define ECHO_TEST_TXD  (4)
-#define ECHO_TEST_RXD  (5)
-#define ECHO_TEST_RTS  (18)
-#define ECHO_TEST_CTS  (19)
+static QueueHandle_t uart0_queue;
 
-QueueHandle_t uart0_queue;
-void uart_task(void *pvParameters)
+static void uart_event_task(void *pvParameters)
 {
-    int uart_num = (int) pvParameters;
     uart_event_t event;
     size_t buffered_size;
     uint8_t* dtmp = (uint8_t*) malloc(BUF_SIZE);
     for(;;) {
         //Waiting for UART event.
         if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
-            ESP_LOGI(TAG, "uart[%d] event:", uart_num);
+            ESP_LOGI(TAG, "uart[%d] event:", EX_UART_NUM);
             switch(event.type) {
                 //Event of UART receving data
                 /*We'd better handler data event fast, there would be much more data events than
@@ -64,7 +54,7 @@ void uart_task(void *pvParameters)
                 be full.
                 in this example, we don't process data in event, but read data outside.*/
                 case UART_DATA:
-                    uart_get_buffered_data_len(uart_num, &buffered_size);
+                    uart_get_buffered_data_len(EX_UART_NUM, &buffered_size);
                     ESP_LOGI(TAG, "data, len: %d; buffered len: %d", event.size, buffered_size);
                     break;
                 //Event of HW FIFO overflow detected
@@ -72,14 +62,14 @@ void uart_task(void *pvParameters)
                     ESP_LOGI(TAG, "hw fifo overflow\n");
                     //If fifo overflow happened, you should consider adding flow control for your application.
                     //We can read data out out the buffer, or directly flush the rx buffer.
-                    uart_flush(uart_num);
+                    uart_flush(EX_UART_NUM);
                     break;
                 //Event of UART ring buffer full
                 case UART_BUFFER_FULL:
                     ESP_LOGI(TAG, "ring buffer full\n");
                     //If buffer full happened, you should consider encreasing your buffer size
                     //We can read data out out the buffer, or directly flush the rx buffer.
-                    uart_flush(uart_num);
+                    uart_flush(EX_UART_NUM);
                     break;
                 //Event of UART RX break detected
                 case UART_BREAK:
@@ -109,9 +99,9 @@ void uart_task(void *pvParameters)
     vTaskDelete(NULL);
 }
 
-void uart_evt_test()
+/* Configure the UART events example */
+void app_main()
 {
-    int uart_num = UART_NUM_0;
     uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
@@ -121,63 +111,26 @@ void uart_evt_test()
        .rx_flow_ctrl_thresh = 122,
     };
     //Set UART parameters
-    uart_param_config(uart_num, &uart_config);
+    uart_param_config(EX_UART_NUM, &uart_config);
     //Set UART log level
     esp_log_level_set(TAG, ESP_LOG_INFO);
     //Install UART driver, and get the queue.
-    uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);
-    //Set UART pins,(-1: default pin, no change.)
-    //For UART0, we can just use the default pins.
-    //uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
+    uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);
+
+    //Set UART pins (using UART0 default pins ie no changes.)
+    uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
+
     //Set uart pattern detect function.
-    uart_enable_pattern_det_intr(uart_num, '+', 3, 10000, 10, 10);
+    uart_enable_pattern_det_intr(EX_UART_NUM, '+', 3, 10000, 10, 10);
     //Create a task to handler UART event from ISR
-    xTaskCreate(uart_task, "uart_task", 2048, (void*)uart_num, 12, NULL);
+    xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
     //process data
     uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
     do {
-        int len = uart_read_bytes(uart_num, data, BUF_SIZE, 100 / portTICK_RATE_MS);
+        int len = uart_read_bytes(EX_UART_NUM, data, BUF_SIZE, 100 / portTICK_RATE_MS);
         if(len > 0) {
             ESP_LOGI(TAG, "uart read : %d", len);
-            uart_write_bytes(uart_num, (const char*)data, len);
+            uart_write_bytes(EX_UART_NUM, (const char*)data, len);
         }
     } while(1);
 }
-
-//an example of echo test with hardware flow control on UART1
-void uart_echo_test()
-{
-    int uart_num = UART_NUM_1;
-    uart_config_t uart_config = {
-        .baud_rate = 115200,
-        .data_bits = UART_DATA_8_BITS,
-        .parity = UART_PARITY_DISABLE,
-        .stop_bits = UART_STOP_BITS_1,
-        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
-        .rx_flow_ctrl_thresh = 122,
-    };
-    //Configure UART1 parameters
-    uart_param_config(uart_num, &uart_config);
-    //Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
-    uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
-    //Install UART driver( We don't need an event queue here)
-    //In this example we don't even use a buffer for sending data.
-    uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
-
-    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
-    while(1) {
-        //Read data from UART
-        int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
-        //Write data back to UART
-        uart_write_bytes(uart_num, (const char*) data, len);
-    }
-}
-
-void app_main()
-{
-    //A uart read/write example without event queue;
-    xTaskCreate(uart_echo_test, "uart_echo_test", 1024, NULL, 10, NULL);
-
-    //A uart example with event queue.
-    uart_evt_test();
-}

+ 2 - 2
examples/protocols/coap_client/main/coap_client.c → examples/protocols/coap_client/main/coap_client_example_main.c

@@ -65,7 +65,7 @@ static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *l
     }
 }
 
-static void coap_demo_thread(void *p)
+static void coap_example_task(void *p)
 {
     struct hostent *hp;
     struct ip4_addr *ip4_addr;
@@ -201,5 +201,5 @@ void app_main(void)
 {
     ESP_ERROR_CHECK( nvs_flash_init() );
     wifi_conn_init();
-    xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
+    xTaskCreate(coap_example_task, "coap", 2048, NULL, 5, NULL);
 }

+ 2 - 2
examples/protocols/coap_server/main/coap_server.c → examples/protocols/coap_server/main/coap_server_example_main.c

@@ -80,7 +80,7 @@ async_handler(coap_context_t *ctx, struct coap_resource_t *resource,
     async = coap_register_async(ctx, peer, request, COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM, (void*)"no data");
 }
 
-static void coap_demo_thread(void *p)
+static void coap_example_thread(void *p)
 {
     coap_context_t*  ctx = NULL;
     coap_address_t   serv_addr;
@@ -188,5 +188,5 @@ void app_main(void)
     ESP_ERROR_CHECK( nvs_flash_init() );
     wifi_conn_init();
 
-    xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
+    xTaskCreate(coap_example_thread, "coap", 2048, NULL, 5, NULL);
 }

+ 0 - 0
examples/protocols/http_request/main/http_request_main.c → examples/protocols/http_request/main/http_request_example_main.c


+ 0 - 0
examples/protocols/https_request/main/https_request_main.c → examples/protocols/https_request/main/https_request_example_main.c


+ 2 - 2
examples/protocols/mdns/main/mdns_example_main.c

@@ -125,7 +125,7 @@ static void query_mdns_service(mdns_server_t * mdns, const char * service, const
     }
 }
 
-static void mdns_task(void *pvParameters)
+static void mdns_example_task(void *pvParameters)
 {
     mdns_server_t * mdns = NULL;
     while(1) {
@@ -180,5 +180,5 @@ void app_main()
 {
     ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
-    xTaskCreate(&mdns_task, "mdns_task", 2048, NULL, 5, NULL);
+    xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
 }

+ 12 - 12
examples/protocols/openssl_client/main/openssl_client.h → examples/protocols/openssl_client/main/openssl_client_example.h

@@ -7,8 +7,8 @@
    CONDITIONS OF ANY KIND, either express or implied.
 */
 
-#ifndef _OPENSSL_DEMO_H_
-#define _OPENSSL_DEMO_H_
+#ifndef _OPENSSL_EXAMPLE_H_
+#define _OPENSSL_EXAMPLE_H_
 
 /* The examples use simple WiFi configuration that you can set via
    'make menuconfig'.
@@ -23,21 +23,21 @@
    you can set via 'make menuconfig'.
 
    If you'd rather not, just change the below entries to strings with
-   the config you want - ie #define OPENSSL_DEMO_TARGET_NAME "www.baidu.com"
-   and ie #define OPENSSL_DEMO_TARGET_TCP_PORT 433 
+   the config you want - ie #define OPENSSL_EXAMPLE_TARGET_NAME "www.baidu.com"
+   and ie #define OPENSSL_EXAMPLE_TARGET_TCP_PORT 433 
 */
-#define OPENSSL_DEMO_TARGET_NAME        CONFIG_TARGET_DOMAIN
-#define OPENSSL_DEMO_TARGET_TCP_PORT    CONFIG_TARGET_PORT_NUMBER
+#define OPENSSL_EXAMPLE_TARGET_NAME        CONFIG_TARGET_DOMAIN
+#define OPENSSL_EXAMPLE_TARGET_TCP_PORT    CONFIG_TARGET_PORT_NUMBER
 
-#define OPENSSL_DEMO_REQUEST            "{\"path\": \"/v1/ping/\", \"method\": \"GET\"}\r\n"
+#define OPENSSL_EXAMPLE_REQUEST            "{\"path\": \"/v1/ping/\", \"method\": \"GET\"}\r\n"
 
-#define OPENSSL_DEMO_THREAD_NAME        "OpenSSL_demo"
-#define OPENSSL_DEMO_THREAD_STACK_WORDS 10240
-#define OPENSSL_DEMO_THREAD_PRORIOTY    8
+#define OPENSSL_EXAMPLE_TASK_NAME        "openssl_example"
+#define OPENSSL_EXAMPLE_TASK_STACK_WORDS 10240
+#define OPENSSL_EXAMPLE_TASK_PRORIOTY    8
 
-#define OPENSSL_DEMO_RECV_BUF_LEN       1024
+#define OPENSSL_EXAMPLE_RECV_BUF_LEN       1024
 
-#define OPENSSL_DEMO_LOCAL_TCP_PORT     443
+#define OPENSSL_EXAMPLE_LOCAL_TCP_PORT     443
 
 #endif
 

+ 21 - 21
examples/protocols/openssl_client/main/openssl_client.c → examples/protocols/openssl_client/main/openssl_client_example_main.c

@@ -7,7 +7,7 @@
    CONDITIONS OF ANY KIND, either express or implied.
 */
 
-#include "openssl_client.h"
+#include "openssl_client_example.h"
 
 #include <string.h>
 
@@ -33,9 +33,9 @@ static EventGroupHandle_t wifi_event_group;
    to the AP with an IP? */
 const static int CONNECTED_BIT = BIT0;
 
-const static char *TAG = "Openssl_demo";
+const static char *TAG = "openssl_example";
 
-void openssl_demo_thread(void *p)
+static void openssl_example_task(void *p)
 {
     int ret;
     SSL_CTX *ctx;
@@ -46,15 +46,15 @@ void openssl_demo_thread(void *p)
     struct ip4_addr *ip4_addr;
     
     int recv_bytes = 0;
-    char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN];
+    char recv_buf[OPENSSL_EXAMPLE_RECV_BUF_LEN];
     
-    const char send_data[] = OPENSSL_DEMO_REQUEST;
+    const char send_data[] = OPENSSL_EXAMPLE_REQUEST;
     const int send_bytes = sizeof(send_data);
 
     ESP_LOGI(TAG, "OpenSSL demo thread start OK");
 
     ESP_LOGI(TAG, "get target IP address");
-    hp = gethostbyname(OPENSSL_DEMO_TARGET_NAME);
+    hp = gethostbyname(OPENSSL_EXAMPLE_TARGET_NAME);
     if (!hp) {
         ESP_LOGI(TAG, "failed");
         goto failed1;
@@ -84,7 +84,7 @@ void openssl_demo_thread(void *p)
     memset(&sock_addr, 0, sizeof(sock_addr));
     sock_addr.sin_family = AF_INET;
     sock_addr.sin_addr.s_addr = 0;
-    sock_addr.sin_port = htons(OPENSSL_DEMO_LOCAL_TCP_PORT);
+    sock_addr.sin_port = htons(OPENSSL_EXAMPLE_LOCAL_TCP_PORT);
     ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
     if (ret) {
         ESP_LOGI(TAG, "failed");
@@ -92,11 +92,11 @@ void openssl_demo_thread(void *p)
     }
     ESP_LOGI(TAG, "OK");
 
-    ESP_LOGI(TAG, "socket connect to remote %s ......", OPENSSL_DEMO_TARGET_NAME);
+    ESP_LOGI(TAG, "socket connect to remote %s ......", OPENSSL_EXAMPLE_TARGET_NAME);
     memset(&sock_addr, 0, sizeof(sock_addr));
     sock_addr.sin_family = AF_INET;
     sock_addr.sin_addr.s_addr = ip4_addr->addr;
-    sock_addr.sin_port = htons(OPENSSL_DEMO_TARGET_TCP_PORT);
+    sock_addr.sin_port = htons(OPENSSL_EXAMPLE_TARGET_TCP_PORT);
     ret = connect(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
     if (ret) {
         ESP_LOGI(TAG, "failed");
@@ -115,7 +115,7 @@ void openssl_demo_thread(void *p)
     SSL_set_fd(ssl, socket);
 
     ESP_LOGI(TAG, "SSL connected to %s port %d ......",
-        OPENSSL_DEMO_TARGET_NAME, OPENSSL_DEMO_TARGET_TCP_PORT);
+        OPENSSL_EXAMPLE_TARGET_NAME, OPENSSL_EXAMPLE_TARGET_TCP_PORT);
     ret = SSL_connect(ssl);
     if (!ret) {
         ESP_LOGI(TAG, "failed " );
@@ -124,7 +124,7 @@ void openssl_demo_thread(void *p)
     ESP_LOGI(TAG, "OK");
 
     ESP_LOGI(TAG, "send https request to %s port %d ......",
-        OPENSSL_DEMO_TARGET_NAME, OPENSSL_DEMO_TARGET_TCP_PORT);
+        OPENSSL_EXAMPLE_TARGET_NAME, OPENSSL_EXAMPLE_TARGET_TCP_PORT);
     ret = SSL_write(ssl, send_data, send_bytes);
     if (ret <= 0) {
         ESP_LOGI(TAG, "failed");
@@ -133,7 +133,7 @@ void openssl_demo_thread(void *p)
     ESP_LOGI(TAG, "OK");
 
     do {
-        ret = SSL_read(ssl, recv_buf, OPENSSL_DEMO_RECV_BUF_LEN - 1);
+        ret = SSL_read(ssl, recv_buf, OPENSSL_EXAMPLE_RECV_BUF_LEN - 1);
         if (ret <= 0) {
             break;
         }
@@ -141,7 +141,7 @@ void openssl_demo_thread(void *p)
         ESP_LOGI(TAG, "%s", recv_buf);
     } while (1);
     
-    ESP_LOGI(TAG, "totaly read %d bytes data from %s ......", recv_bytes, OPENSSL_DEMO_TARGET_NAME);
+    ESP_LOGI(TAG, "totaly read %d bytes data from %s ......", recv_bytes, OPENSSL_EXAMPLE_TARGET_NAME);
 
 failed5:
     SSL_shutdown(ssl);
@@ -159,20 +159,20 @@ failed1:
     return ;
 }
 
-static void openssl_client_init(void)
+static void openssl_example_client_init(void)
 {
     int ret;
     xTaskHandle openssl_handle;
 
-    ret = xTaskCreate(openssl_demo_thread,
-                      OPENSSL_DEMO_THREAD_NAME,
-                      OPENSSL_DEMO_THREAD_STACK_WORDS,
+    ret = xTaskCreate(openssl_example_task,
+                      OPENSSL_EXAMPLE_TASK_NAME,
+                      OPENSSL_EXAMPLE_TASK_STACK_WORDS,
                       NULL,
-                      OPENSSL_DEMO_THREAD_PRORIOTY,
-                      &openssl_handle); 
+                      OPENSSL_EXAMPLE_TASK_PRORIOTY,
+                      &openssl_handle);
 
     if (ret != pdPASS)  {
-        ESP_LOGI(TAG, "create thread %s failed", OPENSSL_DEMO_THREAD_NAME);
+        ESP_LOGI(TAG, "create thread %s failed", OPENSSL_EXAMPLE_TASK_NAME);
     }
 }
 
@@ -184,7 +184,7 @@ static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
         break;
     case SYSTEM_EVENT_STA_GOT_IP:
         xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
-        openssl_client_init();
+        openssl_example_client_init();
         break;
     case SYSTEM_EVENT_STA_DISCONNECTED:
         /* This is a workaround as ESP32 WiFi libs don't currently

+ 5 - 5
examples/protocols/openssl_server/main/openssl_server.h → examples/protocols/openssl_server/main/openssl_server_example.h

@@ -21,13 +21,13 @@
 #define EXAMPLE_WIFI_SSID               CONFIG_WIFI_SSID
 #define EXAMPLE_WIFI_PASS               CONFIG_WIFI_PASSWORD
 
-#define OPENSSL_DEMO_THREAD_NAME        "OpenSSL_demo"
-#define OPENSSL_DEMO_THREAD_STACK_WORDS 10240
-#define OPENSSL_DEMO_THREAD_PRORIOTY    8
+#define OPENSSL_EXAMPLE_TASK_NAME        "openssl_example"
+#define OPENSSL_EXAMPLE_TASK_STACK_WORDS 10240
+#define OPENSSL_EXAMPLE_TASK_PRORIOTY    8
 
-#define OPENSSL_DEMO_RECV_BUF_LEN       1024
+#define OPENSSL_EXAMPLE_RECV_BUF_LEN       1024
 
-#define OPENSSL_DEMO_LOCAL_TCP_PORT     443
+#define OPENSSL_EXAMPLE_LOCAL_TCP_PORT     443
 
 #endif
 

+ 16 - 16
examples/protocols/openssl_server/main/openssl_server.c → examples/protocols/openssl_server/main/openssl_server_example_main.c

@@ -7,7 +7,7 @@
    CONDITIONS OF ANY KIND, either express or implied.
 */
 
-#include "openssl_server.h"
+#include "openssl_server_example.h"
 
 #include <string.h>
 
@@ -33,20 +33,20 @@ static EventGroupHandle_t wifi_event_group;
    to the AP with an IP? */
 const static int CONNECTED_BIT = BIT0;
 
-const static char *TAG = "Openssl_demo";
+const static char *TAG = "Openssl_example";
 
-#define OPENSSL_DEMO_SERVER_ACK "HTTP/1.1 200 OK\r\n" \
+#define OPENSSL_EXAMPLE_SERVER_ACK "HTTP/1.1 200 OK\r\n" \
                                 "Content-Type: text/html\r\n" \
                                 "Content-Length: 98\r\n\r\n" \
                                 "<html>\r\n" \
                                 "<head>\r\n" \
-                                "<title>OpenSSL demo</title></head><body>\r\n" \
-                                "OpenSSL server demo!\r\n" \
+                                "<title>OpenSSL example</title></head><body>\r\n" \
+                                "OpenSSL server example!\r\n" \
                                 "</body>\r\n" \
                                 "</html>\r\n" \
                                 "\r\n"
 
-static void openssl_demo_thread(void *p)
+static void openssl_example_task(void *p)
 {
     int ret;
 
@@ -57,9 +57,9 @@ static void openssl_demo_thread(void *p)
     socklen_t addr_len;
     struct sockaddr_in sock_addr;
 
-    char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN];
+    char recv_buf[OPENSSL_EXAMPLE_RECV_BUF_LEN];
 
-    const char send_data[] = OPENSSL_DEMO_SERVER_ACK;
+    const char send_data[] = OPENSSL_EXAMPLE_SERVER_ACK;
     const int send_bytes = sizeof(send_data);
 
     extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
@@ -110,7 +110,7 @@ static void openssl_demo_thread(void *p)
     memset(&sock_addr, 0, sizeof(sock_addr));
     sock_addr.sin_family = AF_INET;
     sock_addr.sin_addr.s_addr = 0;
-    sock_addr.sin_port = htons(OPENSSL_DEMO_LOCAL_TCP_PORT);
+    sock_addr.sin_port = htons(OPENSSL_EXAMPLE_LOCAL_TCP_PORT);
     ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
     if (ret) {
         ESP_LOGI(TAG, "failed");
@@ -155,8 +155,8 @@ reconnect:
 
     ESP_LOGI(TAG, "SSL server read message ......");
     do {
-        memset(recv_buf, 0, OPENSSL_DEMO_RECV_BUF_LEN);
-        ret = SSL_read(ssl, recv_buf, OPENSSL_DEMO_RECV_BUF_LEN - 1);
+        memset(recv_buf, 0, OPENSSL_EXAMPLE_RECV_BUF_LEN);
+        ret = SSL_read(ssl, recv_buf, OPENSSL_EXAMPLE_RECV_BUF_LEN - 1);
         if (ret <= 0) {
             break;
         }
@@ -199,15 +199,15 @@ static void openssl_client_init(void)
     int ret;
     xTaskHandle openssl_handle;
 
-    ret = xTaskCreate(openssl_demo_thread,
-                      OPENSSL_DEMO_THREAD_NAME,
-                      OPENSSL_DEMO_THREAD_STACK_WORDS,
+    ret = xTaskCreate(openssl_example_task,
+                      OPENSSL_EXAMPLE_TASK_NAME,
+                      OPENSSL_EXAMPLE_TASK_STACK_WORDS,
                       NULL,
-                      OPENSSL_DEMO_THREAD_PRORIOTY,
+                      OPENSSL_EXAMPLE_TASK_PRORIOTY,
                       &openssl_handle); 
 
     if (ret != pdPASS)  {
-        ESP_LOGI(TAG, "create thread %s failed", OPENSSL_DEMO_THREAD_NAME);
+        ESP_LOGI(TAG, "create task %s failed", OPENSSL_EXAMPLE_TASK_NAME);
     }
 }
 

+ 0 - 0
examples/protocols/sntp/main/sntp_main.c → examples/protocols/sntp/main/sntp_example_main.c


+ 0 - 0
examples/storage/nvs_rw_blob/main/nvs_rw_blob.c → examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c


+ 0 - 0
examples/storage/nvs_rw_value/main/nvs_rw_value.c → examples/storage/nvs_rw_value/main/nvs_value_example_main.c


+ 0 - 0
examples/storage/sd_card/main/sd_card.c → examples/storage/sd_card/main/sd_card_example_main.c


+ 0 - 0
examples/system/deep_sleep/main/deep_sleep_wakeup.c → examples/system/deep_sleep/main/deep_sleep_example_main.c


+ 4 - 4
examples/system/ota/main/ota_example.c → examples/system/ota/main/ota_example_main.c

@@ -135,7 +135,7 @@ static bool read_past_http_header(char text[], int total_len, esp_ota_handle_t u
     return false;
 }
 
-bool connect_to_http_server()
+static bool connect_to_http_server()
 {
     ESP_LOGI(TAG, "Server IP: %s Server Port:%s", EXAMPLE_SERVER_IP, EXAMPLE_SERVER_PORT);
     sprintf(http_request, "GET %s HTTP/1.1\r\nHost: %s:%s \r\n\r\n", EXAMPLE_FILENAME, EXAMPLE_SERVER_IP, EXAMPLE_SERVER_PORT);
@@ -168,7 +168,7 @@ bool connect_to_http_server()
     return false;
 }
 
-void __attribute__((noreturn)) task_fatal_error()
+static void __attribute__((noreturn)) task_fatal_error()
 {
     ESP_LOGE(TAG, "Exiting task due to fatal error...");
     close(socket_id);
@@ -179,7 +179,7 @@ void __attribute__((noreturn)) task_fatal_error()
     }
 }
 
-void main_task(void *pvParameter)
+static void ota_example_task(void *pvParameter)
 {
     esp_err_t err;
     /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
@@ -295,5 +295,5 @@ void app_main()
     ESP_ERROR_CHECK( err );
 
     initialise_wifi();
-    xTaskCreate(&main_task, "main_task", 8192, NULL, 5, NULL);
+    xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
 }

+ 2 - 2
examples/wifi/wpa2_enterprise/main/wpa2_enterprise_main.c

@@ -127,7 +127,7 @@ static void initialise_wifi(void)
     ESP_ERROR_CHECK( esp_wifi_start() );
 }
 
-static void wpa2_enterprise_task(void *pvParameters)
+static void wpa2_enterprise_example_task(void *pvParameters)
 {
     tcpip_adapter_ip_info_t ip;
     memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
@@ -150,5 +150,5 @@ void app_main()
 {
     ESP_ERROR_CHECK( nvs_flash_init() );
     initialise_wifi();
-    xTaskCreate(&wpa2_enterprise_task, "wpa2_enterprise_task", 4096, NULL, 5, NULL);
+    xTaskCreate(&wpa2_enterprise_example_task, "wpa2_enterprise_example_task", 4096, NULL, 5, NULL);
 }