/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include "mcu.h"
#include "pdl_header.h"
#include "pdl.h"
#include "gpio.h"
#define BUILD_S6E2CC

//NRF24L01 registers
#define NRF_CONFIG      0x00
#define EN_AA       0x01
#define EN_RXADDR   0x02
#define SETUP_AW    0x03
#define SETUP_RETR  0x04
#define RF_CH       0x05
#define RF_SETUP    0x06
#define NRF_STATUS  0x07
#define OBSERVE_TX  0x08
#define CD          0x09
#define RX_ADDR_P0  0x0A
#define RX_ADDR_P1  0x0B
#define RX_ADDR_P2  0x0C
#define RX_ADDR_P3  0x0D
#define RX_ADDR_P4  0x0E
#define RX_ADDR_P5  0x0F
#define TX_ADDR     0x10
#define RX_PW_P0    0x11
#define RX_PW_P1    0x12
#define RX_PW_P2    0x13
#define RX_PW_P3    0x14
#define RX_PW_P4    0x15
#define RX_PW_P5    0x16
#define FIFO_STATUS 0x17
#define DYNPD	    0x1C
#define FEATURE	    0x1D
//Instructions
#define R_REGISTER    0x00
#define W_REGISTER    0x20
#define REGISTER_MASK 0x1F
#define ACTIVATE      0x50
#define R_RX_PL_WID   0x60
#define R_RX_PAYLOAD  0x61
#define W_TX_PAYLOAD  0xA0
#define W_ACK_PAYLOAD 0xA8
#define FLUSH_TX      0xE1
#define FLUSH_RX      0xE2
#define REUSE_TX_PL   0xE3
#define RF24_NOP      0xFF

//Master address
const char Address[6]= "00001";

static stc_mfs_csio_config_t stcCsio3Config;
//static stc_csio_serial_timer_t stcCsioSerialTimer;

volatile stc_mfsn_csio_t* CsioCh3 = &CSIO3;
void Delay (uint64_t sk){
	uint64_t i;
	for (i=0; i<sk; i++);
}

void CSN (uint8_t reiksme){
	
	Gpio1pin_Put(GPIO1PIN_PF0, reiksme);
}

void CE (uint8_t reiksme){
	Gpio1pin_Put(GPIO1PIN_P7D, reiksme);
}
void Write_Reg (uint8_t reg, uint8_t data){
	Mfs_Csio_EnableFunc(&CSIO3, CsioTx);
}
void SPI_Init(void){
	  SetPinFunc_SIN3_1();		// MCU dokumentacijoje 46 pinas arba CN7-4 PIN (MISO), arduininis pinoutas http://www.cypress.com/file/290916/download, o cia radau pinu suderinima, apie puslapi, LQP  http://www.cypress.com/file/235126/download
    SetPinFunc_SOT3_1();		//47 pinas arba CN7-5 PIN (MOSI)
    SetPinFunc_SCK3_1();		//48 pinas arba CN7-6 (CLK)
	
	PDL_ZERO_STRUCT(stcCsio3Config);

	
/*stcCsioSerialTimer.enClkDiv = CsioTimerDiv2;
	stcCsioSerialTimer.u8TransferByteCnt = 8u;
	stcCsioSerialTimer.u16CompareVal = 1;
	*/
	
	stcCsio3Config.enMsMode = CsioMaster;
    stcCsio3Config.enActMode = CsioActSpiMode;
    stcCsio3Config.bInvertClk = FALSE;
    stcCsio3Config.u32BaudRate = 400000;
    stcCsio3Config.enDataLength = CsioEightBits;
	stcCsio3Config.enSyncWaitTime = CsioSyncWaitZero;
    stcCsio3Config.enBitDirection = CsioDataMsbFirst;
    stcCsio3Config.enSyncWaitTime = CsioSyncWaitZero;
	stcCsio3Config.pstcCsConfig = NULL;//&stcCsio6CsConfig;
	stcCsio3Config.pstcSerialTimer =  NULL;//&stcCsioSerialTimer;
    stcCsio3Config.pstcFifoConfig = NULL;
    stcCsio3Config.pstcIrqEn = NULL;               ///< Pointer to CSIO interrupt enable structure, if set to NULL, no interrupt enabled.
    stcCsio3Config.pstcIrqCb = NULL;               ///< Pointer to CSIO interrupt callback functions structure, if set to NULL, no interrupt callback initialized.
    stcCsio3Config.bTouchNvic = FALSE; 		
	
	while(Ok!=Mfs_Csio_Init(CsioCh3, &stcCsio3Config));
}

int main(void)
{
		//when Delay(100) then period T is 518 ns or 1.930 MHz
		uint8_t RxData[1];
	
		Gpio1pin_InitOut(GPIO1PIN_PF0, Gpio1pin_InitVal(1u));  //CSN init as 0
		Gpio1pin_InitOut(GPIO1PIN_P7D, Gpio1pin_InitVal(0u));	//CE init as 0
	
		SPI_Init();
	
		Delay (50000); //Turn on Delay, in datasheet it was specified 150 uS, I give ~250 uS
		Mfs_Csio_EnableFunc(&CSIO3, CsioTx);
		Mfs_Csio_EnableFunc(&CSIO3, CsioRx);
		CE(1);
		Delay(1000);	//4 uS Delay between CE 1 and CSN 0
		CSN(0);
		Mfs_Csio_SendData(&CSIO3, 0x07, FALSE); 	//asking for status report
		while (TRUE != Mfs_Csio_GetStatus(&CSIO3, CsioRxFull));
		RxData[0]=Mfs_Csio_ReceiveData(&CSIO3);
		while (TRUE != Mfs_Csio_GetStatus(&CSIO3, CsioTxIdle));
		
	//}
 /* Disable RX function of CSIO3 */
		Mfs_Csio_DisableFunc(&CSIO3, CsioTx);
		Mfs_Csio_DisableFunc(&CSIO3, CsioRx);
		CSN(1);	//CSN set 1
		CE(0);
///}
    while(1){
		}
}
