添加智能灯固件代码

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,4 @@
idf_component_register(SRCS "efuse_main.c"
"esp_efuse_custom_table.c"
INCLUDE_DIRS "."
"include")

View File

@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

View File

@@ -0,0 +1,155 @@
/* efuse 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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_efuse.h"
#include "esp_efuse_table.h"
#include "esp_efuse_custom_table.h"
#include "sdkconfig.h"
static const char* TAG = "example";
typedef struct {
uint8_t module_version; /*!< Module version: length 8 bits */
uint8_t device_role; /*!< Device role: length 3 bits */
uint8_t setting_1; /*!< Setting 1: length 6 bits */
uint8_t setting_2; /*!< Setting 2: length 5 bits */
size_t custom_secure_version; /*!< Custom secure version: length 16 bits */
uint16_t reserv; /*!< Reserv */
} device_desc_t;
static void print_device_desc(device_desc_t *desc)
{
ESP_LOGI(TAG, "module_version = %d", desc->module_version);
if (desc->device_role == 0) {
ESP_LOGI(TAG, "device_role = None");
} else if (desc->device_role == 1) {
ESP_LOGI(TAG, "device_role = Master");
} else if (desc->device_role == 2) {
ESP_LOGI(TAG, "device_role = Slave");
} else {
ESP_LOGI(TAG, "device_role = Not supported");
}
ESP_LOGI(TAG, "setting_1 = %d", desc->setting_1);
ESP_LOGI(TAG, "setting_2 = %d", desc->setting_2);
ESP_LOGI(TAG, "custom_secure_version = %d", desc->custom_secure_version);
}
static void read_device_desc_efuse_fields(device_desc_t *desc)
{
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_MODULE_VERSION, &desc->module_version, 8));
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_DEVICE_ROLE, &desc->device_role, 3));
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_SETTING_1, &desc->setting_1, 6));
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_SETTING_2, &desc->setting_2, 5));
ESP_ERROR_CHECK(esp_efuse_read_field_cnt(ESP_EFUSE_CUSTOM_SECURE_VERSION, &desc->custom_secure_version));
print_device_desc(desc);
}
static void read_efuse_fields(device_desc_t *desc)
{
ESP_LOGI(TAG, "read efuse fields");
uint8_t mac[6];
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, &mac, sizeof(mac) * 8));
ESP_LOGI(TAG, "1. read MAC address: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
size_t secure_version = 0;
ESP_ERROR_CHECK(esp_efuse_read_field_cnt(ESP_EFUSE_SECURE_VERSION, &secure_version));
ESP_LOGI(TAG, "2. read secure_version: %d", secure_version);
ESP_LOGI(TAG, "3. read custom fields");
read_device_desc_efuse_fields(desc);
}
static void write_efuse_fields(device_desc_t *desc, esp_efuse_coding_scheme_t coding_scheme)
{
#ifdef CONFIG_EFUSE_VIRTUAL
#if CONFIG_IDF_TARGET_ESP32
const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_3_4;
#else
const esp_efuse_coding_scheme_t coding_scheme_for_batch_mode = EFUSE_CODING_SCHEME_RS;
#endif
ESP_LOGI(TAG, "write custom efuse fields");
if (coding_scheme == coding_scheme_for_batch_mode) {
ESP_LOGI(TAG, "In the case of 3/4 or RS coding scheme, you cannot write efuse fields separately");
ESP_LOGI(TAG, "You should use the batch mode of writing fields for this");
ESP_ERROR_CHECK(esp_efuse_batch_write_begin());
}
ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_MODULE_VERSION, &desc->module_version, 8));
ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_DEVICE_ROLE, &desc->device_role, 3));
ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_SETTING_1, &desc->setting_1, 6));
ESP_ERROR_CHECK(esp_efuse_write_field_blob(ESP_EFUSE_SETTING_2, &desc->setting_2, 5));
ESP_ERROR_CHECK(esp_efuse_write_field_cnt(ESP_EFUSE_CUSTOM_SECURE_VERSION, desc->custom_secure_version));
if (coding_scheme == coding_scheme_for_batch_mode) {
ESP_ERROR_CHECK(esp_efuse_batch_write_commit());
}
#endif // CONFIG_EFUSE_VIRTUAL
}
static esp_efuse_coding_scheme_t get_coding_scheme(void)
{
// The coding scheme is used for EFUSE_BLK1, EFUSE_BLK2 and EFUSE_BLK3.
// We use EFUSE_BLK3 (custom block) to verify it.
esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK3);
if (coding_scheme == EFUSE_CODING_SCHEME_NONE) {
ESP_LOGI(TAG, "Coding Scheme NONE");
#if CONFIG_IDF_TARGET_ESP32
} else if (coding_scheme == EFUSE_CODING_SCHEME_3_4) {
ESP_LOGI(TAG, "Coding Scheme 3/4");
} else {
ESP_LOGI(TAG, "Coding Scheme REPEAT");
}
#else
} else if (coding_scheme == EFUSE_CODING_SCHEME_RS) {
ESP_LOGI(TAG, "Coding Scheme RS (Reed-Solomon coding)");
}
#endif
return coding_scheme;
}
void app_main(void)
{
esp_efuse_coding_scheme_t coding_scheme = get_coding_scheme();
device_desc_t device_desc = { 0 };
read_efuse_fields(&device_desc);
ESP_LOGW(TAG, "This example does not burn any efuse in reality only virtually");
#ifdef CONFIG_EFUSE_VIRTUAL
ESP_LOGW(TAG, "Write operations in efuse fields are performed virtually");
if (device_desc.device_role == 0) {
device_desc.module_version = 1;
device_desc.device_role = 2;
device_desc.setting_1 = 3;
device_desc.setting_2 = 4;
device_desc.custom_secure_version = 5;
write_efuse_fields(&device_desc, coding_scheme);
read_device_desc_efuse_fields(&device_desc);
}
#else
ESP_LOGW(TAG, "The part of the code that writes efuse fields is disabled");
#endif
ESP_LOGI(TAG, "Done");
}

View File

@@ -0,0 +1,81 @@
// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at",
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#include "sdkconfig.h"
#include "esp_efuse.h"
#include <assert.h>
#include "esp_efuse_custom_table.h"
// md5_digest_table 584317af6a850ee16ef6206c139d6cf5
// This file was generated from the file esp_efuse_custom_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
// If you want to change some fields, you need to change esp_efuse_custom_table.csv file
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
// To show efuse_table run the command 'show_efuse_table'.
#define MAX_BLK_LEN CONFIG_EFUSE_MAX_BLK_LEN
// The last free bit in the block is counted over the entire file.
#define LAST_FREE_BIT_BLK3 94
_Static_assert(LAST_FREE_BIT_BLK3 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");
static const esp_efuse_desc_t MODULE_VERSION[] = {
{EFUSE_BLK3, 56, 8}, // Module version,
};
static const esp_efuse_desc_t DEVICE_ROLE[] = {
{EFUSE_BLK3, 64, 3}, // Device role,
};
static const esp_efuse_desc_t SETTING_1[] = {
{EFUSE_BLK3, 67, 6}, // Setting 1,
};
static const esp_efuse_desc_t SETTING_2[] = {
{EFUSE_BLK3, 73, 5}, // Setting 2,
};
static const esp_efuse_desc_t CUSTOM_SECURE_VERSION[] = {
{EFUSE_BLK3, 78, 16}, // Custom secure version,
};
const esp_efuse_desc_t* ESP_EFUSE_MODULE_VERSION[] = {
&MODULE_VERSION[0], // Module version
NULL
};
const esp_efuse_desc_t* ESP_EFUSE_DEVICE_ROLE[] = {
&DEVICE_ROLE[0], // Device role
NULL
};
const esp_efuse_desc_t* ESP_EFUSE_SETTING_1[] = {
&SETTING_1[0], // Setting 1
NULL
};
const esp_efuse_desc_t* ESP_EFUSE_SETTING_2[] = {
&SETTING_2[0], // Setting 2
NULL
};
const esp_efuse_desc_t* ESP_EFUSE_CUSTOM_SECURE_VERSION[] = {
&CUSTOM_SECURE_VERSION[0], // Custom secure version
NULL
};

View File

@@ -0,0 +1,18 @@
# field_name, | efuse_block, | bit_start, | bit_count, |comment #
# | (EFUSE_BLK0 | (0..255) | (1..-) | #
# | EFUSE_BLK1 | |MAX_BLK_LEN*| #
# | EFUSE_BLK2 | | | #
# | EFUSE_BLK3) | | | #
##########################################################################
# *) The value MAX_BLK_LEN depends on CONFIG_EFUSE_MAX_BLK_LEN, will be replaced with "None" - 256. "3/4" - 192. "REPEAT" - 128.
# !!!!!!!!!!! #
# After editing this file, run the command manually "make efuse_common_table" or "idf.py efuse_common_table"
# this will generate new source files, next rebuild all the sources.
# !!!!!!!!!!! #
MODULE_VERSION, EFUSE_BLK3, 56, 8, Module version
DEVICE_ROLE, EFUSE_BLK3, 64, 3, Device role
SETTING_1, EFUSE_BLK3, 67, 6, Setting 1
SETTING_2, EFUSE_BLK3, 73, 5, Setting 2
CUSTOM_SECURE_VERSION, EFUSE_BLK3, 78, 16, Custom secure version
Can't render this file because it contains an unexpected character in line 7 and column 87.

View File

@@ -0,0 +1,36 @@
// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at",
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#ifdef __cplusplus
extern "C" {
#endif
// md5_digest_table 584317af6a850ee16ef6206c139d6cf5
// This file was generated from the file esp_efuse_custom_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
// If you want to change some fields, you need to change esp_efuse_custom_table.csv file
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
// To show efuse_table run the command 'show_efuse_table'.
extern const esp_efuse_desc_t* ESP_EFUSE_MODULE_VERSION[];
extern const esp_efuse_desc_t* ESP_EFUSE_DEVICE_ROLE[];
extern const esp_efuse_desc_t* ESP_EFUSE_SETTING_1[];
extern const esp_efuse_desc_t* ESP_EFUSE_SETTING_2[];
extern const esp_efuse_desc_t* ESP_EFUSE_CUSTOM_SECURE_VERSION[];
#ifdef __cplusplus
}
#endif