main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdarg.h>
  2. #include "yc11xx.h"
  3. #include "yc11xx_uart.h"
  4. #include "yc11xx_gpio.h"
  5. #include "SCI7816.h"
  6. #define LOW 0
  7. #define HIGH 1
  8. #define SCI7816_DETECT_GPIO 23
  9. #define PULLUP 0
  10. #define PULLDOWN 1
  11. void LED_Run(void)
  12. {
  13. static uint32_t times = 0;
  14. times++;
  15. if(times>0x50000)
  16. {
  17. times = 0;
  18. GPIO_CONFIG(2) = (GPIO_CONFIG(2)==GPCFG_OUTPUT_HIGH)? GPCFG_OUTPUT_LOW: GPCFG_OUTPUT_HIGH;
  19. }
  20. }
  21. #define IO_TXA 8
  22. #define IO_RXA 9
  23. #define IO_TXB 6
  24. #define IO_RXB 14
  25. #define UARTBUFSIZE 128
  26. void UartxInit(USART_TypeDef UARTx)
  27. {
  28. USART_InitTypeDef USART_InitStruct ;
  29. USART_InitStruct.USART_BaudRate = UARTE_BAUDRATE_BAUDRATE_Baud115200;
  30. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  31. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  32. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  33. USART_InitStruct.USART_Mode =USART_Mode_duplex;
  34. USART_InitStruct.USART_Parity = USART_Parity_Even ;
  35. USART_InitStruct.USART_RXLen = UARTBUFSIZE;
  36. USART_InitStruct.USART_TXLen = UARTBUFSIZE;
  37. if(UARTA == UARTx){
  38. GPIO_SetGpioMultFunction(IO_TXA,GPCFG_UART_TXD);
  39. GPIO_SetGpioMultFunction(IO_RXA,GPCFG_UART_RXD);
  40. }else if (UARTB == UARTx){
  41. GPIO_SetGpioMultFunction(IO_TXB,GPCFG_UARTB_TXD);
  42. GPIO_SetGpioMultFunction(IO_RXB,GPCFG_UARTB_RXD);
  43. }
  44. USART_Init(UARTx,&USART_InitStruct);
  45. }
  46. void debug_print(uint32_t data)
  47. {
  48. USART_SendData(UARTB,(data>>24) & 0xff);
  49. USART_SendData(UARTB,(data>>16) & 0xff);
  50. USART_SendData(UARTB,(data>>8 ) & 0xff);
  51. USART_SendData(UARTB,(data>>0 ) & 0xff);
  52. }
  53. void print_buf(uint8_t *data,int len)
  54. {
  55. int i;
  56. for(i=0;i<len;i++)
  57. {
  58. printf("%02x ",data[i]);
  59. }
  60. printf("\n");
  61. }
  62. uint8_t sci7816_gpio_detection(uint8_t Gpio)
  63. {
  64. if(GPIO_GetInputStatus(Gpio) == LOW)
  65. {
  66. while(GPIO_GetInputStatus(Gpio) == LOW)
  67. return LOW;
  68. }
  69. else
  70. return HIGH;
  71. }
  72. void delay_ms(unsigned int time)
  73. {
  74. unsigned int num,j;
  75. for (num = 0; num <time; num++)
  76. {
  77. for (j = 0; j < 4000; j++);
  78. }
  79. }
  80. uint8_t card_remove=0;
  81. int main(void)
  82. {
  83. int i = 0x100000;
  84. uint8_t UrataReData=0,UratbReData=0;
  85. uint8_t buf[50]={0};
  86. uint8_t Rece_Data[50]={0};
  87. uint8_t pcmd[5]={0x00,0x84,0x00,0x00,0x08};
  88. GPIO_SetGpioMultFunction(26, GPCFG_OUTPUT_LOW);
  89. UartxInit(UARTB); //print log
  90. //UartxInit(UARTA);
  91. uint16_t recv_len = 0;
  92. while(i--);
  93. printf("This is SCI7816 demo test\r\n");
  94. // for(int i = 0; i< 256;i++)
  95. // {
  96. // USART_SendData(UARTB,i);
  97. // USART_SendData(UARTA,i);
  98. // }
  99. GPIO_SetInput(SCI7816_DETECT_GPIO,PULLUP);
  100. printf("wait card insert\n");
  101. while(1)
  102. {
  103. if(sci7816_gpio_detection(SCI7816_DETECT_GPIO) == LOW && card_remove==0)
  104. {
  105. card_remove=1;
  106. printf("card insert\n");
  107. if(SCI7816_Reset(buf,50,&recv_len)==0)
  108. {
  109. printf("SCI7816_Reset ok\n");
  110. delay_ms(10);
  111. if(SCI7816_CosOperation(pcmd,5,1,Rece_Data,50,&recv_len)==OK)
  112. {
  113. printf("SCI7816_CosOperation ok\n");
  114. }
  115. else
  116. {
  117. printf("SCI7816_CosOperation error\n");
  118. }
  119. printf("recv_len=%d",recv_len);
  120. printf("receive data:\n");
  121. print_buf(Rece_Data,recv_len);
  122. }
  123. else
  124. {
  125. printf("SCI7816_Reset fail\n");
  126. }
  127. printf("recv_len=%d\n",recv_len);
  128. printf("reset data:\n");
  129. print_buf(buf,recv_len);
  130. }
  131. else
  132. {
  133. if(card_remove==1 && sci7816_gpio_detection(SCI7816_DETECT_GPIO) == HIGH)
  134. {
  135. card_remove=0;
  136. printf("card remove\n");
  137. printf("wait card insert\n");
  138. }
  139. }
  140. }
  141. }