main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdarg.h>
  2. #include "yc11xx.h"
  3. #include "ycdef.h"
  4. #include "yc11xx_uart.h"
  5. #include "yc11xx_gpio.h"
  6. #include "iic.h"
  7. #include "systick.h"
  8. void LED_Run(void)
  9. {
  10. static uint32_t times = 0;
  11. times++;
  12. if(times>0x50000)
  13. {
  14. times = 0;
  15. GPIO_CONFIG(2) = (GPIO_CONFIG(2)==GPCFG_OUTPUT_HIGH)? GPCFG_OUTPUT_LOW: GPCFG_OUTPUT_HIGH;
  16. }
  17. }
  18. #define IIC_SCL_GPIO_NUM 25
  19. #define IIC_SDA_GPIO_NUM 6
  20. #define I2CBUFSIZE 256
  21. void debug_print(uint32_t data)
  22. {
  23. USART_SendData(UARTB,(data>>24) & 0xff);
  24. USART_SendData(UARTB,(data>>16) & 0xff);
  25. USART_SendData(UARTB,(data>>8 ) & 0xff);
  26. USART_SendData(UARTB,(data>>0 ) & 0xff);
  27. }
  28. /**
  29. *@brief IIC(AT24C02)Write the specified amount of data at the specified location
  30. *@param Device address
  31. *@param addr
  32. *@param len
  33. *@param data
  34. *@return None
  35. */
  36. //void AT24C02_WriteData(uint8_t devaddr,uint8_t addr,uint8_t Len,uint8_t *p)
  37. //{
  38. // uint8_t Page_remain = 8 - addr%8;
  39. // if(Page_remain >= Len)
  40. // {
  41. // Page_remain = Len;
  42. // }
  43. // while(1)
  44. // {
  45. // I2C_SendDataFromBuff(devaddr,addr,p,Len);
  46. // if(Page_remain == Len) break;
  47. // addr+=Page_remain;
  48. // p+=Page_remain;
  49. // Len-=Page_remain;
  50. // if(Len>=8) Page_remain =8;
  51. // else Page_remain=Len;
  52. // }
  53. //}
  54. ///**
  55. // *@brief IIC(AT24C02)read and write test
  56. // *@param None.
  57. // *@return normal return 1 ,fild return 0
  58. // */
  59. //void I2c_Test()
  60. //{
  61. //#define SEND_BUFF_SIZE 4
  62. //#define RECV_BUFF_SIZE 4
  63. //#define DEV_ADDR_EEPROM 0x50
  64. //#define REG_ADDR_EEPROM 0x02
  65. //#define I2C_Txlen_SIZE 4
  66. //#define I2C_Rxlen_SIZE 4
  67. //
  68. // I2C_InitTypeDef mem_test;
  69. // mem_test.I2C_RXLen = 64;
  70. // mem_test.I2C_TXLen = 64;
  71. // mem_test.I2C_ClockSpeed = IICD_CLOCKSPEED400KHZ ;
  72. // I2C_Init(&mem_test);
  73. // printf ("iic init \n");
  74. // GPIO_SetGpioMultFunction(IIC_SCL_GPIO_NUM, GPCFG_IIC_SCL); //set gpio function to iic scl
  75. // GPIO_SetGpioMultFunction(IIC_SDA_GPIO_NUM, GPCFG_IIC_SDA ); //setbuf gpio function to iic sda
  76. // GPIO_SetGpioMultFunction(29,0);
  77. // GPIO_SetGpioMultFunction(30,0);
  78. // uint8_t Data = 0;
  79. // uint8_t send_buff[SEND_BUFF_SIZE]={Data++, Data++, Data++, Data++};
  80. // uint8_t recv_buff[RECV_BUFF_SIZE];
  81. // uint32_t DevAddr = DEV_ADDR_EEPROM;
  82. // uint32_t RegAddr = REG_ADDR_EEPROM;
  83. // uint16_t TxLen = I2C_Txlen_SIZE;
  84. // uint8_t* TxBuff = send_buff;
  85. // uint16_t RxLen = I2C_Rxlen_SIZE;
  86. // uint8_t* RxBuff = recv_buff;
  87. // printf ("init data\n");
  88. // while(1){
  89. //
  90. // uint32_t i,RxData=0;
  91. //#if 0
  92. // I2C_SendData(DevAddr, I2C_REGADDR16BITS(RegAddr), Data);
  93. // printf("Data = 0x%02x\n",Data);
  94. // Data ++;
  95. // delay_ms(500);
  96. //
  97. // RxData = I2C_ReceiveData( DevAddr, I2C_REGADDR16BITS(RegAddr));
  98. // printf("RxData = 0x%02x\n",RxData);
  99. // delay_ms(500);
  100. //
  101. // I2C_SendDataFromBuff(DevAddr, I2C_REGADDR16BITS(RegAddr), TxBuff, TxLen);
  102. //
  103. // for(i=0;i<4;i++)printf("TxBuff[%d] = 0x%02x ,",i,TxBuff[i]);
  104. // printf("\n");
  105. // delay_ms(500);
  106. //
  107. // I2C_ReadDatatoBuff( DevAddr, I2C_REGADDR16BITS(RegAddr), RxBuff, RxLen);
  108. // for(i=0;i<4;i++)printf("RxBuff[%d] = 0x%02x ,",i,RxBuff[i]);
  109. // printf("\n");
  110. // delay_ms(500);
  111. //#else
  112. // I2C_SendData(DevAddr, I2C_REGADDR8BITS(RegAddr), Data);
  113. // printf("Data = 0x%02x\n",Data);
  114. // Data ++;
  115. // delay_ms(1);
  116. //
  117. // RxData=I2C_ReceiveData( DevAddr, I2C_REGADDR8BITS(RegAddr));
  118. // printf("RxData = 0x%02x\n",RxData);
  119. // delay_ms(50);
  120. //
  121. // I2C_SendDataFromBuff(DevAddr, I2C_REGADDR8BITS(RegAddr), TxBuff, TxLen);
  122. // for(i=0;i<4;i++)printf("TxBuff[%d] = 0x%02x ,",i,TxBuff[i]);
  123. // printf("\n");
  124. // delay_ms(500);
  125. //
  126. // I2C_ReadDatatoBuff( DevAddr, I2C_REGADDR8BITS(RegAddr), RxBuff, RxLen);
  127. // for(i=0;i<4;i++)printf("RxBuff[%d] = 0x%02x ,",i,RxBuff[i]);
  128. // printf("\n");
  129. // delay_ms(500);
  130. //#endif
  131. // }
  132. // printf("welcome\n");
  133. //}
  134. int main(void)
  135. {
  136. // GPIO_CONFIG(20)= GPCFG_JTAG_SWCLK;
  137. // GPIO_CONFIG(21)= GPCFG_JTAG_SWDAT;
  138. systick_init();
  139. UartxInit(UARTB); //print log
  140. delay_ms(1000);
  141. printf("\r\n *******This is IIC TEST********\n");
  142. // I2c_Test();
  143. //0xa0 eeprom device and write,
  144. //0x00 eeprom addr
  145. //'a' data
  146. uint8_t src_w[10]={0xa0,0x00,'a','b','c','d','e','f','g','h'};
  147. //0xa0 eeprom device and write,
  148. //0x00 eeprom addr;
  149. //0xa1 eeprom device and read;
  150. uint8_t src_r[3]={0xa0,0x00,0xa1};
  151. uint8_t data[9]={"None"};
  152. data[8]='\0';
  153. IIC_SendData(src_w,10);
  154. printf("Initial dest: %s\n", data);
  155. // delay_ms(75);
  156. IIC_ReceiveData(src_r,3,data,8);
  157. printf("End dest:%s\n", data);
  158. while(1)
  159. {
  160. }
  161. }