Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/cpu: add function to get CPU id/serial number #854

Merged
merged 2 commits into from
Jul 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a comment on this define.


#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;
}