Skip to content

Commit

Permalink
periphery: rewrite pwn and led, so save memory, improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Mar 12, 2024
1 parent e09c4d5 commit 0857904
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 111 deletions.
8 changes: 5 additions & 3 deletions Src/periphery/led/led.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// This software is distributed under the terms of the MIT License.
/// Copyright (c) 2022-2023 Dmitry Ponomarev.
/// Author: Dmitry Ponomarev <[email protected]>
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
*/

#ifndef SRC_APPLICATION_PERIPHERY_LED_LED_HPP_
#define SRC_APPLICATION_PERIPHERY_LED_LED_HPP_
Expand Down
8 changes: 5 additions & 3 deletions Src/periphery/pwm/pwm.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// This software is distributed under the terms of the MIT License.
/// Copyright (c) 2023 Dmitry Ponomarev.
/// Author: Dmitry Ponomarev <[email protected]>
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
*/

#ifndef SRC_APPLICATION_PERIPHERY_PWM_HPP_
#define SRC_APPLICATION_PERIPHERY_PWM_HPP_
Expand Down
50 changes: 21 additions & 29 deletions Src/platform/stm32f103/led.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
/// This software is distributed under the terms of the MIT License.
/// Copyright (c) 2022-2023 Dmitry Ponomarev.
/// Author: Dmitry Ponomarev <[email protected]>
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
*/

#include "periphery/led/led.hpp"
#include "main.h"

static void write_red(GPIO_PinState state) {
HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, state);
}
static void write_green(GPIO_PinState state) {
HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, state);
}
static void write_blue(GPIO_PinState state) {
HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, state);
}

void LedPeriphery::reset() {
HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, GPIO_PIN_SET);
write_red(GPIO_PIN_SET);
write_green(GPIO_PIN_SET);
write_blue(GPIO_PIN_SET);
}

void LedPeriphery::toggle(LedColor led_color) {
void LedPeriphery::toggle(LedColor color) {
auto crnt_time_ms = HAL_GetTick();
GPIO_PinState state = (crnt_time_ms % 1000 > 500) ? GPIO_PIN_SET : GPIO_PIN_RESET;

switch (led_color) {
case LedColor::RED_COLOR:
HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, state);
HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, GPIO_PIN_SET);
break;

case LedColor::GREEN_COLOR:
HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, state);
HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, GPIO_PIN_SET);
break;

case LedColor::BLUE_COLOR:
HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, state);
break;

default:
break;
}
write_red(color == LedColor::RED_COLOR ? state : GPIO_PIN_SET);
write_green(color == LedColor::BLUE_COLOR ? state : GPIO_PIN_SET);
write_blue(color == LedColor::GREEN_COLOR ? state : GPIO_PIN_SET);
}
106 changes: 30 additions & 76 deletions Src/platform/stm32f103/pwm.cpp
Original file line number Diff line number Diff line change
@@ -1,97 +1,51 @@
/// This software is distributed under the terms of the MIT License.
/// Copyright (c) 2022-2023 Dmitry Ponomarev.
/// Author: Dmitry Ponomarev <[email protected]>
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
*/

#include "periphery/pwm/pwm.hpp"
#include "main.h"

extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;

int8_t PwmPeriphery::init(PwmPin pwm_pin) {
switch (pwm_pin) {
case PwmPin::PWM_1:
if (HAL_OK != HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2)) {
return -1;
}
TIM4->CCR2 = 1000;
break;

case PwmPin::PWM_2:
if (HAL_OK != HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1)) {
return -1;
}
TIM4->CCR1 = 1000;
break;
struct PwmPinInfo {
TIM_HandleTypeDef& htim;
uint32_t channel;
volatile uint32_t& ccr;
};

case PwmPin::PWM_3:
if (HAL_OK != HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1)) {
return -1;
}
TIM3->CCR1 = 1000;
break;
const static PwmPinInfo info[static_cast<uint8_t>(PwmPin::PWM_AMOUNT)] = {
{.htim = htim4, .channel = TIM_CHANNEL_2, .ccr = TIM4->CCR2}, // PB7
{.htim = htim4, .channel = TIM_CHANNEL_1, .ccr = TIM4->CCR1}, // PB6
{.htim = htim3, .channel = TIM_CHANNEL_1, .ccr = TIM3->CCR1}, // PB4
{.htim = htim3, .channel = TIM_CHANNEL_2, .ccr = TIM3->CCR2}, // PB5
};

case PwmPin::PWM_4:
if (HAL_OK != HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2)) {
return -1;
}
TIM3->CCR2 = 1000;
break;

default:
return -1;
int8_t PwmPeriphery::init(PwmPin pwm_pin) {
if (pwm_pin > PwmPin::PWM_AMOUNT) {
return -1;
}

return 0;
auto& pwm_pin_info = info[static_cast<uint8_t>(pwm_pin)];
return HAL_OK == HAL_TIM_PWM_Start(&pwm_pin_info.htim, pwm_pin_info.channel) ? 0 : -1;
}

void PwmPeriphery::set_duration(const PwmPin pwm_pin, uint32_t duration_us) {
switch (pwm_pin) {
case PwmPin::PWM_1:
TIM4->CCR2 = duration_us;
break;

case PwmPin::PWM_2:
TIM4->CCR1 = duration_us;
break;

case PwmPin::PWM_3:
TIM3->CCR1 = duration_us;
break;

case PwmPin::PWM_4:
TIM3->CCR2 = duration_us;
break;

default:
break;
if (pwm_pin > PwmPin::PWM_AMOUNT) {
return;
}

auto& pwm_pin_info = info[static_cast<uint8_t>(pwm_pin)];
pwm_pin_info.ccr = duration_us;
}

uint32_t PwmPeriphery::get_duration(PwmPin pwm_pin) {
uint32_t pwm_duration;

switch (pwm_pin) {
case PwmPin::PWM_1:
pwm_duration = TIM4->CCR2;
break;

case PwmPin::PWM_2:
pwm_duration = TIM4->CCR1;
break;

case PwmPin::PWM_3:
pwm_duration = TIM3->CCR1;
break;

case PwmPin::PWM_4:
pwm_duration = TIM3->CCR2;
break;

default:
pwm_duration = 0;
break;
if (pwm_pin > PwmPin::PWM_AMOUNT) {
return 0;
}

return pwm_duration;
auto& pwm_pin_info = info[static_cast<uint8_t>(pwm_pin)];
return pwm_pin_info.ccr;
}

0 comments on commit 0857904

Please sign in to comment.