sx1509b.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /**
  2. * Copyright (c) 2017 - 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 "sx1509b.h"
  41. static sx1509b_instance_t * m_p_instances;
  42. static uint8_t m_max_instance_count;
  43. static uint8_t m_inst_count;
  44. #define RETURN_IF_ERR(_err) \
  45. if (_err != NRF_SUCCESS)\
  46. { \
  47. return _err; \
  48. }
  49. /**
  50. * ===============================================================================================
  51. * @brief General expander utility functions.
  52. */
  53. void sx1509b_init(sx1509b_instance_t * p_instances, uint8_t count)
  54. {
  55. ASSERT(p_instances != NULL);
  56. m_p_instances = p_instances;
  57. m_max_instance_count = count;
  58. m_inst_count = 0;
  59. }
  60. static void sx1509b_default_cfg_set(uint8_t instance_num)
  61. {
  62. m_p_instances[instance_num].start_addr = 0x00;
  63. for (uint8_t i = SX1509B_REG_INPUT_DISABLE_B; i < SX1509B_REG_DIR_B; i++)
  64. {
  65. m_p_instances[instance_num].registers[i] = 0;
  66. }
  67. for (uint8_t i = SX1509B_REG_DIR_B; i < SX1509B_REG_SENSE_H_B; i++)
  68. {
  69. m_p_instances[instance_num].registers[i] = 0xFF;
  70. }
  71. for (uint8_t i = SX1509B_REG_SENSE_H_B; i < SX1509B_REG_KEY_DATA_1; i++)
  72. {
  73. m_p_instances[instance_num].registers[i] = 0;
  74. }
  75. m_p_instances[instance_num].registers[SX1509B_REG_KEY_DATA_1] = 0xFF;
  76. m_p_instances[instance_num].registers[SX1509B_REG_KEY_DATA_2] = 0xFF;
  77. m_p_instances[instance_num].registers[SX1509B_REG_MISC] = 0x01;
  78. m_p_instances[instance_num].high_input[0] = 0;
  79. m_p_instances[instance_num].high_input[1] = 0;
  80. }
  81. ret_code_t sx1509b_add_instance(nrf_twi_sensor_t * p_twi_sensor,
  82. uint8_t sensor_address)
  83. {
  84. ASSERT(p_twi_sensor != NULL);
  85. if (m_p_instances == NULL)
  86. {
  87. return NRF_ERROR_MODULE_NOT_INITIALIZED;
  88. }
  89. if (m_inst_count >= m_max_instance_count)
  90. {
  91. return NRF_ERROR_STORAGE_FULL;
  92. }
  93. m_p_instances[m_inst_count].p_sensor_data = p_twi_sensor;
  94. m_p_instances[m_inst_count].sensor_addr = sensor_address;
  95. sx1509b_default_cfg_set(m_inst_count);
  96. m_inst_count++;
  97. ret_code_t err_code = sx1509b_cfg_write(m_inst_count - 1);
  98. return err_code;
  99. }
  100. ret_code_t sx1509b_cfg_write(uint8_t instance_num)
  101. {
  102. if (instance_num >= m_inst_count)
  103. {
  104. return NRF_ERROR_INVALID_PARAM;
  105. }
  106. ret_code_t err = nrf_twi_sensor_reg_write(m_p_instances[instance_num].p_sensor_data,
  107. m_p_instances[instance_num].sensor_addr,
  108. SX1509B_REG_HIGH_INPUT_B,
  109. m_p_instances[instance_num].high_input,
  110. 2);
  111. RETURN_IF_ERR(err);
  112. return nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  113. m_p_instances[instance_num].sensor_addr,
  114. &m_p_instances[instance_num].start_addr,
  115. SX1509B_REG_COUNT + 1,
  116. false);
  117. }
  118. ret_code_t sx1509b_cfg_read(uint8_t instance_num)
  119. {
  120. if (instance_num >= m_inst_count)
  121. {
  122. return NRF_ERROR_INVALID_PARAM;
  123. }
  124. ret_code_t err = nrf_twi_sensor_reg_read(m_p_instances[instance_num].p_sensor_data,
  125. m_p_instances[instance_num].sensor_addr,
  126. SX1509B_REG_HIGH_INPUT_B,
  127. NULL,
  128. m_p_instances[instance_num].high_input,
  129. 2);
  130. RETURN_IF_ERR(err);
  131. return nrf_twi_sensor_reg_read(m_p_instances[instance_num].p_sensor_data,
  132. m_p_instances[instance_num].sensor_addr,
  133. m_p_instances[instance_num].start_addr,
  134. NULL,
  135. m_p_instances[instance_num].registers,
  136. SX1509B_REG_COUNT);
  137. }
  138. ret_code_t sx1509b_clock_set(uint8_t instance_num, sx1509b_clock_t source, bool oscio_set, uint8_t oscio_freq)
  139. {
  140. if (instance_num >= m_inst_count)
  141. {
  142. return NRF_ERROR_INVALID_PARAM;
  143. }
  144. uint8_t * p_reg_val = &m_p_instances[instance_num].registers[SX1509B_REG_CLOCK];
  145. NRF_TWI_SENSOR_REG_SET(*p_reg_val, SX1509B_OSC_SRC_MASK, SX1509B_OSC_SRC_POS, source);
  146. NRF_TWI_SENSOR_REG_SET(*p_reg_val, SX1509B_OSCIO_PIN_MASK, SX1509B_OSCIO_PIN_POS, oscio_set);
  147. NRF_TWI_SENSOR_REG_SET(*p_reg_val,
  148. SX1509B_OSCOUT_FREQ_MASK,
  149. SX1509B_OSCOUT_FREQ_POS,
  150. oscio_freq);
  151. uint8_t send_msg[] = {
  152. SX1509B_REG_CLOCK,
  153. *p_reg_val
  154. };
  155. return nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  156. m_p_instances[instance_num].sensor_addr,
  157. send_msg,
  158. ARRAY_SIZE(send_msg),
  159. true);
  160. }
  161. ret_code_t sx1509b_misc_set(uint8_t instance_num,
  162. bool nreset_func,
  163. sx1509b_debounce_t debounce_time,
  164. bool autoclear_nint)
  165. {
  166. if (instance_num >= m_inst_count)
  167. {
  168. return NRF_ERROR_INVALID_PARAM;
  169. }
  170. uint8_t * p_reg_val = &m_p_instances[instance_num].registers[SX1509B_REG_MISC];
  171. NRF_TWI_SENSOR_REG_SET(*p_reg_val, SX1509B_NRESET_PIN_MASK, SX1509B_NRESET_PIN_POS, nreset_func);
  172. NRF_TWI_SENSOR_REG_SET(*p_reg_val,
  173. SX1509B_AUTO_CLEAR_NINT_MASK,
  174. SX1509B_AUTO_CLEAR_NINT_POS,
  175. autoclear_nint);
  176. uint8_t send_msg[] = {
  177. SX1509B_REG_MISC,
  178. *p_reg_val
  179. };
  180. ret_code_t err = nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  181. m_p_instances[instance_num].sensor_addr,
  182. send_msg,
  183. ARRAY_SIZE(send_msg),
  184. true);
  185. RETURN_IF_ERR(err);
  186. m_p_instances[instance_num].registers[SX1509B_REG_DEBOUNCE_CONFIG] = debounce_time;
  187. send_msg[0] = SX1509B_REG_DEBOUNCE_CONFIG;
  188. send_msg[1] = debounce_time;
  189. return nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  190. m_p_instances[instance_num].sensor_addr,
  191. send_msg,
  192. ARRAY_SIZE(send_msg),
  193. true);
  194. }
  195. ret_code_t sx1509b_sw_reset(uint8_t instance_num)
  196. {
  197. if (instance_num >= m_inst_count)
  198. {
  199. return NRF_ERROR_INVALID_PARAM;
  200. }
  201. uint8_t send_msg[] = {
  202. SX1509B_REG_SW_RESET,
  203. SX1509B_INNER_RESET_BYTE1
  204. };
  205. ret_code_t err = nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  206. m_p_instances[instance_num].sensor_addr,
  207. send_msg,
  208. ARRAY_SIZE(send_msg),
  209. true);
  210. RETURN_IF_ERR(err);
  211. send_msg[1] = SX1509B_INNER_RESET_BYTE2;
  212. err = nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  213. m_p_instances[instance_num].sensor_addr,
  214. send_msg,
  215. ARRAY_SIZE(send_msg),
  216. true);
  217. RETURN_IF_ERR(err);
  218. sx1509b_default_cfg_set(instance_num);
  219. return err;
  220. }
  221. ret_code_t sx1509b_pin_cfg_reg_set(sx1509b_registers_t reg, uint32_t pin, uint8_t set)
  222. {
  223. if (pin >= SX1509B_INNER_PIN_COUNT * m_inst_count)
  224. {
  225. return NRF_ERROR_INVALID_PARAM;
  226. }
  227. uint8_t inst_num = pin / SX1509B_INNER_PIN_COUNT;
  228. pin %= SX1509B_INNER_PIN_COUNT;
  229. uint8_t * p_reg_val;
  230. uint8_t reg_addr = reg;
  231. uint32_t mask = 1;
  232. if (reg_addr == SX1509B_REG_LEVEL_SHIFTER_1)
  233. {
  234. mask = 3; // Level shifter register parameter is 2 bits long.
  235. pin %= SX1509B_INNER_NEXT_BANK;
  236. pin *= 2;
  237. }
  238. if (reg_addr == SX1509B_REG_SENSE_H_B)
  239. {
  240. reg_addr += 3 - (pin / SX1509B_INNER_SENSE_REG_NUM); // Setting correct sense register
  241. pin %= SX1509B_INNER_SENSE_REG_NUM;
  242. pin *= 2; // Multiplying by 2 to make space for 2 bits.
  243. mask = 3; // Sense register parameter is 2 bits long.
  244. }
  245. else
  246. {
  247. if (pin >= SX1509B_INNER_NEXT_BANK)
  248. {
  249. reg_addr = reg;
  250. pin -= SX1509B_INNER_NEXT_BANK;
  251. }
  252. else
  253. {
  254. reg_addr = reg + 1; // Moving to bank A registers
  255. }
  256. }
  257. p_reg_val = &m_p_instances[inst_num].registers[reg_addr];
  258. NRF_TWI_SENSOR_REG_SET(*p_reg_val, (mask<<pin), pin, set);
  259. uint8_t send_msg[] = {
  260. reg_addr,
  261. *p_reg_val
  262. };
  263. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  264. m_p_instances[inst_num].sensor_addr,
  265. send_msg,
  266. ARRAY_SIZE(send_msg),
  267. true);
  268. }
  269. uint8_t sx1509b_pin_cfg_reg_get(sx1509b_registers_t reg, uint32_t pin)
  270. {
  271. if (pin >= SX1509B_INNER_PIN_COUNT * m_inst_count)
  272. {
  273. return 0xFF;
  274. }
  275. uint8_t inst_num = pin / SX1509B_INNER_PIN_COUNT;
  276. pin %= SX1509B_INNER_PIN_COUNT;
  277. uint8_t * p_reg_val;
  278. uint8_t reg_addr = reg;
  279. uint8_t mask = 1;
  280. if (reg_addr == SX1509B_REG_LEVEL_SHIFTER_1)
  281. {
  282. mask = 3; // Level shifter register parameter is 2 bits long.
  283. pin %= SX1509B_INNER_NEXT_BANK;
  284. pin *= 2;
  285. }
  286. if (reg_addr >= SX1509B_REG_SENSE_H_B && reg_addr <= SX1509B_REG_SENSE_L_A)
  287. {
  288. reg_addr += 3 - (pin / SX1509B_INNER_SENSE_REG_NUM); // Setting correct sense register
  289. pin %= SX1509B_INNER_SENSE_REG_NUM;
  290. pin *= 2; // Multiplying by 2 to make space for 2 bits.
  291. mask = 3; // Sense register parameter is 2 bits long.
  292. }
  293. else
  294. {
  295. reg_addr += (pin >= SX1509B_INNER_NEXT_BANK) ? 0 : 1;
  296. pin %= SX1509B_INNER_NEXT_BANK;
  297. }
  298. p_reg_val = &m_p_instances[inst_num].registers[reg_addr];
  299. return NRF_TWI_SENSOR_REG_VAL_GET(*p_reg_val,(mask<<pin),pin);
  300. }
  301. ret_code_t sx1509b_port_cfg_reg_set(sx1509b_registers_t reg,
  302. uint32_t port,
  303. uint8_t mask,
  304. sx1509b_port_op_t flag)
  305. {
  306. if (port >= SX1509B_INNER_PORT_COUNT * m_inst_count)
  307. {
  308. return NRF_ERROR_INVALID_PARAM;
  309. }
  310. uint8_t inst_num = port / SX1509B_INNER_PORT_COUNT;
  311. port %= SX1509B_INNER_PORT_COUNT;
  312. uint8_t reg_addr = reg + !port;
  313. uint8_t * reg_val = &m_p_instances[inst_num].registers[reg_addr];
  314. switch (flag)
  315. {
  316. case SX1509B_PORT_WRITE:
  317. *reg_val = mask;
  318. break;
  319. case SX1509B_PORT_CLEAR:
  320. *reg_val &= ~mask;
  321. break;
  322. case SX1509B_PORT_SET:
  323. *reg_val |= mask;
  324. break;
  325. default:
  326. return NRF_ERROR_INVALID_PARAM;
  327. }
  328. uint8_t send_msg[] = {
  329. reg_addr,
  330. *reg_val
  331. };
  332. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data, m_p_instances[inst_num].sensor_addr, send_msg, ARRAY_SIZE(send_msg), true);
  333. }
  334. uint8_t sx1509b_port_cfg_reg_get(sx1509b_registers_t reg, uint32_t port)
  335. {
  336. if (port >= SX1509B_INNER_PORT_COUNT * m_inst_count)
  337. {
  338. return 0;
  339. }
  340. uint8_t inst_num = port / SX1509B_INNER_PORT_COUNT;
  341. port %= SX1509B_INNER_PORT_COUNT;
  342. uint8_t reg_addr = reg + !port;
  343. return m_p_instances[inst_num].registers[reg_addr];
  344. }
  345. ret_code_t sx1509b_pin_data_update(nrf_twi_sensor_reg_cb_t user_cb)
  346. {
  347. ret_code_t err_code;
  348. for (uint8_t i = 0; i < m_inst_count - 1; i++)
  349. {
  350. err_code = nrf_twi_sensor_reg_read(m_p_instances[i].p_sensor_data,
  351. m_p_instances[i].sensor_addr,
  352. SX1509B_REG_DATA_B,
  353. NULL,
  354. &m_p_instances[i].registers[SX1509B_REG_DATA_B],
  355. 2);
  356. RETURN_IF_ERR(err_code);
  357. }
  358. return nrf_twi_sensor_reg_read(m_p_instances[m_inst_count - 1].p_sensor_data,
  359. m_p_instances[m_inst_count - 1].sensor_addr,
  360. SX1509B_REG_DATA_B,
  361. user_cb,
  362. &m_p_instances[m_inst_count - 1].registers[SX1509B_REG_DATA_B],
  363. 2);
  364. }
  365. ret_code_t sx1509b_pin_latch_update(nrf_twi_sensor_reg_cb_t user_cb)
  366. {
  367. ret_code_t err_code;
  368. for (uint8_t i = 0; i < m_inst_count - 1; i++) // -1 so last read triggers callback
  369. {
  370. err_code = nrf_twi_sensor_reg_read(m_p_instances[i].p_sensor_data,
  371. m_p_instances[i].sensor_addr,
  372. SX1509B_REG_INT_SRC_B,
  373. NULL,
  374. &m_p_instances[i].registers[SX1509B_REG_INT_SRC_B],
  375. 2);
  376. RETURN_IF_ERR(err_code);
  377. }
  378. return nrf_twi_sensor_reg_read(m_p_instances[m_inst_count - 1].p_sensor_data,
  379. m_p_instances[m_inst_count - 1].sensor_addr,
  380. SX1509B_REG_INT_SRC_B,
  381. user_cb,
  382. &m_p_instances[m_inst_count - 1].registers[SX1509B_REG_INT_SRC_B],
  383. 2);
  384. }
  385. ret_code_t sx1509b_pin_high_input(uint32_t pin_number, bool set)
  386. {
  387. if (pin_number >= SX1509B_INNER_PIN_COUNT * m_inst_count)
  388. {
  389. return NRF_ERROR_INVALID_PARAM;
  390. }
  391. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  392. pin_number %= SX1509B_INNER_PIN_COUNT;
  393. uint8_t reg_addr;
  394. uint8_t * p_reg_val;
  395. if (pin_number < SX1509B_INNER_NEXT_BANK)
  396. {
  397. reg_addr = SX1509B_REG_HIGH_INPUT_A;
  398. p_reg_val = &m_p_instances[inst_num].high_input[1];
  399. }
  400. else
  401. {
  402. reg_addr = SX1509B_REG_HIGH_INPUT_B;
  403. p_reg_val = &m_p_instances[inst_num].high_input[0];
  404. pin_number -= SX1509B_INNER_NEXT_BANK;
  405. }
  406. NRF_TWI_SENSOR_REG_SET(*p_reg_val, (1U << pin_number), pin_number, set);
  407. uint8_t send_msg[] = {
  408. reg_addr,
  409. *p_reg_val
  410. };
  411. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  412. m_p_instances[inst_num].sensor_addr,
  413. send_msg,
  414. ARRAY_SIZE(send_msg),
  415. true);
  416. }
  417. ret_code_t sx1509b_port_high_input(uint8_t port_num, uint8_t out_mask, sx1509b_port_op_t flag)
  418. {
  419. if (port_num >= SX1509B_INNER_PORT_COUNT * m_inst_count)
  420. {
  421. return NRF_ERROR_INVALID_PARAM;
  422. }
  423. uint8_t inst_num = port_num / SX1509B_INNER_PORT_COUNT;
  424. port_num %= SX1509B_INNER_PORT_COUNT;
  425. uint8_t reg_addr = SX1509B_REG_HIGH_INPUT_B + !port_num;
  426. uint8_t * reg_val = &m_p_instances[inst_num].high_input[!port_num];
  427. switch (flag)
  428. {
  429. case SX1509B_PORT_WRITE:
  430. *reg_val = out_mask;
  431. break;
  432. case SX1509B_PORT_CLEAR:
  433. *reg_val &= ~out_mask;
  434. break;
  435. case SX1509B_PORT_SET:
  436. *reg_val |= out_mask;
  437. break;
  438. default:
  439. return NRF_ERROR_INVALID_PARAM;
  440. }
  441. uint8_t send_msg[] = {
  442. reg_addr,
  443. *reg_val
  444. };
  445. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  446. m_p_instances[inst_num].sensor_addr,
  447. send_msg,
  448. ARRAY_SIZE(send_msg),
  449. true);
  450. }
  451. /**
  452. * ===============================================================================================
  453. * @brief Functions compatible with nrf_gpio
  454. */
  455. ret_code_t sx1509b_pin_cfg_input(uint32_t pin_number, sx1509b_pin_pull_t pull_config)
  456. {
  457. ret_code_t err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_DIR_B, pin_number, SX1509B_PIN_DIR_INPUT);
  458. RETURN_IF_ERR(err_code);
  459. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_INPUT_DISABLE_B, pin_number, 0);
  460. RETURN_IF_ERR(err_code);
  461. switch (pull_config)
  462. {
  463. case SX1509B_PIN_NOPULL:
  464. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_PULL_DOWN_B, pin_number, 0);
  465. RETURN_IF_ERR(err_code);
  466. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_PULL_UP_B, pin_number, 0);
  467. break;
  468. case SX1509B_PIN_PULLDOWN:
  469. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_PULL_DOWN_B, pin_number, 1);
  470. RETURN_IF_ERR(err_code);
  471. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_PULL_UP_B, pin_number, 0);
  472. break;
  473. case SX1509B_PIN_PULLUP:
  474. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_PULL_DOWN_B, pin_number, 0);
  475. RETURN_IF_ERR(err_code);
  476. err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_PULL_UP_B, pin_number, 1);
  477. break;
  478. };
  479. return err_code;
  480. }
  481. ret_code_t sx1509b_pin_cfg_default(uint32_t pin_number)
  482. {
  483. if (pin_number >= SX1509B_INNER_PIN_COUNT * m_inst_count)
  484. {
  485. return NRF_ERROR_INVALID_PARAM;
  486. }
  487. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  488. pin_number %= SX1509B_INNER_PIN_COUNT;
  489. uint8_t reg = (pin_number >= SX1509B_INNER_NEXT_BANK) ? 0 : 1;
  490. pin_number %= SX1509B_INNER_NEXT_BANK;
  491. ret_code_t err_code = NRF_SUCCESS;
  492. for (uint8_t i = SX1509B_REG_INPUT_DISABLE_B + reg; i < SX1509B_REG_DIR_B; i += 2)
  493. {
  494. if (IS_SET(m_p_instances[inst_num].registers[i], pin_number) == 1)
  495. {
  496. CLR_BIT(m_p_instances[inst_num].registers[i], pin_number);
  497. err_code = nrf_twi_sensor_reg_write(m_p_instances[inst_num].p_sensor_data,
  498. m_p_instances[inst_num].sensor_addr,
  499. i,
  500. &m_p_instances[inst_num].registers[i],
  501. 1);
  502. }
  503. }
  504. for (uint8_t i = SX1509B_REG_DIR_B + reg; i < SX1509B_REG_SENSE_H_B; i += 2)
  505. {
  506. if (IS_SET(m_p_instances[inst_num].registers[i], pin_number) == 0)
  507. {
  508. SET_BIT(m_p_instances[inst_num].registers[i], pin_number);
  509. err_code = nrf_twi_sensor_reg_write(m_p_instances[inst_num].p_sensor_data,
  510. m_p_instances[inst_num].sensor_addr,
  511. i,
  512. &m_p_instances[inst_num].registers[i],
  513. 1);
  514. }
  515. }
  516. for (uint8_t i = SX1509B_REG_SENSE_H_B + reg; i < SX1509B_REG_KEY_DATA_1; i += 2)
  517. {
  518. if (IS_SET(m_p_instances[inst_num].registers[i], pin_number) == 1)
  519. {
  520. CLR_BIT(m_p_instances[inst_num].registers[i], pin_number);
  521. err_code = nrf_twi_sensor_reg_write(m_p_instances[inst_num].p_sensor_data,
  522. m_p_instances[inst_num].sensor_addr,
  523. i,
  524. &m_p_instances[inst_num].registers[i],
  525. 1);
  526. }
  527. }
  528. return err_code;
  529. }
  530. ret_code_t sx1509b_pin_cfg_sense_input(uint32_t pin_number,
  531. sx1509b_pin_pull_t pull_config,
  532. sx1509b_pin_sense_t sense_config)
  533. {
  534. ret_code_t err_code = sx1509b_pin_cfg_input(pin_number, pull_config);
  535. RETURN_IF_ERR(err_code);
  536. return sx1509b_pin_cfg_sense_set(pin_number, sense_config);
  537. }
  538. ret_code_t sx1509b_pin_cfg_sense_set(uint32_t pin_number, sx1509b_pin_sense_t sense_config)
  539. {
  540. ret_code_t err;
  541. if (sense_config == SX1509B_PIN_NOSENSE)
  542. {
  543. err = sx1509b_pin_cfg_reg_set(SX1509B_REG_INT_MASK_B, pin_number, 1);
  544. RETURN_IF_ERR(err);
  545. }
  546. else
  547. {
  548. err = sx1509b_pin_cfg_reg_set(SX1509B_REG_INT_MASK_B, pin_number, 0);
  549. RETURN_IF_ERR(err);
  550. }
  551. return sx1509b_pin_cfg_reg_set(SX1509B_REG_SENSE_H_B, pin_number, sense_config);
  552. }
  553. ret_code_t sx1509b_pin_dir_set(uint32_t pin_number, sx1509b_pin_dir_t direction)
  554. {
  555. if (direction == SX1509B_PIN_DIR_INPUT)
  556. {
  557. return sx1509b_pin_cfg_input(pin_number, SX1509B_PIN_NOPULL);
  558. }
  559. else
  560. {
  561. return sx1509b_pin_cfg_output(pin_number);
  562. }
  563. }
  564. ret_code_t sx1509b_ports_read(uint8_t start_port, uint32_t length, uint8_t * p_masks)
  565. {
  566. if (start_port + length > SX1509B_INNER_PORT_COUNT * m_inst_count)
  567. {
  568. return NRF_ERROR_INVALID_LENGTH;
  569. }
  570. for (uint8_t i = 0; i < length; i++)
  571. {
  572. p_masks[i] = sx1509b_port_in_read(start_port + i);
  573. }
  574. return NRF_SUCCESS;
  575. }
  576. ret_code_t sx1509b_latches_read(uint8_t start_port, uint32_t length, uint8_t * p_masks)
  577. {
  578. if (start_port + length > SX1509B_INNER_PORT_COUNT * m_inst_count)
  579. {
  580. return NRF_ERROR_INVALID_LENGTH;
  581. }
  582. for (uint8_t i = 0; i < length; i++)
  583. {
  584. p_masks[i] = sx1509b_port_cfg_reg_get(SX1509B_REG_INT_SRC_B, start_port + i);
  585. }
  586. return NRF_SUCCESS;
  587. }
  588. ret_code_t sx1509b_pin_latch_clear(uint32_t pin_number)
  589. {
  590. ret_code_t err_code = sx1509b_pin_cfg_reg_set(SX1509B_REG_INT_SRC_B, pin_number, 1);
  591. RETURN_IF_ERR(err_code);
  592. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  593. pin_number %= SX1509B_INNER_PIN_COUNT;
  594. uint8_t reg = SX1509B_REG_INT_SRC_B;
  595. reg += (pin_number >= SX1509B_INNER_NEXT_BANK) ? 0 : 1;
  596. pin_number %= SX1509B_INNER_NEXT_BANK;
  597. CLR_BIT(m_p_instances[inst_num].registers[reg], pin_number);
  598. return err_code;
  599. }
  600. /**
  601. * ===============================================================================================
  602. * @brief Led driver functions.
  603. */
  604. ret_code_t sx1509b_led_driver_enable(uint8_t instance_num, bool clock_internal, uint8_t frequency)
  605. {
  606. if (instance_num >= m_inst_count)
  607. {
  608. return NRF_ERROR_INVALID_PARAM;
  609. }
  610. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_CLOCK],
  611. SX1509B_OSC_SRC_MASK,
  612. SX1509B_OSC_SRC_POS,
  613. (clock_internal == 1) ? 2 : 1);
  614. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_MISC],
  615. SX1509B_LED_FREQ_MASK,
  616. SX1509B_LED_FREQ_POS,
  617. frequency);
  618. uint8_t send_msg[] = {
  619. SX1509B_REG_CLOCK,
  620. m_p_instances[instance_num].registers[SX1509B_REG_CLOCK],
  621. m_p_instances[instance_num].registers[SX1509B_REG_MISC]
  622. };
  623. return nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  624. m_p_instances[instance_num].sensor_addr,
  625. send_msg,
  626. ARRAY_SIZE(send_msg),
  627. true);
  628. }
  629. ret_code_t sx1509b_led_mode(uint8_t port_num, bool mode)
  630. {
  631. if (port_num >= SX1509B_INNER_PORT_COUNT * m_inst_count)
  632. {
  633. return NRF_ERROR_INVALID_PARAM;
  634. }
  635. uint8_t inst_num = port_num / SX1509B_INNER_PORT_COUNT;
  636. port_num %= SX1509B_INNER_PORT_COUNT;
  637. uint8_t *p_reg_val = &m_p_instances[inst_num].registers[SX1509B_REG_MISC];
  638. if (port_num == 1)
  639. {
  640. NRF_TWI_SENSOR_REG_SET(*p_reg_val, SX1509B_LED_MODE_B_MASK, SX1509B_LED_MODE_B_POS, mode);
  641. }
  642. else
  643. {
  644. NRF_TWI_SENSOR_REG_SET(*p_reg_val, SX1509B_LED_MODE_A_MASK, SX1509B_LED_MODE_A_POS, mode);
  645. }
  646. uint8_t send_msg[] = {
  647. SX1509B_REG_MISC,
  648. *p_reg_val
  649. };
  650. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  651. m_p_instances[inst_num].sensor_addr,
  652. send_msg,
  653. ARRAY_SIZE(send_msg),
  654. true);
  655. }
  656. uint8_t sx1509b_led_driver_get_reg(uint32_t pin_number)
  657. {
  658. uint8_t reg;
  659. bool fade_reg = false;
  660. if (pin_number >= SX1509B_INNER_NEXT_BANK)
  661. {
  662. pin_number %= SX1509B_INNER_NEXT_BANK;
  663. if (pin_number >= SX1509B_LED_DRIVER_TIME_REG_NUM)
  664. {
  665. reg = SX1509B_REG_LED_FADE_B_START;
  666. fade_reg = true;
  667. }
  668. else
  669. {
  670. reg = SX1509B_REG_LED_BANK_B_START;
  671. }
  672. }
  673. else
  674. {
  675. if (pin_number >= SX1509B_LED_DRIVER_TIME_REG_NUM)
  676. {
  677. reg = SX1509B_REG_LED_FADE_A_START;
  678. fade_reg = true;
  679. }
  680. else
  681. {
  682. reg = SX1509B_REG_LED_BANK_A_START;
  683. }
  684. }
  685. if (fade_reg == true)
  686. {
  687. pin_number %= SX1509B_LED_DRIVER_FADE_REG_NUM;
  688. reg += SX1509B_LED_DRIVER_FADE_REG_LEN * pin_number;
  689. }
  690. else
  691. {
  692. pin_number %= SX1509B_LED_DRIVER_TIME_REG_NUM;
  693. reg += SX1509B_LED_DRIVER_TIME_REG_LEN * pin_number;
  694. }
  695. return reg;
  696. }
  697. ret_code_t sx1509b_led_pin_time(uint32_t pin_number,
  698. uint8_t on_time,
  699. uint8_t on_intensity,
  700. uint8_t off_time,
  701. uint8_t off_intensity)
  702. {
  703. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  704. if (inst_num >= m_inst_count)
  705. {
  706. return NRF_ERROR_INVALID_PARAM;
  707. }
  708. pin_number %= SX1509B_INNER_PIN_COUNT;
  709. uint8_t reg = sx1509b_led_driver_get_reg(pin_number);
  710. uint8_t send_msg[] = {
  711. reg,
  712. on_time & 0x1F,
  713. on_intensity,
  714. (off_time << SX1509B_OFF_TIME_POS) | (off_intensity & SX1509B_OFF_INTENSITY_MASK)
  715. };
  716. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  717. m_p_instances[inst_num].sensor_addr,
  718. send_msg,
  719. ARRAY_SIZE(send_msg),
  720. true);
  721. }
  722. ret_code_t sx1509b_led_pin_fade(uint32_t pin_number, uint8_t fade_in, uint8_t fade_out)
  723. {
  724. if ((pin_number % SX1509B_INNER_NEXT_BANK) <= SX1509B_LED_DRIVER_TIME_REG_LEN)
  725. {
  726. return NRF_ERROR_INVALID_PARAM;
  727. }
  728. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  729. if (inst_num >= m_inst_count)
  730. {
  731. return NRF_ERROR_INVALID_PARAM;
  732. }
  733. pin_number %= SX1509B_INNER_PIN_COUNT;
  734. uint8_t reg = sx1509b_led_driver_get_reg(pin_number) + SX1509B_LED_DRIVER_T_RISE;
  735. uint8_t send_msg[] = {
  736. reg,
  737. fade_in & 0x1F,
  738. fade_out & 0x1F
  739. };
  740. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  741. m_p_instances[inst_num].sensor_addr,
  742. send_msg,
  743. ARRAY_SIZE(send_msg),
  744. true);
  745. }
  746. ret_code_t sx1509b_led_pin_enable(uint32_t pin_number)
  747. {
  748. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  749. if (inst_num >= m_inst_count)
  750. {
  751. return NRF_ERROR_INVALID_PARAM;
  752. }
  753. pin_number %= SX1509B_INNER_PIN_COUNT;
  754. uint8_t reg_add = (pin_number > SX1509B_INNER_NEXT_BANK) ? 0 : 1;
  755. pin_number %= SX1509B_INNER_NEXT_BANK;
  756. SET_BIT(m_p_instances[inst_num].registers[SX1509B_REG_INPUT_DISABLE_B + reg_add], pin_number);
  757. CLR_BIT(m_p_instances[inst_num].registers[SX1509B_REG_PULL_UP_B + reg_add], pin_number);
  758. SET_BIT(m_p_instances[inst_num].registers[SX1509B_REG_OPEN_DRAIN_B + reg_add], pin_number);
  759. CLR_BIT(m_p_instances[inst_num].registers[SX1509B_REG_DIR_B + reg_add], pin_number);
  760. CLR_BIT(m_p_instances[inst_num].registers[SX1509B_REG_DATA_B + reg_add], pin_number);
  761. SET_BIT(m_p_instances[inst_num].registers[SX1509B_REG_LED_DRV_ENABLE_B + reg_add], pin_number);
  762. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  763. m_p_instances[inst_num].sensor_addr,
  764. &m_p_instances[inst_num].start_addr,
  765. SX1509B_REG_DEBOUNCE_CONFIG + 1, // + 1 byte for address
  766. false);
  767. }
  768. ret_code_t sx1509b_led_pin_disable(uint32_t pin_number)
  769. {
  770. uint8_t inst_num = pin_number / SX1509B_INNER_PIN_COUNT;
  771. if (inst_num >= m_inst_count)
  772. {
  773. return NRF_ERROR_INVALID_PARAM;
  774. }
  775. pin_number %= SX1509B_INNER_PIN_COUNT;
  776. uint8_t reg_add = (pin_number > SX1509B_INNER_NEXT_BANK) ? 0 : 1;
  777. pin_number %= SX1509B_INNER_NEXT_BANK;
  778. CLR_BIT(m_p_instances[inst_num].registers[SX1509B_REG_INPUT_DISABLE_B + reg_add], pin_number);
  779. CLR_BIT(m_p_instances[inst_num].registers[SX1509B_REG_OPEN_DRAIN_B + reg_add], pin_number);
  780. SET_BIT(m_p_instances[inst_num].registers[SX1509B_REG_DIR_B + reg_add], pin_number);
  781. SET_BIT(m_p_instances[inst_num].registers[SX1509B_REG_DATA_B + reg_add], pin_number);
  782. CLR_BIT(m_p_instances[inst_num].registers[SX1509B_REG_LED_DRV_ENABLE_B + reg_add], pin_number);
  783. return nrf_twi_sensor_write(m_p_instances[inst_num].p_sensor_data,
  784. m_p_instances[inst_num].sensor_addr,
  785. &m_p_instances[inst_num].start_addr,
  786. SX1509B_REG_DEBOUNCE_CONFIG + 1, // + 1 byte for address
  787. false);
  788. }
  789. /**
  790. * ===============================================================================================
  791. * @brief Key Engine functions.
  792. */
  793. ret_code_t sx1509b_key_engine_enable(uint8_t instance_num,
  794. uint8_t rows,
  795. uint8_t columns,
  796. sx1509b_key_sleep_t sleep_time,
  797. sx1509b_key_scan_t scan_time,
  798. sx1509b_debounce_t debounce_time)
  799. {
  800. if (instance_num >= m_inst_count)
  801. {
  802. return NRF_ERROR_INVALID_PARAM;
  803. }
  804. if (rows < 2)
  805. {
  806. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_2],
  807. SX1509B_ROW_NUM_MASK,
  808. SX1509B_ROW_NUM_POS,
  809. 0);
  810. uint8_t send_msg[] = {
  811. SX1509B_REG_KEY_CONFIG_2,
  812. m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_2]
  813. };
  814. return nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  815. m_p_instances[instance_num].sensor_addr,
  816. send_msg,
  817. ARRAY_SIZE(send_msg),
  818. true);
  819. }
  820. uint8_t in_mask = 0, out_mask = 0;
  821. uint8_t in_port = 0 + instance_num * SX1509B_INNER_PORT_COUNT;
  822. uint8_t out_port = 1 + instance_num * SX1509B_INNER_PORT_COUNT;
  823. for (uint8_t i = 0; i < rows; i++)
  824. {
  825. in_mask <<= 1;
  826. in_mask |= 1;
  827. }
  828. for (uint8_t i = 0; i < columns; i++)
  829. {
  830. out_mask <<= 1;
  831. out_mask |= 1;
  832. }
  833. ret_code_t err = sx1509b_port_dir_output_set(in_port, in_mask);
  834. RETURN_IF_ERR(err);
  835. err = sx1509b_port_dir_input_set(out_port, out_mask);
  836. RETURN_IF_ERR(err);
  837. err = sx1509b_port_open_drain(out_port, out_mask, SX1509B_PORT_SET);
  838. RETURN_IF_ERR(err);
  839. err = sx1509b_port_pull_up(in_port, in_mask, SX1509B_PORT_SET);
  840. RETURN_IF_ERR(err);
  841. m_p_instances[instance_num].registers[SX1509B_REG_DEBOUNCE_CONFIG] = debounce_time;
  842. m_p_instances[instance_num].registers[SX1509B_REG_DEBOUNCE_EN_B] |= in_mask;
  843. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_1],
  844. SX1509B_SLEEP_TIME_MASK,
  845. SX1509B_SLEEP_TIME_POS,
  846. sleep_time);
  847. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_1],
  848. SX1509B_SCAN_TIME_MASK,
  849. SX1509B_SCAN_TIME_POS,
  850. scan_time);
  851. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_2],
  852. SX1509B_ROW_NUM_MASK,
  853. SX1509B_ROW_NUM_POS,
  854. rows - 1);
  855. NRF_TWI_SENSOR_REG_SET(m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_2],
  856. SX1509B_COL_NUM_MASK,
  857. SX1509B_COL_NUM_POS,
  858. columns - 1);
  859. uint8_t send_msg[] = {
  860. SX1509B_REG_DEBOUNCE_CONFIG,
  861. m_p_instances[instance_num].registers[SX1509B_REG_DEBOUNCE_CONFIG],
  862. m_p_instances[instance_num].registers[SX1509B_REG_DEBOUNCE_EN_B],
  863. m_p_instances[instance_num].registers[SX1509B_REG_DEBOUNCE_EN_A],
  864. m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_1],
  865. m_p_instances[instance_num].registers[SX1509B_REG_KEY_CONFIG_2]
  866. };
  867. return nrf_twi_sensor_write(m_p_instances[instance_num].p_sensor_data,
  868. m_p_instances[instance_num].sensor_addr,
  869. send_msg,
  870. ARRAY_SIZE(send_msg),
  871. true);
  872. }
  873. ret_code_t sx1509b_key_data_update(uint8_t instance_num, nrf_twi_sensor_reg_cb_t user_cb)
  874. {
  875. if (instance_num >= m_inst_count)
  876. {
  877. return NRF_ERROR_INVALID_PARAM;
  878. }
  879. return nrf_twi_sensor_reg_read(m_p_instances[instance_num].p_sensor_data,
  880. m_p_instances[instance_num].sensor_addr,
  881. SX1509B_REG_KEY_DATA_1,
  882. user_cb,
  883. &m_p_instances[instance_num].registers[SX1509B_REG_KEY_DATA_1],
  884. 2);
  885. }
  886. static uint8_t sx1509b_key_get_bit_pos(uint8_t reg)
  887. {
  888. uint8_t ret_val = 0xFF;
  889. for(uint8_t i = 0; i < 8; i++)
  890. {
  891. if (IS_SET(reg, 0) == 1)
  892. {
  893. ret_val = i;
  894. break;
  895. }
  896. reg >>= 1;
  897. }
  898. return ret_val;
  899. }
  900. uint8_t sx1509b_key_column_get(uint8_t instance_num)
  901. {
  902. if (instance_num >= m_inst_count)
  903. {
  904. return NRF_ERROR_INVALID_PARAM;
  905. }
  906. uint8_t reg_val = ~m_p_instances[instance_num].registers[SX1509B_REG_KEY_DATA_1];
  907. return sx1509b_key_get_bit_pos(reg_val);
  908. }
  909. uint8_t sx1509b_key_row_get(uint8_t instance_num)
  910. {
  911. if (instance_num >= m_inst_count)
  912. {
  913. return NRF_ERROR_INVALID_PARAM;
  914. }
  915. uint8_t reg_val = ~m_p_instances[instance_num].registers[SX1509B_REG_KEY_DATA_2];
  916. return sx1509b_key_get_bit_pos(reg_val);
  917. }