i2c_device.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* mbed Microcontroller Library
  2. *******************************************************************************
  3. * Copyright (c) 2015, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *******************************************************************************
  29. */
  30. #ifndef MBED_I2C_DEVICE_H
  31. #define MBED_I2C_DEVICE_H
  32. #include "cmsis.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #ifdef DEVICE_I2C
  37. #define I2C_IP_VERSION_V2
  38. #define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
  39. /* Family specifc settings for clock source */
  40. #define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_SYSCLK
  41. #define I2CAPI_I2C2_CLKSRC RCC_I2C2CLKSOURCE_SYSCLK
  42. #define I2CAPI_I2C3_CLKSRC RCC_I2C3CLKSOURCE_SYSCLK
  43. #define I2CAPI_I2C4_CLKSRC RCC_I2C4CLKSOURCE_SYSCLK
  44. /* Provide the suitable timing depending on requested frequencie */
  45. static inline uint32_t get_i2c_timing(int hz)
  46. {
  47. uint32_t tim = 0;
  48. if (SystemCoreClock == 80000000) {
  49. // Common settings: I2C clock = 80 MHz, Analog filter = ON, Digital filter coefficient = 0
  50. switch (hz) {
  51. case 100000:
  52. tim = 0x30C14E6B; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
  53. break;
  54. case 400000:
  55. tim = 0x10D1143A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
  56. break;
  57. case 1000000:
  58. tim = 0x00810E27; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
  59. break;
  60. default:
  61. break;
  62. }
  63. } else if (SystemCoreClock == 48000000) {
  64. // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
  65. switch (hz) {
  66. case 100000:
  67. tim = 0x20A03E55; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
  68. break;
  69. case 400000:
  70. tim = 0x10800C21; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
  71. break;
  72. case 1000000:
  73. tim = 0x00500816; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. return tim;
  80. }
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #endif // DEVICE_I2C
  85. #endif