Help asked -- PAL-GPIO PWM and Interrupts

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
BMWPower
Posts: 24
Joined: Mon Oct 24, 2016 10:27 pm
Has thanked: 1 time
Been thanked: 2 times

Help asked -- PAL-GPIO PWM and Interrupts

Post by BMWPower »

I am struggling with combining some functions. Maybe when I write my "problem" out someone can help me setting me in the right direction.

I am trying to read a PWM signal from a RC receiver (servo). With my logic analyzer I have been finding out that the duration in ms (milliseconds) in the signal is telling me something about the position.

Then I was searching for some documentation and this article/blog
http://www.camelsoftware.com/2015/12/25/reading-pwm-signals-from-an-rc-receiver-with-arduino/
has been making me think porting this to ChibiOS and STM32L476RG.

So I get the concept and get the Arduino code. But I am struggling with interrupts in ChibiOS....

I also think I have to combine something with PWMDriver, PAL and Interrupts..... as I am using 18.2.0 (RT).

Who can help me out?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Help asked -- PAL-GPIO PWM and Interrupts

Post by Giovanni »

Hi,

You should look into the ICU driver, it measures a PWM signal using a timer returning period and duty cycle.

There are several PWM-ICU demos under ./testhal/stm32.

Moving this topic in the correct forum: STM32 support.

Giovanni
BMWPower
Posts: 24
Joined: Mon Oct 24, 2016 10:27 pm
Has thanked: 1 time
Been thanked: 2 times

Re: Help asked -- PAL-GPIO PWM and Interrupts

Post by BMWPower »

A thanks! I will check this out. And interrupt examples?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Help asked -- PAL-GPIO PWM and Interrupts

Post by Giovanni »

Hi,

Have you seen this article?

http://chibios.org/dokuwiki/doku.php?id ... interrupts

Note that in all device drivers you can find plenty examples of "real" ISRs and the enable code. In general, if you use HAL, there should not be the need to write ISRs, the drivers give you callbacks from ISRs that are normal functions.

Giovanni
BMWPower
Posts: 24
Joined: Mon Oct 24, 2016 10:27 pm
Has thanked: 1 time
Been thanked: 2 times

Re: Help asked -- PAL-GPIO PWM and Interrupts

Post by BMWPower »

This code is working for me (maybe someone is helped by the post). The interrupts I still have to find out. How to use them ISR...

Code: Select all

#include "ch.h"
#include "hal.h"
#include "dspwm.h"


BaseSequentialStream *ichp;

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

icucnt_t last_width, last_period;

static void icuwidthcb(ICUDriver *icup) {

  palSetLine(LINE_LED_GREEN);
  last_width = icuGetWidthX(icup);
}

static void icuperiodcb(ICUDriver *icup) {

  palClearLine(LINE_LED_GREEN);
  last_period = icuGetPeriodX(icup);
}

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

static ICUConfig icucfg = {
  ICU_INPUT_ACTIVE_HIGH,
  10000,                                    /* 10kHz ICU clock frequency.   value: 10-15-19*/
  icuwidthcb,
  icuperiodcb,
  NULL,
  ICU_CHANNEL_1,
  0
};

void initICUDs(BaseSequentialStream *chp)
{
    palSetPadMode(stm32L476RGPwmPort1, stm32L476RGPwmPin1, PAL_MODE_ALTERNATE(1));

    /*
    * Starting the PWM driver 1.
    */
  //  pwmStart(&PWMD1, &pwmcfg); //TIMER 1!! PWMD9 = TIM9
    chprintf(chp, "ICU started!\n");
    ichp = chp;

    icuStart(&ICUD3, &icucfg);
    palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2));
  }

void readICU(BaseSequentialStream *chp)
{
    icuStartCapture(&ICUD3);
    icuEnableNotifications(&ICUD3);
    chThdSleepMilliseconds(2000);

    icuStopCapture(&ICUD3);
    icuStop(&ICUD3);

    //chprintf(chp, last_width);
    chprintf(chp,"period: %d\r\n",last_period);
    chprintf(chp,"width: %d\r\n",last_width);
    chprintf(chp, "\n");

}
Post Reply