Date:2016-03-30 00:03:48 (3 years 7 months ago)
Author:Stefan Schmidt
Commit:65912c2acbced58b4345deae94ddc763d99c7454
Message:atusb: fw: first round of re-factor after the integration of rzusb as a board

Move board specific parts in board_$NAME.c/h for a little abstraction and less
ifdef handling in common code.
Files: atusb/fw/Makefile (1 diff)
atusb/fw/board.c (3 diffs)
atusb/fw/board.h (2 diffs)
atusb/fw/board_atusb.c (1 diff)
atusb/fw/board_atusb.h (1 diff)
atusb/fw/board_rzusb.c (1 diff)
atusb/fw/board_rzusb.h (1 diff)

Change Details

atusb/fw/Makefile
4646BOOT_OBJS = boot.o board.o sernum.o spi.o flash.o dfu.o \
4747            dfu_common.o usb.o boot-atu2.o
4848
49
50ifeq ($(NAME),rzusb)
51OBJS += board_rzusb.o
52BOOT_OBJS += board_rzusb.o
53else
54OBJS += board_atusb.o
55BOOT_OBJS += board_atusb.o
56endif
57
58
4959vpath %.c usb/
5060
5161CFLAGS += -Iinclude -Iusb -I.
atusb/fw/board.c
2929
3030uint8_t board_sernum[42] = { 42, USB_DT_STRING };
3131
32
33static void set_clkm(void)
34{
35    /* switch CLKM to 8 MHz */
36
37    /*
38     * @@@ Note: Atmel advise against changing the external clock in
39     * mid-flight. We should therefore switch to the RC clock first, then
40     * crank up the external clock, and finally switch back to the external
41     * clock. The clock switching procedure is described in the ATmega32U2
42     * data sheet in secton 8.2.2.
43     */
44#ifdef ATUSB
45    spi_begin();
46    spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
47    spi_send(CLKM_CTRL_8MHz);
48    spi_end();
49#endif
50#ifdef RZUSB
51    spi_begin();
52    spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
53    spi_send(0x10);
54    spi_end();
55
56    /* TX_AUTO_CRC_ON, default disabled */
57    spi_begin();
58    spi_send(AT86RF230_REG_WRITE | 0x05);
59    spi_send(0x80);
60    spi_end();
61#endif
62}
63
64
6532void reset_rf(void)
6633{
6734    /* set up all the outputs; default port value is 0 */
...... 
12289}
12390
12491
125static void get_sernum(void)
92void get_sernum(void)
12693{
12794    uint8_t sig;
12895    uint8_t i;
...... 
133100        board_sernum[(i << 2)+4] = hex(sig & 0xf);
134101    }
135102}
136
137
138void board_init(void)
139{
140    /* Disable the watchdog timer */
141
142    MCUSR = 0; /* Remove override */
143    WDTCSR |= 1 << WDCE; /* Enable change */
144    WDTCSR = 1 << WDCE; /* Disable watchdog while still enabling
145                   change */
146
147    CLKPR = 1 << CLKPCE;
148#ifdef ATUSB
149    /* We start with a 1 MHz/8 clock. Disable the prescaler. */
150    CLKPR = 0;
151#endif
152#ifdef RZUSB
153    /* We start with a 16 MHz/8 clock. Put the prescaler to 2. */
154    CLKPR = 1 << CLKPS0;
155#endif
156
157    get_sernum();
158}
atusb/fw/board.h
1919#include <atusb/atusb.h>
2020
2121#ifdef ATUSB
22#define LED_PORT B
23#define LED_BIT 6
24#define nRST_RF_PORT C
25#define nRST_RF_BIT 7
26#define SLP_TR_PORT B
27#define SLP_TR_BIT 4
28
29#define SCLK_PORT D
30#define SCLK_BIT 5
31#define MOSI_PORT D
32#define MOSI_BIT 3
33
34#define MISO_PORT D
35#define MISO_BIT 2
36#define nSS_PORT D
37#define nSS_BIT 1
38#define IRQ_RF_PORT D
39#define IRQ_RF_BIT 0
40
41#define SPI_WAIT_DONE() while (!(UCSR1A & 1 << RXC1))
42#define SPI_DATA UDR1
43
22#include "board_atusb.h"
4423#endif
4524#ifdef RZUSB
46#define LED_PORT D
47#define LED_BIT 7
48#define nRST_RF_PORT B
49#define nRST_RF_BIT 5
50#define SLP_TR_PORT B
51#define SLP_TR_BIT 4
52
53#define SCLK_PORT B
54#define SCLK_BIT 1
55#define MOSI_PORT B
56#define MOSI_BIT 2
57
58#define MISO_PORT B
59#define MISO_BIT 3
60#define nSS_PORT B
61#define nSS_BIT 0
62#define IRQ_RF_PORT D
63#define IRQ_RF_BIT 4
64
65#define SPI_WAIT_DONE() while ((SPSR & (1 << SPIF)) == 0)
66#define SPI_DATA SPDR
67
25#include "board_rzusb.h"
6826#endif
6927
7028#define SET_2(p, b) PORT##p |= 1 << (b)
...... 
12179bool gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res);
12280void gpio_cleanup(void);
12381
124void board_init(void);
82void get_sernum(void);
83
12584void board_app_init(void);
12685
12786#endif /* !BOARD_H */
atusb/fw/board_atusb.c
1/*
2 * fw/board_atusb.c - ATUSB Board-specific functions (for boot loader and application)
3 *
4 * Written 2016 by Stefan Schmidt
5 * 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
29void set_clkm(void)
30{
31    /* switch CLKM to 8 MHz */
32
33    /*
34     * @@@ Note: Atmel advise against changing the external clock in
35     * mid-flight. We should therefore switch to the RC clock first, then
36     * crank up the external clock, and finally switch back to the external
37     * clock. The clock switching procedure is described in the ATmega32U2
38     * data sheet in secton 8.2.2.
39     */
40    spi_begin();
41    spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
42    spi_send(CLKM_CTRL_8MHz);
43    spi_end();
44}
45
46void board_init(void)
47{
48    /* Disable the watchdog timer */
49
50    MCUSR = 0; /* Remove override */
51    WDTCSR |= 1 << WDCE; /* Enable change */
52    WDTCSR = 1 << WDCE; /* Disable watchdog while still enabling
53                   change */
54
55    CLKPR = 1 << CLKPCE;
56    /* We start with a 1 MHz/8 clock. Disable the prescaler. */
57    CLKPR = 0;
58
59    get_sernum();
60}
atusb/fw/board_atusb.h
1/*
2 * fw/board_atusb.h - ATUSB Board-specific functions and definitions
3 *
4 * Written 2016 by Stefan Schmidt
5 * 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_ATUSB_H
14#define BOARD_ATUSB_H
15
16#include <stdbool.h>
17#include <stdint.h>
18
19#define LED_PORT B
20#define LED_BIT 6
21#define nRST_RF_PORT C
22#define nRST_RF_BIT 7
23#define SLP_TR_PORT B
24#define SLP_TR_BIT 4
25
26#define SCLK_PORT D
27#define SCLK_BIT 5
28#define MOSI_PORT D
29#define MOSI_BIT 3
30
31#define MISO_PORT D
32#define MISO_BIT 2
33#define nSS_PORT D
34#define nSS_BIT 1
35#define IRQ_RF_PORT D
36#define IRQ_RF_BIT 0
37
38#define SPI_WAIT_DONE() while (!(UCSR1A & 1 << RXC1))
39#define SPI_DATA UDR1
40
41void set_clkm(void);
42void board_init(void);
43
44#endif /* !BOARD_H */
atusb/fw/board_rzusb.c
1/*
2 * fw/board_rzusb.c - RZUSB Board-specific functions (for boot loader and application)
3 *
4 * Written 2016 by Stefan Schmidt
5 * 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
29void set_clkm(void)
30{
31    /* switch CLKM to 8 MHz */
32
33    /*
34     * @@@ Note: Atmel advise against changing the external clock in
35     * mid-flight. We should therefore switch to the RC clock first, then
36     * crank up the external clock, and finally switch back to the external
37     * clock. The clock switching procedure is described in the ATmega32U2
38     * data sheet in secton 8.2.2.
39     */
40    spi_begin();
41    spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
42    spi_send(0x10);
43    spi_end();
44
45    /* TX_AUTO_CRC_ON, default disabled */
46    spi_begin();
47    spi_send(AT86RF230_REG_WRITE | 0x05);
48    spi_send(0x80);
49    spi_end();
50}
51
52void board_init(void)
53{
54    /* Disable the watchdog timer */
55
56    MCUSR = 0; /* Remove override */
57    WDTCSR |= 1 << WDCE; /* Enable change */
58    WDTCSR = 1 << WDCE; /* Disable watchdog while still enabling
59                   change */
60
61    CLKPR = 1 << CLKPCE;
62    /* We start with a 16 MHz/8 clock. Put the prescaler to 2. */
63    CLKPR = 1 << CLKPS0;
64
65    get_sernum();
66}
atusb/fw/board_rzusb.h
1/*
2 * fw/board_rzusb.h - RZUSB Board-specific functions and definitions
3 *
4 * Written 2016 by Stefan Schmidt
5 * 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_RZUSB_H
14#define BOARD_RZUSB_H
15
16#include <stdbool.h>
17#include <stdint.h>
18
19#define LED_PORT D
20#define LED_BIT 7
21#define nRST_RF_PORT B
22#define nRST_RF_BIT 5
23#define SLP_TR_PORT B
24#define SLP_TR_BIT 4
25
26#define SCLK_PORT B
27#define SCLK_BIT 1
28#define MOSI_PORT B
29#define MOSI_BIT 2
30
31#define MISO_PORT B
32#define MISO_BIT 3
33#define nSS_PORT B
34#define nSS_BIT 0
35#define IRQ_RF_PORT D
36#define IRQ_RF_BIT 4
37
38#define SPI_WAIT_DONE() while ((SPSR & (1 << SPIF)) == 0)
39#define SPI_DATA SPDR
40
41void set_clkm(void);
42void board_init(void);
43
44#endif /* !BOARD_H */

Archive Download the corresponding diff file



interactive