KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
power.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : nestor.c
11  * Created : 21 jan. 2014
12  * Author : Vincent van Beveren
13  */
14 
15 #include "modules/power.h"
16 
17 #include <stdio.h>
18 
19 #include "util/macro.h"
20 #include "kernel/err.h"
21 #include "drv/i2c/ltc2631.h"
22 #include "drv/i2c/max123x.h"
23 #include "kernel/tm.h"
24 
25 #include "cfg_board.h"
26 
27 // VLED only runs to up half the DAC range.
28 #define VLED_DAC_MAX ( LTC2631_MAX / 2 )
29 
30 //
31 // Channel configuration for the power-board
32 // ADC CH CONVERSION ( ( CONST * ADC_VALUE ) / ADC_MAX_VALUE )
33 //
34 
35 #define _ADC0_COUNT 11
36 #define _ADC1_COUNT 7
37 #define _ADC_COUNT (_ADC0_COUNT + _ADC1_COUNT)
38 
39 static const uint8_t _ADDR[] = { POWER_ADC0_I2C_ADDR, POWER_ADC1_I2C_ADDR };
40 static const uint8_t _COUNTS[] = { _ADC0_COUNT, _ADC1_COUNT };
41 
42 static const uint32_t _SCALING[_ADC_COUNT] = {
43  3300, //! ADC0, CH0, POWER_12V_CUR
44  3300, //! ADC0, CH1, POWER_1V_CUR
45  1320, //! ADC0, CH2, POWER_1V8_CUR
46  1320, //! ADC0, CH3, POWER_2V5_CUR
47  3300, //! ADC0, CH4, POWER_3V3_CUR
48  1320, //! ADC0, CH5, POWER_5V_CUR
49  1320, //! ADC0, CH6, POWER_3V3PMT_CUR
50  6600, //! ADC0, CH7, POWER_VLED_CUR
51  33330, //! ADC0, CH8, POWER_VLED_LVL
52  16500, //! ADC0, CH9, POWER_12V_LVL
53  330000, //! ADC0, CH10, POWER_TEMP_LVL
54  3300, //! ADC1, CH0, POWER_1V_LVL
55  3300, //! ADC1, CH1, POWER_1V8_LVL
56  3300, //! ADC1, CH2, POWER_2V5_LVL
57  6600, //! ADC1, CH3, POWER_3V3_LVL
58  6600, //! ADC1, CH4, POWER_5V_LVL
59  6600, //! ADC1, CH5, POWER_3V3PMT_LVL
60  3300 //! ADC1, CH6, POWER_DACCTL_LVL
61 };
62 
63 bool pwrInit()
64 {
65 #ifdef BASE
66  return true;
67 #else
68  if (!max123xSetup(POWER_I2C_DEV, _ADDR[0], MAX123X_REF_EXT_RIN, false, false)) return false;
69  return max123xSetup(POWER_I2C_DEV, _ADDR[1], MAX123X_REF_EXT_RIN, false, false);
70 #endif
71 }
72 
73 bool pwrMeasureAll(uint16_t * results)
74 {
75 
76 #ifdef BASE
77 return true;
78 #else
79  unsigned int i;
80  uint16_t tmp[_ADC_COUNT];
81  if (!max123xScanUpTo(POWER_I2C_DEV, _ADDR[0], _COUNTS[0] - 1, &(tmp[0]))) return false;
82  if (!max123xScanUpTo(POWER_I2C_DEV, _ADDR[1], _COUNTS[1] - 1, &(tmp[_ADC0_COUNT]))) return false;
83  for (i = 0; i < _ADC_COUNT; ++i) {
84  results[i] = (uint16_t)( ( _SCALING[i] * tmp[i] ) / MAX123X_MAX_VALUE );
85  }
86  return true;
87 #endif
88 }
89 
90 
91 bool pwrSetLED(uint32_t mv)
92 {
93 #ifdef BASE
94  return true;
95 #else
96  if (mv > POWER_VLED_MAX_MV) return errSet(ERROR(E_INVARGUMENT));
97 
98  uint32_t pwr = ( mv * VLED_DAC_MAX + (POWER_VLED_MAX_MV / 2) ) / POWER_VLED_MAX_MV;
99 
100 
101  // printf("Setting nano power: %d mv=%d DAC\n", mv,pwr);
102 
103  return ltc2631Write(POWER_I2C_DEV, POWER_DAC_I2C_ADDR, pwr, true);
104 #endif
105 }
This module provides access to the peripherals on the power board.
#define POWER_I2C_DEV
Power board I2C device.
Definition: cfg_board.h:68
bool ltc2631Write(I2C_Device *dev, uint8_t addr, uint16_t value, bool update)
Sets the DAC value on the input register.
Definition: ltc2631.c:26
Driver of the LTC2631 DAC as found on the power board.
#define POWER_DAC_I2C_ADDR
The address of the Power Board DAC (LTC2631)
Definition: cfg_board.h:81
#define POWER_ADC0_I2C_ADDR
The address of the Power Board ADC (LTC2499) - deprecated, no longer used.
Definition: cfg_board.h:75
bool max123xSetup(I2C_Device *dev, i2cAddr addr, int ref, bool extclk, bool bipolar)
Writes the setup register of the MAX123x.
Definition: max123x.c:38
#define E_INVARGUMENT
Generic error: invalid argument.
Definition: errorcode.h:112
bool pwrMeasureAll(uint16_t *results)
Initiates a conversion for the given channel.
Definition: power.c:73
bool max123xScanUpTo(I2C_Device *dev, i2cAddr addr, int upToCh, uint16_t *results)
Scan from analog input up to a given channel.
Definition: max123x.c:68
Manages the global system error.
MAX123x multichannel ADC driver.
Simple timer functions.
bool pwrInit()
Initializes the powerboard.
Definition: power.c:63
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
Provides common macros.
bool pwrSetLED(uint32_t mv)
Set the LED output in millivolts.
Definition: power.c:91
Configures the board-specific peripherals, like I2C, SPI etc...
#define POWER_VLED_MAX_MV
Maximum power which can be supplied to the LED.
Definition: power.h:54