PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/i2c.c

https://gitlab.com/MujibAzizi/cpkn
C | 164 lines | 71 code | 55 blank | 38 comment | 2 complexity | 7912230d1c79d521eb53edf0421884f5 MD5 | raw file
  1. //***************************************************************************//**
  2. //EEPROM LIB v 1.0
  3. //******************************************************************************/
  4. #include "stm32f10x_gpio.h"
  5. #include "stm32f10x_i2c.h"
  6. #include "stm32f10x_rcc.h"
  7. //#define I2C_FLASH_PAGESIZE 32 //not used yet
  8. #define EEPROM_HW_ADDRESS 0xA0 /* E0 = E1 = E2 = 0 */
  9. #define I2C_EE I2C1//interface number
  10. void Delay_ms(uint32_t ms);
  11. /***************************************************************************//**
  12. * @brief I2C Configuration
  13. ******************************************************************************/
  14. void I2C_Configuration(void)
  15. {
  16. I2C_InitTypeDef I2C_InitStructure;
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
  19. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO , ENABLE);//
  20. /* Configure I2C1 pins: PB6->SCL and PB7->SDA */
  21. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  22. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  24. GPIO_Init(GPIOB, &GPIO_InitStructure);
  25. I2C_DeInit(I2C_EE);
  26. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  27. I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;
  28. I2C_InitStructure.I2C_OwnAddress1 = 1;
  29. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  30. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  31. I2C_InitStructure.I2C_ClockSpeed = 100000; /* 100kHz */
  32. I2C_Cmd(I2C_EE, ENABLE);
  33. I2C_Init(I2C_EE, &I2C_InitStructure);
  34. I2C_AcknowledgeConfig(I2C_EE, ENABLE);
  35. }
  36. //*******************************************************************8
  37. //***************************************************************
  38. void I2C_EE_ByteWrite(uint8_t val, uint16_t WriteAddr)
  39. {
  40. /* Send START condition */
  41. I2C_GenerateSTART(I2C_EE, ENABLE);
  42. /* Test on EV5 and clear it */
  43. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  44. /* Send EEPROM address for write */
  45. I2C_Send7bitAddress(I2C_EE, EEPROM_HW_ADDRESS, I2C_Direction_Transmitter);
  46. /* Test on EV6 and clear it */
  47. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  48. /* Send the EEPROM's internal address to write to : MSB of the address first */
  49. I2C_SendData(I2C_EE, (uint8_t)((WriteAddr & 0xFF00) >> 8));
  50. /* Test on EV8 and clear it */
  51. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  52. /* Send the EEPROM's internal address to write to : LSB of the address */
  53. I2C_SendData(I2C_EE, (uint8_t)(WriteAddr & 0x00FF));
  54. /* Test on EV8 and clear it */
  55. while(! I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  56. I2C_SendData(I2C_EE, val);
  57. /* Test on EV8 and clear it */
  58. while (!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  59. /* Send STOP condition */
  60. I2C_GenerateSTOP(I2C_EE, ENABLE);
  61. //delay between write and read...not less 4ms
  62. Delay_ms(5);
  63. }
  64. //*********************************************************************************
  65. uint8_t I2C_EE_ByteRead( uint16_t ReadAddr)
  66. {
  67. uint8_t tmp;
  68. /* While the bus is busy */
  69. while(I2C_GetFlagStatus(I2C_EE, I2C_FLAG_BUSY));
  70. /* Send START condition */
  71. I2C_GenerateSTART(I2C_EE, ENABLE);
  72. /* Test on EV5 and clear it */
  73. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  74. /* Send EEPROM address for write */
  75. I2C_Send7bitAddress(I2C_EE, EEPROM_HW_ADDRESS, I2C_Direction_Transmitter);
  76. /* Test on EV6 and clear it */
  77. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  78. /* Send the EEPROM's internal address to read from: MSB of the address first */
  79. I2C_SendData(I2C_EE, (uint8_t)((ReadAddr & 0xFF00) >> 8));
  80. /* Test on EV8 and clear it */
  81. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  82. /* Send the EEPROM's internal address to read from: LSB of the address */
  83. I2C_SendData(I2C_EE, (uint8_t)(ReadAddr & 0x00FF));
  84. /* Test on EV8 and clear it */
  85. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  86. /* Send STRAT condition a second time */
  87. I2C_GenerateSTART(I2C_EE, ENABLE);
  88. /* Test on EV5 and clear it */
  89. while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  90. /* Send EEPROM address for read */
  91. I2C_Send7bitAddress(I2C_EE, EEPROM_HW_ADDRESS, I2C_Direction_Receiver);
  92. /* Test on EV6 and clear it */
  93. while(!I2C_CheckEvent(I2C_EE,I2C_EVENT_MASTER_BYTE_RECEIVED));//I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
  94. tmp=I2C_ReceiveData(I2C_EE);
  95. I2C_AcknowledgeConfig(I2C_EE, DISABLE);
  96. /* Send STOP Condition */
  97. I2C_GenerateSTOP(I2C_EE, ENABLE);
  98. return tmp;
  99. }
  100. //*******************************************************************************
  101. void Delay_ms(uint32_t ms)
  102. {
  103. volatile uint32_t nCount;
  104. RCC_ClocksTypeDef RCC_Clocks;
  105. RCC_GetClocksFreq (&RCC_Clocks);
  106. nCount=(RCC_Clocks.HCLK_Frequency/10000)*ms;
  107. for (; nCount!=0; nCount--);
  108. }
  109. //*****************************************************************************