setting extended address during init replaced with setting short address
[mrf-link.git] / fw / mrf / mrf_impl.h
blobf2ae70f5077a2666d38c30ead4b373e1f23453d4
1 /*
2 * Copyright 2010 Grygoriy Fuchedzhy <grygoriy.fuchedzhy@gmail.com>
4 * This file is part of mrf-link.
6 * mrf-link is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * mrf-link is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with mrf-link. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef __MRF_IMPL__
21 #define __MRF_IMPL__
23 #include <mrf/mrf.h>
25 #include <util/delay.h>
26 #include <avr/spi.h>
27 #include <avr/interrupt.h>
29 uint16_t mrf_short_addr = MRF_SHORT_ADDR;
30 uint16_t mrf_pan_id = MRF_PAN_ID;
32 // set given short register to given value via spi
33 void mrf_write_short_addr(uint8_t addr, uint8_t val)
35 uint8_t sreg = SREG;
36 cli();
37 spi_select();
38 spi_shift_byte(0x1 | (addr << 1));
39 spi_shift_byte(val);
40 spi_deselect();
41 SREG = sreg;
44 // get and return value of given short register via spi
45 uint8_t mrf_read_short_addr(uint8_t addr)
47 uint8_t sreg = SREG;
48 cli();
49 spi_select();
50 spi_shift_byte(addr << 1);
51 uint8_t res = spi_shift_byte(0x00);
52 spi_deselect();
53 SREG = sreg;
54 return res;
57 // set given long register to given value via spi
58 void mrf_write_long_addr(uint16_t addr, uint8_t val)
60 uint8_t sreg = SREG;
61 cli();
62 spi_select();
63 spi_shift_byte(0x80 | (addr >> 3));
64 spi_shift_byte(0x10 | (addr << 5));
65 spi_shift_byte(val);
66 spi_deselect();
67 SREG = sreg;
70 // get and return value of given long register via spi
71 uint8_t mrf_read_long_addr(uint16_t addr)
73 uint8_t sreg = SREG;
74 cli();
75 spi_select();
76 spi_shift_byte(0x80 | (addr >> 3));
77 spi_shift_byte(addr << 5);
78 uint8_t res = spi_shift_byte(0x00);
79 spi_deselect();
80 SREG = sreg;
81 return res;
84 // set channel, ch must be: 0x0 - 0xF
85 void mrf_set_channel(uint8_t ch)
87 // 0x2 - recommended value
88 mrf_set_reg(MRF_R_RFCON0, (ch<<4) | 0x2);
89 // reset rf module
90 mrf_set_reg(MRF_R_RFCTL, _BV(MRF_B_RFRST));
91 mrf_set_reg(MRF_R_RFCTL, 0);
92 _delay_us(192);
95 // reset and initialize radio
96 void mrf_init()
98 // software reset, the bits will be automatically cleared by hardware.
99 mrf_set_reg(MRF_R_SOFTRST, _BV(MRF_B_RSTPWR) | _BV(MRF_B_RSTBB) | _BV(MRF_B_RSTMAC));
101 // register initialization
102 mrf_set_reg(MRF_R_PACON2, _BV(MRF_B_FIFOEN) | MRF_RV_TXONTS);
103 mrf_set_reg(MRF_R_TXSTBL, MRF_RV_RFSTBL | MRF_RV_MSIFS);
104 mrf_set_reg(MRF_R_RFCON1, MRF_RV_VCOOPT);
105 mrf_set_reg(MRF_R_RFCON2, _BV(MRF_B_PLLEN));
106 mrf_set_reg(MRF_R_RFCON6, _BV(MRF_B_TXFIL) | _BV(MRF_B_20MRECVR));
107 mrf_set_reg(MRF_R_RFCON7, MRF_V_SLPCLKSEL_100kHz);
108 mrf_set_reg(MRF_R_RFCON8, _BV(MRF_B_RFVCO));
109 mrf_set_reg(MRF_R_SLPCON1, _BV(MRF_B_CLKOUTEN) | 0x1);
110 mrf_set_reg(MRF_R_BBREG2, MRF_V_CCAMODE_ENERGY_ABOVE_THRESHOLD);
111 mrf_set_reg(MRF_R_CCAEDTH, MRF_RV_CCAEDTH);
112 mrf_set_reg(MRF_R_BBREG6, _BV(MRF_B_RSSIMODE2));
114 // enable interrupts, (inverted values for this register)
115 mrf_set_reg(MRF_R_INTCON, ~(_BV(MRF_B_TXN) | _BV(MRF_B_RX)));
117 // read and set address
118 mrf_set_reg(MRF_R_PANIDL, mrf_pan_id);
119 mrf_set_reg(MRF_R_PANIDH, mrf_pan_id>>8);
120 mrf_set_reg(MRF_R_SADRL, mrf_short_addr);
121 mrf_set_reg(MRF_R_SADRH, mrf_short_addr>>8);
123 mrf_set_channel(0);
126 #endif // __MRF_IMPL__