/* * @brief Sets the PAN for any transceiver device * * @param t The transceiver device * @param pan The PAN to be set * * @return The PAN AFTER calling the set command, -1 on error */ static int32_t set_pan(transceiver_type_t t, void *pan) { uint16_t c = *((uint16_t *) pan); switch (t) { #ifdef MODULE_CC2420 case TRANSCEIVER_CC2420: return cc2420_set_pan(c); #endif #ifdef MODULE_NATIVENET case TRANSCEIVER_NATIVE: return nativenet_set_pan(c); #endif #ifdef MODULE_AT86RF231 case TRANSCEIVER_AT86RF231: return at86rf231_set_pan(c); #endif #ifdef MODULE_MC1322X case TRANSCEIVER_MC1322X: return maca_set_pan(c); #endif default: /* get rid of compiler warning about unused variable */ (void) c; return -1; } }
void cc2420_init(int tpid) { uint16_t reg; transceiver_pid = tpid; cc2420_spi_init(); hwtimer_wait(CC2420_WAIT_TIME); cc2420_reset(); cc2420_strobe(CC2420_STROBE_XOSCON); //enable crystal while((cc2420_strobe(NOBYTE) & 0x40) == 0); //wait for crystal to be stable hwtimer_wait(CC2420_WAIT_TIME); reg = cc2420_read_reg(CC2420_REG_MDMCTRL0); reg |= CC2420_ADR_DECODE; //enable adr decode reg |= CC2420_AUTOACK; //enable auto ack reg |= CC2420_AUTOCRC; //enable auto crc reg &= ~(CC2420_RES_FRM_MODE); //disable reserved frames cc2420_write_reg(CC2420_REG_MDMCTRL0, reg); /* Change default values as recomended in the data sheet, */ /* RX bandpass filter = 1.3uA. */ reg = cc2420_read_reg(CC2420_REG_RXCTRL1); reg |= CC2420_RXBPF_LOCUR; cc2420_write_reg(CC2420_REG_RXCTRL1, reg); /* Set the FIFOP threshold to maximum. */ cc2420_write_reg(CC2420_REG_IOCFG0, 127); /* Turn off "Security enable" (page 32). */ reg = cc2420_read_reg(CC2420_REG_SECCTRL0); reg &= ~CC2420_RXFIFO_PROTECTION; cc2420_write_reg(CC2420_REG_SECCTRL0, reg); /* set output power to 0dbm */ cc2420_write_reg(CC2420_REG_TXCTRL, 0xA0FF); cc2420_set_channel(CC2420_DEFAULT_CHANNR); cc2420_set_pan(0x1111); DEBUG("CC2420 initialized and set to channel %i and pan %i\n", radio_channel, radio_pan); cc2420_init_interrupts(); cc2420_switch_to_rx(); }
int cc2420_init(cc2420_t *dev) { uint16_t reg; uint8_t addr[8]; /* reset options and sequence number */ dev->netdev.seq = 0; dev->netdev.flags = 0; /* set default address, channel, PAN ID, and TX power */ luid_get(addr, sizeof(addr)); /* make sure we mark the address as non-multicast and not globally unique */ addr[0] &= ~(0x01); addr[0] |= 0x02; cc2420_set_addr_short(dev, &addr[6]); cc2420_set_addr_long(dev, addr); cc2420_set_pan(dev, CC2420_PANID_DEFAULT); cc2420_set_chan(dev, CC2420_CHAN_DEFAULT); cc2420_set_txpower(dev, CC2420_TXPOWER_DEFAULT); /* set default options */ cc2420_set_option(dev, CC2420_OPT_AUTOACK, true); cc2420_set_option(dev, CC2420_OPT_CSMA, true); cc2420_set_option(dev, CC2420_OPT_TELL_TX_START, true); cc2420_set_option(dev, CC2420_OPT_TELL_RX_END, true); #ifdef MODULE_NETSTATS_L2 cc2420_set_option(dev, CC2420_OPT_TELL_RX_END, true); #endif /* set default protocol*/ #ifdef MODULE_GNRC_SIXLOWPAN dev->netdev.proto = GNRC_NETTYPE_SIXLOWPAN; #elif MODULE_GNRC dev->netdev.proto = GNRC_NETTYPE_UNDEF; #endif /* change default RX bandpass filter to 1.3uA (as recommended) */ reg = cc2420_reg_read(dev, CC2420_REG_RXCTRL1); reg |= CC2420_RXCTRL1_RXBPF_LOCUR; cc2420_reg_write(dev, CC2420_REG_RXCTRL1, reg); /* set the FIFOP threshold to maximum. */ cc2420_reg_write(dev, CC2420_REG_IOCFG0, CC2420_PKT_MAXLEN); /* turn off "Security enable" (page 33). */ reg = cc2420_reg_read(dev, CC2420_REG_SECCTRL0); reg &= ~CC2420_SECCTRL0_RXFIFO_PROT; cc2420_reg_write(dev, CC2420_REG_SECCTRL0, reg); /* set preamble length to 3 leading zero byte */ /* and turn on hardware CRC generation */ reg = cc2420_reg_read(dev, CC2420_REG_MDMCTRL0); reg &= ~(CC2420_MDMCTRL0_PREAMBLE_M); reg |= CC2420_MDMCTRL0_PREAMBLE_3B; reg |= CC2420_MDMCTRL0_AUTOCRC; cc2420_reg_write(dev, CC2420_REG_MDMCTRL0, reg); /* go into RX state */ cc2420_set_state(dev, CC2420_GOTO_RX); return 0; }
static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t val_len) { if (netdev == NULL) { return -ENODEV; } cc2420_t *dev = (cc2420_t *)netdev; int ext = netdev_ieee802154_set(&dev->netdev, opt, val, val_len); switch (opt) { case NETOPT_ADDRESS: assert(val_len == 2); cc2420_set_addr_short(dev, val); return 2; case NETOPT_ADDRESS_LONG: assert(val_len == 8); cc2420_set_addr_long(dev, val); return 8; case NETOPT_NID: assert(val_len == sizeof(uint16_t)); cc2420_set_pan(dev, to_u16(val)); return sizeof(uint16_t); case NETOPT_CHANNEL: assert(val_len == sizeof(uint16_t)); return cc2420_set_chan(dev, to_u16(val)); case NETOPT_TX_POWER: assert(val_len == sizeof(int16_t)); cc2420_set_txpower(dev, to_i16(val)); return sizeof(int16_t); case NETOPT_STATE: assert(val_len == sizeof(netopt_state_t)); return cc2420_set_state(dev, *((netopt_state_t *)val)); case NETOPT_AUTOACK: return cc2420_set_option(dev, CC2420_OPT_AUTOACK, to_bool(val)); case NETOPT_CSMA: return cc2420_set_option(dev, CC2420_OPT_CSMA, to_bool(val)); case NETOPT_PRELOADING: return cc2420_set_option(dev, CC2420_OPT_PRELOADING, to_bool(val)); case NETOPT_PROMISCUOUSMODE: return cc2420_set_option(dev, CC2420_OPT_PROMISCUOUS, to_bool(val)); case NETOPT_RX_START_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_RX_START, to_bool(val)); case NETOPT_RX_END_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_RX_END, to_bool(val)); case NETOPT_TX_START_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_TX_START, to_bool(val)); case NETOPT_TX_END_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_TX_END, to_bool(val)); default: return ext; } return 0; }