MAX30102.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /** \file max30102.cpp ******************************************************
  2. *
  3. * Project: MAXREFDES117#
  4. * Filename: max30102.cpp
  5. * Description: This module is an embedded controller driver for the MAX30102
  6. *
  7. *
  8. * --------------------------------------------------------------------
  9. *
  10. * This code follows the following naming conventions:
  11. *
  12. * char ch_pmod_value
  13. * char (array) s_pmod_s_string[16]
  14. * float f_pmod_value
  15. * int32_t n_pmod_value
  16. * int32_t (array) an_pmod_value[16]
  17. * int16_t w_pmod_value
  18. * int16_t (array) aw_pmod_value[16]
  19. * uint16_t uw_pmod_value
  20. * uint16_t (array) auw_pmod_value[16]
  21. * uint8_t uch_pmod_value
  22. * uint8_t (array) auch_pmod_buffer[16]
  23. * uint32_t un_pmod_value
  24. * int32_t * pn_pmod_value
  25. *
  26. * ------------------------------------------------------------------------- */
  27. /*******************************************************************************
  28. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  29. *
  30. * Permission is hereby granted, free of charge, to any person obtaining a
  31. * copy of this software and associated documentation files (the "Software"),
  32. * to deal in the Software without restriction, including without limitation
  33. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  34. * and/or sell copies of the Software, and to permit persons to whom the
  35. * Software is furnished to do so, subject to the following conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be included
  38. * in all copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  41. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  42. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  43. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  44. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  45. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  46. * OTHER DEALINGS IN THE SOFTWARE.
  47. *
  48. * Except as contained in this notice, the name of Maxim Integrated
  49. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  50. * Products, Inc. Branding Policy.
  51. *
  52. * The mere transfer of this software does not imply any licenses
  53. * of trade secrets, proprietary technology, copyrights, patents,
  54. * trademarks, maskwork rights, or any other form of intellectual
  55. * property whatsoever. Maxim Integrated Products, Inc. retains all
  56. * ownership rights.
  57. *******************************************************************************
  58. */
  59. #include "luat_i2c.h"
  60. #include "MAX30102.h"
  61. extern uint8_t max30102_i2c_id;
  62. bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
  63. /**
  64. * \brief Write a value to a MAX30102 register
  65. * \par Details
  66. * This function writes a value to a MAX30102 register
  67. *
  68. * \param[in] uch_addr - register address
  69. * \param[in] uch_data - register data
  70. *
  71. * \retval true on success
  72. */
  73. {
  74. char ach_i2c_data[2];
  75. ach_i2c_data[0]=uch_addr;
  76. ach_i2c_data[1]=uch_data;
  77. if(luat_i2c_send(max30102_i2c_id, MAX30102_I2C_ADDR, ach_i2c_data, 2,1)==0)
  78. return true;
  79. else
  80. return false;
  81. }
  82. bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
  83. /**
  84. * \brief Read a MAX30102 register
  85. * \par Details
  86. * This function reads a MAX30102 register
  87. *
  88. * \param[in] uch_addr - register address
  89. * \param[out] puch_data - pointer that stores the register data
  90. *
  91. * \retval true on success
  92. */
  93. {
  94. if(luat_i2c_transfer(max30102_i2c_id, MAX30102_I2C_ADDR, &uch_addr, 1, puch_data, 1)==0){
  95. return true;
  96. } else
  97. return false;
  98. }
  99. bool maxim_max30102_init(void)
  100. /**
  101. * \brief Initialize the MAX30102
  102. * \par Details
  103. * This function initializes the MAX30102
  104. *
  105. * \param None
  106. *
  107. * \retval true on success
  108. */
  109. {
  110. if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting
  111. return false;
  112. if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00))
  113. return false;
  114. if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)) //FIFO_WR_PTR[4:0]
  115. return false;
  116. if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)) //OVF_COUNTER[4:0]
  117. return false;
  118. if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)) //FIFO_RD_PTR[4:0]
  119. return false;
  120. if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f)) //sample avg = 1, fifo rollover=false, fifo almost full = 17
  121. return false;
  122. if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
  123. return false;
  124. if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
  125. return false;
  126. if(!maxim_max30102_write_reg(REG_LED1_PA,0x24)) //Choose value for ~ 7mA for LED1
  127. return false;
  128. if(!maxim_max30102_write_reg(REG_LED2_PA,0x24)) // Choose value for ~ 7mA for LED2
  129. return false;
  130. if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f)) // Choose value for ~ 25mA for Pilot LED
  131. return false;
  132. return true;
  133. }
  134. bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
  135. /**
  136. * \brief Read a set of samples from the MAX30102 FIFO register
  137. * \par Details
  138. * This function reads a set of samples from the MAX30102 FIFO register
  139. *
  140. * \param[out] *pun_red_led - pointer that stores the red LED reading data
  141. * \param[out] *pun_ir_led - pointer that stores the IR LED reading data
  142. *
  143. * \retval true on success
  144. */
  145. {
  146. uint32_t un_temp;
  147. unsigned char uch_temp;
  148. *pun_red_led=0;
  149. *pun_ir_led=0;
  150. char ach_i2c_data[6];
  151. //read and clear status register
  152. maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
  153. maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
  154. ach_i2c_data[0]=REG_FIFO_DATA;
  155. if(luat_i2c_send(max30102_i2c_id, MAX30102_I2C_ADDR, ach_i2c_data, 1,1)!=0)
  156. return false;
  157. if(luat_i2c_recv(max30102_i2c_id, MAX30102_I2C_ADDR, ach_i2c_data, 6)!=0)
  158. {
  159. return false;
  160. }
  161. un_temp=(unsigned char) ach_i2c_data[0];
  162. un_temp<<=16;
  163. *pun_red_led+=un_temp;
  164. un_temp=(unsigned char) ach_i2c_data[1];
  165. un_temp<<=8;
  166. *pun_red_led+=un_temp;
  167. un_temp=(unsigned char) ach_i2c_data[2];
  168. *pun_red_led+=un_temp;
  169. un_temp=(unsigned char) ach_i2c_data[3];
  170. un_temp<<=16;
  171. *pun_ir_led+=un_temp;
  172. un_temp=(unsigned char) ach_i2c_data[4];
  173. un_temp<<=8;
  174. *pun_ir_led+=un_temp;
  175. un_temp=(unsigned char) ach_i2c_data[5];
  176. *pun_ir_led+=un_temp;
  177. *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
  178. *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]
  179. return true;
  180. }
  181. bool maxim_max30102_reset(void)
  182. /**
  183. * \brief Reset the MAX30102
  184. * \par Details
  185. * This function resets the MAX30102
  186. *
  187. * \param None
  188. *
  189. * \retval true on success
  190. */
  191. {
  192. if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x40))
  193. return false;
  194. else
  195. return true;
  196. }
  197. bool maxim_max30102_shutdown(void)
  198. {
  199. if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x80))
  200. return false;
  201. else
  202. return true;
  203. }