tire zone definitions and CAN communication

This commit is contained in:
Tim-Erik Düntzsch
2024-11-29 14:30:55 +01:00
parent 50be1de24e
commit 62ec24511f
39 changed files with 29826 additions and 446 deletions

View File

@ -14,6 +14,7 @@
#include <math.h>
#include "main.h"
#include "HTPA_32x32d.h"
#include "HTPA_lookuptable_short-300degC.h"
// I2C address
#define HTPA_SENSOR_ADDRESS 0x1A
@ -27,26 +28,55 @@
#define HTPA_SENSOR_TRIM_4 0x06 // Clock frequency
#define HTPA_SENSOR_TRIM_5 0x07 // Common mode voltage preamplifier top
#define HTPA_SENSOR_TRIM_6 0x08 // Common mode voltage preamplifier bot
#define HTPA_SENSOR_TRIM_7 0x09 // Interal pull-ups SDA, SCL
#define HTPA_SENSOR_TRIM_7 0x09 // Internal pull-ups SDA, SCL
// Sensor read only registers
#define HTPA_SENSOR_STATUS 0x02 // Status register
#define HTPA_SENSOR_READTOP 0x0A // Read top half
#define HTPA_SENSOR_READBOT 0x0B // Read bot half
// EEPROM addresses
#define HTPA_EEPROM_VDDCOMPGRAD 0x0340 // Start address for vddcompgrad[i][j]
#define HTPA_EEPROM_VDDCOMPOFF 0x0540 // Start address for vddcompoff[i][j]
#define HTPA_EEPROM_THGRAD 0x0A40 // Start address for thgrad[i][j] (top, block3, pixel 384 -> 0x0740 + 2*384 = 0x0A40
#define HTPA_EEPROM_THOFFSET 0x1240 // Start address for thoffset[i][j] (top, block3, pixel 384 -> 0x0F40 + 2*384 = 0x1240
#define HTPA_EEPROM_PI 0x1A40 // Start address for pij[i][j] (top, block3, pixel 384 -> 0x1740 + 2*384 = 0x1A40
#define HTPA_ROWSELECTION 3u // select which row of block 3 is used for temperature calculation (0-3)
#define HTPA_CUSTOM_EPSILON 84u
// I2C transmit delay
#define HTPA_I2C_MAX_DELAY 0xFFFFFFFF
#define HTPA_I2C_MAX_DELAY 0xFF
I2C_HandleTypeDef* htpa_hi2c; // pointer to i2c handle
HTPA_Status htpa_statusReg;
// EEPROM data:
uint8_t gradscale, vddscgrad, vddscoff, epsilon, arraytype, nrofdefpix;
int8_t globaloff;
uint16_t vddth1, vddth2, ptatth1, ptatth2, globalgain, tablenumber;
uint16_t pij[32];
int16_t thgrad[32];
int16_t thoffset[32];
int16_t vddcompgrad[32];
int16_t vddcompoff[32];
float pixcmin, pixcmax, ptatgr, ptatoff;
// Sensor data:
HTPA_Status htpa_statusReg;
uint8_t data_topBlock[258];
uint8_t elOffset_topBlock[258];
//uint8_t data_botBlock[258];
uint16_t vdd_topBlock, ptat_topBlock;
uint16_t pixel_topBlock[32];
uint16_t elOffset[32];
uint16_t vdd_topBlock;
uint16_t ptat_topBlock;
uint16_t pixel_topBlock[4][32];
// Calculated values:
uint32_t gradscale_div, vddscgrad_div, vddscoff_div;
int32_t pixcij[32]; // sensitivity coefficients per pixel (needed for 11.5)
int32_t vij_comp[32]; // thermal offset compensated (11.2)
int32_t vij_comp_s[32]; // electrical offset compensated (11.3)
int32_t vij_vddcomp[32]; // vdd compensated (11.4)
int32_t vij_pixc[32]; // sensitivity coefficients applied (11.5)
uint32_t temp_pix[32]; // final pixel temperature in dK (11.7)
float ambient_temperature;
/**
* @brief Initialization of HTPA Sensor
@ -59,26 +89,89 @@ uint16_t pixel_topBlock[4][32];
*/
void HTPA_Init(I2C_HandleTypeDef *hi2c){
htpa_hi2c = hi2c;
// I2C initialized on 400kbit Fast Mode
/*
* 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(
* (see datasheet Figure 13)
*/
uint8_t eeprom_float[4] = {0};
eeprom_float[0] = HTPA_ReadEEPROM_byte(0x0000);
eeprom_float[1] = HTPA_ReadEEPROM_byte(0x0001);
eeprom_float[2] = HTPA_ReadEEPROM_byte(0x0002);
eeprom_float[3] = HTPA_ReadEEPROM_byte(0x0003);
pixcmin = *(float*)eeprom_float;
eeprom_float[0] = HTPA_ReadEEPROM_byte(0x0004);
eeprom_float[1] = HTPA_ReadEEPROM_byte(0x0005);
eeprom_float[2] = HTPA_ReadEEPROM_byte(0x0006);
eeprom_float[3] = HTPA_ReadEEPROM_byte(0x0007);
pixcmax = *(float*)eeprom_float;
gradscale = HTPA_ReadEEPROM_byte(0x0008);
tablenumber = HTPA_ReadEEPROM_byte(0x000C) << 8 | HTPA_ReadEEPROM_byte(0x000B);
epsilon = HTPA_ReadEEPROM_byte(0x000D);
arraytype = HTPA_ReadEEPROM_byte(0x0022);
vddth1 = HTPA_ReadEEPROM_byte(0x0027) << 8 | HTPA_ReadEEPROM_byte(0x0026);
vddth2 = HTPA_ReadEEPROM_byte(0x0029) << 8 | HTPA_ReadEEPROM_byte(0x0028);
eeprom_float[0] = HTPA_ReadEEPROM_byte(0x0034);
eeprom_float[1] = HTPA_ReadEEPROM_byte(0x0035);
eeprom_float[2] = HTPA_ReadEEPROM_byte(0x0036);
eeprom_float[3] = HTPA_ReadEEPROM_byte(0x0037);
ptatgr = *(float*)eeprom_float;
eeprom_float[0] = HTPA_ReadEEPROM_byte(0x0038);
eeprom_float[1] = HTPA_ReadEEPROM_byte(0x0039);
eeprom_float[2] = HTPA_ReadEEPROM_byte(0x003A);
eeprom_float[3] = HTPA_ReadEEPROM_byte(0x003B);
ptatoff = *(float*)eeprom_float;
ptatth1 = HTPA_ReadEEPROM_byte(0x003D) << 8 | HTPA_ReadEEPROM_byte(0x003C);
ptatth2 = HTPA_ReadEEPROM_byte(0x003F) << 8 | HTPA_ReadEEPROM_byte(0x003E);
vddscgrad = HTPA_ReadEEPROM_byte(0x004E);
vddscoff = HTPA_ReadEEPROM_byte(0x004F);
globaloff = HTPA_ReadEEPROM_byte(0x0054);
globalgain = HTPA_ReadEEPROM_byte(0x0056) << 8 | HTPA_ReadEEPROM_byte(0x0055);
nrofdefpix = HTPA_ReadEEPROM_byte(0x007F);
for(uint8_t i = 0; i < 32; i++) {
// start at top half, row 4
vddcompgrad[i] = HTPA_ReadEEPROM_byte(HTPA_EEPROM_VDDCOMPGRAD + HTPA_ROWSELECTION * 64 + 2 * i + 1) << 8 | HTPA_ReadEEPROM_byte(HTPA_EEPROM_VDDCOMPGRAD + HTPA_ROWSELECTION * 64 + 2 * i);
// ignore bottom half
}
for(uint8_t i = 0; i < 32; i++) {
// start at top half, row 4
vddcompoff[i] = HTPA_ReadEEPROM_byte(HTPA_EEPROM_VDDCOMPOFF + HTPA_ROWSELECTION * 64 + 2 * i + 1) << 8 | HTPA_ReadEEPROM_byte(HTPA_EEPROM_VDDCOMPOFF + HTPA_ROWSELECTION * 64 + 2 * i);
// ignore bottom half
}
for(uint8_t i = 0; i < 32; i++) {
// start at block 3, row 4 (pixel 480)
thgrad[i] = HTPA_ReadEEPROM_byte(HTPA_EEPROM_THGRAD + HTPA_ROWSELECTION * 64 + 2 * i + 1) << 8 | HTPA_ReadEEPROM_byte(HTPA_EEPROM_THGRAD + HTPA_ROWSELECTION * 64 + 2 * i);
// ignore bottom half
}
for(uint8_t i = 0; i < 32; i++) {
// start at block 3, row 4 (pixel 480)
thoffset[i] = HTPA_ReadEEPROM_byte(HTPA_EEPROM_THOFFSET + HTPA_ROWSELECTION * 64 + 2 * i + 1) << 8 | HTPA_ReadEEPROM_byte(HTPA_EEPROM_THOFFSET + HTPA_ROWSELECTION * 64 + 2 * i);
// ignore bottom half
}
for(uint8_t i = 0; i < 32; i++) {
// start at block 3, row 4 (pixel 480)
pij[i] = HTPA_ReadEEPROM_byte(HTPA_EEPROM_PI + HTPA_ROWSELECTION * 64 + 2 * i + 1) << 8 | HTPA_ReadEEPROM_byte(HTPA_EEPROM_PI + HTPA_ROWSELECTION * 64 + 2 * i);
// ignore bottom half
}
/* Set I2C to Fast Mode Plus (1Mbit) for sensor readout: */
if (HAL_I2C_DeInit(htpa_hi2c) != HAL_OK)
{
Error_Handler();
}
htpa_hi2c->Init.Timing = 0x00000107;
if (HAL_I2C_Init(htpa_hi2c) != HAL_OK)
{
Error_Handler();
}
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C1);
HAL_Delay(100);
/*
* 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
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)
@ -97,9 +190,35 @@ void HTPA_Init(I2C_HandleTypeDef *hi2c){
HAL_Delay(10);
//HTPA_WriteRegister(HTPA_SENSOR_CONFIG, 0x09); // start sensor
//HAL_Delay(10);
/*
* Calculations
*/
//gradscale_div = HTPA_calcPowerTwo(gradscale);
gradscale_div = HTPA_calcPowerTwo(gradscale);
vddscgrad_div = HTPA_calcPowerTwo(vddscgrad);
vddscoff_div = HTPA_calcPowerTwo(vddscoff);
// calculate sensitivity coefficients: (datasheet 11.5)
for(uint8_t i = 0; i < 32; i++) {
pixcij[i] = (int32_t)pixcmax - (int32_t)pixcmin;
pixcij[i] = pixcij[i] / 65535;
pixcij[i] = pixcij[i] * pij[i];
pixcij[i] = pixcij[i] + pixcmin;
pixcij[i] = pixcij[i] * 1.0 * HTPA_CUSTOM_EPSILON / 100;
pixcij[i] = pixcij[i] * 1.0 * globalgain / 10000;
}
}
void HTPA_ReadSensor(void) {
uint32_t HTPA_calcPowerTwo(uint8_t power) {
if (power == 0)
return 1;
else if ((power % 2) == 0)
return HTPA_calcPowerTwo(power / 2) * HTPA_calcPowerTwo(power / 2);
else
return 2 * HTPA_calcPowerTwo(power / 2) * HTPA_calcPowerTwo(power / 2);
}
void HTPA_ReadSensor(uint32_t dataArray[32]) {
uint8_t config = 0;
/*
* Read top array half of block3 with PTAT
@ -137,29 +256,75 @@ void HTPA_ReadSensor(void) {
* 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];
*/
pixel_topBlock[i] = (data_topBlock[2*(i+32*HTPA_ROWSELECTION) + 2] << 8) | data_topBlock[2*(i+32*HTPA_ROWSELECTION) + 3];
/*
elOffset[0][i] = (elOffset_topBlock[2*i + 2] << 8) | elOffset_topBlock[2*i + 3];
elOffset[1][i] = (elOffset_topBlock[2*(i+32) + 2] << 8) | elOffset_topBlock[2*(i+32) + 3];
elOffset[2][i] = (elOffset_topBlock[2*(i+64) + 2] << 8) | elOffset_topBlock[2*(i+64) + 3];
elOffset[3][i] = (elOffset_topBlock[2*(i+96) + 2] << 8) | elOffset_topBlock[2*(i+96) + 3];
*/
elOffset[i] = (elOffset_topBlock[2*(i+32*HTPA_ROWSELECTION) + 2] << 8) | elOffset_topBlock[2*(i+32*HTPA_ROWSELECTION) + 3];
}
/*
* calculate temperature
*
*/
int64_t vij_pixc_and_pcscaleval;
int64_t vdd_calc_steps;
uint16_t table_row, table_col;
int32_t vx, vy, ydist, dta;
// 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
ambient_temperature = ptat_topBlock * ptatgr + ptatoff; // value in dK
// find column of lookup table (ambient temperature)
for(uint8_t i = 0; i < NROFTAELEMENTS; i++) {
if(ambient_temperature > XTATemps[i]) {
table_col = i;
}
}
*/
dta = ambient_temperature - XTATemps[table_col];
ydist = (int32_t)ADEQUIDISTANCE;
for(int i=0; i<32; i++) {
// 11.2 thermal offset:
vij_comp[i] = pixel_topBlock[i] - (thgrad[i] * ptat_topBlock / gradscale_div) - thoffset[i];
// 11.3 electrical offset:
vij_comp_s[i] = vij_comp[i] - elOffset[i];
// 11.4 Vdd compensation:
vdd_calc_steps = vddcompgrad[i] * ptat_topBlock;
vdd_calc_steps = vdd_calc_steps / vddscgrad_div;
vdd_calc_steps = vdd_calc_steps + vddcompoff[i];
vdd_calc_steps = vdd_calc_steps * (vdd_topBlock - vddth1 - ((vddth2 - vddth1) / (ptatth2 - ptatth1)) * (ptat_topBlock - ptatth1));
vdd_calc_steps = vdd_calc_steps / vddscoff_div;
vij_vddcomp[i] = vij_comp_s[i] - vdd_calc_steps;
// 11.5 calculate object temperature
vij_pixc_and_pcscaleval = (int64_t)vij_vddcomp[i] * (int64_t)PCSCALEVAL;
vij_pixc[i] = (int32_t)(vij_pixc_and_pcscaleval / (int64_t)pixcij[i]);
// find temperature in lookup table and do bilinear interpolation
table_row = vij_pixc[i] + TABLEOFFSET;
table_row = table_row >> ADEXPBITS;
vx = ((((int32_t)TempTable[table_row][table_col + 1] - (int32_t)TempTable[table_row][table_col]) * (int32_t)dta) / (int32_t)TAEQUIDISTANCE) + (int32_t)TempTable[table_row][table_col];
vy = ((((int32_t)TempTable[table_row + 1][table_col + 1] - (int32_t)TempTable[table_row + 1][table_col]) * (int32_t)dta) / (int32_t)TAEQUIDISTANCE) + (int32_t)TempTable[table_row + 1][table_col];
temp_pix[i] = (uint32_t)((vy - vx) * ((int32_t)(vij_pixc[i] + TABLEOFFSET) - (int32_t)YADValues[table_row]) / ydist + (int32_t)vx);
// --- GLOBAL OFFSET ---
temp_pix[i] = temp_pix[i] + globaloff;
dataArray[i] = temp_pix[i] - 2731;
}
}
/**
@ -182,7 +347,6 @@ void HTPA_WriteRegister(uint8_t address, uint8_t byte){
* @param address: register address
* @param pData: pointer to output data array
* @param length: length of data to be read
* @return
*/
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);
@ -205,3 +369,16 @@ void HTPA_GetStatus(void){
htpa_statusReg.blind = (i2c_readData >> 1) & 0x01;
htpa_statusReg.eoc = i2c_readData & 0x01;
}
/**
* @brief Get status of sensor
*
* Reads the sensors status register and stores the information in
* the htpa_statusReg variable
*
*/
uint8_t HTPA_ReadEEPROM_byte(uint16_t address){
uint8_t data = 0;
HAL_I2C_Mem_Read(htpa_hi2c, (HTPA_EEPROM_ADDRESS << 1), address, I2C_MEMADD_SIZE_16BIT, &data, 1, HTPA_I2C_MAX_DELAY);
return data;
}

View File

@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
@ -21,8 +21,8 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdbool.h>
#include "HTPA_32x32d.h"
#include "tts.h"
/* USER CODE END Includes */
@ -33,6 +33,7 @@
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@ -41,16 +42,22 @@
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
I2C_HandleTypeDef hi2c1;
/* USER CODE BEGIN PV */
uint8_t htpa_blockData[258];
uint32_t pixelTemps[32];
uint32_t tireTemps[5];
uint32_t systicks = 0;
uint8_t blinkCount = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */
@ -67,6 +74,7 @@ static void MX_I2C1_Init(void);
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
@ -89,9 +97,15 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HTPA_Init(&hi2c1);
TTS_Init(&hcan);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
HAL_CAN_Start(&hcan);
/* USER CODE END 2 */
@ -99,9 +113,21 @@ int main(void)
/* USER CODE BEGIN WHILE */
while (1)
{
//HTPA_ReadBlock(0x0A, 3, (uint8_t *)&htpa_blockData);
HTPA_ReadSensor();
HAL_Delay(1000);
systicks = HAL_GetTick();
if((systicks % 100) <= 1){
if(blinkCount >= 9) {
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
blinkCount = 0;
}
else {
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
blinkCount++;
}
HTPA_ReadSensor(pixelTemps);
TTS_TireZones(pixelTemps,tireTemps);
TTS_SendCAN(tireTemps);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
@ -122,13 +148,9 @@ void SystemClock_Config(void)
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
@ -138,7 +160,7 @@ void SystemClock_Config(void)
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
@ -154,6 +176,43 @@ void SystemClock_Config(void)
}
}
/**
* @brief CAN Initialization Function
* @param None
* @retval None
*/
static void MX_CAN_Init(void)
{
/* USER CODE BEGIN CAN_Init 0 */
/* USER CODE END CAN_Init 0 */
/* USER CODE BEGIN CAN_Init 1 */
/* USER CODE END CAN_Init 1 */
hcan.Instance = CAN;
hcan.Init.Prescaler = 2;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = DISABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = DISABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN_Init 2 */
/* USER CODE END CAN_Init 2 */
}
/**
* @brief I2C1 Initialization Function
* @param None
@ -170,7 +229,7 @@ static void MX_I2C1_Init(void)
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00000107;
hi2c1.Init.Timing = 0x0010061A;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
@ -196,10 +255,6 @@ static void MX_I2C1_Init(void)
{
Error_Handler();
}
/** I2C Fast mode Plus enable
*/
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C1);
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
@ -213,12 +268,24 @@ static void MX_I2C1_Init(void)
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
@ -236,6 +303,7 @@ void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
__disable_irq();
while (1)
{

View File

@ -1,3 +1,4 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
@ -7,7 +8,7 @@
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
@ -20,7 +21,6 @@
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
@ -63,6 +63,7 @@
*/
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
@ -77,6 +78,72 @@ void HAL_MspInit(void)
/* USER CODE END MspInit 1 */
}
/**
* @brief CAN MSP Initialization
* This function configures the hardware resources used in this example
* @param hcan: CAN handle pointer
* @retval None
*/
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hcan->Instance==CAN)
{
/* USER CODE BEGIN CAN_MspInit 0 */
/* USER CODE END CAN_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_CAN1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**CAN GPIO Configuration
PA11 ------> CAN_RX
PA12 ------> CAN_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN CAN_MspInit 1 */
/* USER CODE END CAN_MspInit 1 */
}
}
/**
* @brief CAN MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hcan: CAN handle pointer
* @retval None
*/
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
{
if(hcan->Instance==CAN)
{
/* USER CODE BEGIN CAN_MspDeInit 0 */
/* USER CODE END CAN_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_CAN1_CLK_DISABLE();
/**CAN GPIO Configuration
PA11 ------> CAN_RX
PA12 ------> CAN_TX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
/* USER CODE BEGIN CAN_MspDeInit 1 */
/* USER CODE END CAN_MspDeInit 1 */
}
}
/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example
@ -92,23 +159,24 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
/* USER CODE END I2C1_MspInit 0 */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PF0-OSC_IN ------> I2C1_SDA
PF1-OSC_OUT ------> I2C1_SCL
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
@ -130,12 +198,12 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PF0-OSC_IN ------> I2C1_SDA
PF1-OSC_OUT ------> I2C1_SCL
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_1);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
/* USER CODE BEGIN I2C1_MspDeInit 1 */

View File

@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
@ -72,7 +72,7 @@ void NMI_Handler(void)
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)
while (1)
{
}
/* USER CODE END NonMaskableInt_IRQn 1 */

View File

@ -10,7 +10,7 @@
******************************************************************************
* @attention
*
* Copyright (c) 2020-2022 STMicroelectronics.
* Copyright (c) 2020-2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file

View File

@ -10,7 +10,7 @@
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file

184
Core/Src/tts.c Normal file
View File

@ -0,0 +1,184 @@
/*
* tts.c
*
* Created on: Jun 28, 2024
* Author: ted
*/
#include "tts.h"
#include "HTPA_32x32d.h"
// CAN Frame:
#define TTS_CANIDSTART 0x701; //
CAN_HandleTypeDef* tts_hcan;
CAN_TxHeaderTypeDef tts_canHeader;
uint8_t tts_canData[8];
uint32_t tts_canMailbox;
// Car / Tire info:
TTS_SensorID tts_sensorid;
TTS_CarID tts_carid;
TTS_TireID tts_tireid;
TTS_TireData tts_tiredb[4];
void TTS_Init(CAN_HandleTypeDef *hcan) {
// initialize values
TTS_LoadTireData();
tts_sensorid = TTS_FL;
tts_carid = FT24;
tts_tireid = OZ7_SLICKS;
// init CAN
tts_hcan = hcan;
// set CAN filter
/*
CAN_FilterTypeDef canfilterconfig;
canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
canfilterconfig.FilterBank = 0;
canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
canfilterconfig.FilterIdHigh = 0x704<<5;
canfilterconfig.FilterIdLow = 0x700<<5;
canfilterconfig.FilterMaskIdHigh = 0x704<<5;
canfilterconfig.FilterMaskIdLow = 0x700<<5;
canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
HAL_CAN_ConfigFilter(tts_hcan, &canfilterconfig);
*/
// init CAN_Tx Frame
//uint8_t canID = tts_sensorid + TTS_CANIDSTART;
tts_canHeader.IDE = CAN_ID_STD;
tts_canHeader.StdId = 0x701;
tts_canHeader.DLC = 8;
tts_canHeader.RTR = CAN_RTR_DATA;
for(uint8_t i=0; i<8; i++) {
tts_canData[i] = 0xFF;
}
}
void TTS_SendCAN(uint32_t tireZones[5]) {
// Outer left:
tts_canData[0] = tireZones[0] & 0xFF;
tts_canData[1] = (tireZones[0] >> 8) & 0xF;
// Center left:
tts_canData[1] = tts_canData[1] | ((tireZones[1] & 0xF) << 4);
tts_canData[2] = (tireZones[1] >> 4) & 0xFF;
// Center:
tts_canData[3] = tireZones[2] & 0xFF;
tts_canData[4] = (tireZones[2] >> 8) & 0xF;
// Center right:
tts_canData[4] = tts_canData[4] | ((tireZones[3] & 0xF) << 4);
tts_canData[5] = (tireZones[3] >> 4) & 0xFF;
// Center right:
tts_canData[6] = tireZones[4] & 0xFF;
tts_canData[7] = (tireZones[4] >> 8) & 0xF;
// current tire selected:
//tts_canData[7] = tts_canData[7] | ((tts_tireid & 0xF) << 4);
if(HAL_CAN_AddTxMessage(tts_hcan, &tts_canHeader, tts_canData, &tts_canMailbox) != HAL_OK) {
Error_Handler();
}
}
void TTS_TireZones(uint32_t tempArray[32], uint32_t tireTempArray[5]) {
for(uint8_t i = 0; i < 5; i++) {
tireTempArray[i] = 0;
}
uint8_t zoneWidth[5] = {0};
uint8_t tireid = tts_tireid;
for(uint8_t i = 0; i < 32; i++) {
// outer right:
if((i <= tts_tiredb[tts_tireid].outerRightStart) && (i >= tts_tiredb[tts_tireid].outerRightStop)) {
tireTempArray[4] = tireTempArray[4] + tempArray[i];
zoneWidth[4]++;
}
// center right:
if((i <= tts_tiredb[tts_tireid].centerRightStart) && (i >= tts_tiredb[tts_tireid].centerRightStop)) {
tireTempArray[3] = tireTempArray[3] + tempArray[i];
zoneWidth[3]++;
}
// center:
if((i <= tts_tiredb[tts_tireid].centerStart) && (i >= tts_tiredb[tts_tireid].centerStop)) {
tireTempArray[2] = tireTempArray[2] + tempArray[i];
zoneWidth[2]++;
}
// center left:
if((i <= tts_tiredb[tts_tireid].centerLeftStart) && (i >= tts_tiredb[tts_tireid].centerLeftStop)) {
tireTempArray[1] = tireTempArray[1] + tempArray[i];
zoneWidth[1]++;
}
// outer left:
if((i <= tts_tiredb[tts_tireid].outerLeftStart) && (i >= tts_tiredb[tts_tireid].outerLeftStop)) {
tireTempArray[0] = tireTempArray[0] + tempArray[i];
zoneWidth[0]++;
}
}
tireTempArray[4] = tireTempArray[4] / zoneWidth[4];
tireTempArray[3] = tireTempArray[3] / zoneWidth[3];
tireTempArray[2] = tireTempArray[2] / zoneWidth[2];
tireTempArray[1] = tireTempArray[1] / zoneWidth[1];
tireTempArray[0] = tireTempArray[0] / zoneWidth[0];
}
void TTS_LoadTireData(void) {
tts_tiredb[UNKNOWN].id = UNKNOWN;
tts_tiredb[UNKNOWN].epsilon = 84;
tts_tiredb[UNKNOWN].outerLeftStart = 31;
tts_tiredb[UNKNOWN].outerLeftStop = 26;
tts_tiredb[UNKNOWN].centerLeftStart = 25;
tts_tiredb[UNKNOWN].centerLeftStop = 20;
tts_tiredb[UNKNOWN].centerStart = 19;
tts_tiredb[UNKNOWN].centerStop = 12;
tts_tiredb[UNKNOWN].centerRightStart = 11;
tts_tiredb[UNKNOWN].centerRightStop = 6;
tts_tiredb[UNKNOWN].outerRightStart = 5;
tts_tiredb[UNKNOWN].outerRightStop = 0;
tts_tiredb[OZ7_SLICKS].id = OZ7_SLICKS;
tts_tiredb[OZ7_SLICKS].epsilon = 84;
tts_tiredb[OZ7_SLICKS].outerLeftStart = 27;
tts_tiredb[OZ7_SLICKS].outerLeftStop = 25;
tts_tiredb[OZ7_SLICKS].centerLeftStart = 24;
tts_tiredb[OZ7_SLICKS].centerLeftStop = 19;
tts_tiredb[OZ7_SLICKS].centerStart = 18;
tts_tiredb[OZ7_SLICKS].centerStop = 13;
tts_tiredb[OZ7_SLICKS].centerRightStart = 12;
tts_tiredb[OZ7_SLICKS].centerRightStop = 7;
tts_tiredb[OZ7_SLICKS].outerRightStart = 6;
tts_tiredb[OZ7_SLICKS].outerRightStop = 4;
tts_tiredb[OZ7_RAIN].id = OZ7_RAIN;
tts_tiredb[OZ7_RAIN].epsilon = 84;
tts_tiredb[OZ7_RAIN].outerLeftStart = 31;
tts_tiredb[OZ7_RAIN].outerLeftStop = 26;
tts_tiredb[OZ7_RAIN].centerLeftStart = 25;
tts_tiredb[OZ7_RAIN].centerLeftStop = 20;
tts_tiredb[OZ7_RAIN].centerStart = 19;
tts_tiredb[OZ7_RAIN].centerStop = 12;
tts_tiredb[OZ7_RAIN].centerRightStart = 11;
tts_tiredb[OZ7_RAIN].centerRightStop = 6;
tts_tiredb[OZ7_RAIN].outerRightStart = 5;
tts_tiredb[OZ7_RAIN].outerRightStop = 0;
tts_tiredb[JP8_SLICKS].id = JP8_SLICKS;
tts_tiredb[JP8_SLICKS].epsilon = 84;
tts_tiredb[JP8_SLICKS].outerLeftStart = 28;
tts_tiredb[JP8_SLICKS].outerLeftStop = 24;
tts_tiredb[JP8_SLICKS].centerLeftStart = 23;
tts_tiredb[JP8_SLICKS].centerLeftStop = 19;
tts_tiredb[JP8_SLICKS].centerStart = 18;
tts_tiredb[JP8_SLICKS].centerStop = 13;
tts_tiredb[JP8_SLICKS].centerRightStart = 12;
tts_tiredb[JP8_SLICKS].centerRightStop = 8;
tts_tiredb[JP8_SLICKS].outerRightStart = 7;
tts_tiredb[JP8_SLICKS].outerRightStop = 3;
}