#include "bsp_usart.h"
USERSTR UartStr;
struct WIRELESS_INIT wireless_init = WIRELESS_INIT_DEFAULT;
static uint8_t WirelessModuleSet[10] = {0xC1,0xC1,0xC1,0xC0,0x64,0x36,0x25,0x4C,0xCC};
u8 TestData[8] = {0x00,0xF8,0xF8,0x01,0x02,0x03,0x04,0x05};
uint8_t UartRecLen = 0;
uint8_t UartRecBuff[25] = {0};
uint8_t UartTimeOut = 0;
void UartRecDeal(void)
{
    USART_ClearITPendingBit(USART1,USART_IT_RXNE);
    UartTimeOut = 10;
    UartRecBuff[UartRecLen++] = USART_ReceiveData8(USART1);
    if(UartRecLen>=25)UartRecLen = 0;
}
int fputc(int ch, FILE *f)//STM8S105,printf函数重定向 
{ 
  while (!(USART1->SR&0x80)); 
  USART1->DR=ch;
  return ch; 
} 
void HostPCSend(void)
{
    if(UartRecLen&&(UartTimeOut==0)&&(TxModel))
    {
        SX78Str.SX7xTxFlag = TRUE;
	SX78Str.SX7xCmd = CmdTX;
	memcpy(SX78Str.SX7xTxStr,UartRecBuff,SX78Str.SX7xTxlen = UartRecLen*sizeof(uint8_t));
        memset(UartRecBuff,0,25*sizeof(uint8_t));
        UartRecLen = 0;
    }
}
void bsp_usart_init(void)
{
      //USART_ClockInit(USART1,USART_Clock_Enable,USART_CPOL_Low,USART_CPHA_2Edge,USART_LastBit_Enable);
      //CLK->PCKENR1 = 0x25;//使能定时器时钟
      CLK_PeripheralClockConfig(CLK_Peripheral_USART1,ENABLE);
      GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_3, ENABLE);
      GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_2, ENABLE);
      //USART_Init(USART1,9600,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx ));
      USART_Init(USART1,19200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx |USART_Mode_Rx ));
      //USART1->CR2 = 0X80;
      USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
      USART_Cmd(USART1,ENABLE);
      //USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}

void UartTest(void)
{
    if(!wireless_init.InitFlag)
    {
        WirelessInit(&wireless_init,WirelessModuleSet,6);
    }
    else
    {
        if(UartStr.Cnt>0)return;
        UartStr.Cnt = 1000;
        UsartPrint(TestData,8);
    }
/*
    if(!UARTTEST)return;
    if(UartStr.Cnt>0)return;
    UartStr.Cnt = 4000;
    UsartPrint(TestData,5);
*/
}

void UsartPrint(uint8_t *data,uint32_t len)
{
      uint32_t i = 0;
      while((USART1->SR&0x80)==0);
      for(i=0;i<len;i++)
      {
          USART1->DR = *(data++);
          while((USART1->SR&0x40)==0);
      }
      asm("NOP");
}
static void WirelessInit(struct WIRELESS_INIT *WirelessInitCp,uint8_t *wireless,uint8_t num)
{
    if(WirelessInitCp->InitFlag)return;
    if(WirelessInitCp->InitDelay)return;
    if(UartTimeOut)return;
    static int cmd_count = 0,cmd_state=0;
    switch(cmd_state)
    {
        case init_start:
        {
            WiRELESSINIT;
            ConfigMode3;
            //nrf_delay_ms(10);
            cmd_count = 0;
            cmd_state = winit_cmd_send;
            UsartPrint(wireless,3);
            cmd_state = init_delay;
            WirelessInitCp->SendDelay = 50;
            break;
        }
        case init_cmd_send:
        {
            UsartPrint(wireless+3,num);
            cmd_state = init_delay;
            WirelessInitCp->SendDelay = 50;
            break;
        }
        case init_delay:
        {
            if(WirelessInitCp->SendDelay)break;
            cmd_state = init_ing;
            break;
        }
        case init_ing:
        {
            if(strstr((char *)UartRecBuff,(char *)(WirelessModuleSet+3))!=NULL)
            {
                memset((char *)UartRecBuff,0,25*sizeof(char));
                UartRecLen = 0;
                cmd_state = init_ok;
                break;
            }
            else
            {
                if(cmd_count>10)
                {
                    cmd_state = init_error;
                    break;
                }
                else
                {
                    cmd_count++;
                    cmd_state = init_cmd_send;
                    break;
                }
            }
        }
        case init_ok:
        {
            ConfigMode2;
            //nrf_delay_ms(10);
            //beep_start(1,2,2);
            WirelessInitCp->InitFlag = TRUE;
            cmd_state = winit_start;
            cmd_count = 0;
            USART_Init(USART1,19200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx ));
            break;
        }
        case init_error:
        {
            WirelessInitCp->InitFlag = FALSE;
            WirelessInitCp->InitDelay = 1000;
            cmd_state = winit_start;
            cmd_count = 0;
        }
        default:break;
    }
}

