static void read(unsigned char reg, unsigned char * b, unsigned int len) { unsigned int flags; RAISE_IPL(flags, PRIO_COMMUNICATION); while(i2c_master_is_busy(i2c_bus)); i2c_master_transfert_block(i2c_bus, RF_ADDRESS, ®, 1, b, len); IRQ_ENABLE(flags); }
void rf_init(int bus) { unsigned int flags; unsigned char reg = REG_STATUS; unsigned char s; unsigned char ret; clock_delay_us(5000); // we need to wait until the chip is ready .... RAISE_IPL(flags, PRIO_COMMUNICATION); i2c_bus = bus; // Wait until the bus is idle while(i2c_master_is_busy(i2c_bus)); // Try to access the chip ret = i2c_master_transfert_block(i2c_bus, RF_ADDRESS, ®, 1, &s, 1); if(!ret) { // FIXME // Try harder ... ret = i2c_master_transfert_block(i2c_bus, RF_ADDRESS, ®, 1, &s, 1); } IRQ_ENABLE(flags); if (ret) { write(REG_CTRL, 3); // Put us in presence detect only status = RF_PRESENT | RF_LINK_UP; } else { status = 0; } }
/*---------------------------------------------------------------------------*/ static int spirit_radio_on(void) { PRINTF("Spirit1: on\n"); if(spirit_on == OFF) { IRQ_DISABLE(); /* ensure we are in READY state as we go from there to Rx */ spirit1_strobe(SPIRIT1_STROBE_READY); BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_READY, 5 * RTIMER_SECOND/1000); if(SPIRIT1_STATUS() != SPIRIT1_STATE_READY) { PRINTF("Spirit1: failed to turn on\n"); panic_ra(4); return 1; } /* now we go to Rx */ spirit1_strobe(SPIRIT1_STROBE_RX); BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, 5 * RTIMER_SECOND/1000); if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) { PRINTF("Spirit1: failed to enter rx\n"); panic_ra(5); return 1; } /* Enables the mcu to get IRQ from the SPIRIT1 */ IRQ_ENABLE(); spirit_on = ON; } return 0; }
static void write(unsigned char reg, unsigned char b) { unsigned char buffer[2] = {reg,b}; unsigned int flags; RAISE_IPL(flags, PRIO_COMMUNICATION); while(i2c_master_is_busy(i2c_bus)); i2c_master_transfert_block(i2c_bus, RF_ADDRESS, buffer, 2, 0, 0); IRQ_ENABLE(flags); }
/*---------------------------------------------------------------------------*/ static int spirit_radio_channel_clear(void) { float rssi_value; /* Local variable used to memorize the SPIRIT1 state */ uint8_t spirit_state = ON; PRINTF("CHANNEL CLEAR IN\n"); if(spirit_on == OFF) { /* Wakes up the SPIRIT1 */ spirit_radio_on(); spirit_state = OFF; } /* */ IRQ_DISABLE(); spirit1_strobe(SPIRIT1_STROBE_SABORT); /* SpiritCmdStrobeSabort();*/ SpiritIrqClearStatus(); IRQ_ENABLE(); { rtimer_clock_t timeout = RTIMER_NOW() + 5 * RTIMER_SECOND/1000; do { SpiritRefreshStatus(); } while((g_xStatus.MC_STATE != MC_STATE_READY) && (RTIMER_NOW() < timeout)); if(RTIMER_NOW() < timeout) { return 1; } } /* Stores the RSSI value */ rssi_value = SpiritQiGetRssidBm(); /* Puts the SPIRIT1 in its previous state */ if(spirit_state==OFF) { spirit_radio_off(); } else { spirit1_strobe(SPIRIT1_STROBE_RX); /* SpiritCmdStrobeRx();*/ BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, 5 * RTIMER_SECOND/1000); } PRINTF("CHANNEL CLEAR OUT\n"); /* Checks the RSSI value with the threshold */ if(rssi_value<CCA_THRESHOLD) { return 0; } else { return 1; } }
void rf_poll(void) { unsigned int flags; RAISE_IPL(flags, PRIO_COMMUNICATION); if(!(status & RF_PRESENT) || !(status & RF_LINK_UP)) { // RF not present or activated if(read_acc) { read_acc = 0; mma7660_read_async(); } } else if(!i2c_master_is_busy(i2c_bus)) { // Try to read data from the device i2c_master_start_operations(i2c_bus, i2c_cb, NULL); } IRQ_ENABLE(flags); }
/** * @brief Puts the SPIRIT1 in READY state. * @param None * @retval None */ void SpiritSetReadyState(void) { PRINTF("READY IN\n"); SpiritIrqClearStatus(); IRQ_DISABLE(); if(SPIRIT1_STATUS() == SPIRIT1_STATE_STANDBY) { spirit1_strobe(SPIRIT1_STROBE_READY); /* SpiritCmdStrobeReady();*/ } else if(SPIRIT1_STATUS() == SPIRIT1_STATE_RX) { spirit1_strobe(SPIRIT1_STROBE_SABORT); /* SpiritCmdStrobeSabort();*/ SpiritIrqClearStatus(); } IRQ_ENABLE(); PRINTF("READY OUT\n"); }