rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
mcp3208.cpp File Reference

Functions

static int getValue (McpAdcState *state, int channel)
 
int getMcp3208adc (int channel)
 
static int getNextChannel (void)
 
static void spiCallback (SPIDriver *spip)
 
static void createRequest (McpAdcState *state, int channel)
 
void requestAdcValue (McpAdcState *state, int channel)
 
void requestAdcValueI (McpAdcState *state, int channel)
 
void adc_in_out (McpAdcState *state)
 
void init_adc_mcp3208 (McpAdcState *state, SPIDriver *driver)
 

Variables

McpAdcStatehack
 
static int adcEventCounter = 0
 
static int adcErrorCounter = 0
 
static const SPIConfig spicfg
 

Function Documentation

◆ adc_in_out()

void adc_in_out ( McpAdcState state)

Definition at line 98 of file mcp3208.cpp.

98 {
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}
static int adcEventCounter
Definition mcp3208.cpp:20
static int adcErrorCounter
Definition mcp3208.cpp:21
static int getValue(McpAdcState *state, int channel)
Definition mcp3208.cpp:23
state("state", SensorCategory.SENSOR_INPUTS, FieldType.INT8, 1871, 1.0, -1.0, -1.0, "")
Here is the call graph for this function:

◆ createRequest()

static void createRequest ( McpAdcState state,
int  channel 
)
static

Definition at line 73 of file mcp3208.cpp.

73 {
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}
uint16_t channel
Definition adc_inputs.h:104

Referenced by requestAdcValue(), and requestAdcValueI().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getMcp3208adc()

int getMcp3208adc ( int  channel)

Definition at line 27 of file mcp3208.cpp.

27 {
28 return getValue(hack, channel);
29}
McpAdcState * hack
Definition mcp3208.cpp:18
Here is the call graph for this function:

◆ getNextChannel()

static int getNextChannel ( void  )
static

Definition at line 31 of file mcp3208.cpp.

31 {
32 return adcEventCounter % 2;
33}

Referenced by spiCallback().

Here is the caller graph for this function:

◆ getValue()

static int getValue ( McpAdcState state,
int  channel 
)
static

Definition at line 23 of file mcp3208.cpp.

23 {
24 return state->results[channel] & 4095;
25}

Referenced by adc_in_out(), and getMcp3208adc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_adc_mcp3208()

void init_adc_mcp3208 ( McpAdcState state,
SPIDriver *  driver 
)

Definition at line 128 of file mcp3208.cpp.

128 {
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}
static const SPIConfig spicfg
Definition mcp3208.cpp:64
Here is the call graph for this function:

◆ requestAdcValue()

void requestAdcValue ( McpAdcState state,
int  channel 
)

Definition at line 82 of file mcp3208.cpp.

82 {
84
85 spiSelect(state->driver);
86 spiStartExchange(state->driver, 3, state->tx_buff, state->rx_buff);
87 // SPI unselect is in the callback
88}
static void createRequest(McpAdcState *state, int channel)
Definition mcp3208.cpp:73
Here is the call graph for this function:

◆ requestAdcValueI()

void requestAdcValueI ( McpAdcState state,
int  channel 
)

Definition at line 90 of file mcp3208.cpp.

90 {
92
93 spiSelectI(state->driver);
94 spiStartExchangeI(state->driver, 3, state->tx_buff, state->rx_buff);
95 // SPI unselect is in the callback
96}

Referenced by spiCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spiCallback()

static void spiCallback ( SPIDriver *  spip)
static

Definition at line 35 of file mcp3208.cpp.

35 {
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}
void requestAdcValueI(McpAdcState *state, int channel)
Definition mcp3208.cpp:90
static int getNextChannel(void)
Definition mcp3208.cpp:31
MCP3208 ADC chip driver structure.
Definition mcp3208.h:28
unsigned char rx_buff[3]
Definition mcp3208.h:31
Here is the call graph for this function:

Variable Documentation

◆ adcErrorCounter

int adcErrorCounter = 0
static

Definition at line 21 of file mcp3208.cpp.

Referenced by adc_in_out(), and spiCallback().

◆ adcEventCounter

int adcEventCounter = 0
static

Definition at line 20 of file mcp3208.cpp.

Referenced by adc_in_out(), getNextChannel(), and spiCallback().

◆ hack

McpAdcState* hack

Definition at line 18 of file mcp3208.cpp.

Referenced by getMcp3208adc(), init_adc_mcp3208(), and spiCallback().

◆ spicfg

const SPIConfig spicfg
static
Initial value:
MCP3208_CS_PORT,
MCP3208_CS_PIN,
SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2
}
static void spiCallback(SPIDriver *spip)
Definition mcp3208.cpp:35

Definition at line 64 of file mcp3208.cpp.

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 };

Referenced by init_adc_mcp3208().

Go to the source code of this file.