Page 2 of 2

Re: Cannot generate the PWM signal to the breakout board of the STM32F405 microcontroller

Posted: Mon Jun 19, 2017 12:15 pm
by tobias
Great! Would be nice to know what you fixed to get it to work.

Re: Cannot generate the PWM signal to the breakout board of the STM32F405 microcontroller

Posted: Mon Jun 19, 2017 12:55 pm
by thanhvu94
Here is the code I use:

Code: Select all

#include "deck.h"
#include "debug.h"

/* ST includes */
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"

/*Predefined macros*/
#define IR_TX_CARRIER_FREQ			56000
#define IR_TX_CARRIER_PWM_PERIOD	(SystemCoreClock/IR_TX_CARRIER_FREQ)
#define IR_TX_DELAY_PRESCALER		(84-1)
#define ENABLE 		1
#define IR_TX_CARRIER_TIMER                   TIM10
#define IR_TX_CARRIER_TIMER_RCC               RCC_APB2Periph_TIM10
#define IR_TX_CARRIER_TIMER_CH_Init           TIM_OC1Init
#define IR_TX_CARRIER_TIMER_CH_PreloadConfig  TIM_OC1PreloadConfig
#define IR_TX_CARRIER_TIMER_CH_SetCompare     TIM_SetCompare1
#define IR_TX_CARRIER_RCC                     RCC_AHB1Periph_GPIOB
#define IR_TX_CARRIER_PORT                    GPIOB
#define IR_TX_CARRIER_PIN                     GPIO_Pin_8

void pwmInit(DeckInfo *info)
{
	  DEBUG_PRINT("PWM Inited.\n");
	  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	  TIM_OCInitTypeDef  TIM_OCInitStructure;
	  GPIO_InitTypeDef GPIO_InitStructure;

	  RCC_AHB1PeriphClockCmd(0x00000002, ENABLE);
	  RCC_APB2PeriphClockCmd(0x00020000, ENABLE);

	  // Configure the GPIO for the timer output
	  GPIO_StructInit(&GPIO_InitStructure);
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	  GPIO_InitStructure.GPIO_Pin = 0x0100;
	  GPIO_Init(((GPIO_TypeDef *) ((0x40000000 + 0x00020000) + 0x0400)), &GPIO_InitStructure);

	  GPIO_PinAFConfig(((GPIO_TypeDef *) ((0x40000000 + 0x00020000) + 0x0400)), 0x08, 0x03);

	  // Time base configuration
	  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
	  TIM_TimeBaseStructure.TIM_Period = 9881;
	  TIM_TimeBaseStructure.TIM_Prescaler = 0;
	  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	  TIM_TimeBaseStructure.TIM_CounterMode = (0x0000);
	  TIM_TimeBaseInit(((TIM_TypeDef *) ((0x40000000 + 0x00010000) + 0x4400)), &TIM_TimeBaseStructure);

	  // PWM channel configuration
	  TIM_OCStructInit(&TIM_OCInitStructure);
	  TIM_OCInitStructure.TIM_OCMode = 0x0060;
	  TIM_OCInitStructure.TIM_OutputState = 0x0001;
	  TIM_OCInitStructure.TIM_Pulse = 0;
	  TIM_OCInitStructure.TIM_OCPolarity = 0x0000;
	  //TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;

	  // Configure Output Compare for PWM
	  IR_TX_CARRIER_TIMER_CH_Init(((TIM_TypeDef *) ((0x40000000 + 0x00010000) + 0x4400)), &TIM_OCInitStructure);
	  IR_TX_CARRIER_TIMER_CH_PreloadConfig(((TIM_TypeDef *) ((0x40000000 + 0x00010000) + 0x4400)), 0x0008);

	  TIM_ARRPreloadConfig(((TIM_TypeDef *) ((0x40000000 + 0x00010000) + 0x4400)), 1);
	  TIM_Cmd(((TIM_TypeDef *) ((0x40000000 + 0x00010000) + 0x4400)), ENABLE);

	  ((TIM_TypeDef *) ((0x40000000 + 0x00010000) + 0x4400))->CCR1 = 50*(9881/100);

}

bool pwmTest()
{
	DEBUG_PRINT("PWM initlized successfully. \n");
	return 1;
}

const DeckDriver pwm_ir = {
	.vid = 0,
	.pid = 0,
	//.usedPeriph = DECK_USING_TIMER10,
	//.usedGpio = DECK_USING_IO_1,
	.name = "myPWM",
	.init = pwmInit,
	.test = pwmTest,
};

DECK_DRIVER(pwm_ir);
What I have changed is:
- I don't know why the macros or the names like TIM10, GPIOB, GPIO_Pin_8 .... have the error "Symbol .... could not be resolved, return 0" so I replace them all with the address in the libraries. Maybe some problems of Eclipse. This will remove all the bug signs in some lines as I posted at the beginning of the thread.
- I just deleted the old Virtual Machine Crazyflie, readded a new VM, clone all the projects and add my PWM deck in the project like the tutorials. The compilation and flashing work well then. I don't know why I can only flash once. If I want to reflash again, i have to delete the VM and readd again.

Re: [SOLVED] Cannot generate the PWM signal to the breakout board of the STM32F405 microcontroller

Posted: Mon Jun 19, 2017 1:10 pm
by thanhvu94
I aslo think it would be nice if you can list all the name of the free peripherals that still can be used for extension. By this way, we will not touch the peripherals that are already used for the basic function of the Crazyflie.

Re: [SOLVED] Cannot generate the PWM signal to the breakout board of the STM32F405 microcontroller

Posted: Tue Jun 20, 2017 7:11 am
by tobias
I think eclipse is not configured the right way that is why you get the warnings from the macros. I would strongly advice to use the macros anyway as the code becomes hard to read without them.
By this way, we will not touch the peripherals that are already used for the basic function of the Crazyflie.
Yes I agree but something that might be hard to maintain and keep updated. Could be worth a try though. I tend to use the search function to find what is used. So if I what to know what timers are used I search for "TIM" to get all timers in the code.

Re: [SOLVED] Cannot generate the PWM signal to the breakout board of the STM32F405 microcontroller

Posted: Sun Jun 03, 2018 8:05 pm
by Amir_94
In your code, can you make the PWM stop, go slower or faster? Sorry I had no clue how to interpret this code(seems so complicated)?

Thanks

Re: [SOLVED] Cannot generate the PWM signal to the breakout board of the STM32F405 microcontroller

Posted: Mon Jun 04, 2018 10:37 am
by arnaud
In the code copy-pasted above IR_TX_CARRIER_PWM_PERIOD and IR_TX_DELAY_PRESCALER should set the period of the PWM. These are pretty low level controls so I suggest you look at the STM32F405 reference manual to familiarise yourself with the timer you plan to use.