-
Notifications
You must be signed in to change notification settings - Fork 1
DAC Street Boys - DAC Support for STM32 F3xx and F4xx #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d4df0e8
6f2689f
2a38725
41b0208
ae9f8dd
079ddee
e2bee4f
bf9a7ed
adfd0f5
e5119e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #ifndef _EVT_DAC_ | ||
| #define _EVT_DAC_ | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| namespace core::io { | ||
|
|
||
| // Forward declarations: | ||
| // The different pins are hardware specific. Forward declarations to allow | ||
| // at compilation time the decision of which pins should be used. | ||
| enum class Pin; | ||
| enum class DACPeriph; | ||
|
|
||
| class DACBase { | ||
|
|
||
| public: | ||
| /** | ||
| * Setup the given pin for DAC (digital to analog) usage | ||
| * | ||
| * @param[in] pin The pin to setup for DAC | ||
| * @param[in] dacPeriph The DAC peripheral being used | ||
| */ | ||
| DACBase(Pin pin, DACPeriph dacPeriph); | ||
|
|
||
| /** | ||
| * Set the DAC output value | ||
| * @param value The digital value to convert (0-4095 for 12-bit DAC) | ||
| */ | ||
| virtual void setValue(uint32_t value) = 0; | ||
|
|
||
| /** | ||
| * Get the current DAC output value | ||
| * @return The current digital value being output | ||
| */ | ||
| virtual uint32_t getValue() const = 0; | ||
|
|
||
| /** | ||
| * Set the DAC output voltage directly | ||
| * @param voltage The desired output voltage in volts | ||
| */ | ||
| virtual void setVoltage(float voltage) = 0; | ||
|
|
||
| /** | ||
| * Get the current DAC output voltage | ||
| * @return The current output voltage in volts | ||
| */ | ||
| virtual float getVoltage() const = 0; | ||
|
|
||
| protected: | ||
| /// The pin the DAC is attached to | ||
| Pin pin; | ||
| /// The internal DAC being used | ||
| DACPeriph dacPeriph; | ||
| }; | ||
|
|
||
| } // namespace core::io | ||
|
|
||
| // Convenience typedef - use different name to avoid macro conflict | ||
| namespace core::io { | ||
| using DigitalToAnalogConverter = DACBase; | ||
| } | ||
|
|
||
| #endif // _EVT_DAC_ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #ifndef _EVT_DACF3XX_H | ||
| #define _EVT_DACF3XX_H | ||
|
|
||
| #include <HALf3/stm32f3xx.h> | ||
| #include <HALf3/stm32f3xx_hal.h> | ||
|
|
||
| #include <core/io/DAC.hpp> | ||
|
|
||
| namespace core::io { | ||
|
|
||
| enum class DACPeriph { | ||
| ONE, | ||
| TWO | ||
| }; | ||
|
|
||
| class DACf3xx : public DACBase { | ||
| public: | ||
| /** | ||
| * Setup the given pin for DAC usage | ||
| * | ||
| * @param[in] pin The pin to setup for DAC | ||
| * @param[in] dacPeriph The DAC peripheral being used | ||
| */ | ||
| DACf3xx(Pin pin, DACPeriph dacPeriph); | ||
|
|
||
| void setValue(uint32_t value) override; | ||
| uint32_t getValue() const override; | ||
| void setVoltage(float voltage) override; | ||
| float getVoltage() const override; | ||
|
|
||
| /// HAL DAC handle for STM32 operations (public for interrupt access) | ||
| DAC_HandleTypeDef halDac; | ||
|
|
||
| private: | ||
| /// Maximum raw DAC value (12-bit resolution) | ||
| static constexpr uint32_t MAX_RAW = 4095; | ||
| /// Reference voltage for DAC output calculation | ||
| static constexpr float VREF_POS = 3.3; | ||
| /// Current DAC output value | ||
| uint32_t currentValue = 0; | ||
| /// DAC channel for this instance | ||
| uint32_t channel = 0; | ||
|
|
||
| /** | ||
| * Bit packed struct to contain the channel along with the DAC peripherals the channel supports | ||
| * | ||
| * dac1: 1 bit. Support for DAC1 peripheral. 1 for supported, 0 for not supported. | ||
| * dac2: 1 bit. Support for DAC2 peripheral. 1 for supported, 0 for not supported. | ||
| * channel: 5 bits. The STM32 DAC channel value with said supported DAC peripherals | ||
| */ | ||
| struct Channel_Support { | ||
| uint8_t dac1 : 1; | ||
| uint8_t dac2 : 1; | ||
| uint8_t channel : 5; | ||
| }; | ||
|
|
||
| /** | ||
| * Check if the channel that is being initialized supports the DAC peripheral that it is being initialized on. | ||
| * | ||
| * @param periph the DAC peripheral being used | ||
| * @param channelStruct the struct of the channel with supports to test | ||
| * @return true if channel is supported by the DAC peripheral, false otherwise | ||
| */ | ||
| static bool checkSupport(DACPeriph periph, Channel_Support channelStruct); | ||
|
|
||
| /** | ||
| * Initialize the DAC peripheral with proper configuration | ||
| */ | ||
| void initDAC(); | ||
|
|
||
| /** | ||
| * Initialize GPIO pins for DAC functionality | ||
| */ | ||
| void initGPIO(); | ||
|
|
||
| /** | ||
| * Determine the DAC channel based on the pin | ||
| * @return The STM32 DAC channel value | ||
| */ | ||
| uint32_t getChannelFromPin(); | ||
|
|
||
| /** | ||
| * Get channel support information for a given pin | ||
| * @param pin The pin to get support information for | ||
| * @return Channel_Support struct with DAC peripheral support information | ||
| */ | ||
| static Channel_Support getChannelSupport(Pin pin); | ||
| }; | ||
|
|
||
| } // namespace core::io | ||
|
|
||
| #endif // _EVT_DACF3XX_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #ifndef _EVT_DACF4XX_H | ||
| #define _EVT_DACF4XX_H | ||
|
|
||
| #include <HALf4/stm32f4xx.h> | ||
| #include <HALf4/stm32f4xx_hal.h> | ||
|
|
||
| #include <core/io/DAC.hpp> | ||
|
|
||
| namespace core::io { | ||
|
|
||
| enum class DACPeriph { | ||
| ONE, | ||
| TWO | ||
| }; | ||
|
|
||
| class DACf4xx : public DACBase { | ||
| public: | ||
| /** | ||
| * Setup the given pin for DAC usage | ||
| * | ||
| * @param[in] pin The pin to setup for DAC | ||
| * @param[in] dacPeriph The DAC peripheral being used | ||
| */ | ||
| DACf4xx(Pin pin, DACPeriph dacPeriph); | ||
|
|
||
| void setValue(uint32_t value) override; | ||
| uint32_t getValue() const override; | ||
| void setVoltage(float voltage) override; | ||
| float getVoltage() const override; | ||
|
|
||
| /// HAL DAC handle for STM32 operations (public for interrupt access) | ||
| DAC_HandleTypeDef halDac; | ||
|
|
||
| private: | ||
| /// Maximum raw DAC value (12-bit resolution) | ||
| static constexpr uint32_t MAX_RAW = 4095; | ||
| /// Reference voltage for DAC output calculation | ||
| static constexpr float VREF_POS = 3.3; | ||
| /// Current DAC output value | ||
| uint32_t currentValue = 0; | ||
| /// DAC channel for this instance | ||
| uint32_t channel = 0; | ||
|
|
||
| /** | ||
| * Bit packed struct to contain the channel along with the DAC peripherals the channel supports | ||
| * | ||
| * dac1: 1 bit. Support for DAC1 peripheral. 1 for supported, 0 for not supported. | ||
| * dac2: 1 bit. Support for DAC2 peripheral. 1 for supported, 0 for not supported. | ||
| * channel: 5 bits. The STM32 DAC channel value with said supported DAC peripherals | ||
| */ | ||
| struct Channel_Support { | ||
| uint8_t dac1 : 1; | ||
| uint8_t dac2 : 1; | ||
| uint8_t channel : 5; | ||
| }; | ||
|
|
||
| /** | ||
| * Check if the channel that is being initialized supports the DAC peripheral that it is being initialized on. | ||
| * | ||
| * @param periph the DAC peripheral being used | ||
| * @param channelStruct the struct of the channel with supports to test | ||
| * @return true if channel is supported by the DAC peripheral, false otherwise | ||
| */ | ||
| static bool checkSupport(DACPeriph periph, Channel_Support channelStruct); | ||
|
|
||
| /** | ||
| * Initialize the DAC peripheral with proper configuration | ||
| */ | ||
| void initDAC(); | ||
|
|
||
| /** | ||
| * Initialize GPIO pins for DAC functionality | ||
| */ | ||
| void initGPIO(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add descriptions for these methods
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chat these methods are like not worth documenting
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am with Diego on this, add at least a little description. |
||
|
|
||
| /** | ||
| * Determine the DAC channel based on the pin | ||
| * @return The STM32 DAC channel value | ||
| */ | ||
| uint32_t getChannelFromPin(); | ||
|
|
||
| /** | ||
| * Get channel support information for a given pin | ||
| * @param pin The pin to get support information for | ||
| * @return Channel_Support struct with DAC peripheral support information | ||
| */ | ||
| static Channel_Support getChannelSupport(Pin pin); | ||
| }; | ||
|
|
||
| } // namespace core::io | ||
|
|
||
| #endif // _EVT_DACF4XX_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/evt-core_build.cmake) | ||
|
|
||
| make_exe(dac main.cpp) |
Uh oh!
There was an error while loading. Please reload this page.