summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/BSP/MESHBEAN/src/tsl2550.c
blob: 9011c6fd398d8addbe2a6d2c326188f606205ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**************************************************************************//**
\file  tsl2550.c

\brief Implementation of access to tsl2550 the sensor, light sensor.

\author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

\internal
  History:
    29/06/07 E. Ivanov - Created
******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <tsl2550.h>
#include <i2cPacket.h>
#include <bspTaskManager.h>
#include <appTimer.h>
#include <bspDbg.h>

/******************************************************************************
                   Definitions section
******************************************************************************/
// device address on i2c bus
#define TSL_DEVICE_ADDRESS 0x39

// device registers internal address
#define TSL_READ_CHANNEL0_COMMAND 0x43
#define TSL_READ_CHANNEL1_COMMAND 0x83
#define TSL_POWERDOWN_COMMAND      0x00
#define TSL_POWERUP_COMMAND        0x03
#define TSL_EXTENDED_RANGE_COMMAND 0x1D
#define TSL_STANDARD_RANGE_COMMAND 0x18

// standard mode max
#define TSL_MAX_LUX_VALUE 1846

// Conversion time (400ms typ by datasheet). Choose a little more to be on a safe side.
#define TSL_CONVERSION_TIME 500

/******************************************************************************
                   Types section
******************************************************************************/
// states
typedef enum
{
  OFF,  
  IDLE,
  POWERUP,
  POWERUP_ERR,
  DATA,  
  DATA_ERR,
} Tsl2550States_t;

typedef struct
{  
  Tsl2550States_t state;
  uint8_t ch0;
  uint8_t ch1;
  void(* callback)(bool result, int16_t data); // data callback pointer
} Tsl2550Control_t;

/******************************************************************************
                   Constants section
******************************************************************************/
PROGMEM_DECLARE(const uint8_t tsl2550Ratio[129]) =
{
100,100,100,100,100,100,100,100,
100,100,100,100,100,100, 99, 99,
 99, 99, 99, 99, 99, 99, 99, 99,
 99, 99, 99, 98, 98, 98, 98, 98,
 98, 98, 97, 97, 97, 97, 97, 96,
 96, 96, 96, 95, 95, 95, 94, 94,
 93, 93, 93, 92, 92, 91, 91, 90,
 89, 89, 88, 87, 87, 86, 85, 84,
 83, 82, 81, 80, 79, 78, 77, 75,
 74, 73, 71, 69, 68, 66, 64, 62,
 60, 58, 56, 54, 52, 49, 47, 44,
 42, 41, 40, 40, 39, 39, 38, 38,
 37, 37, 37, 36, 36, 36, 35, 35,
 35, 35, 34, 34, 34, 34, 33, 33,
 33, 33, 32, 32, 32, 32, 32, 31,
 31, 31, 31, 31, 30, 30, 30, 30,
 30
};

PROGMEM_DECLARE(const uint16_t tsl2550Count[128]) =
{
   0,   1,   2,   3,   4,   5,   6,   7,
   8,   9,  10,  11,  12,  13,  14,  15,
  16,  18,  20,  22,  24,  26,  28,  30,
  32,  34,  36,  38,  40,  42,  44,  46,
  49,  53,  57,  61,  65,  69,  73,  77,
  81,  85,  89,  93,  97, 101, 105, 109,
 115, 123, 131, 139, 147, 155, 163, 171,
 179, 187, 195, 203, 211, 219, 227, 235,
 247, 263, 279, 295, 311, 327, 343, 359,
 375, 391, 407, 423, 439, 455, 471, 487,
 511, 543, 575, 607, 639, 671, 703, 735,
 767, 799, 831, 863, 895, 927, 959, 991,
1039,1103,1167,1231,1295,1359,1423,1487,
1551,1615,1679,1743,1807,1871,1935,1999,
2095,2223,2351,2479,2607,2735,2863,2991,
3119,3247,3375,3503,3631,3759,3887,4015
};

/******************************************************************************
                   Static function prototypes section
******************************************************************************/
static bool tsl2550StartReading(void);
static void tsl2550StartFirstReading(void);
static void tsl2550I2cPacketReadDoneCh1(bool result);
static void tsl2550I2cPacketWriteDoneCh1(bool result);
static void tsl2550I2cPacketReadDoneCh0(bool result);
static void tsl2550I2cPacketWriteDoneCh0(bool result);
static void tsl2550I2cPowerupDone(bool result);
static bool tsl2550StartPowerup(void);

/******************************************************************************
                   Implementations section
******************************************************************************/

static Tsl2550Control_t tsl2550Control = {.state = OFF};

/**************************************************************************//**
\brief  Opens the component to use.

\return
  BC_SUCCESS - the component is ready to be used. \n
  BC_FAIL - otherwise.
******************************************************************************/
result_t openTsl2550(void)
{
  if (IDLE == tsl2550Control.state || OFF == tsl2550Control.state)
    return BC_SUCCESS;

  return BC_FAIL;
}

/**************************************************************************//**
\brief  Performs the test if the component have completed request.

\return
  BC_FAIL - the previous request is not completed. \n
  BC_SUCCES - otherwise.
******************************************************************************/
result_t closeTsl2550(void)
{
  if (IDLE == tsl2550Control.state || OFF == tsl2550Control.state)
    return BC_SUCCESS;

  return BC_FAIL;
}

/**************************************************************************//**
\brief  Reads data from tsl2550 sensor.

\param[in]
    f - callback method
\param[in]
    result - the result of the requested operation.
             true - operation finished successfully, false - some error has
             occured.
\param[in]
    data - sensor data.

\return
    BC_FAIL - the previous request was not completed,
           the address of callback is 0, i2c interface is busy,
           there is error on i2c interface. \n
    BC_SUCCESS - in other case.
******************************************************************************/
result_t readTsl2550Data(void (*f)(bool result, int16_t data))
{
  HAL_i2cMode_t i2cMode = {.clockrate = I2C_CLOCK_RATE_62};

  if (NULL == f)
    return BC_FAIL;

  if (OFF == tsl2550Control.state)    // Sensor is in powerdown mode, powerup it before reading
  {
    if (-1 == HAL_OpenI2cPacket(&i2cMode))
      return BC_FAIL;

    if (false == tsl2550StartPowerup())
    {    
      HAL_CloseI2cPacket();
      return BC_FAIL;
    }
    tsl2550Control.state = POWERUP;
  }
  else if (IDLE == tsl2550Control.state)
  {      
    if (-1 == HAL_OpenI2cPacket(&i2cMode))
      return BC_FAIL;

    if (false == tsl2550StartReading())
    {    
      HAL_CloseI2cPacket();
      return BC_FAIL;
    }
    tsl2550Control.state = DATA;
  }
  else 
  {
    return BC_FAIL;
  }

  tsl2550Control.callback = f;

  return BC_SUCCESS;
}

/**************************************************************************//**
\brief BSP tsl2550 handler.
******************************************************************************/
void bspTsl2550Handler(void)
{
  uint8_t result = false;
  uint16_t lux = 0;

  HAL_CloseI2cPacket(); // free

  switch (tsl2550Control.state)
  {
    case POWERUP_ERR:      
      tsl2550Control.state = OFF;
      break;
      
    case DATA_ERR:    
      tsl2550Control.state = IDLE;      
      break;
  
    case DATA:
      {        
        uint32_t count0 = 0, count1 = 0;
        uint8_t ratio = 128;    // default scaling factor        
        uint8_t R;

        tsl2550Control.state = IDLE;

        if (tsl2550Control.ch0 & tsl2550Control.ch1 & 0x80)
        {
          memcpy_P(&count0, &tsl2550Count[tsl2550Control.ch0 & 0x7F], sizeof(uint16_t));
          memcpy_P(&count1, &tsl2550Count[tsl2550Control.ch1 & 0x7F], sizeof(uint16_t));

          if (!count0 || (count0 <= count1))  // count1 cannot be greater than count0
            break;          

          ratio = ((uint32_t)(count1 * 128ul) / count0);
          // calculate lux
          // the "256" is a scaling factor
          memcpy_P(&R, &tsl2550Ratio[ratio], sizeof(uint8_t));
          lux = ((count0 - count1) * R) / 256;
          // range check lux
          if (lux > TSL_MAX_LUX_VALUE)
            lux = TSL_MAX_LUX_VALUE;          

          result = true;
        }
      }
      break;
            
    default:
      ASSERT(false, TSL2550_UNEXPECTED_STATE);      
      tsl2550Control.state = IDLE;
      break;
  }
  
  tsl2550Control.callback(result, lux); 
}

/**************************************************************************//**
\brief Callback that reading from tsl2550 was completed.

\param[in]
  result - contains result of operation.
           if result is false there was problem on i2c interface.
******************************************************************************/
static void tsl2550I2cPacketReadDoneCh1(bool result)
{
  if (false == result)  
    tsl2550Control.state = DATA_ERR;

  bspPostTask(BSP_LIGHT);
}

/**************************************************************************//**
\brief Callback that writing command to tsl2550 was completed.

\param[in]
  result - contains result of operation.
           if result is false there was problem on i2c interface.
******************************************************************************/
static void tsl2550I2cPacketWriteDoneCh1(bool result)
{
  HAL_I2cParams_t i2cParam = 
  {
    .data = &tsl2550Control.ch1,
    .f = tsl2550I2cPacketReadDoneCh1,
    .id = TSL_DEVICE_ADDRESS,
    .length = 1,
    .lengthAddr = HAL_NO_INTERNAL_ADDRESS,
  };

  if ((false == result) || (-1 == HAL_ReadI2cPacket(&i2cParam)))
  {
    tsl2550Control.state = DATA_ERR;
    bspPostTask(BSP_LIGHT);
  }
}

/**************************************************************************//**
\brief Callback that reading from tsl2550 was completed.

\param[in]
  result - contains result of operation.
           if result is false there was problem on i2c interface.
******************************************************************************/
static void tsl2550I2cPacketReadDoneCh0(bool result)
{
  HAL_I2cParams_t i2cParam = 
  {
    .data = &tsl2550Control.ch1,
    .f = tsl2550I2cPacketWriteDoneCh1,
    .id = TSL_DEVICE_ADDRESS,
    .length = 1,
    .lengthAddr = HAL_NO_INTERNAL_ADDRESS,
  };

  tsl2550Control.ch1 = TSL_READ_CHANNEL1_COMMAND;

  if ((false == result) || (-1 == HAL_WriteI2cPacket(&i2cParam)))
  {
    tsl2550Control.state = DATA_ERR;
    bspPostTask(BSP_LIGHT);
  }
}

/**************************************************************************//**
\brief Callback that writing command to tsl2550 was completed.

\param[in]
  result - contains result of operation.
           if result is false there was problem on i2c interface.
******************************************************************************/
static void tsl2550I2cPacketWriteDoneCh0(bool result)
{
  HAL_I2cParams_t i2cParam = 
  {
    .data = &tsl2550Control.ch0,
    .f = tsl2550I2cPacketReadDoneCh0,
    .id = TSL_DEVICE_ADDRESS,
    .length = 1,
    .lengthAddr = HAL_NO_INTERNAL_ADDRESS,
  };

  if ((false == result) || (-1 == HAL_ReadI2cPacket(&i2cParam)))
  {
    tsl2550Control.state = DATA_ERR;
    bspPostTask(BSP_LIGHT);
  }
}

/**************************************************************************//**
\brief Start tsl2550 read sequence.

\return
    false - i2c packet wasn't sent
    true - in other case.
******************************************************************************/
static bool tsl2550StartReading(void)
{
  HAL_I2cParams_t i2cParam = 
  {
    .data = &tsl2550Control.ch0,
    .f = tsl2550I2cPacketWriteDoneCh0,
    .id = TSL_DEVICE_ADDRESS,
    .length = 1,
    .lengthAddr = HAL_NO_INTERNAL_ADDRESS,
  };

  tsl2550Control.ch0 = TSL_READ_CHANNEL0_COMMAND;

  return (-1 != HAL_WriteI2cPacket(&i2cParam)) ? true : false;
}

/**************************************************************************//**
\brief Callback on completion of tsl2550's delay after powerup.
******************************************************************************/
static void tsl2550StartFirstReading(void)
{
  if (false == tsl2550StartReading())
  {
    tsl2550Control.state = DATA_ERR;
    bspPostTask(BSP_LIGHT);    
    return;
  }
    
  tsl2550Control.state = DATA;
}

/**************************************************************************//**
\brief Callback on completion of tsl2550 powerup.

\param[in]
  result - contains result of operation.
           if result is false there was problem on i2c interface.
******************************************************************************/
static void tsl2550I2cPowerupDone(bool result)
{  
  static HAL_AppTimer_t tsl2550PowerupTimer =
  {
    .interval = TSL_CONVERSION_TIME * 2,    // Wait for conversions on two channels one-after-one
    .mode = TIMER_ONE_SHOT_MODE,
    .callback = tsl2550StartFirstReading,
  };

  if (false == result)
  {
    tsl2550Control.state = POWERUP_ERR;
    bspPostTask(BSP_LIGHT);
    return;
  }

  HAL_StartAppTimer(&tsl2550PowerupTimer);
}

/**************************************************************************//**
\brief Start tsl2550 powerup sequence.

\return
    false - i2c packet wasn't sent
    true - in other case.
******************************************************************************/
static bool tsl2550StartPowerup(void)
{
  HAL_I2cParams_t i2cParam = 
  {
    .data = &tsl2550Control.ch0,
    .f = tsl2550I2cPowerupDone,
    .id = TSL_DEVICE_ADDRESS,
    .length = 1,
    .lengthAddr = HAL_NO_INTERNAL_ADDRESS,
  };

  tsl2550Control.ch0 = TSL_POWERUP_COMMAND;

  return (-1 != HAL_WriteI2cPacket(&i2cParam)) ? true : false;
}

// eof tsl2550.c