#include "Ultrasonic.h"
#include "stm8l15x.h"
#include "bsp.h"

bool UltraSonicDetecResult = FALSE;
bool UltraSonicDateFlag = FALSE;


void SUPERSONIC_hanld(void)
{


  #define UTX UTX1
  #define URX URX1
  #define EXTI_CFG EXTI_CFG1
  #define TD                    TIM2

  #define SUPERSONIC_INT        {GPIOC->DDR |= 1<<UTX;GPIOC->CR1 |= 1<<UTX; GPIOC->ODR &= ~(1<<UTX);\
                                 \
                                  GPIOC->DDR &=~(1<<URX);GPIOC->CR2 &=~(1<<URX);  EXTI->CR2 |= EXTI_CFG;EXTI->SR1|=1<<URX;\
                                }
  #define SUPERSONIC_REVCER_INTERRUPT_ENABLE            {GPIOC->CR2 |= (1<<URX);}
  #define SUPERSONIC_REVCER_INTERRUPT_DISABLE           {GPIOC->CR2 &=~(1<<URX);}
  #define SUPERSONIC_REVCER_INTERRUPT_IFG               (EXTI->SR1 &(1<<URX))
  #define SUPERSONIC_REVCER_INTERRUPT_IFG_CLEAR         {EXTI->SR1 |= (1<<URX);}

  #define SUPERSONIC_SEND_T     {GPIOC->ODR ^=(1<<UTX);}
  #define SUPERSONIC_SEND_FREE     {GPIOC->CR1 &=~(1<<UTX);GPIOC->ODR &=~(1<<UTX);GPIOC->DDR &=~(1<<UTX);}

  #define SEND_COUNT            33//16*2+1
  #define BLIND_TIME            144//2*10000/125  2MS_160   1.5MS_120   1.8MS_144
  #define CYCLE_TIME            1600//20*10000/125

   if(UltraSonicDateFlag == TRUE)return;
   UltraSonicDateFlag = TRUE;
   
   
   GPIOC->DDR |= 1<<6;//使能
   GPIOC->CR1 |= 1<<6;//超声波
   GPIOC->ODR |= 1<<6;//模块
   
  {//延时
      int i=0,j=0;
      for(i=0;i<16*10;i++)
         for(j=0;j<1000;j++)
            {
                ;
            }
  }
   
   uint8_t UTX1 = 0,URX1 = 0,EXTI_CFG1 = 0;
   UTX1 = 5; 
   URX1 = 4;
   EXTI_CFG1 = 3<<0;
   asm("sim");
   unsigned int send_count=0;
   bool result=FALSE;

   SUPERSONIC_INT;

   CLK->CKDIVR =0;

   CLK->PCKENR1 |=(1<<0);        //TIM2_0      TIM3_1          TIM4_2
   TD->CR1     =0;
   TD->SR1     =0;
   TD->PSCR    =0;             //tim_clk = f_clk/ power(2,(PSC[2:0]))  16M/power(2,4)=1M
   TD->CNTRH   =0;
   TD->CNTRL   =0;             //计数器清0
   TD->ARRH    =0;
   TD->ARRL    =199;
   TD->CR1     =(1<<7)+(1<<0);
   while(1)
   {
     if(TD->SR1 &0x01)
     {
       TD->SR1=0;
       send_count++;
       if(send_count<SEND_COUNT){SUPERSONIC_SEND_T;}                                    /* 发送40K时钟 */
       else
       {
           //if(send_count==SEND_COUNT)           SUPERSONIC_SEND_T;                       /* 盲区1 开始点 */
           if(send_count==BLIND_TIME)           //SUPERSONIC_SEND_T;                       /* 盲区1 结束点 和 采样  开始点 */
           {
             SUPERSONIC_SEND_FREE;
             SUPERSONIC_REVCER_INTERRUPT_ENABLE;
             asm("nop");
           }

           if(SUPERSONIC_REVCER_INTERRUPT_IFG)
           {
             SUPERSONIC_REVCER_INTERRUPT_IFG_CLEAR;
             result=TRUE;
             break;
           }

           //if(send_count==CYCLE_TIME)           SUPERSONIC_SEND_T;                       /* 采样  结束点 */
           if(send_count>CYCLE_TIME)            break;                                  /* 周期结束点*/
       }
     }
   }
   SUPERSONIC_REVCER_INTERRUPT_DISABLE;
   TD->CR1     =0;
   CLK->PCKENR1   &=~(1<<0);
   asm("rim");
   UltraSonicDetecResult = result;
   
   GPIOC->ODR &= ~(1<<6);//Disable模块
}

