rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
poten.cpp
Go to the documentation of this file.
1/**
2 * @file poten.cpp
3 * @brief MCP42010 digital potentiometer driver
4 *
5 * @date Mar 16, 2013
6 * @author Andrey Belomutskiy, (c) 2012-2020
7 */
8
9#include "pch.h"
10
11#include "poten.h"
12#include "eficonsole.h"
13#include "hardware.h"
14#include "mpu_util.h"
15
16#if HAL_USE_SPI
17
18/**
19 * MCP42010 digital potentiometer driver
20 *
21 *
22 * 1 CS pin select PB12 PA10
23 * 2 SCK serial clock PA5 PC10
24 * 3 SI serial input (MOSI) PA7 PC12
25 * 4 Vss ground
26 * 5 PB1
27 * 6 PW1
28 * 7 PA1
29 * 8 PA0
30 * 9 PW0
31 * 10 PB0
32 * 11 RS Reset
33 *
34 * 14 Vdd V input
35 *
36 * Rwa = 10000 * (256 - d) / 256 + 52
37 * d = 256 - (Rwa - 52) * 256 / 10000
38 *
39 */
40
41/* Low speed SPI configuration (281.250kHz, CPHA=0, CPOL=0, MSb first).*/
42
43#if defined(STM32F7XX)
44#define SPI_POT_CONFIG SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_CRCL
45#else /* defined(STM32F4XX) */
46#define SPI_POT_CONFIG SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_DFF
47#endif /* defined(STM32F4XX) */
48
49#if EFI_POTENTIOMETER
50static Mcp42010Driver potConfig[DIGIPOT_COUNT];
51
52void initPotentiometer(Mcp42010Driver *driver, SPIDriver *spi, brain_pin_e csPin) {
53 driver->spiConfig.cr1 = SPI_POT_CONFIG;
54 driver->spi = spi;
55 initSpiCs(&driver->spiConfig, csPin);
56}
57
58static int getPotStep(int resistanceWA) {
59 return 256 - (int) ((resistanceWA - 52) * 256 / 10000);
60}
61
62static void sendToPot(Mcp42010Driver *driver, int channel, int value) {
63 lockSpi(SPI_NONE);
64 spiStart(driver->spi, &driver->spiConfig);
65 spiSelect(driver->spi);
66 int word = (17 + channel) * 256 + value;
67 spiSend(driver->spi, 1, &word);
68 spiUnselect(driver->spi);
69 spiStop(driver->spi);
70 unlockSpi(SPI_NONE);
71}
72
73void setPotResistance(Mcp42010Driver *driver, int channel, int resistance) {
74 int value = getPotStep(resistance);
75
76 efiPrintf("Sending to potentiometer%d: %d for R=%d", channel, value, resistance);
77 sendToPot(driver, channel, value);
78}
79
80static void setPotResistanceCommand(int index, int value) {
81 setPotResistance(&potConfig[index / 2], index % 2, value);
82}
83
84static void setPotValue1(int value) {
85 sendToPot(&potConfig[0], 1, value);
86}
87
88#endif /* EFI_POTENTIOMETER */
89
91#if EFI_POTENTIOMETER
93 efiPrintf("digiPot spi disabled");
94 return;
95 }
96 // todo: we have centralized SPI management see other usages of 'turnOnSpi'
97 //turnOnSpi(engineConfiguration->digitalPotentiometerSpiDevice);
98
99 for (int i = 0; i < DIGIPOT_COUNT; i++) {
101 if (!isBrainPinValid(csPin)) {
102 continue;
103 }
104
106 if (driver == NULL) {
107 // error already reported
108 return;
109 }
110
111 initPotentiometer(&potConfig[i], driver, csPin);
112 }
113
115
117
118 setPotResistance(&potConfig[0], 0, 3000);
119 setPotResistance(&potConfig[0], 1, 7000);
120#endif
121}
122
123#endif /* HAL_USE_SPI */
uint16_t channel
Definition adc_inputs.h:104
void initSpiCs(SPIConfig *spiConfig, brain_pin_e csPin)
Definition at32_spi.cpp:241
void addConsoleActionII(const char *token, VoidIntInt callback)
Register a console command with two Integer parameters.
void addConsoleActionI(const char *token, VoidInt callback)
Register a console command with one Integer parameter.
Console package entry point header.
static constexpr engine_configuration_s * engineConfiguration
void unlockSpi(spi_device_e device)
Definition hardware.cpp:195
SPIDriver * getSpiDevice(spi_device_e spiDevice)
Definition hardware.cpp:149
void lockSpi(spi_device_e device)
Definition hardware.cpp:190
bool isBrainPinValid(brain_pin_e brainPin)
void initPotentiometers()
Definition poten.cpp:90
void initPotentiometer(Mcp42010Driver *driver, SPIDriver *spi, brain_pin_e csPin)
Definition poten.cpp:52
void setPotResistance(Mcp42010Driver *driver, int channel, int resistance)
Definition poten.cpp:73
static int getPotStep(int resistanceWA)
Definition poten.cpp:58
static void setPotResistanceCommand(int index, int value)
Definition poten.cpp:80
static Mcp42010Driver potConfig[DIGIPOT_COUNT]
Definition poten.cpp:50
static void setPotValue1(int value)
Definition poten.cpp:84
static void sendToPot(Mcp42010Driver *driver, int channel, int value)
Definition poten.cpp:62
MCP42010 digital potentiometer driver.
SPIConfig spiConfig
Definition poten.h:19
SPIDriver * spi
Definition poten.h:18