i2c_eeprom.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file i2c_eeprom.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __I2C_EEPROM_H__
  35. #define __I2C_EEPROM_H__
  36. #include <rtthread.h>
  37. #include "n32g45x.h"
  38. #include <stdio.h>
  39. #include "i2c.h"
  40. typedef enum i2c_state
  41. {
  42. COMM_DONE = 0, /// done successfully
  43. COMM_PRE = 1,
  44. COMM_IN_PROCESS = 2,
  45. COMM_EXIT = 3 /// exit since failure
  46. } I2C_STATE;
  47. typedef enum i2c_direction
  48. {
  49. Transmitter = 0x00,
  50. Receiver = 0x01
  51. } I2C_DIRECTION;
  52. /**
  53. * PROCESS MODE
  54. * 0=polling
  55. * 1=interrupt
  56. * 2=DMA
  57. */
  58. #define PROCESS_MODE 0
  59. #define I2C1_TEST
  60. #define I2C1_REMAP
  61. #ifdef I2C1_TEST
  62. #define I2Cx I2C1
  63. #ifdef I2C1_REMAP
  64. #define I2Cx_SCL_PIN GPIO_PIN_8
  65. #define I2Cx_SDA_PIN GPIO_PIN_9
  66. #define GPIOx GPIOB
  67. #else
  68. #define I2Cx_SCL_PIN GPIO_PIN_6
  69. #define I2Cx_SDA_PIN GPIO_PIN_7
  70. #define GPIOx GPIOB
  71. #endif
  72. #define I2Cx_peripheral_clk_en() RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE)
  73. #define I2Cx_scl_clk_en() RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE)
  74. #define I2Cx_sda_clk_en() RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE)
  75. #endif
  76. #define TEST_EEPROM_SIZE 256
  77. #define TEST_EEPROM_ADDR 0x00
  78. #define I2C_Speed 400000
  79. #define EEPROM_ADDRESS 0xA0
  80. #define I2C_PageSize 8 /// eeprom IC type AT24C08
  81. #define sEE_FLAG_TIMEOUT ((uint32_t)0x1000)
  82. #define sEE_LONG_TIMEOUT ((uint32_t)(100 * sEE_FLAG_TIMEOUT))
  83. #define IIC_RETRY_NUM 2
  84. /** Maximum number of trials for sEE_WaitEepromStandbyState() function */
  85. #define sEE_MAX_TRIALS_NUMBER 150
  86. #define FALSE 0
  87. #define TRUE 1
  88. void I2C_EE_Init(void);
  89. void I2C_EE_WriteBuffer(struct rt_i2c_bus_device *i2c_bus, u8* pBuffer, u16 WriteAddr, u16 NumByteToWrite);
  90. void I2C_EE_WriteOnePage(struct rt_i2c_bus_device *i2c_bus, u8* pBuffer, u16 WriteAddr, u16 NumByteToWrite);
  91. void I2C_EE_PageWrite(struct rt_i2c_bus_device *bus, u8* pBuffer, u16 WriteAddr, u16 NumByteToWrite);
  92. void I2C_EE_WriteOnePageCompleted(void);
  93. void I2C_EE_ReadBuffer(u8* pBuffer, u16 ReadAddr, u16 NumByteToRead);
  94. void I2C_EE_WaitOperationIsCompleted(void);
  95. void I2C_EE_WaitEepromStandbyState(void);
  96. void i2c1_evt_handle(void);
  97. void i2c1_err_handle(void);
  98. void i2c1_send_dma_handle(void);
  99. void i2c1_receive_dma_handle(void);
  100. rt_err_t rt_eeprom_register(rt_uint8_t flag);
  101. #endif /* __I2C_EEPROM_H__ */