Files
@ a6cb9f350919
Branch filter:
Location: HydroBot/protomodule-firmware/Src/gpio.c
a6cb9f350919
3.9 KiB
text/plain
Updated to hydrobot baseline functionality
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 | #include "gpio.h"
TIM_HandleTypeDef htim1;
uint8_t led_brightness = 0;
gpio_led_t led1 = {GPIO_PIN_10, GPIOA, &htim1, TIM_CHANNEL_3};
gpio_led_t led2 = {GPIO_PIN_9, GPIOA, &htim1, TIM_CHANNEL_2};
gpio_led_t led3 = {GPIO_PIN_8, GPIOA, &htim1, TIM_CHANNEL_1};
gpio_led_t* leds[GPIO_NUM_LEDS] = {&led1, &led2, &led3};
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
void gpio_init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
// /*Configure GPIO pin Output Level */
// HAL_GPIO_WritePin(GPIOA, LED3_Pin|LED2_Pin|LED1_Pin, GPIO_PIN_RESET);
//
// /*Configure GPIO pins : PAPin PAPin PAPin */
// GPIO_InitStruct.Pin = LED3_Pin|LED2_Pin|LED1_Pin;
// GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
TIM_OC_InitTypeDef sConfigOC;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xFFF;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim1);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim1);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
HAL_TIM_MspPostInit(&htim1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
}
bool gpio_set_led(led_name_t led, bool value)
{
bool result = true;
__HAL_TIM_SET_COMPARE(leds[led]->timer, leds[led]->channel, (uint16_t)(led_brightness * 40.95 * value));
return result;
}
bool gpio_toggle_led(led_name_t led)
{
bool result = true;
if (__HAL_TIM_GET_COMPARE(leds[led]->timer, leds[led]->channel))
{
__HAL_TIM_SET_COMPARE(leds[led]->timer, leds[led]->channel, 0);
}
else
{
__HAL_TIM_SET_COMPARE(leds[led]->timer, leds[led]->channel, (uint16_t)(led_brightness * 40.95));
}
return result;
}
bool gpio_set_led_brightness(uint8_t brightness)
{
bool result = true;
led_brightness = brightness;
for (uint8_t i = 0; i < GPIO_NUM_LEDS; i++)
{
gpio_led_t* led = leds[i];
if (__HAL_TIM_GET_COMPARE(led->timer, led->channel))
{
__HAL_TIM_SET_COMPARE(led->timer, led->channel, (uint16_t)(led_brightness * 40.95));
}
}
return result;
}
|