Refactored timer for F0

This commit is contained in:
Ea-r-th
2026-03-06 21:12:09 -08:00
parent 3593d8cbd2
commit 43bdee4406
26 changed files with 1763 additions and 606 deletions

View File

@@ -5,7 +5,7 @@
#include "SHAL_ADC.h"
//Can hard code registers on F0 because all F0 devices have only one ADC, and use only one clock
SHAL_Result SHAL_ADC::init() {
SHAL_Result SHAL_ADC::init(ADC_Key key) {
if(m_ADCKey == ADC_Key::INVALID || m_ADCKey == ADC_Key::NUM_ADC){
return SHAL_Result::ERROR;
@@ -81,7 +81,7 @@ uint16_t SHAL_ADC::singleConvertSingle(SHAL_ADC_Channel channel, SHAL_ADC_Sample
return result;
}
void SHAL_ADC::multiConvertSingle(SHAL_ADC_Channel* channels, const int numChannels, uint16_t* result, SHAL_ADC_SampleTime time) {
SHAL_Result SHAL_ADC::multiConvertSingle(SHAL_ADC_Channel* channels, int numChannels, uint16_t* result, SHAL_ADC_SampleTime time) {
ADC_TypeDef* ADC_reg = getADCRegister(m_ADCKey);
ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_TSEN; //Enable VREFINT and Temp sensor in global ADC struct

View File

@@ -3,7 +3,7 @@
//
#include "SHAL_GPIO.h"
#include "SHAL_EXTI_CALLBACK.h"
//#include "SHAL_EXTI_CALLBACK.h"
@@ -13,10 +13,9 @@ SHAL_GPIO::SHAL_GPIO() : m_GPIO_KEY(GPIO_Key::INVALID){
SHAL_GPIO::SHAL_GPIO(GPIO_Key key) : m_GPIO_KEY(key) {
volatile unsigned long* gpioEnable = getGPIORCCEnable(key).reg;
unsigned long gpioOffset = getGPIORCCEnable(key).offset;
auto GPIORCCEnable = getGPIORCCEnable(key);
*gpioEnable |= (1 << gpioOffset); //Set enable flag
SHAL_set_register_value(GPIORCCEnable.reg,GPIORCCEnable.mask);
}
void SHAL_GPIO::setLow() {
@@ -54,12 +53,23 @@ void SHAL_GPIO::setAlternateFunction(GPIO_Alternate_Function AF) volatile {
SHAL_set_bits(alternateFunctionReg.reg,4,static_cast<uint8_t>(AF),alternateFunctionReg.offset);
}
void SHAL_GPIO::setPinMode(PinMode mode) volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->MODER &= ~(0x03 << (2 * gpioPeripheral.global_offset)); //Clear any previous mode
gpioPeripheral.reg->MODER |= (static_cast<uint8_t>(mode) << (2 * gpioPeripheral.global_offset)); //Set mode based on pinmode bit structure
SHAL_Result SHAL_GPIO::setPinMode(PinMode mode) volatile {
auto pinModeReg = getGPIOModeRegister(m_GPIO_KEY);
/*
if(mode == PinMode::ANALOG_MODE && getGPIOPortInfo(m_GPIO_KEY).ADCChannel == SHAL_ADC_Channel::NO_ADC_MAPPING){
char buff[100];
sprintf(buff, "Error: GPIO pin %d has no valid ADC mapping\r\n", static_cast<uint8_t>(m_GPIO_KEY));
SHAL_UART2.sendString(buff);
return SHAL_Result::ERROR;
}
*/
SHAL_set_bits(pinModeReg.reg,2,static_cast<uint8_t>(mode),pinModeReg.offset); //Set mode
return SHAL_Result::OKAY;
}
/* TODO Fix implementation for STM32F072
void SHAL_GPIO::useAsExternalInterrupt(TriggerMode mode, EXTICallback callback) {
uint32_t gpioPin = getGPIORegister(m_GPIO_KEY).global_offset; //Use existing structs to get offset
@@ -98,23 +108,25 @@ void SHAL_GPIO::useAsExternalInterrupt(TriggerMode mode, EXTICallback callback)
__enable_irq(); //Enable IRQ just in case
}
*/
/* TODO reimplement
uint16_t SHAL_GPIO::analogRead(SHAL_ADC_SampleTime sampleTime) {
SHAL_ADC_Channel channel = getGPIOPortInfo(m_GPIO_KEY).ADCChannel;
return GPIOManager::getGPIOADC().singleConvertSingle(channel,sampleTime);
}
*/
SHAL_GPIO& GPIOManager::get(GPIO_Key key) {
unsigned int gpioPort = getGPIOPortNumber(key);
unsigned long gpioPin = getGPIORegister(key).global_offset; //Use existing structs to get offset
uint8_t gpioPin = getGPIOPinNumber(key);
if (m_gpios[gpioPort][gpioPin].m_GPIO_KEY == GPIO_Key::INVALID){
m_gpios[gpioPort][gpioPin] = SHAL_GPIO(key);
}
return m_gpios[gpioPort][gpioPin];
}
}

View File

@@ -95,8 +95,6 @@ void SHAL_I2C::masterWriteRead(uint8_t addr,const uint8_t* writeData, size_t wri
//Read phase
if (readLen > 0) {
SHAL_UART2.sendString("Read initiated\r\n");
I2CPeripheral->CR2 &= ~(I2C_CR2_NBYTES | I2C_CR2_SADD | I2C_CR2_RD_WRN);
I2CPeripheral->CR2 |= (addr << 1) |
I2C_CR2_RD_WRN |

View File

@@ -5,45 +5,96 @@
#include "SHAL_TIM.h"
#include <cassert>
Timer::Timer(Timer_Key t) : TIMER_KEY(t){
Timer::Timer(Timer_Key t) : m_key(t){
}
Timer::Timer() : TIMER_KEY(Timer_Key::S_TIM_INVALID){
Timer::Timer() : m_key(Timer_Key::S_TIM_INVALID){
}
void Timer::start() {
getTimerRegister(TIMER_KEY)->CR1 |= TIM_CR1_CEN;
getTimerRegister(TIMER_KEY)->EGR |= TIM_EGR_UG; //load prescaler reg and ARR
auto control_reg = getTimerControlRegister1(m_key);
auto event_generation_reg = getTimerEventGenerationRegister(m_key);
auto status_reg = getTimerStatusRegister(m_key);
auto break_time_dead_reg = getTimerBreakDeadTimeRegister(m_key);
auto rcc_reg = getTimerRCC(m_key);
SHAL_apply_bitmask(control_reg.reg, control_reg.counter_enable_mask); //Enable counter
SHAL_apply_bitmask(control_reg.reg, control_reg.auto_reload_preload_enable_mask); //Preload enable (buffer)
SHAL_apply_bitmask(event_generation_reg.reg, event_generation_reg.update_generation_mask);
SHAL_clear_bitmask(status_reg.reg,status_reg.update_interrupt_flag_mask);
SHAL_apply_bitmask(rcc_reg.reg,rcc_reg.enable_mask);
SHAL_apply_bitmask(break_time_dead_reg.reg,break_time_dead_reg.main_output_enable_mask);
enableInterrupt();
}
void Timer::stop() {
getTimerRegister(TIMER_KEY)->CR1 &= ~TIM_CR1_CEN;
auto rcc_reg = getTimerRCC(m_key);
SHAL_clear_bitmask(rcc_reg.reg,rcc_reg.enable_mask);
}
void Timer::setPrescaler(uint16_t presc) {
getTimerRegister(TIMER_KEY)->PSC = presc;
getTimerRegister(m_key)->PSC = presc;
}
void Timer::setARR(uint16_t arr) {
getTimerRegister(TIMER_KEY)->ARR = arr;
getTimerRegister(m_key)->ARR = arr;
}
void Timer::enableInterrupt() {
getTimerRegister(TIMER_KEY)->DIER |= TIM_DIER_UIE;
NVIC_EnableIRQ(getIRQn(TIMER_KEY));
getTimerRegister(m_key)->DIER |= TIM_DIER_UIE;
NVIC_EnableIRQ(getIRQn(m_key));
}
void Timer::init(uint32_t prescaler, uint32_t autoReload) {
TIM_RCC_Enable rcc = getTimerRCC(TIMER_KEY);
*rcc.busEnableReg |= (1 << rcc.offset);
void Timer::init(uint16_t prescaler, uint16_t autoReload) {
SHAL_TIM_RCC_Register rcc = getTimerRCC(m_key);
SHAL_apply_bitmask(rcc.reg,rcc.enable_mask);
setPrescaler(prescaler);
setARR(autoReload);
}
void Timer::setOutputCompareMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode outputCompareMode) {
auto channelNum = static_cast<uint8_t>(channel);
auto CCMR = getTimerOutputCaptureCompareModeRegister(m_key, channel);
uint32_t OCMR_Offset = channelNum % 2 == 1 ? CCMR.output_compare_1_mode_offset : CCMR.output_compare_2_mode_offset;
SHAL_set_bits(CCMR.reg,3,static_cast<uint8_t>(outputCompareMode),OCMR_Offset);
}
void Timer::enableChannel(SHAL_Timer_Channel channel, SHAL_Timer_Channel_Main_Output_Mode mainOutputMode,
SHAL_Timer_Channel_Complimentary_Output_Mode complimentaryOutputMode) {
SHAL_TIM_Capture_Compare_Enable_Register captureCompareEnableReg = getTimerCaptureCompareEnableRegister(m_key, channel);
uint16_t setValue = 0; //Value to set the register as
auto channelNum = static_cast<uint8_t>(channel);
uint8_t channelStride = 4; //4 bits per field
setValue |= (static_cast<uint8_t>(mainOutputMode) << ((channelNum - 1) * channelStride)); //xxBB shifted by c - 1
setValue |= (static_cast<uint8_t>(complimentaryOutputMode) << (((channelNum - 1) * channelStride) + 2)); //BBxx shifted by c - 1
SHAL_set_bits(captureCompareEnableReg.reg,16,setValue,0);
}
void Timer::setCaptureCompareValue(SHAL_Timer_Channel channel, uint16_t value) {
auto captureCompareReg = getTimerCaptureCompareRegister(m_key,channel);
SHAL_set_bits(captureCompareReg.reg,16,value,0);
}
Timer &TimerManager::get(Timer_Key timer_key) {
@@ -53,11 +104,9 @@ Timer &TimerManager::get(Timer_Key timer_key) {
Timer& selected = timers[static_cast<int>(timer_key)];
//Timer queried is not initialized yet (defaults to invalid)
if(selected.TIMER_KEY == Timer_Key::S_TIM_INVALID){
if(selected.m_key == Timer_Key::S_TIM_INVALID){
timers[static_cast<int>(timer_key)] = Timer(timer_key); //Initialize TIMER_KEY
}
return timers[static_cast<int>(timer_key)];
}
}

View File

@@ -112,7 +112,7 @@ uint16_t SHAL_ADC::singleConvertSingle(SHAL_ADC_Channel channel, SHAL_ADC_Sample
return result;
}
SHAL_Result SHAL_ADC::multiConvertSingle(SHAL_ADC_Channel* channels, const int numChannels, uint16_t* result, SHAL_ADC_SampleTime time) {
SHAL_Result SHAL_ADC::multiConvertSingle(SHAL_ADC_Channel* channels, int numChannels, uint16_t* result, SHAL_ADC_SampleTime time) {
auto data_reg = getADCDataReg(m_ADCKey); //Where our output will be stored
setADCSequenceAmount(numChannels); //Convert the correct amount of channels

View File

@@ -79,7 +79,6 @@ void SHAL_I2C::masterWriteRead(uint8_t addr,const uint8_t* writeData, size_t wri
for (size_t i = 0; i < writeLen; i++) {
if(!SHAL_WAIT_FOR_CONDITION_MS((I2CPeripheral->ISR & I2C_ISR_TXIS) != 0, 100)){
SHAL_UART2.sendString("I2C timed out waiting for TX\r\n");
return;
}
I2CPeripheral->TXDR = writeData[i];

View File

@@ -1,264 +1,19 @@
#include <cstdio>
#include "SHAL.h"
#define NUM_CHANNELS 6
// Physical order on right-side header: A0, A1, A3, A4, A5, A6, A7
SHAL_ADC_Channel channels[NUM_CHANNELS] = {
SHAL_ADC_Channel::CH5,
SHAL_ADC_Channel::CH6,
SHAL_ADC_Channel::CH8,
SHAL_ADC_Channel::CH9,
SHAL_ADC_Channel::CH10,
SHAL_ADC_Channel::CH12,
};
bool isDeviceOn = false;
bool shouldToggleDeviceState = true;
bool shouldCheckSensorThresholds = true;
uint16_t vals[NUM_CHANNELS] = {0,0,0,0,0,0};
uint8_t currentSensor = 0;
bool isAlarmBeeping = false;
uint16_t sensorThresholds[NUM_CHANNELS] = {0,0,0,0,0,0};
int buzzer_beepCount = 0;
bool isBeepingForCalibration = false;
bool prevIsCalibrateButtonHigh = false;
int cyclesPerPrint = 2;
int currentCycle = 0;
bool areSensorRequirementsMetCurrent = false;
bool areSensorRequirementsMetPrevious = false;
void getSensorData(){
vals[currentSensor] = SHAL_ADC1.singleConvertSingle(channels[currentSensor]);
if(currentSensor == (NUM_CHANNELS - 1) && currentCycle == cyclesPerPrint - 1){
char buff[125];
// Print in the same order as the channels[] array (physical order)
sprintf(buff, "A0:%u,A1:%u,A3:%u,A4:%u,A5:%u,A6:%u\r\n",
vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]);
SHAL_UART2.sendString(buff);
}
currentSensor = (currentSensor + 1) % NUM_CHANNELS;
currentCycle = (currentCycle + 1) % cyclesPerPrint;
}
void startBeeping(){
SHAL_TIM6.setPrescaler(4000);
SHAL_TIM6.setARR(200);
SHAL_TIM6.start();
}
void stopBeeping(){
SHAL_TIM1.stop();
SHAL_TIM6.stop();
isAlarmBeeping = false;
isBeepingForCalibration = false;
}
void checkSensorThresholds(){
bool localFlag = true;
for(int i = 0; i < NUM_CHANNELS; i++){
if(vals[i] < sensorThresholds[i]){
areSensorRequirementsMetCurrent = false; //Conditions not met
localFlag = false;
break;
}
}
if(localFlag){
areSensorRequirementsMetCurrent = true;
}
if(areSensorRequirementsMetCurrent){
if(!areSensorRequirementsMetPrevious){
SHAL_TIM1.stop();
SHAL_TIM6.stop();
SHAL_TIM15.stop();
PIN(A9).setLow();
stopBeeping();
}
}
else{
if(areSensorRequirementsMetPrevious){
SHAL_TIM15.start();
PIN(A9).setHigh();
}
}
areSensorRequirementsMetPrevious = areSensorRequirementsMetCurrent;
}
void calibrateThresholds(){
// Read every channel once and set threshold to 80% of reading
for(int i = 0; i < NUM_CHANNELS; i++){
uint16_t sensorVal = (vals[i] * 3) / 5;
if(sensorVal < 50){
sensorVal = 0;
}
else{
sensorVal = sensorVal - 50;
}
sensorThresholds[i] = sensorVal;
}
char buff[125];
// Print in the same order as the channels[] array (physical order)
sprintf(buff, "Thresholds calibrated to: A0:%u,A1:%u,A3:%u,A4:%u,A5:%u,A6:%u\r\n",
sensorThresholds[0], sensorThresholds[1], sensorThresholds[2], sensorThresholds[3], sensorThresholds[4], sensorThresholds[5]);
SHAL_UART2.sendString(buff);
}
void PWMToggle(){
//Flash light
PIN(A9).toggle();
SHAL_TIM15.stop(); //Stop timer for allowed time off sensors
if(isBeepingForCalibration && buzzer_beepCount > 2){
isBeepingForCalibration = false;
buzzer_beepCount = 0;
SHAL_TIM6.stop(); //Reset timer 6
SHAL_TIM1.stop(); //Stop buzzer
SHAL_TIM6.setPrescaler(4000);
SHAL_TIM6.setARR(400);
}
if(!isAlarmBeeping){
SHAL_TIM1.start();
buzzer_beepCount++;
}
else{
SHAL_TIM1.stop();
}
isAlarmBeeping = !isAlarmBeeping;
}
void buttonHoldCallback(){
shouldCheckSensorThresholds = false; //Dont check sensor thresholds yet, ensure that calibration beep happens
SHAL_TIM7.stop(); //Stop this timer
SHAL_TIM2.stop(); //Stop reading from ADC
buzzer_beepCount = 0;
isBeepingForCalibration = true;
SHAL_TIM6.init(4000,50);
SHAL_TIM6.start();
calibrateThresholds();
SHAL_TIM1.start();
SHAL_TIM2.start(); //Restart value checks
shouldToggleDeviceState = false;
shouldCheckSensorThresholds = true;
}
int main() {
SHAL_init();
//SHAL_UART2.init(UART_Pair_Key::Tx2A2_Rx2A3);
//SHAL_UART2.begin(115200);
PIN(A8).setPinMode(PinMode::ALTERNATE_FUNCTION_MODE);
PIN(A0).setPinMode(PinMode::ANALOG_MODE);
PIN(A1).setPinMode(PinMode::ANALOG_MODE);
PIN(A3).setPinMode(PinMode::ANALOG_MODE);
PIN(A4).setPinMode(PinMode::ANALOG_MODE);
PIN(A5).setPinMode(PinMode::ANALOG_MODE);
PIN(A6).setPinMode(PinMode::ANALOG_MODE);
PIN(A7).setPinMode(PinMode::ANALOG_MODE);
PIN(A8).setAlternateFunction(GPIO_Alternate_Function::AF2);
PIN(B6).setPinMode(PinMode::INPUT_MODE);
PIN(A9).setPinMode(PinMode::OUTPUT_MODE);
PIN(B0).setAlternateFunction(GPIO_Alternate_Function_Mapping::B0_TIM1CH2N);
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
PIN(A8).setPinMode(PinMode::OUTPUT_MODE);
PIN(A8).setInternalResistor(InternalResistorType::NO_PULL);
SHAL_TIM2.init(4000,200);
SHAL_TIM2.setCallbackFunc(getSensorData);
SHAL_TIM2.enableInterrupt();
SHAL_TIM2.start();
SHAL_TIM1.init(0,2400); //PWM signal
SHAL_TIM1.setPWMMode(SHAL_Timer_Channel::CH2,SHAL_TIM_Output_Compare_Mode::PWMMode1,SHAL_Timer_Channel_Main_Output_Mode::Polarity_Normal,SHAL_Timer_Channel_Complimentary_Output_Mode::Polarity_Reversed);
SHAL_TIM1.setPWMDutyCycle(900);
SHAL_TIM6.init(4000,500); //PWM switcher
SHAL_TIM6.setCallbackFunc(PWMToggle);
SHAL_TIM6.enableInterrupt();
SHAL_TIM7.init(4000,3000); //Calibrate timer
SHAL_TIM7.setCallbackFunc(buttonHoldCallback);
SHAL_TIM7.enableInterrupt();
SHAL_TIM15.init(4000,5000); //5 seconds
SHAL_TIM15.setCallbackFunc(startBeeping);
SHAL_TIM15.enableInterrupt();
SHAL_UART2.sendString("Hello3\r\n");
while (true) { //TODO set to use button for simulating off sensor, uncomment for real functionality
if(PIN(B6).digitalRead() != 1){
if(prevIsCalibrateButtonHigh){
SHAL_TIM7.start();
}
prevIsCalibrateButtonHigh = false;
}
else{
if(!prevIsCalibrateButtonHigh){
if(shouldToggleDeviceState){
if(!isDeviceOn){ //Turn device on
PIN(A8).setHigh();
isDeviceOn = true;
}
else{ //Turn device off
PIN(A8).setLow();
PIN(A9).setLow();
isDeviceOn = false;
areSensorRequirementsMetCurrent = true;
areSensorRequirementsMetPrevious = true;
SHAL_TIM15.stop();
stopBeeping();
}
}
shouldToggleDeviceState = true;
SHAL_TIM7.stop();
}
prevIsCalibrateButtonHigh = true;
}
if(isDeviceOn && shouldCheckSensorThresholds){
checkSensorThresholds();
}
}
SHAL_TIM1.init(48,100);
SHAL_TIM1.setOutputCompareMode(SHAL_Timer_Channel::CH1,SHAL_TIM_Output_Compare_Mode::PWMMode1);
SHAL_TIM1.enableChannel(SHAL_Timer_Channel::CH1,SHAL_Timer_Channel_Main_Output_Mode::Polarity_Reversed,SHAL_Timer_Channel_Complimentary_Output_Mode::Disabled);
SHAL_TIM1.setCaptureCompareValue(SHAL_Timer_Channel::CH1, 5);
SHAL_TIM1.start();
}