Adds a board common way of retriving the SoC' Unique identifier

This abstraction will support mcu's with longer ID's then the
   stm32 family.

   It provids a common interface for byte and 32 bit word access
   to the UUID data and a facility to reorder it.
This commit is contained in:
David Sidrane
2017-01-25 07:43:11 +01:00
committed by Lorenz Meier
parent b86380086e
commit bf0de997cb
4 changed files with 214 additions and 4 deletions
+102 -3
View File
@@ -188,11 +188,41 @@
/************************************************************************************
* Public Data
************************************************************************************/
/* board reset control */
typedef enum board_reset_e {
board_reset_normal = 0,
board_reset_extended = 1,
board_reset_enter_bootloader = 2
board_reset_normal = 0, /* Perform a normal reset */
board_reset_extended = 1, /* Perform an extend reset as defined by board */
board_reset_power_off = 2, /* Reset to the boot loader, signaling a power off */
board_reset_enter_bootloader = 3 /* Perform a reset to the boot loader */
} board_reset_e;
/* Defined the types used for board UUID
*
* A type suitable for holding the byte format of the UUID
* The raw format is the result of a memcopy from the lowest address
* to the highest address of the CPU's serial number
* Bytes [00] ,[01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11]...
* Bits 00-07,8-15,16-23,24-31,32-39,40-47,48-55,56-63,64-71,72-79,80-87,88-95...
*
*/
typedef uint8_t raw_uuid_byte_t[PX4_CPU_UUID_BYTE_LENGTH];
/* A type suitable for holding the reordering array for the byte format of the UUID
*/
typedef const uint8_t uuid_uint8_reorder_t[PX4_CPU_UUID_BYTE_LENGTH];
/* A type suitable for defining the 32bit format of the UUID and reordering */
typedef uint32_t raw_uuid_uint32_t[PX4_CPU_UUID_WORD32_LENGTH];
typedef const uint8_t raw_uuid_uint32_reorder_t[PX4_CPU_UUID_WORD32_LENGTH];
/*
* The Legacy px4 uint32 ordering
* word [0] [1] [2]
* bits 31-00, 63-32, 95-64
*/
extern const raw_uuid_uint32_reorder_t px4_legacy_word32_order;
/************************************************************************************
* Private Functions
************************************************************************************/
@@ -276,3 +306,72 @@ __EXPORT void board_system_reset(int status) noreturn_function;
#else
__EXPORT int board_set_bootload_mode(board_reset_e mode);
#endif
#if !defined(BOARD_OVERRIDE_UUID)
/************************************************************************************
* Name: board_get_uuid_raw
*
* Description:
* All boards either provide a way to read a uuid of PX4_CPU_UUID_BYTE_LENGTH
* from PX4_CPU_UUID_ADDRESS in the SoC's address space OR define
* BOARD_OVERRIDE_UUID as an array of bytes that is PX4_CPU_UUID_BYTE_LENGTH
* The raw format is the result of coping the bytes from the SoC in low memory
* to high memory.
*
************************************************************************************/
__EXPORT void board_get_uuid_raw(raw_uuid_byte_t *raw_uuid_bytes);
/************************************************************************************
* Name: board_get_uuid
*
* Description:
* All boards either provide a way to read a uuid of PX4_CPU_UUID_BYTE_LENGTH
* from PX4_CPU_UUID_ADDRESS in the SoC's address space OR define
* BOARD_OVERRIDE_UUID as an array of bytes that is PX4_CPU_UUID_BYTE_LENGTH
* The format is the result of coping the bytes from the SoC in low memory
* to high memory and then reordering them based on the reorder.
*
************************************************************************************/
__EXPORT void board_get_uuid(raw_uuid_byte_t raw_uuid_bytes,
uuid_uint8_reorder_t reorder);
/************************************************************************************
* Name: board_get_uuid_raw32
*
* Description:
* All boards either provide a way to read a uuid of PX4_CPU_UUID_WORD32_LENGTH
* from PX4_CPU_UUID_ADDRESS in the Soc's address space OR define
* BOARD_OVERRIDE_UUID as an array of bytes that is PX4_CPU_UUID_BYTE_LENGTH
* The raw32 format is the result of coping returning the 32bit words from
* low memory to high memory. But provide an optional to argument to reorder
* the output array.
*
************************************************************************************/
__EXPORT void board_get_uuid_raw32(raw_uuid_uint32_t raw_uuid_words,
raw_uuid_uint32_reorder_t *optional_reorder);
/************************************************************************************
* Name: board_get_uuid_formated32
*
* Description:
* All boards either provide a way to retrieve a uuid and format it
* Or define BOARD_OVERRIDE_UUID
* The format is the result the optionally reordered raw 32bit word format
* printed with the optional separator
*
* With seperator = ":"
* 31-00:63-32:95-64
* 32383336:412038:33355110
* With seperator = " "
* 31-00:63-32:95-64
* 32383336 412038 33355110
* With seperator = NULL
* 31-00:63-32:95-64
* 3238333641203833355110
*
************************************************************************************/
__EXPORT int board_get_uuid_formated32(char *format_buffer, int size,
const char *format,
const char *seperator,
raw_uuid_uint32_reorder_t *optional_reorder);
#endif // !defined(BOARD_OVERRIDE_UUID)
@@ -0,0 +1,95 @@
/****************************************************************************
*
* Copyright (C) 2017 PX4 Development Team. All rights reserved.
* Author: @author David Sidrane <david_s5@nscdg.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file board_identity.c
* Implementation of STM32 based Board identity API
*/
#include <px4_config.h>
#include <stdio.h>
#include <string.h>
const raw_uuid_uint32_reorder_t px4_legacy_word32_order = PX4_CPU_UUID_WORD32_LEGACY_FORMAT_ORDER;
void board_get_uuid_raw(raw_uuid_byte_t *raw_uuid)
{
memcpy(raw_uuid, (uint8_t *) STM32_SYSMEM_UID, PX4_CPU_UUID_BYTE_LENGTH);
}
void board_get_uuid(raw_uuid_byte_t uuid, uuid_uint8_reorder_t reorder)
{
raw_uuid_byte_t raw_uuid;
board_get_uuid_raw(&raw_uuid);
for (int i = 0; i < PX4_CPU_UUID_BYTE_LENGTH; i++) {
uuid[i] = raw_uuid[reorder[i]];
}
}
__EXPORT void board_get_uuid_raw32(raw_uuid_uint32_t raw_uuid_words,
raw_uuid_uint32_reorder_t *optional_reorder)
{
if (optional_reorder == NULL) {
optional_reorder = &px4_legacy_word32_order;
}
uint32_t *chip_uuid = (uint32_t *) STM32_SYSMEM_UID;
for (int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) {
raw_uuid_words[i] = chip_uuid[(*optional_reorder)[i]];
}
}
int board_get_uuid_formated32(char *format_buffer, int size,
const char *format,
const char *seperator,
raw_uuid_uint32_reorder_t *optional_reorder)
{
raw_uuid_uint32_t uuid;
board_get_uuid_raw32(uuid, optional_reorder);
int offset = 0;
int sep_size = seperator ? strlen(seperator) : 0;
for (int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) {
offset += snprintf(&format_buffer[offset], size - ((i * 2 * sizeof(uint32_t)) + 1), format, uuid[i]);
if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) {
strcat(&format_buffer[offset], seperator);
offset += sep_size;
}
}
return 0;
}
+1 -1
View File
@@ -43,8 +43,8 @@
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <board_config.h>
#include "px4_micro_hal.h"
#include <board_config.h>
#elif defined (__PX4_POSIX)
# include <system_config.h>
+16
View File
@@ -56,6 +56,22 @@ __BEGIN_DECLS
# include <stm32_tim.h>
# include <stm32_spi.h>
# include <stm32_i2c.h>
/* STM32/32F7 defines the 96 bit UUID as
* init32_t[3] that can be read as bytes/half-words/words
* init32_t[0] PX4_CPU_UUID_ADDRESS[0] bits 31:0 (offset 0)
* init32_t[1] PX4_CPU_UUID_ADDRESS[1] bits 63:32 (offset 4)
* init32_t[2] PX4_CPU_UUID_ADDRESS[3] bits 96:64 (offset 8)
*
* PX4 uses the legacy uint32 ordering
* word [0] [1] [2]
* bits 31-00, 63-32, 95-64
*/
# define PX4_CPU_UUID_BYTE_LENGTH 12
# define PX4_CPU_UUID_WORD32_LENGTH (PX4_CPU_UUID_BYTE_LENGTH/sizeof(uint32_t))
# define PX4_CPU_UUID_WORD32_LEGACY_FORMAT_ORDER {0,1,2}
# define PX4_CPU_UUID_WORD32_LEGACY_FORMAT_SIZE (PX4_CPU_UUID_WORD32_LENGTH-1+(2*PX4_CPU_UUID_BYTE_LENGTH))
# define px4_spibus_initialize(port_1based) stm32_spibus_initialize(port_1based)
# define px4_i2cbus_initialize(bus_num_1based) stm32_i2cbus_initialize(bus_num_1based)