Page 1 of 2

Generate 30 Mhz Clock and an Interrupt on every nth Clock

Posted: Sat Aug 04, 2012 4:00 pm
by Mario
Hi together,

I am quite new to Chibios and I am using it on an STM32F407-Discovery Board. I am tryping to feed a TLC5940 from the STM32, which is a 16 Channel LED-Driver. The HW is working as a simple loop trigers the TLC5940 correctly.

Now I am trying to setup ChibiOS so that the STM32 generates a 30 Mhz Clock Output to drive the Clock-Signal for the TLC5940. Additionaly I do need to call a interrupt routinge every 4096 clock cycles of the 30 Mhz-Signal to restart the Grayscale-PWM of the TLC5940. I can setup two GPT timers with 30 MHz. The first toggles the GPIO for generating the Clock-Signal, the second gets a 4096 counter and does the Grayscale-Restart. That looks like this:

Code: Select all

static void gpt2cb(GPTDriver *gptp);
static const GPTConfig gpt2cfg = {
   60000000, /* 60 MHz timer clock.*/
   gpt2cb /* Timer callback.*/
};

static void gpt3cb(GPTDriver *gptp);
static const GPTConfig gpt3cfg = {
   60000000, /* 60 MHz timer clock.*/
   gpt3cb /* Timer callback.*/
};

gptStart(&GPTD2, &gpt2cfg);
gptStartContinuousI(&GPTD2, 2);  // 1 as counter does not work, so I use 60 Mhz

gptStart(&GPTD3, &gpt3cfg);
gptStartContinuousI(&GPTD3, 8192);


But this seem to me very inefficient. Isn't there a way to use just on HW-Setup that directly put out 30Mhz (or something near to it) and calls an ISR every 4096 Clock-Cycles?

Thanks for any help
Mario

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Sat Aug 04, 2012 6:13 pm
by Giovanni
Hi Mario,

Thread moved because you posted in an unrelated area.

The GPT driver is not designed for that because it would generate a callback even for the output clock and that is way too fast to handle.

I think the best way to do what you need is to use one of the timers directly for the clock generation (without any interrupt), the GPT driver can provide your callback using the other timer. The two timers would have the same clock so synchronization is assured.

Giovanni

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Sat Aug 04, 2012 8:43 pm
by Mario
Hi Giovanni,

I thougth do to so. But how can I use a timer in ChibiOS directly to drive a clock signal?

Thanks for the quick reply
Mario

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Sat Aug 04, 2012 8:50 pm
by Giovanni
You could use the PWM driver for that task (with callbacks kept at NULL) with a 50% duty cycle.

Giovanni

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Sat Aug 04, 2012 9:12 pm
by Mario
Hi Giovanni,

thanks. I will try.

Mario

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Mon Aug 06, 2012 8:48 am
by Mario
Hi again,

yesterday I tried to get a 21 Mhz Signal generated on GPIOA, Pin 15 by the STM32F407VG with the following:

Code: Select all

static PWMConfig pwmcfg = {
  21000000,                                       /* 21Mhz PWM clock frequency.  PWM Prescaler should be 8 with 168 Mhz, so ASSERTS won't fail */
  1,                                          /* Period is 1 Tick */
  NULL,
  {
   {PWM_OUTPUT_ACTIVE_HIGH, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
};

static void gpt3cb(GPTDriver *gptp);
static const GPTConfig gpt3cfg = {
   21000000, /* 21 MHz timer clock.*/
   gpt3cb /* Timer callback.*/
};

pwmStart(&PWMD2, &pwmcfg);
pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH (&PWMD2, 5000));

gptStart(&GPTD3, &gpt3cfg);
gptStartContinuous(&GPTD3, 4096); // Call Callback every 4096 Ticks



But this does not seem to work. Maybe I don't see a obvious error? BTW, would it be possible to use MCO2 (DIV2) with a PLLI2SN set to 240, PLLI2SR set to 4 to generate 30 Mhz? I don`t use I2C in my project. Does ChibiOS use the PLLI2C-Clock in any way so that setting PLLI2C 60 Mhz would interfere with anything else?

Greetings
Mario

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Mon Aug 06, 2012 9:21 am
by Giovanni
Hi Mario,

Did you program the timer output as alternate function? the driver does not do that by itself, the PWM would not be output otherwise.

You can use MCO, you can configure it in mcuconf.h.

Giovanni

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Mon Aug 06, 2012 5:05 pm
by Mario
Hi Giovanni,

yes I initialized the Output with

Code: Select all

// DDR, PORT an PIN connected to GSCLK
#define GSCLK_PORT       GPIOA
#define GSCLK_PIN       15

palSetPadMode(GSCLK_PORT, GSCLK_PIN, PAL_MODE_ALTERNATE(1));


But I noticed that to use GPT3 I enabled TIM3 and TIM2 for PWMD2. I can not use TIM2 for both. Will it still be the "same" clock source to synchronize the to clocks?

Greetings
Mario

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Mon Aug 06, 2012 5:10 pm
by Mario
Hi Again,

if I use MCO2 (PC9) as clock signal is it possible to use an EXT-Interrupt directly with PIN PC9 or do I have to wire that output in hardware to a GPIO-Pin?

I hope I don't ask to silly questions.

Thanks
Mario

Re: Generate 30 Mhz Clock and an Interrupt on every nth Cloc

Posted: Mon Aug 06, 2012 5:57 pm
by Giovanni
Mario wrote:Will it still be the "same" clock source to synchronize the to clocks?


Yes, same PLL, it would be perfectly synchronous.

Mario wrote:if I use MCO2 (PC9) as clock signal is it possible to use an EXT-Interrupt directly with PIN PC9 or do I have to wire that output in hardware to a GPIO-Pin?


a 30MHz interrupt rate would be a bad idea, or I misunderstood it?

Giovanni

Edit: removed pointless text :-)