Date:2011-06-09 17:48:44 (9 years 2 months ago)
Author:Werner Almesberger
Commit:e678401ac35ea9bcb1a5075091bd5454d8100f27
Message:atusb/fw/: split board functions into app-only/shared part (boot overlflowed)

- board.c (timer_h, reset_cpu, read_irq, slp_tr, timer_poll,
timer_read, gpio): moved to new file board_app.c
- Makefile (OBJS): added board_app.o
Files: atusb/fw/Makefile (1 diff)
atusb/fw/board.c (5 diffs)
atusb/fw/board_app.c (1 diff)

Change Details

atusb/fw/Makefile
3131USB_ID = 20b7:1540
3232
3333USB_OBJS = usb.o atu2.o
34OBJS = atusb.o board.o sernum.o spi.o descr.o ep0.o $(USB_OBJS)
34OBJS = atusb.o board.o board_app.o sernum.o spi.o descr.o ep0.o $(USB_OBJS)
3535BOOT_OBJS = boot.o board.o sernum.o spi.o flash.o dfu.o $(USB_OBJS)
3636
3737vpath %.c usb/
atusb/fw/board.c
11/*
2 * fw/board.c - Board-specific functions
2 * fw/board.c - Board-specific functions (for boot loader and application)
33 *
44 * Written 2011 by Werner Almesberger
55 * Copyright 2011 Werner Almesberger
...... 
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
3532static void set_clkm(void)
3633{
3734    /* switch CLKM to 8 MHz */
...... 
8481}
8582
8683
87void reset_cpu(void)
88{
89    WDTCSR = 1 << WDE;
90}
91
92
93uint8_t read_irq(void)
94{
95    return PIN(IRQ_RF);
96}
97
98
99void slp_tr(void)
100{
101    SET(SLP_TR);
102    CLR(SLP_TR);
103}
104
105
10684void led(int on)
10785{
10886    if (on)
...... 
124102}
125103
126104
127void timer_poll(void)
128{
129    if (!(TIFR1 & (1 << TOV1)))
130        return;
131    TIFR1 = 1 << TOV1;
132    timer_h++;
133}
134
135
136uint64_t timer_read(void)
137{
138    uint32_t high;
139    uint8_t low, mid;
140
141    do {
142        timer_poll();
143        high = timer_h;
144        low = TCNT1L;
145        mid = TCNT1H;
146    }
147    while (TIFR1 & (1 << TOV1));
148
149    /*
150     * We need all these casts because the intermediate results are handled
151     * as if they were signed and thus get sign-expanded. Sounds wrong-ish.
152     */
153    return (uint64_t) high << 16 | (uint64_t) mid << 8 | (uint64_t) low;
154}
155
156
157105static char hex(uint8_t nibble)
158106{
159107    return nibble < 10 ? '0'+nibble : 'a'+nibble-10;
...... 
173121}
174122
175123
176int gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res)
177{
178    switch (port) {
179    case 1:
180        DDRB = (DDRB & ~mask) | dir;
181        PORTB = (PORTB & ~mask) | data;
182        break;
183    case 2:
184        DDRC = (DDRC & ~mask) | dir;
185        PORTC = (PORTC & ~mask) | data;
186        break;
187    case 3:
188        DDRD = (DDRD & ~mask) | dir;
189        PORTD = (PORTD & ~mask) | data;
190        break;
191    default:
192        return 0;
193    }
194
195    /* disable the UART so that we can meddle with these pins as well. */
196    UCSR1B = 0;
197    _delay_ms(1);
198
199    switch (port) {
200    case 1:
201        res[0] = PINB;
202        res[1] = PORTB;
203        res[2] = DDRB;
204        break;
205    case 2:
206        res[0] = PINC;
207        res[1] = PORTC;
208        res[2] = DDRC;
209        break;
210    case 3:
211        res[0] = PIND;
212        res[1] = PORTD;
213        res[2] = DDRD;
214        break;
215    }
216
217    spi_init();
218
219    return 1;
220}
221
222
223124void board_init(void)
224125{
225126    /* Disable the watchdog timer */
atusb/fw/board_app.c
1/*
2 * fw/board_app.c - Board-specific functions (for the application)
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
14#include <stdint.h>
15
16#include <avr/io.h>
17#include <avr/interrupt.h>
18#include <avr/boot.h>
19
20#define F_CPU 8000000UL
21#include <util/delay.h>
22
23#include "usb.h"
24#include "at86rf230.h"
25#include "board.h"
26#include "spi.h"
27
28
29static uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */
30
31
32void reset_cpu(void)
33{
34    WDTCSR = 1 << WDE;
35}
36
37
38uint8_t read_irq(void)
39{
40    return PIN(IRQ_RF);
41}
42
43
44void slp_tr(void)
45{
46    SET(SLP_TR);
47    CLR(SLP_TR);
48}
49
50
51void timer_poll(void)
52{
53    if (!(TIFR1 & (1 << TOV1)))
54        return;
55    TIFR1 = 1 << TOV1;
56    timer_h++;
57}
58
59
60uint64_t timer_read(void)
61{
62    uint32_t high;
63    uint8_t low, mid;
64
65    do {
66        timer_poll();
67        high = timer_h;
68        low = TCNT1L;
69        mid = TCNT1H;
70    }
71    while (TIFR1 & (1 << TOV1));
72
73    /*
74     * We need all these casts because the intermediate results are handled
75     * as if they were signed and thus get sign-expanded. Sounds wrong-ish.
76     */
77    return (uint64_t) high << 16 | (uint64_t) mid << 8 | (uint64_t) low;
78}
79
80
81int gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res)
82{
83    switch (port) {
84    case 1:
85        DDRB = (DDRB & ~mask) | dir;
86        PORTB = (PORTB & ~mask) | data;
87        break;
88    case 2:
89        DDRC = (DDRC & ~mask) | dir;
90        PORTC = (PORTC & ~mask) | data;
91        break;
92    case 3:
93        DDRD = (DDRD & ~mask) | dir;
94        PORTD = (PORTD & ~mask) | data;
95        break;
96    default:
97        return 0;
98    }
99
100    /* disable the UART so that we can meddle with these pins as well. */
101    UCSR1B = 0;
102    _delay_ms(1);
103
104    switch (port) {
105    case 1:
106        res[0] = PINB;
107        res[1] = PORTB;
108        res[2] = DDRB;
109        break;
110    case 2:
111        res[0] = PINC;
112        res[1] = PORTC;
113        res[2] = DDRC;
114        break;
115    case 3:
116        res[0] = PIND;
117        res[1] = PORTD;
118        res[2] = DDRD;
119        break;
120    }
121
122    spi_init();
123
124    return 1;
125}

Archive Download the corresponding diff file



interactive