Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Migra o template para CPP #46

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
"editor.tabSize": 4,
"editor.detectIndentation": false,
"editor.insertSpaces": true,

"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.eol": "\n",
"files.encoding": "utf8",
"editor.defaultFormatter": "zachflower.uncrustify",
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",

"[c]": {
"editor.formatOnSave": true
},

"[markdown]": {
"files.trimTrailingWhitespace": false,
"files.trimFinalNewlines": false
Expand Down
33 changes: 18 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)

# Cube file name without .ioc extension
project(stm32_project_template C ASM)
project(stm32_project_template C CXX ASM)

# Set the board version to an empty string if your board doesn't have a version
set(BOARD_VERSION "")
Expand Down Expand Up @@ -112,7 +112,7 @@ set(LIB_INCLUDE_DIRECTORIES
## Include directories
###############################################################################

set(C_INCLUDE_DIRECTORIES
set(PROJECT_INCLUDE_DIRECTORIES
./inc
./cube/Inc
)
Expand All @@ -125,12 +125,12 @@ set(TEST_INCLUDE_DIRECTORIES
## Input files
###############################################################################

file(GLOB_RECURSE C_SOURCES CONFIGURE_DEPENDS "src/*.c")
file(GLOB_RECURSE C_HEADERS CONFIGURE_DEPENDS "inc/*.h")
file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "src/*.cpp" "src/*.c")
file(GLOB_RECURSE PROJECT_HEADERS CONFIGURE_DEPENDS "inc/*.hpp" "inc/*.h")

file(GLOB_RECURSE TESTS_SOURCES CONFIGURE_DEPENDS "tests/src/*.c")
file(GLOB_RECURSE TESTS_HEADERS CONFIGURE_DEPENDS "tests/inc/*.h")
file(GLOB_RECURSE TESTS_BIN CONFIGURE_DEPENDS "tests/bin/*.c")
file(GLOB_RECURSE TESTS_SOURCES CONFIGURE_DEPENDS "tests/src/*.cpp" "tests/src/*.c")
file(GLOB_RECURSE TESTS_HEADERS CONFIGURE_DEPENDS "tests/inc/*.hpp" "tests/inc/*.h")
file(GLOB_RECURSE TESTS_BIN CONFIGURE_DEPENDS "tests/bin/*.cpp" "tests/bin/*.c")

file(GLOB_RECURSE CUBE_SOURCES CONFIGURE_DEPENDS "cube/Src/*.c")

Expand All @@ -142,20 +142,20 @@ list(REMOVE_ITEM CUBE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cube/Src/system_${DEVI
set(FORCED_INCLUDE_HEADERS
)

targets_generate_format_target(C_SOURCES C_HEADERS TESTS_SOURCES TESTS_HEADERS TESTS_BIN)
targets_generate_format_target(PROJECT_SOURCES PROJECT_HEADERS TESTS_SOURCES TESTS_HEADERS TESTS_BIN)

###############################################################################
## Main executable target
###############################################################################

add_executable(${PROJECT_NAME}
${CUBE_SOURCES}
${C_SOURCES}
${PROJECT_SOURCES}
${LIB_SOURCES}
)

target_include_directories(${PROJECT_NAME} PUBLIC
${C_INCLUDE_DIRECTORIES}
${PROJECT_INCLUDE_DIRECTORIES}
${LIB_INCLUDE_DIRECTORIES}
${CMSIS_INCLUDE_DIRS}
${HAL_INCLUDE_DIRS}
Expand Down Expand Up @@ -184,24 +184,27 @@ targets_generate_helpme_target()
## Generate test executables
###############################################################################

# Since each test has its own main function, we don't need the main.c from the project
list(REMOVE_ITEM C_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c)
# Since each test has its own main function, we don't need the main.cpp from the project
list(REMOVE_ITEM PROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
)

foreach(TEST_FILE ${TESTS_BIN})

# If TEST_FILE contains /dir1/dir2/file.c, TEST_NAME will be 'file'
# If TEST_FILE contains /dir1/dir2/file.cpp, TEST_NAME will be 'file'
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WLE)

add_executable(${TEST_NAME} EXCLUDE_FROM_ALL
${TEST_FILE}
${CUBE_SOURCES}
${C_SOURCES}
${PROJECT_SOURCES}
${TESTS_SOURCES}
${LIB_SOURCES}
)

target_include_directories(${TEST_NAME} PUBLIC
${C_INCLUDE_DIRECTORIES}
${PROJECT_INCLUDE_DIRECTORIES}
${LIB_INCLUDE_DIRECTORIES}
${CMSIS_INCLUDE_DIRS}
${HAL_INCLUDE_DIRS}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Para isso é necessário mudar o nome do projeto, o qual deve ter o mesmo do arq

```c
# Cube file name without .ioc extension
project(stm32_project_template C ASM)
project(stm32_project_template C CXX ASM)
```

> Os argumentos `C` e `ASM` estão relacionados ao tipo de linguagem que o projeto utiliza (C e Assembly).
Expand Down
4 changes: 2 additions & 2 deletions cube/stm32_project_template.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Mcu.PinsNb=14
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32F303RETx
MxCube.Version=6.9.0
MxDb.Version=DB.6.0.90
MxCube.Version=6.10.0
MxDb.Version=DB.6.0.100
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:false\:true\:false\:false
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:false\:true\:false\:false
NVIC.ForceEnableDMAVector=true
Expand Down
39 changes: 0 additions & 39 deletions inc/mcu.h

This file was deleted.

41 changes: 41 additions & 0 deletions inc/mcu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @file mcu.hpp
*
* @brief MCU related
*/

#ifndef __MCU_HPP__
#define __MCU_HPP__

#include <stdint.h>

/*****************************************
* Public Function Prototypes
*****************************************/

extern "C"
Eduardo-Barreto marked this conversation as resolved.
Show resolved Hide resolved
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O uncrustify deixa essa chave aqui mesmo? Estranho ahushuashuashu

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aisduhas de fato estranho, mas o make format nao mudou isso aí

/**
* @brief Initializes System Clock.
* @note Defined by cube.
*/
void SystemClock_Config(void);
}

namespace hal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Então, não sei sobre deixar esse namespace hal aqui, acho que pode deixar mais simples pro template. Não é porque existem namespaces que a gente tem que colocar vários, tem que saber dosar. Mas assim batata também.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, na minha cabeça fazia sentido (principalmente considerando as libs), só botei pq nao vi pq nao botar sinceramente

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acho que eu gosto de um namespace hal, ele ajuda a segmentar o que é baixo do que é alto. Se algo tem escrito hal:: antes, a gente sabe que é uma abstração de algo bem baixo nível, coisa de função da ST.

class mcu {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tenho alguns mixed feeling sobre deixar isso como classe no template. Tipo não é porque suporta classe que a gente tem que usar classe em tudo. O sleep e o init até fazem sentindo estarem numa classe assim, tanto que eles já tinham o prefixo do mcu_, mas aí o led toggle deixa meio estranho. Como não tem muito também onde colocar, batata também ahushuauahas.

Esse e o comentário do namespace são meio batatas, é só pra pensarem quando tiverem as coisas de usar com moderação as features da linguagem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o sleep e o init foram por esse motivo q vc citou, o led_toggle eu imaginei que se referia ao led da placa asudhasiudh no caso da bluepill é o C13, mas no código estar o A5 deixa mais esquisito mesmo asduihasiu

Copy link
Collaborator

@pedroazeredo04 pedroazeredo04 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Também tenho mixed feelings mas acho que tem que ficar como classe sim. Na minha opinião, numa alternativa sem classe, ficaria meeeio estranho de acessar, ficaria algo do tipo hal::sleep(50), daí ainda mais com RTOS, é um sleep do HAL? É um Sleep do RTOS? Sei lá asdhuadhuasd acho que hal::mcu::sleep(50) é mais bonito. Posteriormente poderíamos ter um hal::rtos::sleep(50) ou sei lá hal::rtos::publish(mensagem), enfim, daí vai, mas é tudo hal né

public:
/**
* @brief Initializes MCU and some peripherals.
*/
static void init(void);

/**
* @brief Put the MCU to sleep.
*
* @param ms Sleep time in milliseconds
*/
static void sleep(uint32_t ms);
};
};
#endif // __MCU_HPP__
11 changes: 5 additions & 6 deletions src/main.c → src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
/**
* @file main.c
* @file main.cpp
*
* @brief Main function
*/

#include "mcu.h"
#include "mcu.hpp"

/*****************************************
* Private Constant Definitions
*****************************************/

#define LED_TOGGLE_DELAY_MS 1500
static constexpr uint16_t led_toggle_delay_ms = 1500;

/*****************************************
* Main Function
*****************************************/

int main(void) {
mcu_init();
hal::mcu::init();

for (;;) {
led_toggle();
mcu_sleep(LED_TOGGLE_DELAY_MS);
hal::mcu::sleep(led_toggle_delay_ms);
}
}
12 changes: 5 additions & 7 deletions src/mcu.c → src/mcu.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @file mcu.c
* @file mcu.cpp
*
* @brief MCU related
*/

#include <stdint.h>

#include "mcu.h"
#include "mcu.hpp"

#include "gpio.h"
#include "main.h"
Eduardo-Barreto marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -15,18 +15,16 @@
* Public Function Body Definitions
*****************************************/

void mcu_init(void) {
namespace hal {
void mcu::init(void) {
HAL_Init();

SystemClock_Config();

MX_GPIO_Init();
}

void mcu_sleep(uint32_t ms) {
void mcu::sleep(uint32_t ms) {
HAL_Delay(ms);
}

void led_toggle(void) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
Eduardo-Barreto marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions tests/bin/test_main.c → tests/bin/test_main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @file test_main.c
* @file test_main.cpp
*
* @brief Main function for tests.
*/

#include <stdio.h>
#include "tests_core.h"
#include "test_core.hpp"

int main(void) {
test_core_init();
Expand Down
2 changes: 1 addition & 1 deletion tests/inc/test_core.h → tests/inc/test_core.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file test_core.h
* @file test_core.hpp
*
* @brief Core functions to the test
*
Expand Down
8 changes: 4 additions & 4 deletions tests/src/test_core.c → tests/src/test_core.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file test_core.c
* @file test_core.cpp
*
* @brief Core functions to the test
*
Expand All @@ -9,13 +9,13 @@
*
*/

#include "test_core.h"
#include "mcu.h"
#include "test_core.hpp"
#include "mcu.hpp"

/*****************************************
* Public Functions Bodies Definitions
*****************************************/

void test_core_init(void) {
mcu_init();
hal::mcu::init();
}