/*
==========================未经授权，转载需保留此信息============================
Function : 
Taobao website:http://zeyaotech.taobao.com/
Official website:http://www.ashining.com/
Written by:Lijian
TEL:028-64891123(Sales),028-64891129(Technology),e-mail: service@ashining.com
================================================================================
*/

#include "board.h"
#include "Oled.h"
#include "main.h"
#include "CC1101.h"

	volatile INT8U txBuffer[10] = {0,1,2,3,4,5,6,7,8,9 }, rxBuffer[5], i;
	INT32U x;
	INT8U testbuffer[10] = {"00000"};
	INT16U tx_couter = 0, itmp;
  INT8U sdendbuffer[] = {"www.ashining.com\r\n"};
	
void display_tx(void)
{
	LCD_Init( );
	LCD_Dis_Logo( );
	LCD_Dis_Str( 2,24,"STM8 board" );
	LCD_Dis_Str( 4,0,"www.ashining.com" );
	LCD_Dis_Str( 6,0,"CC1101:TX " );
	LCD_Dis_Str( 6, 80, (char*)testbuffer );
}

void GS_CC1101_RX_HANDLE(void)
{
	x = 10000;
		while( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_2 ) != 0 )
		{
			if( x ) { x--; }
			else  { break; }
		}
		if( x != 0 )
		{
			while( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_2 ) == 0 );
			if( GS_CC1101RecPacket( rxBuffer ) == 5 )
			{
				//已经收到应答数据
				LED_Toggle( );
				tx_couter ++;
                itmp = tx_couter;
                testbuffer[0] = ( itmp / 10000 ) + '0';
                itmp %= 10000;
                testbuffer[1] = ( itmp / 1000 ) + '0';
                itmp %= 1000;
                testbuffer[2] = ( itmp / 100 ) + '0';
                itmp %= 100;
                testbuffer[3] = ( itmp / 10 ) + '0';
                itmp %= 10;
                testbuffer[4] = itmp + '0';
                testbuffer[5] = 0;
                LCD_Dis_Str( 6, 80, (char*)testbuffer );
			}
			else
			{
				//未收到应答数据，添加代码
			}
		}
		else  {GPIO_SetBits( GPIOB, GPIO_Pin_0 );}
		
	
}

main()
{
	SPI_Initial( );
	GPIO_Initial( );
	display_tx();
	//复位CC1101
	GS_CC1101Init( );

	while( 1 )
	{
		//发送数据包，每发送一次，LED闪烁一次
		GS_CC1101SendPacket( sdendbuffer,strlen( sdendbuffer ), ADDRESS_CHECK );
		GS_CC1101SetTRMode( RX_MODE );
		GS_CC1101_RX_HANDLE();
		GPIO_ResetBits( GPIOB, GPIO_Pin_1);
		for( x = 0; x < 500; x ++ );
		GPIO_SetBits( GPIOB, GPIO_Pin_1 );
		for( x = 0; x < 20000; x ++ );
	}
}