Gas station without pumps

2015 August 24

PteroDAQ frequency channels

I’ve been trying to add frequency channels to PteroDAQ, so that it can plot the frequency of an input on a digital channel.  So far, I’ve only implemented this for the Teensy LC, which uses a KL26 ARM processor.  Because I want to be able to mix frequency channels with regular analog or digital channels, I’m using the method that counts rising edges in a fixed time period (the sampling period set for PteroDAQ), rather than timing how long there is between edges.  The chosen edge-counting method is more appropriate for high-frequency signals than for low-frequency ones, but by using long-term averaging, one can measure fairly low frequencies also.

In order to count adequately high frequencies, I need to run a hardware clock that does not use interrupts to do individual counts, but which can keep counting why the processor does useful stuff.  One choice (made, for example in the Teensy FreqCount library) is to use the low-power timer LPTMR.  But only one pin on the Teensy LC can be used with this timer—pin 13, which is also the LED output pin.  I wanted something more versatile, that could be used with any digital pin and that could have multiple frequencies being counted at once.

The closest I’ve been able to come so far is to use the DMA channels as counters (a suggestion my son made to me, based on ideas he saw in a Freescale application note).  I can use any pin on Port A, C, or D to control a DMA channel, and there are 4 DMA channels, but each port only provides one signal to the DMA, so I can have 3 independent frequency measurements (one for any pin from Port A, one for any pin from Port C, one for any pin from Port D).  If I use the same trick on the Teensy 3.1, which is a K20 processor, I can have 5 independent measurements, one each from Ports A, B, C, D, and E.  On the FRDM-KL25Z board, I only get 2 measurements, one each from Ports A and D.

To use the DMA channel as a counter, I had to set it up with a large byte count (the max is 0xFFFFF, or 1,048,575) and have the DMA do a no-op 1-byte transfer from a fixed source to a fixed destination on each external signal. On each read of the frequency, I read the remaining count and subtracted from 0xFFFFF to see how many external signals had happened, and put that count in the queue for sending to the host computer like any analog read.

It took me a long time to get the code working on the Teensy LC (about 2 days).  The first problem was just understanding all the pieces of the DMA system:

  • Using the SIM_SCGC6 and SIM_SCGC7 registers to turn on clocking to the DMAMUX and DMA
  • Using DMAMUX0_CHCFGn to route signals from the ports to the DMA channels
  • Setting up the PORTx_PCRn register to send DMA signals on rising edges
  • Setting up the DMA_SAR, DMA_DAR, DMA_DSR_BCR, and DMA_DCR registers for the DMA operation itself.

Another big chunk of time was spent rearranging some of the python software, so that the frequency channels could be selected in the GUI and the communications protocol to the microcontroller board updated to handle frequency channels.

But the biggest chunk of time was spent trying to debug a race condition.  No matter what I did, running a high-frequency signal with a high sampling rate resulted in things failing in unpredictable ways after an unpredictable delay (the counts all coming out 0, the communication packets getting incorrect checksums, or just freezing).  I had to power cycle the Teensy LC after each failure to try again.

I tried all sorts of debugging tricks (no SWD debugging interface on the Teensy LC), including sending contents of the various DMA registers instead of the count, to see which things were going wrong.  Unfortunately, almost everything seemed ok until it failed, and then nothing came out.  I was convinced it was a race condition—but where?

I tried various ways of turning the counting on and off, based on different suggestions in different Freescale manuals (using the ERQ bit in DMA_DCR or using the ENABLE bit in DMAMUX).  I even tried various combinations of the turning on and turning off in different orders.  It was clear to me that the problem was in reading or resetting the DMA_DSR_BCR register, but there were several places where a race might be occurring and no help in any of the reference manuals.

By trial and error, I finally figured out that if I used the DMAMUX enable bit to turn off the counting, and put a NOP after it before looking at the DMA_DSR_BCR register, the race condition went away.  The crucial problem seems to be that looking at DMA_DSR_BCR at the same time that hardware is trying to change it results in unpredictable behavior—not just unpredictable values for the read, but seriously wedging the DMA so that a hardware reset is needed.  The extra NOP makes sure that any pending DMA operations are completed before accessing the DMA_DSR_BCR register.  This warning does not seem to be present in any of the KL26 documentation—I’m not sure the engineers who designed the DMA were aware of it.  Either that or the engineers failed to communicate the importance of this constraint to the tech writers who did a sloppy cut-and-paste job in writing the KL26 DMA documentation (for example, there are references to a non-existent “TCDn”, by which they mean the 4 registers DMA_SARn, DMA_DARn, DMA_DSR_BCRn, and DMA_DCRn).

Running the frequency channel with a square-wave input from the FG085 function generator lead to some more discoveries:

  • The samples taken by PteroDAQ have a fairly large amount of jitter at high sampling frequencies.  At 10kHz sampling and a 500kHz input, the number of observed edges varied from about 47 to 53, representing a ±6µs variation on the nominal 100µs.  My son suggested that the jitter was caused by the USB communications—if the timer interrupt occurred while the USB interrupt was in progress, the sample would be delayed up to about 6µs, and this could happen about every millisecond with heavy communication. Sure enough, the long periods were followed immediately by short periods, and some clusters about 1msec apart occurred (though not ever millisecond).  At low sampling frequencies, there is less USB communication, so a lower probability of getting the 6µs delay, which also represents a much small fraction of the period, so I did not see jitter at lower sampling frequencies.
  • One of the clocks is out of spec.  When the FG085 was putting out a nominal 500kHz signal, the PteroDAQ system measured it at 499958.5 Hz, which is –83ppm.  The two crystals should each be about ±20ppm, so an 83ppm discrepancy is larger than expected. With a nominal 5kHZ signal, the PteroDAQ system measured it at 4999.524 Hz (–95ppm).  I’ll have to take the function generator and PteroDAQ board into the circuits lab and use the frequency counters and function generators there to see what the actual accuracies are.  For anything that we do in the applied electronics class, 100ppm is good enough, but it would be nice to know how far off my instruments really are.
  • The FG085 glitches (turning off the output for a while) when changing frequency.  I plotted the frequency as I stepped the frequency down in 100Hz steps from 5kHz to 2kHz:
Each time I turned the knob, the FG085 turned off the output for a while, resulting in a large number of missing counts.

Each time I turned the knob, the FG085 turned off the output for a while, resulting in a large number of missing counts.

The next step for me is to implement frequency channels for the FRDM-KL25Z board (pretty much identical to the KL26 on the Teensy LC, except for having to use the MBED syntax for the register setting rather than the one Teensyduino uses) and the Teensy 3.1 board (the K20 DMA is much more complicated, so it will probably take me a while to figure out and set up—especially if it also has undocumented race conditions). Once I get those working, I’ll have to figure out how to do the GUI so that people can quickly figure out that which combinations of frequency channels are legal. I’ll probably want to do that with separators in a menu, greying out sections that already have frequency channels in use.

Once all that is done (later this week??), I’ll push it up to the PteroDAQ repository.

2015 August 16

Teensy LC support for PteroDAQ working

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 09:08
Tags: , , , ,

Despite what I claimed in Teensy LC support for PteroDAQ written but not tested, adding support for the Teensy LC board to the PteroDAQ data acquisition system also took a day of effort, not just a couple of hours.  The board arrived in Saturday’s mail, so I tested the code immediately. My initial attempt did not work, and debugging took an embarrassingly long time.

The first problem was that the KL25Z code had used the SysTick timer to do the interrupts for the periodic sampling, but the Teensyduino usb_serial code uses the SysTick timer for other timing purposes (it uses SysTick as a timestamp).  On the Teensy 3.1, I’d used one PIT (periodic interrupt timer) for the periodic sampling, and two others for keeping a high-resolution timestamp, but the Teensy LC does not have that many PITs.

I ended up using the LPTMR (low-power timer) for the sampling frequency, which gives a wide range of frequencies with the precaler (allowing a very long sample period of 268s).  Unfortunately, the LPTMR is only a 16-bit counter, so the resolution of frequency is not great—about 15ppm at low frequencies (with counter values between 32k and 64k), and period step sizes of 62.5nsec at sampling frequencies above 244Hz.  I can’t hit 60Hz exactly, but the 10ppm that it is off is less than the error of the crystal oscillator and far less than the error of the line frequency of the power lines.

Getting the LPTMR to work correctly took a long time, mainly because of spelling errors (sometimes the name needed is LPTMR, sometimes LPTIMER, sometimes LPTMR0).  The inconsistency seems to come mainly from the kinetis.h file provided in the Teensyduino code.

The SysTick timer we used on FRDM KL25Z implementation is a 24-bit counter running at 48MHz, so it has a resolution of  20.83 nsec for frequencies above 0.35Hz, and 333nsec at lower frequencies. The Arduino implementations use the same timer for both timestamps or periodic interrupts (which I could also have done on the Teensy LC, using the same pair of PITs for either) since the timestamps are not used directly with periodic interrupts.

I looked at the size of the different implementations of the firmware for PteroDAQ with the current implementations as well as the longest sampling periods:

board bytes longest period
Teensy 3.1 12,536 119.3046 s
Teensy LC 12,632 268.4355 s
FRDM KL25Z 26.6k 5.592405 s
Arduino Leonardo 9,908 4.194240 s
Arduino Uno 7,350 4.194240 s

The Teensy 3.1 implementation could easily be modified to get ridiculously long sample times, as there is a spare 32-bit PIT to get times up to 512Gs (over 16000 years) with a resolution of 27.8ns, but the other implementations would be harder to change.  Using the 2-PIT approach on the Teensy LC or the FRDM KL25Z could easily get up to 384Gs, with a resolution of 41.7ns.

I think I may switch all the ARM boards over to using 2 PITs for timing, for both the timestamps and for the periodic interrupts, as this seems to provide the best tradeoffs of period and resolution.


2015 August 13

Teensy LC support for PteroDAQ written but not tested

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 19:29
Tags: , , , ,

Adding the Teensy 3.1 board to the PteroDAQ data acquisition system only took a day of effort (see PteroDAQ supports Teensy 3.1), extending that support to the Teensy LC, took only a couple of hours, since it just meant adding another board description to and merging the KL25Z and Teensy 3.1 code that was already written.

The Teensy LC board I ordered from the PJRC store) has been shipped, but probably won’t arrive until Monday, so I’ve not been able to test the code yet.

I looked at the size of the different implementations of the firmware for PteroDAQ with the current implementations:

board bytes
Teensy 3.1 12,528
Teensy LC 12,544
FRDM KL25Z 26.6k
Arduino Leonardo 9,840
Arduino Uno 7,310

The MBED USB stack used for the FRDM KL25Z board seems to bloat the code there enormously, but even the Teensy code seems a bit bloated compared to the Arduino code (which is already much more bloated than really needed).

If the Teensy LC code works (and I see no reason it shouldn’t, after tiny amounts of debugging to remove any bugs that the compiler hasn’t already caught), I’ll probably look into using the Teensyduino setup for programming the FRDM KL25Z board also.  If I can get that to work, I’ll probably give up on the MBED toolchain entirely—their software is way too deeply layered with way too many dependencies.  It looks like it was developed by computer scientists who drank too much of the “layered abstraction” kool-aid.

The biggest problem with the Teensyduino environment for developing for the FRDM KL25Z is that they’ve not set up any of the macros for the KL25Z, which is almost (but not quite) identical to the KL26Z that they have set up.  I do have a FRDM KL26Z board, though, so maybe I should develop for that.  The difficult parts will be

  • Setting up macros that allow different compilation for FRDM KL26Z and Teensy LC, even though both are using KL26Z processors.  I can probably handle that with a new entry in the boards.txt file.
  • Setting up the download using the MBED downloader on the FRDM KL26Z board rather than the Teensy loader. I’m not sure that the Arduino environment provides any way to set up a download as “create a bin format file and write to fake flash drive”.  Even just getting the bin file created and put in an easy-to-find place may be difficult without manual intervention.

Since the Teensyduino development is supported by the sale of Teensy boards, it is unlikely that Teesyduino will ever be officially extended to support the boards sold by Freescale.

2015 August 11

PteroDAQ will support Teensy LC

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 07:49
Tags: , , , ,

Adding the Teensy 3.1 board to the PteroDAQ data acquisition system only took a day of effort (see PteroDAQ supports Teensy 3.1), and it looks fairly straightforward to extend that support to the Teensy LC, which at $11.65 looks like the cheapest ARM development board available.  The KL26Z chip on the Teensy LC is very similar in its capabilities to the KL25Z chip on the FRDM KL25Z boards that I’ve been using with PteroDAQ for the last couple of years, though the Teensy LC uses one with less RAM, so the boards may be more limited in handling bursts of high sampling rate.

I just ordered a Teensy LC board this morning (direct from the PJRC store) and will probably do the coding and testing for the board this weekend.  I’m now leaning towards using the Teensy LC for the class, as it will provide a lower-cost option than the FRDM KL25Z boards, especially once the cost of headers is included (we can use the Teensy LC with mostly male headers and just a couple of small female headers, which cuts about $2/board off the price of the headers).  The maximum sampling rate will be a little lower than on the Teensy 3.1, but PteroDAQ is currently more limited by the ability of the laptop to accept the data than by the board’s ability to generate it.

If I can figure out a good way to get the Teensyduino code onto a FRDM KL26Z board, I might be able to do some debugging even before the Teensy LC arrives, because I have a FRDM KL26Z board that I bought some time ago but never used.  The Teensyduino uses the GCC ARM compiler, so I think all I’ll need to do is figure out where all the compilers, linkers, and format conversion routines are kept, and all the flags that get passed to them, and I should be able to get things in the right format for the MBED download firmware on the KL26Z board.  Running the Arduino IDE with “verbose” output should provide all this information.

All the Teensy boards have their schematics online, so the pin mapping can be set up now.  I’ll have to merge some of the code we wrote for the Teensy 3.1 and for the FRDM KL25Z boards to get the rest, after checking to see what registers are different between the KL25Z and the KL26Z. All the analog-to-digital conversion and timing is being done with bare-metal manipulation of the peripheral registers, to avoid the overhead of the APIs of the MBED, Arduino, or Teensyduino systems.

I think that we may end up reusing (or modifying) the usb-serial code from the Teensy LC for the FRDM KL25Z board also, so that we can get rid of the use of MBED code.  Moving code onto and off of the MBED site is a big pain, and it would be good to have everything compilable on the laptop without needing internet access.  The MBED USB stack is about all we’re really using of the MBED code, and the Teensyduino USB stack seems to work as well (comparing Teensy 3.1 and FRDM KL25Z, which is not really a fair comparison).

2015 August 10

PteroDAQ supports Teensy 3.1

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 00:25
Tags: , , , ,

In one day my son and I added support for the Teensy 3.1 board to the PteroDAQ data acquisition system that previously supported the Freedom KL25Z board and the ATMega-based Arduino boards.

We ended up using the Teensyduino development system, but really only for the downloading and for the usb-serial library, since the K20 ARM chip on the Teensy 3.1 is quite similar to the KL25 that we originally based things on.

The Teensy 3.1 is a lot easier to install the software on than the Freedom boards, and runs a little faster (72MHz instead of 48MHz), but has essentially the same ADC.  Actually, it has 2 analog-to-digital converters, but most of the pins can only be read by ADC0, so we’ve not set up ADC1 to read anything but the internal 1.2V Vref (which is conveniently provided as an output on the AREF pin).  We had originally planned to use just ADC0, but the code for reading the Vref signal on ADC0 never worked—I suspect an error in the reference manual, since changing to reading Vref with ADC1 worked fine.

The Freedom boards are cheaper, are easier to unplug the USB cables from, can deliver more power at 3.3V, have RGB LED, and have a lot of neat features missing from the Teensy boards, but the Teensy boards can be configured to plug directly into a bread board (if you give up a lot of the connections and just use 26 pins), and have more RAM (so can run for longer at high sampling rates before the buffer overflows).

I’m going to have to rewrite part of my book to talk about the possibility of using the Teensy 3.1, and I’ll have to decide whether the extra $6–$7 is worth the simpler setup for my Applied Electronics lab course. We’d sacrifice being able to get much power from the board (probably only about 100mA instead of 500mA at 3.3V), but that is a relatively minor loss, since we have bench power supplies at every station.

I’m not sure what I’ll recommend in the book for people trying to learn on their own—I’ll probably have to play with the Teensy a bit to see how useful it is.  I have at least one other program that the students have been using in the lab (the frequency detector for turning a relaxation oscillator into a touch sensor) that I’ll have to port to Arduinos and the Teensy 3.1.

For home hobbyists who aren’t planning to dive deep into embedded-system programming, the Teensyduino IDE is a lot friendlier than the MBED.ORG tools (and I hear that the Kinetis SDK has a very, very large learning curve), so it might be a better board despite the lack of peripherals (no accelerometer, RGB LED, or capacitive touch slider).


« Previous PageNext Page »

%d bloggers like this: