Test program done

This commit is contained in:
Luca Lizaranzu
2026-03-20 11:41:44 -07:00
parent 303a554595
commit 1b29371fff
20 changed files with 1037 additions and 114 deletions

View File

@@ -0,0 +1,213 @@
//
// Created by Luca on 10/8/2025.
//
#ifndef SHMINGO_HAL_SHAL_ADC_REG_H753ZI_H
#define SHMINGO_HAL_SHAL_ADC_REG_H753ZI_H
#include "SHAL_CORE.h"
#include "SHAL_ADC_TYPES.h"
#include "stm32h753xx.h"
#define SHAL_ADC1 SHAL_ADC(1)
#define SHAL_ADC2 SHAL_ADC(2)
#define SHAL_ADC3 SHAL_ADC(3)
#define NUM_ADCS 3
#define NUM_ADC_CHANNELS 16
enum class SHAL_ADC_Channel : uint32_t {
CH0 = 0,
CH1,
CH2,
CH3,
CH4,
CH5,
CH6,
CH7,
CH8,
CH9,
CH10,
CH11,
CH12,
CH13,
CH14,
CH15,
CH16,
CH17,
CH18,
CHTemp,
CHRef,
CHBat,
NO_ADC_MAPPING
};
enum class ADC_Key : uint8_t{
S_ADC1 = 0,
S_ADC2 = 1,
S_ADC3 = 2,
NUM_ADC = 3,
INVALID = 255
};
enum class SHAL_ADC_Resolution : uint8_t { //TODO figure out what to do with this difference
B16 = 0x00,
B14 = 0x01,
B12 = 0x02,
B10 = 0x03,
B8 = 0x04,
B6 = 0x05,
};
enum class SHAL_ADC_Clock_Mode {
ASYNC = 0x00,
SYNC_BY_1 = 0x01,
SYNC_BY_2 = 0x02,
SYNC_BY_4 = 0x03,
};
static volatile ADC_TypeDef* ADC_TABLE[3] = { //Lookup table for ADCs
ADC1,
ADC2,
ADC3,
};
static volatile ADC_Common_TypeDef* ADC_CCR_TABLE[3] = { //Common registers
ADC12_COMMON, //1 and 2 share a common register
ADC12_COMMON,
ADC3_COMMON,
};
enum class ADC_Clock_Source : uint32_t {
SHAL_NO_CLOCK = 0x00,
SHAL_PLLSAI1 = 0x01,
SHAL_PLLSYS = 0x02,
SHAL_SYSCLK = 0x03,
};
static SHAL_ADC_Common_Control_Reg getADCCommonControl(ADC_Key key) {
SHAL_ADC_Common_Control_Reg res = {
.reg = nullptr,
.VoltageRefEnable = ADC_CCR_VREFEN,
.TempSensorEnable = ADC_CCR_TSEN,
.VBatteryEnable = ADC_CCR_VBATEN,
.clock_mode_position = ADC_CCR_CKMODE_Pos
};
res.reg = &ADC_CCR_TABLE[static_cast<uint8_t>(key)]->CCR;
return res;
}
static inline SHAL_ADC_RCC_Enable_Reg getADCRCCEnableRegister(const ADC_Key key){
switch (key) {
case ADC_Key::S_ADC1:
case ADC_Key::S_ADC2:
return {&RCC->AHB1ENR, RCC_AHB1ENR_ADC12EN};
case ADC_Key::S_ADC3:
return {&RCC->AHB4ENR, RCC_AHB4ENR_ADC3EN};
default:
__builtin_unreachable();
}
}
static inline SHAL_ADC_Control_Reg getADCControlReg(ADC_Key key) {
SHAL_ADC_Control_Reg res = {nullptr, ADC_CR_ADEN,
ADC_CR_ADSTP,
ADC_CR_ADDIS,
ADC_CR_ADCAL,
ADC_CR_ADSTART,
ADC_CR_DEEPPWD,
ADC_CR_ADVREGEN,
ADC_CR_ADCALDIF};
res.reg = &(ADC_TABLE[static_cast<uint8_t>(key)]->CR);
return res;
}
static inline SHAL_ADC_Config_Reg getADCConfigReg(ADC_Key key) {
//NOTE offset of 33 means the function is deprecated
SHAL_ADC_Config_Reg res = {nullptr,
ADC_CFGR_CONT,
ADC_CFGR_RES_Pos,
0, //TODO 0 is broken, shouldnt have alignment
ADC_CFGR_CONT_Msk
};
res.reg = &(ADC_TABLE[static_cast<uint8_t>(key)]->CFGR);
return res;
}
static inline SHAL_ADC_ISR_Reg getADCISRReg(ADC_Key key){
SHAL_ADC_ISR_Reg res = {nullptr,
ADC_ISR_EOC,
ADC_ISR_EOS,
ADC_ISR_ADRDY,
ADC_ISR_OVR,
ADC_ISR_LDORDY};
res.reg = &(ADC_TABLE[static_cast<uint8_t>(key)]->ISR);
return res;
}
static inline SHAL_ADC_Data_Reg getADCDataReg(ADC_Key key){
SHAL_ADC_Data_Reg res = {nullptr, 0xFFFF};
res.reg = &(ADC_TABLE[static_cast<uint8_t>(key)]->DR);
return res;
}
static inline SHAL_ADC_Channel_Sampling_Time_Reg getADCChannelSamplingTimeRegister(ADC_Key key, SHAL_ADC_Channel channel){
volatile ADC_TypeDef* ADCReg = ADC_TABLE[static_cast<uint8_t>(key)];
volatile uint32_t* SMPReg = nullptr;
uint32_t pos;
auto channelNum = static_cast<uint8_t>(channel);
if (channelNum <= 9) {
SMPReg = &ADCReg->SMPR1;
pos = (channelNum * 3);
} else {
SMPReg = &ADCReg->SMPR2;
pos = ((channelNum - 10) * 3);
}
return {SMPReg, pos};
}
static inline SHAL_ADC_Sequence_Amount_Reg getADCSequenceAmountRegister(ADC_Key key){
SHAL_ADC_Sequence_Amount_Reg res = {nullptr, ADC_SQR1_L_Pos};
res.reg = &(ADC_TABLE[static_cast<uint8_t>(key)]->SQR1);
return res;
}
static SHAL_ADC_Sequence_Reg getADCSequenceRegister(ADC_Key key, uint32_t conversionNum){
volatile ADC_TypeDef* adc_reg = ADC_TABLE[static_cast<uint8_t>(key)];
volatile uint32_t* sqr[4] = {&adc_reg->SQR1,
&adc_reg->SQR2,
&adc_reg->SQR3,
&adc_reg->SQR4,
};
const uint8_t sqrIndex = conversionNum / 5; //CONVERSION NUM STARTS AT 1! AS PER DATASHEET REFERENCING IT AS SQ1
const uint32_t offset = (((conversionNum) % 5) * 6);
return {sqr[sqrIndex], offset};
}
static SHAL_ADC_Preselect_Reg getADCPreselectRegister(ADC_Key key, SHAL_ADC_Channel channel) {
SHAL_ADC_Preselect_Reg res = {nullptr, static_cast<uint32_t>(channel)};
res.reg = &(ADC_TABLE[static_cast<uint8_t>(key)]->PCSEL);
return res;
}
#endif //SHMINGO_HAL_SHAL_ADC_REG_H753ZI_H

View File

@@ -16,19 +16,21 @@ class SHAL_ADC {
public:
SHAL_Result init(ADC_Key key);
SHAL_Result init(ADC_Key key, SHAL_ADC_Sample_Mode mode);
SHAL_Result calibrate();
SHAL_Result calibrate(SHAL_ADC_Sample_Mode sampleMode) const;
SHAL_Result configureResolution(SHAL_ADC_Resolution resolution);
SHAL_Result configureResolution(SHAL_ADC_Resolution resolution) const;
SHAL_Result configureAlignment(SHAL_ADC_Alignment alignment);
SHAL_Result configureAlignment(SHAL_ADC_Alignment alignment) const;
SHAL_Result preselectChannel(SHAL_ADC_Channel channel) const;
/// Performs analog to digital conversion on a single channel, one time
/// \param channel Channel to be converted
/// \param time SHAL_ADC_SampleTime - amount of clock cycles per conversion
/// \return resulting value
uint16_t singleConvertSingle(SHAL_ADC_Channel channel, SHAL_ADC_SampleTime time = SHAL_ADC_SampleTime::C8);
uint32_t singleConvertSingle(SHAL_ADC_Channel channel, SHAL_ADC_SampleTime time = SHAL_ADC_SampleTime::C8) const;
/// Performs analog to digital conversion on multiple channels, one time
/// \param channels Pointer to an array of channels to convert
@@ -46,29 +48,29 @@ private:
ADC_Key m_ADCKey = ADC_Key::INVALID;
//Checks to see if instance is initialized with a proper ADC peripheral tag
bool isValid();
bool isValid() const;
//Enabled peripheral
SHAL_Result enable();
SHAL_Result enable() const;
//Disables peripheral
SHAL_Result disable();
SHAL_Result disable() const;
//Wake up ADC from initial deep sleep state
SHAL_Result wakeFromDeepSleep();
SHAL_Result wakeFromDeepSleep() const;
SHAL_Result startConversion();
SHAL_Result startConversion() const;
/// Adds an ADC channel to the conversion sequence
/// \param channel Channel to add
/// \param index Index to add channel to (ADC channel will be the nth channel to convert
/// \param conversionNumber Index to add channel to (ADC channel will be the nth channel to convert
/// \return Result
SHAL_Result addADCChannelToSequence(SHAL_ADC_Channel channel, uint32_t index);
SHAL_Result addADCChannelToSequence(SHAL_ADC_Channel channel, uint32_t conversionNumber) const;
/// Sets the amount of ADC channels to convert
/// \param amount Number of channels to convert
/// \return
SHAL_Result setADCSequenceAmount(uint32_t amount);
SHAL_Result setADCSequenceAmount(uint32_t amount) const;
};
@@ -83,7 +85,6 @@ public:
static SHAL_ADC& getByIndex(int index);
ADCManager() = delete;
private:

View File

@@ -88,6 +88,50 @@
#elif defined(STM32L4S7xx)
#include "stm32l4s7xx.h"
#elif defined(STM32L4S9xx)
#elif defined(STM32H743xx)
#include "stm32h743xx.h"
#elif defined(STM32H753xx)
#include "SHAL_ADC_REG_H753ZI.h"
#elif defined(STM32H750xx)
#include "stm32h750xx.h"
#elif defined(STM32H742xx)
#include "stm32h742xx.h"
#elif defined(STM32H745xx)
#include "stm32h745xx.h"
#elif defined(STM32H745xG)
#include "stm32h745xg.h"
#elif defined(STM32H755xx)
#include "stm32h755xx.h"
#elif defined(STM32H747xx)
#include "stm32h747xx.h"
#elif defined(STM32H747xG)
#include "stm32h747xg.h"
#elif defined(STM32H757xx)
#include "stm32h757xx.h"
#elif defined(STM32H7B0xx)
#include "stm32h7b0xx.h"
#elif defined(STM32H7B0xxQ)
#include "stm32h7b0xxq.h"
#elif defined(STM32H7A3xx)
#include "stm32h7a3xx.h"
#elif defined(STM32H7B3xx)
#include "stm32h7b3xx.h"
#elif defined(STM32H7A3xxQ)
#include "stm32h7a3xxq.h"
#elif defined(STM32H7B3xxQ)
#include "stm32h7b3xxq.h"
#elif defined(STM32H735xx)
#include "stm32h735xx.h"
#elif defined(STM32H733xx)
#include "stm32h733xx.h"
#elif defined(STM32H730xx)
#include "stm32h730xx.h"
#elif defined(STM32H730xxQ)
#include "stm32h730xxq.h"
#elif defined(STM32H725xx)
#include "stm32h725xx.h"
#elif defined(STM32H723xx)
#include "stm32h723xx.h"
#else
#error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)"
#endif

View File

@@ -5,12 +5,15 @@
#ifndef SHMINGO_HAL_SHAL_ADC_TYPES_H
#define SHMINGO_HAL_SHAL_ADC_TYPES_H
#include "SHAL_CORE.h"
//Common register among all ADC peripherals
struct SHAL_ADC_Common_Control_Reg {
volatile uint32_t* reg;
uint32_t VoltageRefEnable;
uint32_t TempSensorEnable;
uint32_t VBatteryEnable;
uint32_t clock_mode_position;
};
//Register controlling the ADC peripheral clock
@@ -29,7 +32,7 @@ struct SHAL_ADC_Control_Reg {
uint32_t start_mask;
uint32_t deep_power_down_mask;
uint32_t voltage_regulator_mask;
uint32_t differential_mode_mask;
uint32_t sample_mode_offset;
};
//Register controlling ADC configuration
@@ -55,6 +58,7 @@ struct SHAL_ADC_ISR_Reg {
uint32_t end_of_sequence_mask;
uint32_t ready_mask;
uint32_t overrun_mask;
uint32_t ldo_ready_mask;
};
//Register controlling the clock source for the ADC
@@ -79,9 +83,13 @@ struct SHAL_ADC_Sequence_Amount_Reg {
*reg 1 + offset 1
*Any sections after the last one (for example, max for a 16 channel register is reg 4 offset 2*/
struct SHAL_ADC_Sequence_Reg {
volatile uint32_t* regs[6];
volatile uint32_t* reg;
uint32_t sequence_offset;
};
uint32_t offsets[5];
struct SHAL_ADC_Preselect_Reg {
volatile uint32_t* reg;
uint32_t channel_offset;
};
@@ -98,16 +106,14 @@ enum class SHAL_ADC_SampleTime : uint32_t {
C8 = 0x07 //239.5 cycles
};
enum class SHAL_ADC_Resolution : uint8_t {
B12 = 0x00,
B10 = 0x01,
B8 = 0x02,
B6 = 0x03,
};
enum class SHAL_ADC_Alignment : uint8_t {
RIGHT = 0x00,
LEFT = 0x01,
};
enum class SHAL_ADC_Sample_Mode {
SINGLE_ENDED = 0,
DIFFERENTIAL = 1,
};
#endif //SHMINGO_HAL_SHAL_ADC_TYPES_H

View File

@@ -17,31 +17,33 @@ class SHAL_GPIO{
public:
void toggle() volatile;
void toggle() const volatile;
//TODO replace stupid offset hack from APB
void setHigh();
void setLow();
void setHigh() const;
void setLow() const;
/// Uses the ADC to read an analog voltage value
/// \param sampleTime The amount of clock cycles to use for the ADC
/// \return ADC result
//uint16_t analogRead(SHAL_ADC_SampleTime sampleTime = SHAL_ADC_SampleTime::C8); TODO Reimplement
uint16_t digitalRead();
uint16_t digitalRead() const;
void setAlternateFunction(GPIO_Alternate_Function AF) volatile;
void setAlternateFunction(GPIO_Alternate_Function AF) const volatile;
//void setAlternateFunction(GPIO_Alternate_Function_Mapping AF) volatile; //TODO reimplement?
void setOutputType(PinType type) volatile;
void setOutputType(PinType type) const volatile;
void setOutputSpeed(OutputSpeed speed) volatile;
void setOutputSpeed(OutputSpeed speed) const volatile;
void setInternalResistor(InternalResistorType type) volatile;
void setInternalResistor(InternalResistorType type) const volatile;
//void useAsExternalInterrupt(TriggerMode mode, EXTICallback callback); TODO reimplement
SHAL_Result setPinMode(PinMode mode) volatile;
SHAL_Result setPinMode(PinMode mode) const volatile;
[[nodiscard]] GPIO_Key getKey() const {return m_GPIO_KEY;};
private:
@@ -73,6 +75,9 @@ class GPIOManager{
public:
static SHAL_GPIO& get(GPIO_Key);
static SHAL_GPIO& get(uint8_t portNum, uint8_t pinNum);
static void initGPIO(GPIO_Key key);
//static SHAL_ADC getGPIOADC(){ return m_GPIO_ADC;} TODO Reimplement

View File

@@ -19,8 +19,13 @@ enum class Timer_Key : uint8_t { //For STM32F072
S_TIM1 = 0,
S_TIM2 = 1,
S_TIM3 = 2,
S_TIM4,
S_TIM5,
S_TIM6,
S_TIM7,
S_TIM8,
S_TIM12,
S_TIM13,
S_TIM14,
S_TIM15,
S_TIM16,
@@ -32,19 +37,29 @@ enum class Timer_Key : uint8_t { //For STM32F072
#define SHAL_TIM1 TimerManager::get(Timer_Key::S_TIM1)
#define SHAL_TIM2 TimerManager::get(Timer_Key::S_TIM2)
#define SHAL_TIM3 TimerManager::get(Timer_Key::S_TIM3)
#define SHAL_TIM4 TimerManager::get(Timer_Key::S_TIM4)
#define SHAL_TIM5 TimerManager::get(Timer_Key::S_TIM5)
#define SHAL_TIM6 TimerManager::get(Timer_Key::S_TIM6)
#define SHAL_TIM7 TimerManager::get(Timer_Key::S_TIM7)
#define SHAL_TIM8 TimerManager::get(Timer_Key::S_TIM8)
#define SHAL_TIM12 TimerManager::get(Timer_Key::S_TIM12)
#define SHAL_TIM13 TimerManager::get(Timer_Key::S_TIM13)
#define SHAL_TIM14 TimerManager::get(Timer_Key::S_TIM14)
#define SHAL_TIM15 TimerManager::get(Timer_Key::S_TIM15)
#define SHAL_TIM16 TimerManager::get(Timer_Key::S_TIM16)
#define SHAL_TIM17 TimerManager::get(Timer_Key::S_TIM17)
static SHAL_TIM_Info TIM_INFO_TABLE[9] = {
static SHAL_TIM_Info TIM_INFO_TABLE[14] = {
{TIM1,TIM1_TRG_COM_IRQn,4},
{TIM2,TIM2_IRQn,4},
{TIM3,TIM3_IRQn,4},
{TIM3,TIM4_IRQn,4},
{TIM3,TIM5_IRQn,4},
{TIM6,TIM6_DAC_IRQn,0},
{TIM7,TIM7_IRQn,0},
{TIM8,TIM8_TRG_COM_TIM14_IRQn,6},
{TIM12,TIM8_BRK_TIM12_IRQn,2},
{TIM13,TIM8_UP_TIM13_IRQn,1},
{TIM14,TIM8_TRG_COM_TIM14_IRQn,1},
{TIM15,TIM15_IRQn,2},
{TIM16,TIM16_IRQn,1},
@@ -67,9 +82,14 @@ static SHAL_TIM_RCC_Register getTimerRCC(Timer_Key t) {
case Timer_Key::S_TIM1: return {&RCC->APB2ENR, RCC_APB2ENR_TIM1EN};
case Timer_Key::S_TIM2: return {&RCC->APB1LENR, RCC_APB1LENR_TIM2EN};
case Timer_Key::S_TIM3: return {&RCC->APB1LENR, RCC_APB1LENR_TIM3EN};
case Timer_Key::S_TIM4: return {&RCC->APB1LENR, RCC_APB1LENR_TIM4EN};
case Timer_Key::S_TIM5: return {&RCC->APB1LENR, RCC_APB1LENR_TIM5EN};
case Timer_Key::S_TIM6: return {&RCC->APB1LENR, RCC_APB1LENR_TIM6EN};
case Timer_Key::S_TIM7: return {&RCC->APB1LENR, RCC_APB1LENR_TIM7EN};
case Timer_Key::S_TIM14: return {&RCC->APB1LENR, RCC_APB1LENR_TIM14EN};
case Timer_Key::S_TIM8: return {&RCC->APB1LENR, RCC_APB2ENR_TIM8EN};
case Timer_Key::S_TIM14: return {&RCC->APB1LENR, RCC_APB1LENR_TIM12EN};
case Timer_Key::S_TIM12: return {&RCC->APB1LENR, RCC_APB1LENR_TIM13EN};
case Timer_Key::S_TIM13: return {&RCC->APB1LENR, RCC_APB1LENR_TIM14EN};
case Timer_Key::S_TIM15: return {&RCC->APB2ENR, RCC_APB2ENR_TIM15EN};
case Timer_Key::S_TIM16: return {&RCC->APB2ENR, RCC_APB2ENR_TIM16EN};
case Timer_Key::S_TIM17: return {&RCC->APB2ENR, RCC_APB2ENR_TIM17EN};

View File

@@ -21,18 +21,19 @@ public:
/// Initializes a timer
/// \param prescaler The amount of times the base clock has to cycle before the timer adds one to the count
/// \param autoReload The number of timer counts before the count is reset and IRQ is called
void init(uint16_t prescaler, uint16_t autoReload);
void init(uint32_t prescaler, uint32_t autoReload);
void init() const; //Empty init (enable RCC)
/// Simple function to set a timer in basic PWM mode
/// @param channel Channel to output on
/// @param prescaler Divider from sysclock
/// @param autoReload Counter value to reset at
/// @param captureCompareThreshold PWM trigger value (duty cycle = this / autoReload)
void configurePWM(SHAL_Timer_Channel channel, uint16_t prescaler, uint16_t autoReload, uint16_t captureCompareThreshold);
void configurePWM(SHAL_Timer_Channel channel, uint32_t prescaler, uint32_t autoReload, uint32_t captureCompareThreshold);
void configureOneshot(SHAL_Timer_Channel channel, uint16_t prescaler, uint16_t autoReload, uint16_t captureCompareThreshold);
void configureOneshot(SHAL_Timer_Channel channel, uint32_t prescaler, uint32_t autoReload, uint32_t captureCompareThreshold);
//Starts the counter
void start();
@@ -41,16 +42,16 @@ public:
void stop() const;
//Set prescaler value
void setPrescaler(uint16_t presc) const;
void setPrescaler(uint32_t presc) const;
//Set auto reload register
void setARR(uint16_t arr) const;
void setARR(uint32_t arr) const;
//Enable interrupts
void enableInterrupt();
//Capture Compare Functions
void setCaptureCompareValue(SHAL_Timer_Channel channel, uint16_t value);
void setCaptureCompareValue(SHAL_Timer_Channel channel, uint32_t value) const;
void enableChannel(SHAL_Timer_Channel channel, SHAL_Timer_Channel_Main_Output_Mode mainOutputMode, SHAL_Timer_Channel_Complimentary_Output_Mode complimentaryOutputMode);
void setOutputCompareMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode outputCompareMode);

View File

@@ -63,7 +63,8 @@ static SHAL_UART_Control_Register_1 getUARTControlRegister1(const UART_Pair_Key
USART_CR1_TE,
USART_CR1_RE,
USART_CR1_M0,
USART_CR1_M1
USART_CR1_M1,
USART_CR1_RXNEIE
};
res.reg = &getUARTPair(key).USARTReg->CR1;
@@ -90,7 +91,7 @@ static SHAL_UART_ISR getUARTISR(const UART_Pair_Key key) { //TODO Support for mu
}
static SHAL_UART_Transmit_Data_Register getUARTTransmitDataRegister(const UART_Pair_Key key) {
return {&getUARTPair(key).USARTReg->CR1};
return {&getUARTPair(key).USARTReg->TDR};
}
static

View File

@@ -23,10 +23,9 @@ public:
void begin(uint32_t baudRate, SHAL_USART_Word_Length wordLength) const volatile;
//Sends a string
void sendString(const char* s) volatile;
void sendString(const char* s) const volatile;
//Sends a char
void sendChar(char c) volatile;
void sendChar(char c) const volatile;
private:

View File

@@ -29,6 +29,7 @@ struct SHAL_UART_Control_Register_1 {
uint32_t receive_enable_mask;
uint32_t m0_mask;
uint32_t m1_mask;
uint32_t Rx_interrupt_enable_mask;
};
struct SHAL_UART_Baud_Rate_Generation_Register {