sunxi_hal_twi.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef SUNXI_HAL_TWI_H
  2. #define SUNXI_HAL_TWI_H
  3. #include "hal_sem.h"
  4. #include "hal_clk.h"
  5. #include "sunxi_hal_common.h"
  6. #include "hal_gpio.h"
  7. #include "sunxi_hal_regulator.h"
  8. #include <twi/platform_twi.h>
  9. #include <twi/common_twi.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. //for debug
  14. #define CONFIG_DRIVERS_TWI_DEBUG
  15. #ifndef CONFIG_DRIVERS_TWI_DEBUG
  16. #define TWI_INFO(fmt, arg...) hal_log_info(fmt, ##arg)
  17. #else
  18. #define TWI_INFO(fmt, arg...) do {}while(0)
  19. #endif
  20. #define TWI_ERR(fmt, arg...) hal_log_err(fmt, ##arg)
  21. typedef enum
  22. {
  23. TWI_XFER_IDLE = 0x1,
  24. TWI_XFER_START = 0x2,
  25. TWI_XFER_RUNNING = 0x4,
  26. } twi_xfer_status_t;
  27. /** @brief This enum defines the HAL interface return value. */
  28. typedef enum
  29. {
  30. TWI_STATUS_ERROR = -4, /**< An error occurred and the transaction has failed. */
  31. //TWI_STATUS_ERROR_TIMEOUT = -4, /**< The TWI bus xfer timeout, an error occurred. */
  32. TWI_STATUS_ERROR_BUSY = -3, /**< The TWI bus is busy, an error occurred. */
  33. TWI_STATUS_INVALID_PORT_NUMBER = -2, /**< A wrong port number is given. */
  34. TWI_STATUS_INVALID_PARAMETER = -1, /**< A wrong parameter is given. */
  35. TWI_STATUS_OK = 0 /**< No error occurred during the function call. */
  36. } twi_status_t;
  37. typedef enum
  38. {
  39. TWI_MASTER_0, /**< TWI master 0. */
  40. TWI_MASTER_1, /**< TWI master 1. */
  41. TWI_MASTER_2, /**< TWI master 0. */
  42. TWI_MASTER_3, /**< TWI master 1. */
  43. S_TWI_MASTER_0, /**< S_TWI master 0. */
  44. TWI_MASTER_MAX /**< max TWI master number, \<invalid\> */
  45. } twi_port_t;
  46. /** @brief This enum defines the TWI transaction speed. */
  47. typedef enum
  48. {
  49. TWI_FREQUENCY_100K = 100000, /**< 100kbps. */
  50. TWI_FREQUENCY_200K = 200000, /**< 200kbps. */
  51. TWI_FREQUENCY_400K = 400000, /**< 400kbps. */
  52. } twi_frequency_t;
  53. /** @brief This enum defines the TWI transaction speed. */
  54. typedef enum
  55. {
  56. ENGINE_XFER = 0,
  57. TWI_DRV_XFER = 1,
  58. } twi_mode_t;
  59. typedef struct twi_msg
  60. {
  61. uint16_t addr; /* slave address */
  62. uint16_t flags;
  63. #define TWI_M_RD 0x0001 /* read data, from slave to master
  64. * TWI_M_RD is guaranteed to be 0x0001!
  65. * */
  66. #define TWI_M_TEN 0x0010 /* this is a ten bit chip address */
  67. uint16_t len; /* msg length */
  68. uint8_t *buf; /* pointer to msg data */
  69. } twi_msg_t;
  70. typedef struct sunxi_twi
  71. {
  72. uint8_t port;
  73. uint8_t result;
  74. uint8_t already_init;
  75. uint8_t twi_drv_used;
  76. uint8_t pkt_interval;
  77. uint16_t slave_addr;
  78. uint16_t flags;
  79. uint32_t timeout;
  80. uint32_t msgs_num;
  81. uint32_t msgs_idx;
  82. uint32_t msgs_ptr;
  83. unsigned long base_addr;
  84. uint32_t irqnum;
  85. struct regulator_dev regulator;
  86. hal_clk_t pclk;
  87. hal_clk_t mclk;
  88. twi_frequency_t freq;
  89. uint32_t pinmux;
  90. uint32_t pin[TWI_PIN_NUM];
  91. twi_xfer_status_t status;
  92. hal_sem_t hal_sem;
  93. twi_msg_t *msgs;
  94. struct sunxi_dma_chan *dma_chan;
  95. hal_sem_t dma_complete;
  96. } hal_twi_t;
  97. typedef enum
  98. {
  99. I2C_SLAVE = 0,
  100. I2C_SLAVE_FORCE = 1,
  101. I2C_TENBIT = 2,
  102. I2C_RDWR = 3
  103. } hal_twi_transfer_cmd_t;
  104. //initialize twi port
  105. twi_status_t hal_twi_init(twi_port_t port);
  106. //uninitialize twi port
  107. twi_status_t hal_twi_uninit(twi_port_t port);
  108. //twi write
  109. twi_status_t hal_twi_write(twi_port_t port, unsigned long pos, const void *buf, uint32_t size);
  110. //twi read
  111. twi_status_t hal_twi_read(twi_port_t port, unsigned long pos, void *buf, uint32_t size);
  112. //twi control
  113. twi_status_t hal_twi_control(twi_port_t port, hal_twi_transfer_cmd_t cmd, void *args);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif