/**
  ******************************************************************************
  * @file    main.c
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/


#include "stm32f10x.h"
#include "periph.h"
#include "lmk61e2.h"
#include "clock.h"
#include "datafifo.h"
#include "radio.h"
			

int main(void)
{
	SystemInit();

	initGPIO();
	initI2C();

	/* Init and start the TI clock */
	uint8_t valid_conf = 1;

	if(validPLL(VNDRID_BY1, 0x10)) {
		writeTIPLL(OUTDIV_BY1, 0x00); // Outdiv = 192;
		if(!validPLL(OUTDIV_BY1, 0x00)) valid_conf = 0;
		writeTIPLL(OUTDIV_BY0, 0xC0); // Outdiv = 192;
		if(!validPLL(OUTDIV_BY0, 0xC0)) valid_conf = 0;

		writeTIPLL(PLL_CTRL0, 0x04); // Remove Doubler
		if(!validPLL(PLL_CTRL0, 0x04)) valid_conf = 0;

		writeTIPLL(PLL_NDIV_BY1, 0x00); // Integer part = 99
		if(!validPLL(PLL_NDIV_BY1, 0x00)) valid_conf = 0;
		writeTIPLL(PLL_NDIV_BY0, 0x63); // Integer part = 99
		if(!validPLL(PLL_NDIV_BY0, 0x63)) valid_conf = 0;

		writeTIPLL(PLL_FRACNUM_BY2, 0x00); // Fractionnal numerator = 84
		if(!validPLL(PLL_FRACNUM_BY2, 0x00)) valid_conf = 0;
		writeTIPLL(PLL_FRACNUM_BY1, 0x00); // Fractionnal numerator = 84
		if(!validPLL(PLL_FRACNUM_BY1, 0x00)) valid_conf = 0;
		writeTIPLL(PLL_FRACNUM_BY0, 0x54); // Fractionnal numerator = 84
		if(!validPLL(PLL_FRACNUM_BY0, 0x54)) valid_conf = 0;

		writeTIPLL(PLL_FRACDEN_BY2, 0x00); //Fractionnal denumerator = 100
		if(!validPLL(PLL_FRACDEN_BY2, 0x00)) valid_conf = 0;
		writeTIPLL(PLL_FRACDEN_BY1, 0x00); //Fractionnal denumerator = 100
		if(!validPLL(PLL_FRACDEN_BY1, 0x00)) valid_conf = 0;
		writeTIPLL(PLL_FRACDEN_BY0, 0x64); //Fractionnal denumerator = 100
		if(!validPLL(PLL_FRACDEN_BY0, 0x64)) valid_conf = 0;

		writeTIPLL(DIFFCTL, 0x02); // LVDS Output
		if(!validPLL(DIFFCTL, 0x02)) valid_conf = 0;
	}

	if(valid_conf) {
		GPIO_SetBits(GPIOB, PLL_EN);
		GPIO_ResetBits(GPIOA, LED);

		/* Switch to the external 13MHz clock */
		SetSysClockTo65();
		SystemCoreClockUpdate();

		/* Init UART and SPI */
		// Init the SPI without dma for SI4461 configuration
		initSPI(NODMA);
		initSi446x();

		// Init the SPI with DMA for communication
		initSPI(WITHDMA);
		init_fifo();


		GPIO_SetBits(GPIOA, LED);
	}


	/* Start SI446x */


	/* Start UART RX */


	/* Poll the FIFO and SEND */

	for(;;);
}
