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/>.
25 #include <util/delay.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
)
38 spi_shift_byte(0x1 | (addr
<< 1));
44 // get and return value of given short register via spi
45 uint8_t mrf_read_short_addr(uint8_t addr
)
50 spi_shift_byte(addr
<< 1);
51 uint8_t res
= spi_shift_byte(0x00);
57 // set given long register to given value via spi
58 void mrf_write_long_addr(uint16_t addr
, uint8_t val
)
63 spi_shift_byte(0x80 | (addr
>> 3));
64 spi_shift_byte(0x10 | (addr
<< 5));
70 // get and return value of given long register via spi
71 uint8_t mrf_read_long_addr(uint16_t addr
)
76 spi_shift_byte(0x80 | (addr
>> 3));
77 spi_shift_byte(addr
<< 5);
78 uint8_t res
= spi_shift_byte(0x00);
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);
90 mrf_set_reg(MRF_R_RFCTL
, _BV(MRF_B_RFRST
));
91 mrf_set_reg(MRF_R_RFCTL
, 0);
95 // reset and initialize radio
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);
126 #endif // __MRF_IMPL__