linux target: add support for building esp_hw_support, soc and hal components for linux target

This commit is contained in:
Marius Vikhammer
2022-12-09 13:59:39 +08:00
parent f7d6f83c41
commit 4ddbaa4166
21 changed files with 250 additions and 21 deletions
@@ -15,3 +15,7 @@ components/esp_hw_support/test_apps/rtc_clk:
- if: IDF_TARGET in ["esp32c6"]
temporary: true
reason: Unsupported on C6 for now. TODO IDF-5645
components/heap/host_test/host_test_linux:
enable:
- if: IDF_TARGET == "linux"
+8
View File
@@ -1,5 +1,13 @@
idf_build_get_property(target IDF_TARGET)
# On Linux, we only support a few features, hence this simple component registration
if(${target} STREQUAL "linux")
idf_component_register(SRCS "port/linux/esp_random.c"
"port/linux/chip_info.c"
INCLUDE_DIRS "include")
return()
endif()
set(requires soc)
# only esp_hw_support/adc_share_hw_ctrl.c requires efuse component
set(priv_requires efuse spi_flash bootloader_support)
+1 -1
View File
@@ -33,7 +33,7 @@ menu "Hardware Settings"
bool
# Insert chip-specific MAC config
rsource "./port/$IDF_TARGET/Kconfig.mac"
orsource "./port/$IDF_TARGET/Kconfig.mac"
endmenu
menu "Sleep Config"
@@ -0,0 +1,9 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five 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.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)
project(test_hw_support_linux)
@@ -0,0 +1,3 @@
| Supported Targets | Linux |
| ----------------- | ----- |
@@ -0,0 +1,3 @@
idf_component_register(SRCS "test_hw_support_linux.c"
INCLUDE_DIRS "."
PRIV_REQUIRES unity)
@@ -0,0 +1,78 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "esp_random.h"
/* Note: these are just sanity tests, the implementation of esp_random do not produce cryptographically secure numbers on Linux
*/
TEST_CASE("call esp_random()", "[random]")
{
const size_t NUM_RANDOM = 128; /* in most cases this is massive overkill */
uint32_t zeroes = UINT32_MAX;
uint32_t ones = 0;
for (int i = 0; i < NUM_RANDOM - 1; i++) {
uint32_t r = esp_random();
ones |= r;
zeroes &= ~r;
}
/* assuming a 'white' random distribution, we can expect
usually at least one time each bit will be zero and at
least one time each will be one. Statistically this
can still fail, just *very* unlikely to. */
TEST_ASSERT_EQUAL_HEX32(0, zeroes);
TEST_ASSERT_EQUAL_HEX32(UINT32_MAX, ones);
}
TEST_CASE("call esp_fill_random()", "[random]")
{
const size_t NUM_BUF = 200;
const size_t BUF_SZ = 16;
uint8_t buf[NUM_BUF][BUF_SZ];
uint8_t zero_buf[BUF_SZ];
uint8_t one_buf[BUF_SZ];
bzero(buf, sizeof(buf));
bzero(one_buf, sizeof(zero_buf));
memset(zero_buf, 0xFF, sizeof(one_buf));
for (int i = 0; i < NUM_BUF; i++) {
esp_fill_random(buf[i], BUF_SZ);
}
/* No two 128-bit buffers should be the same
(again, statistically this could happen but it's very unlikely) */
for (int i = 0; i < NUM_BUF; i++) {
for (int j = 0; j < NUM_BUF; j++) {
if (i != j) {
TEST_ASSERT_NOT_EQUAL(0, memcmp(buf[i], buf[j], BUF_SZ));
}
}
}
/* Do the same all bits are zero and one at least once test across the buffers */
for (int i = 0; i < NUM_BUF; i++) {
for (int x = 0; x < BUF_SZ; x++) {
zero_buf[x] &= ~buf[i][x];
one_buf[x] |= buf[i][x];
}
}
for (int x = 0; x < BUF_SZ; x++) {
TEST_ASSERT_EQUAL_HEX8(0, zero_buf[x]);
TEST_ASSERT_EQUAL_HEX8(0xFF, one_buf[x]);
}
}
void app_main(void)
{
printf("Running heap linux API host test app");
unity_run_menu();
}
@@ -0,0 +1 @@
CONFIG_IDF_TARGET="linux"
@@ -27,6 +27,7 @@ typedef enum {
CHIP_ESP32H4 = 6, //!< ESP32-H4
CHIP_ESP32C2 = 12, //!< ESP32-C2
CHIP_ESP32C6 = 13, //!< ESP32-C6
CHIP_POSIX_LINUX = 999, //!< The code is running on POSIX/Linux simulator
} esp_chip_model_t;
/* Chip feature flags, used in esp_chip_info_t */
@@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2013-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "esp_chip_info.h"
void esp_chip_info(esp_chip_info_t *out_info)
{
memset(out_info, 0, sizeof(*out_info));
out_info->model = CHIP_POSIX_LINUX;
// TODO: May need to adjust once networking becomes available on POSIX/Linux
out_info->features = 0;
out_info->revision = 0;
out_info->cores = 1;
}
@@ -0,0 +1,40 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <sys/param.h>
#include "esp_log.h"
static const char* TAG = "esp-random";
static void __attribute__((constructor)) esp_random_init()
{
srand(time(NULL));
ESP_LOGW(TAG, "esp_random do not provide a cryptographically secure numbers on Linux, and should never be used for anything security related");
}
uint32_t esp_random(void)
{
/* Adding INT32_MAX to shift the results such that after conversion to uint32_t we still get 32 bits of random data */
return (rand() + INT32_MAX);
}
void esp_fill_random(void *buf, size_t len)
{
assert(buf != NULL);
uint8_t *buf_bytes = (uint8_t *)buf;
while (len > 0) {
uint32_t word = esp_random();
uint32_t to_copy = MIN(sizeof(word), len);
memcpy(buf_bytes, &word, to_copy);
buf_bytes += to_copy;
len -= to_copy;
}
}