Date:2011-05-10 22:23:08 (8 years 3 months ago)
Author:Werner Almesberger
Commit:85f60de9d52195afb3a0232a03e850d96584bb9a
Message: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
Files: atusb/fw/Makefile (1 diff)
atusb/fw/atusb.c (2 diffs)
atusb/fw/board.c (3 diffs)
atusb/fw/board.h (1 diff)
atusb/fw/descr.c (1 diff)
atusb/fw/sernum.c (1 diff)
atusb/fw/sernum.h (1 diff)
atusb/fw/usb/dfu.c (3 diffs)
atusb/fw/usb/usb.h (2 diffs)

Change Details

atusb/fw/Makefile
2929SIZE = $(AVR_PREFIX)size
3030
3131USB_OBJS = usb.o atu2.o
32OBJS = atusb.o board.o spi.o descr.o ep0.o $(USB_OBJS)
33BOOT_OBJS = boot.o board.o spi.o flash.o dfu.o $(USB_OBJS)
32OBJS = atusb.o board.o sernum.o spi.o descr.o ep0.o $(USB_OBJS)
33BOOT_OBJS = boot.o board.o sernum.o spi.o flash.o dfu.o $(USB_OBJS)
3434
3535vpath %.c usb/
3636
atusb/fw/atusb.c
1818#include "usb.h"
1919
2020#include "board.h"
21#include "sernum.h"
2122#include "spi.h"
2223#include "atusb/ep0.h"
2324
...... 
2829    spi_init();
2930    reset_rf();
3031
32    user_get_descriptor = sernum_get_descr;
33
3134    /* now we should be at 8 MHz */
3235
3336    usb_init();
atusb/fw/board.c
1515
1616#include <avr/io.h>
1717#include <avr/interrupt.h>
18#include <avr/boot.h>
1819
1920#define F_CPU 8000000UL
2021#include <util/delay.h>
2122
23#include "usb.h"
2224#include "at86rf230.h"
2325#include "board.h"
2426#include "spi.h"
2527
2628
29uint8_t board_sernum[42] = { 42, USB_DT_STRING };
30
31
2732static void set_clkm(void)
2833{
2934    /* switch CLKM to 8 MHz */
...... 
9499}
95100
96101
102static char hex(uint8_t nibble)
103{
104    return nibble < 10 ? '0'+nibble : 'a'+nibble-10;
105}
106
107
108static void get_sernum(void)
109{
110    uint8_t sig;
111    int i;
112
113    for (i = 0; i != 10; i++) {
114        sig = boot_signature_byte_get(i+0xe);
115        board_sernum[(i << 2)+2] = hex(sig >> 4);
116        board_sernum[(i << 2)+4] = hex(sig & 0xf);
117    }
118}
119
120
97121void board_init(void)
98122{
99123    /* Disable the watchdog timer */
...... 
111135    /* set up all the outputs; default port value is 0 */
112136
113137    OUT(LED);
114    OUT(nRST_RF); /* resets the transceiver */
138    OUT(nRST_RF); /* this also resets the transceiver */
115139    OUT(SLP_TR);
140
141    get_sernum();
116142}
atusb/fw/board.h
6262#define DFU_USB_PRODUCT USB_PRODUCT
6363
6464
65#define HAS_BOARD_SERNUM
66
67extern uint8_t board_sernum[42];
68
69
6570void reset_rf(void);
6671void reset_cpu(void);
6772uint8_t read_irq(void);
atusb/fw/descr.c
3434    LE(0x0001), /* bcdDevice */
3535    0, /* iManufacturer */
3636    0, /* iProduct */
37#ifdef HAS_BOARD_SERNUM
38    1, /* iSerialNumber */
39#else
3740    0, /* iSerialNumber */
41#endif
3842    1 /* bNumConfigurations */
3943};
4044
atusb/fw/sernum.c
1/*
2 * fw/sernum.c - ATUSB serial number
3 *
4 * Written 2008-2011 by Werner Almesberger
5 * Copyright 2008-2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include "usb.h"
17
18#include "board.h"
19#include "sernum.h"
20
21
22static const uint8_t string_descriptor_0[] = {
23    4, /* blength */
24    USB_DT_STRING, /* bDescriptorType */
25    LE(USB_LANGID_ENGLISH_US) /* wLANGID[0] */
26};
27
28
29int sernum_get_descr(uint8_t type, uint8_t index, const uint8_t **reply,
30    uint8_t *size)
31{
32    if (type != USB_DT_STRING)
33        return 0;
34    switch (index) {
35    case 0:
36        *reply = string_descriptor_0;
37        *size = sizeof(string_descriptor_0);
38        return 1;
39    case 1:
40        *reply = board_sernum;
41        *size = sizeof(board_sernum);
42        return 1;
43    default:
44        return 0;
45    }
46}
atusb/fw/sernum.h
1/*
2 * fw/sernum.h - ATUSB serial number
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef SERNUM_H
14#define SERNUM_H
15
16#include <stdint.h>
17
18#include "board.h"
19
20
21#ifdef HAS_BOARD_SERNUM
22
23int sernum_get_descr(uint8_t type, uint8_t index, const uint8_t **reply,
24    uint8_t *size);
25
26#else /* HAS_BOARD_SERNUM */
27
28static inline int sernum_get_descr(uint8_t type, uint8_t index,
29    const uint8_t **reply, uint8_t *size)
30{
31    return 0;
32}
33
34#endif /* !HAS_BOARD_SERNUM */
35
36#endif /* !SERNUM_H */
atusb/fw/usb/dfu.c
3131#include "dfu.h"
3232
3333#include "../board.h"
34#include "../sernum.h"
3435
3536
3637#ifndef NULL
...... 
5455    LE(0x0001), /* bcdDevice */
5556    0, /* iManufacturer */
5657    0, /* iProduct */
58#ifdef HAS_BOARD_SERNUM
59    1, /* iSerialNumber */
60#else
5761    0, /* iSerialNumber */
62#endif
5863    1 /* bNumConfigurations */
5964};
6065
...... 
262267    uint8_t *size)
263268{
264269    if (type != DFU_DT_FUNCTIONAL)
265        return 0;
270        return sernum_get_descr(type, index, reply, size);
266271    *reply = functional_descriptor;
267272    *size = sizeof(functional_descriptor);
268273    return 1;
atusb/fw/usb/usb.h
11/*
22 * fw/usb//usb.h - USB hardware setup and standard device requests
33 *
4 * Written 2008, 2009 by Werner Almesberger
5 * Copyright 2008, 2009 Werner Almesberger
4 * Written 2008, 2009, 2011 by Werner Almesberger
5 * Copyright 2008, 2009, 2011 Werner Almesberger
66 *
77 * This program is free software; you can redistribute it and/or modify
88 * it under the terms of the GNU General Public License as published by
...... 
7474#define SET_INTERFACE 0x0b
7575#define SYNCH_FRAME 0x0c
7676
77/*
78 * USB Language ID codes
79 *
80 * http://www.usb.org/developers/docs/USB_LANGIDs.pdf
81 */
82
83#define USB_LANGID_ENGLISH_US 0x409
84
7785
7886/*
7987 * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,

Archive Download the corresponding diff file



interactive