uint8_t CX10::_spi_read_address(uint8_t address) { uint8_t result; CS_off; _spi_write(address); result = _spi_read(); CS_on; return(result); }
void CX10::Read_Packet() { uint8_t i; CS_off; _spi_write(0x61); // Read RX payload for (i=0;i<PACKET_LENGTH;i++) { packet[i]=_spi_read(); } CS_on; }
void ServiceMode() { unsigned char rssi_table[256]; uint8_t service_channel; unsigned long time = millis(); static unsigned long last_hop_time; Red_LED_ON; Green_LED_ON; service_channel = 0; last_hop_time = time; ChannelSet(service_channel); while(1) { time = millis(); if (_spi_read(0x0C)==0) // detect the locked module and reboot { RF22B_init_parameter(); to_rx_mode(); } //channel change if (time-last_hop_time>12) { rssi_table[service_channel] = _spi_read(0x26); service_channel++; last_hop_time = time; } if (UART_lock) //awaiting message from UART { Service_Recieve(UART_command_type,UART_command_size,(char *)UART_command); UART_lock = 0; //unlock uart - allowing to recieve next message } Service_Send(rssi_table); } }
byte NRF_ReadRegister(int nAddress) { byte nResult; //Every new command must be started by a high to low transition on CSN NRF_Select(FALSE); delay_us(1); //Tcwh = 50ns, CSN Inactive time NRF_Select(TRUE); //Chip select delay_us(1); //Tcsd = 38ns, CSN to Data Valid _spi_write(nAddress&0x1F); nResult = _spi_read(); NRF_Select(FALSE); return nResult; }
void NRF_reset_rx() { int i; //Read RX payload NRF_WriteCommand(0x61); for(i=0; i<NRF_PACKET_SIZE; i++) { gNRFPacket[i] = _spi_read(); } gNRFPacket[NRF_PACKET_SIZE] = 0; //Flush rx FIFO NRF_WriteCommand(0xE2); //Reset int NRF_WriteRegister(0x27, 0x40); NRF_Select(FALSE); }
void spi_rx1_isr_handler(void) { struct spi_module *module = _spi_instances[1]; /* get interrupt flags and mask out enabled callbacks */ uint32_t flags = module->hw->RECEIVE_STATUS.reg; flags &= module->hw->RX_INTERRUPT_MASK.reg; if (flags & SPI_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY) { if (module->hw->RECEIVE_STATUS.reg & SPI_RECEIVE_STATUS_FIFO_OVERRUN) { if (module->dir != SPI_DIRECTION_WRITE) { /* Store the error code */ module->status = STATUS_ERR_OVERFLOW; /* End transaction */ module->dir = SPI_DIRECTION_IDLE; module->hw->RX_INTERRUPT_MASK.reg &= ~(SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK | SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK); /* Run callback if registered and enabled */ if ((module->enabled_callback & (1 << SPI_CALLBACK_ERROR)) && (module->registered_callback & (1 << SPI_CALLBACK_ERROR))) { module->status = STATUS_ERR_OVERFLOW; module->hw->RX_INTERRUPT_MASK.reg &= ~(SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK); (module->callback[SPI_CALLBACK_ERROR])(module); } } /* Flush */ uint16_t flush = module->hw->RECEIVE_DATA.reg; UNUSED(flush); } else { if (module->dir == SPI_DIRECTION_WRITE) { /* Flush receive buffer when writing */ _spi_read_dummy(module); if (module->remaining_dummy_buffer_length == 0) { module->hw->RX_INTERRUPT_MASK.reg &= ~SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK; module->status = STATUS_OK; module->dir = SPI_DIRECTION_IDLE; } } else { _spi_read(module); if (module->remaining_rx_buffer_length == 0) { if(module->dir == SPI_DIRECTION_READ) { if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_RECEIVED)) && (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_RECEIVED))) { module->status = STATUS_OK; module->hw->RX_INTERRUPT_MASK.reg &= ~(SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK); (module->callback[SPI_CALLBACK_BUFFER_RECEIVED])(module); } } else if (module->dir == SPI_DIRECTION_BOTH) { if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED)) && (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED))) { module->hw->RX_INTERRUPT_MASK.reg &= ~(SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK); if (flag_direction_both[1]) { module->status = STATUS_OK; flag_direction_both[1] = false; (module->callback[SPI_CALLBACK_BUFFER_TRANSCEIVED])(module); } else { flag_direction_both[1] = true; } } } } } } } }
int main() { uint8_t i; Init(); if (service_mode_rx) ServiceMode(); //enter service mode //Hop to first frequency from Carrier #ifdef FREQUENCY_HOPPING Hopping(); #endif RF_Mode = Receive; Red_LED_OFF; time = millis(); last_pack_time = time; // reset the last pack receiving time for first usage last_hopping_time = time; // reset hopping timer // quality_check_time = time; //-------------- MAIN LOOP ------------------------------------------------------------------------------------------- while(1) { time = millis(); if (_spi_read(0x0C)==0) // detect the locked module and reboot { RF22B_init_parameter(); to_rx_mode(); } SignalLostProcedure(); if(RF_Mode == Received) // RFM22B INT pin Enabled by received Data { last_pack_time = time; // record last package time Red_LED_OFF; Green_LED_ON; send_read_address(0x7f); // Send the package read command //read all buffer for(i = 0; i<DATA_PACKAGE_SIZE; i++) RF_Rx_Buffer[i] = read_8bit_data(); rx_reset(); if (RF_Rx_Buffer[0] == 'S') // servo control data { for(i = 0; i<8; i++) //Write into the Servo Buffer { temp_int = (256*RF_Rx_Buffer[1+(2*i)]) + RF_Rx_Buffer[2+(2*i)]; if ((temp_int>1500) && (temp_int<4500)) Servo_Position[i] = temp_int; } } // sum_rssi += _spi_read(0x26); // Read the RSSI value //binding mode if (bind) if (Bind()) break; #ifdef FREQUENCY_HOPPING ChannelHopping(1); #endif last_ok_channel = hopping_channel; last_hopping_time = time; RF_Mode = Receive; } else Green_LED_OFF; } //---------------------------------------------------------------------------------------------------------------------- if (bind) //binding finished, now you have to restart RX { Red_LED_ON; Green_LED_ON; while(1); } }