| 1 | /******************************************************************************
|
|---|
| 2 | *
|
|---|
| 3 | * @file ads1260.c
|
|---|
| 4 | * @author ECS, Joseph Zimmer
|
|---|
| 5 | * @version V1.0.0
|
|---|
| 6 | * @date 25-04-2019
|
|---|
| 7 | * @brief
|
|---|
| 8 | * INITIALISIERUNG ADS1260:
|
|---|
| 9 | * 0. Setze die ADC Zustandsvariable auf ADC_STATE_INITIALIZE
|
|---|
| 10 | * PIN CONFIG:
|
|---|
| 11 | * 1. ADC_POWER_DOWN_Pin auf 1 setzen -> ADS power up
|
|---|
| 12 | * 2. ADC_RESET_Pin auf 1 setzen -> ADS reset Zustand abschalten
|
|---|
| 13 | * 3. ADC_START_CONV_Pin auf 0 setzen -> ADS in Konfigurationsmodus ADC läuft nicht
|
|---|
| 14 | *
|
|---|
| 15 | * WARTEN AUF:
|
|---|
| 16 | * 4. // warten bis ADC_DATA_READY_Pin auf 1 ist -> wenn auf 1 ist dann ist der Chip bereit für Kommunikation //
|
|---|
| 17 | * wurde ersetzt durch einschalten des Data Ready Interrupts dieser löst bei fallender Flanke aus
|
|---|
| 18 | * die fallende Flanke wird generiert durch den ADS1260 wenn dieser mit der Data Conversion fertig ist.
|
|---|
| 19 | *
|
|---|
| 20 | * REGISTER CONFIG:
|
|---|
| 21 | * 5. interne Referenzspannung 2.500V wird eingeschaltet, lässt sich mit ADC vom STM32G0 messen
|
|---|
| 22 | * 6. Samplerate auf 10 sps- setzen
|
|---|
| 23 | * 7. Filter auf FIR setzen
|
|---|
| 24 | * 8. Conversion Mode auf Mode Pulse setzen -> nur eine Conversion wenn gestartet muss für jede neue Conversion neu aufgerufen werden
|
|---|
| 25 | * 9. Schalte AIN0 und AIN1 auf dem ADC
|
|---|
| 26 | * 10. Self Offset Calibration wird durchgeführt
|
|---|
| 27 | *
|
|---|
| 28 | * x. Setze die ADC Zustandsvariable auf ADC_STATE_CONVERSION_STOPPED
|
|---|
| 29 | *
|
|---|
| 30 | *
|
|---|
| 31 | *
|
|---|
| 32 | *
|
|---|
| 33 | * REGISTER SCHREIBEN:
|
|---|
| 34 | *
|
|---|
| 35 | * 1.Byte 0x40 + Register Adresse || 2.Byte 0xXX Daten
|
|---|
| 36 | * Bsp:
|
|---|
| 37 | * Code 0x40 + Register 0x06, Daten 0x10
|
|---|
| 38 | * => 1.Byte 0x46, => 2.Byte 0x10
|
|---|
| 39 | *
|
|---|
| 40 | *
|
|---|
| 41 | *
|
|---|
| 42 | ******************************************************************************/
|
|---|
| 43 |
|
|---|
| 44 | // --- INCLUDES -----------------------------------------------------------------
|
|---|
| 45 | #include "ads1260.h"
|
|---|
| 46 | #include "spi.h"
|
|---|
| 47 | //#include "math.h"
|
|---|
| 48 | #include "main.h"
|
|---|
| 49 | #include "eeprom.h"
|
|---|
| 50 | #include <stdio.h>
|
|---|
| 51 | #include "iwdg.h"
|
|---|
| 52 | // --- EXTERNE VARIABLEN --------------------------------------------------------
|
|---|
| 53 |
|
|---|
| 54 | // --- LOKALE DEFINES - bitte hier dokumentieren --------------------------------
|
|---|
| 55 |
|
|---|
| 56 | /*************************************************************************************************************/
|
|---|
| 57 |
|
|---|
| 58 | /*************************************************************************************************************/
|
|---|
| 59 | #define VOLTAGE (0)
|
|---|
| 60 | #define CURRENT (1)
|
|---|
| 61 | #define TEMPERATURE (2)
|
|---|
| 62 |
|
|---|
| 63 | #define DEFAULT_ADS1260_TRANSMIT_TIMEOUT (10)
|
|---|
| 64 | #define DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT (1000)
|
|---|
| 65 | #define ADS1260_SELF_OFFSET_CALIBRATION_TIMEOUT (2000) // > 16 * sampletime muss eingestellt werden
|
|---|
| 66 | #define ADS1260_SYSTEM_OFFSET_CALIBRATION_TIMEOUT (2000)
|
|---|
| 67 | #define ADS1260_GAIN_CALIBRATION_TIMEOUT (2000)
|
|---|
| 68 |
|
|---|
| 69 | #define COMMAND_POS (0)
|
|---|
| 70 | #define SEND_DATA_POS (1)
|
|---|
| 71 | #define RECEIVE_DATA_POS (2)
|
|---|
| 72 |
|
|---|
| 73 | #define SEND_DATA_NR_OF_BYTES (2)
|
|---|
| 74 | #define RECEIVE_DATA_NR_OF_BYTES (3)
|
|---|
| 75 | #define DATA_ARRAY_SIZE (3)
|
|---|
| 76 |
|
|---|
| 77 | #define REGISTER_READ_COMMAND (1 << 5)
|
|---|
| 78 | #define REGISTER_WRITE_COMMAND (1 << 6)
|
|---|
| 79 |
|
|---|
| 80 | #define SYSTEM_OFFSET_CALIBRATION_COMMAND (0x16)
|
|---|
| 81 | #define GAIN_CALIBRATION_COMMAND (0x17)
|
|---|
| 82 | #define SELF_OFFSET_CALIBRATION_COMMAND (0x19)
|
|---|
| 83 | // Register Number:
|
|---|
| 84 | #define DATA_RATE_REGISTER (0x02)
|
|---|
| 85 | #define DATA_RATE_2_5 (0b00000 << 3)
|
|---|
| 86 | #define DATA_RATE_5 (0b00001 << 3)
|
|---|
| 87 | #define DATA_RATE_10 (0b00010 << 3)
|
|---|
| 88 | #define DATA_RATE_16_6 (0b00011 << 3)
|
|---|
| 89 | #define DATA_RATE_20 (0b00100 << 3)/*Default*/
|
|---|
| 90 | #define DATA_RATE_50 (0b00101 << 3)
|
|---|
| 91 | #define DATA_RATE_60 (0b00110 << 3)
|
|---|
| 92 | #define DATA_RATE_100 (0b00111 << 3)
|
|---|
| 93 | #define DATA_RATE_400 (0b01000 << 3)
|
|---|
| 94 | #define DATA_RATE_1200 (0b01001 << 3)
|
|---|
| 95 | #define DATA_RATE_2400 (0b01010 << 3)
|
|---|
| 96 | #define DATA_RATE_4800 (0b01011 << 3)
|
|---|
| 97 | #define DATA_RATE_7200 (0b01100 << 3)
|
|---|
| 98 | #define DATA_RATE_14400 (0b01101 << 3)
|
|---|
| 99 | #define DATA_RATE_19200 (0b01110 << 3)
|
|---|
| 100 | #define DATA_RATE_25600 (0b01111 << 3)
|
|---|
| 101 | #define DATA_RATE_40000 (0b10000 << 3)
|
|---|
| 102 | #define DATA_RATE_RESET_MASK ~(0b11111 << 3)
|
|---|
| 103 |
|
|---|
| 104 | #define DIGITAL_FILTER_REGISTER (DATA_RATE_REGISTER)
|
|---|
| 105 | #define FILTER_SINC1 (0b000 << 0)
|
|---|
| 106 | #define FILTER_SINC2 (0b001 << 0)
|
|---|
| 107 | #define FILTER_SINC3 (0b010 << 0)
|
|---|
| 108 | #define FILTER_SINC4 (0b011 << 0)
|
|---|
| 109 | #define FILTER_FIR (0b100 << 0)/*Default*/
|
|---|
| 110 | #define FILTER_RESET_MASK ~(0b111 << 0)
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | #define CHOP_MODE_REGISTER (0x03)
|
|---|
| 114 | #define CHOP_MODE_NORMAL (0b00 << 5)/*Default*/
|
|---|
| 115 | #define CHOP_MODE_CHOP_MODE (0b01 << 5)
|
|---|
| 116 | #define CHOP_MODE_RESET_MASK ~(0b11 << 5)/*Default*/
|
|---|
| 117 |
|
|---|
| 118 | #define CONVERSION_MODE_REGISTER (CHOP_MODE_REGISTER)
|
|---|
| 119 | #define CONVERSION_MODE_CONTINIOUS (0 << 4)/*Default*/
|
|---|
| 120 | #define CONVERSION_MODE_PULSE (1 << 4)
|
|---|
| 121 | #define CONVERSION_MODE_RESET_MASK ~(1 << 4)
|
|---|
| 122 |
|
|---|
| 123 | #define CONVERSION_START_DELAY_REGISTER (CHOP_MODE_REGISTER)
|
|---|
| 124 | #define CONVERSION_START_DELAY_0u (0b0000 << 0)
|
|---|
| 125 | #define CONVERSION_START_DELAY_50u (0b0001 << 0)/*Default*/
|
|---|
| 126 | #define CONVERSION_START_DELAY_59u (0b0010 << 0)
|
|---|
| 127 | #define CONVERSION_START_DELAY_67u (0b0011 << 0)
|
|---|
| 128 | #define CONVERSION_START_DELAY_85u (0b0100 << 0)
|
|---|
| 129 | #define CONVERSION_START_DELAY_119u (0b0101 << 0)
|
|---|
| 130 | #define CONVERSION_START_DELAY_189u (0b0110 << 0)
|
|---|
| 131 | #define CONVERSION_START_DELAY_328u (0b0111 << 0)
|
|---|
| 132 | #define CONVERSION_START_DELAY_605u (0b1000 << 0)
|
|---|
| 133 | #define CONVERSION_START_DELAY_1_16m (0b1001 << 0)
|
|---|
| 134 | #define CONVERSION_START_DELAY_2_27m (0b1010 << 0)
|
|---|
| 135 | #define CONVERSION_START_DELAY_4_49m (0b1011 << 0)
|
|---|
| 136 | #define CONVERSION_START_DELAY_8_89m (0b1100 << 0)
|
|---|
| 137 | #define CONVERSION_START_DELAY_17_8m (0b1101 << 0)
|
|---|
| 138 | #define CONVERSION_START_RESET_MASK ~(0b1111 << 0)
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | #define REFERENCE_CONFIG_REGISTER (0x06)
|
|---|
| 143 | #define INTERNAL_REFERENCE_ENABLE (1 << 4)
|
|---|
| 144 | #define INTERNAL_REFERENCE_DISABLE (0 << 4)/*Default*/
|
|---|
| 145 | #define INTERNAL_REFERENCE_RESET_MASK ~(1 << 4)
|
|---|
| 146 |
|
|---|
| 147 | #define SELECT_POS_REFERENCE_INTERNAL (0b00 << 2)
|
|---|
| 148 | #define SELECT_POS_REFERENCE_AVDD (0b01 << 2)/*Default*/
|
|---|
| 149 | #define SELECT_POS_REFERENCE_AIN0 (0b10 << 2)
|
|---|
| 150 | #define SELECT_POS_REFERENCE_AIN2 (0b11 << 2)
|
|---|
| 151 |
|
|---|
| 152 | #define SELECT_NEG_REFERENCE_INTERNAL (0b00 << 0)
|
|---|
| 153 | #define SELECT_NEG_REFERENCE_AVSS (0b01 << 0)/*Default*/
|
|---|
| 154 | #define SELECT_NEG_REFERENCE_AIN1 (0b10 << 0)
|
|---|
| 155 | #define SELECT_NEG_REFERENCE_AIN3 (0b11 << 0)
|
|---|
| 156 |
|
|---|
| 157 | #define SELECT_REFERENCE_RESET_MASK ~(0b1111 << 0)
|
|---|
| 158 |
|
|---|
| 159 | #define OFFSET_CAL_LOW_BYTE_REG (0x07)
|
|---|
| 160 | #define OFFSET_CAL_MID_BYTE_REG (0x08)
|
|---|
| 161 | #define OFFSET_CAL_HIGH_BYTE_REG (0x09)
|
|---|
| 162 |
|
|---|
| 163 | #define FSCALE_CAL_LOW_BYTE_REG (0x0A)
|
|---|
| 164 | #define FSCALE_CAL_MID_BYTE_REG (0x0B)
|
|---|
| 165 | #define FSCALE_CAL_HIGH_BYTE_REG (0x0C)
|
|---|
| 166 |
|
|---|
| 167 | #define INPUT_MUX_REGISTER (0x11)
|
|---|
| 168 |
|
|---|
| 169 | #define POS_INPUT_MUX_SELECT_AINCOM (0b0000 << 4)
|
|---|
| 170 | #define POS_INPUT_MUX_SELECT_AIN0 (0b0001 << 4)
|
|---|
| 171 | #define POS_INPUT_MUX_SELECT_AIN1 (0b0010 << 4)
|
|---|
| 172 | #define POS_INPUT_MUX_SELECT_AIN2 (0b0011 << 4)
|
|---|
| 173 | #define POS_INPUT_MUX_SELECT_AIN3 (0b0100 << 4)
|
|---|
| 174 | #define POS_INPUT_MUX_SELECT_AIN4 (0b0101 << 4)
|
|---|
| 175 | #define POS_INPUT_MUX_SELECT_INT_TEMP_SENSOR_POS (0b1011 << 4)
|
|---|
| 176 | #define POS_INPUT_MUX_SELECT_INT_AVDD_DIV4_POS (0b1100 << 4)
|
|---|
| 177 | #define POS_INPUT_MUX_SELECT_INT_DVDD_DIV4_POS (0b1101 << 4)
|
|---|
| 178 | #define POS_INPUT_MUX_SELECT_INPUTS_OPEN (0b1110 << 4)
|
|---|
| 179 | #define POS_INPUT_MUX_SELECT_VCOM (0b1111 << 4)
|
|---|
| 180 |
|
|---|
| 181 | #define NEG_INPUT_MUX_SELECT_AINCOM (0b0000 << 4)
|
|---|
| 182 | #define NEG_INPUT_MUX_SELECT_AIN0 (0b0001 << 0)
|
|---|
| 183 | #define NEG_INPUT_MUX_SELECT_AIN1 (0b0010 << 0)
|
|---|
| 184 | #define NEG_INPUT_MUX_SELECT_AIN2 (0b0011 << 0)
|
|---|
| 185 | #define NEG_INPUT_MUX_SELECT_AIN3 (0b0100 << 0)
|
|---|
| 186 | #define NEG_INPUT_MUX_SELECT_AIN4 (0b0101 << 0)
|
|---|
| 187 | #define NEG_INPUT_MUX_SELECT_INT_TEMP_SENSOR_NEG (0b1011 << 0)
|
|---|
| 188 | #define NEG_INPUT_MUX_SELECT_INT_AVDD_DIV4_NEG (0b1100 << 0)
|
|---|
| 189 | #define NEG_INPUT_MUX_SELECT_INT_DVDD_DIV4_NEG (0b1101 << 0)
|
|---|
| 190 | #define NEG_INPUT_MUX_SELECT_INPUTS_OPEN (0b1110 << 0)
|
|---|
| 191 | #define NEG_INPUT_MUX_SELECT_VCOM (0b1111 << 0)
|
|---|
| 192 | #define INPUT_MUX_SELECT_RESET_MASK ~(0b00000000 << 0)
|
|---|
| 193 |
|
|---|
| 194 | // --- LOKALE TYPE DEFS - bitte hier dokumentieren-------------------------------
|
|---|
| 195 |
|
|---|
| 196 | // --- DEFINITIONEN GLOBALER VARIABLEN - Bitte in Header dokumentieren ----------
|
|---|
| 197 | uint32_t ahCounter[50];
|
|---|
| 198 | int32_t nrOfValuesCurrent;
|
|---|
| 199 | int32_t avgValWithOffsetCompensation;
|
|---|
| 200 | int32_t avgValWithOffsetCommonModeOffsetCorrection;
|
|---|
| 201 | int32_t avgValWithOffsetCommonModeOffsetTemperatureCorrection;
|
|---|
| 202 | double current;
|
|---|
| 203 | double currentWithGainCorrection;
|
|---|
| 204 | double currentWithGainAndGainShuntTempCorrection;
|
|---|
| 205 | //double currentWithGainAndGainShuntTempAndGainChipTempCorrection;
|
|---|
| 206 | // --- LOKALE VARIABLEN - bitte hier dokumentieren ------------------------------
|
|---|
| 207 | static adc_state_enum_t ads1260DataCoversionState;
|
|---|
| 208 |
|
|---|
| 209 | static const uint8_t RREG_BaseOpcode = 0x20; // Read Register CMD
|
|---|
| 210 | static const uint8_t WREG_BaseOpcode = 0x40; // Write Register CMD
|
|---|
| 211 | static const uint8_t LOCK_Opcode = 0xF2; // Lock registers modification CMD
|
|---|
| 212 | static const uint8_t RDATA_Opcode = 0x12; // Read conversion DATA CMD
|
|---|
| 213 |
|
|---|
| 214 | static const uint8_t MODE3_regAdr = 0x05; // MODE3 register address
|
|---|
| 215 | static const uint8_t MODE3_STATENB = 6U; // Status enable bit position in MODE3 register
|
|---|
| 216 | static const uint8_t MODE3_CRCENB = 5U; // CRC enable bit position in MODE3 register
|
|---|
| 217 |
|
|---|
| 218 | static const uint8_t STATUS_regAdr = 0x01;
|
|---|
| 219 | static const uint8_t STATUS_LOCK = 7U;
|
|---|
| 220 | static const uint8_t STATUS_CRCERR = 6U;
|
|---|
| 221 | static const uint8_t STATUS_REFL_ALM = 3U;
|
|---|
| 222 | static const uint8_t STATUS_DRDY = 2U;
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 | static const uint8_t arbitraryByte = 0xEC; // Don't care byte
|
|---|
| 226 | static const uint8_t replyHeader = 0xFF;
|
|---|
| 227 |
|
|---|
| 228 | // --- LOKALE FUNKTIONS PROTOTYPEN ----------------------------------------------
|
|---|
| 229 | static void ADS_1260_SetConversionMode(SPI_HandleTypeDef * hspi, uint8_t conversionMode);
|
|---|
| 230 | static void ADS_1260_SetInternalReference(SPI_HandleTypeDef * hspi);
|
|---|
| 231 | static void ADS_1260_SetExternalReference(SPI_HandleTypeDef * hspi);
|
|---|
| 232 | static void ADS_1260_InputMuxSelect(SPI_HandleTypeDef * hspi, uint8_t muxSelect);
|
|---|
| 233 | static void ADS_1260_SetChopMode(SPI_HandleTypeDef * hspi, uint8_t chopMode);
|
|---|
| 234 | static void ADS_1260_ActivateStatusData(void);
|
|---|
| 235 | static void ADS_1260_ActivateLock(void);
|
|---|
| 236 |
|
|---|
| 237 | static uint32_t ADS1260_ProcessCurrent(int32_t current);
|
|---|
| 238 | volatile uint32_t newCurrentValue=0;
|
|---|
| 239 | //static uint32_t ADS1260_ProcessVoltage(int32_t voltage, sys_data_t * data);
|
|---|
| 240 | //static uint32_t ADS1260_ProcessTemperature(int32_t temperature, sys_data_t * data);
|
|---|
| 241 |
|
|---|
| 242 | // Funktionen werden extern
|
|---|
| 243 |
|
|---|
| 244 | // --- LOKALE FUNKTIONEN - bitte hier dokumentieren -----------------------------
|
|---|
| 245 |
|
|---|
| 246 | /*
|
|---|
| 247 | * @brief Einstellung Conversion Mode
|
|---|
| 248 | * @param kein
|
|---|
| 249 | * @retval kein
|
|---|
| 250 | */
|
|---|
| 251 | static void ADS_1260_SetConversionMode(SPI_HandleTypeDef * hspi, uint8_t conversionMode)
|
|---|
| 252 | {
|
|---|
| 253 | uint8_t spiData[DATA_ARRAY_SIZE];
|
|---|
| 254 | // Read
|
|---|
| 255 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + CONVERSION_MODE_REGISTER);
|
|---|
| 256 | // HAL_GPIO_WritePin(SPI3_NSS_GPIO_Port, SPI3_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 257 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 258 | // HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 259 | // Modify
|
|---|
| 260 | spiData[SEND_DATA_POS] = ((spiData[RECEIVE_DATA_POS] & CONVERSION_MODE_RESET_MASK) | conversionMode); // so gefriemelt dass der Conversionsmodus gesetzt wird und der Rest des Registers unberührt beleibt
|
|---|
| 261 | // Write
|
|---|
| 262 | spiData[COMMAND_POS] = (REGISTER_WRITE_COMMAND + CONVERSION_MODE_REGISTER);
|
|---|
| 263 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 264 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, SEND_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 265 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 266 | // Read
|
|---|
| 267 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + CONVERSION_MODE_REGISTER);
|
|---|
| 268 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 269 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 270 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 271 | // Verify
|
|---|
| 272 | if((spiData[RECEIVE_DATA_POS] & conversionMode) != conversionMode)
|
|---|
| 273 | {
|
|---|
| 274 | printf("ERROR ADS_1260_SetConversionMode\n");
|
|---|
| 275 | while(1);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /*
|
|---|
| 281 | * @brief Einstellung Chop Mode
|
|---|
| 282 | * @param kein
|
|---|
| 283 | * @retval kein
|
|---|
| 284 | */
|
|---|
| 285 | static void ADS_1260_SetChopMode(SPI_HandleTypeDef * hspi, uint8_t chopMode)
|
|---|
| 286 | {
|
|---|
| 287 | uint8_t spiData[DATA_ARRAY_SIZE];
|
|---|
| 288 | // Read
|
|---|
| 289 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + CHOP_MODE_REGISTER);
|
|---|
| 290 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 291 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 292 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 293 | // Modify
|
|---|
| 294 | spiData[SEND_DATA_POS] = ((spiData[RECEIVE_DATA_POS] & CHOP_MODE_RESET_MASK) | chopMode); // so gefriemelt dass der Conversionsmodus gesetzt wird und der Rest des Registers unberührt beleibt
|
|---|
| 295 | // Write
|
|---|
| 296 | spiData[COMMAND_POS] = (REGISTER_WRITE_COMMAND + CHOP_MODE_REGISTER);
|
|---|
| 297 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 298 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, SEND_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 299 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 300 | // Read
|
|---|
| 301 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + CHOP_MODE_REGISTER);
|
|---|
| 302 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 303 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 304 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 305 | // Verify
|
|---|
| 306 | if((spiData[RECEIVE_DATA_POS] & chopMode) != chopMode)
|
|---|
| 307 | {
|
|---|
| 308 | printf("ERROR ADS_1260_SetChopMode\n");
|
|---|
| 309 | while(1);
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /*
|
|---|
| 314 | * @brief Einstellung Datarate
|
|---|
| 315 | * @param kein
|
|---|
| 316 | * @retval kein
|
|---|
| 317 | */
|
|---|
| 318 | void ADS_1260_SetDataRate(SPI_HandleTypeDef * hspi, uint8_t dataRate)
|
|---|
| 319 | {
|
|---|
| 320 | uint8_t spiData[DATA_ARRAY_SIZE];
|
|---|
| 321 | // Read
|
|---|
| 322 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + DATA_RATE_REGISTER);
|
|---|
| 323 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 324 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 325 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 326 | // Modify
|
|---|
| 327 | spiData[SEND_DATA_POS] = ((spiData[RECEIVE_DATA_POS] & DATA_RATE_RESET_MASK) | dataRate); // so gefriemelt dass die Datarate gesetzt wird und der Rest des Registers unberührt beleibt
|
|---|
| 328 | // Write
|
|---|
| 329 | spiData[COMMAND_POS] = (REGISTER_WRITE_COMMAND + DATA_RATE_REGISTER);
|
|---|
| 330 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 331 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, SEND_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 332 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 333 | // Read
|
|---|
| 334 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + DATA_RATE_REGISTER);
|
|---|
| 335 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 336 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 337 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 338 | // Verify
|
|---|
| 339 | if((spiData[RECEIVE_DATA_POS] & dataRate) != dataRate)
|
|---|
| 340 | {
|
|---|
| 341 | printf("ERROR ADS_1260_SetDataRate\n");
|
|---|
| 342 | while(1);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /*
|
|---|
| 348 | * @brief Einstellung Filtertyp
|
|---|
| 349 | * @param kein
|
|---|
| 350 | * @retval kein
|
|---|
| 351 | */
|
|---|
| 352 | void ADS_1260_SetDigitalFilter(SPI_HandleTypeDef * hspi, uint8_t digitalFilter)
|
|---|
| 353 | {
|
|---|
| 354 | uint8_t spiData[DATA_ARRAY_SIZE];
|
|---|
| 355 | // Read
|
|---|
| 356 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + DIGITAL_FILTER_REGISTER);
|
|---|
| 357 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 358 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 359 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 360 | // Modify
|
|---|
| 361 | spiData[SEND_DATA_POS] = ((spiData[RECEIVE_DATA_POS] & FILTER_RESET_MASK) | digitalFilter); // so gefriemelt dass der Filter gesetzt wird und der Rest des Registers unberührt beleibt
|
|---|
| 362 | // Write
|
|---|
| 363 | spiData[COMMAND_POS] = (REGISTER_WRITE_COMMAND + DIGITAL_FILTER_REGISTER);
|
|---|
| 364 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 365 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, SEND_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 366 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 367 | // Read
|
|---|
| 368 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + DIGITAL_FILTER_REGISTER);
|
|---|
| 369 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 370 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, RECEIVE_DATA_NR_OF_BYTES, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 371 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 372 | // Verify
|
|---|
| 373 | if((spiData[RECEIVE_DATA_POS] & digitalFilter) != digitalFilter)
|
|---|
| 374 | {
|
|---|
| 375 | printf("ERROR ADS_1260_SetDigitalFilter\n");
|
|---|
| 376 | while(1);
|
|---|
| 377 | }
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /*
|
|---|
| 381 | * @brief schaltet über die Mux die Eingänge auf den ADC
|
|---|
| 382 | * @param kein
|
|---|
| 383 | * @retval kein
|
|---|
| 384 | */
|
|---|
| 385 | static void ADS_1260_InputMuxSelect(SPI_HandleTypeDef * hspi, uint8_t muxSelect)
|
|---|
| 386 | {
|
|---|
| 387 | // Write
|
|---|
| 388 | uint8_t spiData[3] = {(REGISTER_WRITE_COMMAND + INPUT_MUX_REGISTER), muxSelect, 0};
|
|---|
| 389 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 390 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, 2, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 391 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 392 | // Read
|
|---|
| 393 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + INPUT_MUX_REGISTER);
|
|---|
| 394 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 395 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, 3, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 396 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 397 | // Verifie
|
|---|
| 398 | if(spiData[RECEIVE_DATA_POS] != muxSelect)
|
|---|
| 399 | {
|
|---|
| 400 | printf("ERROR ADS_1260_InputMuxSelect\n");
|
|---|
| 401 | // while(1);
|
|---|
| 402 | }
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 | /*
|
|---|
| 407 | * @brief schaltet die interne 2.500 Volt Referenzspannungsquelle ein
|
|---|
| 408 | * und wählt diese als Referenspannungsquelle aus
|
|---|
| 409 | * @param kein
|
|---|
| 410 | * @retval kein
|
|---|
| 411 | */
|
|---|
| 412 | static void ADS_1260_SetInternalReference(SPI_HandleTypeDef * hspi)
|
|---|
| 413 | {
|
|---|
| 414 | // Write
|
|---|
| 415 | uint8_t spiData[3] = {(REGISTER_WRITE_COMMAND + REFERENCE_CONFIG_REGISTER), (INTERNAL_REFERENCE_ENABLE + SELECT_POS_REFERENCE_INTERNAL + SELECT_NEG_REFERENCE_INTERNAL), 0};
|
|---|
| 416 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 417 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, 2, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 418 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 419 | // Read
|
|---|
| 420 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + REFERENCE_CONFIG_REGISTER);
|
|---|
| 421 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 422 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, 3, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 423 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 424 | // Verifie
|
|---|
| 425 | if(spiData[RECEIVE_DATA_POS] != (INTERNAL_REFERENCE_ENABLE + SELECT_POS_REFERENCE_INTERNAL + SELECT_NEG_REFERENCE_INTERNAL))
|
|---|
| 426 | {
|
|---|
| 427 | printf("ERROR ADS_1260_SetInternalReference\n");
|
|---|
| 428 | while(1);
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 | /*
|
|---|
| 435 | * @brief schaltet die interne 2.500 Volt Referenzspannungsquelle ein
|
|---|
| 436 | * und wählt diese als Referenspannungsquelle aus
|
|---|
| 437 | * @param kein
|
|---|
| 438 | * @retval kein
|
|---|
| 439 | */
|
|---|
| 440 | static void ADS_1260_SetExternalReference(SPI_HandleTypeDef * hspi)
|
|---|
| 441 | {
|
|---|
| 442 | // Write
|
|---|
| 443 | uint8_t spiData[3] = {(REGISTER_WRITE_COMMAND + REFERENCE_CONFIG_REGISTER), (INTERNAL_REFERENCE_DISABLE + SELECT_POS_REFERENCE_AIN0 + SELECT_NEG_REFERENCE_AIN1), 0};
|
|---|
| 444 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 445 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, 2, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 446 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 447 | // Read
|
|---|
| 448 | spiData[COMMAND_POS] = (REGISTER_READ_COMMAND + REFERENCE_CONFIG_REGISTER);
|
|---|
| 449 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 450 | HAL_SPI_TransmitReceive(hspi, spiData, spiData, 3, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 451 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 452 | // Verifie
|
|---|
| 453 | if(spiData[RECEIVE_DATA_POS] != (INTERNAL_REFERENCE_DISABLE + SELECT_POS_REFERENCE_AIN0 + SELECT_NEG_REFERENCE_AIN1))
|
|---|
| 454 | {
|
|---|
| 455 | printf("ERROR ADS_1260_SetInternalReference\n");
|
|---|
| 456 | while(1);
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 | /************************************************** KAL *****************************************************************/
|
|---|
| 466 | /*
|
|---|
| 467 | * @brief Software Offsetkalibrierung für die Strommessung.
|
|---|
| 468 | * Voraussetzungen: Es darf kein Strom über den Shunt fließen.
|
|---|
| 469 | * Warten bis Mittelwertbildung abgeschlossen ist.
|
|---|
| 470 | * @param kein
|
|---|
| 471 | * @retval kein
|
|---|
| 472 | */
|
|---|
| 473 | void ADS_1260_BatteryCurrentOffsetCalibrationStart(sys_data_t * data)
|
|---|
| 474 | {
|
|---|
| 475 | data->s.parameter.batteryCurrentOffset = data->s.values.battryCurrentRaw;
|
|---|
| 476 | data->s.parameter.batteryCurrentOffsetRefTemperatureShunt = data->s.values.shuntTemperature;
|
|---|
| 477 | data->s.parameter.batteryCurrentOffsetRefTemperatureChip = data->s.values.chipTemperature;
|
|---|
| 478 | data->s.parameter.batteryCurrentOffsetRefshuntVoltage = data->s.values.shuntVoltage;
|
|---|
| 479 | EEPROM_storeConfig(&sys_data,0);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | void ADS_1260_BatteryCurrentOffsetCommonModeErrorComepensationStart(sys_data_t * data)
|
|---|
| 483 | {
|
|---|
| 484 | //speichere geänderte CommonMode Spannung
|
|---|
| 485 | data->s.parameter.batteryCurrentOffsetCommonModeCalibrationVoltage = data->s.values.shuntVoltage;
|
|---|
| 486 |
|
|---|
| 487 | //Delta berechnen
|
|---|
| 488 | //Kompensationswert speichern in ADC Steps *1000 pro mV Common Mode Voltage
|
|---|
| 489 | int32_t deltaU = data->s.parameter.batteryCurrentOffsetCommonModeCalibrationVoltage - data->s.parameter.batteryCurrentOffsetRefshuntVoltage;
|
|---|
| 490 |
|
|---|
| 491 | //Entstandene Abweichung durch Common Mode Fehler, ist aktueller Messwert mit vorherigen Kompensationen
|
|---|
| 492 | int32_t deltaADC = avgValWithOffsetCompensation;
|
|---|
| 493 | int32_t compensationFactor = deltaADC * 1000 / deltaU;
|
|---|
| 494 | data->s.parameter.batteryCurrentOffsetCommonModeCompensationFactor = compensationFactor;
|
|---|
| 495 | EEPROM_storeConfig(&sys_data,0);
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | void ADS_1260_BatteryCurrentOffsetTemperatureErrorComepensationStart(void)
|
|---|
| 499 | {
|
|---|
| 500 | //speichere geänderte Temperatur
|
|---|
| 501 | //Achtung die Offset Kompeensation machen wir hier absichtlich mit der Chip Temperatur und nicht mit der Shunt Temperatur
|
|---|
| 502 | //Die Chip spiegeelt genaueer die Temperatur der ADC und Strommessverstärker wieder. Der Offset driftt hängt von der Temp der ADC/VREF/Messverstrker zusammen
|
|---|
| 503 | //und nicht mit der Temp der Shunt Widerstände
|
|---|
| 504 | sys_data.s.parameter.batteryCurrentOffsetTemperatureCalibrationTemperature = sys_data.s.values.chipTemperature;
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 | //Delta berechnen
|
|---|
| 508 | int32_t deltaT = sys_data.s.parameter.batteryCurrentOffsetTemperatureCalibrationTemperature - sys_data.s.parameter.batteryCurrentOffsetRefTemperatureChip;
|
|---|
| 509 | int32_t deltaADC = avgValWithOffsetCommonModeOffsetCorrection;
|
|---|
| 510 | int32_t compensationFactor = deltaADC * 1000 / deltaT;
|
|---|
| 511 | sys_data.s.parameter.batteryCurrentOffsetTemperatureCompensationFactor = compensationFactor;
|
|---|
| 512 | EEPROM_storeConfig(&sys_data,0);
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 | void ADS_1260_BatteryCurrentGainCalibrationStart(sys_data_t * data)
|
|---|
| 519 | {
|
|---|
| 520 | double helper;
|
|---|
| 521 | printf("--- Gain CAL ---");
|
|---|
| 522 | if(data->s.parameter.batteryCurrentGainRefCurrent == 0) // Fehler
|
|---|
| 523 | {
|
|---|
| 524 | printf("ADS_1260_BatteryCurrentGainCalibrationStart: ERROR IN CALIBRATION, NO REFERENCE CURRENT!\n");
|
|---|
| 525 | return;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 | // Sollstrom durch Batteriestrom teilen
|
|---|
| 531 | // Sollstrom ist in mA also umrechen in A, da Batteriestrom ("current") auch in A
|
|---|
| 532 | // ACHTUNG Das Punkt 0 ist wichtig, muss mit Fließkomma Berechnung durchgeführt werden!!!!
|
|---|
| 533 | helper = (data->s.parameter.batteryCurrentGainRefCurrent / 1000.0 ) / current;
|
|---|
| 534 | // in den Batteriegain umrechnen
|
|---|
| 535 | data->s.parameter.batteryCurrentGainCorrectionFaktor = (helper * 1000000.0);
|
|---|
| 536 | // schreibe Temperatur bei der kalibriert wurde
|
|---|
| 537 | data->s.parameter.batteryCurrentGainRefTempShunt = data->s.values.shuntTemperature;
|
|---|
| 538 | data->s.parameter.batteryCurrentGainRefTempChip = data->s.values.chipTemperature;
|
|---|
| 539 |
|
|---|
| 540 | printf("I (without compensation)=%f\n", current);
|
|---|
| 541 | printf("I Referenz=%f\n", data->s.parameter.batteryCurrentGainRefCurrent / 1000.0);
|
|---|
| 542 | printf("Tshunt=%f\n", data->s.parameter.batteryCurrentGainRefTempShunt/100.0);
|
|---|
| 543 | printf("Tship=%f\n", data->s.parameter.batteryCurrentGainRefTempChip/100.0);
|
|---|
| 544 | printf("Korrekturfaktor=%f\n", data->s.parameter.batteryCurrentGainCorrectionFaktor*1000000.0 );
|
|---|
| 545 | printf("--- Gain CAL ENDE---");
|
|---|
| 546 | EEPROM_storeConfig(&sys_data,0);
|
|---|
| 547 | }
|
|---|
| 548 | //Self Heat Kompensation
|
|---|
| 549 | void ADS_1260_BatteryCurrentGainTemperatureCalibrationShuntStart(void)
|
|---|
| 550 | {
|
|---|
| 551 | double helper;
|
|---|
| 552 | printf("--- Gain Drift CAL ---");
|
|---|
| 553 | //speichere aktuelle Temperatur
|
|---|
| 554 | sys_data.s.parameter.batteryCurrentGainTemperatureCalibrationShuntTemperature = sys_data.s.values.shuntTemperature;
|
|---|
| 555 | printf("Actual T=%f C\n", sys_data.s.parameter.batteryCurrentGainTemperatureCalibrationShuntTemperature/100.0);
|
|---|
| 556 | //Temperaturänderung berechnen
|
|---|
| 557 | int32_t deltaTShunt = ( sys_data.s.values.shuntTemperature - sys_data.s.parameter.batteryCurrentGainRefTempShunt);
|
|---|
| 558 | printf("delta T=%f C\n", deltaTShunt/100.0);
|
|---|
| 559 |
|
|---|
| 560 | helper = currentWithGainCorrection;
|
|---|
| 561 | printf("Acutal I=%f A(without gain temp drift correction)\n", currentWithGainCorrection);
|
|---|
| 562 | printf("Ref I=%f\n", sys_data.s.parameter.batteryCurrentGainRefCurrent/1000.0);
|
|---|
| 563 | // Sollstrom durch Batteriestrom teilen
|
|---|
| 564 | // wir erhalten den Korrektur Faktor für die aktuelle Temperatur
|
|---|
| 565 | helper = (sys_data.s.parameter.batteryCurrentGainRefCurrent/1000.0) / helper;
|
|---|
| 566 |
|
|---|
| 567 | // Speichere Korrekturfaktor pro Schritt Temperaturänderung
|
|---|
| 568 | helper = helper - 1.0;
|
|---|
| 569 |
|
|---|
| 570 | helper = helper / (deltaTShunt);
|
|---|
| 571 |
|
|---|
| 572 | //Speicher um Faktor 10000000 erhöht um Kommazahlen zu vermeiden
|
|---|
| 573 | sys_data.s.parameter.batteryCurrentGainTemperatureCompensationShuntFactor = helper*1000000000.0;
|
|---|
| 574 |
|
|---|
| 575 | printf("Korrekturfaktor=%f [ 1 / Celsius]\n", (sys_data.s.parameter.batteryCurrentGainTemperatureCompensationShuntFactor / 1000000000.0 * 100) + 1.0 );
|
|---|
| 576 | printf("--- Gain Drift CAL ENDE ---");
|
|---|
| 577 | EEPROM_storeConfig(&sys_data,0);
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | ////Ambient Temperature
|
|---|
| 581 | //void ADS_1260_BatteryCurrentGainTemperatureCalibrationChipStart()
|
|---|
| 582 | //{
|
|---|
| 583 | // double helper;
|
|---|
| 584 | // //speichere geänderte Temperatur
|
|---|
| 585 | // sys_data.s.parameter.batteryCurrentGainTemperatureCalibrationChipTemperature = sys_data.s.values.chipTemperature;
|
|---|
| 586 | // int32_t deltaT = sys_data.s.values.chipTemperature - sys_data.s.parameter.batteryCurrentGainRefTempChip;
|
|---|
| 587 | //
|
|---|
| 588 | //
|
|---|
| 589 | // helper = currentWithGainAndGainShuntTempCorrection;
|
|---|
| 590 | // // Sollstrom durch Batteriestrom teilen
|
|---|
| 591 | // // wir erhalten den Korrektur Faktor für die aktuelle Temperatur
|
|---|
| 592 | // helper = (sys_data.s.parameter.batteryCurrentGainRefCurrent/1000.0) / helper;
|
|---|
| 593 | //
|
|---|
| 594 | // // Speichere Korrekturfaktor pro Schritt Temperaturänderung
|
|---|
| 595 | // helper = helper - 1.0;
|
|---|
| 596 | //
|
|---|
| 597 | // helper = helper / deltaT;
|
|---|
| 598 | //
|
|---|
| 599 | // //Speicher um Faktor 10000000 erhöht um Kommazahlen zu vermeiden
|
|---|
| 600 | // sys_data.s.parameter.batteryCurrentGainTemperatureCompensationChipFactor = helper*1000000000.0;
|
|---|
| 601 | //
|
|---|
| 602 | //}
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 | /*
|
|---|
| 606 | * @brief Rohwerte ADC in Strom umrechnen
|
|---|
| 607 | * @param kein
|
|---|
| 608 | * @retval kein
|
|---|
| 609 | */
|
|---|
| 610 |
|
|---|
| 611 | #define BATTERY_CURRENT_FILTER 2
|
|---|
| 612 |
|
|---|
| 613 | static uint32_t ADS1260_ProcessCurrent(int32_t newval)
|
|---|
| 614 | {
|
|---|
| 615 | static signed long avgsum = 0;
|
|---|
| 616 | static int meas_counter;
|
|---|
| 617 | if (meas_counter < INT32_MAX) meas_counter++;
|
|---|
| 618 | int32_t avgval;
|
|---|
| 619 |
|
|---|
| 620 | // Filterlängen in 2er-Potenzen --> Compiler optimiert
|
|---|
| 621 | avgsum -= avgsum/ BATTERY_CURRENT_FILTER;
|
|---|
| 622 | avgsum += newval;
|
|---|
| 623 | avgval = avgsum / BATTERY_CURRENT_FILTER;
|
|---|
| 624 | sys_data.s.values.battryCurrentRaw = avgval;
|
|---|
| 625 | /**********************Offset Kompensation:*******************************/
|
|---|
| 626 | // Offset abziehen
|
|---|
| 627 | avgValWithOffsetCompensation = avgval - sys_data.s.parameter.batteryCurrentOffset;
|
|---|
| 628 | // Temperaturabhängiges Offset abziehen
|
|---|
| 629 | // in ADC Messwerten mal Abweichung von Referenttemperatur
|
|---|
| 630 | //current = current - ((sys_data.s.ads1260.s.offsetTemperatureFactorCurrent / 1000.0) * ((sys_data.s.device.parameter.shuntTemperature - sys_data.s.ads1260.s.refTempSoftwareOffsetCalibrationCurrent)/1000.0));
|
|---|
| 631 | /**********************Offset Kompensation:*******************************/
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 | /********************** START Common Mode Kompensation:*******************************/
|
|---|
| 635 | //Berechne Änderung der aktuellen Spannung am Shunt zu der Spannung am shunt bei Kalibrierung
|
|---|
| 636 | int32_t commonModeDeltaU = ((int32_t)sys_data.s.values.shuntVoltage - (int32_t)sys_data.s.parameter.batteryCurrentOffsetRefshuntVoltage) ;
|
|---|
| 637 | int32_t commonModeErrorAdcSteps = (commonModeDeltaU * sys_data.s.parameter.batteryCurrentOffsetCommonModeCompensationFactor) / 1000.0 ;
|
|---|
| 638 | sys_data.s.values.batteryCurrentOffsetCommonModeCorrectionADCSteps = commonModeErrorAdcSteps;
|
|---|
| 639 | avgValWithOffsetCommonModeOffsetCorrection = avgValWithOffsetCompensation - commonModeErrorAdcSteps;
|
|---|
| 640 | /********************** ENDE Common Mode Kompensation:*******************************/
|
|---|
| 641 |
|
|---|
| 642 | /********************** START Offset Temperature Kompensation*******************************/
|
|---|
| 643 | //Berechne Änderung der aktuellen Spannung am Shunt zu der Spannung am shunt bei Kalibrierung
|
|---|
| 644 | //Achtung wir arbeiten für die Offset Temperatur Kompenssation mit der Chip Temperatur, nicht mit der Shunt Temperatur, vgl Kal. Faunktion
|
|---|
| 645 | double temperatureDeltaT = ((int32_t)sys_data.s.values.chipTemperature - (int32_t) sys_data.s.parameter.batteryCurrentOffsetRefTemperatureChip);
|
|---|
| 646 | int32_t temperatureErrorAdcSteps = (temperatureDeltaT * sys_data.s.parameter.batteryCurrentOffsetTemperatureCompensationFactor) / 1000.0 ;
|
|---|
| 647 | avgValWithOffsetCommonModeOffsetTemperatureCorrection = avgValWithOffsetCommonModeOffsetCorrection - temperatureErrorAdcSteps;
|
|---|
| 648 | /********************** ENDE Offset Temperature Kompensation *******************************/
|
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 | // ADC Messwerte nach Mittwelwertbildung und Offset speichern
|
|---|
| 654 | //sys_data.s.ads1260.s.mwADCStepsWithOffsetCorrectionCurrent = current;
|
|---|
| 655 |
|
|---|
| 656 | // 250 resultiert aus 100µOhm Shunt + (Verstärkung Strommessverstärker = 20) * 2 -> Umrechnung in Strom
|
|---|
| 657 | // 200 resultiert aus 125µOhm Shunt + (Verstärkung Strommessverstärker = 20) * 2 -> Umrechnung in Strom
|
|---|
| 658 | // 2.5 = Vref, externe Referenz ist 3.0V
|
|---|
| 659 | // 0x800000 = ADC Auflösung
|
|---|
| 660 | #if (DEVICETYPE == 500)
|
|---|
| 661 | current = ((avgValWithOffsetCommonModeOffsetTemperatureCorrection * (double)3.0 * 200.0) / (double)0x800000);
|
|---|
| 662 | #elif (DEVICETYPE == 250)
|
|---|
| 663 | current = ((avgValWithOffsetCommonModeOffsetTemperatureCorrection * (double)3.0 * 100.0) / (double)0x800000);
|
|---|
| 664 | #elif (DEVICETYPE == 125)
|
|---|
| 665 | current = ((avgValWithOffsetCommonModeOffsetTemperatureCorrection * (double)3.0 * 50.0) / (double)0x800000);
|
|---|
| 666 | #else
|
|---|
| 667 | #error No valid device type
|
|---|
| 668 | #endif
|
|---|
| 669 | // Gain aus Sysdata
|
|---|
| 670 | currentWithGainCorrection = current * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktor / 1000000.0);
|
|---|
| 671 |
|
|---|
| 672 | /**********************Gain Temperatur Kompensation:*******************************/
|
|---|
| 673 | // Wenn sich in Abhängigkeit von der Temperatur das Gain ändert wird der Messwert mit einem Wert 1 +/- einem kleinen Faktor
|
|---|
| 674 | // der abhängig von der Temperaturabweichung ist multipliziert
|
|---|
| 675 | //ausgabe = ausgabe * ( 1 + ((sys_data.s.ads1260.s.gainTemperatureFactorCurrent * ((sys_data.s.device.parameter.shuntTemperature - sys_data.s.ads1260.s.refTempSoftwareGainCalibrationCurrent) / 1000.0) / 1000000000.0)));
|
|---|
| 676 | /**********************Gain Temperatur Kompensation:*******************************/
|
|---|
| 677 |
|
|---|
| 678 | #ifdef PRINT_BATTERY_CURRENT
|
|---|
| 679 | // Ausgabe runden auf %f.3
|
|---|
| 680 | printf("battery current = %.4fA\n", current);
|
|---|
| 681 | #endif
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 | double temperatureDeltaTShunt;
|
|---|
| 687 | //double temperatureDeltaTChip;
|
|---|
| 688 | temperatureDeltaTShunt = ((int32_t)sys_data.s.values.shuntTemperature - (int32_t) sys_data.s.parameter.batteryCurrentGainRefTempShunt);
|
|---|
| 689 | //temperatureDeltaTChip = ((int32_t)sys_data.s.values.chipTemperature - (int32_t) sys_data.s.parameter.batteryCurrentGainRefTempChip);
|
|---|
| 690 |
|
|---|
| 691 | // Gain Temperaturkompensation anwenden - Shunt
|
|---|
| 692 | double f = (sys_data.s.parameter.batteryCurrentGainTemperatureCompensationShuntFactor / 1000000000.0);
|
|---|
| 693 | double k = 1.0 + (temperatureDeltaTShunt * f);
|
|---|
| 694 | currentWithGainAndGainShuntTempCorrection = currentWithGainCorrection * k;
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 | // Gain Temperaturkompensation anwenden - Ambient
|
|---|
| 698 | //double f2 = (sys_data.s.parameter.batteryCurrentGainTemperatureCompensationChipFactor / 1000000000.0);
|
|---|
| 699 | //double k2 = 1.0 + ( temperatureDeltaTChip * f2);
|
|---|
| 700 | //k2=1; //Testabschaltung
|
|---|
| 701 | //currentWithGainAndGainShuntTempAndGainChipTempCorrection = currentWithGainAndGainShuntTempCorrection * k2;
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 | // printf("i=%f A. ist=%f, fs=%f, dTs=%f\n", currentWithGainCorrection, currentWithGainAndGainShuntTempCorrection, k, temperatureDeltaTShunt );
|
|---|
| 706 |
|
|---|
| 707 | //Endergebniss in mA speichern
|
|---|
| 708 | #if (DEVICETYPE == 500)
|
|---|
| 709 | if ((currentWithGainAndGainShuntTempCorrection > 550.0) || (currentWithGainAndGainShuntTempCorrection < -550.0))
|
|---|
| 710 | {
|
|---|
| 711 | sys_data.s.values.batteryCurrent = sys_data.s.values.fast_current;
|
|---|
| 712 | }
|
|---|
| 713 | else
|
|---|
| 714 | {
|
|---|
| 715 | sys_data.s.values.batteryCurrent = currentWithGainAndGainShuntTempCorrection * 1000.0;
|
|---|
| 716 | }
|
|---|
| 717 | #elif (DEVICETYPE == 250)
|
|---|
| 718 | if ((currentWithGainAndGainShuntTempCorrection > 275.0) || (currentWithGainAndGainShuntTempCorrection < -275.0))
|
|---|
| 719 | {
|
|---|
| 720 | sys_data.s.values.batteryCurrent = sys_data.s.values.fast_current;
|
|---|
| 721 | }
|
|---|
| 722 | else
|
|---|
| 723 | {
|
|---|
| 724 | sys_data.s.values.batteryCurrent = currentWithGainAndGainShuntTempCorrection * 1000.0;
|
|---|
| 725 | }
|
|---|
| 726 | #elif (DEVICETYPE == 125)
|
|---|
| 727 | if ((currentWithGainAndGainShuntTempCorrection > 137.0) || (currentWithGainAndGainShuntTempCorrection < -137.0))
|
|---|
| 728 | {
|
|---|
| 729 | sys_data.s.values.batteryCurrent = sys_data.s.values.fast_current;
|
|---|
| 730 | }
|
|---|
| 731 | else
|
|---|
| 732 | {
|
|---|
| 733 | sys_data.s.values.batteryCurrent = currentWithGainAndGainShuntTempCorrection * 1000.0;
|
|---|
| 734 | }
|
|---|
| 735 | #else
|
|---|
| 736 | #error No valid device type
|
|---|
| 737 | #endif
|
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 | if (meas_counter > (BATTERY_CURRENT_FILTER *10)) // Nur aktualiseren, wenn es schon ausreichend Messwerte gab
|
|---|
| 742 | {
|
|---|
| 743 | // höchster und niedrigster Stromwert werden gespeichert
|
|---|
| 744 | if(sys_data.s.values.batteryCurrent > sys_data.s.values.batteryCurrentMax)
|
|---|
| 745 | {
|
|---|
| 746 | sys_data.s.values.batteryCurrentMax = sys_data.s.values.batteryCurrent;
|
|---|
| 747 | }
|
|---|
| 748 | if(sys_data.s.values.batteryCurrent < sys_data.s.values.batteryCurrentMin)
|
|---|
| 749 | {
|
|---|
| 750 | sys_data.s.values.batteryCurrentMin = sys_data.s.values.batteryCurrent;
|
|---|
| 751 | }
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | newCurrentValue=1;
|
|---|
| 755 |
|
|---|
| 756 | return 0;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 | // --- GLOBALE FUNKTIONEN - bitte in Header dokumentieren------------------------
|
|---|
| 761 |
|
|---|
| 762 | void ADS1260_init(void)
|
|---|
| 763 | {
|
|---|
| 764 | uint8_t sdata[10] = {0x47,0x00,0x00,0x00,0x00,0x00};
|
|---|
| 765 | /* 0*/ ads1260DataCoversionState = ADC_STATE_INITIALIZE;
|
|---|
| 766 | /* 3*/ HAL_GPIO_WritePin(ADC_START_CONV_GPIO_Port, ADC_START_CONV_Pin, GPIO_PIN_SET);
|
|---|
| 767 | HAL_Delay(150); // Delay weil die Vref braucht zeit um sich zu stabilisieren (siehe Datenblatt Seite 9)
|
|---|
| 768 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 769 | /* 1*/ //HAL_GPIO_WritePin(ADC_POWER_DOWN_GPIO_Port, ADC_POWER_DOWN_Pin, GPIO_PIN_RESET);
|
|---|
| 770 | //HAL_Delay(150); // Delay weil die Vref braucht zeit um sich zu stabilisieren (siehe Datenblatt Seite 9)
|
|---|
| 771 | /* 1*/ //HAL_GPIO_WritePin(ADC_POWER_DOWN_GPIO_Port, ADC_POWER_DOWN_Pin, GPIO_PIN_SET);
|
|---|
| 772 | //HAL_Delay(150); // Delay weil die Vref braucht zeit um sich zu stabilisieren (siehe Datenblatt Seite 9)
|
|---|
| 773 | /* 2*/ HAL_GPIO_WritePin(ADC_RESET_GPIO_Port, ADC_RESET_Pin, GPIO_PIN_RESET);
|
|---|
| 774 | HAL_Delay(150); // Delay weil die Vref braucht zeit um sich zu stabilisieren (siehe Datenblatt Seite 9)
|
|---|
| 775 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 776 | /* 2*/ HAL_GPIO_WritePin(ADC_RESET_GPIO_Port, ADC_RESET_Pin, GPIO_PIN_SET);
|
|---|
| 777 | HAL_Delay(150); // Delay weil die Vref braucht zeit um sich zu stabilisieren (siehe Datenblatt Seite 9)
|
|---|
| 778 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 779 | /* 3*/ HAL_GPIO_WritePin(ADC_START_CONV_GPIO_Port, ADC_START_CONV_Pin, GPIO_PIN_RESET);
|
|---|
| 780 |
|
|---|
| 781 | /* 4*/ //while(HAL_GPIO_ReadPin(ADC_DATA_READY_GPIO_Port, ADC_DATA_READY_Pin) == GPIO_PIN_RESET);
|
|---|
| 782 | //HAL_NVIC_SetPriority(EXTI2_IRQn, 2, 0);
|
|---|
| 783 | //HAL_NVIC_EnableIRQ(EXTI2_IRQn);
|
|---|
| 784 |
|
|---|
| 785 | /* 5*/ ADS_1260_SetExternalReference(&hspi3);
|
|---|
| 786 | HAL_Delay(150);
|
|---|
| 787 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 788 | /* 6*/ ADS_1260_SetDataRate(&hspi3, DATA_RATE_20);
|
|---|
| 789 | // /* 7*/ ADS_1260_SetDigitalFilter(&hspi1, FILTER_SINC4);
|
|---|
| 790 | /* 8*/ ADS_1260_SetConversionMode(&hspi3, CONVERSION_MODE_CONTINIOUS);
|
|---|
| 791 | // langsamer
|
|---|
| 792 | ADS_1260_SetChopMode(&hspi3, CHOP_MODE_CHOP_MODE);
|
|---|
| 793 | ADS_1260_InputMuxSelect(&hspi3, POS_INPUT_MUX_SELECT_AIN2 + NEG_INPUT_MUX_SELECT_AIN3);
|
|---|
| 794 |
|
|---|
| 795 | ADS_1260_ActivateStatusData();
|
|---|
| 796 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 797 | ADS_1260_ActivateLock();
|
|---|
| 798 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 799 |
|
|---|
| 800 | /*10*/ //ADS_1260_SelfOffsetCalibration(&hspi1);
|
|---|
| 801 | HAL_Delay(150);
|
|---|
| 802 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 803 | /*x*/ ads1260DataCoversionState = ADC_STATE_READY_FOR_CONVERSION;
|
|---|
| 804 | ADS1260_StartConversion();
|
|---|
| 805 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 806 | }
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 | void ADS1260_StartConversion(void)
|
|---|
| 810 | {
|
|---|
| 811 | HAL_GPIO_WritePin(ADC_START_CONV_GPIO_Port, ADC_START_CONV_Pin, GPIO_PIN_SET);
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | void ADS1260_ReadConversion(void)
|
|---|
| 815 | {
|
|---|
| 816 | extern CRC_HandleTypeDef hcrc;
|
|---|
| 817 | convert_union_t convert;
|
|---|
| 818 |
|
|---|
| 819 | // CRC2
|
|---|
| 820 | uint8_t spiDataIn[9] = { RDATA_Opcode, arbitraryByte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 821 | spiDataIn[2] = HAL_CRC_Calculate(&hcrc, (uint32_t*) spiDataIn, 2);
|
|---|
| 822 | uint8_t spiDataOut[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 823 |
|
|---|
| 824 | int32_t value = 0;
|
|---|
| 825 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 826 | HAL_SPI_TransmitReceive(&hspi3, spiDataIn, spiDataOut, 9, DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 827 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 828 |
|
|---|
| 829 | if (spiDataOut[0] == replyHeader && spiDataOut[1] == spiDataIn[0] && spiDataOut[2] == spiDataIn[1] && spiDataOut[3] == spiDataIn[2] && spiDataOut[8] == HAL_CRC_Calculate(&hcrc, (uint32_t*) &spiDataOut[4], 4))
|
|---|
| 830 | {
|
|---|
| 831 | uint8_t STATUS_reg = spiDataOut[4];
|
|---|
| 832 |
|
|---|
| 833 | if ((STATUS_reg & (1 << STATUS_LOCK)) && (STATUS_reg & (1 << STATUS_DRDY)) && !(STATUS_reg & (1 << STATUS_CRCERR)) && !(STATUS_reg & (1 << STATUS_REFL_ALM)))
|
|---|
| 834 | {
|
|---|
| 835 | // Rohwerte Byteswitch
|
|---|
| 836 | convert.s[3] = 0;
|
|---|
| 837 | convert.s[2] = spiDataOut[5];
|
|---|
| 838 | convert.s[1] = spiDataOut[6];
|
|---|
| 839 | convert.s[0] = spiDataOut[7];
|
|---|
| 840 |
|
|---|
| 841 | // Vorzeichen ausrechnen (24 bit MSB = Vorzeichenbit muss auf 32 bit umgesetzt werden)
|
|---|
| 842 | if(convert.w >= 0x800000)
|
|---|
| 843 | {
|
|---|
| 844 | convert.sw = -(0xFFFFFF - convert.w);
|
|---|
| 845 | value = convert.sw;
|
|---|
| 846 | }
|
|---|
| 847 | else if(convert.w < 0x800000)
|
|---|
| 848 | {
|
|---|
| 849 | //convert.sw = convert.w;
|
|---|
| 850 | value = convert.w;
|
|---|
| 851 | }
|
|---|
| 852 | }
|
|---|
| 853 | else
|
|---|
| 854 | {
|
|---|
| 855 | sys_data.s.values.adc_restarts++;
|
|---|
| 856 | ADS1260_init();
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | }
|
|---|
| 860 | else
|
|---|
| 861 | {
|
|---|
| 862 | sys_data.s.values.adc_restarts++;
|
|---|
| 863 | ADS1260_init();
|
|---|
| 864 | }
|
|---|
| 865 |
|
|---|
| 866 | ADS1260_ProcessCurrent(value);
|
|---|
| 867 | }
|
|---|
| 868 |
|
|---|
| 869 | //-----------------------------------------------------------------------------
|
|---|
| 870 |
|
|---|
| 871 | static void ADS_1260_ActivateLock(void)
|
|---|
| 872 | {
|
|---|
| 873 | extern CRC_HandleTypeDef hcrc;
|
|---|
| 874 | const int maxReTries = 5;
|
|---|
| 875 | int lockIsWritten = 0;
|
|---|
| 876 |
|
|---|
| 877 | for (int i = 0; i < maxReTries; i++)
|
|---|
| 878 | {
|
|---|
| 879 | // Sendin LOCK command CRC2
|
|---|
| 880 | uint8_t Din[] = { LOCK_Opcode, arbitraryByte, 0x00, 0x00 };
|
|---|
| 881 | Din[2] = HAL_CRC_Calculate(&hcrc, (uint32_t*) Din, 2);
|
|---|
| 882 | uint8_t Dout[] = { 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 883 |
|
|---|
| 884 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 885 | HAL_SPI_TransmitReceive(&hspi3, Din, Dout, sizeof(Din) / sizeof(Din[0]), DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 886 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 887 |
|
|---|
| 888 | if (Dout[0] == replyHeader && Dout[1] == Din[0] && Dout[2] == Din[1] && Dout[3] == Din[2])
|
|---|
| 889 | {
|
|---|
| 890 | lockIsWritten = 1;
|
|---|
| 891 | break;
|
|---|
| 892 | }
|
|---|
| 893 | else continue;
|
|---|
| 894 | }
|
|---|
| 895 |
|
|---|
| 896 | if (!lockIsWritten)
|
|---|
| 897 | while (1)
|
|---|
| 898 | { // Blink the RED LED forever
|
|---|
| 899 | HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
|
|---|
| 900 | HAL_Delay(350);
|
|---|
| 901 | }
|
|---|
| 902 |
|
|---|
| 903 | int lockIsWrittenCorrect = 0;
|
|---|
| 904 | // Reading STATUS register to make sure that LOCK is active
|
|---|
| 905 | for (int i = 0; i < maxReTries; i++)
|
|---|
| 906 | {
|
|---|
| 907 | // Reading the content of the STATUS register CRC2
|
|---|
| 908 | uint8_t Din[] = { RREG_BaseOpcode | STATUS_regAdr, arbitraryByte, 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 909 | Din[2] = HAL_CRC_Calculate(&hcrc, (uint32_t*) Din, 2);
|
|---|
| 910 | uint8_t Dout[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 911 |
|
|---|
| 912 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 913 | HAL_SPI_TransmitReceive(&hspi3, Din, Dout, sizeof(Din) / sizeof(Din[0]), DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 914 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 915 |
|
|---|
| 916 | if (Dout[0] == replyHeader && Dout[1] == Din[0] && Dout[2] == Din[1] && Dout[3] == Din[2] && Dout[5] == HAL_CRC_Calculate(&hcrc, (uint32_t*)&Dout[4], 1))
|
|---|
| 917 | {
|
|---|
| 918 | uint8_t STATUS_reg = Dout[4];
|
|---|
| 919 | if (STATUS_reg & (1U << STATUS_LOCK))
|
|---|
| 920 | {
|
|---|
| 921 | lockIsWrittenCorrect = 1;
|
|---|
| 922 | break;
|
|---|
| 923 | }
|
|---|
| 924 | }
|
|---|
| 925 | else continue;
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | if (!lockIsWrittenCorrect)
|
|---|
| 929 | while (1)
|
|---|
| 930 | { // Blink the RED LED forever
|
|---|
| 931 | HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
|
|---|
| 932 | HAL_Delay(400);
|
|---|
| 933 | }
|
|---|
| 934 |
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | //-----------------------------------------------------------------------------
|
|---|
| 938 |
|
|---|
| 939 | static void ADS_1260_ActivateStatusData(void)
|
|---|
| 940 | {
|
|---|
| 941 | extern CRC_HandleTypeDef hcrc;
|
|---|
| 942 | const int maxReTries = 5;
|
|---|
| 943 | int mode3IsRead = 0;
|
|---|
| 944 | uint8_t MODE3_Reg;
|
|---|
| 945 |
|
|---|
| 946 | for (int i = 0; i < maxReTries; i++)
|
|---|
| 947 | {
|
|---|
| 948 | // Reading the content of the MODE3 register
|
|---|
| 949 | uint8_t Din[] = { RREG_BaseOpcode | MODE3_regAdr, arbitraryByte, 0x00 };
|
|---|
| 950 | uint8_t Dout[] = { 0x00, 0x00, 0x00 };
|
|---|
| 951 |
|
|---|
| 952 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 953 | HAL_SPI_TransmitReceive(&hspi3, Din, Dout, sizeof(Din) / sizeof(Din[0]), DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 954 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 955 |
|
|---|
| 956 | if (Dout[0] == replyHeader && Dout[1] == Din[0])
|
|---|
| 957 | {
|
|---|
| 958 | MODE3_Reg = Dout[2]; // Saving the content of the MODE3 register
|
|---|
| 959 | mode3IsRead = 1;
|
|---|
| 960 | break;
|
|---|
| 961 | }
|
|---|
| 962 | else continue;
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | if (!mode3IsRead)
|
|---|
| 966 | while (1)
|
|---|
| 967 | { // Blink the RED LED forever
|
|---|
| 968 | HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
|
|---|
| 969 | HAL_Delay(200);
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | // Setting STATENB and CRCENB bits in MODE3 register
|
|---|
| 973 | MODE3_Reg |= (1U << MODE3_STATENB) | (1U << MODE3_CRCENB);
|
|---|
| 974 |
|
|---|
| 975 | int mode3IsWritten = 0;
|
|---|
| 976 |
|
|---|
| 977 | for (int i = 0; i < maxReTries; i++)
|
|---|
| 978 | {
|
|---|
| 979 | // Writing back the content of the MODE3 register
|
|---|
| 980 | uint8_t Din[] = { WREG_BaseOpcode | MODE3_regAdr, MODE3_Reg };
|
|---|
| 981 | uint8_t Dout[] = { 0x00, 0x00 };
|
|---|
| 982 |
|
|---|
| 983 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 984 | HAL_SPI_TransmitReceive(&hspi3, Din, Dout, sizeof(Din) / sizeof(Din[0]), DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 985 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 986 |
|
|---|
| 987 | if (Dout[0] == replyHeader && Dout[1] == Din[0])
|
|---|
| 988 | {
|
|---|
| 989 | mode3IsWritten = 1;
|
|---|
| 990 | break;
|
|---|
| 991 | }
|
|---|
| 992 | else continue;
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | if (!mode3IsWritten)
|
|---|
| 996 | while (1)
|
|---|
| 997 | { // Blink the RED LED forever
|
|---|
| 998 | HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
|
|---|
| 999 | HAL_Delay(250);
|
|---|
| 1000 | }
|
|---|
| 1001 |
|
|---|
| 1002 | int mode3IsWrittenCorrect = 0;
|
|---|
| 1003 |
|
|---|
| 1004 | // We have activated CRC in every data packet, so we need take it into account
|
|---|
| 1005 | for (int i = 0; i < maxReTries; i++)
|
|---|
| 1006 | {
|
|---|
| 1007 | // Reading one more time the content of the MODE3 register CRC2
|
|---|
| 1008 | uint8_t Din[] = { RREG_BaseOpcode | MODE3_regAdr, arbitraryByte, 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 1009 | Din[2] = HAL_CRC_Calculate(&hcrc, (uint32_t*) Din, 2);
|
|---|
| 1010 | uint8_t Dout[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|---|
| 1011 |
|
|---|
| 1012 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_RESET);
|
|---|
| 1013 | HAL_SPI_TransmitReceive(&hspi3, Din, Dout, sizeof(Din) / sizeof(Din[0]), DEFAULT_ADS1260_TRANSMIT_RECEIVE_TIMEOUT);
|
|---|
| 1014 | //HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_NSS_Pin, GPIO_PIN_SET);
|
|---|
| 1015 |
|
|---|
| 1016 | if (Dout[0] == replyHeader && Dout[1] == Din[0] && Dout[2] == Din[1] && Dout[3] == Din[2] && Dout[5] == HAL_CRC_Calculate(&hcrc, (uint32_t*)&Dout[4], 1))
|
|---|
| 1017 | {
|
|---|
| 1018 | if ((Dout[4] & (1U << MODE3_STATENB)) && (Dout[4] & (1U << MODE3_CRCENB)))
|
|---|
| 1019 | {
|
|---|
| 1020 | mode3IsWrittenCorrect = 1;
|
|---|
| 1021 | break;
|
|---|
| 1022 | }
|
|---|
| 1023 | }
|
|---|
| 1024 | else continue;
|
|---|
| 1025 | }
|
|---|
| 1026 |
|
|---|
| 1027 | if (!mode3IsWrittenCorrect)
|
|---|
| 1028 | while (1)
|
|---|
| 1029 | { // Blink the RED LED forever
|
|---|
| 1030 | HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
|
|---|
| 1031 | HAL_Delay(300);
|
|---|
| 1032 | }
|
|---|
| 1033 | }
|
|---|
| 1034 |
|
|---|
| 1035 | //-----------------------------------------------------------------------------
|
|---|
| 1036 |
|
|---|
| 1037 | void ADS1260_ConversionFinished(void)
|
|---|
| 1038 | {
|
|---|
| 1039 | ADS1260_ReadConversion();
|
|---|
| 1040 | // ADS1260_StartConversion();
|
|---|
| 1041 | }
|
|---|
| 1042 |
|
|---|
| 1043 | //-----------------------------------------------------------------------------
|
|---|