Generating a 5MHz clock on a GPIO pin on an LPC1768 - Page 1
Forum section: electronics / general

author: perdrix
author_rank: Frequent Contributor
timestamp: August 02, 2023, 12:43:54 pm
content: The processor is running with a processor clock of 100MHz.I only have the following GPIO pins available for use:P0.2, P0.3, P0.23, P0.24, P0.25 and P0.26I tried to use a timer interrupt to generate a 5MHz clock signal, but AFAICT there just aren't enough processor cycles available to handle a timer interrupt every 10 clocks (which makes sense) and I only achieved a 1MHz signal out and there were probably not many processor cycles available to do any other work!Is there a neat way to get a 5MHz clock out those pins using hardware to toggle it (perhaps using one of the peripherals)? Ideally I'd like to use P0.23 and P0.26 for the clock signal (but not both at the same time).Thanks, David

author: JPortici
author_rank: Super Contributor
timestamp: August 02, 2023, 12:53:04 pm
content: Check the datasheet for one of the following- Output compare/PWM/Whatever is called on LPC- Timer module could have a trigger output- Reference clock generator- I2S Clock

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 02, 2023, 01:22:06 pm
content: I'm not familiar with that processor, but normally this is done with one of the timer modules in your uC.It may also be possible to abuse DMA for this. I'm thinking along the lines of setting up a small array with ones and zero's and use DMA to output that array to an output pin in a circular way. I never used DMA myself though so I'm not sure if this can work.

author: wek
author_rank: Frequent Contributor
timestamp: August 02, 2023, 02:38:49 pm
content: I don't use the LPC1768, but P0.23 appears to be usable as I2SRX_CLK - can't that be abused in some way to output the required clock?JW

author: bingo600
author_rank: Super Contributor
timestamp: August 02, 2023, 02:48:04 pm
content: Would this helphttps://circuitsgeek.com/guides-and-how-to/how-to-use-timer-in-lpc1768-lpc1768-timer-tutorial/Or this - But it seems like for a precise 5MHz you'd have to run the MCU PLL @100MHz , NOT 96MHz (What does USB say to that)https://os.mbed.com/forum/mbed/topic/733/?page=1#comment-11587100MHz Clockhttps://github-wiki-see.page/m/tarasjg/mbed-vga/wiki/Reconfiguring-the-LPC1768-Main-PLLhttps://msys-mv.blogspot.com/2011/01/using-pwm-on-lpc17xx.html

author: nctnico
author_rank: Super Contributor
timestamp: August 03, 2023, 06:58:20 am
content: The USB enabled LPC mcus typically have two plls. One for usb and one for the rest of the system. So you can run the timer from a 100mhz clock and usb from a 48mhz clock

author: perdrix
author_rank: Frequent Contributor
timestamp: August 03, 2023, 09:35:43 am
content: Let's make this clear I already know how to use the timers in the LPC1768. The clock runs at 100MHz. To generate a 5MHz square wave you need to generate a timer interrupt every 100ns or 10 clock cyclesTo do that the timer has to be set with a MR0 set to 10, and run off the processor clock (CCLK). This causes a timer interrupt every TEN processor cycles and it takes many more clocks than that to process an interrupt.That's why I need a HW generated 5MHz (and 1MHz - though not at the same time).PWM doesn't appear to be an option as AFAICT none of these pins appear to be usable as PWM output pins (please put me straight if I got that wrong).I'm not sure about abusing I2S to clock out on P0.23 - I've a nasty feeling that it will interfere with way too many other pins I see a lot of google in my future.D.

author: hans
author_rank: Super Contributor
timestamp: August 03, 2023, 10:12:17 am
content: Yes, no timer outputs on any of those pins..Regarding DMA to transfer to GPIOs: you can't use bitbanding for that, as that's a memory view only available to CPU. So it would need to write the whole port. Now the LPC1768 does have SET/CLR registers, but they are not combined (as in STM32), so the DMA controller would have to support increment both memory and peripheral address.But a bigger issue with DMA is the high amount of bus transfers. 5MHz clock = 10MT/s, of the 100MHz bus cycles available. I don't know about the bus structure used, but if it has some arbitration overhead it could consume even more bandwidth or cause considerable jitter.Now it does look you could use I2SRX_CLK. Despite the name "RX", the I2S master could still be the LPC1768, where it is sampling data from an audio ADC. I think you can map individual I2S pins using the pin function select registers, so only I2SRX_CLK is driven to P0. Fig 107 on page 487 of the user manual does show several dividers in place, where:I2SRXMCLK = PCLK_I2S * (X/Y) /2(which signal could stay internal in the chip)And I2SRX_CLK is I2SRXMCLK / N where N=1..64So that sounds like it could do what you want.

author: JPortici
author_rank: Super Contributor
timestamp: August 03, 2023, 10:47:27 am
content: Unless you have some pins that are hardwired to be SPI/I2S when you switch on the peripheral (for example, dedicated pins achieve higher clock frequency), i see no reason why you couldn't enable I2S and just route the clock pin.Another crazy idea: Output a lower frequency clock (how much depends on the hardware) and use a PLL to scale it up. But i don't know how much hardware you can change, and if you can change the hardware i would look into changing the pin arrangement so you have PWM and be done with it.

author: nctnico
author_rank: Super Contributor
timestamp: August 03, 2023, 12:13:14 pm
content: This sounds like it is time for a new board revision that provides a timer pwm or match output on the right pin

author: Siwastaja
author_rank: Super Contributor
timestamp: August 03, 2023, 12:26:34 pm
content: Quote from: nctnico on August 03, 2023, 12:13:14 pmYes, or modifying the existing board(s), if not hundreds of them, with a simple bodge wire (or two if you have all other pins in use and need to do a swap).

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 03, 2023, 01:49:02 pm
content: Is it like in stm32, where you can remap pins to different peripherals?What are the options for those pins?You still have a way no matter what, run a timer at 10MHz, attach a circular dma channel configured to transfer a buffer with two values for resetting/setting the BSRR bits for that port (Is it like stm32?). Edit: FIOSET/FIOCLR?So each time the timer expires the DMA updates the pin.In circular mode the DMA will run forever, no need of any irq.

author: nctnico
author_rank: Super Contributor
timestamp: August 03, 2023, 07:28:04 pm
content: Time triggered dma could be a way but it will eat a significant amount of memory bandwidth

author: wek
author_rank: Frequent Contributor
timestamp: August 03, 2023, 08:59:49 pm
content: > I'm not sure about abusing I2S to clock out on P0.23 - I've a nasty feeling that it will interfere with way too many other pins I see a lot of google in my future.Why don't you just try. It should be something like a 10 minute exercise - set P0.23 to this peripheral, enable clock to I2S, set its Rx to master, set the clock prescaler. I don't quite see why would that interfere with any other pin.JW

author: peter-h
author_rank: Super Contributor
timestamp: August 04, 2023, 06:25:56 am
content: Could this be done with DMA, memory to GPIO, circular mode, reading two locations one containing 1 and the other 0 ?You would not have much control over the frequency but it would be fast

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 05, 2023, 01:55:16 pm
content: Quote from: perdrix on August 03, 2023, 09:35:43 amUsing a timer to generate a periodic ISR is only one of the many uses of a timer.Another very common use of a timer is to generate a PWM output directly.And it's easy to program the timer itself in such a way that it overflows every 200ns combined wit resetting the output at overflow, and then setting the output with a capture / compare register halfway the count.Quote from: perdrix on August 03, 2023, 09:35:43 amYes, exactly. Program the timer once, and it will output 5MHz to a hardware pin for as long as it has power applied or you have had enough of it.The only limitation is that not all outputs can be used for PWM, which leads to:Quote from: perdrix on August 03, 2023, 09:35:43 amStop the guesswork and check the datasheet. You'll know soon enough if this can work or not. Usually there is some big matrix in the datasheet which lists all the alternate pin functions for each of the pins.And if it's possible, it is by far the best method. All the others are kludges. I would even consider cutting a PCB track and routing a wire or two to swap some pins to make this possible, or revise the project and spin a new PCB variant.This may work:Quote from: perdrix on August 03, 2023, 09:35:43 amWith STM32 you can swap pins on an individual bases to a GPIO pin, or to an "alternate function" (which is one of timer, uart, spi, etc pins). and by only setting the clock pin to an "alternate output", it's quite possible it won't interfere with other pins.