// STM32 Nucleo-L031K6 HAL Blink + printf() example
// Simulation: https://wokwi.com/projects/367244067477216257
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32l0xx_hal.h>
// ST Nucleo Green user LED (PB3)
#define LED_PORT GPIOB
#define LED_PIN GPIO_PIN_3
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
#define VCP_TX_Pin GPIO_PIN_2
#define VCP_RX_Pin GPIO_PIN_15
UART_HandleTypeDef huart2;
static void MX_USART2_UART_Init(void);
// int main(void)
// {
// // HAL_Init();
// // SystemClock_Config();
// // initGPIO();
// MX_USART2_UART_Init();
// printf("Hello, %s!\n", "Wokwi");
// // HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
// // while (1);
// // return 0;
// // 1. הפעלת שעון לפורט B (כתיבת 2 לכתובת השעון)
// *(unsigned int *)0x4002102CU = 0x02U;
// // // 2. הגדרת פין 3 כ-Output (כתיבת הערך המתאים לרגיסטר המצב)
// *(unsigned int *)0x50000400U = 0x40U;
// while (1) {
// // // 3. הדלקת הלד (פין 3)
// *(unsigned int *)0x50000414U = 0x08U;
// // // 4. לולאת השהיה (בדיוק כמו בתמונה שלך)
// volatile int counter = 0;
// while (counter < 10000000) {
// ++counter;
// }
// int x;
// scanf("%d", &x);
// // // 5. כיבוי הלד
// *(unsigned int *)0x50000414U = 0x00U;
// counter = 0;
// while (counter < 10000000) {
// ++counter;
// }
// scanf("%d", &x);
// }
// return 0;
// }
int main() {
// אתחול תקשורת להדפסה בלבד
MX_USART2_UART_Init();
printf("Blink with system register names!\n");
// 1. הפעלת שעון לפורט B - שימוש במקרו של המערכת
RCC->IOPENR = 0x02U;
// 2. הגדרת פין PB3 כ-Output - גישה ישירה לרגיסטר MODER
GPIOB->MODER = 0x40U;
// בבקר הזה אין צורך ב-Digital Enable (DEN), אז מדלגים לשלב הבא
while (1) {
// 3. הדלקת הלד (פין 3) - גישה ישירה לרגיסטר הנתונים ODR
GPIOB->ODR = 0x08U;
// 4. לולאת השהיה (בדיוק כמו בתמונה מהשיעור)
volatile int counter = 0;
while (counter < 500000) {
++counter;
}
// 5. כיבוי הלד
GPIOB->ODR = 0x00U;
// 6. לולאת השהיה נוספת
counter = 0;
while (counter < 500000) {
++counter;
}
}
return 0;
}
/**
@brief USART2 Initialization Function
@param None
@retval None
*/
static void MX_USART2_UART_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA15 ------> USART2_RX
*/
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = VCP_TX_Pin | VCP_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_USART2_CLK_ENABLE();
}
void Error_Handler(void)
{
/* User can add his own implementation to report the HAL error return state */
}
// The following makes printf() write to USART2:
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
int _write(int file, uint8_t *ptr, int len)
{
switch (file)
{
case STDOUT_FILENO:
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
break;
case STDERR_FILENO:
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
break;
default:
return -1;
}
return len;
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6