void bindRX(bool timeout) { uint32_t start = millis(); printStrLn("waiting bind..."); init_rfm(1); RF_Mode = Receive; to_rx_mode(); while(!timeout || ((millis() - start) < 500)) { if (RF_Mode == Received) { printStrLn("Got pkt\n"); spiSendAddress(0x7f); // Send the package read command uint8_t rxb = spiReadData(); if (rxb == 'b') { for (uint8_t i = 0; i < sizeof(bind_data); i++) { *(((uint8_t*) &bind_data) + i) = spiReadData(); } if (bind_data.version == BINDING_VERSION) { printStrLn("data good\n"); rxb = 'B'; tx_packet(&rxb, 1); // ACK that we got bound bindWriteEeprom(); Red_LED_ON; //signal we got bound on LED:s Green_LED_ON; //signal we got bound on LED:s return; } } } } }
void Init() { ReadConfigFromEEPROM(); //LEDs DDRC |= (1<<PC3); //Red LED DDRB |= (1<<PB5); //Green LED //check for service mode CheckServiceMode(); #ifndef NO_BINDING Bind_RX_Init(); #endif Red_LED_ON; Timer0_Init(); USART_Init(); RF22B_Init(); Servo_Init(); to_rx_mode(); sei(); }
void Init() { ReadConfigFromEEPROM(); //LEDs DDRC |= (1<<PC3); //Red LED DDRB |= (1<<PB5); //Green LED //check for service mode CheckServiceMode(); #ifndef NO_BINDING //check BIND button state DDRD &= ~(1<<PD3); PORTD |= (1<<PD3); _delay_ms(1); if (bit_is_clear(PIND,PD3)) { _delay_ms(1000); if (bit_is_clear(PIND,PD3)) bind = 1; } else bind = 0; if (bind) { RF_Header[0] = 'B'; RF_Header[1] = 'I'; RF_Header[2] = 'N'; RF_Header[3] = 'D'; } else { RF_Header[0] = SETTINGS.RF_HEADER[0]; RF_Header[1] = SETTINGS.RF_HEADER[1]; RF_Header[2] = SETTINGS.RF_HEADER[2]; RF_Header[3] = SETTINGS.RF_HEADER[3]; } #endif Red_LED_ON; Timer0_Init(); USART_Init(); RF22B_Init(); Servo_Init(); to_rx_mode(); sei(); }
int main(void) { unsigned char i, chksum; ioinit(); init_SPI(); sbi(CSPORT,CS); printf("********RFM22 Communication Test********\n"); //====================// //Communications Test printf("Reading DTYPE register, should be 0x08: %d", read(DTYPE)); printf("\n*****************************************\n\n"); init_RFM22(); // Initialize all RFM22 registers printf("Entering RX Mode...\n"); to_rx_mode(); while(1) { if((PIND & (1<<NIRQ)) == 0) //Interrupt will be generated if data is recieved { // Read 18-byte packet into RF_RXBUF[]: for(i=0; i<17; i++) { RF_RXBUF[i] = read(0x7F); } // Check that the checksum matches up: chksum = 0; for(i=0; i<16; i++) chksum += RF_RXBUF[i]; // If checksum is good, print out what was received into the terminal if(chksum == RF_RXBUF[16]) { write(0x07, 0x01); // To ready mode printf("Received: "); for(i=0; i<17; i++) printf("%c", RF_RXBUF[i]); // Print characters if passed the checksum printf("\n"); //printf("To Ready mode\n"); } // otherwise reset and start listening again else { rx_reset(); //printf("Bad checksum RX RESET...Listening again\n"); } delay_ms(50); } } }
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); } }
/*********************************************** go to tx mode(transmit data leSS_RFM than 64byte) ************************************************/ void to_tx_mode(void) { to_ready_mode(); _delay_us(200); unsigned char j,k; txError=0; clear_fifo(); spi_write(0x3E, txBufCnt); _RFM_SPCR _SS_AKT //start SS_RFM _delay_us(1); SPDR=0xff; //adres while(!(SPSR & (1<<SPIF))); for (k=0;k<txBufCnt;k++) { //dane SPDR=txBufTab[k]; while(!(SPSR & (1<<SPIF))); } txBufCnt=0; _delay_us(1); _SS_DEZ _delay_us(1); j=0;/* while (to_tx_mode_table[j]!=0xFFFF) { spi_write_command(to_tx_mode_table[j]); j++; }*/ spi_write_command(to_tx_mode_table[0]); spi_write_command(to_tx_mode_table[1]); ItStatus1 = spi_read(INTERRUPT_STATUS_1); ItStatus2 = spi_read(INTERRUPT_STATUS_2); spi_write_command(to_tx_mode_table[2]); while(!(0x02&spi_read(0x02))); //_delay_ms(50); //tempStat1=spi_read(0x02); #ifdef ODMIERZANIE_CZASU txczas = czasomierz; #endif do { #ifdef ODMIERZANIE_CZASU if (czasomierz>txczas+_STOMILI(TX_CZAS_CZEKANIA)) { ItStatus2=spi_read(INTERRUPT_STATUS_2); ItStatus1=spi_read(INTERRUPT_STATUS_1); txError=1; break; } #endif } #ifdef _RFM_NIRQ while(_RFM_NIRQ); //zmienic na sprawdzanie flagi //tempStat2=spi_read(0x02); ItStatus2=spi_read(INTERRUPT_STATUS_2); ItStatus1=spi_read(INTERRUPT_STATUS_1); #else while(!(spi_read(0x31)&0x01)); #endif #ifdef TX_ERROR_RESET if (txError) { RF23B_init_parameter(); //software_reset(); //module software reset //_delay_ms(5); //delay 5ms,then re_Initialization the module //RF23B_init_parameter(); } #endif to_rx_mode(); }
void setup(void) { watchdogConfig(WATCHDOG_OFF); setupSPI(); #ifdef SDN_pin pinMode(SDN_pin, OUTPUT); //SDN digitalWrite(SDN_pin, 0); #endif //LED and other interfaces pinMode(Red_LED, OUTPUT); //RED LED pinMode(Green_LED, OUTPUT); //GREEN LED #ifdef Red_LED2 pinMode(Red_LED2, OUTPUT); //RED LED pinMode(Green_LED2, OUTPUT); //GREEN LED #endif // pinMode(BTN, INPUT); //Button pinMode(SLAVE_SELECT, INPUT); digitalWrite(SLAVE_SELECT, HIGH); // enable pullup for TX:s with open collector output buzzerInit(); serialInit(115200,SERIAL_8N1); checkOperatingMode(); printStr("OpenLRSng DL starting "); printVersion(version); printStr(" on HW "); printUL(BOARD_TYPE); printStr(" ("); printUL(RFMTYPE); printStr("MHz) MDOE="); buzzerOn(BZ_FREQ); digitalWrite(BTN, HIGH); Red_LED_ON ; sei(); delay(50); if (!slaveMode) { printStrLn("MASTER"); if (!bindReadEeprom()) { printStrLn("eeprom bogus reinit...."); bindInitDefaults(); bindWriteEeprom(); } if (!digitalRead(BTN)) { bindMode(); } } else { printStrLn("SLAVE"); if (!digitalRead(BTN) || !bindReadEeprom()) { bindRX(false); } else { bindRX(true); } } packetInterval = getInterval(&bind_data); printStrLn("Entering normal mode"); serialFlush(); serialInit(bind_data.serial_baudrate, bind_data.serial_mode); Red_LED_OFF; buzzerOff(); init_rfm(0); rfmSetChannel(RF_channel); rx_reset(); if (slaveMode) { to_rx_mode(); RF_Mode = Receive; } watchdogConfig(WATCHDOG_2S); lastReceived=micros(); }
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); } }
int main(void) { //uint8_t i=0; inicjalizacja(); _delay_ms(500); spi_write(0x07, 0x80); while (_RFM_NIRQ); //while (!(0x02&spi_read(INTERRUPT_STATUS_2))); //_delay_ms(200); RF23B_init_parameter(); //while (_RFM_NIRQ); while (!((1<<ICHIPRDY)&spi_read(INTERRUPT_STATUS_2))); uint8_t temp8; uint16_t temp16; setup(); czasomierz=0; sei(); _delay_ms(500); _LED_Z; to_ready_mode(); _delay_ms(100); _LED_OFF; _LED_C; _delay_ms(100); _LED_OFF; _delay_ms(500); uint8_t led_stan=0; to_rx_mode(); while (1) { if (!_RFM_NIRQ) { if (led_stan) { _LED_Z; led_stan=0; } else { _LED_OFF; led_stan=1; } if(rxBufTab[0]==R_STOP) {f.ARMED=0; }else if (rxBufTab[0]==R_ARMUJ) { f.ARMED=1; headFreeModeHold = heading; }else if (rxBufTab[0]==R_SILNIKI) { temp16=0; temp16|=rxBufTab[2]; temp16=temp16<<8; temp8=rxBufTab[1]; temp16|=temp8; rfmRcData[THROTTLE]=500;//temp16; rfmRcData[ROLL]=500; rfmRcData[PITCH]=500; rfmRcData[YAW]=500; } to_rx_mode(); } loop(); } return 0; }