From 50be1de24e0f09f51dc06763f2649dff8270da96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TED=C3=BC?= Date: Sat, 9 Mar 2024 15:33:12 +0100 Subject: [PATCH] working sensor readout for 1 block --- CANdb/TTS_5zone.dbc | 77 ++++++++++++++++ Core/Inc/HTPA_32x32d.h | 11 ++- Core/Src/HTPA_32x32d.c | 195 ++++++++++++++++++++++++++--------------- Core/Src/main.c | 11 +-- TTS.ioc | 14 --- 5 files changed, 209 insertions(+), 99 deletions(-) create mode 100644 CANdb/TTS_5zone.dbc diff --git a/CANdb/TTS_5zone.dbc b/CANdb/TTS_5zone.dbc new file mode 100644 index 0000000..443c9b4 --- /dev/null +++ b/CANdb/TTS_5zone.dbc @@ -0,0 +1,77 @@ +VERSION "" + + +NS_ : + NS_DESC_ + CM_ + BA_DEF_ + BA_ + VAL_ + CAT_DEF_ + CAT_ + FILTER + BA_DEF_DEF_ + EV_DATA_ + ENVVAR_DATA_ + SGTYPE_ + SGTYPE_VAL_ + BA_DEF_SGTYPE_ + BA_SGTYPE_ + SIG_TYPE_REF_ + VAL_TABLE_ + SIG_GROUP_ + SIG_VALTYPE_ + SIGTYPE_VALTYPE_ + BO_TX_BU_ + BA_DEF_REL_ + BA_REL_ + BA_DEF_DEF_REL_ + BU_SG_REL_ + BU_EV_REL_ + BU_BO_REL_ + SG_MUL_VAL_ + +BS_: + +BU_: + + +BO_ 3 TTS_RR: 8 Vector__XXX + SG_ Status : 60|4@1+ (1,0) [0|0] "" Vector__XXX + SG_ Right_Outer : 48|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Right_Inner : 36|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Center : 24|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Inner : 12|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Outer : 0|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + +BO_ 2 TTS_RL: 8 Vector__XXX + SG_ Status : 60|4@1+ (1,0) [0|0] "" Vector__XXX + SG_ Right_Outer : 48|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Right_Inner : 36|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Center : 24|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Inner : 12|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Outer : 0|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + +BO_ 1 TTS_FR: 8 Vector__XXX + SG_ Status : 60|4@1+ (1,0) [0|0] "" Vector__XXX + SG_ Right_Outer : 48|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Right_Inner : 36|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Center : 24|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Inner : 12|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Outer : 0|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + +BO_ 0 TTS_FL: 8 Vector__XXX + SG_ Status : 60|4@1+ (1,0) [0|0] "" Vector__XXX + SG_ Right_Outer : 48|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Right_Inner : 36|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Center : 24|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Inner : 12|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + SG_ Left_Outer : 0|12@1- (0.1,0) [-204.8|204.7] "°C" Vector__XXX + + + +BA_DEF_ "MultiplexExtEnabled" ENUM "No","Yes"; +BA_DEF_ "BusType" STRING ; +BA_DEF_DEF_ "MultiplexExtEnabled" "No"; +BA_DEF_DEF_ "BusType" "CAN"; + diff --git a/Core/Inc/HTPA_32x32d.h b/Core/Inc/HTPA_32x32d.h index 059efd0..12ebd0f 100644 --- a/Core/Inc/HTPA_32x32d.h +++ b/Core/Inc/HTPA_32x32d.h @@ -27,11 +27,14 @@ typedef struct { bool eoc; // end of conversion flag } HTPA_Status; + + void HTPA_Init(I2C_HandleTypeDef *); -void HTPA_ReadBlock(uint8_t, uint8_t, uint8_t blockData[256]); -void HTPA_WriteRegister(uint8_t, uint8_t); -uint8_t HTPA_ReadRegister(uint8_t register_address); -HTPA_Status HTPA_GetStatus(void); +void HTPA_ReadSensor(void); + +void HTPA_WriteRegister(uint8_t address, uint8_t byte); +void HTPA_ReadRegister(uint8_t address, uint8_t* pData, uint16_t length); +void HTPA_GetStatus(void); #endif /* INC_HTPA_32X32D_H_ */ diff --git a/Core/Src/HTPA_32x32d.c b/Core/Src/HTPA_32x32d.c index dfb2c94..627302b 100644 --- a/Core/Src/HTPA_32x32d.c +++ b/Core/Src/HTPA_32x32d.c @@ -3,18 +3,15 @@ * @brief Library for HTPA 32x32d infrared array sensor * @author Tim-Erik Düntzsch t.duentzsch@fasttube.de * - * @date 25.03.2023 - first implementation (untested) + * @date 08.03.2024 - successful readout of block 3 top half and conversion factors * - * @todo finish HTPA_ReadBlock function - * @todo add calibration function - * @todo add temperature conversion function + * @test eeprom readout and temperature conversion * - * @test communication and readout - * - * @version 0.1 + * @version 0.7 */ #include +#include #include "main.h" #include "HTPA_32x32d.h" @@ -36,14 +33,20 @@ #define HTPA_SENSOR_READTOP 0x0A // Read top half #define HTPA_SENSOR_READBOT 0x0B // Read bot half // I2C transmit delay -#define I2C_MAX_DELAY 0xFFFFFFFF +#define HTPA_I2C_MAX_DELAY 0xFFFFFFFF -I2C_HandleTypeDef* i2c_handle; // pointer to i2c handle -HAL_StatusTypeDef i2c_return; // error handling maybe? +I2C_HandleTypeDef* htpa_hi2c; // pointer to i2c handle -uint8_t data_topBlock[256]; -uint8_t data_botBlock[256]; -uint8_t htpa_statusReg; +HTPA_Status htpa_statusReg; + +uint8_t data_topBlock[258]; +uint8_t elOffset_topBlock[258]; +//uint8_t data_botBlock[258]; + +uint16_t vdd_topBlock; +uint16_t ptat_topBlock; + +uint16_t pixel_topBlock[4][32]; /** * @brief Initialization of HTPA Sensor @@ -55,45 +58,108 @@ uint8_t htpa_statusReg; * @param *hi2c: Pointer to I2C Handle */ void HTPA_Init(I2C_HandleTypeDef *hi2c){ - i2c_handle = hi2c; + htpa_hi2c = hi2c; + /* + * Read EEPROM calibration values + * + HAL_I2C_DeInit(htpa_hi2c); + htpa_hi2c->Init->Timing = 0; // set I2C frequency to 400kHz + HAL_I2C_Init(htpa_hi2c); + EEPROM auslesen: + HTPA_ReadEEPROM( + */ + + + /* + * Write sensor calibration registers + * + HAL_I2C_DeInit(htpa_hi2c); + htpa_hi2c->Init->Timing = 0; // set I2C frequency to 1MHz + HAL_I2C_Init(htpa_hi2c); + */ + // Berechnung für clk / sample aus I2C parametern? HTPA_WriteRegister(HTPA_SENSOR_CONFIG, 0x01); // wakeup - HTPA_WriteRegister(HTPA_SENSOR_TRIM_1, 0x0C); + HAL_Delay(10); + HTPA_WriteRegister(HTPA_SENSOR_TRIM_1, 0x0C); // bit 5,4 = 00 -> amplification = 0, bit 3-0 = 1100 -> 16bit ADC-Resolution (4 + m=12) + HAL_Delay(10); HTPA_WriteRegister(HTPA_SENSOR_TRIM_2, 0x0C); + HAL_Delay(10); HTPA_WriteRegister(HTPA_SENSOR_TRIM_3, 0x0C); - HTPA_WriteRegister(HTPA_SENSOR_TRIM_4, 0x14); + HAL_Delay(10); + HTPA_WriteRegister(HTPA_SENSOR_TRIM_4, 0x14); // clock frequency set to 0x14 -> 4.75MHz -> time for quarter frame: ~27ms + HAL_Delay(10); HTPA_WriteRegister(HTPA_SENSOR_TRIM_5, 0x0C); + HAL_Delay(10); HTPA_WriteRegister(HTPA_SENSOR_TRIM_6, 0x0C); + HAL_Delay(10); HTPA_WriteRegister(HTPA_SENSOR_TRIM_7, 0x88); - HTPA_WriteRegister(HTPA_SENSOR_CONFIG, 0x09); // start sensor + HAL_Delay(10); + //HTPA_WriteRegister(HTPA_SENSOR_CONFIG, 0x09); // start sensor + //HAL_Delay(10); } -/** - * @brief Read data of one block in one array half - * - * Block selection gets written to the config register and conversion is started. - * After selected block of upper and lower half are converted, the selected half - * is read and the data stored in the passed array. - * - * @param array_half: select top or bottom half of array - * @param block: select block of array half (0-3) - * @param *pData[128]: pointer to 128 word array for read data -*/ -void HTPA_ReadBlock(uint8_t array_half, uint8_t block, uint8_t blockData[256]){ +void HTPA_ReadSensor(void) { uint8_t config = 0; - uint8_t statusReg = 0; - //config = (block << 4); // bit 5,4 block - config |= 0x09; // bit 3 start | bit 1 wakeup + /* + * Read top array half of block3 with PTAT + */ + // write block and vdd/ptat selection to config register: + config |= (3 << 4); // bit 5,4 block 3 selection + config |= 0x09; // bit 3 start | bit 0 wakeup HTPA_WriteRegister(HTPA_SENSOR_CONFIG, config); - HAL_Delay(1000); // dauer??? - // read status register: - statusReg = HTPA_ReadRegister(HTPA_SENSOR_STATUS); - HAL_Delay(100); + HAL_Delay(30); // conversion around 27ms in standard config + HTPA_GetStatus(); + while(htpa_statusReg.eoc != 1) { + HAL_Delay(1); + HTPA_GetStatus(); + } // wait until eoc flag is set then read register data + HTPA_ReadRegister(HTPA_SENSOR_READTOP, data_topBlock, 258); + ptat_topBlock = (data_topBlock[0] << 8) | data_topBlock[1]; + /* + * Read electrical offset with VDD + */ + config |= 0x04; // bit 2 vdd_meas + config |= 0x02; // bit 1 blind for electrical offset readout (block selection is ignored) + HTPA_WriteRegister(HTPA_SENSOR_CONFIG, config); + HAL_Delay(30); // conversion around 27ms in standard config + while(htpa_statusReg.eoc != 1) { + HAL_Delay(1); + HTPA_GetStatus(); + } // wait until eoc flag is set then read register data + HTPA_ReadRegister(HTPA_SENSOR_READTOP, elOffset_topBlock, 258); + vdd_topBlock = (elOffset_topBlock[0] << 8) | elOffset_topBlock[1]; - // tbc + /* + * Sort sensor data and assign to pixels + */ + for(int i=0; i<32; i++) { + pixel_topBlock[0][i] = (data_topBlock[2*i + 2] << 8) | data_topBlock[2*i + 3]; + pixel_topBlock[1][i] = (data_topBlock[2*(i+32) + 2] << 8) | data_topBlock[2*(i+32) + 3]; + pixel_topBlock[2][i] = (data_topBlock[2*(i+64) + 2] << 8) | data_topBlock[2*(i+64) + 3]; + pixel_topBlock[3][i] = (data_topBlock[2*(i+96) + 2] << 8) | data_topBlock[2*(i+96) + 3]; + } + + /* + * calculate temperature + * + // 11.1 ambient temperature: + float t_ambient = ptat_topBlock*ptat_gradient + ptat_offset; + for(int i=0; i<4; i++) { + for(int j=0; j<32; j++) { + // 11.2 thermal offset: + vij_comp[i][j] = vij[i][j] - (thGrad[i][j]*ptat_topBlock/pow(2, gradScale)) - thOffset[i][j]; + // 11.3 electrical offset: + vij_compElec[i][j] = vij[i][j] - elOffset[(i+j*32)%128]; + // 11.4 Vdd compensation: + vij_compVdd[i][j] = vij_compElec[i][j] * ... + // 11.5 calculate object temperature + } + } + */ } /** @@ -101,58 +167,41 @@ void HTPA_ReadBlock(uint8_t array_half, uint8_t block, uint8_t blockData[256]){ * * description * - * @param register_address: address of register - * @param byte: byte to be written to register + * @param address: address of register + * @param byte: byte to be written to register */ -void HTPA_WriteRegister(uint8_t register_address, uint8_t byte){ - uint8_t i2c_data = register_address; - uint8_t i2c_address = (HTPA_SENSOR_ADDRESS << 1); - i2c_address &= 0xFE; // set read/write bit to write (0) - HAL_I2C_Master_Transmit(i2c_handle, i2c_address, &i2c_data, 1, I2C_MAX_DELAY); - i2c_data = byte; - HAL_I2C_Master_Transmit(i2c_handle, i2c_address, &i2c_data, 1, I2C_MAX_DELAY); +void HTPA_WriteRegister(uint8_t address, uint8_t byte){ + HAL_I2C_Mem_Write(htpa_hi2c, (HTPA_SENSOR_ADDRESS << 1), address, I2C_MEMADD_SIZE_8BIT, &byte, 1, HTPA_I2C_MAX_DELAY); } /** - * @brief Read single register + * @brief Read from address for specified length * * description * - * @param register_address: address of register + * @param address: register address + * @param pData: pointer to output data array + * @param length: length of data to be read * @return */ -uint8_t HTPA_ReadRegister(uint8_t register_address){ - uint8_t i2c_data = register_address; - uint8_t i2c_address = (HTPA_SENSOR_ADDRESS << 1); - uint8_t i2c_rx = 0; - i2c_address &= 0xFE; // set read/write bit to write (0) - HAL_I2C_Master_Transmit(i2c_handle, i2c_address, &i2c_data, 1, I2C_MAX_DELAY); - i2c_address |= 0x01; // set read/write bit to write (1) - HAL_I2C_Master_Receive(i2c_handle, i2c_address, &i2c_rx, 1, I2C_MAX_DELAY); - return i2c_rx; +void HTPA_ReadRegister(uint8_t address, uint8_t *pData, uint16_t length){ + HAL_I2C_Mem_Read(htpa_hi2c, (HTPA_SENSOR_ADDRESS << 1), address, I2C_MEMADD_SIZE_8BIT, pData, length, HTPA_I2C_MAX_DELAY); } /** * @brief Get status of sensor * * Reads the sensors status register and stores the information in - * the HTPA_Statsu structure. + * the htpa_statusReg variable * - * @return HTPA_Status: status register struct */ -HTPA_Status HTPA_GetStatus(void){ - HAL_StatusTypeDef i2c_status; - HTPA_Status status_return; - uint8_t i2c_data = HTPA_SENSOR_STATUS; - uint8_t i2c_address = (HTPA_SENSOR_ADDRESS << 1); +void HTPA_GetStatus(void){ uint8_t i2c_readData = 0; - i2c_address &= 0xFE; // set read/write bit 0 to write (0) - HAL_I2C_Master_Transmit(i2c_handle, i2c_address, &i2c_data, 1, I2C_MAX_DELAY); - i2c_address |= 0x01; // set read/write bit 0 to read (1) - i2c_status = HAL_I2C_Master_Receive(i2c_handle, i2c_address, &i2c_readData, 1, I2C_MAX_DELAY); - status_return.block = (i2c_readData >> 4) && 0xFC; - status_return.vdd_meas = (i2c_readData >> 2) && 0xFE; - status_return.blind = (i2c_readData >> 1) && 0xFE; - status_return.eoc = i2c_readData && 0xFE; - return status_return; + + HTPA_ReadRegister(HTPA_SENSOR_STATUS, &i2c_readData, 1); + + htpa_statusReg.block = (i2c_readData >> 4) & 0x03; + htpa_statusReg.vdd_meas = (i2c_readData >> 2) & 0x01; + htpa_statusReg.blind = (i2c_readData >> 1) & 0x01; + htpa_statusReg.eoc = i2c_readData & 0x01; } diff --git a/Core/Src/main.c b/Core/Src/main.c index c56a118..8e3c76e 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -44,8 +44,7 @@ I2C_HandleTypeDef hi2c1; /* USER CODE BEGIN PV */ -HTPA_Status htpa_status_test; -uint8_t htpa_blockData[256]; +uint8_t htpa_blockData[258]; /* USER CODE END PV */ @@ -93,9 +92,6 @@ int main(void) MX_I2C1_Init(); /* USER CODE BEGIN 2 */ HTPA_Init(&hi2c1); - htpa_status_test.vdd_meas = 1; - htpa_status_test.block = 3; - /* USER CODE END 2 */ @@ -103,9 +99,8 @@ int main(void) /* USER CODE BEGIN WHILE */ while (1) { - htpa_status_test = HTPA_GetStatus(); - HAL_Delay(1000); - HTPA_ReadBlock(1, 1, htpa_blockData); + //HTPA_ReadBlock(0x0A, 3, (uint8_t *)&htpa_blockData); + HTPA_ReadSensor(); HAL_Delay(1000); /* USER CODE END WHILE */ diff --git a/TTS.ioc b/TTS.ioc index 15e9f2b..1a58c6c 100644 --- a/TTS.ioc +++ b/TTS.ioc @@ -20,12 +20,7 @@ Mcu.Pin0=PF0-OSC_IN Mcu.Pin1=PF1-OSC_OUT Mcu.Pin2=PA13 Mcu.Pin3=PA14 -<<<<<<< HEAD -Mcu.Pin4=VP_SYS_VS_Systick -Mcu.PinsNb=5 -======= Mcu.PinsNb=4 ->>>>>>> afcf6b12b5d8a341020af122f3264c2701e1162e Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F042K6Tx @@ -78,17 +73,10 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=true -<<<<<<< HEAD ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C1_Init-I2C1-false-HAL-true RCC.AHBFreq_Value=16000000 RCC.APB1Freq_Value=16000000 RCC.APB1TimFreq_Value=16000000 -======= -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true -RCC.AHBFreq_Value=8000000 -RCC.APB1Freq_Value=8000000 -RCC.APB1TimFreq_Value=8000000 ->>>>>>> afcf6b12b5d8a341020af122f3264c2701e1162e RCC.CECFreq_Value=32786.88524590164 RCC.FCLKCortexFreq_Value=16000000 RCC.FamilyName=M @@ -106,7 +94,5 @@ RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK RCC.TimSysFreq_Value=16000000 RCC.USART1Freq_Value=16000000 RCC.VCOOutput2Freq_Value=8000000 -VP_SYS_VS_Systick.Mode=SysTick -VP_SYS_VS_Systick.Signal=SYS_VS_Systick board=custom isbadioc=false