/*
 * radio.c
 *
 *  Created on: 3 Jul 2018
 *      Author: nats
 */

#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "periph.h"
#include "datafifo.h"
#include "radio.h"

uint8_t beaconbuffer[48] = { 	0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xDC, 0x0F, 0xFE, 0xBA, 0xDF, 0x00, 0xD0,
								0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xDC, 0x0F, 0xFE, 0xBA, 0xDF, 0x00, 0xD0,
								0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xDC, 0x0F, 0xFE, 0xBA, 0xDF, 0x00, 0xD0,
								0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xDC, 0x0F, 0xFE, 0xBA, 0xDF, 0x00, 0xD0 };

TIM_TimeBaseInitTypeDef radTimConf;

void send_packet() {
	uint8_t * p = 0;
	if(read_packet(&p) == FIFOERROR) {
		// Nothing to send in the FIFO
		// We send the Toxic beacon
		dmaSPISend(beaconbuffer);
	} else {
		dmaSPISend(p);
	}
}

void si44_spisend(uint8_t data) {
	SPI_I2S_SendData(SPI1, data);
	while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
}

void si44_spiread(uint8_t *data, uint8_t size) {
	for(uint8_t i = 0; i<size;i++) {
		SPI_I2S_SendData(SPI1, 0);
		data[i] = SPI_I2S_ReceiveData(SPI1);
	}
}

void set_property(unsigned short property, unsigned char length) {
    si44_spisend(SET_PROPERTY);
    si44_spisend(property >> 8);
    si44_spisend(length);
    si44_spisend(property & 0xFF);
}

void initSiTimer() {
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	TIM_Cmd(TIM1, ENABLE);

	TIM_TimeBaseInitTypeDef radTimConf;
	TIM_TimeBaseStructInit(&radTimConf);

	radTimConf.TIM_ClockDivision = TIM_CKD_DIV1;
	radTimConf.TIM_Prescaler = 64;
	radTimConf.TIM_CounterMode = TIM_CounterMode_Up;
	radTimConf.TIM_Period = 0xFFFF;
	radTimConf.TIM_RepetitionCounter = 0;

	TIM_TimeBaseInit(TIM1, &radTimConf);
}

void tim1Wait(uint16_t us) {
	TIM1->SR = 0; // Clear All Flag !
	TIM1->CNT = 0;
	while(1) {
		uint16_t read_val = TIM1->CNT;
		if(read_val >= us) break;
	}
}

void initSi446x() {
	initSiTimer();

	GPIO_SetBits(GPIOB, SI_SHDN);
	tim1Wait(10000);
	GPIO_ResetBits(GPIOB, SI_SHDN);
	tim1Wait(10000);

	check_part_info();
	
	GPIO_ResetBits(GPIOA, SI_CS);
	si44_spisend(POWER_UP);
	si44_spisend(1);
	si44_spisend(1);
	GPIO_SetBits(GPIOA, SI_CS);
	tim1Wait(60000);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
	set_property(GLOBAL_XO_TUNE, 1);
    si44_spisend(0);
	GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
	set_property(GLOBAL_CONFIG, 1);
    si44_spisend(0xC0);
	GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
	set_property(MODEM_MOD_TYPE,3);
    si44_spisend(1<<7);
    si44_spisend(0<<7);
    si44_spisend(0x03);
	GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
	set_property(MODEM_CLKGEN_BAND,1);
    si44_spisend(1<<3 | BAND << 0);
	GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
	set_property(0x4000, 8);
    si44_spisend(FCINT);
    si44_spisend((FCFRACT>>16)&0xFF);
    si44_spisend((FCFRACT>>8)&0xFF);
    si44_spisend(FCFRACT&0xFF);
    si44_spisend(0);
    si44_spisend(0);
    si44_spisend(0x20);
    si44_spisend(0xFE);
	GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
    set_property(PREAMBLE_TX_LENGTH, 1);
    si44_spisend(0);
    GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
    set_property(PREAMBLE_CONFIG, 1);
    si44_spisend(0);
    GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
    set_property(0x1005, 4);
    si44_spisend(0);
    si44_spisend(0);
    si44_spisend(0);
    si44_spisend(0);
    GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
    set_property(SYNC_CONFIG, 1);
    si44_spisend(1<<7);
    GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
    set_property(PA_MODE, 4);
    si44_spisend(7<<2);
    si44_spisend(POWER_LEVEL);
    si44_spisend(CLK_DUTY_LOW);
    si44_spisend(31);
    si44_spisend(0);
    si44_spisend(40);
    GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	GPIO_ResetBits(GPIOA, SI_CS);
    set_property(0x2003, 10);
    si44_spisend(((1000000)>> 16) & 0xFF);
    si44_spisend(((1000000)>>  8) & 0xFF);
    si44_spisend(((1000000)     ) & 0xFF);
    si44_spisend(0<<2| ((Fxtal >>  24) & 0xFF));
    si44_spisend((Fxtal >>  16) & 0xFF);
    si44_spisend((Fxtal >>   8) & 0xFF);
    si44_spisend((Fxtal       ) & 0xFF);
    si44_spisend(0x00);
    GPIO_SetBits(GPIOA, SI_CS);
	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	
}

void check_part_info() {
	GPIO_ResetBits(GPIOA, SI_CS);
	si44_spisend(PART_INFO);
	GPIO_SetBits(GPIOA, SI_CS);

	while(!GPIO_ReadInputDataBit(GPIOA, SI_CTS));
	
	uint8_t part_info[9];
	GPIO_ResetBits(GPIOA, SI_CS);
	si44_spiread(part_info, 9);
	GPIO_SetBits(GPIOA, SI_CS);

	// To be changed for SI4464
	if(part_info[2] != 0x44 || part_info[3] != 0x61) {
		while(1) {
			GPIO_SetBits(GPIOA, LED);
			tim1Wait(60000);
			tim1Wait(60000);
			tim1Wait(60000);
			GPIO_ResetBits(GPIOA, LED);
			tim1Wait(60000);
			tim1Wait(60000);
			tim1Wait(60000);
		}
	}
}
