rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
mcp3208.cpp
Go to the documentation of this file.
1/*
2 * @file mcp3208.cpp
3 * @brief MCP3208 external ADC chip implementation. Not really used right now.
4 *
5 * never has been used in PROD? kill it?
6 *
7 * @date Aug 12, 2013
8 * @author Andrey Belomutskiy, (c) 2012-2020
9 */
10
11#include "global.h"
12
13#if 0
14
15#include "mcp3208.h"
16#include "pin_repository.h"
17
19
20static int adcEventCounter = 0;
21static int adcErrorCounter = 0;
22
23static int getValue(McpAdcState *state, int channel) {
24 return state->results[channel] & 4095;
25}
26
28 return getValue(hack, channel);
29}
30
31static int getNextChannel(void) {
32 return adcEventCounter % 2;
33}
34
35static void spiCallback(SPIDriver *spip) {
36 spiUnselectI(spip);
38
40
41 int withError = 0;
42
43 if (state->rx_buff[0] != 255) {
44 withError = 1;
45 //fatal("ADC: first byte");
46 }
47
48 if ((state->rx_buff[1] & 0xE0) != 0xE0) {
49 withError = 1;
50 //fatal("ADC: second byte");
51 }
52
53 if (!withError) {
54// unsigned char upperByte = state->rx_buff[1] & 0b00001111;
55 int result = (state->rx_buff[0] << 16) + (state->rx_buff[1] << 8) + state->rx_buff[2];
56 state->results[state->requestedChannel] = result;
57 } else {
59 }
60
62}
63
64static const SPIConfig spicfg = { spiCallback,
65/* HW dependent part.*/
66MCP3208_CS_PORT,
67MCP3208_CS_PIN,
68//SPI_CR1_MSTR |
69 SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2
70// SPI_CR1_BR_1 | SPI_CR1_BR_2
71 };
72
74 efiAssertVoid(ObdCode::CUSTOM_ERR_6680, channel < 8, "Invalid ADC channel");
75
76 state->requestedChannel = channel;
77
78 state->tx_buff[0] = 0x06 + (channel >> 2);
79 state->tx_buff[1] = (channel & 3) << 6;
80}
81
84
85 spiSelect(state->driver);
86 spiStartExchange(state->driver, 3, state->tx_buff, state->rx_buff);
87 // SPI unselect is in the callback
88}
89
92
93 spiSelectI(state->driver);
94 spiStartExchangeI(state->driver, 3, state->tx_buff, state->rx_buff);
95 // SPI unselect is in the callback
96}
97
99
100// chThdSleepMilliseconds(10);
101
102 int result = state->results[0];
103
104// int v = result;
105 unsigned r = 0;
106//
107// while (v >>= 1)
108// r++;
109//
110// unsigned int f;
111// if (r < 12) {
112// f = -1;
113// } else {
114// f = result >> (r - 12);
115// f = f & 4095;
116// }
117
118 int errRatio = 1000 * adcErrorCounter / adcEventCounter;
119
120 print("c/e %7d/%7d/%4d result %5d r=%d ", adcEventCounter, adcErrorCounter, errRatio, result, r);
121
122 unsigned int f0 = getValue(state, 0);
123 print("ch0=%d adj %d ", f0, f0 * 5000 / 4096);
124 unsigned int f1 = getValue(state, 1);
125 print("ch1=%d adj %d\r\n", f1, f1 * 5000 / 4096);
126}
127
128void init_adc_mcp3208(McpAdcState *state, SPIDriver *driver) {
129 state->driver = driver;
130 state->tx_buff[2] = 0;
131
132 hack = state;
133
134 todo: convert to new API, todo: array of CS
135 mySetPadMod("ext adc chip select", MCP3208_CS_PORT, MCP3208_CS_PIN, PAL_STM32_MODE_OUTPUT);
136
137
138 spiStart(driver, &spicfg);
139}
140
141#endif /* 0 */
uint16_t channel
Definition adc_inputs.h:104
static void spiCallback(SPIDriver *spip)
Definition mcp3208.cpp:35
static int adcEventCounter
Definition mcp3208.cpp:20
void requestAdcValueI(McpAdcState *state, int channel)
Definition mcp3208.cpp:90
int getMcp3208adc(int channel)
Definition mcp3208.cpp:27
McpAdcState * hack
Definition mcp3208.cpp:18
static int getNextChannel(void)
Definition mcp3208.cpp:31
void adc_in_out(McpAdcState *state)
Definition mcp3208.cpp:98
void requestAdcValue(McpAdcState *state, int channel)
Definition mcp3208.cpp:82
static int adcErrorCounter
Definition mcp3208.cpp:21
static void createRequest(McpAdcState *state, int channel)
Definition mcp3208.cpp:73
static const SPIConfig spicfg
Definition mcp3208.cpp:64
static int getValue(McpAdcState *state, int channel)
Definition mcp3208.cpp:23
void init_adc_mcp3208(McpAdcState *state, SPIDriver *driver)
Definition mcp3208.cpp:128
I/O pin registry header.
state("state", SensorCategory.SENSOR_INPUTS, FieldType.INT8, 1871, 1.0, -1.0, -1.0, "")
MCP3208 ADC chip driver structure.
Definition mcp3208.h:28
int results[8]
Definition mcp3208.h:33
unsigned char rx_buff[3]
Definition mcp3208.h:31