-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiomod.c
175 lines (153 loc) · 5.75 KB
/
iomod.c
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
/* Copyright (C) 2016, Marc-Andre Guimond <guimond.marcandre@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* This file is encoded in UTF-8.
*/
// Lib includes.
#include "iomod.h"
#include "boardconfig.h"
#include "conversion.h"
#include "utils.h"
// Drivers includes.
#include "adc128d818.h"
#include "usp10973.h"
// ----------------------------------------------------------------------------
// Private variables.
IOMod_t gIOMod;
static uint8_t gADCI2CAddressTable[kADC128D818_MaxAddresses];
// Private macros.
#define mIOModValidateDriverStatus(returnStatus) if (returnStatus != 0) { return kIOModPortStatus_DriverBusError; }
// ----------------------------------------------------------------------------
uint8_t IOModGetADCAddress(uint8_t inSlaveID)
{
// Set local devices addresses.
uint8_t adcAddress;
switch (inSlaveID)
{
case 0:
adcAddress = kADC128D818_SlaveAddress1;
break;
case 1:
adcAddress = kADC128D818_SlaveAddress2;
break;
case 2:
adcAddress = kADC128D818_SlaveAddress3;
break;
case 3:
adcAddress = kADC128D818_SlaveAddress4;
break;
case 4:
adcAddress = kADC128D818_SlaveAddress5;
break;
case 5:
adcAddress = kADC128D818_SlaveAddress6;
break;
case 6:
adcAddress = kADC128D818_SlaveAddress7;
break;
case 7:
adcAddress = kADC128D818_SlaveAddress8;
break;
case 8:
adcAddress = kADC128D818_SlaveAddress9;
break;
default:
adcAddress = 0xFF;
break;
}
return adcAddress;
}
// ----------------------------------------------------------------------------
IOModPortStatus_e IOModADCInit(uint8_t inSlaveID)
{
// Get the slave ADC address.
gADCI2CAddressTable[inSlaveID] = IOModGetADCAddress(inSlaveID);
// Initialize ADC.
mIOModValidateDriverStatus(ADC128D818Init(gADCI2CAddressTable[inSlaveID]));
// Start ADC continuous conversions.
mIOModValidateDriverStatus(ADC128D818StartConversion(gADCI2CAddressTable[inSlaveID], kADC128D818_ConversionRate_Continuous));
// If we make it this far, its a success.
return 0;
}
// ----------------------------------------------------------------------------
IOModPortStatus_e IOModGetTemperature(uint8_t inSlaveID, uint8_t inChannelIdx, int32_t* outADCData)
{
int status = 0;
int32_t temperature = 0;
uint16_t adcRawData;
mIOModValidateDriverStatus(ADC128D818ReadChannel(gADCI2CAddressTable[inSlaveID], inChannelIdx, &adcRawData));
// Convert thermistor value.
if (USP10973BetaComputeTemperature(adcRawData, &temperature) != 0)
{
status = kIOModPortStatus_InvalidRange;
}
else
{
status = kIOModPortStatus_Valid;
*outADCData = temperature;
}
// If we make it this far, its a success.
return status;
}
// ----------------------------------------------------------------------------
IOModPortStatus_e IOModGetInternalTemperature(uint8_t inSlaveID, int32_t* outADCData)
{
uint16_t adcRawData;
mIOModValidateDriverStatus(ADC128D818SetMode(gADCI2CAddressTable[inSlaveID], kADC128D818_Mode_Temp));
// TODO: Wait for conversion switch to stabilize.
DelayUs(30000);
mIOModValidateDriverStatus(ADC128D818ReadChannel(gADCI2CAddressTable[inSlaveID], kADC128D818_RegisterChannel7Read, &adcRawData));
// TODO: Should put back in the config mode it was instead of forcing back single ended.
mIOModValidateDriverStatus(ADC128D818SetMode(gADCI2CAddressTable[inSlaveID], kADC128D818_Mode_SingleEnded));
// Shift right 3 as we pass from 12 bit to 9 bit result for internal temperature.
adcRawData >>= 3;
// 9-bit two's-complement conversions of the temperature (see datasheet p.28).
// Note: Normal conversion x 1000 to add 3 float digits to be compatible with single-ended ADC conversions (however precision is 0.5C, thus 500).
if (!(adcRawData & kADC128D818_TemperatureMSBMask))
{
// If temperature is positive.
// If DOUT[MSb] = 0: + Temp(�C) = DOUT(dec) / 2.
*outADCData = (adcRawData * 1000) >> 1;
}
else
{
// If temperature is negative.
// If DOUT[MSb] = 1: � Temp(�C) = [29 � DOUT(dec)] / 2 .
*outADCData = -(int32_t)(((0x0200 - adcRawData) * 1000) >> 1);
}
// If we make it this far, its a success.
return 0;
}
// ----------------------------------------------------------------------------
IOModPortStatus_e IOModGetCurrent(uint8_t inSlaveID, uint8_t inChannelIdx, int32_t* outADCData)
{
int status;
int32_t current = 0;
uint16_t adcRawData;
mIOModValidateDriverStatus(ADC128D818ReadChannel(gADCI2CAddressTable[inSlaveID], inChannelIdx, &adcRawData));
// Convert to mA: (300 / 2^12 - 1) * 2^16.
current = ConversionDecode(adcRawData, 4801, 16, 1);
// TODO: Make constants (should also be programmable).
if (current > 300)
{
status = kIOModPortStatus_OverLoad;
}
else
{
status = kIOModPortStatus_Valid;
*outADCData = current;
}
return status;
}