mcp4725.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2016 - 2019, 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 "mcp4725.h"
  41. #include "nrf_drv_twi.h"
  42. #include "nrf_delay.h"
  43. #include "boards.h"
  44. #include "app_util_platform.h"
  45. /*lint ++flb "Enter library region" */
  46. #define MCP4725_BASE_ADDRESS 0x60 //!< MCP4725 base address
  47. #define MCP4725_DAC_ADDRESS 0x40 //!< MCP4725 write-to-dac register
  48. #define MCP4725_EEPROM_ADDRESS 0x60 //!< MCP4725 write-to-eeprom register
  49. #define RDY_BIT_POS 0x07 //!< Position of RDY bit
  50. /* TWI instance. */
  51. static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID_USED);
  52. /* Twi transfer indicators. */
  53. volatile bool m_xfer_done = false;
  54. volatile bool m_read_done = false;
  55. /**
  56. * @brief TWI events handler.
  57. */
  58. static void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
  59. {
  60. switch (p_event->type)
  61. {
  62. case NRF_DRV_TWI_EVT_DONE:
  63. if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX)
  64. {
  65. m_xfer_done = true;
  66. }
  67. if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
  68. {
  69. m_read_done = true;
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. /**
  77. * @brief TWI initialization.
  78. *
  79. * @param[in] p_pins_config Pointer to structere holding pins numbers to be used by TWI.
  80. */
  81. static ret_code_t twi_init(mcp4725_pins_config_t const * p_pins_config)
  82. {
  83. ret_code_t err_code;
  84. const nrf_drv_twi_config_t twi_mcp4725_config = {
  85. .scl = p_pins_config->scl_pin,
  86. .sda = p_pins_config->sda_pin,
  87. .frequency = NRF_DRV_TWI_FREQ_100K,
  88. .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
  89. .clear_bus_init = false
  90. };
  91. err_code = nrf_drv_twi_init(&m_twi, &twi_mcp4725_config, twi_handler, NULL);
  92. if (err_code != NRF_SUCCESS)
  93. {
  94. return err_code;
  95. }
  96. nrf_drv_twi_enable(&m_twi);
  97. return NRF_SUCCESS;
  98. }
  99. ret_code_t mcp4725_setup(mcp4725_pins_config_t const * p_pins_config)
  100. {
  101. ret_code_t err_code = twi_init(p_pins_config);
  102. if (err_code != NRF_SUCCESS)
  103. {
  104. return err_code;
  105. }
  106. return NRF_SUCCESS;
  107. }
  108. ret_code_t mcp4725_set_voltage(uint16_t val, bool write_eeprom)
  109. {
  110. /* Shift parameter val to get 2 8-bits values. */
  111. uint8_t reg[3] = {write_eeprom ? MCP4725_EEPROM_ADDRESS : MCP4725_DAC_ADDRESS,
  112. (val>>4), (val<<4)};
  113. m_xfer_done = false;
  114. ret_code_t err_code = nrf_drv_twi_tx(&m_twi, MCP4725_BASE_ADDRESS, reg, sizeof(reg), false);
  115. if (err_code != NRF_SUCCESS)
  116. {
  117. return err_code;
  118. }
  119. while (m_xfer_done == false);
  120. return NRF_SUCCESS;
  121. }
  122. bool mcp4725_is_busy(void)
  123. {
  124. uint8_t busy;
  125. m_read_done = false;
  126. ret_code_t err_code = nrf_drv_twi_rx(&m_twi, MCP4725_BASE_ADDRESS, &busy, sizeof(busy));
  127. if (err_code != NRF_SUCCESS)
  128. {
  129. return err_code;
  130. }
  131. while (m_read_done == false);
  132. return (bool)(!(busy >> RDY_BIT_POS));
  133. }
  134. /*lint --flb "Leave library region" */