Gas station without pumps

2015 August 28

Book draft 2015 Aug 28

Filed under: Circuits course — gasstationwithoutpumps @ 21:09
Tags: , , ,

I released an updated version of the Applied Electronics for Bioengineers text today.  This draft involved several changes:

  • I moved the electrode chapter and electrode lab to just before the EKG lab. This makes more sense in terms of the flow of the course, and means that the electrode lab can be done during the shortened Memorial Day week. The microphone, loudspeaker, and first audio-amp lab are now closer together, making a more coherent cluster (the hysteresis lab is still between the loudspeaker lab and the audio-amp lab). Unfortunately, the change also means that the introduction to op amps gets squeezed a bit, as the first audio-amp lab is a week earlier.  I’ll see how well that works this spring, and whether some other adjustment to the schedule is needed.
  • The hysteresis and relaxation oscillator chapter and lab were rewritten to take advantage of the frequency-measurement channels added to PteroDAQ. I replaced the old frequency recording with one done using PteroDAQ, and I added discussion of how to do the lab without bench equipment. I think that these chapters are essentially done,
  • The protoboards for using op amps came this week, so I took photos and put them in the audio-amp lab:
    Front and back of op-amp protoboard. This board replaces the instrumentation-amp protoboard used in previous years. Students will solder two boards: a microphone preamp and an EKG.

    Front and back of op-amp protoboard. This board replaces the instrumentation-amp protoboard used in previous years. Students will solder two boards: a microphone preamp and an EKG.

    I think that the new protoboards will be easier for students to use than the old ones—the grouping of wires is more obvious, there are more dedicated resistor slots, and each resistor can now connect to two wires at each end.

  • I also added a picture to the power-amp chapter of a bread board that I had melted by getting an nFET too hot:

    Too much current through a nFET can make it hotter than a breadboard can handle.

    Too much current through a nFET can make it hotter than a bread board can handle.

  • Throughout the book I changed references to the KL25Z board to references to the FRDM-KL25Z, as that seems to be the official part number for the development board.

At this point, I’ve rewritten and updated through Chapter 17, and I still have Chapters 18 through 28 to go.  It looks like I won’t get a full pass over the book done this summer

Work on the book and PteroDAQ will slow down somewhat for the next few weeks as I take care of other pressing deadlines: my teaching/research/service statement and biobib for a merit review—it’s been 4 years since the last time I did them; an external review for tenure of an assistant professor; final approval of PhD thesis that I read earlier this summer; finalizing the H. pylori genome assemblies I’ve been working on; setting up my web sites for fall quarter; planning the schedules for both my fall quarter classes; ophthalmologist appointment; training session for undergrad peer advisers; School of Engineering “all-hands” meeting; … .

I’ve spent about 4–5 hours on the external review so far, and have only read the CV, research statement, and two of the papers—there are five more papers to read, so I figure it will take me about 10 more hours to read the remaining papers and synthesis a coherent 1–2-page letter supporting (or not) the tenure case.

The thesis is a less difficult task, as I’ve already read the whole thing and marked it up—all I have to do is check that the corrections and clarifications I requested have been made.  I expect that to take 4–8 hours.

One problem is that both the tenure review and the thesis checking are tedious, so I can’t do them for very long at a time—but both are due next Wednesday. Writing the teaching/research/service statement will take some effort, as I have never been good at the bragging that seems to be expected—I tend to write honest statements of what I’ve been doing, which then get misinterpreted as my having been much less successful than what a straight reading of the material says.

DMA counter for event counting on Freescale Kinetis processors

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

The application note mentioned in PteroDAQ frequency channels is Freescale’s AN5083Using DMA for pulse counting on Kinetis, which I finally got around to looking up and reading today, after implementing frequency channels on the KL26, KL25, and K20 processors (Teensy LC, FRDM-KL25Z, and Teensy 3.1 boards, respectively).  The reason I looked it up finally was to look for a workaround for the biggest problem—namely that the CITER counter on the K20 has a maximum of 32767.  If I’m counting at the highest frequency (supposedly 8MHz or 6.26MHz, depending which documentation I believe, but I’ve not been able to get measurements much above 4MHz), that requires sampling at 125–245Hz or faster. But I often want to sample at factors of 60Hz, to alias out any frequency modulation from line interference.  The cruder DMA systems of the KL25 and KL26 use a 20-bit counter, and should be able to count to 4-5MHz (though they are likely to top out at 3MHz), so I could sample as slowly as once every 3.8s, even at the highest measurable input frequency.

I had no problem getting the counting to work with the 15-bit DMA_TCD_CITER counter. I can live with the 32767 count limit (at 60Hz sampling, that would be a 1.966MHz limit on the counting), but I wanted to see if I could use the optional interrupt that can be set up when the CITER counter ends to increment a software counter and reset the DMA counting.  That should give me an arbitrarily wide counter, at the cost of perhaps missing a count at the wraparound (an error of 30.5ppm, about the same as the clock error).

Unfortunately, AN5083 merely explains what I had already figured out for myself—without even the essential warning that you need a NOP after disabling the DMAMUX before looking at the counter to avoid a race condition on the KL25 and KL26:

    (&DMAMUX0_CHCFG0)[0] &= ~(DMAMUX_ENABLE);       // turn off at DMAMUX
    __asm__ volatile( "nop" );
    uint32_t count = 0xfffff - (DMA_DSR_BCR0 & 0xfffff);

They only say

The major loop counter of the DMA register DMA_TCDn_CITER_ELINKNO is limited to 15 bits, in the case where the channel linking feature of the DMA is not in use (refer to DMA chapter in the Reference Manual). Therefore, there is a limitation of 32K major loops count. So, user must check CITER register and make sure it will not reach to zero. If, CITER reaches zero, BITER will reload the value from BITER register.

I tried for a good chunk of yesterday to get the code working with the interrupt.  I can get the interrupt to occur (detected by turning off an LED in the interrupt return), I can read the longer frequency counter (detected by turning off an LED if the count to return is >32767), and the count is returned to be pushed on the queue, but something gets wedged and that longer count is never output to the host.

The processor needs to be power-cycled, the same as if the processor had gone into an infinite loop (which is what the Teensyduino unimplemented_isr routine does). I did disable the DMA error interrupt routine (with DMA_CEEI), so that isn’t the problem.  I even confirmed this by enabling the error interrupt and providing a dma_error_isr routine that turns off the LED, but the LED did not turn off.  I also checked the DMA_ES register at the beginning and end of the dma_ch0_isr routine and in the routine that reads the counter, and DMA_ES was always 0.  So if there is an error, it is not being reported by the hardware.

I suppose that an SWD debugging interface (not available on the Teensy 3.1 board) might be useful here, as I could at least check my conjecture that the processor is looping in the fault_isr routine.  It may be wedged somewhere else.  And there is always the possibility that there is an undocumented hardware bug (like the race condition between DMAMUX disabling and reading DMA_DSR_BCR on the KL26) that requires some workaround that I haven’t thought of yet.

I’ve given up on the higher resolution counters for Teensy 3.1 for now and just pushed the 15-bit implementation (with the interrupt code mostly commented out) to the BitBucket PteroDAQ repository.  If anyone reading this blog is familiar with the DMA interrupts on the K20 processor, please look at the code in kinetis_frequency.c and tell me what I’m doing wrong!

2015 August 25

PteroDAQ frequency channels on FRDM-KL25Z

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 23:53
Tags: , , , , ,

In PteroDAQ frequency channels I said

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.

I got about half of that done today.  I started out with how to grey out the frequency channels that were inaccessible (because another pin on that port was being used)—but doing that lead me to needing to list all the current channels, which was done in several different places already and was rather awkward.  From there I decided to clean up and refactor a lot of the GUI code, which had gotten rather long and confusing.

So most of the day was spent on refactoring the GUI, breaking it up into classes and getting rid of global variables.  I think that the GUI code is more maintainable  now, but there are still a few things that feel like they are in the wrong place in the code, so some stuff may move around a little in future releases.

After the refactoring, greying out the unavailable frequency pins was pretty easy, and I’m reasonably happy with the user interface for pin selection now, though I think I need a new icon for frequency measurement.  I have a period of a sine wave for analog channels, and a period of a square wave for digital channels, but I don’t have as obvious an icon for frequency channels.  (I was considering just a big “Hz”, but that would not look compatible with the sine and square wave icons.)

Adding frequency channels for the FRDM KL25Z board did not take long—the biggest trouble was converting the KL26 code for the Teensy LC into MBED’s format for accessing registers.  Just about everything else is the same (except that the KL25 has only 2 ports that can control the DMA counters: A and D, rather than the 3 (A, C, and D) of the KL26. I have pushed the code for the frequency channels up to the PteroDAQ site on BitBucket.

I don’t know whether I’ll get the Teensy 3.1 code done tomorrow—I have to go into the office for office hours and for the banana slug genomics meeting, and I need to start working on my biobibliography and teaching/research statements for my merit review.  They are due early in September, and I’ve not started on them yet.

I hope to have the frequency channels working on the Teensy 3.1 by the end of the week, so that I can rewrite the chapter of my book on the hysteresis oscillator, to use the frequency channels, rather than a separate frequency detecting program as I used for the last couple of years.

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 21

LED I-vs-V curves

Filed under: Circuits course,Data acquisition — gasstationwithoutpumps @ 18:41
Tags: , , ,

I was interested in looking at the forward voltages for the various LEDs that I have on hand, to see if there was an easy way to convert the single numbers given on the spec sheets into typical curves.

I used a very simple setup:

  • LED with series resistor (resistor size changed for different current ranges)
  • PteroDAQ (using Teensy 3.1) to record the voltage across the LED (A11) and across the resistor (A10-A11), 32x averaging 1kHz sampling.
  • JYEtech FG085 function generator to drive LED+ resistor (3.2Vpp, +1.6V offset, 1Hz)

I recorded a several seconds of data for each resistor size I used (47Ω, 150Ω, 1kΩ, 12kΩ, 220kΩ).  For a couple of the LEDs, I also tried 3.3MΩ, but the noise in the ADC for that high an impedance source was too much—I would have needed to add unity-gain buffers to be able to read the signal reliably.

I found that the differential inputs were rather noisy below about 100mV, so I only used the larger signals.  I should figure out how to use the programmable gain amplifiers on the Teensy 3.1 and integrate that into PteroDAQ, so that I could read small voltages more reliably. Here are the I-vs-V curves for the identifiable LEDs I had on hand (I have some other “grab-bag” LEDs that I didn’t bother measuring):

The "twinning " of the line for the orange LED at the high end probably comes from measuring different LEDs—I had not included the 47Ω in the first round of tests, and when I tested the orange LED again, I may have gotten a different one from the bag.

The “twinning ” of the line for the orange LED at the high end probably comes from measuring different LEDs—I had not included the 47Ω in the first round of tests, and when I tested the orange LED again, I may have gotten a different one from the bag.

The low-end of the I-vs-V curve for each resistor is noticeably noisier than the high end—even after I trimmed off the results with differential inputs lower than 100mV.  With the 220kΩ series resistor, the single-ended voltage measurement was poor—the high-impedance source causes problems removing the noise injected by the sampling circuitry. This is not such a problem with the differential measurement, since the differential amplifier provides a low-impedance source to the sampling circuitry.  There is a second differential channel on the K20 chip, but one of the pins for it is A13, which is only available as a surface-mount connection on the back of the Teensy board—not accessible for PteroDAQ.

The IR emitters have a much lower forward voltage, and a much higher maximum current (50mA instead of 25mA).  The IR LED has a 1.6V max and 1.2V typical specified forward voltage at 20mA—I got 1.199V at 20mA, which is closer that I have any right to get to the typical value, since I didn’t even measure the 47Ω nominal resistor.

Each of the non-IR LEDs has a slightly different forward voltage spec, but they all have a max forward voltage spec of 2.5V  at 20mA, and typical values ranging from 2V to 2.25V.  The green and red curves (the two on the right) were indeed the two LEDs with the highest typical values. None of the non-IR LEDs got up to high enough voltages in this test to see whether the forward voltage at 20mA was measured correctly.  I should be able to do that with a smaller series resistor.

Bottom-line:  the typical forward voltage at 20mA does give a fair idea of the I-vs-V curves, though the max current is also important for getting a good shape to the curve—the curve switches from straight-line exponential behavior to the gradual saturation at about 1/5th of the max current.

Next Page »

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 318 other followers

%d bloggers like this: