Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- geryyy
- Date:
- 2018-06-19
- Revision:
- 1:5bceb51fde85
- Parent:
- 0:1df541c54e25
- Child:
- 2:7d25b12db2bc
File content as of revision 1:5bceb51fde85:
#include "mbed.h"
#include "stm32f7xx_hal.h"
#define TIMx TIM3
#define TIMx_CLK_ENABLE() __TIM3_CLK_ENABLE()
/* Definition for TIMx Channel Pins */
#define TIMx_CHANNEL_GPIO_PORT() __HAL_RCC_GPIOB_CLK_ENABLE();
#define TIMx_GPIO_PORT_CHANNEL1 GPIOB
#define TIMx_GPIO_PORT_CHANNEL2 GPIOB
#define TIMx_GPIO_PORT_CHANNEL3 GPIOB
#define TIMx_GPIO_PORT_CHANNEL4 GPIOB
#define TIMx_GPIO_PIN_CHANNEL1 GPIO_PIN_4
#define TIMx_GPIO_PIN_CHANNEL2 GPIO_PIN_5
#define TIMx_GPIO_AF_CHANNEL1 GPIO_AF2_TIM3
#define TIMx_GPIO_AF_CHANNEL2 GPIO_AF2_TIM3
TIM_HandleTypeDef TimHandle;
/* Prescaler value declartion*/
uint32_t uhPrescalerValue = 0;
TIM_OC_InitTypeDef sConfig;
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim);
void Error_Handler(char *file, int line);
DigitalOut myled(LED1);
int main() {
printf("HV Pulsgenerator V0.1\nGerald Ebmer 2018\n\n");
uhPrescalerValue = (uint32_t)(((SystemCoreClock/ 2) / 10000000) - 1);
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = uhPrescalerValue;
TimHandle.Init.Period = 1000;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler(__FILE__, __LINE__);
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
/* Set the pulse value for channel 1 */
sConfig.Pulse = 100;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler(__FILE__, __LINE__);
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = 900;
sConfig.OCPolarity = TIM_OCPOLARITY_LOW;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler(__FILE__, __LINE__);
}
/*##-3- Start PWM signals generation #######################################*/
/* Start channel 1 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler(__FILE__, __LINE__);
}
/* Start channel 2 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler(__FILE__, __LINE__);
}
while(1) {
myled = !myled;
wait(1);
}
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* TIMx Peripheral clock enable */
TIMx_CLK_ENABLE();
/* Enable all GPIO Channels Clock requested */
TIMx_CHANNEL_GPIO_PORT();
/* Configure PB.04 (pin 19 in CN7 connector) (TIM3_Channel1), PB.05 (pin 13 in CN7 connector) (TIM3_Channel2), PB.00 (pin 31 in CN10 connector) (TIM3_Channel3),
PB.01 (pin 7 in CN10 connector) (TIM3_Channel4) in output, push-pull, alternate function mode
*/
/* Common configuration for all channels */
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = TIMx_GPIO_AF_CHANNEL1;
GPIO_InitStruct.Pin = TIMx_GPIO_PIN_CHANNEL1;
HAL_GPIO_Init(TIMx_GPIO_PORT_CHANNEL1, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = TIMx_GPIO_AF_CHANNEL2;
GPIO_InitStruct.Pin = TIMx_GPIO_PIN_CHANNEL2;
HAL_GPIO_Init(TIMx_GPIO_PORT_CHANNEL2, &GPIO_InitStruct);
}
/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void Error_Handler(char *file, int line)
{
fprintf(stderr, "Error in file %s linenr %d!\n",file,line);
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}