Update BSP.cpp

This commit is contained in:
Ben V. Brown
2020-11-01 14:05:48 +11:00
parent 84772266fd
commit 7e4187e62d

View File

@@ -17,111 +17,113 @@ const uint8_t tempMeasureTicks = 25;
uint16_t totalPWM; // htim2.Init.Period, the full PWM cycle uint16_t totalPWM; // htim2.Init.Period, the full PWM cycle
// 2 second filter (ADC is PID_TIM_HZ Hz) // 2 second filter (ADC is PID_TIM_HZ Hz)
history<uint16_t, PID_TIM_HZ> rawTempFilter = {{0}, 0, 0}; history<uint16_t, PID_TIM_HZ> rawTempFilter = { { 0 }, 0, 0 };
void resetWatchdog() { void resetWatchdog() {
fwdgt_counter_reload(); fwdgt_counter_reload();
} }
uint16_t getTipInstantTemperature() { uint16_t getTipInstantTemperature() {
volatile uint16_t sum = 0; // 12 bit readings * 8*2 -> 16 bits volatile uint16_t sum = 0; // 12 bit readings * 8*2 -> 16 bits
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
sum += adc_inserted_data_read(ADC0, i); sum += adc_inserted_data_read(ADC0, i);
sum += adc_inserted_data_read(ADC1, i); sum += adc_inserted_data_read(ADC1, i);
} }
return sum; // 8x over sample return sum; // 8x over sample
} }
uint16_t getTipRawTemp(uint8_t refresh) { uint16_t getTipRawTemp(uint8_t refresh) {
if (refresh) { if (refresh) {
uint16_t lastSample = getTipInstantTemperature(); uint16_t lastSample = getTipInstantTemperature();
rawTempFilter.update(lastSample); rawTempFilter.update(lastSample);
return lastSample; return lastSample;
} else { } else {
return rawTempFilter.average(); return rawTempFilter.average();
} }
} }
uint16_t getHandleTemperature() { uint16_t getHandleTemperature() {
#ifdef TEMP_TMP36 #ifdef TEMP_TMP36
// We return the current handle temperature in X10 C // We return the current handle temperature in X10 C
// TMP36 in handle, 0.5V offset and then 10mV per deg C (0.75V @ 25C for // TMP36 in handle, 0.5V offset and then 10mV per deg C (0.75V @ 25C for
// example) STM32 = 4096 count @ 3.3V input -> But We oversample by 32/(2^2) = // example) STM32 = 4096 count @ 3.3V input -> But We oversample by 32/(2^2) =
// 8 times oversampling Therefore 32768 is the 3.3V input, so 0.1007080078125 // 8 times oversampling Therefore 32768 is the 3.3V input, so 0.1007080078125
// mV per count So we need to subtract an offset of 0.5V to center on 0C // mV per count So we need to subtract an offset of 0.5V to center on 0C
// (4964.8 counts) // (4964.8 counts)
// //
int32_t result = getADC(0); int32_t result = getADC(0);
result -= 4965; // remove 0.5V offset result -= 4965; // remove 0.5V offset
// 10mV per C // 10mV per C
// 99.29 counts per Deg C above 0C // 99.29 counts per Deg C above 0C
result *= 100; result *= 100;
result /= 993; result /= 993;
return result; return result;
#else #else
#error #error
#endif #endif
} }
uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) { uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
static uint8_t preFillneeded = 10; static uint8_t preFillneeded = 10;
static uint32_t samples[BATTFILTERDEPTH]; static uint32_t samples[BATTFILTERDEPTH];
static uint8_t index = 0; static uint8_t index = 0;
if (preFillneeded) { if (preFillneeded) {
for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) for (uint8_t i = 0; i < BATTFILTERDEPTH; i++)
samples[i] = getADC(1); samples[i] = getADC(1);
preFillneeded--; preFillneeded--;
} }
if (sample) { if (sample) {
samples[index] = getADC(1); samples[index] = getADC(1);
index = (index + 1) % BATTFILTERDEPTH; index = (index + 1) % BATTFILTERDEPTH;
} }
uint32_t sum = 0; uint32_t sum = 0;
for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) for (uint8_t i = 0; i < BATTFILTERDEPTH; i++)
sum += samples[i]; sum += samples[i];
sum /= BATTFILTERDEPTH; sum /= BATTFILTERDEPTH;
if (divisor == 0) { if (divisor == 0) {
divisor = 1; divisor = 1;
} }
return sum * 4 / divisor; return sum * 4 / divisor;
} }
void unstick_I2C() { void unstick_I2C() {
/* configure SDA/SCL for GPIO */ /* configure SDA/SCL for GPIO */
GPIO_BC(GPIOB) |= SDA_Pin | SCL_Pin; GPIO_BC(GPIOB) |= SDA_Pin | SCL_Pin;
gpio_init(SDA_GPIO_Port, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, gpio_init(SDA_GPIO_Port, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,
SDA_Pin | SCL_Pin); SDA_Pin | SCL_Pin);
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
GPIO_BOP(GPIOB) |= SCL_Pin; GPIO_BOP(GPIOB) |= SCL_Pin;
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
GPIO_BOP(GPIOB) |= SDA_Pin; GPIO_BOP(GPIOB) |= SDA_Pin;
/* connect PB6 to I2C0_SCL */ /* connect PB6 to I2C0_SCL */
/* connect PB7 to I2C0_SDA */ /* connect PB7 to I2C0_SDA */
gpio_init(SDA_GPIO_Port, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, gpio_init(SDA_GPIO_Port, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ,
SDA_Pin | SCL_Pin); SDA_Pin | SCL_Pin);
} }
uint8_t getButtonA() { uint8_t getButtonA() {
return (gpio_input_bit_get(KEY_A_GPIO_Port, KEY_A_Pin) == SET) ? 1 : 0; return (gpio_input_bit_get(KEY_A_GPIO_Port, KEY_A_Pin) == SET) ? 1 : 0;
} }
uint8_t getButtonB() { uint8_t getButtonB() {
return (gpio_input_bit_get(KEY_B_GPIO_Port, KEY_B_Pin) == SET) ? 1 : 0; return (gpio_input_bit_get(KEY_B_GPIO_Port, KEY_B_Pin) == SET) ? 1 : 0;
} }
void reboot() { void reboot() {
// TODO // TODO
for (;;) { for (;;) {
} }
} }
void delay_ms(uint16_t count) { delay_1ms(count); } void delay_ms(uint16_t count) {
delay_1ms(count);
}