Page 1 of 2
STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sat Nov 01, 2014 4:17 pm
by bartocham
Hi!
I'm playing with STM32VL Discovery board. I want to get PWM signal on both - normal and complementary output.
I found out that TIM1 is PWM timer with complementary outputs and that TIM1_CH1 is connected as alternate function to PA8 and TIM1_CH1N is also connected as alternate to PB13.
I'm newbie so I'm scarred as hell about destroying my board so correct if I'm wrong but I think my code should look like:
part of board.h:
Code: Select all
/*
* Port A setup.
* Everything input with pull-up except:
* PA0 - Normal input (BUTTON).
* PA8 - Alternate Push Pull output 50MHz (TIM1_CH1)
*/
#define VAL_GPIOACRL 0x88888884 /* PA7...PA0 */
#define VAL_GPIOACRH 0x8888888B /* PA15...PA8 */
#define VAL_GPIOAODR 0xFFFFFFFF
/*
* Port B setup.
* Everything input with pull-up except:
* PB13 - Alternate Push Pull output 50MHz (TIM1_CH1N)
*/
#define VAL_GPIOBCRL 0x88888888 /* PB7...PB0 */
#define VAL_GPIOBCRH 0x88B88888 /* PB15...PB8 */
#define VAL_GPIOBODR 0xFFFFFFFF
my main.c:
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
static PWMConfig pwmcfg = {
10000, /* 10Khz PWM clock frequency */
10000, /* PWM period 1 second */
NULL, /* No callback */
/* Only channel 4 enabled */
{
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
},
0
};
int main(void)
{
halInit();
chSysInit();
/* Enables PWM output (of TIM1, channel 1) on LED connected to PA8 */
palSetPadMode(GPIOA, 8, PAL_MODE_ALTERNATE(2));
/* Enables PWM output (of TIM1, channel 1N) on LED connected to PB13 */
palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(2));
pwmStart(&PWMD1, &pwmcfg);
pwmEnableChannel(&PWMD1, 0, 5000);
while (TRUE) {
chThdSleepMilliseconds(5000);
}
}
I would be really appreciated if you could help me with that topic.
Thanks!!
Matthew
Update:
And don't know what's wrong but I get those errors from compiler ;/
Can you also help me with that?

Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sat Nov 01, 2014 8:20 pm
by bartocham
I found bugs in code and in mcu config - I used that one from STM32VL example now my code looks like:
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#include "halconf.h"
#include "pwm.h"
#include "pal.h"
#include "pwm_lld.h"
#include "pal_lld.h"
static PWMConfig pwmcfg = {
10000, /* 10Khz PWM clock frequency */
10000, /* PWM period 1 second */
NULL, /* No callback */
/* Only channel 1 enabled */
{
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
},
0,
0
};
int main(void)
{
halInit();
chSysInit();
/* Enables PWM output (of TIM1, channel 1) on LED connected to PA8 */
palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
/* Enables PWM output (of TIM1, channel 1N) on LED connected to PB13 */
palSetPadMode(GPIOB, 13, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
pwmStart(&PWMD1, &pwmcfg);
pwmEnableChannel(&PWMD1, 0, 5000);
while (TRUE) {
chThdSleepMilliseconds(5000);
}
}
and mcuconf.h
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define STM32F100_MCUCONF
/*
* STM32F103 drivers configuration.
* The following settings override the default settings present in
* the various device driver implementation headers.
* Note that the settings for each driver only have effect if the whole
* driver is enabled in halconf.h.
*
* IRQ priorities:
* 15...0 Lowest...Highest.
*
* DMA priorities:
* 0...3 Lowest...Highest.
*/
/*
* HAL driver system settings.
*/
#define STM32_NO_INIT FALSE
#define STM32_HSI_ENABLED TRUE
#define STM32_LSI_ENABLED FALSE
#define STM32_HSE_ENABLED TRUE
#define STM32_LSE_ENABLED FALSE
#define STM32_SW STM32_SW_PLL
#define STM32_PLLSRC STM32_PLLSRC_HSE
#define STM32_PLLXTPRE STM32_PLLXTPRE_DIV1
#define STM32_PLLMUL_VALUE 3
#define STM32_HPRE STM32_HPRE_DIV1
#define STM32_PPRE1 STM32_PPRE1_DIV1
#define STM32_PPRE2 STM32_PPRE2_DIV1
#define STM32_ADCPRE STM32_ADCPRE_DIV2
#define STM32_MCOSEL STM32_MCOSEL_NOCLOCK
#define STM32_RTCSEL STM32_RTCSEL_HSEDIV
#define STM32_PVD_ENABLE FALSE
#define STM32_PLS STM32_PLS_LEV0
/*
* ADC driver system settings.
*/
#define STM32_ADC_USE_ADC1 TRUE
#define STM32_ADC_ADC1_DMA_PRIORITY 2
#define STM32_ADC_ADC1_IRQ_PRIORITY 6
/*
* EXT driver system settings.
*/
#define STM32_EXT_EXTI0_IRQ_PRIORITY 6
#define STM32_EXT_EXTI1_IRQ_PRIORITY 6
#define STM32_EXT_EXTI2_IRQ_PRIORITY 6
#define STM32_EXT_EXTI3_IRQ_PRIORITY 6
#define STM32_EXT_EXTI4_IRQ_PRIORITY 6
#define STM32_EXT_EXTI5_9_IRQ_PRIORITY 6
#define STM32_EXT_EXTI10_15_IRQ_PRIORITY 6
#define STM32_EXT_EXTI16_IRQ_PRIORITY 6
#define STM32_EXT_EXTI17_IRQ_PRIORITY 6
#define STM32_EXT_EXTI18_IRQ_PRIORITY 6
#define STM32_EXT_EXTI19_IRQ_PRIORITY 6
/*
* GPT driver system settings.
*/
#define STM32_GPT_USE_TIM1 FALSE
#define STM32_GPT_USE_TIM2 FALSE
#define STM32_GPT_USE_TIM3 FALSE
#define STM32_GPT_USE_TIM4 FALSE
#define STM32_GPT_USE_TIM5 FALSE
#define STM32_GPT_USE_TIM8 FALSE
#define STM32_GPT_TIM1_IRQ_PRIORITY 7
#define STM32_GPT_TIM2_IRQ_PRIORITY 7
#define STM32_GPT_TIM3_IRQ_PRIORITY 7
#define STM32_GPT_TIM4_IRQ_PRIORITY 7
#define STM32_GPT_TIM5_IRQ_PRIORITY 7
#define STM32_GPT_TIM8_IRQ_PRIORITY 7
/*
* I2C driver system settings.
*/
#define STM32_I2C_USE_I2C1 FALSE
#define STM32_I2C_USE_I2C2 FALSE
#define STM32_I2C_I2C1_IRQ_PRIORITY 5
#define STM32_I2C_I2C2_IRQ_PRIORITY 5
#define STM32_I2C_I2C1_DMA_PRIORITY 3
#define STM32_I2C_I2C2_DMA_PRIORITY 3
#define STM32_I2C_I2C1_DMA_ERROR_HOOK() chSysHalt()
#define STM32_I2C_I2C2_DMA_ERROR_HOOK() chSysHalt()
/*
* ICU driver system settings.
*/
#define STM32_ICU_USE_TIM1 FALSE
#define STM32_ICU_USE_TIM2 FALSE
#define STM32_ICU_USE_TIM3 FALSE
#define STM32_ICU_USE_TIM4 FALSE
#define STM32_ICU_USE_TIM5 FALSE
#define STM32_ICU_USE_TIM8 FALSE
#define STM32_ICU_TIM1_IRQ_PRIORITY 7
#define STM32_ICU_TIM2_IRQ_PRIORITY 7
#define STM32_ICU_TIM3_IRQ_PRIORITY 7
#define STM32_ICU_TIM4_IRQ_PRIORITY 7
#define STM32_ICU_TIM5_IRQ_PRIORITY 7
#define STM32_ICU_TIM8_IRQ_PRIORITY 7
/*
* PWM driver system settings.
*/
#define STM32_PWM_USE_ADVANCED FALSE
#define STM32_PWM_USE_TIM1 TRUE
#define STM32_PWM_USE_TIM2 FALSE
#define STM32_PWM_USE_TIM3 FALSE
#define STM32_PWM_USE_TIM4 FALSE
#define STM32_PWM_USE_TIM5 FALSE
#define STM32_PWM_USE_TIM8 FALSE
#define STM32_PWM_TIM1_IRQ_PRIORITY 7
#define STM32_PWM_TIM2_IRQ_PRIORITY 7
#define STM32_PWM_TIM3_IRQ_PRIORITY 7
#define STM32_PWM_TIM4_IRQ_PRIORITY 7
#define STM32_PWM_TIM5_IRQ_PRIORITY 7
#define STM32_PWM_TIM8_IRQ_PRIORITY 7
/*
* RTC driver system settings.
*/
#define STM32_RTC_IRQ_PRIORITY 15
/*
* SERIAL driver system settings.
*/
#define STM32_SERIAL_USE_USART1 TRUE
#define STM32_SERIAL_USE_USART2 FALSE
#define STM32_SERIAL_USE_USART3 FALSE
#define STM32_SERIAL_USART1_PRIORITY 12
#define STM32_SERIAL_USART2_PRIORITY 12
#define STM32_SERIAL_USART3_PRIORITY 12
/*
* SPI driver system settings.
*/
#define STM32_SPI_USE_SPI1 TRUE
#define STM32_SPI_USE_SPI2 FALSE
#define STM32_SPI_SPI1_DMA_PRIORITY 1
#define STM32_SPI_SPI2_DMA_PRIORITY 1
#define STM32_SPI_SPI1_IRQ_PRIORITY 10
#define STM32_SPI_SPI2_IRQ_PRIORITY 10
#define STM32_SPI_DMA_ERROR_HOOK(spip) chSysHalt()
/*
* UART driver system settings.
*/
#define STM32_UART_USE_USART1 TRUE
#define STM32_UART_USE_USART2 FALSE
#define STM32_UART_USE_USART3 FALSE
#define STM32_UART_USART1_IRQ_PRIORITY 12
#define STM32_UART_USART2_IRQ_PRIORITY 12
#define STM32_UART_USART3_IRQ_PRIORITY 12
#define STM32_UART_USART1_DMA_PRIORITY 0
#define STM32_UART_USART2_DMA_PRIORITY 0
#define STM32_UART_USART3_DMA_PRIORITY 0
#define STM32_UART_DMA_ERROR_HOOK(uartp) chSysHalt()
Would it now work as it is supposed to be?
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sat Nov 01, 2014 9:44 pm
by Giovanni
Hi,
It looks OK, don't be too worried about the board, it never happened to me to break one by just running a program.
Giovanni
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sun Nov 02, 2014 11:30 am
by bartocham
Hi,
Now I have another problem. I get two LEDs connected via 330Ohm resistors to those PA8 and PB13 - I want to see if I get the PWM.
Compilation is going fine. I've created ST-Link GDB server config as below:





After that I've created debug config as below:




After running ST-Link SWD I get below console code:
Atollic TrueSTUDIO gdbserver for ST-Link. Version 1.9.0 Pro
Copyright 2010-2014, Atollic AB.
Starting server with the following options:
Persistant Mode : Enabled
LogFile Name : debug_log.txt
Logging Level : 31
Listen Port Number : 61234
Status Refresh Delay : 15s
Verbose Mode : Disabled
SWD Debug : Enabled
Connecting to the ST-Link Debugger... OK
Waiting for debugger connection...
After running my debugg configuration I have - execution is suspended because of an error.
For a moment I see some errors in console but the consoled is cleared fast so I have no idea what errors are they.
Some ideas what I have wrong ?
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sun Nov 02, 2014 11:45 am
by Giovanni
What GDB server is that?
Giovanni
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sun Nov 02, 2014 11:48 am
by bartocham
Hi,
It's from
Atollic TrueSTUDIO® for ARM® Lite.
Downloaded from here
http://atollic.com/index.php/download
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sun Nov 02, 2014 12:47 pm
by bartocham
Hi,
I found older version of GDB and now it works fine. I've flashed program.
On TIM1_CH1 - PA8 I get a pwm signal but on TIM1_CH1N I did not. Do I have to enable some sort of function to use complementary output ?
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Sun Nov 02, 2014 1:38 pm
by bartocham
Ok found it

now I see both diodes blinking
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#include "halconf.h"
#include "pwm.h"
#include "pal.h"
#include "pwm_lld.h"
#include "pal_lld.h"
static PWMConfig pwmcfg = {
10000, /* 10Khz PWM clock frequency */
10000, /* PWM period 1 second */
NULL, /* No callback */
/* Only channel 1 enabled */
{
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
},
0,
0
};
int main(void)
{
halInit();
chSysInit();
/* Enables PWM output (of TIM1, channel 1) on LED connected to PA8 */
palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
/* Enables PWM output (of TIM1, channel 1N) on LED connected to PB13 */
palSetPadMode(GPIOB, 13, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
pwmStart(&PWMD1, &pwmcfg);
pwmEnableChannel(&PWMD1, 0, 5000);
while (TRUE) {
chThdSleepMilliseconds(5000);
}
}
But I don't know why near " };" after PWMConfig pwmcfg I get compiler error about missing field initializers

Update:
found it too

new code for all interested:
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#include "halconf.h"
#include "pwm.h"
#include "pal.h"
#include "pwm_lld.h"
#include "pal_lld.h"
static PWMConfig pwmcfg = {
10000, /* 10Khz PWM clock frequency */
10000, /* PWM period 1 second */
NULL, /* No callback */
/* Only channel 1 enabled */
{
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
},
0,
0,
#if STM32_PWM_USE_ADVANCED
0
#endif
};
int main(void)
{
halInit();
chSysInit();
/* Enables PWM output (of TIM1, channel 1) on LED connected to PA8 */
palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
/* Enables PWM output (of TIM1, channel 1N) on LED connected to PB13 */
palSetPadMode(GPIOB, 13, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
pwmStart(&PWMD1, &pwmcfg);
pwmEnableChannel(&PWMD1, 0, 5000);
while (TRUE) {
chThdSleepMilliseconds(5000);
}
}
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Thu Nov 20, 2014 7:30 pm
by bartocham
Hi Giovani!
I don't have an oscilloscope and on LEDs it is hard to observe, but correct me if I'm wrong.
I'm trying to get 100Hz PWN signal with 50% DC so my config should like like this one below right?
Code: Select all
static PWMConfig pwmcfg = {
10000, /* 10Khz PWM clock frequency */
10,
NULL, /* No callback */
/* Only channel 1 enabled */
{
{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
},
0,
0,
#if STM32_PWM_USE_ADVANCED
0
#endif
};
And than in main:
Code: Select all
pwmStart(&PWMD1, &pwmcfg);
pwmEnableChannel(&PWMD1, 0, 10);
Re: STM32F100 on STM32VL Discovery - PWM complementary
Posted: Thu Nov 20, 2014 8:05 pm
by Giovanni
That configuration will give you 1000hz, you also need to set to alternate the timer output pin.
Giovanni