Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. struct I2C_Module
  2. {
  3. I2C_HandleTypeDef instance;
  4. uint16_t sdaPin;
  5. GPIO_TypeDef* sdaPort;
  6. uint16_t sclPin;
  7. GPIO_TypeDef* sclPort;
  8. };
  9.  
  10. void I2C_ClearBusyFlagErratum(struct I2C_Module* i2c)
  11. {
  12. GPIO_InitTypeDef GPIO_InitStructure;
  13.  
  14. // 1. Clear PE bit.
  15. i2c->instance.Instance->CR1 &= ~(0x0001);
  16.  
  17. // 2. Configure the SCL and SDA I/Os as General Purpose Output Open-Drain, High level (Write 1 to GPIOx_ODR).
  18. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
  19. GPIO_InitStructure.Alternate = I2C_PIN_MAP;
  20. GPIO_InitStructure.Pull = GPIO_PULLUP;
  21. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
  22.  
  23. GPIO_InitStructure.Pin = i2c->sclPin;
  24. HAL_GPIO_Init(i2c->sclPort, &GPIO_InitStructure);
  25. HAL_GPIO_WritePin(i2c->sclPort, i2c->sclPin, GPIO_PIN_SET);
  26.  
  27. GPIO_InitStructure.Pin = i2c->sdaPin;
  28. HAL_GPIO_Init(i2c->sdaPort, &GPIO_InitStructure);
  29. HAL_GPIO_WritePin(i2c->sdaPort, i2c->sdaPin, GPIO_PIN_SET);
  30.  
  31. // 3. Check SCL and SDA High level in GPIOx_IDR.
  32. while (GPIO_PIN_SET != HAL_GPIO_ReadPin(i2c->sclPort, i2c->sclPin))
  33. {
  34. asm("nop");
  35. }
  36.  
  37. while (GPIO_PIN_SET != HAL_GPIO_ReadPin(i2c->sdaPort, i2c->sdaPin))
  38. {
  39. asm("nop");
  40. }
  41.  
  42. // 4. Configure the SDA I/O as General Purpose Output Open-Drain, Low level (Write 0 to GPIOx_ODR).
  43. HAL_GPIO_WritePin(i2c->sdaPort, i2c->sdaPin, GPIO_PIN_RESET);
  44.  
  45. // 5. Check SDA Low level in GPIOx_IDR.
  46. while (GPIO_PIN_RESET != HAL_GPIO_ReadPin(i2c->sdaPort, i2c->sdaPin))
  47. {
  48. asm("nop");
  49. }
  50.  
  51. // 6. Configure the SCL I/O as General Purpose Output Open-Drain, Low level (Write 0 to GPIOx_ODR).
  52. HAL_GPIO_WritePin(i2c->sclPort, i2c->sclPin, GPIO_PIN_RESET);
  53.  
  54. // 7. Check SCL Low level in GPIOx_IDR.
  55. while (GPIO_PIN_RESET != HAL_GPIO_ReadPin(i2c->sclPort, i2c->sclPin))
  56. {
  57. asm("nop");
  58. }
  59.  
  60. // 8. Configure the SCL I/O as General Purpose Output Open-Drain, High level (Write 1 to GPIOx_ODR).
  61. HAL_GPIO_WritePin(i2c->sclPort, i2c->sclPin, GPIO_PIN_SET);
  62.  
  63. // 9. Check SCL High level in GPIOx_IDR.
  64. while (GPIO_PIN_SET != HAL_GPIO_ReadPin(i2c->sclPort, i2c->sclPin))
  65. {
  66. asm("nop");
  67. }
  68.  
  69. // 10. Configure the SDA I/O as General Purpose Output Open-Drain , High level (Write 1 to GPIOx_ODR).
  70. HAL_GPIO_WritePin(i2c->sdaPort, i2c->sdaPin, GPIO_PIN_SET);
  71.  
  72. // 11. Check SDA High level in GPIOx_IDR.
  73. while (GPIO_PIN_SET != HAL_GPIO_ReadPin(i2c->sdaPort, i2c->sdaPin))
  74. {
  75. asm("nop");
  76. }
  77.  
  78. // 12. Configure the SCL and SDA I/Os as Alternate function Open-Drain.
  79. GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
  80. GPIO_InitStructure.Alternate = I2C_PIN_MAP;
  81.  
  82. GPIO_InitStructure.Pin = i2c->sclPin;
  83. HAL_GPIO_Init(i2c->sclPort, &GPIO_InitStructure);
  84.  
  85. GPIO_InitStructure.Pin = i2c->sdaPin;
  86. HAL_GPIO_Init(i2c->sdaPort, &GPIO_InitStructure);
  87.  
  88. // 13. Set SWRST bit in I2Cx_CR1 register.
  89. I2C_i2c[0].instance.Instance->CR1 |= 0x8000;
  90.  
  91. asm("nop");
  92.  
  93. // 14. Clear SWRST bit in I2Cx_CR1 register.
  94. I2C_i2c[0].instance.Instance->CR1 &= ~0x8000;
  95.  
  96. asm("nop");
  97.  
  98. // 15. Enable the I2C peripheral by setting the PE bit in I2Cx_CR1 register
  99. i2c->instance.Instance->CR1 |= 0x0001;
  100.  
  101. // Call initialization function.
  102. HAL_I2C_Init(&(i2c->instance));
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement