Skip to content

Commit

Permalink
replaced pwm cube project with c++ project
Browse files Browse the repository at this point in the history
  • Loading branch information
wisabel0 committed Sep 19, 2024
1 parent 4f2e142 commit af6a43b
Show file tree
Hide file tree
Showing 22 changed files with 15,577 additions and 488 deletions.
31 changes: 10 additions & 21 deletions docs/getting-started/starter/servo/part1-pwm.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ so please reach out if you ever get stuck!
## Intro

As mentioned earlier, by the end of the project you should be able to drive a servo.
This project will also teach some coding practices used for STM32 code.
This project will also teach some coding practices used for STM32 code. Similar to our real code, this starter project will be in C++.
While you are working through the project, keep the following in mind:

* How could you decrease the amount of space or processing power needed for your code?
* How could you document your code so that others can easily read and understand it?
* How could you write your code so that it can easily be adjusted for different pins, different number of servos, etc.?

Expand Down Expand Up @@ -75,10 +74,10 @@ calculating these values.
### 3. Creating the header file

Having a Servo object will make it easier to adjust the number servos or where the servos are
in the future, so for good practice, we will create a servo object and function prototypes in a
header file.
in the future, so for good practice, we will create a servo class and declare any member variables
and member functions in a header file.

On the menu to the left, in `Core`→`Inc`, create a new header file named `servo.h`.
On the menu to the left, in `Core`→`Inc`, create a new header file named `servo.hpp`.

![creating a header file in CubeIDE](create_header.webp)

Expand All @@ -91,32 +90,22 @@ Near the top of the header file, copy in the following lines:

#include <stdlib.h>

#include "stm32f3xx_hal.h"
#include "stm32g4xx_hal.h"
```

Then, create a Servo struct with 3 member variables:
Then, create a Servo class with 3 member variables:

* `TIM_HandleTypeDef *timer` : this tells the STM which timer is being used to generate the PWM signal
* `uint32_t channel` : this tells the STM which channel is being used for the PWM signal
* `uint32_t *output` : this is the address of the output register where the number of ticks that are set to high is stored

To avoid getting errors when using your Servo struct later on, make sure to declare it like this:
```
typedef struct {
// TODO: fill this in
} Servo;
```

Create the function prototypes for the 3 functions that are needed to create and use a Servo object:
* `new_servo`
* `initialize_servo`
* `set_servo_angle`

Create the function prototypes for the 3 functions that are needed to create and use a Servo object:
* A default constructor that does not take in any parameters
* A constructor that initializes the servo with a timer, sets the channel, and
* `set_servo_angle` that converts an angle to the number of ticks for the CCR

Try to fill out the header on your own, but here are some hints if you get stuck:
* `new_servo` should take in parameters and return a Servo*
* `initialize_servo` should initialize an existing Servo* to have a certain starting angle, it should also start the timer
* `set_servo_angle` should convert an angle to the number of ticks for the CCR

### 4. Implementing Servo functions

Expand Down
420 changes: 214 additions & 206 deletions starter-projects/servo/p1-pwm/.cproject

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions starter-projects/servo/p1-pwm/.mxproject

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions starter-projects/servo/p1-pwm/.project
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>servo-p1-pwm</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.st.stm32cube.ide.mcu.MCUProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUCubeProjectNature</nature>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUCubeIdeServicesRevAev2ProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUAdvancedStructureProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUSingleCpuProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCURootProjectNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>servo-p1-pwm-cpp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.st.stm32cube.ide.mcu.MCUProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUCubeProjectNature</nature>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUCubeIdeServicesRevAev2ProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUAdvancedStructureProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCUSingleCpuProjectNature</nature>
<nature>com.st.stm32cube.ide.mcu.MCURootProjectNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
10 changes: 2 additions & 8 deletions starter-projects/servo/p1-pwm/Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extern "C" {

/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"
#include "stm32g4xx_nucleo.h"
#include <stdio.h>

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
Expand Down Expand Up @@ -57,8 +59,6 @@ void Error_Handler(void);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define B1_Pin GPIO_PIN_13
#define B1_GPIO_Port GPIOC
#define RCC_OSC32_OUT_Pin GPIO_PIN_14
#define RCC_OSC32_OUT_GPIO_Port GPIOC
#define RCC_OSC32_OUTC15_Pin GPIO_PIN_15
Expand All @@ -67,12 +67,6 @@ void Error_Handler(void);
#define RCC_OSC_IN_GPIO_Port GPIOF
#define RCC_OSC_OUT_Pin GPIO_PIN_1
#define RCC_OSC_OUT_GPIO_Port GPIOF
#define LPUART1_TX_Pin GPIO_PIN_2
#define LPUART1_TX_GPIO_Port GPIOA
#define LPUART1_RX_Pin GPIO_PIN_3
#define LPUART1_RX_GPIO_Port GPIOA
#define LD2_Pin GPIO_PIN_5
#define LD2_GPIO_Port GPIOA
#define T_SWDIO_Pin GPIO_PIN_13
#define T_SWDIO_GPIO_Port GPIOA
#define T_SWCLK_Pin GPIO_PIN_14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
/*#define HAL_SPI_MODULE_ENABLED */
/*#define HAL_SRAM_MODULE_ENABLED */
/*#define HAL_TIM_MODULE_ENABLED */
/*#define HAL_UART_MODULE_ENABLED */
#define HAL_UART_MODULE_ENABLED
/*#define HAL_USART_MODULE_ENABLED */
/*#define HAL_WWDG_MODULE_ENABLED */
#define HAL_GPIO_MODULE_ENABLED
Expand Down
1 change: 1 addition & 0 deletions starter-projects/servo/p1-pwm/Core/Inc/stm32g4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void EXTI15_10_IRQHandler(void);
/* USER CODE BEGIN EFP */

/* USER CODE END EFP */
Expand Down
78 changes: 78 additions & 0 deletions starter-projects/servo/p1-pwm/Core/Inc/stm32g4xx_nucleo_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

/**
******************************************************************************
* @file stm32g4xx_nucleo_conf.h
* @author MCD Application Team
* @brief STM32G4xx_Nucleo board configuration file.
* This file should be copied to the application folder and renamed
* to stm32g4xx_nucleo_conf.h
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef STM32G4XX_NUCLEO_CONF_H
#define STM32G4XX_NUCLEO_CONF_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"

/** @addtogroup BSP
* @{
*/

/** @addtogroup STM32G4XX_NUCLEO
* @{
*/

/** @defgroup STM32G4XX_NUCLEO_CONFIG Config
* @{
*/

/** @defgroup STM32C0XX_NUCLEO_CONFIG_Exported_Constants Exported Constants
* @{
*/
/* Nucleo pin and part number defines */
#define USE_STM32G4XX_NUCLEO

/* COM define */
#define USE_COM_LOG 1U
#define USE_BSP_COM_FEATURE 1U

/* IRQ priorities */
#define BSP_BUTTON_USER_IT_PRIORITY 15U

/**
* @}
*/

/**
* @}
*/

/**
* @}
*/

/**
* @}
*/

#ifdef __cplusplus
}
#endif

#endif /* STM32G4XX_NUCLEO_CONF_H */
53 changes: 24 additions & 29 deletions starter-projects/servo/p1-pwm/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

/* Private variables ---------------------------------------------------------*/

COM_InitTypeDef BspCOMInit;

/* USER CODE BEGIN PV */

/* USER CODE END PV */
Expand Down Expand Up @@ -90,10 +92,28 @@ int main(void)

/* USER CODE END 2 */

/* Initialize leds */
BSP_LED_Init(LED_GREEN);

/* Initialize USER push-button, will be used to trigger an interrupt each time it's pressed.*/
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

/* Initialize COM1 port (115200, 8 bits (7-bit data + 1 stop bit), no parity */
BspCOMInit.BaudRate = 115200;
BspCOMInit.WordLength = COM_WORDLENGTH_8B;
BspCOMInit.StopBits = COM_STOPBITS_1;
BspCOMInit.Parity = COM_PARITY_NONE;
BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;
if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE)
{
Error_Handler();
}

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
Expand All @@ -112,7 +132,7 @@ void SystemClock_Config(void)

/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);

/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
Expand All @@ -122,8 +142,8 @@ void SystemClock_Config(void)
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
RCC_OscInitStruct.PLL.PLLN = 9;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
RCC_OscInitStruct.PLL.PLLN = 85;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
Expand All @@ -141,7 +161,7 @@ void SystemClock_Config(void)
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
Expand All @@ -154,7 +174,6 @@ void SystemClock_Config(void)
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

Expand All @@ -164,30 +183,6 @@ static void MX_GPIO_Init(void)
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pins : LPUART1_TX_Pin LPUART1_RX_Pin */
GPIO_InitStruct.Pin = LPUART1_TX_Pin|LPUART1_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF12_LPUART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pin : LD2_Pin */
GPIO_InitStruct.Pin = LD2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
Expand Down
14 changes: 14 additions & 0 deletions starter-projects/servo/p1-pwm/Core/Src/stm32g4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32g4xx.s). */
/******************************************************************************/

/**
* @brief This function handles EXTI line[15:10] interrupts.
*/
void EXTI15_10_IRQHandler(void)
{
/* USER CODE BEGIN EXTI15_10_IRQn 0 */

/* USER CODE END EXTI15_10_IRQn 0 */
BSP_PB_IRQHandler(BUTTON_USER);
/* USER CODE BEGIN EXTI15_10_IRQn 1 */

/* USER CODE END EXTI15_10_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
Loading

0 comments on commit af6a43b

Please sign in to comment.