void nrf24l01_RX_config_slave(void)
{
	ioport_set_pin_low(nrf24l01S_CE);
	
	SPI_MasterSSHigh(&PORTC, PIN4_bm);
	delay_us(20);
	SPI_MasterSSLow(&PORTC, PIN4_bm);
	delay_us(20);
	
	rf_writebuf_slave(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
	rf_writereg_slave(WRITE_REG + EN_AA, 0x01);//enable autoactive 0x01
	rf_writereg_slave(WRITE_REG + EN_RXADDR, 0x01);
	rf_writereg_slave(WRITE_REG + RF_CH, 40);
	rf_writereg_slave(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH);
	rf_writereg_slave(WRITE_REG + RF_SETUP, 0x09);
	rf_writereg_slave(WRITE_REG + CONFIG, 0x0f);
	
	//SPI_MasterSSLow(ssPort, PIN4_bm);
	ioport_set_pin_high(nrf24l01S_CE);
	delay_us(150);//at least 130us
	
	PORT_ConfigurePins( &PORTC,
	0x01,	//set pin PK0 as input 'IRQ';
	false,
	false,
	PORT_OPC_TOTEM_gc,
	PORT_ISC_FALLING_gc );//set falling edge as trigger;
	
	PORT_SetPinsAsInput( &PORTC, 0x01 );
	
	/* Configure Interrupt0 to have medium interrupt level, triggered by pin 0. */
	PORT_ConfigureInterrupt0( &PORTC, PORT_INT0LVL_MED_gc, 0x01 );		
}
void naiboard_uart_init(void) {
	sysclk_enable_peripheral_clock(&USARTMODULE);
	// We're only using the stdout, stdin is handled by the AVR USART driver.
    stdout = &mystdout;

	PORT_SetPinsAsOutput(&USARTPORT, USARTTXPIN);
	PORT_ConfigurePins(&USARTPORT, USARTTXPIN, false, false, PORT_OPC_WIREDANDPULL_gc, PORT_ISC_INPUT_DISABLE_gc);
	PORT_SetPinsAsInput(&USARTPORT, USARTRXPIN);
	PORT_ConfigurePins(&USARTPORT, USARTRXPIN, false, false, PORT_OPC_PULLDOWN_gc, PORT_ISC_FALLING_gc);

	USART_InterruptDriver_Initialize(&naiboard_uart, &USARTMODULE, USART_DREINTLVL_LO_gc);
	USART_RxdInterruptLevel_Set(&USARTMODULE, USART_RXCINTLVL_LO_gc);

	USART_Format_Set(&USARTMODULE, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);
	USART_Baudrate_Set(&USARTMODULE, 1603, -6); // 48MHz, 115200bps
	USART_Rx_Enable(&USARTMODULE);
	USART_Tx_Enable(&USARTMODULE);
}
int main(void)
{
    uint8_t cmdBuf[128];
    uint8_t i;

    // Init peripherals.
    // Clock is set to 32MHz.
    clockInit();
    // This UART is connected to the UC3 device and provides connectivity
    // via USB.
    uartInit(&UARTC0, 8);	// 115,200 BAUD
    // This UART will be connected to the RadioBlocks device.
    uartInit(&UARTF0, 8); // 115,200 BAUD

    // Init the globals.
    isrLen = 0;
    cmdLen = 0;
    cmdFlag = 0;
    testCmd = 0;
    wakeCmd = 0;
    isrCmd = 0;
    ackStatus = 0;

    // Fun!
    ledFlag = 0;

    // These are used to help in debug. Used to print strings
    // to USARTC0 which is connected to the xplained-a1 usb
    // port through the on-board UC3.
    userEnd = 0;
    userStart = 0;

    // DEBUG - Create delay timer.
    //startTimer(1000); // One millisecond test.

    // Configure PORTA as output to measure delay timer...
    // These pins are on Xplained header J2
    PORT_SetPinsAsOutput( &PORTA, 0xFF );

    // Use one of the Xplained-A1 pins. SW4 - PD4
    PORT_SetPinsAsInput( &PORTD, 0x10 );

    // Check UART operation
    //testUartTx();

    // Enable global interrupts.
    sei();

    // Create a function pointer to use with the user uart (UARTC0).
    void (*puartBuf)(uint8_t* , uint8_t);

    // Assign that function pointer to the send data to RadioBlocks.
    puartBuf = &sendUARTF0;

    ///////////////////////	TEST CODE //////////////////
#if 0
    for(uint16_t i=0; i<CIRCSIZE; i++)
        sniffBuff[i] = 255;

    toggleLed(puartBuf, LED_TOGGLE, uartBuf);
    testRequest(puartBuf, uartBuf);
    setAddress(puartBuf, 0x1234, uartBuf);
    getAddress(puartBuf, uartBuf);
    sleepRequest(puartBuf, 1000, uartBuf);
    settingsRequest(puartBuf, uartBuf, RESTORE_CURRENT_SETTINGS);
    configureUART(puartBuf, DATA_BITS_8, PARITY_NONE, STOP_BITS_1, BAUD_115200, uartBuf);
    setPanid(puartBuf, 0x5678, uartBuf);
    getPanid(puartBuf, uartBuf);
    setChannel(puartBuf, CHANNEL_16, uartBuf);
    getChannel(puartBuf,uartBuf);
    setTRXState(puartBuf, TX_ON, uartBuf);
    getTRXState(puartBuf, uartBuf);
    dataRequest(puartBuf, 0x0001, DATA_OPTION_NONE, 0x42, 6, testBuf, uartBuf);
    setTxPower(puartBuf, TX_POWER_2_8_DBM, uartBuf);
    getTxPower(puartBuf, uartBuf);
    //setSecurityKey(puartBuf, uint8_t* key, uartBuf); // max 16 bytes.
#endif

    toggleLed(puartBuf, LED_TOGGLE, uartBuf);
    processResponse();
    usartUartPrint();

    testRequest(puartBuf, uartBuf);
    processResponse();
    usartUartPrint();
    processResponse();
    usartUartPrint();

    setAddress(puartBuf, 0x1234, uartBuf);
    processResponse();
    usartUartPrint();

    getAddress(puartBuf, uartBuf);
    processResponse();
    usartUartPrint();
    processResponse();
    usartUartPrint();

    setPanid(puartBuf, 0x5678, uartBuf);
    processResponse();
    usartUartPrint();
    getPanid(puartBuf, uartBuf);
    processResponse();
    usartUartPrint();
    processResponse();
    usartUartPrint();

    setChannel(puartBuf, CHANNEL_16, uartBuf);
    processResponse();
    usartUartPrint();
    getChannel(puartBuf,uartBuf);
    processResponse();
    usartUartPrint();
    processResponse();
    usartUartPrint();

//	setTRXState(puartBuf, TX_ON, uartBuf);
//	processResponse();
//	getTRXState(puartBuf, uartBuf);
//	processResponse();
//	processResponse();

    setTxPower(puartBuf, TX_POWER_2_8_DBM, uartBuf);
    processResponse();
    usartUartPrint();
    getTxPower(puartBuf, uartBuf);
    processResponse();
    usartUartPrint();
    processResponse();
    usartUartPrint();

    dataRequest(puartBuf, 0x0001, DATA_OPTION_NONE, 0x42, 6, testBuf, uartBuf);
    processResponse();
    usartUartPrint();

    setTRXState(puartBuf, RX_ON, uartBuf);
    processResponse();
    usartUartPrint();
    getTRXState(puartBuf, uartBuf);
    processResponse();
    usartUartPrint();
    processResponse();
    usartUartPrint();

    while(1)
    {
        processResponse();
        usartUartPrint();
        // Fun.
//		toggleLed(puartBuf, LED_TOGGLE, uartBuf);
//		timerLoop(100);	// WARNING, can BLOCK a loooong time.
//		processResponse();
//		testBuf[5]++;
//		setTRXState(puartBuf, TX_ON, uartBuf);
//		processResponse();
//		dataRequest(puartBuf, 0x0001, DATA_OPTION_NONE, 0x42, 6, testBuf, uartBuf);
//		processResponse();
//		setTRXState(puartBuf, RX_ON, uartBuf);
//		processResponse();



        /* USER CODE HERE! */

    }
}