1
0
forked from me/IronOS

Formatting pass

This commit is contained in:
Ben V. Brown
2021-04-27 19:11:12 +10:00
parent 3780a76be8
commit fd78aa192a
13 changed files with 814 additions and 897 deletions

View File

@@ -9,234 +9,219 @@
#include "main.hpp" #include "main.hpp"
#include <IRQ.h> #include <IRQ.h>
volatile uint16_t PWMSafetyTimer = 0; volatile uint16_t PWMSafetyTimer = 0;
volatile uint8_t pendingPWM = 0; volatile uint8_t pendingPWM = 0;
uint16_t totalPWM = 255; uint16_t totalPWM = 255;
const uint16_t powerPWM = 255; const uint16_t powerPWM = 255;
history<uint16_t, PID_TIM_HZ> rawTempFilter = { { 0 }, 0, 0 }; history<uint16_t, PID_TIM_HZ> rawTempFilter = {{0}, 0, 0};
void resetWatchdog() { void resetWatchdog() { HAL_IWDG_Refresh(&hiwdg); }
HAL_IWDG_Refresh(&hiwdg);
}
#ifdef TEMP_NTC #ifdef TEMP_NTC
// Lookup table for the NTC // Lookup table for the NTC
// Stored as ADCReading,Temp in degC // Stored as ADCReading,Temp in degC
static const uint16_t NTCHandleLookup[] = { static const uint16_t NTCHandleLookup[] = {
// ADC Reading , Temp in C // ADC Reading , Temp in C
29189, 0, // 29189, 0, //
29014, 1, // 29014, 1, //
28832, 2, // 28832, 2, //
28644, 3, // 28644, 3, //
28450, 4, // 28450, 4, //
28249, 5, // 28249, 5, //
28042, 6, // 28042, 6, //
27828, 7, // 27828, 7, //
27607, 8, // 27607, 8, //
27380, 9, // 27380, 9, //
27146, 10, // 27146, 10, //
26906, 11, // 26906, 11, //
26660, 12, // 26660, 12, //
26407, 13, // 26407, 13, //
26147, 14, // 26147, 14, //
25882, 15, // 25882, 15, //
25610, 16, // 25610, 16, //
25332, 17, // 25332, 17, //
25049, 18, // 25049, 18, //
24759, 19, // 24759, 19, //
24465, 20, // 24465, 20, //
24164, 21, // 24164, 21, //
23859, 22, // 23859, 22, //
23549, 23, // 23549, 23, //
23234, 24, // 23234, 24, //
22915, 25, // 22915, 25, //
22591, 26, // 22591, 26, //
22264, 27, // 22264, 27, //
21933, 28, // 21933, 28, //
21599, 29, // 21599, 29, //
21261, 30, // 21261, 30, //
20921, 31, // 20921, 31, //
20579, 32, // 20579, 32, //
20234, 33, // 20234, 33, //
19888, 34, // 19888, 34, //
19541, 35, // 19541, 35, //
19192, 36, // 19192, 36, //
18843, 37, // 18843, 37, //
18493, 38, // 18493, 38, //
18143, 39, // 18143, 39, //
17793, 40, // 17793, 40, //
17444, 41, // 17444, 41, //
17096, 42, // 17096, 42, //
16750, 43, // 16750, 43, //
16404, 44, // 16404, 44, //
16061, 45, // 16061, 45, //
// 15719, 46, // // 15719, 46, //
// 15380, 47, // // 15380, 47, //
// 15044, 48, // // 15044, 48, //
// 14710, 49, // // 14710, 49, //
// 14380, 50, // // 14380, 50, //
// 14053, 51, // // 14053, 51, //
// 13729, 52, // // 13729, 52, //
// 13410, 53, // // 13410, 53, //
// 13094, 54, // // 13094, 54, //
// 12782, 55, // // 12782, 55, //
// 12475, 56, // // 12475, 56, //
// 12172, 57, // // 12172, 57, //
// 11874, 58, // // 11874, 58, //
// 11580, 59, // // 11580, 59, //
// 11292, 60, // // 11292, 60, //
}; };
#endif #endif
// These are called by the HAL after the corresponding events from the system // These are called by the HAL after the corresponding events from the system
// timers. // timers.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
// Period has elapsed // Period has elapsed
if (htim->Instance == TIM1) { if (htim->Instance == TIM1) {
// STM uses this for internal functions as a counter for timeouts // STM uses this for internal functions as a counter for timeouts
HAL_IncTick(); HAL_IncTick();
} }
} }
uint16_t getHandleTemperature() { uint16_t getHandleTemperature() {
return 250; //TODO return 250; // TODO
} }
uint16_t getTipInstantTemperature() { uint16_t getTipInstantTemperature() { return getADC(2); }
return getADC(2);
}
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 getInputVoltageX10(uint16_t divisor, uint8_t sample) { uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
// ADC maximum is 32767 == 3.3V at input == 28.05V at VIN // ADC maximum is 32767 == 3.3V at input == 28.05V at VIN
// Therefore we can divide down from there // Therefore we can divide down from there
// Multiplying ADC max by 4 for additional calibration options, // Multiplying ADC max by 4 for additional calibration options,
// ideal term is 467 // ideal term is 467
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;
} }
bool tryBetterPWM(uint8_t pwm) { bool tryBetterPWM(uint8_t pwm) {
//We dont need this for the MHP30 // We dont need this for the MHP30
return false; return false;
} }
void setTipPWM(uint8_t pulse) { void setTipPWM(uint8_t pulse) {
//We can just set the timer directly // We can just set the timer directly
htim3.Instance->CCR1 = pulse; htim3.Instance->CCR1 = pulse;
} }
void unstick_I2C() { void unstick_I2C() {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
int timeout = 100; int timeout = 100;
int timeout_cnt = 0; int timeout_cnt = 0;
// 1. Clear PE bit. // 1. Clear PE bit.
hi2c1.Instance->CR1 &= ~(0x0001); hi2c1.Instance->CR1 &= ~(0x0001);
/**I2C1 GPIO Configuration /**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA PB7 ------> I2C1_SDA
*/ */
// 2. Configure the SCL and SDA I/Os as General Purpose Output Open-Drain, High level (Write 1 to GPIOx_ODR). // 2. Configure the SCL and SDA I/Os as General Purpose Output Open-Drain, High level (Write 1 to GPIOx_ODR).
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = SCL_Pin; GPIO_InitStruct.Pin = SCL_Pin;
HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = SDA_Pin; GPIO_InitStruct.Pin = SDA_Pin;
HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET);
while (GPIO_PIN_SET != HAL_GPIO_ReadPin(SDA_GPIO_Port, SDA_Pin)) { while (GPIO_PIN_SET != HAL_GPIO_ReadPin(SDA_GPIO_Port, SDA_Pin)) {
// Move clock to release I2C // Move clock to release I2C
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET);
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET);
timeout_cnt++; timeout_cnt++;
if (timeout_cnt > timeout) if (timeout_cnt > timeout)
return; return;
} }
// 12. Configure the SCL and SDA I/Os as Alternate function Open-Drain. // 12. Configure the SCL and SDA I/Os as Alternate function Open-Drain.
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = SCL_Pin; GPIO_InitStruct.Pin = SCL_Pin;
HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SDA_Pin; GPIO_InitStruct.Pin = SDA_Pin;
HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET);
// 13. Set SWRST bit in I2Cx_CR1 register. // 13. Set SWRST bit in I2Cx_CR1 register.
hi2c1.Instance->CR1 |= 0x8000; hi2c1.Instance->CR1 |= 0x8000;
asm("nop"); asm("nop");
// 14. Clear SWRST bit in I2Cx_CR1 register. // 14. Clear SWRST bit in I2Cx_CR1 register.
hi2c1.Instance->CR1 &= ~0x8000; hi2c1.Instance->CR1 &= ~0x8000;
asm("nop"); asm("nop");
// 15. Enable the I2C peripheral by setting the PE bit in I2Cx_CR1 register // 15. Enable the I2C peripheral by setting the PE bit in I2Cx_CR1 register
hi2c1.Instance->CR1 |= 0x0001; hi2c1.Instance->CR1 |= 0x0001;
// Call initialization function. // Call initialization function.
HAL_I2C_Init(&hi2c1); HAL_I2C_Init(&hi2c1);
} }
uint8_t getButtonA() { uint8_t getButtonA() { return HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ? 1 : 0; }
return HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ? uint8_t getButtonB() { return HAL_GPIO_ReadPin(KEY_B_GPIO_Port, KEY_B_Pin) == GPIO_PIN_RESET ? 1 : 0; }
1 : 0;
}
uint8_t getButtonB() {
return HAL_GPIO_ReadPin(KEY_B_GPIO_Port, KEY_B_Pin) == GPIO_PIN_RESET ?
1 : 0;
}
void BSPInit(void) { void BSPInit(void) {}
}
void reboot() { void reboot() { NVIC_SystemReset(); }
NVIC_SystemReset();
}
void delay_ms(uint16_t count) { void delay_ms(uint16_t count) { HAL_Delay(count); }
HAL_Delay(count);
}

View File

@@ -16,43 +16,30 @@
*/ */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) { void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE; BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (hadc == &hadc1) { if (hadc == &hadc1) {
if (pidTaskNotification) { if (pidTaskNotification) {
vTaskNotifyGiveFromISR(pidTaskNotification, vTaskNotifyGiveFromISR(pidTaskNotification, &xHigherPriorityTaskWoken);
&xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); }
} }
}
}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
}
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
}
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
}
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
}
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
} }
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c __unused) { FRToSI2C::CpltCallback(); }
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c __unused) { FRToSI2C::CpltCallback(); }
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c __unused) { FRToSI2C::CpltCallback(); }
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c __unused) { FRToSI2C::CpltCallback(); }
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c __unused) { FRToSI2C::CpltCallback(); }
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c __unused) { FRToSI2C::CpltCallback(); }
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
(void) GPIO_Pin; (void)GPIO_Pin;
InterruptHandler::irqCallback(); InterruptHandler::irqCallback();
} }
bool getFUS302IRQLow() { bool getFUS302IRQLow() {
#ifdef POW_PD #ifdef POW_PD
// Return true if the IRQ line is still held low // Return true if the IRQ line is still held low
return HAL_GPIO_ReadPin(INT_PD_GPIO_Port, INT_PD_Pin) == GPIO_PIN_RESET; return HAL_GPIO_ReadPin(INT_PD_GPIO_Port, INT_PD_Pin) == GPIO_PIN_RESET;
#else #else
return false; return false;
#endif #endif

View File

@@ -21,29 +21,28 @@
#define TIP_TEMP_ADC1_CHANNEL ADC_CHANNEL_2 #define TIP_TEMP_ADC1_CHANNEL ADC_CHANNEL_2
#define TIP_TEMP_ADC2_CHANNEL ADC_CHANNEL_2 #define TIP_TEMP_ADC2_CHANNEL ADC_CHANNEL_2
#define VIN_Pin GPIO_PIN_1 #define VIN_Pin GPIO_PIN_1
#define VIN_GPIO_Port GPIOA #define VIN_GPIO_Port GPIOA
#define VIN_ADC1_CHANNEL ADC_CHANNEL_1 #define VIN_ADC1_CHANNEL ADC_CHANNEL_1
#define VIN_ADC2_CHANNEL ADC_CHANNEL_1 #define VIN_ADC2_CHANNEL ADC_CHANNEL_1
#define OLED_RESET_Pin GPIO_PIN_4 #define OLED_RESET_Pin GPIO_PIN_4
#define OLED_RESET_GPIO_Port GPIOB #define OLED_RESET_GPIO_Port GPIOB
#define KEY_A_Pin GPIO_PIN_10 #define KEY_A_Pin GPIO_PIN_10
#define KEY_A_GPIO_Port GPIOA #define KEY_A_GPIO_Port GPIOA
#define PWM_Out_Pin GPIO_PIN_6 #define PWM_Out_Pin GPIO_PIN_6
#define PWM_Out_GPIO_Port GPIOA #define PWM_Out_GPIO_Port GPIOA
#define PWM_Out_CHANNEL TIM_CHANNEL_1 #define PWM_Out_CHANNEL TIM_CHANNEL_1
#define SCL_Pin GPIO_PIN_6 #define SCL_Pin GPIO_PIN_6
#define SCL_GPIO_Port GPIOB #define SCL_GPIO_Port GPIOB
#define SDA_Pin GPIO_PIN_7 #define SDA_Pin GPIO_PIN_7
#define SDA_GPIO_Port GPIOB #define SDA_GPIO_Port GPIOB
#define SCL2_Pin GPIO_PIN_3 #define SCL2_Pin GPIO_PIN_3
#define SCL2_GPIO_Port GPIOB #define SCL2_GPIO_Port GPIOB
#define SDA2_Pin GPIO_PIN_15 #define SDA2_Pin GPIO_PIN_15
#define SDA2_GPIO_Port GPIOA #define SDA2_GPIO_Port GPIOA
#define INT_PD_Pin GPIO_PIN_5 #define INT_PD_Pin GPIO_PIN_5
#define INT_PD_GPIO_Port GPIOB #define INT_PD_GPIO_Port GPIOB
#define HEAT_EN_Pin GPIO_PIN_3 #define HEAT_EN_Pin GPIO_PIN_3
#define HEAT_EN_GPIO_Port GPIOA #define HEAT_EN_GPIO_Port GPIOA
#endif /* BSP_MINIWARE_PINS_H_ */ #endif /* BSP_MINIWARE_PINS_H_ */

View File

@@ -15,8 +15,8 @@ DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx; DMA_HandleTypeDef hdma_i2c1_tx;
IWDG_HandleTypeDef hiwdg; IWDG_HandleTypeDef hiwdg;
TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3; TIM_HandleTypeDef htim3;
#define ADC_CHANNELS 3 #define ADC_CHANNELS 3
#define ADC_SAMPLES 16 #define ADC_SAMPLES 16
uint32_t ADCReadings[ADC_SAMPLES * ADC_CHANNELS]; // room for 32 lots of the pair of readings uint32_t ADCReadings[ADC_SAMPLES * ADC_CHANNELS]; // room for 32 lots of the pair of readings
@@ -31,345 +31,338 @@ static void MX_TIM2_Init(void);
static void MX_DMA_Init(void); static void MX_DMA_Init(void);
static void MX_GPIO_Init(void); static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void); static void MX_ADC2_Init(void);
void Setup_HAL() { void Setup_HAL() {
SystemClock_Config(); SystemClock_Config();
__HAL_AFIO_REMAP_SWJ_NOJTAG(); __HAL_AFIO_REMAP_SWJ_NOJTAG();
MX_GPIO_Init(); MX_GPIO_Init();
MX_DMA_Init(); MX_DMA_Init();
MX_I2C1_Init(); MX_I2C1_Init();
MX_ADC1_Init(); MX_ADC1_Init();
MX_ADC2_Init(); MX_ADC2_Init();
MX_TIM3_Init(); MX_TIM3_Init();
MX_TIM2_Init(); MX_TIM2_Init();
MX_IWDG_Init(); MX_IWDG_Init();
HAL_ADC_Start(&hadc2); HAL_ADC_Start(&hadc2);
HAL_ADCEx_MultiModeStart_DMA(&hadc1, ADCReadings, HAL_ADCEx_MultiModeStart_DMA(&hadc1, ADCReadings,
(ADC_SAMPLES * ADC_CHANNELS)); // start DMA of normal readings (ADC_SAMPLES * ADC_CHANNELS)); // start DMA of normal readings
HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings
HAL_ADCEx_InjectedStart(&hadc2); // enable injected readings HAL_ADCEx_InjectedStart(&hadc2); // enable injected readings
} }
// channel 0 -> temperature sensor, 1-> VIN, 2-> tip // channel 0 -> temperature sensor, 1-> VIN, 2-> tip
uint16_t getADC(uint8_t channel) { uint16_t getADC(uint8_t channel) {
uint32_t sum = 0; uint32_t sum = 0;
for (uint8_t i = 0; i < ADC_SAMPLES; i++) { for (uint8_t i = 0; i < ADC_SAMPLES; i++) {
uint16_t adc1Sample = ADCReadings[channel + (i * ADC_CHANNELS)]; uint16_t adc1Sample = ADCReadings[channel + (i * ADC_CHANNELS)];
uint16_t adc2Sample = ADCReadings[channel + (i * ADC_CHANNELS)] >> 16; uint16_t adc2Sample = ADCReadings[channel + (i * ADC_CHANNELS)] >> 16;
sum += (adc1Sample + adc2Sample); sum += (adc1Sample + adc2Sample);
} }
return sum >> 2; return sum >> 2;
} }
/** System Clock Configuration /** System Clock Configuration
*/ */
void SystemClock_Config(void) { void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit; RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks /**Initializes the CPU, AHB and APB busses clocks
*/ */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI;
| RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64MHz
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64MHz HAL_RCC_OscConfig(&RCC_OscInitStruct);
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/**Initializes the CPU, AHB and APB busses clocks /**Initializes the CPU, AHB and APB busses clocks
*/ */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16; // TIM
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16; // TIM // 2,3,4,5,6,7,12,13,14
// 2,3,4,5,6,7,12,13,14 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 mhz to some peripherals and adc
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 mhz to some peripherals and adc
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_CFGR_ADCPRE_DIV8; // 6 or 8 are the only non overclocked options PeriphClkInit.AdcClockSelection = RCC_CFGR_ADCPRE_DIV8; // 6 or 8 are the only non overclocked options
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
/**Configure the Systick interrupt time /**Configure the Systick interrupt time
*/ */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
/**Configure the Systick /**Configure the Systick
*/ */
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */ /* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0); HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
} }
/* ADC1 init function */ /* ADC1 init function */
static void MX_ADC1_Init(void) { static void MX_ADC1_Init(void) {
ADC_MultiModeTypeDef multimode; ADC_MultiModeTypeDef multimode;
ADC_ChannelConfTypeDef sConfig; ADC_ChannelConfTypeDef sConfig;
/**Common config /**Common config
*/ */
hadc1.Instance = ADC1; hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE; hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = ADC_CHANNELS; hadc1.Init.NbrOfConversion = ADC_CHANNELS;
HAL_ADC_Init(&hadc1); HAL_ADC_Init(&hadc1);
/**Configure the ADC multi-mode /**Configure the ADC multi-mode
*/ */
multimode.Mode = ADC_DUALMODE_REGSIMULT; multimode.Mode = ADC_DUALMODE_REGSIMULT;
HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode); HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode);
/**Configure Regular Channel /**Configure Regular Channel
*/ */
sConfig.Channel = TMP36_ADC1_CHANNEL; sConfig.Channel = TMP36_ADC1_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure Regular Channel /**Configure Regular Channel
*/ */
sConfig.Channel = VIN_ADC1_CHANNEL; sConfig.Channel = VIN_ADC1_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_2; sConfig.Rank = ADC_REGULAR_RANK_2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); HAL_ADC_ConfigChannel(&hadc1, &sConfig);
sConfig.Channel = TIP_TEMP_ADC1_CHANNEL; sConfig.Channel = TIP_TEMP_ADC1_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_3; sConfig.Rank = ADC_REGULAR_RANK_3;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); HAL_ADC_ConfigChannel(&hadc1, &sConfig);
SET_BIT(hadc1.Instance->CR1, (ADC_CR1_EOSIE)); // Enable end of Normal SET_BIT(hadc1.Instance->CR1, (ADC_CR1_EOSIE)); // Enable end of Normal
// Run ADC internal calibration // Run ADC internal calibration
while (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK) while (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK)
; ;
} }
/* ADC2 init function */ /* ADC2 init function */
static void MX_ADC2_Init(void) { static void MX_ADC2_Init(void) {
ADC_ChannelConfTypeDef sConfig; ADC_ChannelConfTypeDef sConfig;
/**Common config /**Common config
*/ */
hadc2.Instance = ADC2; hadc2.Instance = ADC2;
hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc2.Init.ContinuousConvMode = ENABLE; hadc2.Init.ContinuousConvMode = ENABLE;
hadc2.Init.DiscontinuousConvMode = DISABLE; hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.NbrOfConversion = ADC_CHANNELS; hadc2.Init.NbrOfConversion = ADC_CHANNELS;
HAL_ADC_Init(&hadc2); HAL_ADC_Init(&hadc2);
/**Configure Regular Channel /**Configure Regular Channel
*/ */
sConfig.Channel = TMP36_ADC2_CHANNEL; sConfig.Channel = TMP36_ADC2_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
HAL_ADC_ConfigChannel(&hadc2, &sConfig); HAL_ADC_ConfigChannel(&hadc2, &sConfig);
sConfig.Channel = VIN_ADC2_CHANNEL; sConfig.Channel = VIN_ADC2_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_2; sConfig.Rank = ADC_REGULAR_RANK_2;
HAL_ADC_ConfigChannel(&hadc2, &sConfig); HAL_ADC_ConfigChannel(&hadc2, &sConfig);
sConfig.Channel = TIP_TEMP_ADC1_CHANNEL; sConfig.Channel = TIP_TEMP_ADC1_CHANNEL;
sConfig.Rank = ADC_REGULAR_RANK_2; sConfig.Rank = ADC_REGULAR_RANK_2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); HAL_ADC_ConfigChannel(&hadc1, &sConfig);
// Run ADC internal calibration // Run ADC internal calibration
while (HAL_ADCEx_Calibration_Start(&hadc2) != HAL_OK) while (HAL_ADCEx_Calibration_Start(&hadc2) != HAL_OK)
; ;
} }
/* I2C1 init function */ /* I2C1 init function */
static void MX_I2C1_Init(void) { static void MX_I2C1_Init(void) {
hi2c1.Instance = I2C1; hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 75000; //TODO we can probs run this fast hi2c1.Init.ClockSpeed = 75000; // TODO we can probs run this fast
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&hi2c1); HAL_I2C_Init(&hi2c1);
} }
/* IWDG init function */ /* IWDG init function */
static void MX_IWDG_Init(void) { static void MX_IWDG_Init(void) {
hiwdg.Instance = IWDG; hiwdg.Instance = IWDG;
hiwdg.Init.Prescaler = IWDG_PRESCALER_256; hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
hiwdg.Init.Reload = 100; hiwdg.Init.Reload = 100;
#ifndef SWD_ENABLE #ifndef SWD_ENABLE
HAL_IWDG_Init(&hiwdg); HAL_IWDG_Init(&hiwdg);
#endif #endif
} }
/* TIM3 init function */ /* TIM3 init function */
static void MX_TIM3_Init(void) { static void MX_TIM3_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig; TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig; TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC; TIM_OC_InitTypeDef sConfigOC;
htim3.Instance = TIM3; htim3.Instance = TIM3;
htim3.Init.Prescaler = 1; htim3.Init.Prescaler = 1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 255; // htim3.Init.Period = 255; //
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // 4mhz before div htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // 4mhz before div
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; // Preload the ARR register (though we dont use this) htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; // Preload the ARR register (though we dont use this)
HAL_TIM_Base_Init(&htim3); HAL_TIM_Base_Init(&htim3);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig); HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim3); HAL_TIM_PWM_Init(&htim3);
HAL_TIM_OC_Init(&htim3); HAL_TIM_OC_Init(&htim3);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0; //Output control sConfigOC.Pulse = 0; // Output control
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE; sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL);
//TODO need to do buzzer // TODO need to do buzzer
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
/**TIM3 GPIO Configuration /**TIM3 GPIO Configuration
PWM_Out_Pin ------> TIM3_CH1 PWM_Out_Pin ------> TIM3_CH1
*/ */
GPIO_InitStruct.Pin = PWM_Out_Pin; GPIO_InitStruct.Pin = PWM_Out_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // We would like sharp rising edges GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // We would like sharp rising edges
HAL_GPIO_Init(PWM_Out_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(PWM_Out_GPIO_Port, &GPIO_InitStruct);
HAL_TIM_PWM_Start(&htim3, PWM_Out_CHANNEL); HAL_TIM_PWM_Start(&htim3, PWM_Out_CHANNEL);
} }
/* TIM3 init function */ /* TIM3 init function */
static void MX_TIM2_Init(void) { static void MX_TIM2_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig; TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig; TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC; TIM_OC_InitTypeDef sConfigOC;
htim2.Instance = TIM2; htim2.Instance = TIM2;
htim2.Init.Prescaler = 200; // 2 MHz timer clock/2000 = 1 kHz tick rate htim2.Init.Prescaler = 200; // 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 // 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" // 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; htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
// dummy value, will be reconfigured by BSPInit() // dummy value, will be reconfigured by BSPInit()
htim2.Init.Period = 10; htim2.Init.Period = 10;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // 8 MHz (x2 APB1) before divide htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // 8 MHz (x2 APB1) before divide
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0; htim2.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim2); HAL_TIM_Base_Init(&htim2);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig); HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim2); HAL_TIM_PWM_Init(&htim2);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.OCMode = TIM_OCMODE_PWM1;
// dummy value, will be reconfigured by BSPInit() in the BSP.cpp // dummy value, will be reconfigured by BSPInit() in the BSP.cpp
sConfigOC.Pulse = 5; sConfigOC.Pulse = 5;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE; sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = HEAT_EN_Pin; GPIO_InitStruct.Pin = HEAT_EN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // We would like sharp rising edges GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // We would like sharp rising edges
HAL_GPIO_Init(HEAT_EN_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(HEAT_EN_GPIO_Port, &GPIO_InitStruct);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
} }
/** /**
* Enable DMA controller clock * Enable DMA controller clock
*/ */
static void MX_DMA_Init(void) { static void MX_DMA_Init(void) {
/* DMA controller clock enable */ /* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE(); __HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */ /* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */ /* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0); HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
/* DMA1_Channel6_IRQn interrupt configuration */ /* DMA1_Channel6_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 5, 0); HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn); HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
/* DMA1_Channel7_IRQn interrupt configuration */ /* DMA1_Channel7_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 5, 0); HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn); HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
} }
static void MX_GPIO_Init(void) { static void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */ /* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */ /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
/*Configure GPIO pins : PD0 PD1 */ /*Configure GPIO pins : PD0 PD1 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure peripheral I/O remapping */ /*Configure peripheral I/O remapping */
__HAL_AFIO_REMAP_PD01_ENABLE(); __HAL_AFIO_REMAP_PD01_ENABLE();
//^ remap XTAL so that pins can be analog (all input buffers off). //^ remap XTAL so that pins can be analog (all input buffers off).
// reduces power consumption // reduces power consumption
/* /*
* Configure All pins as analog by default * Configure All pins as analog by default
*/ */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_15;
| GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
| GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_15; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PIN_3 |
GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9
| GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13
| GPIO_PIN_14 | GPIO_PIN_15;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : KEY_B_Pin KEY_A_Pin */ /*Configure GPIO pins : KEY_B_Pin KEY_A_Pin */
GPIO_InitStruct.Pin = KEY_B_Pin; GPIO_InitStruct.Pin = KEY_B_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(KEY_B_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(KEY_B_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = KEY_A_Pin; GPIO_InitStruct.Pin = KEY_A_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(KEY_A_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(KEY_A_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : OLED_RESET_Pin */ /*Configure GPIO pin : OLED_RESET_Pin */
GPIO_InitStruct.Pin = OLED_RESET_Pin; GPIO_InitStruct.Pin = OLED_RESET_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(OLED_RESET_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(OLED_RESET_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET);
// Pull down LCD reset // Pull down LCD reset
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET);
HAL_Delay(30); HAL_Delay(30);
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET);
} }
#ifdef USE_FULL_ASSERT #ifdef USE_FULL_ASSERT

View File

@@ -13,37 +13,35 @@
static uint16_t settings_page[512] __attribute__((section(".settings_page"))); static uint16_t settings_page[512] __attribute__((section(".settings_page")));
uint8_t flash_save_buffer(const uint8_t *buffer, const uint16_t length) { uint8_t flash_save_buffer(const uint8_t *buffer, const uint16_t length) {
return; //TODO return; // TODO
FLASH_EraseInitTypeDef pEraseInit; FLASH_EraseInitTypeDef pEraseInit;
pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES; pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
pEraseInit.Banks = FLASH_BANK_1; pEraseInit.Banks = FLASH_BANK_1;
pEraseInit.NbPages = 1; pEraseInit.NbPages = 1;
pEraseInit.PageAddress = (uint32_t) settings_page; pEraseInit.PageAddress = (uint32_t)settings_page;
uint32_t failingAddress = 0; uint32_t failingAddress = 0;
resetWatchdog(); resetWatchdog();
__HAL_FLASH_CLEAR_FLAG( __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY);
FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY); HAL_FLASH_Unlock();
HAL_FLASH_Unlock(); HAL_Delay(1);
HAL_Delay(1); resetWatchdog();
resetWatchdog(); HAL_FLASHEx_Erase(&pEraseInit, &failingAddress);
HAL_FLASHEx_Erase(&pEraseInit, &failingAddress); //^ Erase the page of flash (1024 bytes on this stm32)
//^ Erase the page of flash (1024 bytes on this stm32) // erased the chunk
// erased the chunk // now we program it
// now we program it uint16_t *data = (uint16_t *)buffer;
uint16_t *data = (uint16_t*) buffer; HAL_FLASH_Unlock();
HAL_FLASH_Unlock(); for (uint16_t i = 0; i < (length / 2); i++) {
for (uint16_t i = 0; i < (length / 2); i++) { resetWatchdog();
resetWatchdog(); HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)&settings_page[i], data[i]);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, }
(uint32_t) &settings_page[i], data[i]); HAL_FLASH_Lock();
} return 1;
HAL_FLASH_Lock();
return 1;
} }
void flash_read_buffer(uint8_t *buffer, const uint16_t length) { void flash_read_buffer(uint8_t *buffer, const uint16_t length) {
memset(buffer, 0, length); memset(buffer, 0, length);
return; // TODO return; // TODO
memcpy(buffer, settings_page, length); memcpy(buffer, settings_page, length);
} }

View File

@@ -2,10 +2,10 @@
#ifdef POW_PD #ifdef POW_PD
#include "BSP.h" #include "BSP.h"
#include "I2C_Wrapper.hpp" #include "I2C_Wrapper.hpp"
#include "Pins.h"
#include "Setup.h" #include "Setup.h"
#include "fusb302b.h" #include "fusb302b.h"
#include "fusb_user.h" #include "fusb_user.h"
#include "Pins.h"
/* /*
* Read a single byte from the FUSB302B * Read a single byte from the FUSB302B
* *
@@ -15,11 +15,11 @@
* Returns the value read from addr. * Returns the value read from addr.
*/ */
uint8_t fusb_read_byte(uint8_t addr) { uint8_t fusb_read_byte(uint8_t addr) {
uint8_t data[1]; uint8_t data[1];
if (!FRToSI2C::Mem_Read(FUSB302B_ADDR, addr, (uint8_t*) data, 1)) { if (!FRToSI2C::Mem_Read(FUSB302B_ADDR, addr, (uint8_t *)data, 1)) {
return 0; return 0;
} }
return data[0]; return data[0];
} }
/* /*
@@ -30,9 +30,7 @@ uint8_t fusb_read_byte(uint8_t addr) {
* size: The number of bytes to read * size: The number of bytes to read
* buf: The buffer into which data will be read * buf: The buffer into which data will be read
*/ */
bool fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf) { bool fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf) { return FRToSI2C::Mem_Read(FUSB302B_ADDR, addr, buf, size); }
return FRToSI2C::Mem_Read(FUSB302B_ADDR, addr, buf, size);
}
/* /*
* Write a single byte to the FUSB302B * Write a single byte to the FUSB302B
@@ -41,9 +39,7 @@ bool fusb_read_buf(uint8_t addr, uint8_t size, uint8_t *buf) {
* addr: The memory address to which we will write * addr: The memory address to which we will write
* byte: The value to write * byte: The value to write
*/ */
bool fusb_write_byte(uint8_t addr, uint8_t byte) { bool fusb_write_byte(uint8_t addr, uint8_t byte) { return FRToSI2C::Mem_Write(FUSB302B_ADDR, addr, (uint8_t *)&byte, 1); }
return FRToSI2C::Mem_Write(FUSB302B_ADDR, addr, (uint8_t*) &byte, 1);
}
/* /*
* Write multiple bytes to the FUSB302B * Write multiple bytes to the FUSB302B
@@ -53,23 +49,21 @@ bool fusb_write_byte(uint8_t addr, uint8_t byte) {
* size: The number of bytes to write * size: The number of bytes to write
* buf: The buffer to write * buf: The buffer to write
*/ */
bool fusb_write_buf(uint8_t addr, uint8_t size, const uint8_t *buf) { bool fusb_write_buf(uint8_t addr, uint8_t size, const uint8_t *buf) { return FRToSI2C::Mem_Write(FUSB302B_ADDR, addr, (uint8_t *)buf, size); }
return FRToSI2C::Mem_Write(FUSB302B_ADDR, addr, (uint8_t*)buf, size);
}
uint8_t fusb302_detect() { uint8_t fusb302_detect() {
// Probe the I2C bus for its address // Probe the I2C bus for its address
return FRToSI2C::probe(FUSB302B_ADDR); return FRToSI2C::probe(FUSB302B_ADDR);
} }
void setupFUSBIRQ() { void setupFUSBIRQ() {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = INT_PD_Pin; GPIO_InitStruct.Pin = INT_PD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(INT_PD_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(INT_PD_GPIO_Port, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 10, 0); HAL_NVIC_SetPriority(EXTI9_5_IRQn, 10, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
} }
#endif #endif

View File

@@ -13,13 +13,13 @@
#include "fusbpd.h" #include "fusbpd.h"
#include <I2C_Wrapper.hpp> #include <I2C_Wrapper.hpp>
void preRToSInit() { void preRToSInit() {
/* Reset of all peripherals, Initializes the Flash interface and the Systick. /* Reset of all peripherals, Initializes the Flash interface and the Systick.
*/ */
SCB->VTOR = FLASH_BASE; //Set vector table offset SCB->VTOR = FLASH_BASE; // Set vector table offset
HAL_Init(); HAL_Init();
Setup_HAL(); // Setup all the HAL objects Setup_HAL(); // Setup all the HAL objects
BSPInit(); BSPInit();
I2CBB::init(); I2CBB::init();
/* Init the IPC objects */ /* Init the IPC objects */
FRToSI2C::FRToSInit(); FRToSI2C::FRToSInit();
} }

View File

@@ -1,42 +1,34 @@
// This is the stock standard STM interrupt file full of handlers // This is the stock standard STM interrupt file full of handlers
#include "stm32f1xx_it.h" #include "stm32f1xx_it.h"
#include "Pins.h"
#include "Setup.h" #include "Setup.h"
#include "cmsis_os.h" #include "cmsis_os.h"
#include "stm32f1xx.h" #include "stm32f1xx.h"
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "Pins.h"
extern TIM_HandleTypeDef htim1; // used for the systick extern TIM_HandleTypeDef htim1; // used for the systick
/******************************************************************************/ /******************************************************************************/
/* Cortex-M3 Processor Interruption and Exception Handlers */ /* Cortex-M3 Processor Interruption and Exception Handlers */
/******************************************************************************/ /******************************************************************************/
void NMI_Handler(void) { void NMI_Handler(void) {}
}
// We have the assembly for a breakpoint trigger here to halt the system when a debugger is connected // We have the assembly for a breakpoint trigger here to halt the system when a debugger is connected
// Hardfault handler, often a screwup in the code // Hardfault handler, often a screwup in the code
void HardFault_Handler(void) { void HardFault_Handler(void) {}
}
// Memory management unit had an error // Memory management unit had an error
void MemManage_Handler(void) { void MemManage_Handler(void) {}
}
// Prefetcher or busfault occured // Prefetcher or busfault occured
void BusFault_Handler(void) { void BusFault_Handler(void) {}
}
void UsageFault_Handler(void) { void UsageFault_Handler(void) {}
}
void DebugMon_Handler(void) { void DebugMon_Handler(void) {}
}
// Systick is used by FreeRTOS tick // Systick is used by FreeRTOS tick
void SysTick_Handler(void) { void SysTick_Handler(void) { osSystickHandler(); }
osSystickHandler();
}
/******************************************************************************/ /******************************************************************************/
/* STM32F1xx Peripheral Interrupt Handlers */ /* STM32F1xx Peripheral Interrupt Handlers */
@@ -46,33 +38,17 @@ void SysTick_Handler(void) {
/******************************************************************************/ /******************************************************************************/
// DMA used to move the ADC readings into system ram // DMA used to move the ADC readings into system ram
void DMA1_Channel1_IRQHandler(void) { void DMA1_Channel1_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_adc1); }
HAL_DMA_IRQHandler(&hdma_adc1);
}
// ADC interrupt used for DMA // ADC interrupt used for DMA
void ADC1_2_IRQHandler(void) { void ADC1_2_IRQHandler(void) { HAL_ADC_IRQHandler(&hadc1); }
HAL_ADC_IRQHandler(&hadc1);
}
// Timer 1 has overflowed, used for HAL ticks // Timer 1 has overflowed, used for HAL ticks
void TIM1_UP_IRQHandler(void) { void TIM1_UP_IRQHandler(void) { HAL_TIM_IRQHandler(&htim1); }
HAL_TIM_IRQHandler(&htim1);
}
void I2C1_EV_IRQHandler(void) { void I2C1_EV_IRQHandler(void) { HAL_I2C_EV_IRQHandler(&hi2c1); }
HAL_I2C_EV_IRQHandler(&hi2c1); void I2C1_ER_IRQHandler(void) { HAL_I2C_ER_IRQHandler(&hi2c1); }
}
void I2C1_ER_IRQHandler(void) {
HAL_I2C_ER_IRQHandler(&hi2c1);
}
void DMA1_Channel6_IRQHandler(void) { void DMA1_Channel6_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_i2c1_tx); }
HAL_DMA_IRQHandler(&hdma_i2c1_tx);
}
void DMA1_Channel7_IRQHandler(void) { void DMA1_Channel7_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_i2c1_rx); }
HAL_DMA_IRQHandler(&hdma_i2c1_rx); void EXTI9_5_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(INT_PD_Pin); }
}
void EXTI9_5_IRQHandler(void) {
HAL_GPIO_EXTI_IRQHandler(INT_PD_Pin);
}

View File

@@ -10,308 +10,299 @@
#include <I2CBB.hpp> #include <I2CBB.hpp>
SemaphoreHandle_t I2CBB::I2CSemaphore = NULL; SemaphoreHandle_t I2CBB::I2CSemaphore = NULL;
StaticSemaphore_t I2CBB::xSemaphoreBuffer; StaticSemaphore_t I2CBB::xSemaphoreBuffer;
void I2CBB::init() { void I2CBB::init() {
// Set GPIO's to output open drain // Set GPIO's to output open drain
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStruct.Pin = SDA2_Pin; GPIO_InitStruct.Pin = SDA2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStruct.Pin = SCL2_Pin; GPIO_InitStruct.Pin = SCL2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(SCL2_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SCL2_GPIO_Port, &GPIO_InitStruct);
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
SOFT_SCL_HIGH(); SOFT_SCL_HIGH();
I2CSemaphore = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer); I2CSemaphore = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer);
unlock(); unlock();
} }
bool I2CBB::probe(uint8_t address) { bool I2CBB::probe(uint8_t address) {
if (!lock()) if (!lock())
return false; return false;
start(); start();
bool ack = send(address); bool ack = send(address);
stop(); stop();
unlock(); unlock();
return ack; return ack;
} }
bool I2CBB::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, bool I2CBB::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size) {
uint16_t Size) { if (!lock())
if (!lock()) return false;
return false; start();
start(); bool ack = send(DevAddress);
bool ack = send(DevAddress); if (!ack) {
if (!ack) { stop();
stop(); unlock();
unlock(); return false;
return false; }
} ack = send(MemAddress);
ack = send(MemAddress); if (!ack) {
if (!ack) { stop();
stop(); unlock();
unlock(); return false;
return false; }
} SOFT_SCL_LOW();
SOFT_SCL_LOW(); SOFT_I2C_DELAY();
SOFT_I2C_DELAY(); // stop();
// stop(); start();
start(); ack = send(DevAddress | 1);
ack = send(DevAddress | 1); if (!ack) {
if (!ack) { stop();
stop(); unlock();
unlock(); return false;
return false; }
} while (Size) {
while (Size) { pData[0] = read(Size > 1);
pData[0] = read(Size > 1); pData++;
pData++; Size--;
Size--; }
} stop();
stop(); unlock();
unlock(); return true;
return true;
} }
bool I2CBB::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, bool I2CBB::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, const uint8_t *pData, uint16_t Size) {
const uint8_t *pData, uint16_t Size) { if (!lock())
if (!lock()) return false;
return false; start();
start(); bool ack = send(DevAddress);
bool ack = send(DevAddress); if (!ack) {
if (!ack) { stop();
stop(); asm("bkpt");
asm("bkpt"); unlock();
unlock(); return false;
return false; }
} ack = send(MemAddress);
ack = send(MemAddress); if (!ack) {
if (!ack) { stop();
stop(); asm("bkpt");
asm("bkpt"); unlock();
unlock(); return false;
return false; }
} while (Size) {
while (Size) { resetWatchdog();
resetWatchdog(); ack = send(pData[0]);
ack = send(pData[0]); if (!ack) {
if (!ack) { stop();
stop(); asm("bkpt");
asm("bkpt"); unlock();
unlock(); return false;
return false; }
} pData++;
pData++; Size--;
Size--; }
} stop();
stop(); unlock();
unlock(); return true;
return true;
} }
void I2CBB::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { void I2CBB::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
if (!lock()) if (!lock())
return; return;
start(); start();
bool ack = send(DevAddress); bool ack = send(DevAddress);
if (!ack) { if (!ack) {
stop(); stop();
unlock(); unlock();
return; return;
} }
while (Size) { while (Size) {
ack = send(pData[0]); ack = send(pData[0]);
if (!ack) { if (!ack) {
stop(); stop();
unlock(); unlock();
return; return;
} }
pData++; pData++;
Size--; Size--;
} }
stop(); stop();
unlock(); unlock();
} }
void I2CBB::Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { void I2CBB::Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
if (!lock()) if (!lock())
return; return;
start(); start();
bool ack = send(DevAddress | 1); bool ack = send(DevAddress | 1);
if (!ack) { if (!ack) {
stop(); stop();
unlock(); unlock();
return; return;
} }
while (Size) { while (Size) {
pData[0] = read(Size > 1); pData[0] = read(Size > 1);
pData++; pData++;
Size--; Size--;
} }
stop(); stop();
unlock(); unlock();
} }
void I2CBB::TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, void I2CBB::TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx) {
uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx) { if (Size_tx == 0 && Size_rx == 0)
if (Size_tx == 0 && Size_rx == 0) return;
return; if (lock() == false)
if (lock() == false) return;
return; if (Size_tx) {
if (Size_tx) { start();
start(); bool ack = send(DevAddress);
bool ack = send(DevAddress); if (!ack) {
if (!ack) { stop();
stop(); unlock();
unlock(); return;
return; }
} while (Size_tx) {
while (Size_tx) { ack = send(pData_tx[0]);
ack = send(pData_tx[0]); if (!ack) {
if (!ack) { stop();
stop(); unlock();
unlock(); return;
return; }
} pData_tx++;
pData_tx++; Size_tx--;
Size_tx--; }
} }
} if (Size_rx) {
if (Size_rx) { start();
start(); bool ack = send(DevAddress | 1);
bool ack = send(DevAddress | 1); if (!ack) {
if (!ack) { stop();
stop(); unlock();
unlock(); return;
return; }
} while (Size_rx) {
while (Size_rx) { pData_rx[0] = read(Size_rx > 1);
pData_rx[0] = read(Size_rx > 1); pData_rx++;
pData_rx++; Size_rx--;
Size_rx--; }
} }
} stop();
stop(); unlock();
unlock();
} }
void I2CBB::start() { void I2CBB::start() {
/* I2C Start condition, data line goes low when clock is high */ /* I2C Start condition, data line goes low when clock is high */
SOFT_SCL_HIGH(); SOFT_SCL_HIGH();
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SDA_LOW(); SOFT_SDA_LOW();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SCL_LOW(); SOFT_SCL_LOW();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
} }
void I2CBB::stop() { void I2CBB::stop() {
/* I2C Stop condition, clock goes high when data is low */ /* I2C Stop condition, clock goes high when data is low */
SOFT_SDA_LOW(); SOFT_SDA_LOW();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SCL_HIGH(); SOFT_SCL_HIGH();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
} }
bool I2CBB::send(uint8_t value) { bool I2CBB::send(uint8_t value) {
for (uint8_t i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; i++) {
write_bit(value & 0x80); // write the most-significant bit write_bit(value & 0x80); // write the most-significant bit
value <<= 1; value <<= 1;
} }
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
bool ack = (read_bit() == 0); bool ack = (read_bit() == 0);
return ack; return ack;
} }
uint8_t I2CBB::read(bool ack) { uint8_t I2CBB::read(bool ack) {
uint8_t B = 0; uint8_t B = 0;
uint8_t i; uint8_t i;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
B <<= 1; B <<= 1;
B |= read_bit(); B |= read_bit();
} }
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
if (ack) if (ack)
write_bit(0); write_bit(0);
else else
write_bit(1); write_bit(1);
return B; return B;
} }
uint8_t I2CBB::read_bit() { uint8_t I2CBB::read_bit() {
uint8_t b; uint8_t b;
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SCL_HIGH(); SOFT_SCL_HIGH();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
if (SOFT_SDA_READ()) if (SOFT_SDA_READ())
b = 1; b = 1;
else else
b = 0; b = 0;
SOFT_SCL_LOW(); SOFT_SCL_LOW();
return b; return b;
} }
void I2CBB::unlock() { void I2CBB::unlock() { xSemaphoreGive(I2CSemaphore); }
xSemaphoreGive(I2CSemaphore);
}
bool I2CBB::lock() { bool I2CBB::lock() {
if (I2CSemaphore == NULL) { if (I2CSemaphore == NULL) {
asm("bkpt"); asm("bkpt");
} }
bool a = xSemaphoreTake(I2CSemaphore, (TickType_t)100) == pdTRUE; bool a = xSemaphoreTake(I2CSemaphore, (TickType_t)100) == pdTRUE;
return a; return a;
} }
bool I2CBB::I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data) { bool I2CBB::I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data) { return Mem_Write(address, reg, &data, 1); }
return Mem_Write(address, reg, &data, 1);
}
uint8_t I2CBB::I2C_RegisterRead(uint8_t address, uint8_t reg) { uint8_t I2CBB::I2C_RegisterRead(uint8_t address, uint8_t reg) {
uint8_t temp = 0; uint8_t temp = 0;
Mem_Read(address, reg, &temp, 1); Mem_Read(address, reg, &temp, 1);
return temp; return temp;
} }
void I2CBB::write_bit(uint8_t val) { void I2CBB::write_bit(uint8_t val) {
if (val) { if (val) {
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
} else { } else {
SOFT_SDA_LOW(); SOFT_SDA_LOW();
} }
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SCL_HIGH(); SOFT_SCL_HIGH();
SOFT_I2C_DELAY(); SOFT_I2C_DELAY();
SOFT_SCL_LOW(); SOFT_SCL_LOW();
} }
bool I2CBB::writeRegistersBulk(const uint8_t address, const I2C_REG *registers, bool I2CBB::writeRegistersBulk(const uint8_t address, const I2C_REG *registers, const uint8_t registersLength) {
const uint8_t registersLength) { for (int index = 0; index < registersLength; index++) {
for (int index = 0; index < registersLength; index++) { if (!I2C_RegisterWrite(address, registers[index].reg, registers[index].val)) {
if (!I2C_RegisterWrite(address, registers[index].reg, return false;
registers[index].val)) { }
return false; if (registers[index].pause_ms)
} delay_ms(registers[index].pause_ms);
if (registers[index].pause_ms) }
delay_ms(registers[index].pause_ms); return true;
}
return true;
} }
#endif #endif

View File

@@ -18,39 +18,36 @@
class I2CBB { class I2CBB {
public: public:
static void init(); static void init();
// Probe if device ACK's address or not // Probe if device ACK's address or not
static bool probe(uint8_t address); static bool probe(uint8_t address);
// Issues a complete 8bit register read // Issues a complete 8bit register read
static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress, static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size);
uint8_t *pData, uint16_t Size); // Implements a register write
// Implements a register write static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress, const uint8_t *pData, uint16_t Size);
static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress, static void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
const uint8_t *pData, uint16_t Size); static void Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
static void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size); static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx);
static void Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size); static bool I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data);
static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, static uint8_t I2C_RegisterRead(uint8_t address, uint8_t reg);
uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx); typedef struct {
static bool I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data); const uint8_t reg; // The register to write to
static uint8_t I2C_RegisterRead(uint8_t address, uint8_t reg); uint8_t val; // The value to write to this register
typedef struct { const uint8_t pause_ms; // How many ms to pause _after_ writing this reg
const uint8_t reg; // The register to write to } I2C_REG;
uint8_t val; // The value to write to this register static bool writeRegistersBulk(const uint8_t address, const I2C_REG *registers, const uint8_t registersLength);
const uint8_t pause_ms; // How many ms to pause _after_ writing this reg
} I2C_REG;
static bool writeRegistersBulk(const uint8_t address,
const I2C_REG *registers, const uint8_t registersLength);
private: private:
static SemaphoreHandle_t I2CSemaphore; static SemaphoreHandle_t I2CSemaphore;
static StaticSemaphore_t xSemaphoreBuffer; static StaticSemaphore_t xSemaphoreBuffer;
static void unlock(); static void unlock();
static bool lock(); static bool lock();
static void start(); static void start();
static void stop(); static void stop();
static bool send(uint8_t value); static bool send(uint8_t value);
static uint8_t read(bool ack); static uint8_t read(bool ack);
static uint8_t read_bit(); static uint8_t read_bit();
static void write_bit(uint8_t val); static void write_bit(uint8_t val);
}; };
#endif #endif
#endif /* BSP_MINIWARE_I2CBB_HPP_ */ #endif /* BSP_MINIWARE_I2CBB_HPP_ */

View File

@@ -10,8 +10,8 @@
#ifndef OLED_HPP_ #ifndef OLED_HPP_
#define OLED_HPP_ #define OLED_HPP_
#include "Font.h" #include "Font.h"
#include <BSP.h>
#include "Model_Config.h" #include "Model_Config.h"
#include <BSP.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#ifdef __cplusplus #ifdef __cplusplus
@@ -22,7 +22,6 @@ extern "C" {
} }
#endif #endif
#ifdef OLED_I2CBB #ifdef OLED_I2CBB
#include "I2CBB.hpp" #include "I2CBB.hpp"
#define I2C_CLASS I2CBB #define I2C_CLASS I2CBB
@@ -50,7 +49,7 @@ public:
static bool isInitDone(); static bool isInitDone();
// Draw the buffer out to the LCD using the DMA Channel // Draw the buffer out to the LCD using the DMA Channel
static void refresh() { static void refresh() {
I2C_CLASS::Transmit(DEVICEADDR_OLED, screenBuffer, FRAMEBUFFER_START + (OLED_WIDTH * 2)); I2C_CLASS::Transmit(DEVICEADDR_OLED, screenBuffer, FRAMEBUFFER_START + (OLED_WIDTH * 2));
// DMA tx time is ~ 20mS Ensure after calling this you delay for at least 25ms // DMA tx time is ~ 20mS Ensure after calling this you delay for at least 25ms
// or we need to goto double buffering // or we need to goto double buffering
} }

View File

@@ -6,9 +6,9 @@
*/ */
#include "TipThermoModel.h" #include "TipThermoModel.h"
#include "configuration.h"
#include "BSP.h" #include "BSP.h"
#include "Settings.h" #include "Settings.h"
#include "configuration.h"
#include "main.hpp" #include "main.hpp"
#include "power.hpp" #include "power.hpp"
/* /*

View File

@@ -164,15 +164,15 @@
#endif #endif
#ifdef MODEL_MHP30 #ifdef MODEL_MHP30
#define VOLTAGE_DIV 650 // Default for MHP30 TODO #define VOLTAGE_DIV 650 // Default for MHP30 TODO
#define PID_POWER_LIMIT 75 // Sets the max pwm power limit #define PID_POWER_LIMIT 75 // Sets the max pwm power limit
#define CALIBRATION_OFFSET 900 // the adc offset in uV #define CALIBRATION_OFFSET 900 // the adc offset in uV
#define POWER_LIMIT 65 // 65 watts default power limit #define POWER_LIMIT 65 // 65 watts default power limit
#define MAX_POWER_LIMIT 35 // #define MAX_POWER_LIMIT 35 //
#define POWER_LIMIT_STEPS 2 // #define POWER_LIMIT_STEPS 2 //
#define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS100 // TODO #define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS100 // TODO
#define TEMP_uV_LOOKUP_MHP30 // TODO #define TEMP_uV_LOOKUP_MHP30 // TODO
#define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate #define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate
#endif #endif
#ifdef MODEL_TS100 #ifdef MODEL_TS100
@@ -195,9 +195,7 @@ const uint32_t tipMass = 40;
const uint8_t tipResistance = 45; // x10 ohms, 4.5 typical for ts80 tips const uint8_t tipResistance = 45; // x10 ohms, 4.5 typical for ts80 tips
#endif #endif
#ifdef MODEL_MHP30 #ifdef MODEL_MHP30
const uint32_t tipMass = 80;//TODO const uint32_t tipMass = 80; // TODO
const uint8_t tipResistance = 65; // x10 ohms, 6 typical const uint8_t tipResistance = 65; // x10 ohms, 6 typical
#endif #endif