IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Commit Details
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 | ||
---|---|---|
36 | 36 | usb_init(); |
37 | 37 | ep0_init(); |
38 | 38 | |
39 | while (1) | |
39 | while (1) { | |
40 | 40 | usb_poll(); |
41 | timer_poll(); | |
42 | } | |
41 | 43 | } |
atusb/fw/board.c | ||
---|---|---|
29 | 29 | uint8_t board_sernum[42] = { 42, USB_DT_STRING }; |
30 | 30 | |
31 | 31 | |
32 | static uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */ | |
33 | ||
34 | ||
32 | 35 | static void set_clkm(void) |
33 | 36 | { |
34 | 37 | /* switch CLKM to 8 MHz */ |
... | ... | |
99 | 102 | } |
100 | 103 | |
101 | 104 | |
105 | void timer_poll(void) | |
106 | { | |
107 | if (!(TIFR1 & (1 << TOV1))) | |
108 | return; | |
109 | TIFR1 = 1 << TOV1; | |
110 | timer_h++; | |
111 | } | |
112 | ||
113 | ||
114 | uint64_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 | ||
102 | 135 | static char hex(uint8_t nibble) |
103 | 136 | { |
104 | 137 | return nibble < 10 ? '0'+nibble : 'a'+nibble-10; |
... | ... | |
138 | 171 | OUT(nRST_RF); /* this also resets the transceiver */ |
139 | 172 | OUT(SLP_TR); |
140 | 173 | |
174 | /* configure timer 1 as a free-running CLK counter */ | |
175 | ||
176 | TCCR1A = 0; | |
177 | TCCR1B = 1 << CS10; | |
178 | ||
141 | 179 | get_sernum(); |
142 | 180 | } |
atusb/fw/board.h | ||
---|---|---|
74 | 74 | void led(int on); |
75 | 75 | void panic(void); |
76 | 76 | |
77 | void timer_poll(void); | |
78 | uint64_t timer_read(void); | |
79 | ||
77 | 80 | void board_init(void); |
78 | 81 | |
79 | 82 | #endif /* !BOARD_H */ |
atusb/fw/ep0.c | ||
---|---|---|
12 | 12 | |
13 | 13 | |
14 | 14 | #include <stdint.h> |
15 | #include <string.h> | |
15 | 16 | |
16 | 17 | #include <avr/io.h> |
17 | 18 | |
... | ... | |
57 | 58 | { |
58 | 59 | unsigned tmp; |
59 | 60 | uint8_t i; |
61 | uint64_t tmp64; | |
60 | 62 | |
61 | 63 | switch (setup->bmRequestType | setup->bRequest << 8) { |
62 | 64 | case ATUSB_FROM_DEV(ATUSB_ID): |
... | ... | |
101 | 103 | usb_send(&eps[0], buf, 1, NULL, NULL); |
102 | 104 | return 1; |
103 | 105 | |
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 | ||
104 | 116 | case ATUSB_TO_DEV(ATUSB_REG_WRITE): |
105 | 117 | debug("ATUSB_REG_WRITE\n"); |
106 | 118 | spi_begin(); |
atusb/fw/include/atusb/ep0.h | ||
---|---|---|
24 | 24 | * host-> ATUSB_RF_RESET - - 0 |
25 | 25 | * ->host ATUSB_POLL_INT - - 1 |
26 | 26 | * host-> ATUSB_TEST - - 0 |
27 | * ->host ATUSB_TIMER - - #bytes (6) | |
27 | 28 | * |
28 | 29 | * host-> ATUSB_REG_WRITE value addr 0 |
29 | 30 | * ->host ATUSB_REG_READ - addr 1 |
... | ... | |
64 | 65 | |
65 | 66 | |
66 | 67 | enum atspi_requests { |
67 | ATUSB_ID = 0x00, | |
68 | ATUSB_ID = 0x00, /* system status/control grp */ | |
68 | 69 | ATUSB_BUILD, |
69 | 70 | ATUSB_RESET, |
70 | ATUSB_RF_RESET = 0x10, | |
71 | ATUSB_RF_RESET = 0x10, /* debug/test group */ | |
71 | 72 | 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 */ | |
74 | 76 | ATUSB_REG_READ, |
75 | 77 | ATUSB_BUF_WRITE, |
76 | 78 | ATUSB_BUF_READ, |