#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "stm32f1xx_hall.h"
#include "stm32f1xx_hal_uart.h"
#include "stm32f1xx_hal_gpio.h"
#include "stm32f1xx_hal_rcc.h"
#include "stm32f1xx_hal_cortex.h"
#define LED_PORT GPIOC
#define LED_PIN GPIO_PIN_13
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define IN1_PIN GPIO_PIN_0
#define IN2_PIN GPIO_PIN_1
#define IN3_PIN GPIO_PIN_2
#define IN4_PIN GPIO_PIN_3
#define IN_PORT GPIOA
#define IN_PORT_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE
UART_HandleTypeDef huart2;
void Error_Handler(void);
void SystemClock_Config(void);
static void MX_USART2_UART_Init(void);
void SysTick_Handler(void)
{
HAL_IncTick();
}
void initGPIO()
{
GPIO_InitTypeDef GPIO_Config;
// GPIO configuration for LED
GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Config.Pull = GPIO_NOPULL;
GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_Config.Pin = LED_PIN;
LED_PORT_CLK_ENABLE();
HAL_GPIO_Init(LED_PORT, &GPIO_Config);
// GPIO configuration for L298N
GPIO_Config.Pin = IN1_PIN | IN2_PIN | IN3_PIN | IN4_PIN;
IN_PORT_CLK_ENABLE();
HAL_GPIO_Init(IN_PORT, &GPIO_Config);
}
void setStepperMotor(int step)
{
// Define the step sequence for the stepper motor
uint8_t stepSequence[4][4] = {
{GPIO_PIN_SET, GPIO_PIN_RESET, GPIO_PIN_SET, GPIO_PIN_RESET}, // Step 1
{GPIO_PIN_RESET, GPIO_PIN_SET, GPIO_PIN_SET, GPIO_PIN_RESET}, // Step 2
{GPIO_PIN_RESET, GPIO_PIN_SET, GPIO_PIN_RESET, GPIO_PIN_SET}, // Step 3
{GPIO_PIN_SET, GPIO_PIN_RESET, GPIO_PIN_RESET, GPIO_PIN_SET} // Step 4
};
HAL_GPIO_WritePin(IN_PORT, IN1_PIN, stepSequence[step][0]);
HAL_GPIO_WritePin(IN_PORT, IN2_PIN, stepSequence[step][1]);
HAL_GPIO_WritePin(IN_PORT, IN3_PIN, stepSequence[step][2]);
HAL_GPIO_WritePin(IN_PORT, IN4_PIN, stepSequence[step][3]);
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
int main(void)
{
const char * hello_world = "Hello STM32\r\n";
HAL_Init();
SystemClock_Config();
initGPIO();
MX_USART2_UART_Init();
HAL_UART_Transmit(&huart2, (uint8_t *)hello_world, strlen(hello_world), HAL_MAX_DELAY);
while (1)
{
for (int step = 0; step < 4; step++)
{
setStepperMotor(step);
Delay(100000);
}
}
return 0;
}
/**
@brief System Clock Configuration
@retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Initializes the CPU, AHB and APB busses clocks */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Initializes the CPU, AHB and APB busses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
@brief USART2 Initialization Function
@param None
@retval None
*/
static void MX_USART2_UART_Init(void)
{
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;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
int _write(int file, char *ptr, int len)
{
switch (file)
{
case STDOUT_FILENO: /*stdout*/
HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
break;
case STDERR_FILENO: /* stderr */
HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
break;
default:
return -1;
}
return len;
}
void Error_Handler(void)
{
/* User can add his own implementation to report the HAL error return state */
}
#ifdef USE_FULL_ASSERT
/**
@brief Reports the name of the source file and the source line number
where the assert_param error has occurred.
@param file: pointer to the source file name
@param line: assert_param error line source number
@retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */