diff --git a/SensorSPI.c b/SensorSPI.c index 04b328e..b9fa0fd 100644 --- a/SensorSPI.c +++ b/SensorSPI.c @@ -2,22 +2,133 @@ // Created by cfif on 03.06.2024. // #include "SensorSPI.h" +#include "SystemDelayInterface.h" -bool setSpiSensor(tSensorSPI *env, uint16_t reg) { +static void setSensorTypeAdr(tSensorSPI *env, tConABC *connABC, uint8_t adr) { - GpioPinSet(&env->conns->COON_A_B9_B10.En1, false); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + switch (adr) { + case 0: { + GpioPinSet(&connABC->A, false); + GpioPinSet(&connABC->B, false); + GpioPinSet(&connABC->C, false); + break; + } + case 1: { + GpioPinSet(&connABC->A, true); + GpioPinSet(&connABC->B, false); + GpioPinSet(&connABC->C, false); + break; + } + case 2: { + GpioPinSet(&connABC->A, false); + GpioPinSet(&connABC->B, true); + GpioPinSet(&connABC->C, false); + break; + } + case 3: { + GpioPinSet(&connABC->A, true); + GpioPinSet(&connABC->B, true); + GpioPinSet(&connABC->C, false); + break; + } + case 4: { + GpioPinSet(&connABC->A, false); + GpioPinSet(&connABC->B, false); + GpioPinSet(&connABC->C, true); + break; + } + case 5: { + GpioPinSet(&connABC->A, true); + GpioPinSet(&connABC->B, false); + GpioPinSet(&connABC->C, true); + break; + } + case 6: { + GpioPinSet(&connABC->A, false); + GpioPinSet(&connABC->B, true); + GpioPinSet(&connABC->C, true); + break; + } + case 7: { + GpioPinSet(&connABC->A, true); + GpioPinSet(&connABC->B, true); + GpioPinSet(&connABC->C, true); + break; + } + } +} - GpioPinSet(&env->conns->COON_A_B9_B10.Con.A, false); - GpioPinSet(&env->conns->COON_A_B9_B10.Con.B, false); - GpioPinSet(&env->conns->COON_A_B9_B10.Con.C, false); +bool setSpiSensor(tSensorSPI *env, tSensorType sensorType, tSensorAdr sensorAdr, uint16_t data) { - bool result = SpiPortTransmit(&env->spiPorts->Spi1_IO, ®, 1000); + switch (sensorType) { - GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + case SENSOR_TYPE_A: { - return result; + if (sensorAdr >= SENSOR_ADR_9) { + GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); + GpioPinSet(&env->conns->COON_A_B9_B10.En2, false); + setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, sensorAdr - 8); + } else { + GpioPinSet(&env->conns->COON_A_B9_B10.En1, false); + GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, sensorAdr); + } + + SystemDelayMs(1); + bool result = SpiPortTransmit(&env->spiPorts->Spi1_IO, &data, 1000); + + SystemDelayMs(1); + + GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); + GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + + return result; + } + + case SENSOR_TYPE_B: { + + if ((sensorAdr == SENSOR_ADR_8) || (sensorAdr == SENSOR_ADR_9)) { + GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); + GpioPinSet(&env->conns->COON_A_B9_B10.En2, false); + + if (sensorAdr == SENSOR_ADR_8) { + setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, SENSOR_ADR_8 - 1); + } + + if (sensorAdr == SENSOR_ADR_9) { + setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, SENSOR_ADR_9 - 1); + } + + bool result = SpiPortTransmit(&env->spiPorts->Spi1_IO, &data, 1000); + + GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); + GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + + return result; + } else { + GpioPinSet(&env->conns->COON_B.En1, false); + setSensorTypeAdr(env, &env->conns->COON_B.Con, sensorAdr); + + bool result = SpiPortTransmit(&env->spiPorts->Spi1_IO, &data, 1000); + + GpioPinSet(&env->conns->COON_B.En1, true); + + return result; + } + + break; + } + + case SENSOR_TYPE_C: { + break; + } + + case SENSOR_TYPE_D: { + break; + } + } + + return false; } void SensorSpi_Init(tSensorSPI *env, tSpiPorts *spiPorts, tConns *conns) { diff --git a/SensorSPI.h b/SensorSPI.h index d121e3f..ce116cb 100644 --- a/SensorSPI.h +++ b/SensorSPI.h @@ -22,6 +22,7 @@ typedef struct { typedef struct { tConn COON_A_B9_B10; + tConn COON_B; } tConns; typedef struct { @@ -29,7 +30,34 @@ typedef struct { tSpiPorts *spiPorts; } tSensorSPI; +typedef enum { + SENSOR_TYPE_A, + SENSOR_TYPE_B, + SENSOR_TYPE_C, + SENSOR_TYPE_D, +} tSensorType; + +typedef enum { + SENSOR_ADR_1 = 0, + SENSOR_ADR_2 = 1, + SENSOR_ADR_3 = 2, + SENSOR_ADR_4 = 3, + SENSOR_ADR_5 = 4, + SENSOR_ADR_6 = 5, + SENSOR_ADR_7 = 6, + SENSOR_ADR_8 = 7, + SENSOR_ADR_9 = 8, + SENSOR_ADR_10 = 9, + SENSOR_ADR_11 = 10, + SENSOR_ADR_12 = 11, + SENSOR_ADR_13 = 12, + SENSOR_ADR_14 = 13, + SENSOR_ADR_15 = 14, + SENSOR_ADR_16 = 15 +} tSensorAdr; + void SensorSpi_Init(tSensorSPI *env, tSpiPorts *spiPorts, tConns *conns); -bool setSpiSensor(tSensorSPI *env, uint16_t reg); + +bool setSpiSensor(tSensorSPI *env, tSensorType sensorType, tSensorAdr sensorAdr, uint16_t data); #endif //SMART_COMPONENTS_SENSOR_H