tsl2561.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**************************************************************************/
  2. /*!
  3. @file tsl2561.c
  4. @author K. Townsend (microBuilder.eu)/ Adapted for nodeMCU by Michael Lucas (Aeprox @github)
  5. @brief Drivers for the TAOS TSL2561 I2C digital luminosity sensor
  6. @section DESCRIPTION
  7. The TSL2561 is a 16-bit digital luminosity sensor the approximates
  8. the human eye's response to light. It contains one broadband
  9. photodiode that measures visible plus infrared light (channel 0)
  10. and one infrared photodiode (channel 1).
  11. @section EXAMPLE
  12. @code
  13. #include "drivers/sensors/tsl2561/tsl2561.h"
  14. ...
  15. uint16_t broadband, ir;
  16. uint32_t lux;
  17. // Initialise luminosity sensor
  18. tsl2561Init(sda_pin, scl_pin);
  19. // Optional ... default setting is 400ms with no gain
  20. // Set timing to 101ms with no gain
  21. tsl2561SetTiming(TSL2561_INTEGRATIONTIME_101MS, TSL2561_GAIN_0X);
  22. // Check luminosity level and calculate lux
  23. tsl2561GetLuminosity(&broadband, &ir);
  24. lux = tsl2561CalculateLux(broadband, ir);
  25. printf("Broadband: %u, IR: %u, Lux: %d %s", broadband, ir, lux, CFG_PRINTF_NEWLINE);
  26. @endcode
  27. @section LICENSE
  28. Software License Agreement (BSD License)
  29. Copyright (c) 2010, microBuilder SARL
  30. All rights reserved.
  31. Redistribution and use in source and binary forms, with or without
  32. modification, are permitted provided that the following conditions are met:
  33. 1. Redistributions of source code must retain the above copyright
  34. notice, this list of conditions and the following disclaimer.
  35. 2. Redistributions in binary form must reproduce the above copyright
  36. notice, this list of conditions and the following disclaimer in the
  37. documentation and/or other materials provided with the distribution.
  38. 3. Neither the name of the copyright holders nor the
  39. names of its contributors may be used to endorse or promote products
  40. derived from this software without specific prior written permission.
  41. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  42. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  43. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  45. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. */
  52. /**************************************************************************/
  53. #include "tsl2561.h"
  54. #include "platform.h"
  55. #include "user_interface.h"
  56. #include "osapi.h"
  57. static const uint32_t tsl2561_i2c_id = 0;
  58. static bool _tsl2561Initialised = 0;
  59. static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS;
  60. static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X;
  61. static tsl2561Address_t tsl2561Address = TSL2561_ADDRESS_FLOAT;
  62. static tsl2561Package_t tsl2561Package = TSL2561_PACKAGE_T_FN_CL;
  63. static void delay_ms(uint16_t ms)
  64. {
  65. while (ms--)
  66. os_delay_us(1000);
  67. }
  68. /**************************************************************************/
  69. /*!
  70. @brief Writes an 8 bit values over I2C
  71. */
  72. /**************************************************************************/
  73. tsl2561Error_t tsl2561Write8(uint8_t reg, uint8_t value) {
  74. platform_i2c_send_start(tsl2561_i2c_id);
  75. platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  76. platform_i2c_send_byte(tsl2561_i2c_id, reg);
  77. platform_i2c_send_byte(tsl2561_i2c_id, value);
  78. platform_i2c_send_stop(tsl2561_i2c_id);
  79. return TSL2561_ERROR_OK;
  80. }
  81. /**************************************************************************/
  82. /*!
  83. @brief Reads a 16 bit values over I2C
  84. */
  85. /**************************************************************************/
  86. tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) {
  87. platform_i2c_send_start(tsl2561_i2c_id);
  88. platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  89. platform_i2c_send_byte(tsl2561_i2c_id, reg);
  90. platform_i2c_send_stop(tsl2561_i2c_id);
  91. platform_i2c_send_start(tsl2561_i2c_id);
  92. platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_RECEIVER);
  93. uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0);
  94. platform_i2c_send_stop(tsl2561_i2c_id);
  95. platform_i2c_send_start(tsl2561_i2c_id);
  96. platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  97. platform_i2c_send_byte(tsl2561_i2c_id, reg + 1);
  98. platform_i2c_send_stop(tsl2561_i2c_id);
  99. platform_i2c_send_start(tsl2561_i2c_id);
  100. platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_RECEIVER);
  101. uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0);
  102. platform_i2c_send_stop(tsl2561_i2c_id);
  103. // Shift values to create properly formed integer (low byte first)
  104. *value = (ch_low | (ch_high << 8));
  105. return TSL2561_ERROR_OK;
  106. }
  107. /**************************************************************************/
  108. /*!
  109. @brief Enables the device
  110. */
  111. /**************************************************************************/
  112. tsl2561Error_t tsl2561Enable(void) {
  113. if (!_tsl2561Initialised)
  114. return TSL2561_ERROR_NOINIT;
  115. // Enable the device by setting the control bit to 0x03
  116. return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL,
  117. TSL2561_CONTROL_POWERON);
  118. }
  119. /**************************************************************************/
  120. /*!
  121. @brief Disables the device (putting it in lower power sleep mode)
  122. */
  123. /**************************************************************************/
  124. tsl2561Error_t tsl2561Disable(void) {
  125. if (!_tsl2561Initialised)
  126. return TSL2561_ERROR_NOINIT;
  127. // Turn the device off to save power
  128. return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL,
  129. TSL2561_CONTROL_POWEROFF);
  130. }
  131. void tsl2561SetAddress(uint8_t address){
  132. tsl2561Address = address;
  133. return;
  134. }
  135. void tsl2561SetPackage(uint8_t package){
  136. tsl2561Package = package;
  137. return;
  138. }
  139. /**************************************************************************/
  140. /*!
  141. @brief Initialises the I2C block
  142. */
  143. /**************************************************************************/
  144. tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl) {
  145. // Initialise I2C
  146. platform_i2c_setup(tsl2561_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
  147. _tsl2561Initialised = 1;
  148. // Set default integration time and gain
  149. tsl2561SetTiming(_tsl2561IntegrationTime, _tsl2561Gain);
  150. // Note: by default, the device is in power down mode on bootup
  151. return TSL2561_ERROR_OK;
  152. }
  153. /**************************************************************************/
  154. /*!
  155. @brief Sets the integration time and gain (controls sensitivity)
  156. */
  157. /**************************************************************************/
  158. tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain) {
  159. if (!_tsl2561Initialised)
  160. return TSL2561_ERROR_NOINIT;
  161. tsl2561Error_t error = TSL2561_ERROR_OK;
  162. // Enable the device by setting the control bit to 0x03
  163. error = tsl2561Enable();
  164. if (error)
  165. return error;
  166. // set timing and gain on device
  167. error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain);
  168. if (error)
  169. return error;
  170. // Update value placeholders
  171. _tsl2561IntegrationTime = integration;
  172. _tsl2561Gain = gain;
  173. // Turn the device off to save power
  174. error = tsl2561Disable();
  175. if (error)
  176. return error;
  177. return error;
  178. }
  179. /**************************************************************************/
  180. /*!
  181. @brief Reads the luminosity on both channels from the TSL2561
  182. */
  183. /**************************************************************************/
  184. tsl2561Error_t tsl2561GetLuminosity(uint16_t *broadband, uint16_t *ir) {
  185. if (!_tsl2561Initialised)
  186. return TSL2561_ERROR_NOINIT;
  187. tsl2561Error_t error = TSL2561_ERROR_OK;
  188. // Enable the device by setting the control bit to 0x03
  189. error = tsl2561Enable();
  190. if (error)
  191. return error;
  192. // Wait x ms for ADC to complete
  193. switch (_tsl2561IntegrationTime) {
  194. case TSL2561_INTEGRATIONTIME_13MS:
  195. delay_ms(14); //systickDelay(14);
  196. break;
  197. case TSL2561_INTEGRATIONTIME_101MS:
  198. delay_ms(102); //systickDelay(102);
  199. break;
  200. default:
  201. delay_ms(404); //systickDelay(404);
  202. break;
  203. }
  204. // Reads two byte value from channel 0 (visible + infrared)
  205. error = tsl2561Read16(
  206. TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband);
  207. if (error)
  208. return error;
  209. // Reads two byte value from channel 1 (infrared)
  210. error = tsl2561Read16(
  211. TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir);
  212. if (error)
  213. return error;
  214. // Turn the device off to save power
  215. error = tsl2561Disable();
  216. if (error)
  217. return error;
  218. return error;
  219. }
  220. /**************************************************************************/
  221. /*!
  222. @brief Calculates LUX from the supplied ch0 (broadband) and ch1
  223. (IR) readings
  224. */
  225. /**************************************************************************/
  226. uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) {
  227. unsigned long chScale;
  228. unsigned long channel1;
  229. unsigned long channel0;
  230. switch (_tsl2561IntegrationTime) {
  231. case TSL2561_INTEGRATIONTIME_13MS:
  232. chScale = TSL2561_LUX_CHSCALE_TINT0;
  233. break;
  234. case TSL2561_INTEGRATIONTIME_101MS:
  235. chScale = TSL2561_LUX_CHSCALE_TINT1;
  236. break;
  237. default: // No scaling ... integration time = 402ms
  238. chScale = (1 << TSL2561_LUX_CHSCALE);
  239. break;
  240. }
  241. // Scale for gain (1x or 16x)
  242. if (!_tsl2561Gain)
  243. chScale = chScale << 4;
  244. // scale the channel values
  245. channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE;
  246. channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE;
  247. // find the ratio of the channel values (Channel1/Channel0)
  248. unsigned long ratio1 = 0;
  249. if (channel0 != 0)
  250. ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE + 1)) / channel0;
  251. // round the ratio value
  252. unsigned long ratio = (ratio1 + 1) >> 1;
  253. unsigned int b, m;
  254. if (tsl2561Package == TSL2561_PACKAGE_CS){
  255. if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) {
  256. b = TSL2561_LUX_B1C;
  257. m = TSL2561_LUX_M1C;
  258. } else if (ratio <= TSL2561_LUX_K2C) {
  259. b = TSL2561_LUX_B2C;
  260. m = TSL2561_LUX_M2C;
  261. } else if (ratio <= TSL2561_LUX_K3C) {
  262. b = TSL2561_LUX_B3C;
  263. m = TSL2561_LUX_M3C;
  264. } else if (ratio <= TSL2561_LUX_K4C) {
  265. b = TSL2561_LUX_B4C;
  266. m = TSL2561_LUX_M4C;
  267. } else if (ratio <= TSL2561_LUX_K5C) {
  268. b = TSL2561_LUX_B5C;
  269. m = TSL2561_LUX_M5C;
  270. } else if (ratio <= TSL2561_LUX_K6C) {
  271. b = TSL2561_LUX_B6C;
  272. m = TSL2561_LUX_M6C;
  273. } else if (ratio <= TSL2561_LUX_K7C) {
  274. b = TSL2561_LUX_B7C;
  275. m = TSL2561_LUX_M7C;
  276. } else if (ratio > TSL2561_LUX_K8C) {
  277. b = TSL2561_LUX_B8C;
  278. m = TSL2561_LUX_M8C;
  279. }
  280. }
  281. else{
  282. if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T))
  283. { b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;}
  284. else if (ratio <= TSL2561_LUX_K2T)
  285. { b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;}
  286. else if (ratio <= TSL2561_LUX_K3T)
  287. { b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;}
  288. else if (ratio <= TSL2561_LUX_K4T)
  289. { b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;}
  290. else if (ratio <= TSL2561_LUX_K5T)
  291. { b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;}
  292. else if (ratio <= TSL2561_LUX_K6T)
  293. { b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;}
  294. else if (ratio <= TSL2561_LUX_K7T)
  295. { b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;}
  296. else if (ratio > TSL2561_LUX_K8T)
  297. { b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;}
  298. }
  299. unsigned long temp;
  300. temp = ((channel0 * b) - (channel1 * m));
  301. // do not allow negative lux value
  302. if (temp < 0)
  303. temp = 0;
  304. // round lsb (2^(LUX_SCALE-1))
  305. temp += (1 << (TSL2561_LUX_LUXSCALE - 1));
  306. // strip off fractional portion
  307. uint32_t lux = temp >> TSL2561_LUX_LUXSCALE;
  308. // Signal I2C had no errors
  309. return lux;
  310. }