pcal6408a.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. #include "pcal6408a.h"
  41. static pcal6408a_instance_t * m_p_instances;
  42. static uint8_t m_max_instance_count;
  43. static uint8_t m_added_inst_count;
  44. #define PCAL6408A_WRITE(p_instance, msg) \
  45. nrf_twi_sensor_write(p_instance.p_sensor_data, \
  46. p_instance.sensor_addr, \
  47. msg, \
  48. ARRAY_SIZE(msg), \
  49. true)
  50. #define PCAL6408A_REG_OUTPUT_PORT_DEFAULT_VAL 0xFF
  51. #define PCAL6408A_REG_POLARITY_INVERSION_DEFAULT_VAL 0x00
  52. #define PCAL6408A_REG_CONFIGURATION_DEFAULT_VAL 0xFF
  53. #define PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0_DEFAULT_VAL 0xFF
  54. #define PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1_DEFAULT_VAL 0xFF
  55. #define PCAL6408A_REG_INPUT_LATCH_DEFAULT_VAL 0x00
  56. #define PCAL6408A_REG_PULL_UP_DOWN_ENABLE_DEFAULT_VAL 0x00
  57. #define PCAL6408A_REG_PULL_UP_DOWN_SELECT_DEFAULT_VAL 0xFF
  58. #define PCAL6408A_REG_INTERRUPT_MASK_DEFAULT_VAL 0xFF
  59. #define PCAL6408A_REG_INTERRUPT_STATUS_DEFAULT_VAL 0x00
  60. #define PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION_DEFAULT_VAL 0x00
  61. /**
  62. * ================================================================================================
  63. * @brief General expander utility functions.
  64. */
  65. void pcal6408a_init(pcal6408a_instance_t * p_instances, uint8_t count)
  66. {
  67. ASSERT(p_instances != NULL);
  68. m_p_instances = p_instances;
  69. m_max_instance_count = count;
  70. m_added_inst_count = 0;
  71. }
  72. static void pcal6408a_default_cfg_set(uint8_t instance_num)
  73. {
  74. m_p_instances[instance_num].registers[1] = PCAL6408A_REG_OUTPUT_PORT_DEFAULT_VAL;
  75. m_p_instances[instance_num].registers[2] = PCAL6408A_REG_POLARITY_INVERSION_DEFAULT_VAL;
  76. m_p_instances[instance_num].registers[3] = PCAL6408A_REG_CONFIGURATION_DEFAULT_VAL;
  77. m_p_instances[instance_num].registers[4] = PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0_DEFAULT_VAL;
  78. m_p_instances[instance_num].registers[5] = PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1_DEFAULT_VAL;
  79. m_p_instances[instance_num].registers[6] = PCAL6408A_REG_INPUT_LATCH_DEFAULT_VAL;
  80. m_p_instances[instance_num].registers[7] = PCAL6408A_REG_PULL_UP_DOWN_ENABLE_DEFAULT_VAL;
  81. m_p_instances[instance_num].registers[8] = PCAL6408A_REG_PULL_UP_DOWN_SELECT_DEFAULT_VAL;
  82. m_p_instances[instance_num].registers[9] = PCAL6408A_REG_INTERRUPT_MASK_DEFAULT_VAL;
  83. m_p_instances[instance_num].registers[10] = PCAL6408A_REG_INTERRUPT_STATUS_DEFAULT_VAL;
  84. m_p_instances[instance_num].registers[11] = PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION_DEFAULT_VAL;
  85. }
  86. ret_code_t pcal6408a_add_instance(nrf_twi_sensor_t * p_twi_sensor, uint8_t sensor_address)
  87. {
  88. ASSERT(p_twi_sensor != NULL);
  89. if (m_p_instances == NULL)
  90. {
  91. return NRF_ERROR_MODULE_NOT_INITIALIZED;
  92. }
  93. if (m_added_inst_count >= m_max_instance_count)
  94. {
  95. return NRF_ERROR_STORAGE_FULL;
  96. }
  97. m_p_instances[m_added_inst_count].p_sensor_data = p_twi_sensor;
  98. m_p_instances[m_added_inst_count].sensor_addr = sensor_address;
  99. pcal6408a_default_cfg_set(m_added_inst_count);
  100. m_added_inst_count++;
  101. ret_code_t err_code = pcal6408a_cfg_write(m_added_inst_count - 1);
  102. return err_code;
  103. }
  104. ret_code_t pcal6408a_cfg_write(uint8_t instance_num)
  105. {
  106. if (instance_num >= m_added_inst_count)
  107. {
  108. return NRF_ERROR_INVALID_PARAM;
  109. }
  110. ret_code_t err_code;
  111. for (uint8_t i = PCAL6408A_REG_OUTPUT_PORT; i <= PCAL6408A_REG_CONFIGURATION; i++)
  112. {
  113. err_code = nrf_twi_sensor_reg_write(m_p_instances[instance_num].p_sensor_data,
  114. m_p_instances[instance_num].sensor_addr,
  115. i,
  116. &m_p_instances[instance_num].registers[i],
  117. 1);
  118. if (err_code != NRF_SUCCESS)
  119. {
  120. goto end;
  121. }
  122. while (nrf_twi_mngr_is_idle(m_p_instances[instance_num].p_sensor_data->p_twi_mngr) != true)
  123. {
  124. // Wait for transaction to finish to not overflow msg buffer
  125. }
  126. }
  127. for (uint8_t i = PCAL6408A_REG_COUNT_SEQUENCE_1; i <= (PCAL6408A_REG_COUNT_ALL - 3); i++)
  128. {
  129. err_code = nrf_twi_sensor_reg_write(
  130. m_p_instances[instance_num].p_sensor_data,
  131. m_p_instances[instance_num].sensor_addr,
  132. i - PCAL6408A_REG_COUNT_SEQUENCE_1 + PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0,
  133. &m_p_instances[instance_num].registers[i],
  134. 1);
  135. if (err_code != NRF_SUCCESS)
  136. {
  137. goto end;
  138. }
  139. while (nrf_twi_mngr_is_idle(m_p_instances[instance_num].p_sensor_data->p_twi_mngr) != true)
  140. {
  141. // Wait for transaction to finish to not overflow msg buffer
  142. }
  143. }
  144. err_code = nrf_twi_sensor_reg_write(
  145. m_p_instances[instance_num].p_sensor_data,
  146. m_p_instances[instance_num].sensor_addr,
  147. PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION,
  148. &m_p_instances[instance_num].registers[PCAL6408A_REG_COUNT_ALL - 1],
  149. 1);
  150. end:
  151. return err_code;
  152. }
  153. ret_code_t pcal6408a_cfg_read(uint8_t instance_num)
  154. {
  155. if (instance_num >= m_added_inst_count)
  156. {
  157. return NRF_ERROR_INVALID_PARAM;
  158. }
  159. ret_code_t err_code;
  160. for (uint8_t i = 0; i <= PCAL6408A_REG_CONFIGURATION; i++)
  161. {
  162. err_code = nrf_twi_sensor_reg_read(m_p_instances[instance_num].p_sensor_data,
  163. m_p_instances[instance_num].sensor_addr,
  164. i,
  165. NULL,
  166. &m_p_instances[instance_num].registers[i],
  167. 1);
  168. if (err_code != NRF_SUCCESS)
  169. {
  170. goto end;
  171. }
  172. while (nrf_twi_mngr_is_idle(m_p_instances[instance_num].p_sensor_data->p_twi_mngr) != true)
  173. {
  174. // Wait for transaction to finish to not overflow msg buffer
  175. }
  176. }
  177. for(uint8_t i = PCAL6408A_REG_COUNT_SEQUENCE_1; i <= (PCAL6408A_REG_COUNT_ALL - 3); i++)
  178. {
  179. err_code = nrf_twi_sensor_reg_read(
  180. m_p_instances[instance_num].p_sensor_data,
  181. m_p_instances[instance_num].sensor_addr,
  182. i - PCAL6408A_REG_COUNT_SEQUENCE_1 + PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0,
  183. NULL,
  184. &m_p_instances[instance_num].registers[i],
  185. 1);
  186. if (err_code != NRF_SUCCESS)
  187. {
  188. goto end;
  189. }
  190. while (nrf_twi_mngr_is_idle(m_p_instances[instance_num].p_sensor_data->p_twi_mngr) != true)
  191. {
  192. // Wait for transaction to finish to not overflow msg buffer
  193. }
  194. }
  195. err_code = nrf_twi_sensor_reg_read(
  196. m_p_instances[instance_num].p_sensor_data,
  197. m_p_instances[instance_num].sensor_addr,
  198. PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION,
  199. NULL,
  200. &m_p_instances[instance_num].registers[PCAL6408A_REG_COUNT_ALL - 1],
  201. 1);
  202. end:
  203. return err_code;
  204. }
  205. ret_code_t pcal6408a_pin_data_update(nrf_twi_sensor_reg_cb_t user_cb)
  206. {
  207. ret_code_t err_code;
  208. for (uint8_t i = 0; i < m_added_inst_count - 1; i++)
  209. {
  210. err_code = nrf_twi_sensor_reg_read(m_p_instances[i].p_sensor_data,
  211. m_p_instances[i].sensor_addr,
  212. PCAL6408A_REG_INPUT_PORT,
  213. NULL,
  214. &m_p_instances[i].registers[PCAL6408A_REG_INPUT_PORT],
  215. 1);
  216. if (err_code != NRF_SUCCESS)
  217. {
  218. return err_code;
  219. }
  220. }
  221. return nrf_twi_sensor_reg_read(
  222. m_p_instances[m_added_inst_count - 1].p_sensor_data,
  223. m_p_instances[m_added_inst_count - 1].sensor_addr,
  224. PCAL6408A_REG_INPUT_PORT,
  225. user_cb,
  226. &m_p_instances[m_added_inst_count - 1].registers[PCAL6408A_REG_INPUT_PORT],
  227. 1);
  228. }
  229. ret_code_t pcal6408a_int_status_update(nrf_twi_sensor_reg_cb_t user_cb)
  230. {
  231. ret_code_t err_code;
  232. for (uint8_t i = 0; i < m_added_inst_count - 1; i++)
  233. {
  234. err_code = nrf_twi_sensor_reg_read(m_p_instances[i].p_sensor_data,
  235. m_p_instances[i].sensor_addr,
  236. PCAL6408A_REG_INTERRUPT_STATUS,
  237. NULL,
  238. &m_p_instances[i].registers[10],
  239. 1);
  240. if (err_code != NRF_SUCCESS)
  241. {
  242. return err_code;
  243. }
  244. }
  245. return nrf_twi_sensor_reg_read(
  246. m_p_instances[m_added_inst_count - 1].p_sensor_data,
  247. m_p_instances[m_added_inst_count - 1].sensor_addr,
  248. PCAL6408A_REG_INTERRUPT_STATUS,
  249. user_cb,
  250. &m_p_instances[m_added_inst_count - 1].registers[10],
  251. 1);
  252. }
  253. static uint8_t * get_reg_address(pcal6408a_registers_t reg_addr, uint8_t inst_num)
  254. {
  255. if (reg_addr <= PCAL6408A_REG_CONFIGURATION)
  256. {
  257. return &m_p_instances[inst_num].registers[reg_addr];
  258. }
  259. else if (reg_addr == PCAL6408A_REG_OUTPUT_PORT_CONFIGURATION)
  260. {
  261. return &m_p_instances[inst_num].registers[PCAL6408A_REG_COUNT_ALL - 1];
  262. }
  263. else if (reg_addr >= PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0
  264. && reg_addr <= PCAL6408A_REG_INTERRUPT_STATUS)
  265. {
  266. return &m_p_instances[inst_num].registers[PCAL6408A_REG_COUNT_SEQUENCE_1
  267. + reg_addr
  268. - PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0];
  269. }
  270. return NULL;
  271. }
  272. ret_code_t pcal6408a_pin_cfg_reg_set(pcal6408a_registers_t reg_addr, uint32_t pin, uint8_t value)
  273. {
  274. ASSERT(pin <= (PCAL6408A_INNER_PIN_COUNT * m_added_inst_count));
  275. uint8_t * p_reg_val;
  276. uint8_t mask;
  277. uint8_t inst_num = pin / PCAL6408A_INNER_PIN_COUNT;
  278. pin %= PCAL6408A_INNER_PIN_COUNT;
  279. if (reg_addr == PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0)
  280. {
  281. if (pin > PCAL6408A_DRIVE_STRENGTH_REG_0_PIN_MAX)
  282. {
  283. return NRF_ERROR_INVALID_PARAM;
  284. }
  285. mask = 3; // Current control register parameter is 2 bits long.
  286. pin *= 2;
  287. }
  288. else if (reg_addr == PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1)
  289. {
  290. if (pin <= PCAL6408A_DRIVE_STRENGTH_REG_0_PIN_MAX)
  291. {
  292. return NRF_ERROR_INVALID_PARAM;
  293. }
  294. mask = 3; // Current control register parameter is 2 bits long.
  295. pin %= PCAL6408A_INNER_PIN_COUNT / 2;
  296. pin *= 2;
  297. }
  298. else
  299. {
  300. mask = 1;
  301. }
  302. p_reg_val = get_reg_address(reg_addr, inst_num);
  303. if (p_reg_val == NULL)
  304. {
  305. return NRF_ERROR_INVALID_PARAM;
  306. }
  307. NRF_TWI_SENSOR_REG_SET(*p_reg_val, (uint8_t) (mask << pin), pin, value);
  308. uint8_t send_msg[] = {
  309. reg_addr,
  310. *p_reg_val
  311. };
  312. return PCAL6408A_WRITE(m_p_instances[inst_num], send_msg);
  313. }
  314. uint8_t pcal6408a_pin_cfg_reg_get(pcal6408a_registers_t reg_addr, uint32_t pin)
  315. {
  316. ASSERT(pin <= (PCAL6408A_INNER_PIN_COUNT * m_added_inst_count));
  317. uint8_t * p_reg_val;
  318. uint8_t mask = 1;
  319. uint8_t inst_num = pin / PCAL6408A_INNER_PIN_COUNT;
  320. pin %= PCAL6408A_INNER_PIN_COUNT;
  321. if (reg_addr == PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0
  322. || reg_addr == PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1)
  323. {
  324. if (pin > PCAL6408A_DRIVE_STRENGTH_REG_0_PIN_MAX)
  325. {
  326. reg_addr = PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1;
  327. pin %= PCAL6408A_INNER_PIN_COUNT / 2;
  328. }
  329. else
  330. {
  331. reg_addr = PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0;
  332. }
  333. mask = 3; //Current control register parameter is 2 bits long.
  334. pin *= 2;
  335. }
  336. p_reg_val = get_reg_address(reg_addr, inst_num);
  337. if (p_reg_val == NULL)
  338. {
  339. return NRF_ERROR_INVALID_PARAM;
  340. }
  341. return NRF_TWI_SENSOR_REG_VAL_GET(*p_reg_val, (mask << pin), pin);
  342. }
  343. ret_code_t pcal6408a_port_cfg_reg_set(pcal6408a_registers_t reg_addr,
  344. uint32_t port,
  345. uint8_t mask,
  346. pcal6408a_port_op_t flag)
  347. {
  348. if (port >= m_added_inst_count)
  349. {
  350. return NRF_ERROR_INVALID_PARAM;
  351. }
  352. uint8_t * p_reg_val;
  353. uint8_t inst_num = port;
  354. p_reg_val = get_reg_address(reg_addr, inst_num);
  355. if (p_reg_val == NULL)
  356. {
  357. return NRF_ERROR_INVALID_PARAM;
  358. }
  359. switch (flag)
  360. {
  361. case PCAL6408A_PORT_WRITE:
  362. *p_reg_val = mask;
  363. break;
  364. case PCAL6408A_PORT_CLEAR:
  365. *p_reg_val &= ~mask;
  366. break;
  367. case PCAL6408A_PORT_SET:
  368. *p_reg_val |= mask;
  369. break;
  370. default:
  371. break;
  372. }
  373. uint8_t send_msg[] = {
  374. reg_addr,
  375. *p_reg_val
  376. };
  377. return PCAL6408A_WRITE(m_p_instances[inst_num], send_msg);
  378. }
  379. uint8_t pcal6408a_port_cfg_reg_get(pcal6408a_registers_t reg_addr, uint32_t port)
  380. {
  381. ASSERT(port < m_added_inst_count);
  382. uint8_t inst_num = port;
  383. uint8_t * p_reg_val;
  384. p_reg_val = get_reg_address(reg_addr, inst_num);
  385. if (p_reg_val == NULL)
  386. {
  387. return NRF_ERROR_INVALID_PARAM;
  388. }
  389. return *p_reg_val;
  390. }
  391. ret_code_t pcal6408a_pin_cfg_drive_strength(uint32_t pin_number,
  392. pcal6408a_pin_drive_strength_t drive_strength_config)
  393. {
  394. ret_code_t err_code;
  395. if ((pin_number % PCAL6408A_INNER_PIN_COUNT) <= PCAL6408A_DRIVE_STRENGTH_REG_0_PIN_MAX)
  396. {
  397. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0,
  398. pin_number,
  399. drive_strength_config);
  400. }
  401. else
  402. {
  403. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1,
  404. pin_number,
  405. drive_strength_config);
  406. }
  407. return err_code;
  408. }
  409. ret_code_t pcal6408a_port_cfg_drive_strength(uint32_t port_number,
  410. uint16_t drive_strength_mask,
  411. pcal6408a_port_op_t flag)
  412. {
  413. ret_code_t err_code;
  414. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_0,
  415. port_number,
  416. (uint8_t)drive_strength_mask, flag);
  417. if (err_code != NRF_SUCCESS)
  418. {
  419. return err_code;
  420. }
  421. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_OUTPUT_DRIVE_STRENGTH_1,
  422. port_number,
  423. (uint8_t)(drive_strength_mask >> 8), flag);
  424. return err_code;
  425. }
  426. /**
  427. * ===============================================================================================
  428. * @brief Functions compatible with nrf_gpio
  429. */
  430. ret_code_t pcal6408a_pin_cfg_input(uint32_t pin_number, pcal6408a_pin_pull_t pull_config)
  431. {
  432. ret_code_t err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  433. pin_number,
  434. PCAL6408A_PIN_DIR_INPUT);
  435. if (err_code != NRF_SUCCESS)
  436. {
  437. goto end;
  438. }
  439. switch (pull_config)
  440. {
  441. case PCAL6408A_PIN_NOPULL:
  442. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_ENABLE, pin_number, 0);
  443. break;
  444. case PCAL6408A_PIN_PULLDOWN:
  445. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_ENABLE, pin_number, 1);
  446. if (err_code != NRF_SUCCESS)
  447. {
  448. goto end;
  449. }
  450. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_SELECT, pin_number, 0);
  451. break;
  452. case PCAL6408A_PIN_PULLUP:
  453. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_ENABLE, pin_number, 1);
  454. if (err_code != NRF_SUCCESS)
  455. {
  456. goto end;
  457. }
  458. err_code = pcal6408a_pin_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_SELECT, pin_number, 1);
  459. break;
  460. default:
  461. break;
  462. }
  463. end:
  464. return err_code;
  465. }
  466. ret_code_t pcal6408a_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)
  467. {
  468. if (pin_range_start > pin_range_end)
  469. {
  470. return NRF_ERROR_INVALID_LENGTH;
  471. }
  472. uint8_t start_port = pin_range_start / PCAL6408A_INNER_PIN_COUNT;
  473. uint8_t end_port = pin_range_end / PCAL6408A_INNER_PIN_COUNT;
  474. uint8_t range_value;
  475. ret_code_t err_code;
  476. if (start_port == end_port)
  477. {
  478. range_value = (0xFF >> (PCAL6408A_INNER_PIN_COUNT - pin_range_end - 1)) &
  479. (0xFF << pin_range_start);
  480. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  481. start_port,
  482. range_value,
  483. PCAL6408A_PORT_CLEAR);
  484. if (err_code != NRF_SUCCESS)
  485. {
  486. goto end;
  487. }
  488. }
  489. else
  490. {
  491. range_value = 0xFF << pin_range_start;
  492. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  493. start_port,
  494. range_value,
  495. PCAL6408A_PORT_CLEAR);
  496. if (err_code != NRF_SUCCESS)
  497. {
  498. goto end;
  499. }
  500. range_value = 0xFF >> (PCAL6408A_INNER_PIN_COUNT - pin_range_end - 1);
  501. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  502. start_port,
  503. range_value,
  504. PCAL6408A_PORT_CLEAR);
  505. if (err_code != NRF_SUCCESS)
  506. {
  507. goto end;
  508. }
  509. }
  510. range_value = 0xFF;
  511. for (uint8_t i = (start_port + 1); i < end_port; i++)
  512. {
  513. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  514. i,
  515. range_value,
  516. PCAL6408A_PORT_CLEAR);
  517. if (err_code != NRF_SUCCESS)
  518. {
  519. goto end;
  520. }
  521. }
  522. end:
  523. return err_code;
  524. }
  525. static ret_code_t pcal6408a_port_pull_cfg_set(uint32_t port,
  526. uint8_t mask,
  527. pcal6408a_pin_pull_t pull_config)
  528. {
  529. ret_code_t err_code = NRF_SUCCESS;
  530. switch (pull_config)
  531. {
  532. case PCAL6408A_PIN_NOPULL:
  533. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_ENABLE,
  534. port,
  535. mask,
  536. PCAL6408A_PORT_CLEAR);
  537. break;
  538. case PCAL6408A_PIN_PULLDOWN:
  539. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_ENABLE,
  540. port,
  541. mask,
  542. PCAL6408A_PORT_SET);
  543. if (err_code != NRF_SUCCESS)
  544. {
  545. goto end;
  546. }
  547. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_SELECT,
  548. port,
  549. mask,
  550. PCAL6408A_PORT_CLEAR);
  551. break;
  552. case PCAL6408A_PIN_PULLUP:
  553. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_ENABLE,
  554. port,
  555. mask,
  556. PCAL6408A_PORT_SET);
  557. if (err_code != NRF_SUCCESS)
  558. {
  559. goto end;
  560. }
  561. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_PULL_UP_DOWN_SELECT,
  562. port,
  563. mask,
  564. PCAL6408A_PORT_SET);
  565. break;
  566. default:
  567. break;
  568. }
  569. end:
  570. return err_code;
  571. }
  572. ret_code_t pcal6408a_range_cfg_input(uint32_t pin_range_start,
  573. uint32_t pin_range_end,
  574. pcal6408a_pin_pull_t pull_config)
  575. {
  576. if (pin_range_start > pin_range_end)
  577. {
  578. return NRF_ERROR_INVALID_LENGTH;
  579. }
  580. uint8_t start_port = pin_range_start / PCAL6408A_INNER_PIN_COUNT;
  581. uint8_t end_port = pin_range_end / PCAL6408A_INNER_PIN_COUNT;
  582. uint8_t range_value;
  583. ret_code_t err_code = NRF_SUCCESS;
  584. if (start_port == end_port)
  585. {
  586. range_value = (0xFF >> (PCAL6408A_INNER_PIN_COUNT - pin_range_end - 1))
  587. & (0xFF << pin_range_start);
  588. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  589. start_port,
  590. range_value,
  591. PCAL6408A_PORT_SET);
  592. if (err_code != NRF_SUCCESS)
  593. {
  594. goto end;
  595. }
  596. err_code = pcal6408a_port_pull_cfg_set(start_port, range_value, pull_config);
  597. if (err_code != NRF_SUCCESS)
  598. {
  599. goto end;
  600. }
  601. }
  602. else
  603. {
  604. range_value = 0xFF << pin_range_start;
  605. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  606. start_port,
  607. range_value,
  608. PCAL6408A_PORT_SET);
  609. if (err_code != NRF_SUCCESS)
  610. {
  611. goto end;
  612. }
  613. err_code = pcal6408a_port_pull_cfg_set(start_port, range_value, pull_config);
  614. if (err_code != NRF_SUCCESS)
  615. {
  616. goto end;
  617. }
  618. range_value = 0xFF >> (PCAL6408A_INNER_PIN_COUNT - pin_range_end - 1);
  619. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  620. end_port,
  621. range_value,
  622. PCAL6408A_PORT_SET);
  623. if (err_code != NRF_SUCCESS)
  624. {
  625. goto end;
  626. }
  627. err_code = pcal6408a_port_pull_cfg_set(end_port, range_value, pull_config);
  628. if (err_code != NRF_SUCCESS)
  629. {
  630. goto end;
  631. }
  632. }
  633. range_value = 0xFF;
  634. for (uint8_t i = (start_port + 1); i < end_port; i++)
  635. {
  636. err_code = pcal6408a_port_cfg_reg_set(PCAL6408A_REG_CONFIGURATION,
  637. i,
  638. range_value,
  639. PCAL6408A_PORT_SET);
  640. if (err_code != NRF_SUCCESS)
  641. {
  642. goto end;
  643. }
  644. err_code = pcal6408a_port_pull_cfg_set(i, range_value, pull_config);
  645. if (err_code != NRF_SUCCESS)
  646. {
  647. goto end;
  648. }
  649. }
  650. end:
  651. return err_code;
  652. }