Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Src/boards/rl/mini_v3/board/board.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(BOARD_FLASH_ORIGIN_HEX 0x08000000)
set(BOARD_FLASH_SIZE_KB 512)
set(BOOTLOADER_RESERVED_KB 64)
list(APPEND APPLICATION_SOURCES
${CMAKE_CURRENT_LIST_DIR}/gpio.cpp
${CMAKE_CURRENT_LIST_DIR}/pwm.cpp
${CMAKE_CURRENT_LIST_DIR}/rcpwm_channels.cpp
)
Expand Down
25 changes: 25 additions & 0 deletions Src/boards/rl/mini_v3/board/gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
*/

#include <array>
#include <span>
#include "gpio_mapping.hpp"
#include "peripheral/gpio/gpio_stm32.hpp"

namespace HAL {
namespace {
const std::array<GpioPinInfo, 2> kGpios = {{
{.port = CAN1_TERMINATOR_GPIO_Port, .pin = CAN1_TERMINATOR_Pin},
{.port = CAN2_TERMINATOR_GPIO_Port, .pin = CAN2_TERMINATOR_Pin},
}
};
static_assert(BoardGpio::CAN1_TERMINATOR == 0);
static_assert(BoardGpio::CAN2_TERMINATOR == 1);
static_assert(BoardGpio::CAN2_TERMINATOR + 1 == kGpios.size());
}

const std::span<const GpioPinInfo> gpios{kGpios};

} // namespace HAL
13 changes: 13 additions & 0 deletions Src/boards/rl/mini_v3/board/gpio_mapping.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
*/

#pragma once
#include "peripheral/gpio/gpio.hpp"

namespace BoardGpio {
static constexpr HAL::GpioPin INVALID = HAL::GPIO_INVALID_PIN;
static constexpr HAL::GpioPin CAN1_TERMINATOR = 0;
static constexpr HAL::GpioPin CAN2_TERMINATOR = 1;
} // namespace BoardGpio
28 changes: 0 additions & 28 deletions Src/boards/rl/node_v4/board/gpio_mapping.hpp

This file was deleted.

8 changes: 6 additions & 2 deletions Src/modules/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "module.hpp"
#include "main.h"

#if defined(CAN1_TERMINATOR_Pin) && defined(CAN2_TERMINATOR_Pin)
#include "gpio_mapping.hpp"
#endif

#ifndef LIBPARAMS_HAS_REDUNDANT_STORAGE
#define LIBPARAMS_HAS_REDUNDANT_STORAGE 1
#endif
Expand Down Expand Up @@ -85,8 +89,8 @@ static int8_t init_board_periphery() {
auto teminator_param = paramsGetIntegerValue(IntParamsIndexes::PARAM_SYSTEM_CAN_TEMINATOR);

std::bitset<2> terminator_mask(teminator_param);
HAL::GPIO::set(HAL::GPIO::Pin::CAN1_TERMINATOR, terminator_mask[0]);
HAL::GPIO::set(HAL::GPIO::Pin::CAN2_TERMINATOR, terminator_mask[1]);
HAL::GPIO::set(BoardGpio::CAN1_TERMINATOR, terminator_mask[0]);
HAL::GPIO::set(BoardGpio::CAN2_TERMINATOR, terminator_mask[1]);
#endif
return 0;
}
Expand Down
9 changes: 4 additions & 5 deletions Src/peripheral/gpio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ if(NOT APP_PLATFORM)
message(SEND_ERROR "APP_PLATFORM is not specified or unsupported! Options: stm32f103, stm32g0b1, stm32h753xx, ubuntu.")
endif()

if(APP_PLATFORM STREQUAL "stm32f103")
elseif(APP_PLATFORM STREQUAL "stm32g0b1")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/gpio_stm32g0.cpp)
elseif(APP_PLATFORM STREQUAL "stm32h753xx")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/gpio_stm32h7.cpp)
if(APP_PLATFORM STREQUAL "stm32f103" OR
APP_PLATFORM STREQUAL "stm32g0b1" OR
APP_PLATFORM STREQUAL "stm32h753xx")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/gpio_stm32.cpp)
elseif(APP_PLATFORM STREQUAL "ubuntu")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/gpio_ubuntu.cpp)
else()
Expand Down
43 changes: 9 additions & 34 deletions Src/peripheral/gpio/gpio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,17 @@

namespace HAL {

using GpioPin = uint8_t;
inline constexpr GpioPin GPIO_INVALID_PIN = UINT8_MAX;

class GPIO {
public:
enum class Pin {
/**
* @brief Any node must have an internal RGB LED
*/
INTERNAL_LED_RED,
INTERNAL_LED_GREEN,
INTERNAL_LED_BLUE,

/**
* @brief A node may have an auxilliary external RGB LED
*/
EXT_RGB_LED_RED,
EXT_RGB_LED_GREEN,
EXT_RGB_LED_BLUE,

/**
* @brief CAN terminator 120 ohm resistor
* v2: doesn't exist yet
* v3: PA15
*/
CAN1_TERMINATOR,

/**
* @brief CAN terminator 120 ohm resistor
* v2: doesn't exist yet
* v3: PB15
*/
CAN2_TERMINATOR,

GPIO_AMOUNT,
};

static void set(const Pin gpio_pin, bool state);
static bool get(const Pin gpio_pin);
static uint8_t pin_count();
static void set(GpioPin gpio_pin, bool state);
static bool get(GpioPin gpio_pin);

private:
static bool is_valid_pin(GpioPin gpio_pin);
};

} // namespace HAL
Expand Down
42 changes: 42 additions & 0 deletions Src/peripheral/gpio/gpio_stm32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
*/

#include "peripheral/gpio/gpio_stm32.hpp"
#include <array>

namespace HAL {

namespace {
const std::array<GpioPinInfo, 0> kNoGpios{};
}

extern const std::span<const GpioPinInfo> gpios __attribute__((weak)) = kNoGpios;

bool GPIO::is_valid_pin(GpioPin gpio_pin) {
return gpio_pin < pin_count();
}

uint8_t GPIO::pin_count() {
return static_cast<uint8_t>(gpios.size());
}

void GPIO::set(GpioPin gpio_pin, bool state) {
if (!is_valid_pin(gpio_pin)) {
return;
}

const auto pin_state = state ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(gpios[gpio_pin].port, gpios[gpio_pin].pin, pin_state);
}

bool GPIO::get(GpioPin gpio_pin) {
if (!is_valid_pin(gpio_pin)) {
return false;
}

return HAL_GPIO_ReadPin(gpios[gpio_pin].port, gpios[gpio_pin].pin) == GPIO_PIN_SET;
}

} // namespace HAL
24 changes: 24 additions & 0 deletions Src/peripheral/gpio/gpio_stm32.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
*/

#ifndef SRC_PERIPHERAL_GPIO_STM32_HPP_
#define SRC_PERIPHERAL_GPIO_STM32_HPP_

#include <span>
#include "main.h"
#include "peripheral/gpio/gpio.hpp"

namespace HAL {

struct GpioPinInfo {
GPIO_TypeDef* port;
uint16_t pin;
};

extern const std::span<const GpioPinInfo> gpios;

} // namespace HAL

#endif // SRC_PERIPHERAL_GPIO_STM32_HPP_
51 changes: 0 additions & 51 deletions Src/peripheral/gpio/gpio_stm32g0.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions Src/peripheral/gpio/gpio_stm32h7.cpp

This file was deleted.

31 changes: 23 additions & 8 deletions Src/peripheral/gpio/gpio_ubuntu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#include "peripheral/gpio/gpio.hpp"
#include <vector>
#include "main.h"
#include "peripheral/gpio/gpio_ubuntu.hpp"
#include <array>

namespace HAL {

static std::vector<bool> gpio((int)GPIO::Pin::GPIO_AMOUNT, false);
namespace {
std::array<bool, 0> kNoGpios{};
}

[[gnu::weak]] std::span<bool> gpios{kNoGpios};

Check failure on line 17 in Src/peripheral/gpio/gpio_ubuntu.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Global variables should be const.

See more on https://sonarcloud.io/project/issues?id=RaccoonlabDev_mini_v2_node&issues=AaBCg3d4Mr8OltruEwPZ&open=AaBCg3d4Mr8OltruEwPZ&pullRequest=143

bool GPIO::is_valid_pin(GpioPin gpio_pin) {
return gpio_pin < pin_count();
}

uint8_t GPIO::pin_count() {
return static_cast<uint8_t>(gpios.size());
}

void GPIO::set(GpioPin gpio_pin, bool state) {
if (!is_valid_pin(gpio_pin)) {
return;
}

void GPIO::set(Pin gpio_pin, bool state) {
gpio[(int)gpio_pin] = state;
gpios[gpio_pin] = state;
}

bool GPIO::get(Pin gpio_pin) {
return gpio[(int)gpio_pin];
bool GPIO::get(GpioPin gpio_pin) {
return is_valid_pin(gpio_pin) ? gpios[gpio_pin] : false;
}

} // namespace HAL
Loading
Loading