tca9539.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-02-20 CDT first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <rtdbg.h>
  13. #ifdef BSP_USING_TCA9539
  14. #include "tca9539.h"
  15. /*******************************************************************************
  16. * Local type definitions ('typedef')
  17. ******************************************************************************/
  18. /*******************************************************************************
  19. * Local pre-processor symbols/macros ('#define')
  20. ******************************************************************************/
  21. /* Define for TCA9539 */
  22. #define BSP_TCA9539_I2C_BUS_NAME "i2c1"
  23. #define BSP_TCA9539_DEV_ADDR (0x74U)
  24. #if defined(HC32F4A0) || defined(HC32F4A8)
  25. #define TCA9539_RST_PIN (45) /* PC13 */
  26. #elif defined(HC32F448)
  27. #define TCA9539_RST_PIN (31) /* PB15 */
  28. #elif defined(HC32F472)
  29. #define TCA9539_RST_PIN (44) /* PC12 */
  30. #endif
  31. /*******************************************************************************
  32. * Global variable definitions (declared in header file with 'extern')
  33. ******************************************************************************/
  34. /*******************************************************************************
  35. * Local function prototypes ('static')
  36. ******************************************************************************/
  37. /*******************************************************************************
  38. * Local variable definitions ('static')
  39. ******************************************************************************/
  40. static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
  41. /*******************************************************************************
  42. * Function implementation - global ('extern') and local ('static')
  43. ******************************************************************************/
  44. /**
  45. * @brief BSP TCA9539 write data.
  46. * @param [in] bus: Pointer to the i2c bus device.
  47. * @param [in] reg: Register to be written.
  48. * @param [in] data: The pointer to the buffer contains the data to be written.
  49. * @param [in] len: Buffer size in byte.
  50. * @retval rt_err_t:
  51. * - RT_EOK
  52. * - -RT_ERROR
  53. */
  54. static rt_err_t BSP_TCA9539_I2C_Write(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data, rt_uint16_t len)
  55. {
  56. struct rt_i2c_msg msgs;
  57. rt_uint8_t buf[6];
  58. buf[0] = reg;
  59. if (len > 0)
  60. {
  61. if (len < 6)
  62. {
  63. rt_memcpy(buf + 1, data, len);
  64. }
  65. else
  66. {
  67. return -RT_ERROR;
  68. }
  69. }
  70. msgs.addr = BSP_TCA9539_DEV_ADDR;
  71. msgs.flags = RT_I2C_WR;
  72. msgs.buf = buf;
  73. msgs.len = len + 1;
  74. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  75. {
  76. return RT_EOK;
  77. }
  78. else
  79. {
  80. return -RT_ERROR;
  81. }
  82. }
  83. /**
  84. * @brief BSP TCA9539 Read data.
  85. * @param [in] bus: Pointer to the i2c bus device.
  86. * @param [in] reg: Register to be read.
  87. * @param [out] data: The pointer to the buffer contains the data to be read.
  88. * @param [in] len: Buffer size in byte.
  89. * @retval rt_err_t:
  90. * - RT_EOK
  91. * - -RT_ERROR
  92. */
  93. static rt_err_t BSP_TCA9539_I2C_Read(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data, rt_uint16_t len)
  94. {
  95. struct rt_i2c_msg msgs;
  96. if (RT_EOK != BSP_TCA9539_I2C_Write(bus, reg, RT_NULL, 0))
  97. {
  98. return -RT_ERROR;
  99. }
  100. msgs.addr = BSP_TCA9539_DEV_ADDR;
  101. msgs.flags = RT_I2C_RD;
  102. msgs.buf = data;
  103. msgs.len = len;
  104. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  105. {
  106. return RT_EOK;
  107. }
  108. else
  109. {
  110. return -RT_ERROR;
  111. }
  112. }
  113. /**
  114. * @brief Reset TCA9539.
  115. * @param [in] None
  116. * @retval None
  117. */
  118. static void TCA9539_Reset(void)
  119. {
  120. rt_pin_mode(TCA9539_RST_PIN, PIN_MODE_OUTPUT);
  121. /* Reset the device */
  122. rt_pin_write(TCA9539_RST_PIN, PIN_LOW);
  123. rt_thread_mdelay(3U);
  124. rt_pin_write(TCA9539_RST_PIN, PIN_HIGH);
  125. }
  126. /**
  127. * @brief Write TCA9539 pin output value.
  128. * @param [in] u8Port Port number.
  129. * This parameter can be one of the following values:
  130. * @arg @ref TCA9539_Port_Definition
  131. * @param [in] u8Pin Pin number.
  132. * This parameter can be one or any combination of the following values:
  133. * @arg @ref TCA9539_Pin_Definition
  134. * @param [in] u8PinState Pin state to be written.
  135. * This parameter can be one of the following values:
  136. * @arg @ref TCA9539_Pin_State_Definition
  137. * @retval rt_err_t:
  138. * - RT_ERROR
  139. * - RT_EOK
  140. */
  141. rt_err_t TCA9539_WritePin(uint8_t u8Port, uint8_t u8Pin, uint8_t u8PinState)
  142. {
  143. uint8_t u8TempData[2];
  144. u8TempData[0] = u8Port + TCA9539_REG_OUTPUT_PORT0;
  145. if (RT_EOK != BSP_TCA9539_I2C_Read(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  146. {
  147. return -RT_ERROR;
  148. }
  149. if (0U == u8PinState)
  150. {
  151. u8TempData[1] &= (uint8_t)(~u8Pin);
  152. }
  153. else
  154. {
  155. u8TempData[1] |= u8Pin;
  156. }
  157. if (RT_EOK != BSP_TCA9539_I2C_Write(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  158. {
  159. return -RT_ERROR;
  160. }
  161. return RT_EOK;
  162. }
  163. /**
  164. * @brief Read TCA9539 pin input value.
  165. * @param [in] u8Port Port number.
  166. * This parameter can be one of the following values:
  167. * @arg @ref TCA9539_Port_Definition
  168. * @param [in] u8Pin Pin number.
  169. * This parameter can be one or any combination of the following values:
  170. * @arg @ref TCA9539_Pin_Definition
  171. * @param [in] u8PinState Pin state to be written.
  172. * This parameter can be one of the following values:
  173. * @arg @ref TCA9539_Pin_State_Definition
  174. * @retval rt_err_t:
  175. * - RT_ERROR
  176. * - RT_EOK
  177. */
  178. rt_err_t TCA9539_ReadPin(uint8_t u8Port, uint8_t u8Pin, uint8_t *pu8PinState)
  179. {
  180. uint8_t u8TempData[2];
  181. u8TempData[0] = u8Port + TCA9539_REG_INPUT_PORT0;
  182. if (RT_EOK != BSP_TCA9539_I2C_Read(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  183. {
  184. return -RT_ERROR;
  185. }
  186. if (0U != (u8TempData[1] & u8Pin))
  187. {
  188. *pu8PinState = TCA9539_PIN_SET;
  189. }
  190. else
  191. {
  192. *pu8PinState = TCA9539_PIN_RESET;
  193. }
  194. return RT_EOK;
  195. }
  196. /**
  197. * @brief Toggle TCA9539 pin output value.
  198. * @param [in] u8Port Port number.
  199. * This parameter can be one of the following values:
  200. * @arg @ref TCA9539_Port_Definition
  201. * @param [in] u8Pin Pin number.
  202. * This parameter can be one or any combination of the following values:
  203. * @arg @ref TCA9539_Pin_Definition
  204. * @retval rt_err_t:
  205. * - -RT_ERROR
  206. * - RT_EOK
  207. */
  208. rt_err_t TCA9539_TogglePin(uint8_t u8Port, uint8_t u8Pin)
  209. {
  210. uint8_t u8TempData[2];
  211. u8TempData[0] = u8Port + TCA9539_REG_OUTPUT_PORT0;
  212. if (RT_EOK != BSP_TCA9539_I2C_Read(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  213. {
  214. return -RT_ERROR;
  215. }
  216. u8TempData[1] ^= u8Pin;
  217. if (RT_EOK != BSP_TCA9539_I2C_Write(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  218. {
  219. return -RT_ERROR;
  220. }
  221. return RT_EOK;
  222. }
  223. /**
  224. * @brief Configuration TCA9539 pin.
  225. * @param [in] u8Port Port number.
  226. * This parameter can be one of the following values:
  227. * @arg @ref TCA9539_Port_Definition
  228. * @param [in] u8Pin Pin number.
  229. * This parameter can be one or any combination of the following values:
  230. * @arg @ref TCA9539_Pin_Definition
  231. * @param [in] u8Dir Pin output direction.
  232. * This parameter can be one of the following values:
  233. * @arg @ref TCA9539_Direction_Definition
  234. * @retval rt_err_t:
  235. * - -RT_ERROR
  236. * - RT_EOK
  237. */
  238. rt_err_t TCA9539_ConfigPin(uint8_t u8Port, uint8_t u8Pin, uint8_t u8Dir)
  239. {
  240. uint8_t u8TempData[2];
  241. u8TempData[0] = u8Port + TCA9539_REG_CONFIG_PORT0;
  242. if (RT_EOK != BSP_TCA9539_I2C_Read(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  243. {
  244. return -RT_ERROR;
  245. }
  246. if (TCA9539_DIR_OUT == u8Dir)
  247. {
  248. u8TempData[1] &= (uint8_t)(~u8Pin);
  249. }
  250. else
  251. {
  252. u8TempData[1] |= u8Pin;
  253. }
  254. if (RT_EOK != BSP_TCA9539_I2C_Write(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  255. {
  256. return -RT_ERROR;
  257. }
  258. return RT_EOK;
  259. }
  260. /**
  261. * @brief Initialize TCA9539.
  262. * @param [in] None
  263. * @retval rt_err_t:
  264. * - -RT_ERROR
  265. * - RT_EOK
  266. */
  267. int TCA9539_Init(void)
  268. {
  269. char name[RT_NAME_MAX];
  270. uint8_t u8TempData[2];
  271. TCA9539_Reset();
  272. rt_strncpy(name, BSP_TCA9539_I2C_BUS_NAME, RT_NAME_MAX);
  273. i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
  274. if (i2c_bus == RT_NULL)
  275. {
  276. rt_kprintf("can't find %s device!\n", BSP_TCA9539_I2C_BUS_NAME);
  277. return -RT_ERROR;
  278. }
  279. /* All Pins are input as default */
  280. u8TempData[0] = TCA9539_REG_CONFIG_PORT0;
  281. u8TempData[1] = 0xFFU;
  282. if (RT_EOK != BSP_TCA9539_I2C_Write(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  283. {
  284. return -RT_ERROR;
  285. }
  286. u8TempData[0] = TCA9539_REG_CONFIG_PORT1;
  287. if (RT_EOK != BSP_TCA9539_I2C_Write(i2c_bus, u8TempData[0], &u8TempData[1], 1U))
  288. {
  289. return -RT_ERROR;
  290. }
  291. return RT_EOK;
  292. }
  293. INIT_PREV_EXPORT(TCA9539_Init);
  294. #endif /* BSP_USING_TCA9539 */