Root/atusb/fw/board.c

1/*
2 * fw/board.c - Board-specific functions (for boot loader and application)
3 *
4 * Written 2011, 2013 by Werner Almesberger
5 * Copyright 2011, 2013 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 <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
29
30uint8_t board_sernum[42] = { 42, USB_DT_STRING };
31
32/* ----- Register access --------------------------------------------------- */
33
34void change_state(uint8_t new)
35{
36    while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) ==
37        TRX_STATUS_TRANSITION);
38    reg_write(REG_TRX_STATE, new);
39}
40
41
42uint8_t reg_read(uint8_t reg)
43{
44    uint8_t value;
45
46    spi_begin();
47    spi_send(AT86RF230_REG_READ | reg);
48    value = spi_recv();
49    spi_end();
50
51    return value;
52}
53
54
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. */
61
62    return register_value;
63}
64
65
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();
72}
73
74
75void subreg_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value)
76{
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);
89}
90
91
92void panic(void)
93{
94    cli();
95    while (1) {
96        SET(LED);
97        _delay_ms(100);
98        CLR(LED);
99        _delay_ms(100);
100    }
101}
102
103
104static char hex(uint8_t nibble)
105{
106    return nibble < 10 ? '0'+nibble : 'a'+nibble-10;
107}
108
109
110void get_sernum(void)
111{
112    uint8_t sig;
113    uint8_t i;
114
115    for (i = 0; i != 10; i++) {
116        sig = boot_signature_byte_get(i+0xe);
117        board_sernum[(i << 2)+2] = hex(sig >> 4);
118        board_sernum[(i << 2)+4] = hex(sig & 0xf);
119    }
120}
121

Archive Download this file



interactive