KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ltc2499.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : ltc2499.c
11  * Created : 14 mrt. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #include <stdio.h>
17 
18 #include "drv/i2c/ltc2499.h"
19 
20 #define _LTC2499_RESULT_MASK 0xC0000000
21 #define _LTC2499_RESULT_OVERFLOW 0xC0000000
22 #define _LTC2499_RESULT_UNDERFLOW 0x00000000
23 
24 
25 
26 #define _LTC2499_CH_PREAMBLE 0x80
27 #define _LTC2499_CH_ENABLE 0x20
28 #define _LTC2499_CH_SGL 0x10
29 #define _LTC2499_CH_SIGN 0x08
30 
31 #define _LTC2499_CFG_KEEPPREV 0x00
32 
33 #define _LTC2499_CFG_ENABLE 0x80
34 #define _LTC2499_CFG_TMP 0x40
35 #define _LTC2499_CFG_REJ_5060HZ 0x00
36 #define _LTC2499_CFG_REJ_60HZ 0x20
37 #define _LTC2499_CFG_REJ_50HZ 0x10
38 #define _LTC2499_CFG_SPEED 0x08
39 
40 
41 bool ltc2499Read(I2C_Device * dev, int addr, int32_t * value, LTC2499Status * status)
42 {
43 
44  uint8_t data[4];
45  if (!i2cRead(dev, addr, data, sizeof(data))) return errRebase("LTC2489");
46 
47  uint32_t t = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | ( data[3] & 0xC0 );
48  switch (t & _LTC2499_RESULT_MASK)
49  {
50  case _LTC2499_RESULT_OVERFLOW:
51  *status = ltc2499StatusOverFlow;
52  *value = LTC2499_MAX_VALUE;
53  break;
54  case _LTC2499_RESULT_UNDERFLOW:
55  *status = ltc2499StatusUnderFlow;
56  *value = LTC2499_MIN_VALUE;
57  break;
58  default:
59  *status = ltc2499StatusOk;
60  *value = (int32_t)t << 1;
61  *value = *value >> 7;
62  break;
63  }
64 
65  return true;
66 }
67 
68 static inline uint32_t flt2bits(LTC2499Filter fltCfg) {
69  switch (fltCfg) {
70  case ltc2499Filter50Hz: return _LTC2499_CFG_REJ_50HZ;
71  case ltc2499Filter60Hz: return _LTC2499_CFG_REJ_60HZ;
72  case ltc2499Filter50And60Hz: return _LTC2499_CFG_REJ_5060HZ;
73  default: return _LTC2499_CFG_REJ_5060HZ;
74  }
75 }
76 
77 bool ltc2499CfgExt(I2C_Device * dev, int addr, bool diff, uint8_t posCh, LTC2499Filter fltCfg, bool speed2x)
78 {
79  uint8_t cfg[2];
80  cfg[0] = _LTC2499_CH_PREAMBLE;
81  if (posCh != LTC2499_CH_KEEPPREV)
82  {
83  // channel selection
84  cfg[0] |= _LTC2499_CH_ENABLE | ( diff ? 0 : _LTC2499_CH_SGL);
85  cfg[0] |= ( posCh >> 1 ) | ( posCh & 1 ? _LTC2499_CH_SIGN : 0);
86  }
87 
88  // Additional configuration.
89  cfg[1] = _LTC2499_CFG_ENABLE | (speed2x ? _LTC2499_CFG_SPEED : 0);
90  cfg[1] |= flt2bits(fltCfg);
91 
92  return i2cWrite(dev, addr, cfg, sizeof(cfg));
93 }
94 
95 bool ltc2499CfgTemp(I2C_Device * dev, int addr, LTC2499Filter fltCfg)
96 {
97  uint8_t cfg[2];
98  cfg[0] = _LTC2499_CH_PREAMBLE;
99  cfg[1] = _LTC2499_CFG_ENABLE | _LTC2499_CFG_TMP | flt2bits(fltCfg);
100 
101  return i2cWrite(dev, addr, cfg, sizeof(cfg));
102 }
103 
bool i2cWrite(I2C_Device *dev, i2cAddr addr, uint8_t *bytes, int len)
Writes to the I2C device.
Definition: i2c.c:219
bool ltc2499Read(I2C_Device *dev, int addr, int32_t *value, LTC2499Status *result)
Reads the LTC ADC at I2C address &#39;addr&#39;.
Definition: ltc2499.c:41
bool ltc2499CfgExt(I2C_Device *dev, int addr, bool diff, uint8_t posCh, LTC2499Filter fltCfg, bool speed2x)
Configure LTC2499 with external input.
Definition: ltc2499.c:77
Structure defines OpenCores I2C Device.
Definition: dev_i2c.h:55
Underflow.
Definition: ltc2499.h:35
Driver of the LTC2499 ADC as found on the power board.
Conversion is ok.
Definition: ltc2499.h:33
bool i2cRead(I2C_Device *dev, i2cAddr addr, uint8_t *bytes, int len)
Reads from the I2C device.
Definition: i2c.c:161
static bool errRebase(const char *name)
Rebases the cause of the error message.
Definition: err.h:104
Overflow.
Definition: ltc2499.h:34
LTC2499Status
The status enum, indicating any issues with the value (underflow/overflow).
Definition: ltc2499.h:32