From fc93dbfc10cc22ed5bb0d6c928b106f67271e1bd Mon Sep 17 00:00:00 2001 From: cfif Date: Thu, 23 Apr 2026 12:10:21 +0300 Subject: [PATCH] Init --- SensorSPI.c | 198 ++++++++++++++++++++++++++++++++++++++++++---------- SensorSPI.h | 15 +++- 2 files changed, 173 insertions(+), 40 deletions(-) diff --git a/SensorSPI.c b/SensorSPI.c index 1e5e7ab..73586d9 100644 --- a/SensorSPI.c +++ b/SensorSPI.c @@ -58,87 +58,209 @@ static void setSensorTypeAdr(tSensorSPI *env, tConABC *connABC, uint8_t adr) { } } +#define DelayCn 1000 + static void Delay(uint32_t count) { for (uint32_t i = 0; i < count; ++i) { __asm__ volatile("nop"); } } + bool setSpiSensor(tSensorSPI *env, tSensorType sensorType, tSensorAdr sensorAdr, uint16_t data) { switch (sensorType) { case SENSOR_TYPE_A: { - GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); - Delay(1000); + bool result = false; + if (sensorAdr >= SENSOR_ADR_9) { - setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, sensorAdr - 8); - GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, false); + setSensorTypeAdr(env, &env->conns->COON_A.Con, sensorAdr - SENSOR_ADR_9); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_A.En1, true); + GpioPinSet(&env->conns->COON_A.En2, false); // CS En2 } else { - setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, sensorAdr); - GpioPinSet(&env->conns->COON_A_B9_B10.En1, false); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + setSensorTypeAdr(env, &env->conns->COON_A.Con, sensorAdr); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_A.En1, false); // CS En1 + GpioPinSet(&env->conns->COON_A.En2, true); } - Delay(1000); + Delay(DelayCn); + result = SpiPortTransmit(&env->spiPorts->Spi1_IO, &data, 1000); + Delay(DelayCn); - bool result = SpiPortTransmit(&env->spiPorts->Spi1_IO, &data, 1000); - - Delay(1000); - - GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); - - Delay(1000); + GpioPinSet(&env->conns->COON_A.En1, true); + GpioPinSet(&env->conns->COON_A.En2, true); + Delay(DelayCn); return result; } + case SENSOR_TYPE_B: { + bool result = false; - 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); - } + // Эти управляются мультиплексором SENSOR_TYPE_A + if ((sensorAdr == SENSOR_ADR_9) || (sensorAdr == SENSOR_ADR_10)) { if (sensorAdr == SENSOR_ADR_9) { - setSensorTypeAdr(env, &env->conns->COON_A_B9_B10.Con, SENSOR_ADR_9 - 1); + setSensorTypeAdr(env, &env->conns->COON_A.Con, SENSOR_ADR_7); } - bool result = SpiPortTransmit(&env->spiPorts->Spi1_IO, &data, 1000); + if (sensorAdr == SENSOR_ADR_10) { + setSensorTypeAdr(env, &env->conns->COON_A.Con, SENSOR_ADR_8); + } - GpioPinSet(&env->conns->COON_A_B9_B10.En1, true); - GpioPinSet(&env->conns->COON_A_B9_B10.En2, true); + GpioPinSet(&env->conns->COON_A.En1, true); + GpioPinSet(&env->conns->COON_A.En2, false); // CS En2 - return result; + Delay(DelayCn); + // Только тут SPI2 + result = SpiPortTransmit(&env->spiPorts->Spi2_IO, &data, 1000); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_A.En1, true); + GpioPinSet(&env->conns->COON_A.En2, true); + + Delay(DelayCn); } 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); + Delay(DelayCn); + result = SpiPortTransmit(&env->spiPorts->Spi2_IO, &data, 1000); + Delay(DelayCn); GpioPinSet(&env->conns->COON_B.En1, true); - - return result; } - break; + Delay(DelayCn); + return result; } - case SENSOR_TYPE_C: { - break; + + case SENSOR_TYPE_E: { + + if (sensorAdr >= SENSOR_ADR_9) { + setSensorTypeAdr(env, &env->conns->COON_E.Con, sensorAdr - SENSOR_ADR_9); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_E.En1, true); + GpioPinSet(&env->conns->COON_E.En2, false); // CS En2 + } else { + setSensorTypeAdr(env, &env->conns->COON_E.Con, sensorAdr); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_E.En1, false); // CS En1 + GpioPinSet(&env->conns->COON_E.En2, true); + } + + Delay(DelayCn); + bool result = SpiPortTransmit(&env->spiPorts->Spi3_IO, &data, 1000); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_E.En1, true); + GpioPinSet(&env->conns->COON_E.En2, true); + + Delay(DelayCn); + return result; } + case SENSOR_TYPE_D: { - break; + bool result = false; + + // Эти управляются мультиплексором SENSOR_TYPE_E + if ((sensorAdr == SENSOR_ADR_9) || (sensorAdr == SENSOR_ADR_10)) { + + if (sensorAdr == SENSOR_ADR_9) { + setSensorTypeAdr(env, &env->conns->COON_E.Con, SENSOR_ADR_5); + } + + if (sensorAdr == SENSOR_ADR_10) { + setSensorTypeAdr(env, &env->conns->COON_E.Con, SENSOR_ADR_6); + } + + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_E.En1, true); + GpioPinSet(&env->conns->COON_E.En2, false); // CS En2 + + Delay(DelayCn); + result = SpiPortTransmit(&env->spiPorts->Spi3_IO, &data, 1000); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_E.En1, true); + GpioPinSet(&env->conns->COON_E.En2, true); + + } else { + + GpioPinSet(&env->conns->COON_D.En1, false); // CS En1 + Delay(DelayCn); + + setSensorTypeAdr(env, &env->conns->COON_D.Con, sensorAdr); + + Delay(DelayCn); + result = SpiPortTransmit(&env->spiPorts->Spi3_IO, &data, 1000); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_D.En1, true); + } + + Delay(DelayCn); + return result; } + + + case SENSOR_TYPE_F: { + + if (sensorAdr >= SENSOR_ADR_17) { + setSensorTypeAdr(env, &env->conns->COON_F.Con, sensorAdr - SENSOR_ADR_17); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_F.En1, true); + GpioPinSet(&env->conns->COON_F.En2, true); + GpioPinSet(&env->conns->COON_F.En3, false); // CS En3 + } else { + + if (sensorAdr >= SENSOR_ADR_9) { + setSensorTypeAdr(env, &env->conns->COON_F.Con, sensorAdr - SENSOR_ADR_9); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_F.En1, true); + GpioPinSet(&env->conns->COON_F.En2, false); // CS En2 + GpioPinSet(&env->conns->COON_F.En3, true); + } else { + setSensorTypeAdr(env, &env->conns->COON_F.Con, sensorAdr); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_F.En1, false); // CS En1 + GpioPinSet(&env->conns->COON_F.En2, true); + GpioPinSet(&env->conns->COON_F.En3, true); + } + + } + + Delay(DelayCn); + bool result = SpiPortTransmit(&env->spiPorts->Spi4_IO, &data, 1000); + Delay(DelayCn); + + GpioPinSet(&env->conns->COON_F.En1, true); + GpioPinSet(&env->conns->COON_F.En2, true); + GpioPinSet(&env->conns->COON_F.En3, true); + + + Delay(DelayCn); + return result; + } + + } return false; diff --git a/SensorSPI.h b/SensorSPI.h index ce116cb..82e1185 100644 --- a/SensorSPI.h +++ b/SensorSPI.h @@ -18,11 +18,16 @@ typedef struct { tConABC Con; tGpioPin En1; tGpioPin En2; + tGpioPin En3; } tConn; typedef struct { - tConn COON_A_B9_B10; + tConn COON_A; tConn COON_B; + tConn COON_C; + tConn COON_D; + tConn COON_E; + tConn COON_F; } tConns; typedef struct { @@ -35,6 +40,8 @@ typedef enum { SENSOR_TYPE_B, SENSOR_TYPE_C, SENSOR_TYPE_D, + SENSOR_TYPE_E, + SENSOR_TYPE_F } tSensorType; typedef enum { @@ -53,7 +60,11 @@ typedef enum { SENSOR_ADR_13 = 12, SENSOR_ADR_14 = 13, SENSOR_ADR_15 = 14, - SENSOR_ADR_16 = 15 + SENSOR_ADR_16 = 15, + SENSOR_ADR_17 = 16, + SENSOR_ADR_18 = 17, + SENSOR_ADR_19 = 18, + SENSOR_ADR_20 = 19 } tSensorAdr; void SensorSpi_Init(tSensorSPI *env, tSpiPorts *spiPorts, tConns *conns);