Skip to content

Commit 71ebde9

Browse files
authored
Added example to new ADC with DMA (#81)
* Added UART3 in MSP to debug through UART (In the future it will be refactored to a new UARTDomain, but it does the work so far) * Added ADC example with one and two channels on the same peripheral * Simplified example * New hyper CLI * Updated hyper CLI * ST-LIB updated * minor change on banner * Updated docs with new hyper CLI * Moved hard fault analyzer * Moved hard fault analyzer and included on hyper CLI
1 parent b75728a commit 71ebde9

24 files changed

Lines changed: 1787 additions & 189 deletions

Core/Src/Examples/ExampleADC.cpp

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
11
#ifdef EXAMPLE_ADC
22

3+
#include <cstdio>
4+
#include <cstdint>
5+
36
#include "main.h"
47
#include "ST-LIB.hpp"
58

69
using namespace ST_LIB;
710

11+
namespace {
12+
13+
struct ExampleInput {
14+
GPIODomain::Pin pin;
15+
const char* label;
16+
};
17+
18+
constexpr ExampleInput kSingleChannelInput{ST_LIB::PA0, "PA0"};
19+
constexpr ExampleInput kDualChannelInput0{ST_LIB::PA0, "PA0"};
20+
constexpr ExampleInput kDualChannelInput1{ST_LIB::PC0, "PC0"};
21+
22+
constexpr const char* kTerminalHint = "Terminal: ST-LINK VCP over USB (USART3, 115200 8N1)";
23+
constexpr const char* kSingleChannelWiringHint = "Connect PA0 to GND, 3V3 or a potentiometer.";
24+
constexpr const char* kDualChannelWiringHint = "Connect PA0 and PC0 to two analog sources.";
25+
26+
void start_terminal() {
27+
#ifdef HAL_UART_MODULE_ENABLED
28+
if (!UART::set_up_printf(UART::uart3)) {
29+
ErrorHandler("Unable to set up UART printf for ADC example");
30+
}
31+
UART::start();
32+
#endif
33+
}
34+
35+
void print_banner(const char* title, const char* wiring_hint, const char* columns_hint) {
36+
printf("\n\r=== %s ===\n\r", title);
37+
printf("%s\n\r", kTerminalHint);
38+
printf("%s\n\r", wiring_hint);
39+
printf("Columns: %s\n\r\n\r", columns_hint);
40+
}
41+
42+
} // namespace
43+
844
#ifdef TEST_0
945

1046
constinit float adc_value = 0.0f;
47+
constexpr auto adc_input = kSingleChannelInput;
1148
constexpr auto adc = ADCDomain::ADC(
12-
ST_LIB::PA0,
49+
adc_input.pin,
1350
adc_value,
1451
ADCDomain::Resolution::BITS_12,
1552
ADCDomain::SampleTime::CYCLES_8_5
@@ -18,16 +55,90 @@ constexpr auto adc = ADCDomain::ADC(
1855
int main(void) {
1956
using ExampleADCBoard = ST_LIB::Board<adc>;
2057
ExampleADCBoard::init();
58+
start_terminal();
2159

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

24-
// Ready to compile for Nucleo. Validate by wiring PA0 to 3.3V and then to GND,
25-
// and watch adc_value in the debugger to confirm the change.
62+
print_banner("ADC single-channel example", kSingleChannelWiringHint, "t_ms raw voltage[V]");
63+
printf("Reading input: %s\n\r\n\r", adc_input.label);
64+
65+
uint32_t sample_index = 0;
2666
while (1) {
2767
adc_instance.read();
68+
const float raw = adc_instance.get_raw();
69+
const float voltage = adc_instance.get_value();
70+
71+
printf("%10lu %8.0f %10.4f\n\r", HAL_GetTick(), raw, voltage);
72+
73+
++sample_index;
74+
if ((sample_index % 20U) == 0U) {
75+
printf("Current mirrored output buffer value: %.4f V\n\r\n\r", adc_value);
76+
}
77+
2878
HAL_Delay(100);
2979
}
3080
}
3181

3282
#endif // TEST_0
83+
84+
#ifdef TEST_1
85+
86+
constinit float adc_input_0_value = 0.0f;
87+
constinit float adc_input_1_value = 0.0f;
88+
89+
constexpr auto adc_input_0_cfg = kDualChannelInput0;
90+
constexpr auto adc_input_1_cfg = kDualChannelInput1;
91+
92+
constexpr auto adc_input_0 = ADCDomain::ADC(
93+
adc_input_0_cfg.pin,
94+
adc_input_0_value,
95+
ADCDomain::Resolution::BITS_12,
96+
ADCDomain::SampleTime::CYCLES_8_5
97+
);
98+
99+
constexpr auto adc_input_1 = ADCDomain::ADC(
100+
adc_input_1_cfg.pin,
101+
adc_input_1_value,
102+
ADCDomain::Resolution::BITS_12,
103+
ADCDomain::SampleTime::CYCLES_8_5
104+
);
105+
106+
int main(void) {
107+
using ExampleADCBoard = ST_LIB::Board<adc_input_0, adc_input_1>;
108+
ExampleADCBoard::init();
109+
start_terminal();
110+
111+
auto& adc_input_0_instance = ExampleADCBoard::instance_of<adc_input_0>();
112+
auto& adc_input_1_instance = ExampleADCBoard::instance_of<adc_input_1>();
113+
114+
print_banner(
115+
"ADC dual-channel example",
116+
kDualChannelWiringHint,
117+
"t_ms raw_0 raw_1 v_0[V] v_1[V]"
118+
);
119+
printf("Reading inputs: %s and %s\n\r\n\r", adc_input_0_cfg.label, adc_input_1_cfg.label);
120+
121+
while (1) {
122+
adc_input_0_instance.read();
123+
adc_input_1_instance.read();
124+
125+
const float raw_0 = adc_input_0_instance.get_raw();
126+
const float raw_1 = adc_input_1_instance.get_raw();
127+
const float voltage_0 = adc_input_0_instance.get_value();
128+
const float voltage_1 = adc_input_1_instance.get_value();
129+
130+
printf(
131+
"%10lu %8.0f %8.0f %10.4f %10.4f\n\r",
132+
HAL_GetTick(),
133+
raw_0,
134+
raw_1,
135+
voltage_0,
136+
voltage_1
137+
);
138+
139+
HAL_Delay(100);
140+
}
141+
}
142+
143+
#endif // TEST_1
33144
#endif // EXAMPLE_ADC

Core/Src/Runes/Runes.cpp

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -74,96 +74,30 @@ UART::Instance UART::instance2 = {
7474
.word_length = UART_WORDLENGTH_8B,
7575
};
7676

77+
UART::Instance UART::instance3 = {
78+
.TX = PD8,
79+
.RX = PD9,
80+
.huart = &huart3,
81+
.instance = USART3,
82+
.baud_rate = 115200,
83+
.word_length = UART_WORDLENGTH_8B,
84+
};
85+
7786
UART::Peripheral UART::uart1 = UART::Peripheral::peripheral1;
7887
UART::Peripheral UART::uart2 = UART::Peripheral::peripheral2;
88+
UART::Peripheral UART::uart3 = UART::Peripheral::peripheral3;
7989

8090
unordered_map<UART::Peripheral, UART::Instance*> UART::available_uarts = {
8191
{UART::uart1, &UART::instance1},
8292
{UART::uart2, &UART::instance2},
93+
{UART::uart3, &UART::instance3},
8394
};
8495

8596
uint8_t UART::printf_uart = 0;
8697
bool UART::printf_ready = false;
8798

8899
#endif
89100

90-
/************************************************
91-
* ADC
92-
***********************************************/
93-
#if 0 // Legacy ADC (replaced by NewADC). Kept here only as reference.
94-
#if defined(HAL_ADC_MODULE_ENABLED) && defined(HAL_LPTIM_MODULE_ENABLED)
95-
96-
LowPowerTimer lptim1(*LPTIM1, hlptim1, LPTIM1_PERIOD, "LPTIM 1");
97-
LowPowerTimer lptim2(*LPTIM2, hlptim2, LPTIM2_PERIOD, "LPTIM 2");
98-
LowPowerTimer lptim3(*LPTIM3, hlptim3, LPTIM3_PERIOD, "LPTIM 3");
99-
100-
vector<uint32_t> channels1 = {};
101-
vector<uint32_t> channels2 = {};
102-
vector<uint32_t> channels3 = {};
103-
104-
ST_LIB::DMA_Domain::Instance dma_adc1 = {hdma_adc1};
105-
ST_LIB::DMA_Domain::Instance dma_adc2 = {hdma_adc2};
106-
ST_LIB::DMA_Domain::Instance dma_adc3 = {hdma_adc3};
107-
108-
ADC::InitData init_data1(ADC1, ADC_RESOLUTION_16B, ADC_EXTERNALTRIG_LPTIM1_OUT,
109-
channels1, &dma_adc1, "ADC 1");
110-
ADC::InitData init_data2(ADC2, ADC_RESOLUTION_16B, ADC_EXTERNALTRIG_LPTIM2_OUT,
111-
channels2, &dma_adc2, "ADC 2");
112-
ADC::InitData init_data3(ADC3, ADC_RESOLUTION_12B, ADC_EXTERNALTRIG_LPTIM3_OUT,
113-
channels3, &dma_adc3, "ADC 3");
114-
115-
ADC::Peripheral ADC::peripherals[3] = {
116-
ADC::Peripheral(&hadc1, lptim1, init_data1),
117-
ADC::Peripheral(&hadc2, lptim2, init_data2),
118-
ADC::Peripheral(&hadc3, lptim3, init_data3)
119-
};
120-
121-
map<Pin, ADC::Instance> ADC::available_instances = {
122-
{PF11, Instance(&peripherals[0], ADC_CHANNEL_2)},
123-
{PF12, Instance(&peripherals[0], ADC_CHANNEL_6)},
124-
{PF13, Instance(&peripherals[1], ADC_CHANNEL_2)},
125-
{PF14, Instance(&peripherals[1], ADC_CHANNEL_6)},
126-
{PF5, Instance(&peripherals[2], ADC_CHANNEL_4)},
127-
{PF6, Instance(&peripherals[2], ADC_CHANNEL_8)},
128-
{PF7, Instance(&peripherals[2], ADC_CHANNEL_3)},
129-
{PF8, Instance(&peripherals[2], ADC_CHANNEL_7)},
130-
{PF9, Instance(&peripherals[2], ADC_CHANNEL_2)},
131-
{PF10, Instance(&peripherals[2], ADC_CHANNEL_6)},
132-
{PC2, Instance(&peripherals[2], ADC_CHANNEL_0)},
133-
{PC3, Instance(&peripherals[2], ADC_CHANNEL_1)},
134-
{PF10, Instance(&peripherals[2], ADC_CHANNEL_6)},
135-
{PC0, Instance(&peripherals[0], ADC_CHANNEL_10)},
136-
{PA0, Instance(&peripherals[0], ADC_CHANNEL_16)},
137-
{PA3, Instance(&peripherals[0], ADC_CHANNEL_15)},
138-
{PA4, Instance(&peripherals[0], ADC_CHANNEL_18)},
139-
{PA5, Instance(&peripherals[0], ADC_CHANNEL_19)},
140-
{PA6, Instance(&peripherals[0], ADC_CHANNEL_3)},
141-
{PB0, Instance(&peripherals[0], ADC_CHANNEL_9)},
142-
{PB1, Instance(&peripherals[0], ADC_CHANNEL_5)}
143-
};
144-
145-
uint32_t ADC::ranks[16] = {
146-
ADC_REGULAR_RANK_1,
147-
ADC_REGULAR_RANK_2,
148-
ADC_REGULAR_RANK_3,
149-
ADC_REGULAR_RANK_4,
150-
ADC_REGULAR_RANK_5,
151-
ADC_REGULAR_RANK_6,
152-
ADC_REGULAR_RANK_7,
153-
ADC_REGULAR_RANK_8,
154-
ADC_REGULAR_RANK_9,
155-
ADC_REGULAR_RANK_10,
156-
ADC_REGULAR_RANK_11,
157-
ADC_REGULAR_RANK_12,
158-
ADC_REGULAR_RANK_13,
159-
ADC_REGULAR_RANK_14,
160-
ADC_REGULAR_RANK_15,
161-
ADC_REGULAR_RANK_16
162-
};
163-
164-
#endif
165-
#endif
166-
167101
/************************************************
168102
* I2C
169103
***********************************************/

Core/Src/Runes/generated_metadata.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
extern "C" {
66
const char DESCRIPTION[255] __attribute__((section(".metadata_pool"))) =
77
"****************" // placeholder for beggining
8-
"20260218T203535" // DateTime using ISO-8601 format
8+
"20260316T225223" // DateTime using ISO-8601 format
99
" " // alignment
10-
"019403b6" // STLIB commit
10+
"ee54dc7a" // STLIB commit
1111
"--------" // ADJ commit
12-
"9c87b508" // Board commit
12+
"a3e59583" // Board commit
1313
// the '=' is used for unparsing
1414
;
1515
}

Core/Src/stm32h7xx_hal_msp.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void HAL_MspInit(void) {
9292
*/
9393
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
9494
(void)hadc;
95-
/* ADC MSP is handled by HALAL/Services/ADC/NewADC.hpp */
95+
/* ADC MSP is handled by HALAL/Services/ADC/ADC.hpp */
9696
}
9797

9898
/**
@@ -103,7 +103,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
103103
*/
104104
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) {
105105
(void)hadc;
106-
/* ADC MSP is handled by HALAL/Services/ADC/NewADC.hpp */
106+
/* ADC MSP is handled by HALAL/Services/ADC/ADC.hpp */
107107
}
108108

109109
/**
@@ -711,6 +711,38 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) {
711711
/* USER CODE BEGIN USART2_MspInit 1 */
712712

713713
/* USER CODE END USART2_MspInit 1 */
714+
} else if (huart->Instance == USART3) {
715+
/* USER CODE BEGIN USART3_MspInit 0 */
716+
717+
/* USER CODE END USART3_MspInit 0 */
718+
719+
/** Initializes the peripherals clock
720+
*/
721+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;
722+
PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
723+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
724+
Error_Handler();
725+
}
726+
727+
/* Peripheral clock enable */
728+
__HAL_RCC_USART3_CLK_ENABLE();
729+
730+
__HAL_RCC_GPIOD_CLK_ENABLE();
731+
/**USART3 GPIO Configuration
732+
PD8 ------> USART3_TX
733+
PD9 ------>
734+
* USART3_RX
735+
*/
736+
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
737+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
738+
GPIO_InitStruct.Pull = GPIO_NOPULL;
739+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
740+
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
741+
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
742+
743+
/* USER CODE BEGIN USART3_MspInit 1 */
744+
745+
/* USER CODE END USART3_MspInit 1 */
714746
}
715747
}
716748

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

755787
/* USER CODE END USART2_MspDeInit 1 */
788+
} else if (huart->Instance == USART3) {
789+
/* USER CODE BEGIN USART3_MspDeInit 0 */
790+
791+
/* USER CODE END USART3_MspDeInit 0 */
792+
/* Peripheral clock disable */
793+
__HAL_RCC_USART3_CLK_DISABLE();
794+
795+
/**USART3 GPIO Configuration
796+
PD8 ------> USART3_TX
797+
PD9 ------>
798+
* USART3_RX
799+
*/
800+
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8 | GPIO_PIN_9);
801+
802+
/* USER CODE BEGIN USART3_MspDeInit 1 */
803+
804+
/* USER CODE END USART3_MspDeInit 1 */
756805
}
757806
}
758807

0 commit comments

Comments
 (0)