Skip to content

Commit

Permalink
removed extra stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tmb5932 committed Nov 10, 2024
1 parent ddbc380 commit 6678459
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
19 changes: 15 additions & 4 deletions include/core/io/platform/f3xx/ADCf3xx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class ADCf3xx : public ADC {
static uint16_t buffer[MAX_CHANNELS];
static DMA_HandleTypeDef halDMA;

/**
* Bit packed struct to contain the channel along with the ADC peripherals the channel supports
*
* adc1: 1 bit. Support for ADC1 peripheral. 1 for supported, 0 for not supported.
* channel: 5 bits. The STM32 ADC channel value with said supported ADC peripherals
*/
struct Channel_Support {
uint8_t adc1 : 1;
uint32_t channel : 5;
};

/**
* Initialize the HAL ADC handler. This should only have to be run once
*/
Expand All @@ -64,13 +75,13 @@ class ADCf3xx : public ADC {
void addChannel(uint8_t rank);

/**
* Checks if the channel that is being initialized supports the ADC peripheral that it is being initialized on.
* Check if the channel that is being initialized supports the ADC peripheral that it is being initialized on.
*
* @param periph the ADC peripheral being used
* @param channel the channel trying to be initialized
* @return true if channel is supported by ADCPeriph, false otherwise
* @param channelStruct the struct of the channel with supports to test
* @return true if channel is supported by the ADC peripheral, false otherwise
*/
static bool checkSupport(ADCPeriph periph, uint32_t channel);
static bool checkSupport(ADCPeriph periph, Channel_Support channelStruct);
};

} // namespace core::io
Expand Down
67 changes: 37 additions & 30 deletions src/core/io/platform/f3xx/ADCf3xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ extern "C" void DMA1_Channel1_IRQHandler(void) {

namespace core::io {

constexpr uint8_t ADC1SHIFT = 5;

// Combines the channel memory value with the ADC peripherals it supports into one uint32_t
constexpr uint32_t CHANNEL_SET(uint8_t adc1, uint32_t ch) {
return (ch | (adc1 << ADC1SHIFT));
}

// Init static member variables
ADC_HandleTypeDef ADCf3xx::halADC = {0};
Pin ADCf3xx::channels[MAX_CHANNELS];
Expand Down Expand Up @@ -138,60 +131,75 @@ void ADCf3xx::addChannel(uint8_t rank) {
GPIO_InitTypeDef gpioInit;
Pin myPins[] = {pin};
uint8_t numOfPins = 1;
uint32_t channel;

GPIOf3xx::gpioStateInit(&gpioInit, myPins, numOfPins, GPIO_MODE_ANALOG, GPIO_NOPULL, GPIO_SPEED_FREQ_HIGH);

ADC_ChannelConfTypeDef adcChannel;
Channel_Support channelStruct = {};

switch (pin) {
case Pin::PA_0:
channel = CHANNEL_SET(1, ADC_CHANNEL_1);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_1;
break;
case Pin::PA_1:
channel = CHANNEL_SET(1, ADC_CHANNEL_2);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_2;
break;
case Pin::PA_2:
channel = CHANNEL_SET(1, ADC_CHANNEL_3);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_3;
break;
case Pin::PA_3:
channel = CHANNEL_SET(1, ADC_CHANNEL_4);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_4;
break;
case Pin::PA_4:
channel = CHANNEL_SET(1, ADC_CHANNEL_5);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_5;
break;
case Pin::PC_0:
channel = CHANNEL_SET(1, ADC_CHANNEL_6);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_6;
break;
case Pin::PC_1:
channel = CHANNEL_SET(1, ADC_CHANNEL_7);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_7;
break;
case Pin::PC_2:
channel = CHANNEL_SET(1, ADC_CHANNEL_8);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_8;
break;
case Pin::PC_3:
channel = CHANNEL_SET(1, ADC_CHANNEL_9);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_9;
break;
case Pin::PA_6:
channel = CHANNEL_SET(1, ADC_CHANNEL_10);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_10;
break;
case Pin::PB_0:
channel = CHANNEL_SET(1, ADC_CHANNEL_11);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_11;
break;
case Pin::PB_1:
channel = CHANNEL_SET(1, ADC_CHANNEL_12);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_12;
break;
case Pin::PB_13:
channel = CHANNEL_SET(1, ADC_CHANNEL_13);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_13;
break;
case Pin::PB_11:
channel = CHANNEL_SET(1, ADC_CHANNEL_14);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_14;
break;
case Pin::PA_7:
channel = CHANNEL_SET(1, ADC_CHANNEL_15);
channelStruct.adc1 = 1;
channelStruct.channel = ADC_CHANNEL_15;
break;
default:
channel = 0;
// Channel Struct is set to all 0 at initialization, so no need to be set all support bits to 0
log::LOGGER.log(log::Logger::LogLevel::ERROR, "INVALID PIN 0x%x!!", pin);
break; // Should never get here
}
Expand All @@ -200,9 +208,8 @@ void ADCf3xx::addChannel(uint8_t rank) {
channels[rank - 1] = pin;

// This checks if the pin being used supports the ADC being used
if (checkSupport(adcPeriph, channel)) {
// Masks channel back to proper value (Zero's out ADC information bits)
adcChannel.Channel = channel & 0x1F;
if (checkSupport(adcPeriph, channelStruct)) {
adcChannel.Channel = channelStruct.channel;
} else {
log::LOGGER.log(log::Logger::LogLevel::ERROR, "DOES NOT SUPPORT PIN 0x%x!!", pin);
}
Expand All @@ -217,11 +224,11 @@ void ADCf3xx::addChannel(uint8_t rank) {
HAL_ADC_ConfigChannel(&halADC, &adcChannel);
}

bool ADCf3xx::checkSupport(ADCPeriph periph, uint32_t channel) {
// Checks if the channel contains the bit signifying the proper ADC peripheral support
bool ADCf3xx::checkSupport(ADCPeriph periph, Channel_Support channelStruct) {
// In c++, non-zero values (like 1) are true, and 0 is false, so no comparison is needed.
switch (periph) {
case ADCPeriph::ONE:
return channel & (1 << ADC1SHIFT);
return channelStruct.adc1;
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/core/io/platform/f4xx/ADCf4xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ constexpr uint8_t ADC1_SLOT = 0;
constexpr uint8_t ADC2_SLOT = 1;
constexpr uint8_t ADC3_SLOT = 2;

constexpr uint8_t ADC1SHIFT = 5;
constexpr uint8_t ADC2SHIFT = 6;
constexpr uint8_t ADC3SHIFT = 7;

// Combines the channel memory value with the ADC peripherals it supports into one uint32_t
constexpr uint32_t CHANNEL_SET(uint8_t adc1, uint8_t adc2, uint8_t adc3, uint32_t ch) {
return (ch | (adc1 << ADC1SHIFT) | (adc2 << ADC2SHIFT) | (adc3 << ADC3SHIFT));
}

bool ADCf4xx::timerInit = false;

ADCf4xx::ADCf4xx(Pin pin, ADCPeriph adcPeriph)
Expand Down Expand Up @@ -345,14 +336,14 @@ void ADCf4xx::addChannel(uint8_t rank) {
}

bool ADCf4xx::checkSupport(ADCPeriph periph, Channel_Support channelStruct) {
// Checks if the channel struct contains the bit signifying the proper ADC peripheral support
// In c++, non-zero values (like 1) are true, and 0 is false, so no comparison is needed.
switch (periph) {
case ADCPeriph::ONE:
return (channelStruct.adc1 == 1);
return channelStruct.adc1;
case ADCPeriph::TWO:
return (channelStruct.adc2 == 1);
return channelStruct.adc2;
case ADCPeriph::THREE:
return (channelStruct.adc3 == 1);
return channelStruct.adc3;
}
}

Expand Down

0 comments on commit 6678459

Please sign in to comment.