/**
  ******************************************************************************
  * @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"
			
uint8_t running_dma = 0;

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(); // Trapped here if init fail

		// Init the SPI with DMA for communication
		initSPI(WITHDMA);
		init_fifo();
		// Init the general ms counter
		initMsTimer();


		GPIO_SetBits(GPIOA, LED);
	}

	/* Start UART RX */


	/* Handle flags and DMA transfer */
	uint16_t ctime = 0;
	uint16_t ntime = 0;
	while(1){
		ctime = TIM2->CNT;
		if(ctime >= ntime) {
			spi_CS(1);
			if(!running_dma) {
				send_packet();
				running_dma = 1;
			}
			ntime = TIM2->CNT + 5;
			tim1Wait(60000);
		}
	}
}

void DMA1_Channel3_IRQHandler(void)
{
    //
    //     Test if DMA Stream Transfer Complete interrupt
    //
    if (DMA_GetITStatus(DMA1_IT_TC3) == SET)
    {
        DMA_ClearITPendingBit(DMA1_IT_TC3);
        DMA_Cmd(DMA1_Channel3, DISABLE);
        SPI1->CR2 &= ~2;
        //
        //    The following is required to ensure that the chip select is not
        //    changed while data is still being transmitted.
        //
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
        //
        //    Now set chip select to high.
        //
        running_dma = 0;
        spi_CS(0);
    }
}
