添加智能灯固件代码

This commit is contained in:
kerwincui
2021-07-13 17:14:51 +08:00
parent 332f74dd17
commit ecc0b91b8b
2568 changed files with 229441 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(base_mac_address)

View File

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

View File

@@ -0,0 +1,93 @@
# Base MAC Address
(See the README.md file in the upper level 'examples' directory for more information about examples.)
Each network interface on the ESP32 (e.g., WiFi Station/AP, BT, Ethernet) must be assigned a unique Medium Access Control (MAC) address. Each interface's MAC address is [derived from a base MAC address](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system.html#mac-address) that must be set before any network interface's initialization is called.
This example demonstrates the following:
1. How to retrieve a base MAC address from non-volatile memory
2. How to set the base MAC address
3. How to obtain the derived MAC addresses of each network interface
This example utilizes the [MAC Address API](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system.html#mac-address).
## How to use example
### Hardware Required
This example should be able to run on any commonly available ESP32 development board. However, if an external source of non-volatile memory is used to store the base MAC address (e.g. an I2C Serial EEPROM), the user should wire the connections to their chosen external storage as needed.
### Configure the project
```
idf.py menuconfig
```
* To select the storage source of the base MAC address, go to `Example Configuration > Storage location of the base MAC address` where the following options are available:
* `eFuse BLK0` will cause this example to read the base MAC address from eFuse Block 0 and is selected by default. The `eFuse BLK0` MAC address is factory programmed into each ESP32.
* `eFuse BLK3` will cause this example to read the base MAC address from words 1 & 2 of eFuse Block 3. `eFuse BLK3` allows users to use a custom eFuse, but must be burned into the ESP32 using the [espefuse tool](https://github.com/espressif/esptool/wiki/espefuse). Attempting to read `eFuse BLK3` without burning the eFuse will result in an error.
* `Other external storage` will call a `external_storage_mac_get()` which will merely retrieve an arbitrary base MAC address that is set in software. Users should re-implement `external_storage_mac_get()` to access their chosen external storage (e.g. an I2C Serial EEPROM)
* If `eFuse BLK3` is chosen, the `Behavior when retrieving BLK3 eFuse fails` option may also be selected. When retrieving the base MAC address from `eFuse BLK3` without burning eFuse Block 3, the example can either abort or default to retrieving from eFuse Block 0.
### Build and Flash
Build the project and flash it to the board, then run monitor tool to view serial output:
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
Depending on the `Storage of the base MAC address` configuration option, the console should output one of the following logs to indicate the source of the base MAC address.
```
I (288) BASE_MAC: Base MAC Address read from EFUSE BLK0
...
I (288) BASE_MAC: Base MAC Address read from EFUSE BLK3
...
I (288) BASE_MAC: Base MAC Address read from external storage
```
The console will also output the retrieved 6 byte MAC address when it sets it as the system wide base MAC address.
```
I (298) BASE_MAC: Using "0xd8, 0xa0, 0x1d, 0x0, 0xb, 0x88" as base MAC address
```
The console will then output the derived MAC address of each network interface as such:
```
I (297) WIFI_STA MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x55
I (307) SoftAP MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x56
I (317) BT MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x57
I (317) Ethernet MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x58
```
## Troubleshooting
When attempting to retrieve the base MAC address from `eFuse BLK3` without first burning eFuse Block 3 will result in one of the following situations.
If the example has been configured to abort when retrieving from `eFuse BLK3` fails.
```
E (288) system_api: Base MAC address from BLK3 of EFUSE version error, version = 0
E (298) BASE_MAC: Failed to get base MAC address from EFUSE BLK3. (ESP_ERR_INVALID_VERSION)
E (308) BASE_MAC: Aborting
abort() was called at PC 0x400d237b on core 0
```
If not configured to abort, the example will default to `eFuse BLK0` and output the following instead.
```
E (288) system_api: Base MAC address from BLK3 of EFUSE version error, version = 0
E (298) BASE_MAC: Failed to get base MAC address from EFUSE BLK3. (ESP_ERR_INVALID_VERSION)
I (308) BASE_MAC: Defaulting to base MAC address in BLK0 of EFUSE
I (308) BASE_MAC: Base MAC Address read from EFUSE BLK0
```

View File

@@ -0,0 +1,33 @@
from __future__ import unicode_literals
from tiny_test_fw import Utility
import re
import ttfw_idf
@ttfw_idf.idf_example_test(env_tag='Example_GENERIC')
def test_examples_base_mac_address(env, extra_data):
dut = env.get_dut('base_mac_address', 'examples/system/base_mac_address')
dut.start_app()
dut.expect('BASE_MAC: Base MAC Address read from EFUSE BLK0', timeout=30)
hex_r = r', '.join((r'0x([0-9a-f]{1,2})',) * 6)
mac_m = dut.expect(re.compile(r'BASE_MAC: Using "' + hex_r + r'" as base MAC address'), timeout=5)
Utility.console_log('BASE_MAC detected: {}'.format(':'.join(mac_m)))
def get_expected_mac_string(increment):
'''
Return the string representation of the MAC address mac_m with the last octet incremented.
mac_m is an array of strings in hexa-decimal format without the '0x' prefix.
'''
return ', '.join(['0x{}'.format(m) for m in mac_m[:-1]] + [hex(int(mac_m[-1], 16) + increment)])
dut.expect_all('WIFI_STA MAC: ' + get_expected_mac_string(0),
'SoftAP MAC: ' + get_expected_mac_string(1),
'BT MAC: ' + get_expected_mac_string(2),
'Ethernet MAC: ' + get_expected_mac_string(3),
timeout=10)
if __name__ == '__main__':
test_examples_base_mac_address()

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "base_mac_address_example_main.c"
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,46 @@
menu "Example Configuration"
choice BASE_MAC_ADDRESS_STORAGE
prompt "Storage location of the base MAC address"
default BASE_MAC_STORED_EFUSE_BLK0
help
Select the storage location of the base MAC addresses.
1. eFuse BLK0: The "Default (Espressif factory)" selection. The
default base MAC address is written to words 1 and 2 of eFuse block
0 when the chip was manufactured. Call esp_efuse_mac_get_default()
read retrieve the "eFuse BLK0" MAC address.
2. eFuse BLK3: A custom base MAC address is burned by the user into
eFuse word 0 of block 3. Call esp_efuse_mac_get_custom() to read
the "eFuse BLK3" MAC address.
3. Other External Storage: Selecting this option will cause the
example to call external_storage_mac_get() which is defined in this
example to simply return a MAC address preset in software. Users
should modify this function to access their desired storage mediums
(e.g. flash, EEPROM etc).
config BASE_MAC_STORED_EFUSE_BLK0
bool "Default (Espressif factory) eFuse BLK0"
config BASE_MAC_STORED_EFUSE_BLK3
bool "Custom eFuse BLK3"
config BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE
bool "Other external storage"
endchoice
choice BASE_MAC_STORED_EFUSE_BLK3_ERROR_BEHAV
prompt "Behavior when retrieving eFuse BLK3 fails"
depends on BASE_MAC_STORED_EFUSE_BLK3
default BASE_MAC_STORED_EFUSE_BLK3_ERROR_DEFAULT
help
Select the behavior when reading base MAC address "eFuse BLK3" fails
(i.e. the retrieved result is all 0).
- If "Abort" is selected, the ESP32 will abort.
- If "Use the default base MAC address from BLK0 of eFuse" is
selected, the default "eFuse BLK0" will be used instead.
config BASE_MAC_STORED_EFUSE_BLK3_ERROR_ABORT
bool "Abort"
config BASE_MAC_STORED_EFUSE_BLK3_ERROR_DEFAULT
bool "Use the default base MAC address eFuse BLK0"
endchoice
endmenu

View File

@@ -0,0 +1,108 @@
/* Base mac address 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 <stdlib.h>
#include <string.h>
#include "esp_log.h"
#include "esp_system.h"
#define TAG "BASE_MAC"
#ifdef CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE
static esp_err_t external_storage_mac_get(uint8_t *mac)
{
/* This function simulates getting a base MAC address from external storage
* by simply setting the base MAC to an arbitrary address. Users should
* re-implement this function to access external storage (e.g. flash, EEPROM) */
uint8_t external_storage_mac_addr[6] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
if (mac == NULL) {
ESP_LOGE(TAG, "The mac parameter is NULL");
abort();
}
memcpy(mac, external_storage_mac_addr, 6);
return ESP_OK;
}
#endif//CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE
void app_main(void)
{
//Get the base MAC address from different sources
uint8_t base_mac_addr[6] = {0};
esp_err_t ret = ESP_OK;
#ifdef CONFIG_BASE_MAC_STORED_EFUSE_BLK3
//Get base MAC address from EFUSE BLK3
ret = esp_efuse_mac_get_custom(base_mac_addr);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to get base MAC address from EFUSE BLK3. (%s)", esp_err_to_name(ret));
#ifdef CONFIG_BASE_MAC_STORED_EFUSE_BLK3_ERROR_ABORT
ESP_LOGE(TAG, "Aborting");
abort();
#else
ESP_LOGI(TAG, "Defaulting to base MAC address in BLK0 of EFUSE");
esp_efuse_mac_get_default(base_mac_addr);
ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK0");
#endif//CONFIG_BASE_MAC_STORED_EFUSE_BLK3_ERROR_ABORT
} else {
ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK3");
}
#elif defined(CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE)
//Get base MAC address from other external storage, or set by software
ret = external_storage_mac_get(base_mac_addr);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to get base MAC address from external storage. (%s)", esp_err_to_name(ret));
ESP_LOGE(TAG, "Aborting");
abort();
} else {
ESP_LOGI(TAG, "Base MAC Address read from external storage");
}
#else
//Get base MAC address from EFUSE BLK0(default option)
ret = esp_efuse_mac_get_default(base_mac_addr);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to get base MAC address from EFUSE BLK0. (%s)", esp_err_to_name(ret));
ESP_LOGE(TAG, "Aborting");
abort();
} else {
ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK0");
}
#endif
//Set the base MAC address using the retrieved MAC address
ESP_LOGI(TAG, "Using \"0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\" as base MAC address",
base_mac_addr[0], base_mac_addr[1], base_mac_addr[2], base_mac_addr[3], base_mac_addr[4], base_mac_addr[5]);
esp_base_mac_addr_set(base_mac_addr);
//Get the derived MAC address for each network interface
uint8_t derived_mac_addr[6] = {0};
//Get MAC address for WiFi Station interface
ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_WIFI_STA));
ESP_LOGI("WIFI_STA MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2],
derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]);
//Get MAC address for SoftAp interface
ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_WIFI_SOFTAP));
ESP_LOGI("SoftAP MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2],
derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]);
//Get MAC address for Bluetooth
ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_BT));
ESP_LOGI("BT MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2],
derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]);
//Get MAC address for Ethernet
ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_ETH));
ESP_LOGI("Ethernet MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2],
derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]);
}

View File

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