uint8_t rxSpiWriteByte(uint8_t data) { ENABLE_RX(); const uint8_t ret = rxSpiTransferByte(data); DISABLE_RX(); return ret; }
uint8_t rxSpiReadCommand(uint8_t command, uint8_t data) { ENABLE_RX(); rxSpiTransferByte(command); const uint8_t ret = rxSpiTransferByte(data); DISABLE_RX(); return ret; }
uint8_t rxSpiReadCommandMulti(uint8_t command, uint8_t commandData, uint8_t *retData, uint8_t length) { ENABLE_RX(); const uint8_t ret = rxSpiTransferByte(command); for (uint8_t i = 0; i < length; i++) { retData[i] = rxSpiTransferByte(commandData); } DISABLE_RX(); return ret; }
uint8_t rxSpiWriteCommandMulti(uint8_t command, const uint8_t *data, uint8_t length) { ENABLE_RX(); const uint8_t ret = rxSpiTransferByte(command); for (uint8_t i = 0; i < length; i++) { rxSpiTransferByte(data[i]); } DISABLE_RX(); return ret; }
/* * Fast read of payload, for use in interrupt service routine */ bool NRF24L01_ReadPayloadIfAvailableFast(uint8_t *data, uint8_t length) { // number of bits transferred = 8 * (3 + length) // for 16 byte payload, that is 8*19 = 152 // at 50MHz clock rate that is approximately 3 microseconds bool ret = false; ENABLE_RX(); rxSpiTransferByte(R_REGISTER | (REGISTER_MASK & NRF24L01_07_STATUS)); const uint8_t status = rxSpiTransferByte(NOP); if ((status & BV(NRF24L01_07_STATUS_RX_DR)) == 0) { ret = true; // clear RX_DR flag rxSpiTransferByte(W_REGISTER | (REGISTER_MASK & NRF24L01_07_STATUS)); rxSpiTransferByte(BV(NRF24L01_07_STATUS_RX_DR)); rxSpiTransferByte(R_RX_PAYLOAD); for (uint8_t i = 0; i < length; i++) { data[i] = rxSpiTransferByte(NOP); } } DISABLE_RX(); return ret; }
/************************************************************************************************* Main **************************************************************************************************/ void main(void) { HAL_BOARD_INIT(); UART_init(); U0CSR &= ~0x04; ENABLE_RX(); /*setup sensors*/ sensors_init(); sensor_int_init(); /* Setup LED's */ P1DIR |= BV(0); P0DIR |= BV(4); P1_0 = 0; start_gyro(); //start the gyro init_acc(); //start the accelerometer start_mag(); start_baro(); //zero_mag(); EA = 1; SLEEPCMD |= 0x02; //pm 2 uint8 flag; uint8 IDbyte; uint8 baro_stage = 1; while(1){ if (RXin) { //If it is a cal data request if ( active_sensors & BV(4) ) { flag = 0x03; flush_data(&flag); baro_read_cal(); //End of line char flag = 0x00; flush_byte(&flag); } else if ( active_sensors > 0 ) { // P0_4 = 1; /**************************************************************************/ /* Read and transmit sensor data */ flag = 0x04; flush_byte(&flag); flush_byte(&active_sensors); /*---------------------------------------------------------------------*/ //Read accelerometer and gyro if ( active_sensors & BV(0) ) { IDbyte = BV(0); flush_data(&IDbyte); while( !( acc_int_status() ) ); //wait for interrupt read_acc(); //read the accelerometer while( !(gyro_int_status() & BV(0) ) ); //wait for interrupt read_gyro(); } /*---------------------------------------------------------------------*/ //Read Magnetometer //start_interrupts(MAG_INT); if ( active_sensors & BV(1) ) { IDbyte = BV(1); flush_data(&IDbyte); //PCON |= 1; while( !(mag_status() & 0x08 ) ); read_mag(); // mag_sleep(TRUE); } /*---------------------------------------------------------------------*/ //Barometer //uint16 delay_ticks; uint8 baro_res = 2; if ( active_sensors & BV(2) ) { IDbyte = BV(2); flush_data(&IDbyte); if (active_sensors & 0x40) { //delay_ticks = 0xFA00; baro_capture_press(baro_res); //while(delay_ticks--); baro_read_press(TRUE); //delay_ticks = 0xFA00; baro_capture_temp(); //while(delay_ticks--); baro_read_temp(TRUE); }else{ uint8 nullbyte = 3; switch (baro_stage) { case 1 : baro_capture_press(baro_res); baro_read_press(FALSE); baro_read_temp(FALSE); baro_stage++; break; case 2 : baro_read_press(TRUE); baro_read_temp(FALSE); baro_stage++; break; case 3 : baro_capture_temp(); baro_read_press(FALSE); baro_read_temp(FALSE); baro_stage++; break; case 4 : baro_read_press(FALSE); baro_read_temp(TRUE); baro_stage = 1; break; } } //baro_shutdown(); } /*---------------------------------------------------------------------*/ //Humidity if ( active_sensors & BV(3) ) { IDbyte = BV(3); flush_data(&IDbyte); humid_init(); humid_read_humidity(TRUE); } /*---------------------------------------------------------------------*/ //End of line char flag = 0x00; flush_byte(&flag); } if ( !(active_sensors & BV(5)) ) //if autopoll is off { P0_4 = 1; RXin = 0; //clear the RX flag }else{ P0_4 = 0; } } IEN0 |= 0x04; U0CSR &= ~0x04; } }