* Refactor I2C_SOFT to new #define

* Stitch in some of TS101

Update ShowStartupWarnings.cpp

Update OLED.hpp

Update stm32f1xx_hal_msp.c

Update Setup.cpp

Update Power.cpp

Update Pins.h

Update configuration.h

Power Muxing

Working dual input Voltage handler

Scan mode required for differing injected channels

Inject both dc readings

Update configuration.h

Update configuration.h

Use htim4 for adc control on TS101

Refactor htim names

Add ADC_TRIGGER

Speed up BB I2C a lil

Update configuration.h

Update startup_stm32f103t8ux.S

Update configuration.h

Add LIS2DH clone

LIS2DH gains another clone

Create tooling to allow mapping accelerometers onto different buses

Update startup_stm32f103t8ux.S

Ensure PD IRQ is pulled up

* Stitch in some of TS101

Update ShowStartupWarnings.cpp

Update OLED.hpp

Update stm32f1xx_hal_msp.c

Update Setup.cpp

Update Power.cpp

Update Pins.h

Update configuration.h

Power Muxing

Working dual input Voltage handler

Scan mode required for differing injected channels

Inject both dc readings

Update configuration.h

Update configuration.h

Use htim4 for adc control on TS101

Refactor htim names

Add ADC_TRIGGER

Speed up BB I2C a lil

Update configuration.h

Update startup_stm32f103t8ux.S

Update configuration.h

Add LIS2DH clone

LIS2DH gains another clone

Create tooling to allow mapping accelerometers onto different buses

Update startup_stm32f103t8ux.S

Ensure PD IRQ is pulled up

Allow toggle which button enters PD debug

* Update Pins.h

* Fix hard coded IRQ Pin

Update stm32f1xx_it.c

* Enable EPR

* Tip resistance measurement

* TS101 is a direct drive tip

Update BSP.cpp

* Add S60 and TS101 to builds

Update push.yml

* Update MOVThread.cpp

* Refactor power menu handler

* Correct prescaler

Forgot to update since I changed the period

* Tune in the timer divider for tip control to make PWM less audible

---------

Co-authored-by: discip <53649486+discip@users.noreply.github.com>
This commit is contained in:
Ben V. Brown
2023-06-18 21:58:20 +10:00
committed by GitHub
parent a1b9e40f67
commit d3d8e3d2d5
38 changed files with 1130 additions and 332 deletions

View File

@@ -18,8 +18,8 @@ DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx;
IWDG_HandleTypeDef hiwdg;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htimADC;
TIM_HandleTypeDef htimTip;
#define ADC_FILTER_LEN 4
#define ADC_SAMPLES 16
uint16_t ADCReadings[ADC_SAMPLES]; // Used to store the adc readings for the handle cold junction temp
@@ -29,8 +29,8 @@ static void SystemClock_Config(void);
static void MX_ADC1_Init(void);
static void MX_I2C1_Init(void);
static void MX_IWDG_Init(void);
static void MX_TIM3_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIP_CONTROL_TIMER_Init(void);
static void MX_ADC_CONTROL_TIMER_Init(void);
static void MX_DMA_Init(void);
static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void);
@@ -45,11 +45,13 @@ void Setup_HAL() {
MX_GPIO_Init();
MX_DMA_Init();
#ifndef I2C_SOFT_BUS_1
MX_I2C1_Init();
#endif
MX_ADC1_Init();
MX_ADC2_Init();
MX_TIM3_Init();
MX_TIM2_Init();
MX_TIP_CONTROL_TIMER_Init();
MX_ADC_CONTROL_TIMER_Init();
MX_IWDG_Init();
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADCReadings, (ADC_SAMPLES)); // start DMA of normal readings
HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings
@@ -68,7 +70,41 @@ uint16_t getADCHandleTemp(uint8_t sample) {
return filter.average() >> 1;
}
#ifdef HAS_SPLIT_POWER_PATH
static history<uint16_t, ADC_FILTER_LEN> filteredDC = {{0}, 0, 0};
static history<uint16_t, ADC_FILTER_LEN> filteredPD = {{0}, 0, 0};
uint16_t getRawDCVin() { return filteredDC.average(); }
uint16_t getRawPDVin() { return filteredPD.average(); }
#endif
uint16_t getADCVin(uint8_t sample) {
#ifdef HAS_SPLIT_POWER_PATH
// In split power path operation, we need to read both inputs, and return the larger
if (sample) {
{
uint16_t latestADC = 0;
latestADC += hadc2.Instance->JDR1;
latestADC += hadc2.Instance->JDR2;
latestADC <<= 3;
filteredDC.update(latestADC);
}
{
uint16_t latestADC = 0;
latestADC += hadc2.Instance->JDR3;
latestADC += hadc2.Instance->JDR4;
latestADC <<= 3;
filteredPD.update(latestADC);
}
}
uint16_t dc = filteredDC.average();
uint16_t pd = filteredPD.average();
if (dc > pd) {
return dc;
}
return pd;
#else
static history<uint16_t, ADC_FILTER_LEN> filter = {{0}, 0, 0};
if (sample) {
uint16_t latestADC = 0;
@@ -81,6 +117,7 @@ uint16_t getADCVin(uint8_t sample) {
filter.update(latestADC);
}
return filter.average();
#endif
}
// Returns either average or instant value. When sample is set the samples from the injected ADC are copied to the filter and then the raw reading is returned
uint16_t getTipRawTemp(uint8_t sample) {
@@ -178,7 +215,7 @@ static void MX_ADC1_Init(void) {
sConfigInjected.InjectedRank = 1;
sConfigInjected.InjectedNbrOfConversion = 4;
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_28CYCLES_5;
sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
sConfigInjected.ExternalTrigInjecConv = ADC_TRIGGER;
sConfigInjected.AutoInjectedConv = DISABLE;
sConfigInjected.InjectedDiscontinuousConvMode = DISABLE;
sConfigInjected.InjectedOffset = 0;
@@ -203,7 +240,7 @@ static void MX_ADC2_Init(void) {
/**Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc2.Init.ContinuousConvMode = ENABLE;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
@@ -217,13 +254,18 @@ static void MX_ADC2_Init(void) {
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_1;
sConfigInjected.InjectedNbrOfConversion = 4;
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_28CYCLES_5;
sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
sConfigInjected.ExternalTrigInjecConv = ADC_TRIGGER;
sConfigInjected.AutoInjectedConv = DISABLE;
sConfigInjected.InjectedDiscontinuousConvMode = DISABLE;
sConfigInjected.InjectedOffset = 0;
HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected);
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_2;
HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected);
#ifdef HAS_SPLIT_POWER_PATH
sConfigInjected.InjectedChannel = PD_VIN_ADC2_CHANNEL;
#endif
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_3;
HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected);
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_4;
@@ -259,35 +301,43 @@ static void MX_IWDG_Init(void) {
}
/* TIM3 init function */
static void MX_TIM3_Init(void) {
static void MX_TIP_CONTROL_TIMER_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim3.Instance = TIM3;
htim3.Init.Prescaler = 8;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 100; // 5 Khz PWM freq
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 4mhz before div
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; // Preload the ARR register (though we dont use this)
HAL_TIM_Base_Init(&htim3);
htimTip.Instance = TIP_CONTROL_TIMER;
#ifdef TIP_HAS_DIRECT_PWM
htimTip.Init.Prescaler = 100;
#else
htimTip.Init.Prescaler = 3;
#endif
htimTip.Init.CounterMode = TIM_COUNTERMODE_UP;
htimTip.Init.Period = 255; // 5 Khz PWM freq
htimTip.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 4mhz before div
htimTip.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; // Preload the ARR register (though we dont use this)
HAL_TIM_Base_Init(&htimTip);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
HAL_TIM_ConfigClockSource(&htimTip, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim3);
HAL_TIM_PWM_Init(&htimTip);
HAL_TIM_OC_Init(&htim3);
HAL_TIM_OC_Init(&htimTip);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
HAL_TIMEx_MasterConfigSynchronization(&htimTip, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 50; // 50% duty cycle, that is AC coupled through the cap
sConfigOC.OCMode = TIM_OCMODE_PWM1;
#ifdef TIP_HAS_DIRECT_PWM
sConfigOC.Pulse = 0; // PWM is direct to tip
#else
sConfigOC.Pulse = 127; // 50% duty cycle, that is AC coupled through the cap to provide an on signal (This does not do tip at 50% duty cycle)
#endif
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL);
HAL_TIM_PWM_ConfigChannel(&htimTip, &sConfigOC, PWM_Out_CHANNEL);
GPIO_InitTypeDef GPIO_InitStruct;
@@ -302,12 +352,12 @@ static void MX_TIM3_Init(void) {
// Remap TIM3_CH1 to be on PB4
__HAL_AFIO_REMAP_TIM3_PARTIAL();
#else
// No re-map required
// No re-map required
#endif
HAL_TIM_PWM_Start(&htim3, PWM_Out_CHANNEL);
HAL_TIM_PWM_Start(&htimTip, PWM_Out_CHANNEL);
}
/* TIM3 init function */
static void MX_TIM2_Init(void) {
static void MX_ADC_CONTROL_TIMER_Init(void) {
/*
* We use the channel 1 to trigger the ADC at end of PWM period
* And we use the channel 4 as the PWM modulation source using Interrupts
@@ -318,30 +368,30 @@ static void MX_TIM2_Init(void) {
// Timer 2 is fairly slow as its being used to run the PWM and trigger the ADC
// in the PWM off time.
htim2.Instance = TIM2;
htimADC.Instance = ADC_CONTROL_TIMER;
// dummy value, will be reconfigured by BSPInit()
htim2.Init.Prescaler = 2000; // 2 MHz timer clock/2000 = 1 kHz tick rate
htimADC.Init.Prescaler = 2000; // 2 MHz timer clock/2000 = 1 kHz tick rate
// pwm out is 10k from tim3, we want to run our PWM at around 10hz or slower on the output stage
// These values give a rate of around 3.5 Hz for "fast" mode and 1.84 Hz for "slow"
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htimADC.Init.CounterMode = TIM_COUNTERMODE_UP;
// dummy value, will be reconfigured by BSPInit()
htim2.Init.Period = powerPWM + 14 * 2;
htimADC.Init.Period = powerPWM + 14 * 2;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 8 MHz (x2 APB1) before divide
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim2);
htimADC.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 8 MHz (x2 APB1) before divide
htimADC.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htimADC.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htimADC);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
HAL_TIM_ConfigClockSource(&htimADC, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim2);
HAL_TIM_OC_Init(&htim2);
HAL_TIM_PWM_Init(&htimADC);
HAL_TIM_OC_Init(&htimADC);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
HAL_TIMEx_MasterConfigSynchronization(&htimADC, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
// dummy value, will be reconfigured by BSPInit() in the BSP.cpp
@@ -354,15 +404,15 @@ static void MX_TIM2_Init(void) {
* */
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htimADC, &sConfigOC, TIM_CHANNEL_1);
sConfigOC.Pulse = 0; // default to entirely off
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);
HAL_TIM_OC_ConfigChannel(&htimADC, &sConfigOC, TIM_CHANNEL_4);
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_4);
HAL_NVIC_SetPriority(TIM2_IRQn, 15, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
HAL_TIM_Base_Start_IT(&htimADC);
HAL_TIM_PWM_Start(&htimADC, TIM_CHANNEL_1);
HAL_TIM_PWM_Start_IT(&htimADC, TIM_CHANNEL_4);
HAL_NVIC_SetPriority(ADC_CONTROL_TIMER_IRQ, 15, 0);
HAL_NVIC_EnableIRQ(ADC_CONTROL_TIMER_IRQ);
}
/**
@@ -446,8 +496,8 @@ static void MX_GPIO_Init(void) {
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
#else
/* TS80 */
/* Leave USB lines open circuit*/
/* TS80 */
/* Leave USB lines open circuit*/
#endif
@@ -462,12 +512,45 @@ static void MX_GPIO_Init(void) {
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(OLED_RESET_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET);
// Pull down LCD reset
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET);
HAL_Delay(30);
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET);
#ifdef DC_SELECT_Pin
GPIO_InitStruct.Pin = DC_SELECT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DC_SELECT_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(DC_SELECT_GPIO_Port, DC_SELECT_Pin, GPIO_PIN_RESET);
#endif
#ifdef PD_SELECT_Pin
GPIO_InitStruct.Pin = PD_SELECT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(PD_SELECT_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(PD_SELECT_GPIO_Port, PD_SELECT_Pin, GPIO_PIN_RESET);
#endif
#ifdef TIP_RESISTANCE_SENSE_Pin
GPIO_InitStruct.Pin = TIP_RESISTANCE_SENSE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(TIP_RESISTANCE_SENSE_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(TIP_RESISTANCE_SENSE_GPIO_Port, TIP_RESISTANCE_SENSE_Pin, GPIO_PIN_RESET);
#endif
#ifdef INT_PD_Pin
GPIO_InitStruct.Pin = INT_PD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(INT_PD_GPIO_Port, &GPIO_InitStruct);
#endif
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line) { asm("bkpt"); }