1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-12-23 04:35:32 +02:00

atusb/fw: use the unique serial number of the ATmega8/16/32U2 for iSerialNumber

- usb/usb.h (USB_LANGID_ENGLISH_US): added USB LANGID for US-English
- board.h (board_sernum), board.c (board_sernum, hex, get_sernum,
  board_init): provide the board's serial number in "board_sernum"
  (UTF-encoded)
- sernum.h (sernum_get_descr), sernum.c (sernum_get_descr): return
  string descriptors for the serial number
- descr.c (device_descriptor), usb/dfu.c (device_descriptor):
  set iSerialNumber if serial number is available
- atusb.c (main), usb/dfu.c (my_descr): call sernum_get_descr for
  unknown descriptors
- Makefile (OBJS, BOOT_OBJS): added sernum.o
This commit is contained in:
Werner Almesberger 2011-05-10 17:23:08 -03:00
parent fd91546c59
commit 85f60de9d5
9 changed files with 139 additions and 6 deletions

View File

@ -29,8 +29,8 @@ OBJCOPY = $(AVR_PREFIX)objcopy
SIZE = $(AVR_PREFIX)size
USB_OBJS = usb.o atu2.o
OBJS = atusb.o board.o spi.o descr.o ep0.o $(USB_OBJS)
BOOT_OBJS = boot.o board.o spi.o flash.o dfu.o $(USB_OBJS)
OBJS = atusb.o board.o sernum.o spi.o descr.o ep0.o $(USB_OBJS)
BOOT_OBJS = boot.o board.o sernum.o spi.o flash.o dfu.o $(USB_OBJS)
vpath %.c usb/

View File

@ -18,6 +18,7 @@
#include "usb.h"
#include "board.h"
#include "sernum.h"
#include "spi.h"
#include "atusb/ep0.h"
@ -28,6 +29,8 @@ int main(void)
spi_init();
reset_rf();
user_get_descriptor = sernum_get_descr;
/* now we should be at 8 MHz */
usb_init();

View File

@ -15,15 +15,20 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include "usb.h"
#include "at86rf230.h"
#include "board.h"
#include "spi.h"
uint8_t board_sernum[42] = { 42, USB_DT_STRING };
static void set_clkm(void)
{
/* switch CLKM to 8 MHz */
@ -94,6 +99,25 @@ void panic(void)
}
static char hex(uint8_t nibble)
{
return nibble < 10 ? '0'+nibble : 'a'+nibble-10;
}
static void get_sernum(void)
{
uint8_t sig;
int i;
for (i = 0; i != 10; i++) {
sig = boot_signature_byte_get(i+0xe);
board_sernum[(i << 2)+2] = hex(sig >> 4);
board_sernum[(i << 2)+4] = hex(sig & 0xf);
}
}
void board_init(void)
{
/* Disable the watchdog timer */
@ -111,6 +135,8 @@ void board_init(void)
/* set up all the outputs; default port value is 0 */
OUT(LED);
OUT(nRST_RF); /* resets the transceiver */
OUT(nRST_RF); /* this also resets the transceiver */
OUT(SLP_TR);
get_sernum();
}

View File

@ -62,6 +62,11 @@
#define DFU_USB_PRODUCT USB_PRODUCT
#define HAS_BOARD_SERNUM
extern uint8_t board_sernum[42];
void reset_rf(void);
void reset_cpu(void);
uint8_t read_irq(void);

View File

@ -34,7 +34,11 @@ const uint8_t device_descriptor[18] = {
LE(0x0001), /* bcdDevice */
0, /* iManufacturer */
0, /* iProduct */
#ifdef HAS_BOARD_SERNUM
1, /* iSerialNumber */
#else
0, /* iSerialNumber */
#endif
1 /* bNumConfigurations */
};

46
atusb/fw/sernum.c Normal file
View File

@ -0,0 +1,46 @@
/*
* fw/sernum.c - ATUSB serial number
*
* Written 2008-2011 by Werner Almesberger
* Copyright 2008-2011 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdint.h>
#include "usb.h"
#include "board.h"
#include "sernum.h"
static const uint8_t string_descriptor_0[] = {
4, /* blength */
USB_DT_STRING, /* bDescriptorType */
LE(USB_LANGID_ENGLISH_US) /* wLANGID[0] */
};
int sernum_get_descr(uint8_t type, uint8_t index, const uint8_t **reply,
uint8_t *size)
{
if (type != USB_DT_STRING)
return 0;
switch (index) {
case 0:
*reply = string_descriptor_0;
*size = sizeof(string_descriptor_0);
return 1;
case 1:
*reply = board_sernum;
*size = sizeof(board_sernum);
return 1;
default:
return 0;
}
}

36
atusb/fw/sernum.h Normal file
View File

@ -0,0 +1,36 @@
/*
* fw/sernum.h - ATUSB serial number
*
* Written 2011 by Werner Almesberger
* Copyright 2011 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef SERNUM_H
#define SERNUM_H
#include <stdint.h>
#include "board.h"
#ifdef HAS_BOARD_SERNUM
int sernum_get_descr(uint8_t type, uint8_t index, const uint8_t **reply,
uint8_t *size);
#else /* HAS_BOARD_SERNUM */
static inline int sernum_get_descr(uint8_t type, uint8_t index,
const uint8_t **reply, uint8_t *size)
{
return 0;
}
#endif /* !HAS_BOARD_SERNUM */
#endif /* !SERNUM_H */

View File

@ -31,6 +31,7 @@
#include "dfu.h"
#include "../board.h"
#include "../sernum.h"
#ifndef NULL
@ -54,7 +55,11 @@ const uint8_t device_descriptor[] = {
LE(0x0001), /* bcdDevice */
0, /* iManufacturer */
0, /* iProduct */
#ifdef HAS_BOARD_SERNUM
1, /* iSerialNumber */
#else
0, /* iSerialNumber */
#endif
1 /* bNumConfigurations */
};
@ -262,7 +267,7 @@ static int my_descr(uint8_t type, uint8_t index, const uint8_t **reply,
uint8_t *size)
{
if (type != DFU_DT_FUNCTIONAL)
return 0;
return sernum_get_descr(type, index, reply, size);
*reply = functional_descriptor;
*size = sizeof(functional_descriptor);
return 1;

View File

@ -1,8 +1,8 @@
/*
* fw/usb//usb.h - USB hardware setup and standard device requests
*
* Written 2008, 2009 by Werner Almesberger
* Copyright 2008, 2009 Werner Almesberger
* Written 2008, 2009, 2011 by Werner Almesberger
* Copyright 2008, 2009, 2011 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -74,6 +74,14 @@
#define SET_INTERFACE 0x0b
#define SYNCH_FRAME 0x0c
/*
* USB Language ID codes
*
* http://www.usb.org/developers/docs/USB_LANGIDs.pdf
*/
#define USB_LANGID_ENGLISH_US 0x409
/*
* Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,