#include "main.h"
#include "delay.h"
#include "deviceSetup.h"
#include "usart.h"
#include "ascii.h"
#include "timer.h"
#include "i2c.h"

void timer_test(timerNo_t timer, pinNo_t pin)
{
	
	timerInitCounter(timer, 4000, 999, downCounting); 
	timerSart(timer);

	for(int i = 0; i < 10 ; i++)
	{
		while(!timerGetUpdateInterrupt(timer));
		timerClearUpdateInterrupt(timer);
		pinToggle(pin);
	}
		pinWrite(pin,0);
}

void timer_capture_compare_test(timerNo_t timer)
{
	uint16_t i = 0;
	timerInitCounter(timer, 100, 99, downCounting); 
	// We use pin PA3 (Arduino header A2)
	//timerInitOutputCompare(timer, toggle, 4, pinA3, 2,0, 300);
	timerInitOutputCompare(timer, pwm_normal, 2, pinB3, 2,0, 900);
	timerSart(timer);

	while(1){
		delayMs(200);
		timerSetCounterCompareValue(timer, 2, i);
		i += 10;
		if(i>99) i = 0;
	}
}


void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
{
	int i;
	char pt; 
	print_Usart(usart2, "\n\r"); 
	if(lsbFirst)
	{
		print_Usart(usart2, "| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|"); 
		print_Usart(usart2, "\n\r"); 
		usartSendChar(usart2, '|'); 
		for(i=0; i < 32; i++) 
		{
			pt = (toPrint >> i) & 1;
			usartSendChar(usart2, ' '); 
			usartSendChar(usart2, pt + 0x30); 
			usartSendChar(usart2, '|'); 
		}
	}
	else
	{
		print_Usart(usart2, "|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|"); 
		print_Usart(usart2, "\n\r"); 
		usartSendChar(usart2, '|'); 
		for(i=0; i < 32; i++) 
		{
			pt = (toPrint >> (31-i)) & 1;
			usartSendChar(usart2, ' '); 
			usartSendChar(usart2, pt + 0x30); 
			usartSendChar(usart2, '|'); 
		}
	}
}


void printBinary8(uint8_t toPrint, uint8_t lsbFirst)
{
	int i;
	char pt; 
	print_Usart(usart2, "\n\r"); 
	if(lsbFirst)
	{
		print_Usart(usart2, "| 0| 1| 2| 3| 4| 5| 6| 7|"); 
		print_Usart(usart2, "\n\r"); 
		usartSendChar(usart2, '|'); 
		for(i=0; i < 8; i++) 
		{
			pt = (toPrint >> i) & 1;
			usartSendChar(usart2, ' '); 
			usartSendChar(usart2, pt + 0x30); 
			usartSendChar(usart2, '|'); 
		}
	}
	else
	{
		print_Usart(usart2, "| 7| 6| 5| 4| 3| 2| 1| 0|"); 
		print_Usart(usart2, "\n\r"); 
		usartSendChar(usart2, '|'); 
		for(i=0; i < 8; i++) 
		{
			pt = (toPrint >> (7-i)) & 1;
			usartSendChar(usart2, ' '); 
			usartSendChar(usart2, pt + 0x30); 
			usartSendChar(usart2, '|'); 
		}
	}
}

int main(int argc, char *argv[])
{
	
	uint8_t i = 0;
	uint8_t slaveAddress = 0xC0; 
	uint8_t registerToRead = 0x00; 
	uint8_t i2cRecieved = 0; 
	uint8_t buffer [8];
	uint8_t data = 0;
	// making array with all available timers	
	//timerNo_t timers[MAX_TIMER_CHANNEL_COUNT] = {timer_1, timer_2, timer_3, timer_14, timer_16, timer_17}; 

	delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library 
	
	pinConfig(pinB3, output, pushPull, def_res, def_speed);
	pinConfig(pinA0, input, def_stage, pullDown, def_speed);	
	
	setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main.	
	
	usartInit(	usart2, 
				pinA2, 
				pinA15,
				115200,
				eight, 
				NO_PARITY_CTRL,
				noFlowControl); 

	//clears screen and send the wellcome messgae
	print_Usart(usart2, ASCII_clear); 
	print_Usart(usart2, "Hello to our KED project\n\r"); 
	

	//blinks 10 times to indicate the sicsessfull init if the device
	for(i = 0 ; i < 2 ; i++) {
		delayMs(100); 
		pinToggle(pinB3);
		delayMs(100); 
	}

	pinWrite(pinB3,0);

	// Pin configuration for i2c	
	pinInit(pinA9);
	pinInit(pinA10);
	pinConfig(pinA9, alternate, openDrain, pullUp, fast);// I2C SCL 
	pinConfig(pinA10, alternate, openDrain, pullUp, fast);// I2C CSA
	pinSetAlternate(pinA9, 4); 
	pinSetAlternate(pinA10, 4);

	i2c_t i2c_1;
	i2c_1.channelNo = I2C_CH_1; 
	i2c_1.mode = i2cModeMaster; 
	i2c_1.address = 0x0; 
	i2c_1.addressSecond = 0x00;
	i2c_1.addressCount = i2cAddressCountSingle; 
	i2c_1.addressSize =	i2cAddressSizeSevenBits; 
	i2c_1.speed = i2cSpeedStandart; 
	i2c_1.opperationMode = i2cOpperationPolling; 
	i2c_1.streching = i2cClockStrechingDisable; 
	i2c_1.timing = 0x2000090E; // Taken from stm code generator
	i2c_1.wakeOn = i2cWakeUpDisabled; 
	i2c_1.state = i2cNotInitialized; 

	i2cInit(&i2c_1);

	data = i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved);
	

	printBinary8(data,0);


	print_Usart(usart2, "All is working fine\n\r"); 
	
	while(1)
	{
		delayMs(100); 
		pinToggle(pinB3);
		delayMs(100); 
	}
	
	return 1;
}


