Date:2017-09-11 13:58:33 (6 years 2 months ago)
Author:Josef Filzmaier
Commit:ea23c905d32bfd2478915021476f9ede2ee014b8
Message:atusb/fw: Introduction of a new board named HULUSB

The Busware HUL v1.1 dongle is a product very similar
to the rzusb dongle but with the at86rf212 instead of
the at86rf230 transceiver.

Some code refactoring has been made in order to better
support multiple hardware targets. This includes:

The reset_rf functions are now in the board specific files.
The led functions are now in the board specific files.
The register read/write functions are moved from mac.c to the generic
board.c file as they are used by functions like reset_rf that are
not within the mac.c file. Also the subreg_read and subreg_write
functions were introduced for convenience.
The function to change state is now also in board.c.

The hardware types are moved into the atusb.h file (which is always
synchrornized with the linux atusb driver) because they are now used
by the driver to identify and configure the hardware.

Within the makefile a new target name is specified called: hulusb

Signed-off-by: Josef Filzmaier <j.filzmaier@gmx.at>
Files: atusb/fw/Makefile (2 diffs)
atusb/fw/board.c (1 diff)
atusb/fw/board.h (2 diffs)
atusb/fw/board_app.c (1 diff)
atusb/fw/board_atusb.c (1 diff)
atusb/fw/board_hulusb.c (1 diff)
atusb/fw/board_hulusb.h (1 diff)
atusb/fw/board_rzusb.c (1 diff)
atusb/fw/ep0.c (1 diff)
atusb/fw/include/atusb/atusb.h (1 diff)
atusb/fw/include/atusb/ep0.h (1 diff)
atusb/fw/mac.c (3 diffs)

Change Details

atusb/fw/Makefile
2626ifeq ($(NAME),rzusb)
2727CHIP=at90usb1287
2828CFLAGS += -DRZUSB -DAT86RF230
29else ifeq ($(NAME),hulusb)
30CHIP=at90usb1287
31CFLAGS += -DHULUSB -DAT86RF212
2932else
3033CHIP=atmega32u2
3134CFLAGS += -DATUSB -DAT86RF231
...... 
5861ifeq ($(NAME),rzusb)
5962OBJS += board_rzusb.o
6063BOOT_OBJS += board_rzusb.o
64else ifeq ($(NAME),hulusb)
65OBJS += board_hulusb.o
66BOOT_OBJS += board_hulusb.o
6167else
6268OBJS += board_atusb.o
6369BOOT_OBJS += board_atusb.o
atusb/fw/board.c
2929
3030uint8_t board_sernum[42] = { 42, USB_DT_STRING };
3131
32void reset_rf(void)
32/* ----- Register access --------------------------------------------------- */
33
34void change_state(uint8_t new)
3335{
34    /* set up all the outputs; default port value is 0 */
36    while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) ==
37        TRX_STATUS_TRANSITION);
38    reg_write(REG_TRX_STATE, new);
39}
3540
36    DDRB = 0;
37    DDRC = 0;
38    DDRD = 0;
39    PORTB = 0;
40    PORTC = 0;
41    PORTD = 0;
4241
43    OUT(LED);
44    OUT(nRST_RF); /* this also resets the transceiver */
45    OUT(SLP_TR);
42uint8_t reg_read(uint8_t reg)
43{
44    uint8_t value;
4645
47    spi_init();
46    spi_begin();
47    spi_send(AT86RF230_REG_READ | reg);
48    value = spi_recv();
49    spi_end();
4850
49    /* AT86RF231 data sheet, 12.4.13, reset pulse width: 625 ns (min) */
51    return value;
52}
5053
51    CLR(nRST_RF);
52    _delay_us(2);
53    SET(nRST_RF);
5454
55    /* 12.4.14: SPI access latency after reset: 625 ns (min) */
55uint8_t subreg_read(uint8_t address, uint8_t mask, uint8_t position)
56{
57    /* Read current register value and mask out subregister. */
58    uint8_t register_value = reg_read(address);
59    register_value &= mask;
60    register_value >>= position; /* Align subregister value. */
5661
57    _delay_us(2);
62    return register_value;
63}
5864
59    /* we must restore TRX_CTRL_0 after each reset (9.6.4) */
6065
61    set_clkm();
66void reg_write(uint8_t reg, uint8_t value)
67{
68    spi_begin();
69    spi_send(AT86RF230_REG_WRITE | reg);
70    spi_send(value);
71    spi_end();
6272}
6373
6474
65void led(bool on)
75void subreg_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value)
6676{
67    if (on)
68        SET(LED);
69    else
70        CLR(LED);
77    /* Read current register value and mask area outside the subregister. */
78    uint8_t register_value = reg_read(address);
79    register_value &= ~mask;
80
81    /* Start preparing the new subregister value. shift in place and mask. */
82    value <<= position;
83    value &= mask;
84
85    value |= register_value; /* Set the new subregister value. */
86
87    /* Write the modified register value. */
88    reg_write(address, value);
7189}
7290
7391
atusb/fw/board.h
2424#ifdef RZUSB
2525#include "board_rzusb.h"
2626#endif
27#ifdef HULUSB
28#include "board_hulusb.h"
29#endif
2730
2831#define SET_2(p, b) PORT##p |= 1 << (b)
2932#define CLR_2(p, b) PORT##p &= ~(1 << (b))
...... 
8386
8487void board_app_init(void);
8588
89uint8_t reg_read(uint8_t reg);
90uint8_t subreg_read(uint8_t address, uint8_t mask, uint8_t position);
91void reg_write(uint8_t reg, uint8_t value);
92void subreg_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value);
93void change_state(uint8_t new);
94
8695#endif /* !BOARD_H */
atusb/fw/board_app.c
154154
155155uint8_t irq_serial;
156156
157#ifdef ATUSB
157#if defined(ATUSB) || defined(HULUSB)
158158ISR(INT0_vect)
159159#endif
160160#ifdef RZUSB
atusb/fw/board_atusb.c
2929
3030static bool spi_initialized = 0;
3131
32void reset_rf(void)
33{
34    /* set up all the outputs; default port value is 0 */
35
36    DDRB = 0;
37    DDRC = 0;
38    DDRD = 0;
39    PORTB = 0;
40    PORTC = 0;
41    PORTD = 0;
42
43    OUT(LED);
44    OUT(nRST_RF); /* this also resets the transceiver */
45    OUT(SLP_TR);
46
47    spi_init();
48
49    /* AT86RF231 data sheet, 12.4.13, reset pulse width: 625 ns (min) */
50
51    CLR(nRST_RF);
52    _delay_us(2);
53    SET(nRST_RF);
54
55    /* 12.4.14: SPI access latency after reset: 625 ns (min) */
56
57    _delay_us(2);
58
59    /* we must restore TRX_CTRL_0 after each reset (9.6.4) */
60
61    set_clkm();
62}
63
64void led(bool on)
65{
66    if (on)
67        SET(LED);
68    else
69        CLR(LED);
70}
71
3272void set_clkm(void)
3373{
3474    /* switch CLKM to 8 MHz */
atusb/fw/board_hulusb.c
1/*
2 * fw/board_hulusb.c - Busware HUL Board-specific functions (for boot loader and application)
3 *
4 * Written 2017 by Filzmaier Josef
5 * Based on fw/board_rzusb written and Copyright 2016 Stefan Schmidt
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 <stdbool.h>
15#include <stdint.h>
16
17#include <avr/io.h>
18#include <avr/interrupt.h>
19#include <avr/boot.h>
20
21#define F_CPU 8000000UL
22#include <util/delay.h>
23
24#include "usb.h"
25#include "at86rf230.h"
26#include "board.h"
27#include "spi.h"
28#include "usb/usb.h"
29
30static bool spi_initialized = 0;
31
32void reset_rf(void)
33{
34    /* set up all the outputs; default port value is 0 */
35
36    DDRB = 0;
37    DDRC = 0;
38    DDRD = 0;
39    PORTB = 0;
40    PORTC = 0;
41    PORTD = 0;
42
43    OUT(LED_RED);
44    OUT(LED_GREEN);
45    SET(LED_RED); /* Leds are active low on HULUSB board */
46    CLR(LED_GREEN); /* Green Led indicates the dongle is running */
47    OUT(nRST_RF); /* this also resets the transceiver */
48    OUT(SLP_TR);
49
50    spi_init();
51
52    /* AT86RF212 data sheet, Appendix B, p166 Power-On Reset procedure */
53    /*-----------------------------------------------------------------*/
54    CLR(SLP_TR);
55    SET(nRST_RF);
56    SET(nSS);
57    _delay_us(400);
58
59    CLR(nRST_RF);
60    _delay_us(2);
61    SET(nRST_RF);
62
63    /* 5.1.4.5: Wait t10: 625 ns (min) */
64
65    _delay_us(2);
66
67    reg_write(REG_TRX_CTRL_0, 0x19);
68
69    change_state(TRX_CMD_FORCE_TRX_OFF);
70    /*-----------------------------------------------------------------*/
71
72    /* we must restore TRX_CTRL_0 after each reset (7.7.4) */
73
74    set_clkm();
75}
76
77void led_red(bool on) {
78    if (on)
79        CLR(LED_RED);
80    else
81        SET(LED_RED);
82}
83
84void led_green(bool on) {
85    if (on)
86        CLR(LED_GREEN);
87    else
88        SET(LED_GREEN);
89}
90
91void led(bool on)
92{
93    led_red(on);
94}
95
96void set_clkm(void)
97{
98    /* CLKM is not connected on BUSWARE HUL and therefore it is running in
99     * async mode. */
100    reg_write(REG_TRX_CTRL_0, 0x00);
101
102    /* TX_AUTO_CRC_ON, default disabled */
103    subreg_write(SR_TX_AUTO_CRC_ON, 1);
104}
105
106void board_init(void)
107{
108    /* Disable the watchdog timer */
109
110    MCUSR = 0; /* Remove override */
111    WDTCSR |= 1 << WDCE; /* Enable change */
112    WDTCSR = 1 << WDCE; /* Disable watchdog while still enabling
113    change */
114
115    CLKPR = 1 << CLKPCE;
116    /* We start with a 16 MHz/8 clock. Put the prescaler to 2. */
117    CLKPR = 1 << CLKPS0;
118
119    get_sernum();
120}
121
122void spi_begin(void)
123{
124    if (!spi_initialized)
125        spi_init();
126    CLR(nSS);
127}
128
129void spi_off(void)
130{
131    spi_initialized = 0;
132    SPCR &= ~(1 << SPE);
133}
134
135void spi_init(void)
136{
137    SET(nSS);
138    OUT(SCLK);
139    OUT(MOSI);
140    OUT(nSS);
141    IN(MISO);
142
143    SPCR = (1 << SPE) | (1 << MSTR);
144    SPSR = (1 << SPI2X);
145
146    spi_initialized = 1;
147}
148
149void usb_init(void)
150{
151    USBCON |= 1 << FRZCLK; /* freeze the clock */
152
153    /* enable the PLL and wait for it to lock */
154    /* TODO sheet page 50 For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x. */
155    /* FOR 8 XTAL Mhz only!!! */
156    PLLCSR = ((1 << PLLP1) | (1 << PLLP0));
157    PLLCSR |= 1 << PLLE;
158    while (!(PLLCSR & (1 << PLOCK)));
159
160    UHWCON |= (1 << UVREGE);
161
162    USBCON &= ~((1 << USBE) | (1 << OTGPADE)); /* reset the controller */
163    USBCON |= ((1 << USBE) | (1 << OTGPADE));
164
165    USBCON &= ~(1 << FRZCLK); /* thaw the clock */
166
167    UDCON &= ~(1 << DETACH); /* attach the pull-up */
168    UDIEN = 1 << EORSTE; /* enable device interrupts */
169    // UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */
170
171    ep_init();
172}
173
174void board_app_init(void)
175{
176    /* enable INT0, trigger on rising edge */
177    EICRA = 1 << ISC01 | 1 << ISC00;
178    EIMSK = 1 << INT0;
179}
atusb/fw/board_hulusb.h
1/*
2 * fw/board_hulusb.h - Busware HUL Board-specific functions (for boot loader and application)
3 *
4 * Written 2017 by Filzmaier Josef
5 * Based on fw/board_rzusb written and Copyright 2016 Stefan Schmidt
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 BOARD_HULUSB_H
14#define BOARD_HULUSB_H
15
16#include <stdbool.h>
17#include <stdint.h>
18
19#define LED_RED_PORT A
20#define LED_GREEN_PORT A
21#define LED_RED_BIT 3
22#define LED_GREEN_BIT 4
23#define LED_PORT LED_RED_PORT
24#define LED_BIT LED_RED_BIT
25
26#define nRST_RF_PORT B
27#define nRST_RF_BIT 5
28#define SLP_TR_PORT B
29#define SLP_TR_BIT 4
30
31#define SCLK_PORT B
32#define SCLK_BIT 1
33#define MOSI_PORT B
34#define MOSI_BIT 2
35
36#define MISO_PORT B
37#define MISO_BIT 3
38#define nSS_PORT B
39#define nSS_BIT 0
40#define IRQ_RF_PORT D
41#define IRQ_RF_BIT 4
42
43#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
44#define SR_CHANNEL 0x08, 0x1f, 0
45
46#define RG_CC_CTRL_1 (0x14)
47
48#define SPI_WAIT_DONE() while ((SPSR & (1 << SPIF)) == 0)
49#define SPI_DATA SPDR
50
51void set_clkm(void);
52void board_init(void);
53
54void led_red(bool on);
55void led_green(bool on);
56
57void spi_begin(void);
58void spi_off(void);
59void spi_init(void);
60
61#ifdef DEBUG
62void printStatus(void);
63#define PRINT_STATUS() printStatus()
64#endif
65
66#endif /* !BOARD_HULUSB_H */
atusb/fw/board_rzusb.c
2929
3030static bool spi_initialized = 0;
3131
32void reset_rf(void)
33{
34    /* set up all the outputs; default port value is 0 */
35
36    DDRB = 0;
37    DDRC = 0;
38    DDRD = 0;
39    PORTB = 0;
40    PORTC = 0;
41    PORTD = 0;
42
43    OUT(LED);
44    OUT(nRST_RF); /* this also resets the transceiver */
45    OUT(SLP_TR);
46
47    spi_init();
48
49    /* AT86RF231 data sheet, 12.4.13, reset pulse width: 625 ns (min) */
50
51    CLR(nRST_RF);
52    _delay_us(2);
53    SET(nRST_RF);
54
55    /* 12.4.14: SPI access latency after reset: 625 ns (min) */
56
57    _delay_us(2);
58
59    /* we must restore TRX_CTRL_0 after each reset (9.6.4) */
60
61    set_clkm();
62}
63
64void led(bool on)
65{
66    if (on)
67        SET(LED);
68    else
69        CLR(LED);
70}
71
3272void set_clkm(void)
3373{
3474    /* switch CLKM to 8 MHz */
atusb/fw/ep0.c
3838#include "mac.h"
3939
4040#ifdef ATUSB
41#define HW_TYPE HW_TYPE_110131
41#define HW_TYPE ATUSB_HW_TYPE_110131
4242#endif
4343
4444#ifdef RZUSB
45#define HW_TYPE HW_TYPE_RZUSB
45#define HW_TYPE ATUSB_HW_TYPE_RZUSB
46#endif
47
48#ifdef HULUSB
49#define HW_TYPE ATUSB_HW_TYPE_HULUSB
4650#endif
4751
4852#ifdef DEBUG
atusb/fw/include/atusb/atusb.h
5050    ATUSB_EUI64_READ,
5151};
5252
53enum {
54    ATUSB_HW_TYPE_100813, /* 2010-08-13 */
55    ATUSB_HW_TYPE_101216, /* 2010-12-16 */
56    ATUSB_HW_TYPE_110131, /* 2011-01-31, ATmega32U2-based */
57    ATUSB_HW_TYPE_RZUSB, /* Atmel Raven USB dongle with at86rf230 */
58    ATUSB_HW_TYPE_HULUSB, /* Busware HUL USB dongle with at86rf212 */
59};
60
5361/*
5462 * Direction bRequest wValue wIndex wLength
5563 *
atusb/fw/include/atusb/ep0.h
3232#define EP0ATUSB_MAJOR 0 /* EP0 protocol, major revision */
3333#define EP0ATUSB_MINOR 3 /* EP0 protocol, minor revision */
3434
35#define HW_TYPE_100813 0 /* 2010-08-13 */
36#define HW_TYPE_101216 1 /* 2010-12-16 */
37#define HW_TYPE_110131 2 /* 2011-01-31, ATmega32U2-based */
38#define HW_TYPE_RZUSB 3 /* Atmel Raven USB dongle with at86rf230 */
39
4035
4136/*
4237 * bmRequestType:
atusb/fw/mac.c
4747}
4848
4949
50/* ----- Register access --------------------------------------------------- */
51
52
53static uint8_t reg_read(uint8_t reg)
54{
55    uint8_t value;
56
57    spi_begin();
58    spi_send(AT86RF230_REG_READ | reg);
59    value = spi_recv();
60    spi_end();
61
62    return value;
63}
64
65
66static void reg_write(uint8_t reg, uint8_t value)
67{
68    spi_begin();
69    spi_send(AT86RF230_REG_WRITE | reg);
70    spi_send(value);
71    spi_end();
72}
73
74
7550/* ----- Interrupt handling ------------------------------------------------ */
7651
7752
...... 
10176    usb_next();
10277}
10378
104static void change_state(uint8_t new)
105{
106    while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) ==
107        TRX_STATUS_TRANSITION);
108    reg_write(REG_TRX_STATE, new);
109}
110
11179static void rx_done(void *user)
11280{
11381    led(0);
...... 
223191     */
224192    reg_write(REG_TRX_STATE, TRX_CMD_PLL_ON);
225193#endif
194#ifdef AT86RF212
195    /*
196    * We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new
197    * reception may have begun while we were still working on the previous
198    * one.
199    */
200    reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON);
201#endif
226202
227203    handle_irq();
228204

Archive Download the corresponding diff file



interactive