1
0
forked from me/IronOS

Basic OLED working

* OLED
* Buttons
This commit is contained in:
Ben V. Brown
2021-04-26 22:22:32 +10:00
parent 8b65fa5d10
commit e84717765a
11 changed files with 533 additions and 572 deletions

View File

@@ -9,148 +9,110 @@
#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;
const uint16_t powerPWM = 255;
const uint16_t powerPWM = 255; history<uint16_t, PID_TIM_HZ> rawTempFilter = { { 0 }, 0, 0 };
static const uint8_t holdoffTicks = 14; // delay of 8 ms void resetWatchdog() {
static const uint8_t tempMeasureTicks = 14; HAL_IWDG_Refresh(&hiwdg);
}
uint16_t totalPWM; // htim2.Init.Period, the full PWM cycle
static bool fastPWM;
// 2 second filter (ADC is PID_TIM_HZ Hz)
history<uint16_t, PID_TIM_HZ> rawTempFilter = {{0}, 0, 0};
void resetWatchdog() { 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
// timers.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
// Period has elapsed
if (htim->Instance == TIM1) {
// STM uses this for internal functions as a counter for timeouts
HAL_IncTick();
}
}
uint16_t getHandleTemperature() { uint16_t getHandleTemperature() {
#ifdef TEMP_NTC return 250; //TODO
// TS80P uses 100k NTC resistors instead
// NTCG104EF104FT1X from TDK
// For now not doing interpolation
int32_t result = getADC(0);
for (uint32_t i = 0; i < (sizeof(NTCHandleLookup) / (2 * sizeof(uint16_t))); i++) {
if (result > NTCHandleLookup[(i * 2) + 0]) {
return NTCHandleLookup[(i * 2) + 1] * 10;
}
}
return 45 * 10;
#endif
#ifdef TEMP_TMP36
// 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
// 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
// mV per count So we need to subtract an offset of 0.5V to center on 0C
// (4964.8 counts)
//
int32_t result = getADC(0);
result -= 4965; // remove 0.5V offset
// 10mV per C
// 99.29 counts per Deg C above 0C. Tends to read a tad over across all of my sample units
result *= 100;
result /= 994;
return result;
#endif
} }
uint16_t getTipInstantTemperature() { uint16_t getTipInstantTemperature() {
uint16_t sum = 0; // 12 bit readings * 8 -> 15 bits return 0; //TODO
uint16_t readings[8];
// Looking to reject the highest outlier readings.
// As on some hardware these samples can run into the op-amp recovery time
// Once this time is up the signal stabilises quickly, so no need to reject minimums
readings[0] = hadc1.Instance->JDR1;
readings[1] = hadc1.Instance->JDR2;
readings[2] = hadc1.Instance->JDR3;
readings[3] = hadc1.Instance->JDR4;
readings[4] = hadc2.Instance->JDR1;
readings[5] = hadc2.Instance->JDR2;
readings[6] = hadc2.Instance->JDR3;
readings[7] = hadc2.Instance->JDR4;
for (int i = 0; i < 8; i++) {
sum += readings[i];
}
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 getInputVoltageX10(uint16_t divisor, uint8_t sample) { uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
@@ -158,177 +120,123 @@ uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
// 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;
} }
void setTipPWM(uint8_t pulse) {
PWMSafetyTimer = 10; // This is decremented in the handler for PWM so that the tip pwm is
// disabled if the PID task is not scheduled often enough.
pendingPWM = pulse;
}
static void switchToFastPWM(void) {
fastPWM = true;
totalPWM = powerPWM + tempMeasureTicks * 2 + holdoffTicks;
htim2.Instance->ARR = totalPWM;
// ~3.5 Hz rate
htim2.Instance->CCR1 = powerPWM + holdoffTicks * 2;
// 2 MHz timer clock/2000 = 1 kHz tick rate
htim2.Instance->PSC = 2000;
}
static void switchToSlowPWM(void) {
fastPWM = false;
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
htim2.Instance->ARR = totalPWM;
// ~1.84 Hz rate
htim2.Instance->CCR1 = powerPWM + holdoffTicks;
// 2 MHz timer clock/4000 = 500 Hz tick rate
htim2.Instance->PSC = 4000;
}
bool tryBetterPWM(uint8_t pwm) { bool tryBetterPWM(uint8_t pwm) {
if (fastPWM && pwm == powerPWM) { //We dont need this for the MHP30
// maximum power for fast PWM reached, need to go slower to get more return false;
switchToSlowPWM(); }
return true; void setTipPWM(uint8_t pulse) {
} else if (!fastPWM && pwm < 230) { //We can just set the timer directly
// 254 in fast PWM mode gives the same power as 239 in slow htim3.Instance->CCR1 = pulse;
// allow for some reasonable hysteresis by switching only when it goes
// below 230 (equivalent to 245 in fast mode)
switchToFastPWM();
return true;
}
return false;
} }
// These are called by the HAL after the corresponding events from the system
// timers.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
// Period has elapsed
if (htim->Instance == TIM2) {
// we want to turn on the output again
PWMSafetyTimer--;
// We decrement this safety value so that lockups in the
// scheduler will not cause the PWM to become locked in an
// active driving state.
// While we could assume this could never happen, its a small price for
// increased safety
htim2.Instance->CCR4 = pendingPWM;
if (htim2.Instance->CCR4 && PWMSafetyTimer) {
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
} else {
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
}
} else if (htim->Instance == TIM1) {
// STM uses this for internal functions as a counter for timeouts
HAL_IncTick();
}
}
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
// This was a when the PWM for the output has timed out
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) {
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
}
}
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() { return HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ? 1 : 0; } uint8_t getButtonA() {
uint8_t getButtonB() { return HAL_GPIO_ReadPin(KEY_B_GPIO_Port, KEY_B_Pin) == GPIO_PIN_RESET ? 1 : 0; } return HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ?
1 : 0;
}
uint8_t getButtonB() {
return HAL_GPIO_ReadPin(KEY_B_GPIO_Port, KEY_B_Pin) == GPIO_PIN_RESET ?
1 : 0;
}
void BSPInit(void) { switchToFastPWM(); } void BSPInit(void) {
}
void reboot() { NVIC_SystemReset(); } void reboot() {
NVIC_SystemReset();
}
void delay_ms(uint16_t count) { HAL_Delay(count); } void delay_ms(uint16_t count) {
HAL_Delay(count);
}

View File

@@ -23,8 +23,8 @@
#define TEMP_NTC #define TEMP_NTC
#define I2C_SOFT #define I2C_SOFT
#define LIS_ORI_FLIP #define LIS_ORI_FLIP
#define OLED_FLIP
#define BATTFILTERDEPTH 8 #define BATTFILTERDEPTH 8
#define OLED_I2CBB
#endif #endif
#endif /* BSP_MINIWARE_MODEL_CONFIG_H_ */ #endif /* BSP_MINIWARE_MODEL_CONFIG_H_ */

View File

@@ -300,14 +300,14 @@ static void MX_TIM2_Init(void) {
TIM_OC_InitTypeDef sConfigOC; TIM_OC_InitTypeDef sConfigOC;
htim2.Instance = TIM2; htim2.Instance = TIM2;
htim2.Init.Prescaler = 2000; // 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_DIV4; // 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);
@@ -324,13 +324,12 @@ static void MX_TIM2_Init(void) {
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; // 13 -> Delay of 7 ms sConfigOC.Pulse = 5;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE; sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
sConfigOC.Pulse = 0; // default to entirely off sConfigOC.Pulse = 0; // default to entirely off
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
} }

View File

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

View File

@@ -5,6 +5,7 @@
#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
* *
@@ -53,7 +54,7 @@ bool fusb_write_byte(uint8_t addr, uint8_t byte) {
* 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, buf, size); return FRToSI2C::Mem_Write(FUSB302B_ADDR, addr, (uint8_t*)buf, size);
} }
uint8_t fusb302_detect() { uint8_t fusb302_detect() {

View File

@@ -13,12 +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.
*/ */
HAL_Init(); SCB->VTOR = FLASH_BASE; //Set vector table offset
Setup_HAL(); // Setup all the HAL objects HAL_Init();
BSPInit(); Setup_HAL(); // Setup all the HAL objects
I2CBB::init(); BSPInit();
/* Init the IPC objects */ I2CBB::init();
FRToSI2C::FRToSInit(); /* Init the IPC objects */
FRToSI2C::FRToSInit();
} }

View File

@@ -44,11 +44,7 @@ void ADC1_2_IRQHandler(void) { 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) { HAL_TIM_IRQHandler(&htim1); } void TIM1_UP_IRQHandler(void) { HAL_TIM_IRQHandler(&htim1); }
// Timer 3 is used for the PWM output to the tip
void TIM3_IRQHandler(void) { HAL_TIM_IRQHandler(&htim3); }
// Timer 2 is used for co-ordination of PWM & ADC
void TIM2_IRQHandler(void) { HAL_TIM_IRQHandler(&htim2); }
void I2C1_EV_IRQHandler(void) { HAL_I2C_EV_IRQHandler(&hi2c1); } void I2C1_EV_IRQHandler(void) { 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); }

View File

@@ -10,281 +10,308 @@
#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, uint16_t Size) { bool I2CBB::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData,
if (!lock()) uint16_t Size) {
return false; if (!lock())
start(); return false;
bool ack = send(DevAddress); start();
if (!ack) { bool ack = send(DevAddress);
stop(); if (!ack) {
unlock(); stop();
return false; unlock();
} return false;
ack = send(MemAddress); }
if (!ack) { ack = send(MemAddress);
stop(); if (!ack) {
unlock(); stop();
return false; unlock();
} return false;
SOFT_SCL_LOW(); }
SOFT_I2C_DELAY(); SOFT_SCL_LOW();
// stop(); SOFT_I2C_DELAY();
start(); // stop();
ack = send(DevAddress | 1); start();
if (!ack) { ack = send(DevAddress | 1);
stop(); if (!ack) {
unlock(); stop();
return false; unlock();
} return false;
while (Size) { }
pData[0] = read(Size > 1); while (Size) {
pData++; pData[0] = read(Size > 1);
Size--; pData++;
} Size--;
stop(); }
unlock(); stop();
return true; unlock();
return true;
} }
bool I2CBB::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, const uint8_t *pData, uint16_t Size) { bool I2CBB::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
if (!lock()) const uint8_t *pData, uint16_t Size) {
return false; if (!lock())
start(); return false;
bool ack = send(DevAddress); start();
if (!ack) { bool ack = send(DevAddress);
stop(); if (!ack) {
asm("bkpt"); stop();
unlock(); asm("bkpt");
return false; unlock();
} return false;
ack = send(MemAddress); }
if (!ack) { ack = send(MemAddress);
stop(); if (!ack) {
asm("bkpt"); stop();
unlock(); asm("bkpt");
return false; unlock();
} return false;
while (Size) { }
resetWatchdog(); while (Size) {
ack = send(pData[0]); resetWatchdog();
if (!ack) { ack = send(pData[0]);
stop(); if (!ack) {
asm("bkpt"); stop();
unlock(); asm("bkpt");
return false; unlock();
} return false;
pData++; }
Size--; pData++;
} Size--;
stop(); }
unlock(); stop();
return true; unlock();
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, uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx) { void I2CBB::TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx,
if (Size_tx == 0 && Size_rx == 0) uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx) {
return; if (Size_tx == 0 && Size_rx == 0)
if (lock() == false) return;
return; if (lock() == false)
if (Size_tx) { return;
start(); if (Size_tx) {
bool ack = send(DevAddress); start();
if (!ack) { bool ack = send(DevAddress);
stop(); if (!ack) {
unlock(); stop();
return; unlock();
} return;
while (Size_tx) { }
ack = send(pData_tx[0]); while (Size_tx) {
if (!ack) { ack = send(pData_tx[0]);
stop(); if (!ack) {
unlock(); stop();
return; unlock();
} return;
pData_tx++; }
Size_tx--; pData_tx++;
} Size_tx--;
} }
if (Size_rx) { }
start(); if (Size_rx) {
bool ack = send(DevAddress | 1); start();
if (!ack) { bool ack = send(DevAddress | 1);
stop(); if (!ack) {
unlock(); stop();
return; unlock();
} return;
while (Size_rx) { }
pData_rx[0] = read(Size_rx > 1); while (Size_rx) {
pData_rx++; pData_rx[0] = read(Size_rx > 1);
Size_rx--; pData_rx++;
} Size_rx--;
} }
stop(); }
unlock(); stop();
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() { xSemaphoreGive(I2CSemaphore); } void I2CBB::unlock() {
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) {
return Mem_Write(address, reg, &data, 1);
}
uint8_t I2CBB::I2C_RegisterRead(uint8_t address, uint8_t reg) {
uint8_t temp = 0;
Mem_Read(address, reg, &temp, 1);
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,
const uint8_t registersLength) {
for (int index = 0; index < registersLength; index++) {
if (!I2C_RegisterWrite(address, registers[index].reg,
registers[index].val)) {
return false;
}
if (registers[index].pause_ms)
delay_ms(registers[index].pause_ms);
}
return true;
}
#endif #endif

View File

@@ -18,28 +18,39 @@
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, uint8_t *pData, uint16_t Size); static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
// Implements a register write uint8_t *pData, uint16_t Size);
static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress, const uint8_t *pData, uint16_t Size); // Implements a register write
static void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size); static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
static void Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size); const 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 Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
static void Receive(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 bool I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data);
static uint8_t I2C_RegisterRead(uint8_t address, uint8_t reg);
typedef struct {
const uint8_t reg; // The register to write to
uint8_t val; // The value to write to this register
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

@@ -29,7 +29,7 @@ uint8_t OLED::secondFrameBuffer[OLED_WIDTH * 2];
/*http://www.displayfuture.com/Display/datasheet/controller/SSD1307.pdf*/ /*http://www.displayfuture.com/Display/datasheet/controller/SSD1307.pdf*/
/*All commands are prefixed with 0x80*/ /*All commands are prefixed with 0x80*/
/*Data packets are prefixed with 0x40*/ /*Data packets are prefixed with 0x40*/
FRToSI2C::I2C_REG OLED_Setup_Array[] = { I2C_CLASS::I2C_REG OLED_Setup_Array[] = {
/**/ /**/
{0x80, 0xAE, 0}, /*Display off*/ {0x80, 0xAE, 0}, /*Display off*/
{0x80, 0xD5, 0}, /*Set display clock divide ratio / osc freq*/ {0x80, 0xD5, 0}, /*Set display clock divide ratio / osc freq*/
@@ -89,7 +89,7 @@ void OLED::initialize() {
// initialisation data to the OLED. // initialisation data to the OLED.
for (int tries = 0; tries < 10; tries++) { for (int tries = 0; tries < 10; tries++) {
if (FRToSI2C::writeRegistersBulk(DEVICEADDR_OLED, OLED_Setup_Array, sizeof(OLED_Setup_Array) / sizeof(OLED_Setup_Array[0]))) { if (I2C_CLASS::writeRegistersBulk(DEVICEADDR_OLED, OLED_Setup_Array, sizeof(OLED_Setup_Array) / sizeof(OLED_Setup_Array[0]))) {
return; return;
} }
} }
@@ -238,7 +238,7 @@ void OLED::setRotation(bool leftHanded) {
OLED_Setup_Array[5].val = 0xC0; OLED_Setup_Array[5].val = 0xC0;
OLED_Setup_Array[9].val = 0xA0; OLED_Setup_Array[9].val = 0xA0;
} }
FRToSI2C::writeRegistersBulk(DEVICEADDR_OLED, OLED_Setup_Array, sizeof(OLED_Setup_Array) / sizeof(OLED_Setup_Array[0])); I2C_CLASS::writeRegistersBulk(DEVICEADDR_OLED, OLED_Setup_Array, sizeof(OLED_Setup_Array) / sizeof(OLED_Setup_Array[0]));
inLeftHandedMode = leftHanded; inLeftHandedMode = leftHanded;

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 "I2C_Wrapper.hpp"
#include <BSP.h> #include <BSP.h>
#include "Model_Config.h"
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#ifdef __cplusplus #ifdef __cplusplus
@@ -21,6 +21,16 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#ifdef OLED_I2CBB
#include "I2CBB.hpp"
#define I2C_CLASS I2CBB
#else
#define I2C_CLASS FRToSI2C
#include "I2C_Wrapper.hpp"
#endif
#define DEVICEADDR_OLED (0x3c << 1) #define DEVICEADDR_OLED (0x3c << 1)
#define OLED_WIDTH 96 #define OLED_WIDTH 96
#define OLED_HEIGHT 16 #define OLED_HEIGHT 16
@@ -40,7 +50,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() {
FRToSI2C::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
} }