int main(int argc, char *argv[]) { node_id_restore(); /* init system: clocks, board etc */ system_init(); sio2host_init(); leds_init(); leds_on(LEDS_ALL); system_interrupt_enable_global(); flash_init(); delay_init(); /* Initialize Contiki and our processes. */ #ifdef LOW_POWER_MODE configure_tc3(); #else clock_init(); #endif process_init(); ctimer_init(); rtimer_init(); process_start(&etimer_process, NULL); /* Set MAC address and node ID */ #ifdef NODEID node_id = NODEID; #ifdef BURN_NODEID node_id_burn(node_id); #endif /* BURN_NODEID */ #else/* NODE_ID */ #endif /* NODE_ID */ printf("\r\n\n\n\n Starting the SmartConnect-6LoWPAN \r\n Platform : Atmel IoT device \r\n"); print_reset_causes(); netstack_init(); #if BOARD == SAMR21_XPLAINED_PRO eui64 = edbg_eui_read_eui64(); SetIEEEAddr(eui64); #else SetIEEEAddr(node_mac); #endif set_link_addr(); rf_set_channel(RF_CHANNEL); printf("\r\n Configured RF channel: %d\r\n", rf_get_channel()); leds_off(LEDS_ALL); process_start(&sensors_process, NULL); energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); if(node_id > 0) { printf(" Node id %u.\r\n", node_id); } else { printf(" Node id not set.\r\n"); } /* Setup nullmac-like MAC for 802.15.4 */ #if SAMD memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr)); #else memcpy(&uip_lladdr.addr, eui64, sizeof(uip_lladdr.addr)); #endif queuebuf_init(); printf(" %s %lu %d\r\n", NETSTACK_RDC.name, (uint32_t) (CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval())), RF_CHANNEL); process_start(&tcpip_process, NULL); printf(" IPv6 Address: "); { uip_ds6_addr_t *lladdr; int i; lladdr = uip_ds6_get_link_local(-1); for(i = 0; i < 7; ++i) { printf("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\r\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xfc00, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); printf("Tentative global IPv6 address "); for(i = 0; i < 7; ++i) { printf("%02x%02x:", ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\r\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } print_processes(autostart_processes); /* set up AES key */ #if ((THSQ_CONF_NETSTACK) & THSQ_CONF_AES) #ifndef NETSTACK_AES_KEY #error Please define NETSTACK_AES_KEY! #endif /* NETSTACK_AES_KEY */ { const uint8_t key[] = NETSTACK_AES_KEY; netstack_aes_set_key(key); } printf("AES encryption is enabled\n"); #else /* ((THSQ_CONF_NETSTACK) & THSQ_CONF_AES) */ printf("\r\n Warning: AES encryption is disabled\n"); #endif /* ((THSQ_CONF_NETSTACK) & THSQ_CONF_AES) */ #ifdef ENABLE_LEDCTRL ledctrl_init(); #endif autostart_start(autostart_processes); while(1){ int r = 0; serial_data_handler(); do { r = process_run(); } while(r > 0); } }
/** * \brief Init the radio * \return Returns success/fail * \retval 0 Success */ int rf233_init(void) { volatile uint8_t regtemp; volatile uint8_t radio_state; /* don't optimize this away, it's important */ PRINTF("RF233: init.\n"); /* init SPI and GPIOs, wake up from sleep/power up. */ spi_init(); // RF233 expects line low for CS, this is default SAM4L behavior //spi_set_chip_select(3); // POL = 0 means idle is low spi_set_chip_select(3); spi_set_polarity(0); // PHASE = 0 means sample leading edge spi_set_phase(0); spi_set_rate(400000); /* reset will put us into TRX_OFF state */ /* reset the radio core */ gpio_enable_output(RST_PIN); gpio_enable_output(SLP_PIN); gpio_clear(RST_PIN); delay_ms(1); gpio_set(RST_PIN); gpio_clear(SLP_PIN); /* be awake from sleep*/ /* Read the PART_NUM register to verify that the radio is * working/responding. Could check in software, I just look at * the bus. If this is working, the first write should be 0x9C x00 * and the return bytes should be 0x00 0x0B. - pal*/ regtemp = trx_reg_read(RF233_REG_PART_NUM); /* before enabling interrupts, make sure we have cleared IRQ status */ regtemp = trx_reg_read(RF233_REG_IRQ_STATUS); PRINTF("RF233: After wake from sleep\n"); radio_state = rf233_status(); PRINTF("RF233: Radio state 0x%04x\n", radio_state); calibrate_filters(); if (radio_state == STATE_P_ON) { trx_reg_write(RF233_REG_TRX_STATE, TRXCMD_TRX_OFF); } /* Assign regtemp to regtemp to avoid compiler warnings */ regtemp = regtemp; // Set up interrupts gpio_interrupt_callback(interrupt_callback, NULL); gpio_enable_input(RADIO_IRQ, PullNone); gpio_clear(RADIO_IRQ); gpio_enable_interrupt(RADIO_IRQ, PullNone, RisingEdge); /* Configure the radio using the default values except these. */ trx_reg_write(RF233_REG_TRX_CTRL_1, RF233_REG_TRX_CTRL_1_CONF); trx_reg_write(RF233_REG_PHY_CC_CCA, RF233_REG_PHY_CC_CCA_CONF); trx_reg_write(RF233_REG_PHY_TX_PWR, RF233_REG_PHY_TX_PWR_CONF); trx_reg_write(RF233_REG_TRX_CTRL_2, RF233_REG_TRX_CTRL_2_CONF); trx_reg_write(RF233_REG_IRQ_MASK, RF233_REG_IRQ_MASK_CONF); trx_reg_write(RF233_REG_XAH_CTRL_1, 0x02); trx_bit_write(SR_MAX_FRAME_RETRIES, 0); trx_bit_write(SR_MAX_CSMA_RETRIES, 0); PRINTF("RF233: Configured transciever.\n"); { uint8_t addr[8]; addr[0] = 0x22; addr[1] = 0x22; addr[2] = 0x22; addr[3] = 0x22; addr[4] = 0x22; addr[5] = 0x22; addr[6] = 0x22; addr[7] = 0x22; SetPanId(IEEE802154_CONF_PANID); SetIEEEAddr(addr); SetShortAddr(0x2222); } rf_generate_random_seed(); for (uint8_t i = 0; i < 8; i++) { regtemp = trx_reg_read(0x24 + i); } /* 11_09_rel */ trx_reg_write(RF233_REG_TRX_RPC, 0xFF); /* Enable RPC feature by default */ PRINTF("RF233: Installed addresses. Turning on radio."); rf233_on(); return 0; }