【zephyr】從 kernel space 到User space get_device_id

 

 

1.User space

\tests\drivers\hwinfo\api\src\main.c 

hwinfo: add driver support for NXP i.mx RT device ID

Add driver support for NXP i.mx RT ID device.
This device has an ID of 8 bytes.
 

#include <hwinfo.h>    //函數接口定義
#include <ztest.h>
#include <strings.h>
#include <errno.h>

/*
 * @addtogroup t_hwinfo_get_device_id_api
 * @{
 * @defgroup t_hwinfo_get_device_id test_hwinfo_get_device_id
 * @brief TestPurpose: verify device id get works
 * @details
 * - Test Steps
 *   -# Read the ID
 *   -# Check if to many bytes are written to the buffer
 *   -# Check if UID is plausible
 * - Expected Results
 *   -# Device uid with correct length should be written to the buffer.
 * @}
 */

#define BUFFER_LENGTH 17
#define BUFFER_CANARY 0xFF

/*
 * Function invokes the get_entropy callback in driver
 * to get the random data and fill to passed buffer
 */
static void test_device_id_get(void)
{
	u8_t buffer_1[BUFFER_LENGTH];
	u8_t buffer_2[BUFFER_LENGTH];
	ssize_t length_read_1, length_read_2;
	int i;

	length_read_1 = hwinfo_get_device_id(buffer_1, 1); //第1次測試 
	zassert_not_equal(length_read_1, -ENOTSUP, "Not supported by hardware");
	zassert_false((length_read_1 < 0), "Error returned: %d", length_read_1);
	zassert_not_equal(length_read_1, 0, "Zero bytes read");
	zassert_equal(length_read_1, 1, "Length not adhered");

	memset(buffer_1, BUFFER_CANARY, sizeof(buffer_1));

	length_read_1 = hwinfo_get_device_id(buffer_1, BUFFER_LENGTH - 1);
	zassert_equal(buffer_1[length_read_1], BUFFER_CANARY,
		      "Too many bytes are written");

	memcpy(buffer_2, buffer_1, length_read_1);

	for (i = 0; i < BUFFER_LENGTH; i++) {
		buffer_1[i] ^= 0xA5;
	}

	length_read_2 = hwinfo_get_device_id(buffer_1, BUFFER_LENGTH - 1); //第2次函數
	zassert_equal(length_read_1, length_read_2, "Length varied"); //比較多次讀是否出錯

	zassert_equal(buffer_1[length_read_1], (BUFFER_CANARY ^ 0xA5),
		      "Too many bytes are written");

//比較2次Buffer 讀取是否出錯
	for (i = 0; i < length_read_1; i++) {
		zassert_equal(buffer_1[i], buffer_2[i],
			      "Two consecutively readings don't match");
	}
}

1.1 頭文件

/**
 * @brief Copy the device id to a buffer
 *
 * This routine copies "length" number of bytes of the device ID to the buffer.
 * If the device ID is smaller then length, the rest of the buffer is left unchanged.
 * The ID depends on the hardware and is not guaranteed unique.
 *
 * @param buffer  Buffer to write the ID to.
 * @param length  Max length of the buffer.
 *
 * @retval size of the device ID copied or negative on error.
 */
__syscall ssize_t hwinfo_get_device_id(u8_t *buffer, size_t length);

ssize_t z_impl_hwinfo_get_device_id(u8_t *buffer, size_t length); //內涵函數定義

 2.0 驅動實現

2.1 CMAKE

zephyr_sources_ifdef(CONFIG_USERSPACE   hwinfo_handlers.c)
zephyr_sources_ifdef(CONFIG_HWINFO      hwinfo_weak_impl.c)

zephyr_sources_ifdef(CONFIG_HWINFO_STM32      hwinfo_stm32.c)
zephyr_sources_ifdef(CONFIG_HWINFO_NRF        hwinfo_nrf.c)
zephyr_sources_ifdef(CONFIG_HWINFO_MCUX_SIM   hwinfo_mcux_sim.c)
zephyr_sources_ifdef(CONFIG_HWINFO_IMXRT      hwinfo_imxrt.c)
zephyr_sources_ifdef(CONFIG_HWINFO_SAM        hwinfo_sam.c)

zephyr_sources_ifdef(CONFIG_HWINFO_SHELL hwinfo_shell.c)

2.2 Kconfig

menuconfig HWINFO
	bool "Hardware Information driver"
	help
	  Enable hwinfo driver.

if HWINFO

config HWINFO_SHELL
	bool "Enable HWINFO Shell"
	depends on SHELL
	help
	  Enable hwinfo Shell for testing.

config HWINFO_STM32
	bool "STM32 hwinfo"
	default y
	depends on SOC_FAMILY_STM32
	select USE_STM32_LL_UTILS
	help
	  Enable STM32 hwinfo driver.


config HWINFO_IMXRT
	bool "NXP i.mx RT device ID"
	default y
	depends on SOC_SERIES_IMX_RT
	help
	  Enable NXP i.mx RT hwinfo driver.

config HWINFO_SAM
	bool "Atmel SAM device ID"
	default y
	depends on SOC_FAMILY_SAM
	help
	  Enable Atmel SAM hwinfo driver.

endif

2.3 源文件

*
 * Copyright (c) 2019 Alexander Wachter
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <soc.h>
#include <hwinfo.h>
#include <string.h>

struct imxrt_uid {
	u32_t id[2];
};

ssize_t z_impl_hwinfo_get_device_id(u8_t *buffer, size_t length)
{
	struct imxrt_uid dev_id;

	dev_id.id[0] = OCOTP->CFG1;
	dev_id.id[1] = OCOTP->CFG2;

	if (length > sizeof(dev_id.id)) {
		length = sizeof(dev_id.id);
	}

	memcpy(buffer, dev_id.id, length);

	return length;
}

3. 功能使用

1.打開menuconfig 

menuconfig hardware information driver 

2. 定義功能宏

CONFIG_HWINFO_IMXRT=y
CONFIG_HWINFO=y

3.顯示效果如下 

編譯生成obj
編譯生成 obj

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章