pcal6408a.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef PCAL6408A_H__
  41. #define PCAL6408A_H__
  42. #include "nrf_twi_sensor.h"
  43. #include "pcal6408a_internal.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup pcal6408a_driver PCAL6408A Driver
  49. * @ingroup ext_drivers
  50. *
  51. * @brief Module for configuring and using PCAL6408A GPIO expander.
  52. *
  53. * @{
  54. */
  55. /**
  56. * @brief First possible expander address.
  57. */
  58. #define PCAL6408A_BASE_ADDRESS_FIRST 0x20u
  59. /**
  60. * @brief Second possible expander address.
  61. */
  62. #define PCAL6408A_BASE_ADDRESS_SECOND 0x21u
  63. /**
  64. * @brief Device registers.
  65. */
  66. typedef enum
  67. {
  68. PCAL6408A_REG_INPUT_PORT = 0x00,
  69. PCAL6408A_REG_OUTPUT_PORT = 0x01,
  70. PCAL6408A_REG_POLARITY_INVERSION = 0x02,
  71. PCAL6408A_REG_CONFIGURATION = 0x03,
  72. PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0 = 0x40,
  73. PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1 = 0x41,
  74. PCAL6408A_REG_INPUT_LATCH = 0x42,
  75. PCAL6408A_REG_PULL_UP_DOWN_ENABLE = 0x43,
  76. PCAL6408A_REG_PULL_UP_DOWN_SELECT = 0x44,
  77. PCAL6408A_REG_INTERRUPT_MASK = 0x45,
  78. PCAL6408A_REG_INTERRUPT_STATUS = 0x46,
  79. PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION = 0x4F
  80. } pcal6408a_registers_t;
  81. /**
  82. * @brief Enumerator used for setting the direction of a pin.
  83. */
  84. typedef enum
  85. {
  86. PCAL6408A_PIN_DIR_OUTPUT, /**< Output. */
  87. PCAL6408A_PIN_DIR_INPUT /**< Input. */
  88. } pcal6408a_pin_dir_t;
  89. /**
  90. * @brief Enumerator used for setting the direction of a port.
  91. */
  92. typedef enum
  93. {
  94. PCAL6408A_PORT_DIR_OUTPUT = 0x00, /**< Output. */
  95. PCAL6408A_PORT_DIR_INPUT = 0xFF /**< Input. */
  96. } pcal6408a_port_dir_t;
  97. /**
  98. * @brief Enumerator used for setting the state of a pin configured as an output.
  99. */
  100. typedef enum
  101. {
  102. PCAL6408A_PIN_CLR, /**< Clear. */
  103. PCAL6408A_PIN_SET /**< Set. */
  104. } pcal6408a_pin_set_t;
  105. /**
  106. * @brief Enumerator used for selecting the pin to be pulled down or up.
  107. */
  108. typedef enum
  109. {
  110. PCAL6408A_PIN_NOPULL, /**< No pull. */
  111. PCAL6408A_PIN_PULLDOWN, /**< Pin pulldown resistor enabled. */
  112. PCAL6408A_PIN_PULLUP /**< Pin pullup resistor enabled. */
  113. } pcal6408a_pin_pull_t;
  114. /**
  115. * @brief Enumerator used for selecting the operation for a port.
  116. */
  117. typedef enum
  118. {
  119. PCAL6408A_PORT_WRITE, /**< Mask is written to the port. */
  120. PCAL6408A_PORT_CLEAR, /**< Positive bits in mask are cleared in port. */
  121. PCAL6408A_PORT_SET /**< Positive bits in mask are set in port. */
  122. } pcal6408a_port_op_t;
  123. /**
  124. * @brief Enumerator used for setting the drive strength of a pin.
  125. */
  126. typedef enum
  127. {
  128. PCAL6408A_PIN_25_DRIVE_STRENGTH, /**< Drive strength set to 25% of current drive capability. */
  129. PCAL6408A_PIN_50_DRIVE_STRENGTH, /**< Drive strength set to 50% of current drive capability. */
  130. PCAL6408A_PIN_75_DRIVE_STRENGTH, /**< Drive strength set to 75% of current drive capability. */
  131. PCAL6408A_PIN_100_DRIVE_STRENGTH /**< Drive strength set to 100% of current drive capability. */
  132. } pcal6408a_pin_drive_strength_t;
  133. /**
  134. * @brief Enumerator used for setting push-pull or open-drain I/O stage for a port.
  135. */
  136. typedef enum
  137. {
  138. PCAL6408A_PORT_PUSH_PULL, /**< Push-pull I/O stage. */
  139. PCAL6408A_PORT_OPEN_DRAIN /**< Open-drain I/O stage. */
  140. } pcal6408a_port_io_stage_t;
  141. /**
  142. * @brief Macro that defines expander module.
  143. *
  144. * @param[in] pcal6408a_inst_name Name of the instance to be created.
  145. * @param[in] instance_count Number of connected expanders.
  146. */
  147. #define PCAL6408A_INSTANCES_DEF_START(pcal6408a_inst_name, instance_count) \
  148. static pcal6408a_instance_t pcal6408a_inst_name[instance_count]
  149. /**
  150. * @brief Macro that converts absolute pin number to pin number dependent on number of expander.
  151. *
  152. * @param[in] pin_num Absolute pin number ranging from 0 to 7.
  153. * @param[in] instance_num Number of expander, order is the same as pcal6408a_add_instance calls.
  154. */
  155. #define PIN_NUM_CONVERT(pin_num, instance_num) \
  156. (pin_num + instance_num * PCAL6408A_INNER_PIN_COUNT)
  157. /**
  158. * @brief Function initialising expander module.
  159. *
  160. * @param[in] p_instances Pointer to expander module.
  161. * @param[in] count Number of connected expanders.
  162. */
  163. void pcal6408a_init(pcal6408a_instance_t * p_instances, uint8_t count);
  164. /**
  165. * @brief Function adding expander instance.
  166. *
  167. * @note Should be called for every connected expander.
  168. * Order of calls define order of pins and ports.
  169. *
  170. * @param[in] p_twi_sensor Pointer to common sensor instance. @ref NRF_TWI_SENSOR_DEF
  171. * @param[in] sensor_address Address of expander on I2C bus.
  172. *
  173. * @retval NRF_ERROR_MODULE_NOT_INITIALIZED If expander module wasn't initialised
  174. * @retval NRF_ERROR_STORAGE_FULL If trying to add more instances than defined.
  175. * @retval other Error code from nrf_twi_sensor
  176. * @ref nrf_twi_sensor_write
  177. */
  178. ret_code_t pcal6408a_add_instance(nrf_twi_sensor_t * p_twi_sensor, uint8_t sensor_address);
  179. /**
  180. * @brief Function for writing current configuration to expander.
  181. *
  182. * @param[in] instance_num Number of expander, order is the same as pcal6408a_add_instance calls.
  183. *
  184. * @retval NRF_ERROR_INVALID_PARAM If there is no expander with given number.
  185. * @retval other Error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  186. */
  187. ret_code_t pcal6408a_cfg_write(uint8_t instance_num);
  188. /**
  189. * @brief Function for reading current configuration of expander.
  190. *
  191. * @param[in] instance_num Number of expander, order is the same as pcal6408a_add_instance calls.
  192. *
  193. * @retval NRF_ERROR_INVALID_PARAM If there is no expander with given number.
  194. * @retval other Error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  195. */
  196. ret_code_t pcal6408a_cfg_read(uint8_t instance_num);
  197. /**
  198. * @brief Function for setting register configuration of a single pin.
  199. *
  200. * @param[in] reg_addr Register address.
  201. * @param[in] pin Pin number.
  202. * @param[in] value Value to set.
  203. *
  204. * @return Error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  205. */
  206. ret_code_t pcal6408a_pin_cfg_reg_set(pcal6408a_registers_t reg_addr, uint32_t pin, uint8_t value);
  207. /**
  208. * @brief Function for getting register configuration of a single pin.
  209. *
  210. * @param[in] reg_addr Register address.
  211. * @param[in] pin Pin number.
  212. *
  213. * @return Pin configuration value
  214. */
  215. uint8_t pcal6408a_pin_cfg_reg_get(pcal6408a_registers_t reg_addr, uint32_t pin);
  216. /**
  217. * @brief Function for setting register configuration of a port.
  218. *
  219. * @param[in] reg_addr Register address.
  220. * @param[in] port Port number.
  221. * @param[in] mask Mask for the operation.
  222. * @param[in] flag Operation, whether mask should be written into register,
  223. * values should be cleared or set @ref pcal6408a_port_op_t
  224. *
  225. * @retval NRF_ERROR_INVALID_PARAM If there is no port with such number or invalid flag operation.
  226. * @retval other Error code from nrf_twi_sensor @ref nrf_twi_sensor_write
  227. */
  228. ret_code_t pcal6408a_port_cfg_reg_set(pcal6408a_registers_t reg_addr,
  229. uint32_t port,
  230. uint8_t mask,
  231. pcal6408a_port_op_t flag);
  232. /**
  233. * @brief Function for getting register configuration of a port.
  234. *
  235. * @note When reading input register, it should be updated prior using this function,
  236. * with @ref pcal6408a_pin_data_update
  237. * When reading interrupt status register, it should be updated prior using this function,
  238. * with @ref pcal6408a_int_status_update
  239. *
  240. * @param[in] reg_addr Register address.
  241. * @param[in] port Port number.
  242. *
  243. * @return Register value
  244. */
  245. uint8_t pcal6408a_port_cfg_reg_get(pcal6408a_registers_t reg_addr, uint32_t port);
  246. /**
  247. * @brief Function for updating pin data.
  248. *
  249. * @param user_cb Function to be called after pin data update is done.
  250. *
  251. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  252. */
  253. ret_code_t pcal6408a_pin_data_update(nrf_twi_sensor_reg_cb_t user_cb);
  254. /**
  255. * @brief Function for updating interrupt status data.
  256. *
  257. * @param user_cb Function to be called after interrupt status update is done.
  258. *
  259. * @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
  260. */
  261. ret_code_t pcal6408a_int_status_update(nrf_twi_sensor_reg_cb_t user_cb);
  262. /**
  263. * @brief Function for setting polarity inversion of a given pin.
  264. *
  265. * @note Note that the pin must be configured as an input for this function to have any effect.
  266. *
  267. * @param[in] pin_number Specifies the pin number.
  268. * @param[in] state
  269. * @arg true Enables polarity inversion.
  270. * @arg false Disables polarity inversion.
  271. *
  272. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  273. */
  274. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_polarity_inversion(uint32_t pin_number, bool state);
  275. /**
  276. * @brief Function for setting interrupt of a given pin.
  277. *
  278. * @note Note that the pin must be configured as an input for this function to have any effect.
  279. *
  280. * @param[in] pin_number Specifies the pin number.
  281. * @param[in] state
  282. * @arg true Disables interrupt.
  283. * @arg false Enables interrupt.
  284. *
  285. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  286. */
  287. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_interrupt(uint32_t pin_number, bool state);
  288. /**
  289. * @brief Function for setting input latch of a given pin.
  290. *
  291. * @note Note that the pin must be configured as an input for this function to have any effect.
  292. *
  293. * @param[in] pin_number Specifies the pin number.
  294. * @param[in] state
  295. * @arg true Enables input latch.
  296. * @arg false Disables input latch.
  297. *
  298. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  299. */
  300. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_latch(uint32_t pin_number, bool state);
  301. /**
  302. * @brief Function for setting drive strength for a given pin.
  303. *
  304. * @note Note that the pin must be configured as an output for this function to have any effect.
  305. *
  306. * @param[in] pin_number Specifies the pin number.
  307. * @param[in] drive_strength_config Drive strength of current drive capability (25%, 50%, 75%
  308. * or 100%) @ref pcal6408a_pin_drive_strength_t
  309. *
  310. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  311. */
  312. ret_code_t pcal6408a_pin_cfg_drive_strength(uint32_t pin_number,
  313. pcal6408a_pin_drive_strength_t drive_strength_config);
  314. /**
  315. * @brief Function for setting polarity inversion of a given port.
  316. *
  317. * @note Note that this function have an effect only for pins that are configured as an input.
  318. *
  319. * @param[in] port_number Specifies the port number.
  320. * @param[in] polarity_mask Specifies the mask.
  321. * @param[in] flag Operation, whether mask should be written into register,
  322. * values should be cleared or set @ref pcal6408a_port_op_t
  323. *
  324. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  325. */
  326. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_polarity_inversion(uint32_t port_number,
  327. uint8_t polarity_mask,
  328. pcal6408a_port_op_t flag);
  329. /**
  330. * @brief Function for setting interrupt of a given port.
  331. *
  332. * @note Note that this function have an effect only for pins that are configured as an input.
  333. *
  334. * @param[in] port_number Specifies the port number.
  335. * @param[in] interrupt_mask Specifies the mask.
  336. * @param[in] flag Operation, whether mask should be written into register,
  337. * values should be cleared or set @ref pcal6408a_port_op_t
  338. *
  339. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  340. */
  341. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_interrupt(uint32_t port_number,
  342. uint8_t interrupt_mask,
  343. pcal6408a_port_op_t flag);
  344. /**
  345. * @brief Function for setting input latch of a given port.
  346. *
  347. * @note Note that this function have an effect only for pins that are configured as an input.
  348. *
  349. * @param[in] port_number Specifies the port number.
  350. * @param[in] latch_mask Specifies the mask.
  351. * @param[in] flag Operation, whether mask should be written into register,
  352. * values should be cleared or set @ref pcal6408a_port_op_t
  353. *
  354. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  355. */
  356. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_latch(uint32_t port_number,
  357. uint8_t latch_mask,
  358. pcal6408a_port_op_t flag);
  359. /**
  360. * @brief Function for setting drive strength for a given port.
  361. *
  362. * @note Note that this function have an effect only for pins that are configured as an output.
  363. *
  364. * @param[in] port_number Specifies the port number.
  365. * @param[in] drive_strength_mask Specifies the mask. Note that for each pin there are dedicated
  366. * two adjacent bits.
  367. * @param[in] flag Operation, whether mask should be written into register,
  368. * values should be cleared or set @ref pcal6408a_port_op_t
  369. *
  370. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  371. */
  372. ret_code_t pcal6408a_port_cfg_drive_strength(uint32_t port_number,
  373. uint16_t drive_strength_mask,
  374. pcal6408a_port_op_t flag);
  375. /**
  376. * @brief Function for selecting push-pull or open-drain I/O stage for the given port.
  377. *
  378. * @param[in] port_number Specifies the port number.
  379. * @param[in] io_stage_config I/O stage of the port (push-pull or open-drain)
  380. * @ref pcal6408a_port_io_stage_t
  381. *
  382. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  383. */
  384. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_io_stage(uint32_t port_number,
  385. pcal6408a_port_io_stage_t io_stage_config);
  386. /**
  387. * @brief Function for configuring the given pin number as output.
  388. *
  389. * @param[in] pin_number Specifies the pin number.
  390. *
  391. * @return Error code from pin config set @ref pcal6408a_pin_cfg_reg_set
  392. */
  393. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_output(uint32_t pin_number);
  394. /**
  395. * @brief Function for configuring the given pin number as input.
  396. *
  397. * @param[in] pin_number Specifies the pin number.
  398. * @param[in] pull_config State of the pin pull resistor (no pull, pulled down, or pulled high)
  399. * @ref pcal6408a_pin_pull_t
  400. *
  401. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  402. */
  403. ret_code_t pcal6408a_pin_cfg_input(uint32_t pin_number, pcal6408a_pin_pull_t pull_config);
  404. /**
  405. * @brief Function for setting a pin.
  406. *
  407. * @note Note that the pin must be configured as an output for this function to have any effect.
  408. *
  409. * @param[in] pin_number Specifies the pin number to set.
  410. *
  411. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  412. */
  413. __STATIC_INLINE ret_code_t pcal6408a_pin_set(uint32_t pin_number);
  414. /**
  415. * @brief Function for clearing a pin.
  416. *
  417. * @note Note that the pin must be configured as an output for this function to have any effect.
  418. *
  419. * @param[in] pin_number Specifies the pin number to clear.
  420. *
  421. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  422. */
  423. __STATIC_INLINE ret_code_t pcal6408a_pin_clear(uint32_t pin_number);
  424. /**
  425. * @brief Function for configuring the pin range as outputs.
  426. *
  427. * @note For configuring only one pin as an output use @ref pcal6408a_pin_cfg_output.
  428. *
  429. * @param[in] pin_range_start Specifies the start number (inclusive) in the range of pin numbers
  430. * to be configured.
  431. * @param[in] pin_range_end Specifies the end number (inclusive) in the range of pin numbers
  432. * to be configured.
  433. *
  434. * @retval NRF_ERROR_INVALID_LENGTH If start number is greater than end number.
  435. * @retval other Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  436. */
  437. ret_code_t pcal6408a_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end);
  438. /**
  439. * @brief Function for configuring the pin range as inputs.
  440. *
  441. * @note For configuring only one pin as an input use @ref pcal6408a_pin_cfg_input.
  442. *
  443. * @param[in] pin_range_start Specifies the start number (inclusive) in the range of pin numbers
  444. * to be configured.
  445. * @param[in] pin_range_end Specifies the end number (inclusive) in the range of pin numbers
  446. * to be configured.
  447. * @param[in] pull_config State of the pin pull resistor (no pull, pulled down, or pulled high)
  448. * @ref pcal6408a_pin_pull_t
  449. *
  450. * @retval NRF_ERROR_INVALID_LENGTH If start number is greater than end number.
  451. * @retval other Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  452. */
  453. ret_code_t pcal6408a_range_cfg_input(uint32_t pin_range_start,
  454. uint32_t pin_range_end,
  455. pcal6408a_pin_pull_t pull_config);
  456. /**
  457. * @brief Function for setting the direction for a given pin.
  458. *
  459. * @param[in] pin_number Specifies the pin number.
  460. * @param[in] direction Specifies the direction.
  461. *
  462. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  463. */
  464. __STATIC_INLINE ret_code_t pcal6408a_pin_dir_set(uint32_t pin_number,
  465. pcal6408a_pin_dir_t direction);
  466. /**
  467. * @brief Function for toggling a given pin.
  468. *
  469. * @note Note that the pin must be configured as an output for this function to have any effect.
  470. *
  471. * @param[in] pin_number Specifies the pin number.
  472. *
  473. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  474. */
  475. __STATIC_INLINE ret_code_t pcal6408a_pin_toggle(uint32_t pin_number);
  476. /**
  477. * @brief Function for writing a value to a given pin.
  478. *
  479. * @note Note that the pin must be configured as an output for this function to have any effect.
  480. *
  481. * @param[in] pin_number Specifies the pin number.
  482. * @param[in] value Specifies the value to be written to the pin.
  483. * @arg 0 Clears the pin.
  484. * @arg 1 Sets the pin.
  485. *
  486. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  487. */
  488. __STATIC_INLINE ret_code_t pcal6408a_pin_write(uint32_t pin_number, uint8_t value);
  489. /**
  490. * @brief Function for reading the input level of a given pin.
  491. *
  492. * @note Input data should be updated prior using this function, with @ref pcal6408a_pin_data_update
  493. *
  494. * @param[in] pin_number Specifies the pin number.
  495. *
  496. * @return Error code from pin_cfg_reg_set @ref pcal6408a_pin_cfg_reg_set
  497. */
  498. __STATIC_INLINE uint32_t pcal6408a_pin_read(uint32_t pin_number);
  499. /**
  500. * @brief Function for setting the direction of a port.
  501. *
  502. * @param[in] port_number Specifies the port number.
  503. * @param[in] direction Specifies the direction.
  504. *
  505. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  506. */
  507. __STATIC_INLINE ret_code_t pcal6408a_port_dir_set(uint32_t port_number,
  508. pcal6408a_port_dir_t direction);
  509. /**
  510. * @brief Function for reading a given port.
  511. *
  512. * @note Input data should be updated prior using this function, with @ref pcal6408a_pin_data_update
  513. *
  514. * @param[in] port_number Specifies the port number.
  515. *
  516. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  517. */
  518. __STATIC_INLINE uint32_t pcal6408a_port_read(uint32_t port_number);
  519. /**
  520. * @brief Function for writing to a given port.
  521. *
  522. * @note Note that this function have an effect only for pins that are configured as an output.
  523. *
  524. * @param[in] port_number Specifies the port number.
  525. * @param[in] value Specifies the value to be written to the port.
  526. *
  527. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  528. */
  529. __STATIC_INLINE ret_code_t pcal6408a_port_write(uint32_t port_number, uint8_t value);
  530. /**
  531. * @brief Function for setting individual pins on given port.
  532. *
  533. * @note Note that this function have an effect only for pins that are configured as an output.
  534. *
  535. * @param[in] port_number Specifies the port number.
  536. * @param[in] set_mask Mask specifying which pins to set. A bit set to 1 indicates that
  537. * the corresponding port pin shall be set.
  538. *
  539. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  540. */
  541. __STATIC_INLINE ret_code_t pcal6408a_port_set(uint32_t port_number, uint8_t set_mask);
  542. /**
  543. * @brief Function for clearing individual pins on given port.
  544. *
  545. * @note Note that this function have an effect only for pins that are configured as an output.
  546. *
  547. * @param[in] port_number Specifies the port number.
  548. * @param[in] clr_mask Mask specifying which pins to clear. A bit set to 1 indicates that
  549. * the corresponding port pin shall be cleared.
  550. *
  551. * @return Error code from port_cfg_reg_set @ref pcal6408a_port_cfg_reg_set
  552. */
  553. __STATIC_INLINE ret_code_t pcal6408a_port_clear(uint32_t port_number, uint8_t clr_mask);
  554. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  555. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_polarity_inversion(uint32_t pin_number, bool state)
  556. {
  557. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_POLARITY_INVERSION, pin_number, state);
  558. }
  559. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_interrupt(uint32_t pin_number, bool state)
  560. {
  561. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_INTERRUPT_MASK, pin_number, state);
  562. }
  563. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_latch(uint32_t pin_number, bool state)
  564. {
  565. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_INPUT_LATCH, pin_number, state);
  566. }
  567. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_polarity_inversion(uint32_t port_number,
  568. uint8_t polarity_mask,
  569. pcal6408a_port_op_t flag)
  570. {
  571. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_POLARITY_INVERSION,
  572. port_number,
  573. polarity_mask,
  574. flag);
  575. }
  576. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_interrupt(uint32_t port_number,
  577. uint8_t interrupt_mask,
  578. pcal6408a_port_op_t flag)
  579. {
  580. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_INTERRUPT_MASK,
  581. port_number,
  582. interrupt_mask,
  583. flag);
  584. }
  585. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_latch(uint32_t port_number,
  586. uint8_t latch_mask,
  587. pcal6408a_port_op_t flag)
  588. {
  589. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_INPUT_LATCH,
  590. port_number,
  591. latch_mask,
  592. flag);
  593. }
  594. __STATIC_INLINE ret_code_t pcal6408a_port_cfg_io_stage(uint32_t port_number,
  595. pcal6408a_port_io_stage_t io_stage_config)
  596. {
  597. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION,
  598. port_number * PCAL6408A_INNER_PIN_COUNT,
  599. io_stage_config);
  600. }
  601. __STATIC_INLINE ret_code_t pcal6408a_pin_cfg_output(uint32_t pin_number)
  602. {
  603. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  604. pin_number,
  605. PCAL6408A_PIN_DIR_OUTPUT);
  606. }
  607. __STATIC_INLINE ret_code_t pcal6408a_pin_set(uint32_t pin_number)
  608. {
  609. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT, pin_number, PCAL6408A_PIN_SET);
  610. }
  611. __STATIC_INLINE ret_code_t pcal6408a_pin_clear(uint32_t pin_number)
  612. {
  613. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT, pin_number, PCAL6408A_PIN_CLR);
  614. }
  615. __STATIC_INLINE ret_code_t pcal6408a_pin_dir_set(uint32_t pin_number, pcal6408a_pin_dir_t direction)
  616. {
  617. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_CONFIGURATION, pin_number, direction);
  618. }
  619. __STATIC_INLINE ret_code_t pcal6408a_pin_toggle(uint32_t pin_number)
  620. {
  621. return pcal6408a_pin_cfg_reg_set(
  622. PCAL6408A_REG_OUTPUT_PORT,
  623. pin_number,
  624. !pcal6408a_pin_cfg_reg_get(PCAL6408A_REG_OUTPUT_PORT, pin_number));
  625. }
  626. __STATIC_INLINE ret_code_t pcal6408a_pin_write(uint32_t pin_number, uint8_t value)
  627. {
  628. return pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT, pin_number, value);
  629. }
  630. __STATIC_INLINE uint32_t pcal6408a_pin_read(uint32_t pin_number)
  631. {
  632. return pcal6408a_pin_cfg_reg_get(PCAL6408A_REG_INPUT_PORT, pin_number);
  633. }
  634. __STATIC_INLINE ret_code_t pcal6408a_port_dir_set(uint32_t port_number,
  635. pcal6408a_port_dir_t direction)
  636. {
  637. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  638. port_number,
  639. direction,
  640. PCAL6408A_PORT_WRITE);
  641. }
  642. __STATIC_INLINE uint32_t pcal6408a_port_read(uint32_t port_number)
  643. {
  644. return pcal6408a_port_cfg_reg_get(PCAL6408A_REG_INPUT_PORT, port_number);
  645. }
  646. __STATIC_INLINE ret_code_t pcal6408a_port_write(uint32_t port_number, uint8_t value)
  647. {
  648. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT,
  649. port_number,
  650. value,
  651. PCAL6408A_PORT_WRITE);
  652. }
  653. __STATIC_INLINE ret_code_t pcal6408a_port_set(uint32_t port_number, uint8_t set_mask)
  654. {
  655. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT,
  656. port_number,
  657. set_mask,
  658. PCAL6408A_PORT_SET);
  659. }
  660. __STATIC_INLINE ret_code_t pcal6408a_port_clear(uint32_t port_number, uint8_t clr_mask)
  661. {
  662. return pcal6408a_port_cfg_reg_set(PCAL6408A_REG_OUTPUT_PORT,
  663. port_number,
  664. clr_mask,
  665. PCAL6408A_PORT_CLEAR);
  666. }
  667. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  668. /** @} */
  669. #ifdef __cplusplus
  670. }
  671. #endif
  672. #endif // PCAL6408A_H__