add Chapter11

This commit is contained in:
Amar Mahmutbegovic
2024-09-22 16:52:43 +02:00
parent 7203d7ab66
commit f10d0ef610
205 changed files with 302793 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#include <cstdint>
#include <cstdio>
#include <stm32f072xb.h>
#include <hal.hpp>
#include <uart_stm32.hpp>
#include <retarget.hpp>
#include <algorithm>
#include <array>
#include <string_view>
#include <stdexcept>
#include <charconv>
struct mac_address {
static constexpr std::size_t c_bytes_num = 6;
static constexpr std::size_t c_mac_addr_str_size = 17;
std::array<uint8_t, c_bytes_num> bytes{};
bool is_valid = false;
constexpr mac_address(std::string_view str) {
if (str.size() != c_mac_addr_str_size) {
return;
}
for (size_t i = 0; i < c_bytes_num; ++i) {
const std::string_view byte_str = str.substr(i * 3, 2);
uint8_t value = 0;
auto result = std::from_chars(byte_str.data(), byte_str.data() + byte_str.size(), value, 16);
if (result.ec != std::errc()) {
return;
}
bytes[i] = value;
}
is_valid = true;
}
};
int main()
{
hal::init();
hal::uart_stm32 uart(USART2);
uart.init();
retarget::set_stdio_uart(&uart);
constexpr mac_address addr("00:11:22:33:44:55");
static_assert(addr.is_valid);
const std::array<uint8_t, 6> addr_arr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
const auto & mac_ref = addr.bytes;
//const auto & mac_ref = addr_arr;
printf("%02X:%02X:%02X:%02X:%02X:%02X\r\n", mac_ref[0], mac_ref[1], mac_ref[2],
mac_ref[3], mac_ref[4], mac_ref[5]);
while(true)
{
}
}

View File

@@ -0,0 +1,101 @@
#include <cstdint>
#include <cstdio>
#include <stm32f072xb.h>
#include <hal.hpp>
#include <uart_stm32.hpp>
#include <retarget.hpp>
#include <algorithm>
#include <array>
#include <gpio_stm32.hpp>
ADC_HandleTypeDef hadc;
static void MX_ADC_Init(void)
{
/* USER CODE BEGIN ADC_Init 0 */
/* USER CODE END ADC_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC_Init 1 */
/* USER CODE END ADC_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
//Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
//Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
/* USER CODE END ADC_Init 2 */
}
int main()
{
hal::init();
hal::uart_stm32 uart(USART2);
uart.init();
retarget::set_stdio_uart(&uart);
MX_ADC_Init();
const hal::gpio_stm32<hal::port_a> button1(hal::pin::p4, [](){
printf("Button1 pressed!\r\n");
});
const hal::gpio_stm32<hal::port_a> button2(hal::pin::p5, [](){
printf("Button2 pressed!\r\n");
});
while(true)
{
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, 1000);
auto adc_val = HAL_ADC_GetValue(&hadc);
float mv = 3.3f * static_cast<float>(adc_val) / 4096.f;
printf("%d, %.2f\r\n", adc_val, mv);
hal::time::delay_ms(1000);
}
}
// adc FeedVoltageSampleToChannel 0 3100 10