ds1624.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright (c) 2012 - 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 "ds1624.h"
  41. #include "twi_master.h"
  42. #include "nrf_delay.h"
  43. /*lint ++flb "Enter library region" */
  44. #define DS1634_BASE_ADDRESS 0x90 //!< 4 MSBs of the DS1624 TWI address
  45. #define DS1624_ONESHOT_MODE 0x01 //!< Bit in configuration register for 1-shot mode
  46. #define DS1624_CONVERSION_DONE 0x80 //!< Bit in configuration register to indicate completed temperature conversion
  47. static uint8_t m_device_address; //!< Device address in bits [7:1]
  48. const uint8_t command_access_memory = 0x17; //!< Reads or writes to 256-byte EEPROM memory
  49. const uint8_t command_access_config = 0xAC; //!< Reads or writes configuration data to configuration register
  50. const uint8_t command_read_temp = 0xAA; //!< Reads last converted temperature value from temperature register
  51. const uint8_t command_start_convert_temp = 0xEE; //!< Initiates temperature conversion.
  52. const uint8_t command_stop_convert_temp = 0x22; //!< Halts temperature conversion.
  53. /**
  54. * @brief Function for reading the current configuration of the sensor.
  55. *
  56. * @return uint8_t Zero if communication with the sensor failed. Contents (always non-zero) of configuration register (@ref DS1624_ONESHOT_MODE and @ref DS1624_CONVERSION_DONE) if communication succeeded.
  57. */
  58. static uint8_t ds1624_config_read(void)
  59. {
  60. uint8_t config = 0;
  61. // Write: command protocol
  62. if (twi_master_transfer(m_device_address, (uint8_t*)&command_access_config, 1, TWI_DONT_ISSUE_STOP))
  63. {
  64. if (twi_master_transfer(m_device_address | TWI_READ_BIT, &config, 1, TWI_ISSUE_STOP)) // Read: current configuration
  65. {
  66. // Read succeeded, configuration stored to variable "config"
  67. }
  68. else
  69. {
  70. // Read failed
  71. config = 0;
  72. }
  73. }
  74. return config;
  75. }
  76. bool ds1624_init(uint8_t device_address)
  77. {
  78. bool transfer_succeeded = true;
  79. m_device_address = DS1634_BASE_ADDRESS + (uint8_t)(device_address << 1);
  80. uint8_t config = ds1624_config_read();
  81. if (config != 0)
  82. {
  83. // Configure DS1624 for 1SHOT mode if not done so already.
  84. if (!(config & DS1624_ONESHOT_MODE))
  85. {
  86. uint8_t data_buffer[2];
  87. data_buffer[0] = command_access_config;
  88. data_buffer[1] = DS1624_ONESHOT_MODE;
  89. transfer_succeeded &= twi_master_transfer(m_device_address, data_buffer, 2, TWI_ISSUE_STOP);
  90. }
  91. }
  92. else
  93. {
  94. transfer_succeeded = false;
  95. }
  96. return transfer_succeeded;
  97. }
  98. bool ds1624_start_temp_conversion(void)
  99. {
  100. return twi_master_transfer(m_device_address, (uint8_t*)&command_start_convert_temp, 1, TWI_ISSUE_STOP);
  101. }
  102. bool ds1624_is_temp_conversion_done(void)
  103. {
  104. uint8_t config = ds1624_config_read();
  105. if (config & DS1624_CONVERSION_DONE)
  106. {
  107. return true;
  108. }
  109. else
  110. {
  111. return false;
  112. }
  113. }
  114. bool ds1624_temp_read(int8_t * temperature_in_celcius, int8_t * temperature_fraction)
  115. {
  116. bool transfer_succeeded = false;
  117. // Write: Begin read temperature command
  118. if (twi_master_transfer(m_device_address, (uint8_t*)&command_read_temp, 1, TWI_DONT_ISSUE_STOP))
  119. {
  120. uint8_t data_buffer[2];
  121. // Read: 2 temperature bytes to data_buffer
  122. if (twi_master_transfer(m_device_address | TWI_READ_BIT, data_buffer, 2, TWI_ISSUE_STOP))
  123. {
  124. *temperature_in_celcius = (int8_t)data_buffer[0];
  125. *temperature_fraction = (int8_t)data_buffer[1];
  126. transfer_succeeded = true;
  127. }
  128. }
  129. return transfer_succeeded;
  130. }
  131. /*lint --flb "Leave library region" */