Date:2011-05-30 02:05:28 (12 years 9 months ago)
Author:Werner Almesberger
Commit:8c57277953e9d1b3f63ef55853f4ca1abee225a2
Message:atusb/fw/: added free-running 48 bit 8 MHz counter for clock measurements

- atusb.c (main): poll the 16 bit timer for overflows
- board.h (timer_poll, timer_read), (board.c (timer_h, timer_poll,
timer_read, board_init): added support for a free-running 48 bit timer
(16 bits in hardware, 32 bits in software)
- include/atusb/ep0.h (ATUSB_TIMER), ep0.c (my_setup): new request
ATUSB_TIMER to retrieve the value of the 8 MHz counter
- include/atusb/ep0.h (enum atspi_requests): describe what the groups
of requests do
Files: atusb/fw/atusb.c (1 diff)
atusb/fw/board.c (3 diffs)
atusb/fw/board.h (1 diff)
atusb/fw/ep0.c (3 diffs)
atusb/fw/include/atusb/ep0.h (2 diffs)

Change Details

atusb/fw/atusb.c
3636    usb_init();
3737    ep0_init();
3838
39    while (1)
39    while (1) {
4040        usb_poll();
41        timer_poll();
42    }
4143}
atusb/fw/board.c
2929uint8_t board_sernum[42] = { 42, USB_DT_STRING };
3030
3131
32static uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */
33
34
3235static void set_clkm(void)
3336{
3437    /* switch CLKM to 8 MHz */
...... 
99102}
100103
101104
105void timer_poll(void)
106{
107    if (!(TIFR1 & (1 << TOV1)))
108        return;
109    TIFR1 = 1 << TOV1;
110    timer_h++;
111}
112
113
114uint64_t timer_read(void)
115{
116    uint32_t high;
117    uint8_t low, mid;
118
119    do {
120        timer_poll();
121        high = timer_h;
122        low = TCNT1L;
123        mid = TCNT1H;
124    }
125    while (TIFR1 & (1 << TOV1));
126
127    /*
128     * We need all these casts because the intermediate results are handled
129     * as if they were signed and thus get sign-expanded. Sounds wrong-ish.
130     */
131    return (uint64_t) high << 16 | (uint64_t) mid << 8 | (uint64_t) low;
132}
133
134
102135static char hex(uint8_t nibble)
103136{
104137    return nibble < 10 ? '0'+nibble : 'a'+nibble-10;
...... 
138171    OUT(nRST_RF); /* this also resets the transceiver */
139172    OUT(SLP_TR);
140173
174    /* configure timer 1 as a free-running CLK counter */
175
176    TCCR1A = 0;
177    TCCR1B = 1 << CS10;
178
141179    get_sernum();
142180}
atusb/fw/board.h
7474void led(int on);
7575void panic(void);
7676
77void timer_poll(void);
78uint64_t timer_read(void);
79
7780void board_init(void);
7881
7982#endif /* !BOARD_H */
atusb/fw/ep0.c
1212
1313
1414#include <stdint.h>
15#include <string.h>
1516
1617#include <avr/io.h>
1718
...... 
5758{
5859    unsigned tmp;
5960    uint8_t i;
61    uint64_t tmp64;
6062
6163    switch (setup->bmRequestType | setup->bRequest << 8) {
6264    case ATUSB_FROM_DEV(ATUSB_ID):
...... 
101103        usb_send(&eps[0], buf, 1, NULL, NULL);
102104        return 1;
103105
106    case ATUSB_FROM_DEV(ATUSB_TIMER):
107        debug("ATUSB_TIMER\n");
108        size = setup->wLength;
109        if (size > sizeof(tmp64))
110            size = sizeof(tmp64);
111        tmp64 = timer_read();
112        memcpy(buf, &tmp64, sizeof(tmp64));
113        usb_send(&eps[0], buf, size, NULL, NULL);
114        return 1;
115
104116    case ATUSB_TO_DEV(ATUSB_REG_WRITE):
105117        debug("ATUSB_REG_WRITE\n");
106118        spi_begin();
atusb/fw/include/atusb/ep0.h
2424 * host-> ATUSB_RF_RESET - - 0
2525 * ->host ATUSB_POLL_INT - - 1
2626 * host-> ATUSB_TEST - - 0
27 * ->host ATUSB_TIMER - - #bytes (6)
2728 *
2829 * host-> ATUSB_REG_WRITE value addr 0
2930 * ->host ATUSB_REG_READ - addr 1
...... 
6465
6566
6667enum atspi_requests {
67    ATUSB_ID = 0x00,
68    ATUSB_ID = 0x00, /* system status/control grp */
6869    ATUSB_BUILD,
6970    ATUSB_RESET,
70    ATUSB_RF_RESET = 0x10,
71    ATUSB_RF_RESET = 0x10, /* debug/test group */
7172    ATUSB_POLL_INT,
72    ATUSB_TEST,
73    ATUSB_REG_WRITE = 0x20,
73    ATUSB_TEST, /* atusb-sil only */
74    ATUSB_TIMER,
75    ATUSB_REG_WRITE = 0x20, /* transceiver group */
7476    ATUSB_REG_READ,
7577    ATUSB_BUF_WRITE,
7678    ATUSB_BUF_READ,

Archive Download the corresponding diff file



interactive