Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 114 additions & 3 deletions Core/Src/Examples/ExampleADC.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
#ifdef EXAMPLE_ADC

#include <cstdio>
#include <cstdint>

#include "main.h"
#include "ST-LIB.hpp"

using namespace ST_LIB;

namespace {

struct ExampleInput {
GPIODomain::Pin pin;
const char* label;
};

constexpr ExampleInput kSingleChannelInput{ST_LIB::PA0, "PA0"};
constexpr ExampleInput kDualChannelInput0{ST_LIB::PA0, "PA0"};
constexpr ExampleInput kDualChannelInput1{ST_LIB::PC0, "PC0"};

constexpr const char* kTerminalHint = "Terminal: ST-LINK VCP over USB (USART3, 115200 8N1)";
constexpr const char* kSingleChannelWiringHint = "Connect PA0 to GND, 3V3 or a potentiometer.";
constexpr const char* kDualChannelWiringHint = "Connect PA0 and PC0 to two analog sources.";

void start_terminal() {
#ifdef HAL_UART_MODULE_ENABLED
if (!UART::set_up_printf(UART::uart3)) {
ErrorHandler("Unable to set up UART printf for ADC example");
}
UART::start();
#endif
}

void print_banner(const char* title, const char* wiring_hint, const char* columns_hint) {
printf("\n\r=== %s ===\n\r", title);
printf("%s\n\r", kTerminalHint);
printf("%s\n\r", wiring_hint);
printf("Columns: %s\n\r\n\r", columns_hint);
}

} // namespace

#ifdef TEST_0

constinit float adc_value = 0.0f;
constexpr auto adc_input = kSingleChannelInput;
constexpr auto adc = ADCDomain::ADC(
ST_LIB::PA0,
adc_input.pin,
adc_value,
ADCDomain::Resolution::BITS_12,
ADCDomain::SampleTime::CYCLES_8_5
Expand All @@ -18,16 +55,90 @@ constexpr auto adc = ADCDomain::ADC(
int main(void) {
using ExampleADCBoard = ST_LIB::Board<adc>;
ExampleADCBoard::init();
start_terminal();

auto& adc_instance = ExampleADCBoard::instance_of<adc>();

// Ready to compile for Nucleo. Validate by wiring PA0 to 3.3V and then to GND,
// and watch adc_value in the debugger to confirm the change.
print_banner("ADC single-channel example", kSingleChannelWiringHint, "t_ms raw voltage[V]");
printf("Reading input: %s\n\r\n\r", adc_input.label);

uint32_t sample_index = 0;
while (1) {
adc_instance.read();
const float raw = adc_instance.get_raw();
const float voltage = adc_instance.get_value();

printf("%10lu %8.0f %10.4f\n\r", HAL_GetTick(), raw, voltage);

++sample_index;
if ((sample_index % 20U) == 0U) {
printf("Current mirrored output buffer value: %.4f V\n\r\n\r", adc_value);
}

HAL_Delay(100);
}
}

#endif // TEST_0

#ifdef TEST_1

constinit float adc_input_0_value = 0.0f;
constinit float adc_input_1_value = 0.0f;

constexpr auto adc_input_0_cfg = kDualChannelInput0;
constexpr auto adc_input_1_cfg = kDualChannelInput1;

constexpr auto adc_input_0 = ADCDomain::ADC(
adc_input_0_cfg.pin,
adc_input_0_value,
ADCDomain::Resolution::BITS_12,
ADCDomain::SampleTime::CYCLES_8_5
);

constexpr auto adc_input_1 = ADCDomain::ADC(
adc_input_1_cfg.pin,
adc_input_1_value,
ADCDomain::Resolution::BITS_12,
ADCDomain::SampleTime::CYCLES_8_5
);

int main(void) {
using ExampleADCBoard = ST_LIB::Board<adc_input_0, adc_input_1>;
ExampleADCBoard::init();
start_terminal();

auto& adc_input_0_instance = ExampleADCBoard::instance_of<adc_input_0>();
auto& adc_input_1_instance = ExampleADCBoard::instance_of<adc_input_1>();

print_banner(
"ADC dual-channel example",
kDualChannelWiringHint,
"t_ms raw_0 raw_1 v_0[V] v_1[V]"
);
printf("Reading inputs: %s and %s\n\r\n\r", adc_input_0_cfg.label, adc_input_1_cfg.label);

while (1) {
adc_input_0_instance.read();
adc_input_1_instance.read();

const float raw_0 = adc_input_0_instance.get_raw();
const float raw_1 = adc_input_1_instance.get_raw();
const float voltage_0 = adc_input_0_instance.get_value();
const float voltage_1 = adc_input_1_instance.get_value();

printf(
"%10lu %8.0f %8.0f %10.4f %10.4f\n\r",
HAL_GetTick(),
raw_0,
raw_1,
voltage_0,
voltage_1
);

HAL_Delay(100);
}
}

#endif // TEST_1
#endif // EXAMPLE_ADC
88 changes: 11 additions & 77 deletions Core/Src/Runes/Runes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,96 +74,30 @@ UART::Instance UART::instance2 = {
.word_length = UART_WORDLENGTH_8B,
};

UART::Instance UART::instance3 = {
.TX = PD8,
.RX = PD9,
.huart = &huart3,
.instance = USART3,
.baud_rate = 115200,
.word_length = UART_WORDLENGTH_8B,
};

UART::Peripheral UART::uart1 = UART::Peripheral::peripheral1;
UART::Peripheral UART::uart2 = UART::Peripheral::peripheral2;
UART::Peripheral UART::uart3 = UART::Peripheral::peripheral3;

unordered_map<UART::Peripheral, UART::Instance*> UART::available_uarts = {
{UART::uart1, &UART::instance1},
{UART::uart2, &UART::instance2},
{UART::uart3, &UART::instance3},
};

uint8_t UART::printf_uart = 0;
bool UART::printf_ready = false;

#endif

/************************************************
* ADC
***********************************************/
#if 0 // Legacy ADC (replaced by NewADC). Kept here only as reference.
#if defined(HAL_ADC_MODULE_ENABLED) && defined(HAL_LPTIM_MODULE_ENABLED)

LowPowerTimer lptim1(*LPTIM1, hlptim1, LPTIM1_PERIOD, "LPTIM 1");
LowPowerTimer lptim2(*LPTIM2, hlptim2, LPTIM2_PERIOD, "LPTIM 2");
LowPowerTimer lptim3(*LPTIM3, hlptim3, LPTIM3_PERIOD, "LPTIM 3");

vector<uint32_t> channels1 = {};
vector<uint32_t> channels2 = {};
vector<uint32_t> channels3 = {};

ST_LIB::DMA_Domain::Instance dma_adc1 = {hdma_adc1};
ST_LIB::DMA_Domain::Instance dma_adc2 = {hdma_adc2};
ST_LIB::DMA_Domain::Instance dma_adc3 = {hdma_adc3};

ADC::InitData init_data1(ADC1, ADC_RESOLUTION_16B, ADC_EXTERNALTRIG_LPTIM1_OUT,
channels1, &dma_adc1, "ADC 1");
ADC::InitData init_data2(ADC2, ADC_RESOLUTION_16B, ADC_EXTERNALTRIG_LPTIM2_OUT,
channels2, &dma_adc2, "ADC 2");
ADC::InitData init_data3(ADC3, ADC_RESOLUTION_12B, ADC_EXTERNALTRIG_LPTIM3_OUT,
channels3, &dma_adc3, "ADC 3");

ADC::Peripheral ADC::peripherals[3] = {
ADC::Peripheral(&hadc1, lptim1, init_data1),
ADC::Peripheral(&hadc2, lptim2, init_data2),
ADC::Peripheral(&hadc3, lptim3, init_data3)
};

map<Pin, ADC::Instance> ADC::available_instances = {
{PF11, Instance(&peripherals[0], ADC_CHANNEL_2)},
{PF12, Instance(&peripherals[0], ADC_CHANNEL_6)},
{PF13, Instance(&peripherals[1], ADC_CHANNEL_2)},
{PF14, Instance(&peripherals[1], ADC_CHANNEL_6)},
{PF5, Instance(&peripherals[2], ADC_CHANNEL_4)},
{PF6, Instance(&peripherals[2], ADC_CHANNEL_8)},
{PF7, Instance(&peripherals[2], ADC_CHANNEL_3)},
{PF8, Instance(&peripherals[2], ADC_CHANNEL_7)},
{PF9, Instance(&peripherals[2], ADC_CHANNEL_2)},
{PF10, Instance(&peripherals[2], ADC_CHANNEL_6)},
{PC2, Instance(&peripherals[2], ADC_CHANNEL_0)},
{PC3, Instance(&peripherals[2], ADC_CHANNEL_1)},
{PF10, Instance(&peripherals[2], ADC_CHANNEL_6)},
{PC0, Instance(&peripherals[0], ADC_CHANNEL_10)},
{PA0, Instance(&peripherals[0], ADC_CHANNEL_16)},
{PA3, Instance(&peripherals[0], ADC_CHANNEL_15)},
{PA4, Instance(&peripherals[0], ADC_CHANNEL_18)},
{PA5, Instance(&peripherals[0], ADC_CHANNEL_19)},
{PA6, Instance(&peripherals[0], ADC_CHANNEL_3)},
{PB0, Instance(&peripherals[0], ADC_CHANNEL_9)},
{PB1, Instance(&peripherals[0], ADC_CHANNEL_5)}
};

uint32_t ADC::ranks[16] = {
ADC_REGULAR_RANK_1,
ADC_REGULAR_RANK_2,
ADC_REGULAR_RANK_3,
ADC_REGULAR_RANK_4,
ADC_REGULAR_RANK_5,
ADC_REGULAR_RANK_6,
ADC_REGULAR_RANK_7,
ADC_REGULAR_RANK_8,
ADC_REGULAR_RANK_9,
ADC_REGULAR_RANK_10,
ADC_REGULAR_RANK_11,
ADC_REGULAR_RANK_12,
ADC_REGULAR_RANK_13,
ADC_REGULAR_RANK_14,
ADC_REGULAR_RANK_15,
ADC_REGULAR_RANK_16
};

#endif
#endif

/************************************************
* I2C
***********************************************/
Expand Down
6 changes: 3 additions & 3 deletions Core/Src/Runes/generated_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
extern "C" {
const char DESCRIPTION[255] __attribute__((section(".metadata_pool"))) =
"****************" // placeholder for beggining
"20260218T203535" // DateTime using ISO-8601 format
"20260316T225223" // DateTime using ISO-8601 format
" " // alignment
"019403b6" // STLIB commit
"ee54dc7a" // STLIB commit
"--------" // ADJ commit
"9c87b508" // Board commit
"a3e59583" // Board commit
// the '=' is used for unparsing
;
}
53 changes: 51 additions & 2 deletions Core/Src/stm32h7xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void HAL_MspInit(void) {
*/
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
(void)hadc;
/* ADC MSP is handled by HALAL/Services/ADC/NewADC.hpp */
/* ADC MSP is handled by HALAL/Services/ADC/ADC.hpp */
}

/**
Expand All @@ -103,7 +103,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) {
(void)hadc;
/* ADC MSP is handled by HALAL/Services/ADC/NewADC.hpp */
/* ADC MSP is handled by HALAL/Services/ADC/ADC.hpp */
}

/**
Expand Down Expand Up @@ -711,6 +711,38 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) {
/* USER CODE BEGIN USART2_MspInit 1 */

/* USER CODE END USART2_MspInit 1 */
} else if (huart->Instance == USART3) {
/* USER CODE BEGIN USART3_MspInit 0 */

/* USER CODE END USART3_MspInit 0 */

/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;
PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
Error_Handler();
}

/* Peripheral clock enable */
__HAL_RCC_USART3_CLK_ENABLE();

__HAL_RCC_GPIOD_CLK_ENABLE();
/**USART3 GPIO Configuration
PD8 ------> USART3_TX
PD9 ------>
* USART3_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/* USER CODE BEGIN USART3_MspInit 1 */

/* USER CODE END USART3_MspInit 1 */
}
}

Expand Down Expand Up @@ -753,6 +785,23 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) {
/* USER CODE BEGIN USART2_MspDeInit 1 */

/* USER CODE END USART2_MspDeInit 1 */
} else if (huart->Instance == USART3) {
/* USER CODE BEGIN USART3_MspDeInit 0 */

/* USER CODE END USART3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART3_CLK_DISABLE();

/**USART3 GPIO Configuration
PD8 ------> USART3_TX
PD9 ------>
* USART3_RX
*/
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8 | GPIO_PIN_9);

/* USER CODE BEGIN USART3_MspDeInit 1 */

/* USER CODE END USART3_MspDeInit 1 */
}
}

Expand Down
Loading
Loading