rusEFI
The most advanced open source ECU
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 96 of file mcp3208.cpp.

96  {
97 
98 // chThdSleepMilliseconds(10);
99 
100  int result = state->results[0];
101 
102 // int v = result;
103  unsigned r = 0;
104 //
105 // while (v >>= 1)
106 // r++;
107 //
108 // unsigned int f;
109 // if (r < 12) {
110 // f = -1;
111 // } else {
112 // f = result >> (r - 12);
113 // f = f & 4095;
114 // }
115 
116  int errRatio = 1000 * adcErrorCounter / adcEventCounter;
117 
118  print("c/e %7d/%7d/%4d result %5d r=%d ", adcEventCounter, adcErrorCounter, errRatio, result, r);
119 
120  unsigned int f0 = getValue(state, 0);
121  print("ch0=%d adj %d ", f0, f0 * 5000 / 4096);
122  unsigned int f1 = getValue(state, 1);
123  print("ch1=%d adj %d\r\n", f1, f1 * 5000 / 4096);
124 }
static int adcEventCounter
Definition: mcp3208.cpp:18
static int adcErrorCounter
Definition: mcp3208.cpp:19
static int getValue(McpAdcState *state, int channel)
Definition: mcp3208.cpp:21
static ScState state
Here is the call graph for this function:

◆ createRequest()

static void createRequest ( McpAdcState state,
int  channel 
)
static

Definition at line 71 of file mcp3208.cpp.

71  {
72  efiAssertVoid(ObdCode::CUSTOM_ERR_6680, channel < 8, "Invalid ADC channel");
73 
74  state->requestedChannel = channel;
75 
76  state->tx_buff[0] = 0x06 + (channel >> 2);
77  state->tx_buff[1] = (channel & 3) << 6;
78 }

Referenced by requestAdcValue(), and requestAdcValueI().

Here is the caller graph for this function:

◆ getMcp3208adc()

int getMcp3208adc ( int  channel)

Definition at line 25 of file mcp3208.cpp.

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

◆ getNextChannel()

static int getNextChannel ( void  )
static

Definition at line 29 of file mcp3208.cpp.

29  {
30  return adcEventCounter % 2;
31 }

Referenced by spiCallback().

Here is the caller graph for this function:

◆ getValue()

static int getValue ( McpAdcState state,
int  channel 
)
static

Definition at line 21 of file mcp3208.cpp.

21  {
22  return state->results[channel] & 4095;
23 }

Referenced by adc_in_out(), and getMcp3208adc().

Here is the caller graph for this function:

◆ init_adc_mcp3208()

void init_adc_mcp3208 ( McpAdcState state,
SPIDriver *  driver 
)

Definition at line 126 of file mcp3208.cpp.

126  {
127  state->driver = driver;
128  state->tx_buff[2] = 0;
129 
130  hack = state;
131 
132  todo: convert to new API, todo: array of CS
133  mySetPadMod("ext adc chip select", MCP3208_CS_PORT, MCP3208_CS_PIN, PAL_STM32_MODE_OUTPUT);
134 
135 
136  spiStart(driver, &spicfg);
137 }
static const SPIConfig spicfg
Definition: mcp3208.cpp:62

◆ requestAdcValue()

void requestAdcValue ( McpAdcState state,
int  channel 
)

Definition at line 80 of file mcp3208.cpp.

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

◆ requestAdcValueI()

void requestAdcValueI ( McpAdcState state,
int  channel 
)

Definition at line 88 of file mcp3208.cpp.

88  {
89  createRequest(state, channel);
90 
91  spiSelectI(state->driver);
92  spiStartExchangeI(state->driver, 3, state->tx_buff, state->rx_buff);
93  // SPI unselect is in the callback
94 }

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 33 of file mcp3208.cpp.

33  {
34  spiUnselectI(spip);
36 
38 
39  int withError = 0;
40 
41  if (state->rx_buff[0] != 255) {
42  withError = 1;
43  //fatal("ADC: first byte");
44  }
45 
46  if ((state->rx_buff[1] & 0xE0) != 0xE0) {
47  withError = 1;
48  //fatal("ADC: second byte");
49  }
50 
51  if (!withError) {
52 // unsigned char upperByte = state->rx_buff[1] & 0b00001111;
53  int result = (state->rx_buff[0] << 16) + (state->rx_buff[1] << 8) + state->rx_buff[2];
54  state->results[state->requestedChannel] = result;
55  } else {
57  }
58 
60 }
void requestAdcValueI(McpAdcState *state, int channel)
Definition: mcp3208.cpp:88
static int getNextChannel(void)
Definition: mcp3208.cpp:29
MCP3208 ADC chip driver structure.
Definition: mcp3208.h:28
Here is the call graph for this function:

Variable Documentation

◆ adcErrorCounter

int adcErrorCounter = 0
static

Definition at line 19 of file mcp3208.cpp.

Referenced by adc_in_out(), and spiCallback().

◆ adcEventCounter

int adcEventCounter = 0
static

Definition at line 18 of file mcp3208.cpp.

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

◆ hack

McpAdcState* hack

Definition at line 16 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:33

Definition at line 62 of file mcp3208.cpp.

Referenced by init_adc_mcp3208().

Go to the source code of this file.