Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

update peripheral #84

Merged
merged 6 commits into from
Nov 17, 2024
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if(USE_PLATFORM_UBUNTU)
set(CAN_PLATFORM socketcan)
set(APP_PLATFORM ubuntu)
set(PLATFORM_NAME sitl)
elseif(USE_PLATFORM_NODE_V2)
elseif(USE_PLATFORM_NODE_V2)
set(LIBPARAMS_PLATFORM stm32f103)
set(CAN_PLATFORM bxcan)
set(APP_PLATFORM stm32f103)
Expand Down
2 changes: 2 additions & 0 deletions Src/modules/system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ list(APPEND APPLICATION_SOURCES
list(APPEND LIBPARAMS_PARAMS
${CMAKE_CURRENT_LIST_DIR}/params.yaml
)

include(${ROOT_DIR}/Src/peripheral/led/CMakeLists.txt)
20 changes: 20 additions & 0 deletions Src/peripheral/led/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) 2023-2024 Dmitry Ponomarev <ponomarevda96@gmail.com>
# Distributed under the terms of the GPL v3 license, available in the file LICENSE.

# Include guard
if(PERIPHERAL_LED_CMAKE)
return()
endif()
set(PERIPHERAL_LED_CMAKE ${CMAKE_CURRENT_LIST_DIR})

if(NOT APP_PLATFORM)
message(SEND_ERROR "APP_PLATFORM is not specified or unsupported! Options: stm32f103, stm32g0b1, ubuntu.")
endif()

if(APP_PLATFORM STREQUAL "stm32f103" OR APP_PLATFORM STREQUAL "stm32g0b1")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/led_stm32.cpp)
elseif(APP_PLATFORM STREQUAL "ubuntu")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/led_ubuntu.cpp)
else()
message(SEND_ERROR "APP_PLATFORM is unknown.")
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#include "peripheral/led/led.hpp"
#include "led.hpp"
#include <cstddef>
#include "main.h"

Expand All @@ -22,14 +22,26 @@ namespace Board {
static void write_red(bool enabled) {
GPIO_PinState state = enabled ? GPIO_PIN_RESET : GPIO_PIN_SET;
HAL_GPIO_WritePin(INTERNAL_LED_RED_GPIO_Port, INTERNAL_LED_RED_Pin, state);

#ifdef EXT_RGB_LED_RED_GPIO_Port
HAL_GPIO_WritePin(EXT_RGB_LED_RED_GPIO_Port, EXT_RGB_LED_RED_Pin, state);
#endif
}
static void write_green(bool enabled) {
GPIO_PinState state = enabled ? GPIO_PIN_RESET : GPIO_PIN_SET;
HAL_GPIO_WritePin(INTERNAL_LED_GREEN_GPIO_Port, INTERNAL_LED_GREEN_Pin, state);

#ifdef EXT_RGB_LED_GREEN_GPIO_Port
HAL_GPIO_WritePin(EXT_RGB_LED_GREEN_GPIO_Port, EXT_RGB_LED_GREEN_Pin, state);
#endif
}
static void write_blue(bool enabled) {
GPIO_PinState state = enabled ? GPIO_PIN_RESET : GPIO_PIN_SET;
HAL_GPIO_WritePin(INTERNAL_LED_BLUE_GPIO_Port, INTERNAL_LED_BLUE_Pin, state);

#ifdef EXT_RGB_LED_BLUE_GPIO_Port
HAL_GPIO_WritePin(EXT_RGB_LED_BLUE_GPIO_Port, EXT_RGB_LED_BLUE_Pin, state);
#endif
}

void Led::set(Color color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#include "peripheral/led/led.hpp"
#include "led.hpp"

namespace Board {

Expand Down
20 changes: 20 additions & 0 deletions Src/peripheral/spi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) 2023-2024 Dmitry Ponomarev <ponomarevda96@gmail.com>
# Distributed under the terms of the GPL v3 license, available in the file LICENSE.

# Include guard
if(PERIPHERAL_SPI_CMAKE)
return()
endif()
set(PERIPHERAL_SPI_CMAKE ${CMAKE_CURRENT_LIST_DIR})

if(NOT APP_PLATFORM)
message(SEND_ERROR "APP_PLATFORM is not specified or unsupported! Options: stm32f103, stm32g0b1, ubuntu.")
endif()

if(APP_PLATFORM STREQUAL "stm32f103" OR APP_PLATFORM STREQUAL "stm32g0b1")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/spi_stm32.cpp)
elseif(APP_PLATFORM STREQUAL "ubuntu")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/spi_ubuntu.cpp)
else()
message(SEND_ERROR "APP_PLATFORM is unknown.")
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ static void spi_set_nss(bool nss_state) {
#ifdef SPI2_NSS_GPIO_Port
auto state = nss_state ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(SPI2_NSS_GPIO_Port, SPI2_NSS_Pin, state);
#elif defined(SPI_SS_GPIO_Port)
auto state = nss_state ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, state);
#endif
}

Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions Src/peripheral/uart/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) 2023-2024 Dmitry Ponomarev <ponomarevda96@gmail.com>
# Distributed under the terms of the GPL v3 license, available in the file LICENSE.

# Include guard
if(PERIPHERAL_UART_CMAKE)
return()
endif()
set(PERIPHERAL_UART_CMAKE ${CMAKE_CURRENT_LIST_DIR})

if(NOT APP_PLATFORM)
message(SEND_ERROR "APP_PLATFORM is not specified or unsupported! Options: stm32f103, stm32g0b1, ubuntu.")
endif()

if(APP_PLATFORM STREQUAL "stm32f103" OR APP_PLATFORM STREQUAL "stm32g0b1")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/uart_stm32.cpp)
elseif(APP_PLATFORM STREQUAL "ubuntu")
list(APPEND PERIPHERAL_SOURCES ${CMAKE_CURRENT_LIST_DIR}/uart_ubuntu.cpp)
else()
message(SEND_ERROR "APP_PLATFORM is unknown.")
endif()
53 changes: 48 additions & 5 deletions Src/peripheral/uart/uart.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
// Copyright (C) 2023 Dmitry Ponomarev <ponomarevda96@gmail.com>
// Distributed under the terms of the GPL v3 license, available in the file LICENSE.
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#ifndef SRC_PERIPHERY_UART_UART_HPP_
#define SRC_PERIPHERY_UART_UART_HPP_
#ifndef SRC_PERIPHERAL_UART_UART_HPP_
#define SRC_PERIPHERAL_UART_UART_HPP_

#endif // SRC_PERIPHERY_UART_UART_HPP_
#include <cstdint>
#include <cstddef>

namespace HAL {

class UART {
public:
enum class Instance {
FIRST,
SECOND,
AMOUNT,
};

/**
* @brief Allocate resources, but not initialize yet
*/
explicit UART(UART::Instance instance_) : instance(instance_) {}

/**
* @brief After updating the baudrate, you typically want to reinialize the driver
*/
void set_baudrate(uint32_t rate);

/**
* @brief RX DMA mode
* @return 0 on success, -1 on failure
*/
int8_t init_rx_dma(uint8_t buffer[], uint16_t size);

/**
* @return a value in range [0, RX_DMA_BUFFER_SIZE - 1]
*/
size_t get_last_received_index();

private:
Instance instance;
};

} // namespace HAL

#endif // SRC_PERIPHERAL_UART_UART_HPP_
103 changes: 103 additions & 0 deletions Src/peripheral/uart/uart_stm32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#include "peripheral/uart/uart.hpp"
#include "main.h"

extern UART_HandleTypeDef huart1;
#define UART_1_PTR &huart1

#if defined(SECOND_UART)
extern UART_HandleTypeDef huart2;
#define UART_2_PTR &huart2
#else
#define UART_2_PTR NULL
#endif

typedef enum {
NO_FLAGS = 0,
HALF_RECEIVED_FLAG,
FULL_RECEIVED_FLAG,
BOTH_FLAGS,
} UartRxStatus_t;

typedef struct {
UART_HandleTypeDef* huart_ptr;
uint8_t* buffer;
uint16_t size;
UartRxStatus_t status;
void (*rx_callback)();
uint32_t rx_counter;
} UartRxConfig_t;

static UartRxConfig_t uart_rx[2] = {
{
.huart_ptr = UART_1_PTR,
.buffer = nullptr,
.size = 0,
.status = NO_FLAGS,
.rx_callback = nullptr,
.rx_counter = 0
},
{
.huart_ptr = UART_2_PTR,
.buffer = nullptr,
.size = 0,
.status = NO_FLAGS,
.rx_callback = nullptr,
.rx_counter = 0
},
};

namespace HAL {

void UART::set_baudrate(uint32_t rate) {
if (instance == Instance::FIRST) {
huart1.Init.BaudRate = rate;
HAL_UART_Init(&huart1);
} else if (instance == Instance::SECOND) {
#if defined(SECOND_UART)
huart2.Init.BaudRate = rate;
HAL_HalfDuplex_Init(&huart2);
#endif
}
}


int8_t UART::init_rx_dma(uint8_t buffer[], uint16_t size) {
if (instance >= Instance::AMOUNT) {
return -1;
}

auto& uart_rx_config = uart_rx[static_cast<uint8_t>(instance)];

uart_rx_config.buffer = buffer;
uart_rx_config.size = size;
uart_rx_config.rx_counter = 0;
HAL_StatusTypeDef status = HAL_UART_Receive_DMA(uart_rx_config.huart_ptr, buffer, size);
return status == HAL_OK ? 0 : -1;
}


size_t UART:: get_last_received_index() {
if (instance >= Instance::AMOUNT) {
return 0;
}

auto uart_rx_config = &uart_rx[static_cast<uint8_t>(instance)];

if (uart_rx_config->huart_ptr == nullptr) {
return 0;
}

uint16_t remaining_data_units = __HAL_DMA_GET_COUNTER(uart_rx_config->huart_ptr->hdmarx);
if (remaining_data_units == uart_rx_config->size) {
return uart_rx_config->size - 1;
}
return uart_rx_config->size - remaining_data_units - 1;
}

} // namespace HAL
26 changes: 26 additions & 0 deletions Src/peripheral/uart/uart_ubuntu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
*/

#include "peripheral/uart/uart.hpp"
#include "main.h"

namespace HAL {

void UART::set_baudrate(uint32_t rate) {
(void)rate;
}

int8_t UART::init_rx_dma(uint8_t buffer[], uint16_t size) {
(void)buffer;
(void)size;
return 0;
}

size_t UART:: get_last_received_index() {
return 0;
}

} // namespace HAL
2 changes: 1 addition & 1 deletion Src/platform/stm32f103/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
set(EXECUTABLE ${PROJECT_NAME}.out)
add_executable(${EXECUTABLE}
${APPLICATION_SOURCES}
${PERIPHERAL_SOURCES}
${BUILD_SRC_DIR}/params.cpp
${ROOT_DIR}/Src/common/algorithms.cpp
${ROOT_DIR}/Src/common/logging.cpp
Expand All @@ -15,7 +16,6 @@ add_executable(${EXECUTABLE}
${ROOT_DIR}/Src/platform/stm32/pwm/pwm.cpp
${CMAKE_CURRENT_LIST_DIR}/pwm.cpp
${ROOT_DIR}/Src/platform/stm32/iwdg/iwdg.cpp
${CMAKE_CURRENT_LIST_DIR}/led.cpp
${CMAKE_CURRENT_LIST_DIR}/temperature_sensor.cpp
${ROOT_DIR}/Src/platform/stm32/platform_specific.cpp

Expand Down
5 changes: 3 additions & 2 deletions Src/platform/stm32g0b1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ cmake_path(GET CMAKE_CURRENT_LIST_DIR PARENT_PATH PLATFORM_DIR)
cmake_path(GET PLATFORM_DIR PARENT_PATH SRC_DIR)
cmake_path(GET SRC_DIR PARENT_PATH ROOT_DIR)

include(${ROOT_DIR}/Src/peripheral/spi/CMakeLists.txt)

set(EXECUTABLE ${PROJECT_NAME}.out)
add_executable(${EXECUTABLE}
${APPLICATION_SOURCES}
${PERIPHERAL_SOURCES}
${BUILD_SRC_DIR}/params.cpp
${ROOT_DIR}/Src/common/algorithms.cpp
${ROOT_DIR}/Src/common/logging.cpp
Expand All @@ -20,9 +23,7 @@ add_executable(${EXECUTABLE}
${PLATFORM_DIR}/stm32f103/adc.cpp
${CMAKE_CURRENT_LIST_DIR}/gpio.cpp
${PLATFORM_DIR}/stm32g0b1/pwm.cpp
${PLATFORM_DIR}/stm32g0b1/spi.cpp
${ROOT_DIR}/Src/platform/stm32/iwdg/iwdg.cpp
${PLATFORM_DIR}/stm32f103/led.cpp
${PLATFORM_DIR}/stm32f103/temperature_sensor.cpp
${ROOT_DIR}/Src/platform/stm32/platform_specific.cpp

Expand Down
Loading