00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <aversive.h>
00025 #include <aversive/wait.h>
00026 #include <aversive/error.h>
00027
00028 #include <spi.h>
00029
00030 #include <cc2420.h>
00031 #include <cc2420_arch.h>
00032 #include <cc2420_config.h>
00033
00034
00035 #define SLAVE_SELECT() spi_slave_select(g_slave_adr)
00036 #define SLAVE_DESELECT() spi_slave_deselect(g_slave_adr)
00037
00038
00039
00040
00041
00042
00043 volatile static uint8_t g_slave_adr;
00044
00045
00046
00047
00048
00049 uint8_t cc2420_get_status(void)
00050 {
00051 uint8_t tmp;
00052 SLAVE_SELECT();
00053 tmp = spi_send_and_receive_byte(SNOP);
00054 SLAVE_DESELECT();
00055 return tmp;
00056 }
00057
00058
00059
00060
00061
00062 uint8_t cc2420_strobe_register(uint8_t reg)
00063 {
00064 uint8_t tmp;
00065 SLAVE_SELECT();
00066 tmp = spi_send_and_receive_byte(reg);
00067 SLAVE_DESELECT();
00068 return tmp;
00069 }
00070
00071
00072
00073
00074 uint16_t cc2420_read_register(uint8_t reg)
00075 {
00076 uint16_t value;
00077 SLAVE_SELECT();
00078
00079 spi_send_byte(REG_BIT | READ_BIT | (reg & REG_MASK));
00080
00081 value = spi_receive_byte();
00082 value = (value<<8) | spi_receive_byte();
00083 SLAVE_DESELECT();
00084 return value;
00085 }
00086
00087
00088
00089
00090 void cc2420_write_register(uint8_t reg, uint16_t value)
00091 {
00092 SLAVE_SELECT();
00093
00094 spi_send_byte(REG_BIT | WRITE_BIT | (reg & REG_MASK));
00095
00096 spi_send_byte((uint8_t)((value & 0xFF00)>>8));
00097 spi_send_byte((uint8_t)(value & 0x00FF));
00098 SLAVE_DESELECT();
00099 }
00100
00101
00102
00103
00104
00105 uint8_t cc2420_write_txfifo(uint8_t *buffer, uint8_t length)
00106 {
00107 uint8_t status;
00108 uint8_t i;
00109 SLAVE_SELECT();
00110
00111
00112
00113 NOTICE(E_CC2420, "Writing %d bytes to TXFIFO at address 0x%x", length, REG_BIT | WRITE_BIT | TXFIFO);
00114 spi_send_byte(REG_BIT | WRITE_BIT | TXFIFO);
00115
00116 for(i = 0; i < length; i++)
00117 {
00118
00119
00120 status = spi_send_and_receive_byte(buffer[i]);
00121 }
00122 SLAVE_DESELECT();
00123 return status;
00124 }
00125
00126
00127
00128
00129
00130 uint8_t cc2420_write_rxfifo(uint8_t *buffer, uint8_t length)
00131 {
00132 uint8_t status;
00133 uint8_t i;
00134 SLAVE_SELECT();
00135
00136 spi_send_byte(SFLUSHRX);
00137
00138 spi_send_byte(REG_BIT | WRITE_BIT | RXFIFO);
00139
00140 for(i = 0; i < length; i++)
00141 {
00142
00143
00144 status = spi_send_and_receive_byte(buffer[i]);
00145 }
00146 SLAVE_DESELECT();
00147 return status;
00148 }
00149
00150
00151
00152
00153 void cc2420_read_rxfifo(uint8_t *buffer, uint8_t length)
00154 {
00155 uint8_t i;
00156 SLAVE_SELECT();
00157
00158 spi_send_byte(REG_BIT | READ_BIT | RXFIFO);
00159
00160 for(i = 0; i < length; i++)
00161 {
00162 buffer[i] = spi_receive_byte();
00163 }
00164 SLAVE_DESELECT();
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174 void cc2420_write_ram(uint16_t addr, uint8_t *buffer, uint16_t length)
00175 {
00176 uint16_t i;
00177 SLAVE_SELECT();
00178
00179 spi_send_byte(RAM_BIT | (addr & RAM_MASK));
00180 spi_send_byte((((addr>>1) & BANK_MASK) | RAM_READ_WRITE));
00181
00182 for(i = 0; i < length; i++)
00183 {
00184 spi_send_byte((uint8_t)buffer[i]);
00185 }
00186 SLAVE_DESELECT();
00187 }
00188
00189
00190
00191
00192 void cc2420_read_ram(uint16_t addr, uint8_t *buffer, uint16_t length)
00193 {
00194 uint16_t i;
00195 SLAVE_SELECT();
00196
00197 spi_send_byte((RAM_BIT | (addr & RAM_MASK)));
00198 spi_send_byte((((addr>>1) & BANK_MASK) | RAM_READ));
00199
00200 for(i = 0; i < length; i++)
00201 {
00202 buffer[i] = spi_receive_byte();
00203 }
00204 SLAVE_DESELECT();
00205 }
00206
00207
00208
00209
00210
00211
00212 void cc2420_init(void)
00213 {
00214 NOTICE(E_CC2420, "Initialization");
00215
00216 if (spi_get_mode() == SPI_MODE_UNINIT)
00217 {
00218 g_slave_adr = spi_register_ss_line(&CC2420_SS_PORT, CC2420_SS_PIN);
00219 spi_init(SPI_MODE_MASTER, SPI_FORMAT_0, SPI_CLK_RATE_2);
00220 }
00221
00222 NOTICE(E_CC2420, "Registered slave line: %d", g_slave_adr);
00223
00224
00225 DDR(CC2420_FIFO_PORT) &= ~(_BV(CC2420_FIFO_PIN));
00226 DDR(CC2420_FIFOP_PORT) &= ~(_BV(CC2420_FIFOP_PIN));
00227 DDR(CC2420_CCA_PORT) &= ~(_BV(CC2420_CCA_PIN));
00228 DDR(CC2420_SFD_PORT) &= ~(_BV(CC2420_SFD_PIN));
00229
00230 CC2420_SS_DDR = 0x01;
00231
00232
00233 #ifdef CC2420_VREG_ENABLE
00234 NOTICE(E_CC2420, "Enable on-chip voltage regulator");
00235 DDR(CC2420_VREG_EN_PORT) |= _BV(CC2420_VREG_EN_PIN);
00236 CC2420_VREG_EN_PORT |= _BV(CC2420_VREG_EN_PIN);
00237 wait_ms(1);
00238 #endif
00239
00240
00241 #ifdef CC2420_RESET_ENABLE
00242 NOTICE(E_CC2420, "Reset radio chip");
00243 DDR(CC2420_RESET_PORT) |= _BV(CC2420_RESET_PIN);
00244 CC2420_RESET_PORT &= ~(_BV(CC2420_RESET_PIN));
00245 wait_ms(1);
00246 CC2420_RESET_PORT |= _BV(CC2420_RESET_PIN);
00247 wait_ms(1);
00248 #endif
00249
00250
00251
00252 NOTICE(E_CC2420, "Start the oscillator");
00253
00254
00255 SLAVE_SELECT();
00256 spi_send_byte(SXOSCON);
00257
00258 SLAVE_DESELECT();
00259
00260
00261 while(!CC2420_STATUS_CHECK(XOSC16M_STABLE))
00262 ;
00263
00264 NOTICE(E_CC2420, "Init done");
00265 }