1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
| #include "hal.h" #include "stm32f4xx.h"
void HAL_GPIO_Init(GPIO_PinTypeDef pin, GPIO_ModeTypeDef mode, GPIO_OutputTypeTypeDef outputType, GPIO_PullTypeDef pull) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_TypeDef *GPIOx; uint16_t GPIO_Pin;
if (pin == GPIO_PIN_0) { GPIOx = GPIOA; GPIO_Pin = GPIO_PIN_0; } else if (pin == GPIO_PIN_1) { GPIOx = GPIOA; GPIO_Pin = GPIO_PIN_1; }
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin;
if (mode == GPIO_MODE_INPUT) { GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; } else if (mode == GPIO_MODE_OUTPUT) { GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; if (outputType == GPIO_OUTPUT_PP) { GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; } else if (outputType == GPIO_OUTPUT_OD) { GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; } GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; } else if (mode == GPIO_MODE_AF) { GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; }
if (pull == GPIO_PULL_NONE) { GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; } else if (pull == GPIO_PULL_UP) { GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; } else if (pull == GPIO_PULL_DOWN) { GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; }
GPIO_Init(GPIOx, &GPIO_InitStruct); }
void HAL_GPIO_WritePin(GPIO_PinTypeDef pin, bool pinState) { GPIO_TypeDef *GPIOx; uint16_t GPIO_Pin; if (pin == GPIO_PIN_0) { GPIOx = GPIOA; GPIO_Pin = GPIO_PIN_0; } else if (pin == GPIO_PIN_1) { GPIOx = GPIOA; GPIO_Pin = GPIO_PIN_1; }
if (pinState) { GPIO_SetBits(GPIOx, GPIO_Pin); } else { GPIO_ResetBits(GPIOx, GPIO_Pin); } }
bool HAL_GPIO_ReadPin(GPIO_PinTypeDef pin) { GPIO_TypeDef *GPIOx; uint16_t GPIO_Pin; if (pin == GPIO_PIN_0) { GPIOx = GPIOA; GPIO_Pin = GPIO_PIN_0; } else if (pin == GPIO_PIN_1) { GPIOx = GPIOA; GPIO_Pin = GPIO_PIN_1; }
return GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == Bit_SET; }
void HAL_ADC_Init(ADC_ChannelTypeDef channel, ADC_ResolutionTypeDef resolution) { ADC_InitTypeDef ADC_InitStruct; ADC_CommonInitTypeDef ADC_CommonInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStruct);
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; if (resolution == ADC_RESOLUTION_8BIT) ADC_InitStruct.ADC_Resolution = ADC_Resolution_8b; else if (resolution == ADC_RESOLUTION_10BIT) ADC_InitStruct.ADC_Resolution = ADC_Resolution_10b; else if (resolution == ADC_RESOLUTION_12BIT) ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ScanConvMode = DISABLE; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStruct);
if (channel == ADC_CHANNEL_0) { HAL_GPIO_Init(GPIO_PIN_0, GPIO_MODE_INPUT, GPIO_OUTPUT_PP, GPIO_PULL_NONE); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles); }
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1); }
uint16_t HAL_ADC_Read(ADC_ChannelTypeDef channel) { while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); return ADC_GetConversionValue(ADC1); }
void HAL_PWM_Init(PWM_ChannelTypeDef channel, uint32_t frequency) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; GPIO_InitTypeDef GPIO_InitStruct; TIM_TypeDef *TIMx; uint16_t TIM_Channel; uint32_t GPIO_AF_TIMx; GPIO_TypeDef *GPIOx_PWM; uint16_t GPIO_Pin_PWM;
if (channel == PWM_CHANNEL_0) { TIMx = TIM3; TIM_Channel = TIM_Channel_1; GPIO_AF_TIMx = GPIO_AF_TIM3; GPIOx_PWM = GPIOA; GPIO_Pin_PWM = GPIO_Pin_6; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); }
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_PWM; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOx_PWM, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOx_PWM, GPIO_PinSource6, GPIO_AF_TIMx);
uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / frequency / 10000) - 1; TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1; TIM_TimeBaseInitStruct.TIM_Prescaler = PrescalerValue; TIM_TimeBaseInitStruct.TIM_ClockDivision = 0; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = 0; TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
if (TIM_Channel == TIM_Channel_1) TIM_OC1Init(TIMx, &TIM_OCInitStruct); else if (TIM_Channel == TIM_Channel_2) TIM_OC2Init(TIMx, &TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_ENABLE);
TIM_ARRPreloadConfig(TIMx, ENABLE);
TIM_Cmd(TIMx, ENABLE); }
void HAL_PWM_SetDutyCycle(PWM_ChannelTypeDef channel, float dutyCycle) { TIM_TypeDef *TIMx; uint16_t TIM_Channel; if (channel == PWM_CHANNEL_0) { TIMx = TIM3; TIM_Channel = TIM_Channel_1; }
if (dutyCycle < 0.0f) dutyCycle = 0.0f; if (dutyCycle > 1.0f) dutyCycle = 1.0f;
uint16_t pulseValue = (uint16_t)(dutyCycle * 10000); if (TIM_Channel == TIM_Channel_1) TIM_SetCompare1(TIMx, pulseValue); else if (TIM_Channel == TIM_Channel_2) TIM_SetCompare2(TIMx, pulseValue); }
void HAL_Timer_Init(Timer_IDTypeDef timerId, uint32_t period_ms) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; NVIC_InitTypeDef NVIC_InitStruct; TIM_TypeDef *TIMx; IRQn_Type TIMx_IRQn; uint32_t TIMx_RCC_Periph;
if (timerId == TIMER_ID_0) { TIMx = TIM2; TIMx_IRQn = TIM2_IRQn; TIMx_RCC_Periph = RCC_APB1Periph_TIM2; }
RCC_APB1PeriphClockCmd(TIMx_RCC_Periph, ENABLE);
TIM_TimeBaseInitStruct.TIM_Period = period_ms * 1000 - 1; TIM_TimeBaseInitStruct.TIM_Prescaler = SystemCoreClock / 1000000 - 1; TIM_TimeBaseInitStruct.TIM_ClockDivision = 0; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct);
TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = TIMx_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); }
void HAL_Timer_Start(Timer_IDTypeDef timerId) { TIM_TypeDef *TIMx; if (timerId == TIMER_ID_0) { TIMx = TIM2; }
TIM_Cmd(TIMx, ENABLE); }
void HAL_Timer_Stop(Timer_IDTypeDef timerId) { TIM_TypeDef *TIMx; if (timerId == TIMER_ID_0) { TIMx = TIM2; }
TIM_Cmd(TIMx, DISABLE); }
static void (*timer_callbacks[TIMER_ID_MAX])(void) = {NULL};
void HAL_Timer_RegisterCallback(Timer_IDTypeDef timerId, void (*callback)(void)) { if (timerId < TIMER_ID_MAX) { timer_callbacks[timerId] = callback; } }
void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); if (timer_callbacks[TIMER_ID_0] != NULL) { timer_callbacks[TIMER_ID_0](); } } }
void HAL_UART_Init(UART_IDTypeDef uartId, UART_BaudRateTypeDef baudRate) { }
void HAL_UART_Transmit(UART_IDTypeDef uartId, uint8_t *data, uint32_t size) { }
void HAL_UART_Receive(UART_IDTypeDef uartId, uint8_t *data, uint32_t size) { }
static void (*uart_receive_callbacks[UART_ID_MAX])(uint8_t data) = {NULL};
void HAL_UART_RegisterReceiveCallback(UART_IDTypeDef uartId, void (*callback)(uint8_t data)) { if (uartId < UART_ID_MAX) { uart_receive_callbacks[uartId] = callback; } }
void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { uint8_t received_data = USART_ReceiveData(USART1); if (uart_receive_callbacks[UART_ID_0] != NULL) { uart_receive_callbacks[UART_ID_0](received_data); } } }
void HAL_LCD_Init(void) { }
void HAL_LCD_WriteString(const char *str) { while (*str) { HAL_LCD_WriteChar(*str++); } }
void HAL_LCD_Clear(void) { }
void HAL_LCD_SetCursor(uint8_t row, uint8_t col) { }
void HAL_LCD_WriteChar(char c) { }
#define GPIO_PIN_ENCODER_A GPIO_PIN_8 #define GPIO_PIN_ENCODER_B GPIO_PIN_9
static volatile int32_t encoder_count = 0; static void (*encoder_callback_func)(int32_t increment) = NULL;
void HAL_Encoder_Init(void) { HAL_GPIO_Init(GPIO_PIN_ENCODER_A, GPIO_MODE_INPUT, GPIO_OUTPUT_PP, GPIO_PULL_UP); HAL_GPIO_Init(GPIO_PIN_ENCODER_B, GPIO_MODE_INPUT, GPIO_OUTPUT_PP, GPIO_PULL_UP);
}
int32_t HAL_Encoder_ReadCount(void) { return encoder_count; }
void HAL_Encoder_ResetCount(void) { encoder_count = 0; }
void HAL_Encoder_RegisterCallback(void (*callback)(int32_t increment)) { encoder_callback_func = callback; }
void EXTI9_5_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line8) != RESET) { EXTI_ClearITPendingBit(EXTI_Line8); bool pinA_state = HAL_GPIO_ReadPin(GPIO_PIN_ENCODER_A); bool pinB_state = HAL_GPIO_ReadPin(GPIO_PIN_ENCODER_B); if (pinA_state) { if (pinB_state) { encoder_count--; if (encoder_callback_func != NULL) encoder_callback_func(-1); } else { encoder_count++; if (encoder_callback_func != NULL) encoder_callback_func(1); } } else { if (pinB_state) { encoder_count++; if (encoder_callback_func != NULL) encoder_callback_func(1); } else { encoder_count--; if (encoder_callback_func != NULL) encoder_callback_func(-1); } } } if (EXTI_GetITStatus(EXTI_Line9) != RESET) { EXTI_ClearITPendingBit(EXTI_Line9); } }
void HAL_Button_Init(GPIO_PinTypeDef buttonPin) { HAL_GPIO_Init(buttonPin, GPIO_MODE_INPUT, GPIO_OUTPUT_PP, GPIO_PULL_UP); }
bool HAL_Button_IsPressed(GPIO_PinTypeDef buttonPin) { return !HAL_GPIO_ReadPin(buttonPin); }
|