Example #1
0
void initRadio(void){
	nrf24_init();
	nrf24_config(NRF24_CHANNEL_MHZ, NRF24_PAYLOAD_LEN);//channel 15, 16 byte payload
	nrf24_tx_address(ptxAddr);
	nrf24_rx_address(prxAddr);

}
Example #2
0
bool RHReliableDatagram::init() {
	spi_init();

	if (nrf24_init())
		return 0;

	update_rx_addr(addr);
	nrf24_rx_mode();

	return 1;
}
//system initialize function
void sys_init(void)
{
    TRISB&=0xE7;                            //ss & ce are output, RB2 is input
    ss=1;
    ce=1;
    TRISA=0xFF;                 //RA0 is analog input
    TRISC=0xF8;                  //lcd control pins
    buzzer=0;                   //fire alarm buzzer
    TRISD=0x00;                  //lcd data bus
    lcd_init();                  //initialize LCD
    adc_init();               //initialize ADC
    interrupt_init();         //initialize interrupts
    SPI_master_init(0x00, SLOW);          //initialize MSSP module for SPI communication
    nrf24_init();          //initialize nRF24L01+
}
Example #4
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(nrf24_thread(struct pt *pt))
{
    PT_BEGIN(pt);

    /* init hardware pins */
    nrf24_init();
    
    /* Channel #2 , payload length: 8 */
    nrf24_config(2,8);    

    /* Set the device addresses */
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);

    uint32_t counter;
    uint16_t temp;
    int16_t realval;    

    while(1)
    {
        PT_WAIT_UNTIL(pt,nrf24_dataReady());

        dbg(PSTR("> -------------------------------------\r\n"));        
        
        nrf24_getData(data_array);

        temp = (data_array[0]<<8)+data_array[1];

        realval  = temp * 10;
        realval /= 11;             
        realval -= 500;

        dbg(PSTR("> RAW Temp: %u\r\n"),temp);
        dbg(PSTR("> Temp: %u\r\n"),realval);

        temp = (data_array[2]<<8)+data_array[3];

        dbg(PSTR("> Batt: %u\r\n"),temp);

        counter = (data_array[4]<<24)+(data_array[5]<<16)+(data_array[6]<<8)+data_array[7]; 

        dbg(PSTR("> Counter: %u\r\n"),counter);

    }

    PT_END(pt);
}
Example #5
0
int main(int argc, char **argv){

 uint8_t data_array[4];
 uint8_t tx_address[5] = { 0xE7, 0xE7, 0xE7, 0xE7, 0xE7 };
 uint8_t rx_address[5] = { 0xD7, 0xD7, 0xD7, 0xD7, 0xD7 };

 if (!bcm2835_init())
  return 1;

 mysql_connect();

 /* init hardware pins */
 nrf24_init();

 /* Channel #2 , payload length: 4 */
 nrf24_config(2, 4);

 /* Set the device addresses */
 nrf24_tx_address(tx_address);
 nrf24_rx_address(rx_address);

 printf("Configured\n");

 while (1){
  if (nrf24_dataReady()) {
   nrf24_getData(data_array);
   printf("> %d %d %d %d \n",
    data_array[0],
    data_array[1],
    data_array[2],
    data_array[3]);
  if (argc>1) {
   if (argv[1][0]=='1') {
    writeCpm(
     data_array[0],
     data_array[1],
     data_array[2],
     data_array[3]);
    }
   }
  }
 }
 mysql_disconnect();
 bcm2835_close();
 return 0;
}
Example #6
0
int main(void) {
    // LCD Screen
    lcd_init();
    terminal_init();

    // Input
    input_init();

    // Set power LED
    DDRB |= (1 << PINB6);
    clear_bit(PORTB, PINB6);

    // Radio
    uint8_t local_addr[] = { 0x1B, 0x1B, 0x1B, 0x1B, 0x1B };
    uint8_t robot_addr[] = { 0x0A, 0x0A, 0x0A, 0x0A, 0x0A };
    spi_init();
    nrf24_init();
    nrf24_config(47, sizeof(RemoteCommand)); // Channel 7
    nrf24_set_rx_addr(local_addr);
    nrf24_set_tx_addr(robot_addr);

    // Ready to go!
    sei();

    RemoteMode mode = MODE_MANUAL;

    while (1)
    {
        input_update(&input_state);

        switch (mode) {
        case MODE_MANUAL:
            UpdateManualMode(&input_state);
            break;
        case MODE_AUTO:
            asm("nop"::);
            break;
        case MODE_MENU:
            asm("nop"::);
            break;
        }
    }
}
Example #7
0
/*---------------------------------------------------------------------------*/
void init_hardware()
{
    /* init interrupt pin */
    pinMode(B,2,INPUT);

    /* init led ... */
    led0_init();
    led0_low();

    /* init hardware pins */
    nrf24_init();
    
    /* disable the analog comparator */
    ACSR = (1<<ACD); 

    /* channel #2 , payload length: 4 */
    nrf24_config(2,32);

    /* get the rx address from EEPROM */
    rx_address[0] = eeprom_read_byte((uint8_t*)10);
    rx_address[1] = eeprom_read_byte((uint8_t*)11);
    rx_address[2] = eeprom_read_byte((uint8_t*)12);
    rx_address[3] = eeprom_read_byte((uint8_t*)13);
    rx_address[4] = eeprom_read_byte((uint8_t*)14);

    /* set the device addresses */
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);    

    /* initial power-down */
    nrf24_powerDown();

    /* disable digital buffer at the adc pin */
    cbi(DIDR0,ADC1D);

    PRR = (1<<PRTIM1)|(1<<PRTIM0)|(0<<PRADC)|(0<<PRUSI);
}
Example #8
0
void setup(void) {
	uint8_t s = SREG;
	uint8_t m = MCUCR;
	uint8_t i, addrs[6];

	wdt_disable();
	serial_init();
	timer_init();
	spi_init();
	nrf24_init();
	sei();

	/*
	 * Set our radio address and the remote end's radio address, read
	 * the addresses from EEPROM where they need to be saved first.
	 */
	for (i = 0; i < 6; i ++)
		addrs[i] = eeprom_read(i);

	nrf24_set_rx_addr(addrs + 0);
	nrf24_set_tx_addr(addrs + 3);

	/* Write something to say hello */
	serial_write_str("SREG:");
	serial_write_hex16(s);
	serial_write_str(", MCUCR:");
	serial_write_hex16(m);
	serial_write_str(", our addr: ");
	serial_write1(addrs[0]);
	serial_write1(addrs[1]);
	serial_write1(addrs[2]);
	serial_write_eol();

	nrf24_rx_mode();

	serial_set_handler(handle_input);
}
Example #9
0
int main() {
 DDRB |= 1<<PORTB3;
 PORTB |= 1<<PORTB3;

 ledPort |= _BV(ledPin);

 /* init hardware pins */
 nrf24_init();

 /* Channel #2 , payload length: 4 */
 nrf24_config(2,4);

 /* Set the device addresses */
 nrf24_tx_address(tx_address);
 nrf24_rx_address(rx_address);

 pwm_init();   // initialize timer in PWM mode
 interupt_init();  //initalize Interrupt 0

 /* Fill the data buffer */
 data_array[0] = deviceID;
 data_array[1] = 0xAA;

 while(1) {
  data_array[3] = events;
  data_array[2] = events >> 8;
  nrf24_send(data_array);

  /* Wait for transmission to end */
  while(nrf24_isSending()) {
  }

  ledPort ^= _BV(ledPin);
  _delay_ms(80000);
 }
}
Example #10
0
/* ------------------------------------------------------------------------- */
int main()
{
    /* init the software uart */
    uart_init();

    /* init the xprintf library */
    xdev_out(uart_put_char);

    /* simple greeting message */
    xprintf("\r\n> TX device ready\r\n");
    
    /* init hardware pins */
    nrf24_init();
    
    /* Channel #2 , payload length: 4 */
    nrf24_config(2,4);

    /* Set the device addresses */
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);    

    while(1)
    {                
        /* Fill the data buffer */
        data_array[0] = 0x00;
        data_array[1] = 0xAA;
        data_array[2] = 0x55;
        data_array[3] = q++;                                    

        /* Automatically goes to TX mode */
        nrf24_send(data_array);        
        
        /* Wait for transmission to end */
        while(nrf24_isSending());

        /* Make analysis on last tranmission attempt */
        temp = nrf24_lastMessageStatus();

        if(temp == NRF24_TRANSMISSON_OK)
        {                    
            xprintf("> Tranmission went OK\r\n");
        }
        else if(temp == NRF24_MESSAGE_LOST)
        {                    
            xprintf("> Message is lost ...\r\n");    
        }
        
		/* Retranmission count indicates the tranmission quality */
		temp = nrf24_retransmissionCount();
		xprintf("> Retranmission count: %d\r\n",temp);

		/* Optionally, go back to RX mode ... */
		nrf24_powerUpRx();

		/* Or you might want to power down after TX */
		// nrf24_powerDown();            

		/* Wait a little ... */
		_delay_ms(10);
    }
}
Example #11
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  static u8 status;
  bool flag_display_retrans_cnt;
  disableInterrupts();
  Config();
  Errors_Init();
  enableInterrupts();
  Goto_HALT();
  
  while (1)
  {
    if((btn_pressed != BUTTON1) && (btn_pressed != BUTTON2))
    {
      DELAY_STOP;
      goto sleep;
    }
    
    PWR_PVDCmd(ENABLE);                    /* Power voltage detector and brownout Reset unit supply current 2,6uA */
    PWR_PVDLevelConfig(PWR_PVDLevel_2V26); /* Set Programmable voltage detector threshold to 2,26V */
    if(DELAY_EXPIRED)
      flag_display_retrans_cnt = TRUE;
    else 
      flag_display_retrans_cnt = FALSE;
    while(!DELAY_EXPIRED);           /* wait for power-up delay to expire (199.68ms) - needed for nRF24L01+ power up */
    DELAY_STOP;
    
    CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
    nrf24_init();                          /* init hardware pins */
    nrf24_config(0, 6);                    /* Channel #0 , payload length: 6 */
    nrf24_tx_address(tx_address);          /* Set the device addresses */
    nrf24_rx_address(rx_address);
    
    /* Check NRF24 Init error */
    if(Errors_CheckError(ERROR_NRF24_INIT))
    {
      LED_Red_Blink(2);
      goto sleep;
    }    

    TXmsg.M.status = 0;                    /* Reset status byte */
    
    /* Check Programmable voltage detector (PVD) output flag, if battery low send this information */
    /* Internal reference voltage should be stable at this point */
    if(PWR->CSR1 & PWR_FLAG_PVDOF)
    {
      /* VDD/VDDA is below the VPVD threshold (2,26V) */
      TXmsg.M.status |= STATUS_LOWBATT;
    }
    PWR_PVDCmd(DISABLE);
    
    if(btn_pressed == BUTTON1)
    {
      TXmsg.M.ownID = (u32)ownID_btn1;
      TXmsg.M.txcnt = tx_cnt++;
      nrf24_send((u8*)&TXmsg.B.b[0]);
    }
    else if(btn_pressed = BUTTON2)
    {
      TXmsg.M.ownID = (u32)ownID_btn2;
      TXmsg.M.txcnt = tx_cnt++;
      nrf24_send((u8*)&TXmsg.B.b[0]);
    }
    btn_pressed = 0;

    do
    {
      status = nrf24_getStatus();
    }
    while(!(status & ((1 << TX_DS)  | (1 << MAX_RT))));

    if(status & (1 << TX_DS))  /* send successful */
    {
      if(flag_display_retrans_cnt)
      {
        u8 i;
        u8 retrans_cnt = nrf24_retransmissionCount();
        LED_Green_Blink(retrans_cnt);
      }
      else
      {
        LED_Green_Blink(1);
      }
    }
    else if(status & (1 << MAX_RT))  /* send failed, max retries exceeded */
    {
      LED_Red_Blink(1);
    }
    
    sleep:
    Goto_HALT();
  }
}
Example #12
0
static void nrf24_test(void) {
	uint8_t rx_address[5] = {0xA7,0x95,0xF1,0x36,0x07};
	uint8_t tx_address[5] = {0xA7,0x95,0xF1,0x36,0x06};
	//uint8_t tx_address[5] = {0x17,0x97,0xA7,0xA7,0xD7};
	uint8_t addr[10] = {0x1};
	uint8_t val = 0x17;
	uint8_t reg;
	uint8_t data_array[6];
	uint32_t counter = 0;

	spi_delay(1000000);
	/* init hardware pins */
	nrf24_init();

	if (spi_init() < 0) {
		printf("ERRRRROR SPI!\n");
	}

	/* Returned value is not correct on the first read, who knows why... */
	nrf24_readRegister(CONFIG, &reg, 1);

	nrf24_readRegister(CONFIG, &reg, 1);
	printf("CONFIG (before configured) = %x\n", reg);
	nrf24_readRegister(EN_AA, &reg, 1);
	printf("EN_AA = %x\n", reg);
	nrf24_readRegister(RF_SETUP, &reg, 1);
	printf("RF_SETUP = %x\n", reg);
	nrf24_readRegister(SETUP_AW, &reg, 1);
	printf("SETUP_AW = %x\n", reg);
	nrf24_readRegister(SETUP_RETR, &reg, 1);
	printf("SETUP_RETR = %x\n", reg);
	nrf24_readRegister(FIFO_STATUS, &reg, 1);
	printf("FIFO_STATUS = %x\n", reg);

	nrf24_readRegister(RF_CH, &reg, 1);
	printf("RF_CH = %x\n", reg);
	nrf24_writeRegister(RF_CH, &val, 1);
	nrf24_readRegister(RF_CH, &reg, 1);
	printf("RF_CH = %x\n", reg);
	val = 2;
	nrf24_writeRegister(RF_CH, &val, 1);
	nrf24_readRegister(RF_CH, &reg, 1);
	printf("RF_CH = %x\n", reg);

#if 1
	nrf24_readRegister(RX_ADDR_P0, addr, 5);
	printf("RX_ADDR_P0: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
	nrf24_writeRegister(RX_ADDR_P0, tx_address,5);
	spi_delay(1000000);
	nrf24_readRegister(RX_ADDR_P0, addr, 5);
	printf("RX_ADDR_P0: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
	
	spi_delay(1000000);
	nrf24_readRegister(RF_CH, &reg, 1);

	nrf24_readRegister(TX_ADDR, addr, 5);
	printf("TX_ADDR: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
	nrf24_writeRegister(TX_ADDR, tx_address,5);
	nrf24_readRegister(TX_ADDR, addr, 5);
	printf("TX_ADDR: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);

	spi_delay(1000000);

	nrf24_readRegister(RX_ADDR_P1, addr, 5);
	printf("RX_ADDR_P1: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
	nrf24_writeRegister(RX_ADDR_P1, rx_address,5);
	nrf24_readRegister(RX_ADDR_P1, addr, 5);
	printf("RX_ADDR_P1: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
#endif

	/* Channel #2 , payload length: 4 */
	nrf24_config(16,6);

    nrf24_readRegister(FIFO_STATUS,&reg,1);
	printf("!> 1 FIFO_STATUS = %2X\n", reg);

	spi_delay(1000000);

	nrf24_readRegister(CONFIG, &reg, 1);
	nrf24_readRegister(CONFIG, &reg, 1);
	printf("CONFIG (after configured) = %x\n", reg);
	nrf24_readRegister(EN_AA, &reg, 1);
	printf("EN_AA = %x\n", reg);
	nrf24_readRegister(RF_SETUP, &reg, 1);
	printf("RF_SETUP = %x\n", reg);

	nrf24_readRegister(TX_ADDR, addr, 5);
	printf("TX addr is: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
	//spi_delay(1000000);
	nrf24_readRegister(RX_ADDR_P0, addr, 5);
	printf("RX addr is: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
	//spi_delay(1000000);
	nrf24_readRegister(RX_ADDR_P1, addr, 5);
	printf("RX_ADDR_P1 addr is: %x %x %x %x %x\n", addr[0], addr[1], addr[2], addr[3], addr[4]);
    
	nrf24_readRegister(FIFO_STATUS,&reg,1);
	printf("!!!!!!> 2 FIFO_STATUS = %2X\n", reg);

	/* Set the device addresses */
	//nrf24_tx_address(rx_address);
	//nrf24_rx_address(rx_address);

	//nrf24_ce_digitalWrite(HIGH);

	//reg = nrf24_getStatus();
	//printf("STATUS = %2X\n", reg);

	nrf24_readRegister(FIFO_STATUS,&reg,1);
	printf("!!!!!!> 3 FIFO_STATUS = %2X\n", reg);

	while(1) {
		if(nrf24_dataReady())
		{
			counter = 0;
			//reg = nrf24_getStatus();
			//printf("STATUS = %x\n", reg);
			nrf24_getData(data_array);
			printf("> ");
			printf("%2X ",data_array[0]);
			printf("%2X ",data_array[1]);
			printf("%2X ",data_array[2]);
			printf("%2X ",data_array[3]);
			printf("%2X ",data_array[4]);
			printf("%2X\r\n",data_array[5]);
			//spi_delay(1000);
		} else if (counter > 100000) {
			reg = nrf24_getStatus();
			printf("(counter > 100000) STATUS = %2X\n", reg);
			nrf24_readRegister(FIFO_STATUS,&reg,1);
			printf("(counter > 100000) FIFO_STATUS = %2X\n", reg);
		}
		counter++;
	}
}
Example #13
0
File: main.c Project: ksrm/redox
int main() {

    uint8_t i, change;
    uint8_t hand;
    uint32_t timeout = 0;

    uart_init();
    xdev_out(uart_putchar);

    // Determine which hand this is from PE2
    // Left is hand 0, right is hand 1
    PORTE = (1 << 2);
    DDRE = 0;
    hand = (PINE & 0x04) ? 0 : 1;
    xprintf("\r\nHand %d\r\n", hand);
    
    // Initialise NRF24
    // Set the last byte of the address to the hand ID
    rx_address[4] = hand;
    nrf24_init();
    nrf24_config(CHANNEL, sizeof msg);
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);

    matrix_init();

    msg[0] = hand & 0x01;
    msg[1] = 0;
    msg[2] = 0;

    // Set up LED and flash it briefly
    DDRE |= 1<<6;
    PORTE = 1<<6;
    _delay_ms(500);
    PORTE = 0;

    get_voltage();
    check_voltage();

    // Scan the matrix and detect any changes.
    // Modified rows are sent to the receiver.
    while (1) {  
        timeout++;
        matrix_scan();

        for (i=0; i<ROWS; i++) {
            change = matrix_prev[i] ^ matrix[i];

            // If this row has changed, send the row number and its current state
            if (change) {
                if (DEBUG) xprintf("%d %08b -> %08b   %ld\r\n", i, matrix_prev[i], matrix[i], timeout);
                msg[1] = i;
                msg[2] = matrix[i];

                nrf24_send(msg);
                while (nrf24_isSending());
                timeout = 0;
            }

            matrix_prev[i] = matrix[i];
        }

        // Sleep if there has been no activity for a while
        if (timeout > SLEEP_TIMEOUT) {
            timeout = 0;
            enter_sleep_mode();
        }
    }

}
Example #14
0
int main(void)
{
	// set clock prescaler for 8MHz
	CLKPR = 0x80;
	CLKPR = 0x01;

	cli();

	power_all_disable();
	power_spi_enable();
	power_timer1_enable();
	set_sleep_mode(SLEEP_MODE_IDLE);

	pins_init();

	delay_ms(345); // arbitrary

	uint8_t dpi = 0x0f; // default to 800
	if (!(PIND & (1<<0))) // if left is pressed at boot
		dpi = 0xff; // set to 12800

	pmw3366_init(dpi);
	nrf24_init();

	// button stuff
	// previous debounced state
	uint8_t btn_prev = ~(PIND);
	// time (in 125us) button has been unpressed.
	// consider button to be released if this time exceeds DEBOUNCE_TIME.
	uint8_t btn_time[3] = {0, 0, 0};

	// absolute positions. relies on integer overflow
	union motion_data x = {0}, y = {0};

	// wheel stuff
	uint8_t whl_prev_same = 0; // what A was the last time A == B
	uint8_t whl_prev_diff = 0; // what A was the last time A != B
	// absolute scroll position. relies on integer overflow
	int8_t whl = 0;

	// begin burst mode for 3366
	spi_set3366mode();
	SS_3366_LOW;
	spi_3366_write(0x50, 0x00);
	SS_3366_HIGH;

	// set up timer1 to set OCF0A in TIFR0 every 1ms
	TCCR1A = 0x00;
	TCCR1B = 0x09; // CTC, 8MHz
	OCR1A = 7999; // main loop nominal period (7999 + 1) / 8MHz = 1ms
	OCR1B = 320; // timing of when to read burst mode data from sensor
	OCR1C = 800; // timing of when to load nrf24l01+ with data

	// let receiver know if it's the first time sending data, so that it
	// can reset the reference for absolute position and that there's no
	// jump when rebooting the mouse
	// uint8_t first = 0x80; // transmitted as MSB with button data below.

	// when sync reaches 0, always send a packet with bit 6 in btn set, to
	// tell the receiver to calculate the timing offset.
	// when sync reaches 1, always send a packet requesting ACK to load the
	// timing offset
	// i.e. when it overflows, so 256ms periodicity.

	// when sync reaches 0, afk increments.
	// afk is cleared by any motion or button press.
	// when afk reaches AFK_TIMEOUT, go into powerdown mode.
	for (uint8_t first = 0x80, sync = 0, afk = 0; ; first = 0, sync++) {
		// sync to 1ms intervals using timer1
	//	if (TIFR1 & (1<<OCF1A)) PORTD |= (1<<6);
		TIMSK1 |= (1<<OCIE1A);
		sei(); sleep_mode(); cli();
		TIMSK1 &= ~(1<<OCIE1A);
		TIFR1 |= (1<<OCF1A); TIFR1 |= (1<<OCF1B); TIFR1 |= (1<<OCF1C);

		// begin burst mode read
		spi_set3366mode();
		SS_3366_LOW;
		spi_send(0x50);
		// do stuff here instead of busy waiting for 35us

		// read wheel
		int8_t dwhl = 0;
		const uint8_t whl_a = WHL_A_IS_HIGH;
		const uint8_t whl_b = WHL_B_IS_HIGH;
	//	if (whl_a == whl_b) {
	//		if (whl_a != whl_prev_same) {
	//			dwhl = 2 * (whl_a ^ whl_prev_diff) - 1;
	//			whl += dwhl;
	//			whl_prev_same = whl_a;
	//		}
	//	} else
	//		whl_prev_diff = whl_a;
		if (whl_a != whl_b)
			whl_prev_diff = whl_a;
		else if (whl_a != whl_prev_same) {
			dwhl = 2 * (whl_a ^ whl_prev_diff) - 1;
			whl += dwhl;
			whl_prev_same = whl_a;
		}

		// read buttons
		/*
		PIND 0 EIFR 0: low, no edges -> is low
		PIND 0 EIFR 1: low, edge -> is low
		PIND 1 EIFR 0: high, no edges -> always high during last 1ms
		PIND 1 EIFR 1: high, edge -> low at some point in the last 1ms
		*/
		const uint8_t btn_unpressed = PIND & ~(EIFR);
		EIFR = 0b00000111; // clear EIFR
		// manual loop debouncing for every button
		uint8_t btn_dbncd = 0x00;
		#define DEBOUNCE(index) \
		if ((btn_prev & (1<<index)) && (btn_unpressed & (1<<index))) { \
			btn_time[index]++; \
			if (btn_time[index] < DEBOUNCE_TIME) \
				btn_dbncd |= (1<<index); \
		} else { \
			btn_time[index] = 0; \
			btn_dbncd |= (~btn_unpressed) & (1<<index); \
		}

		DEBOUNCE(0);
		DEBOUNCE(1);
		DEBOUNCE(2);
		#undef DEBOUNCE

		// wait until 35us have elapsed since spi_send(0x50)
	//	if (TIFR1 & (1<<OCF1B)) PORTD |= (1<<6);
		TIMSK1 |= (1<<OCIE1B);
		sei(); sleep_mode(); cli();
		TIMSK1 &= ~(1<<OCIE1B);

		union motion_data dx, dy;
		spi_send(0x00); // motion, not used
		spi_send(0x00); // observation, not used
		dx.lo = spi_recv();
		dx.hi = spi_recv();
		dy.lo = spi_recv();
		dy.hi = spi_recv();
		SS_3366_HIGH;

		x.all += dx.all;
		y.all += dy.all;

		if (sync == 0) afk++;
		const uint8_t changed = (btn_dbncd != btn_prev) || dx.all || dy.all || dwhl;
		if (changed) afk = 0;

		if (changed || (sync <= 1)) {
			btn_prev = btn_dbncd;
			// W_TX_PAYLOAD if sync == 1, W_TX_PAYLOAD_NOACK otherwise
			const uint8_t mode = (sync == 1) ? 0b10100000 : 0b10110000;
			// send miscellaneous info using top bits of btn byte
			uint8_t btn_send = btn_dbncd | first; // first is either 0x80 or 0
			if (sync == 0) btn_send |= 0x40;

			// try to transmit at the same time every frame
	//		if (TIFR1 & (1<<OCF1C)) {PORTD |= (1<<6);}
			TIMSK1 |= (1<<OCIE1C);
			sei(); sleep_mode(); cli();
			TIMSK1 &= ~(1<<OCIE1C);

			spi_setnrf24mode();
			SS_NRF24_LOW;
			spi_send(0x20 | 0x07); // STATUS
			spi_send(0b01110000); // clear IRQ
			SS_NRF24_HIGH;
			SS_NRF24_LOW;
			spi_send(0b11100001); // flush tx
			SS_NRF24_HIGH;
			SS_NRF24_LOW;
			spi_send(0b11100010); // flush rx
			SS_NRF24_HIGH;

			SS_NRF24_LOW;
			spi_send(mode);
			spi_send(btn_send);
			spi_send(x.lo);
			spi_send(x.hi);
			spi_send(y.lo);
			spi_send(y.hi);
			spi_send(whl);
			SS_NRF24_HIGH;

			// pulse CE to transmit
			CE_HIGH;
			delay_us(12);
			CE_LOW;

			if (sync == 1) { // get ack payload of timing offset
				delay_us(400);
				if (IRQ_IS_LOW) {
					// recycle motion_data union for timing
					union motion_data offset;
					SS_NRF24_LOW;
					spi_send(0b01100001);
					offset.lo = spi_recv();
					offset.hi = spi_recv();
					SS_NRF24_HIGH;
					// shift TCNT1 by the offset, plus a
					// little more because of the time it
					// takes to add stuff to TCNT1.
					TCNT1 += offset.all + 11;
				}
			}
		}

		// power down if afk
		if (afk > AFK_TIMEOUT) {
			// enable external interrupts on INT0/1/2/3, PCINT0
			EIMSK = 0b00000111;
			PCICR = 0x01;
			// go power down mode; wake up on interrupt
			set_sleep_mode(SLEEP_MODE_PWR_DOWN);
			sei(); sleep_mode(); cli();
			// disable external interrupts
			PCICR = 0;
			EIMSK = 0;
			// restore state
			set_sleep_mode(SLEEP_MODE_IDLE);
			sync = 0;
			afk = 0;
		}
	}
}
Example #15
0
int main(void)
{
  /* Initialise the GPIO block */
  gpioInit();

  /* Initialise the UART0 block for printf output */
  //uart0Init(115200);
  uart0Init(9600);
  /* Configure the multi-rate timer for 1ms ticks */
  mrtInit(__SYSTEM_CLOCK/1000);

  /* Configure the switch matrix (setup pins for UART0 and GPIO) */
  //configurePins();
  spiInit(LPC_SPI0, 6, 0);
  SwitchMatrix_Init(); //uart & spi
  LPC_GPIO_PORT->DIR0 |= (1 << CSN);
  LPC_GPIO_PORT->DIR0 |= (1 << CE);
  uint8_t temp = 0;

  nrf24_init();




  /* Set the LED pin to output (1 = output, 0 = input) */
  #if !defined(USE_SWD)
    LPC_GPIO_PORT->DIR0 |= (1 << LED_LOCATION);
  #endif

    //printf("write");
    mrtDelay(500);

    nrf24_config(2,16);

	#if TX_NODE
    	nrf24_tx_address(tx_address);
    	nrf24_rx_address(rx_address);
	#else
    	nrf24_tx_address(rx_address); //backwards looking but is fine
    	nrf24_rx_address(tx_address);
	#endif

    uint8_t i = 0;
  while(1)
  {

      /* Turn LED On by setting the GPIO pin high */
      LPC_GPIO_PORT->SET0 = 1 << LED_LOCATION;
      mrtDelay(500);

      /* Turn LED Off by setting the GPIO pin low */
      LPC_GPIO_PORT->CLR0 = 1 << LED_LOCATION;
      mrtDelay(500);

	  /*
      printf("Send\r\n");
      for(i=0; i<16; i++){
    	  printf("%d: %d\n\r", i, tx_data_array[i]);
      }
	   */
      nrf24_send(tx_data_array);
      while(nrf24_isSending());
      nrf24_powerUpRx();
      //done transmitting, set back to rx mode

      for(i=0; i<16; i++){
    	  tx_data_array[i]++;
      }

/*
      if(nrf24_dataReady()){
    	  printf("\n\rGot data\n\r");

          nrf24_getData(rx_data_array);
          for(i=0; i<16; i++){
          printf("%d: %d\n\r", i, rx_data_array[i]);
          }
      }
*/
  }
}