Skip to content

Commit

Permalink
Merge pull request #854 from authmillenon/cpu-id
Browse files Browse the repository at this point in the history
drivers/cpu: add function to get CPU id/serial number
  • Loading branch information
miri64 committed Jul 31, 2014
2 parents d55da67 + 239f2e0 commit 7516c94
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cpu/native/include/cpu-conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@
/* for nativenet */
#define NATIVE_ETH_PROTO 0x1234

#define CPUID_ID_LEN (4)

#endif /* CPUCONF_H_ */
36 changes: 36 additions & 0 deletions cpu/native/periph_cpuid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2014 Martin Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @addtogroup driver_periph
* @{
*
* @file cpuid.c
* @brief Implementation
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/

#include <string.h>
#include <stdint.h>

#include "cpu-conf.h"
#include "native_internal.h"

#include "periph/cpuid.h"

void cpuid_get(void *id)
{
memset(id, 0xff, CPUID_ID_LEN); /* Just in case _native_id is shorter
than CPUID_ID_LEN. */
memcpy(id, &(_native_id), sizeof(_native_id));
}

/**
* @}
*/
37 changes: 37 additions & 0 deletions drivers/include/periph/cpuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2014 Martin Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @addtogroup driver_periph
* @{
*
* @file periph/cpuid.h
* @brief Provides access the CPU's serial number
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/

#ifndef __PERIPH_CPUID_H_
#define __PERIPH_CPUID_H_

#include "cpu-conf.h"

#if CPUID_ID_LEN
/**
* @brief Gets the serial number of the CPU.
*
* @param[out] id The serial number of the CPU of length CPU_ID_LEN (must be
* defined in the CPU's cpu-conf.h)
*/
void cpuid_get(void *id);
#endif /* CPUID_ID_LEN */

#endif /* __PERIPH_CPUID_H_ */
/**
* @}
*/
6 changes: 6 additions & 0 deletions tests/cpuid/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export APPLICATION = test_cpu_id
include ../Makefile.tests_common

BOARD_WHITELIST := native

include $(RIOTBASE)/Makefile.include
44 changes: 44 additions & 0 deletions tests/cpuid/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @ingroup tests
* @{
*
* @file
* @brief GET_CPU_ID() test application
*
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/

#include <stdint.h>
#include <stdio.h>

#include "cpu-conf.h"
#include "periph/cpuid.h"

#ifndef CPUID_ID_LEN
#error "cpuid is not defined for the CPU type of this board"
#endif

int main(void)
{
uint8_t id[CPUID_ID_LEN];

cpuid_get(id);

for (unsigned int i = 0; i < CPUID_ID_LEN; i++) {
printf("0x%02x ", id[i]);
}

printf("\n");

return 0;
}

0 comments on commit 7516c94

Please sign in to comment.