Category Archives: Microprocessors

RPI I2C interface to Microchip embedded uP – Clock stretching

If you are going to implement I2C to an embedded uP read this first http://www.slate.com/blogs/bad_astronomy/2016/03/28/psychedelic_stroboscopic_easter_eggs.html

If you are using a Microchip uP you might want to continue reading.

Here is the problem, Raspberry Pi’s I2C software can’t cope with slave clock stretching, not a problem if you can service the data request quickly, well you would have thought not anyway. Unfortunately Microchip’s hardware (using a PIC18F14K50 and many others)  always puts in a small clock stretch for a slave transmit, even if you disable clock stretching with the SEN bit.   A thorough read of the data sheet gives you a clue “The ACK pulse will be sent on the ninth bit and pin SCK/SCL is held low regardless of SEN“.

So data transmission TO the PIC works fine (if you can deal with the I2C data quickly enough), but for Slave data transmit TO the RPI you get errors. The problem is, they are random and depend on the exact timing of the I2C bus and uP speed.

This clock stretch only happens after the address byte and before the transmission data. It causes a short or missing clock pulse (as the RPI ignores the presence of the clock held low by the slave) as in the transition of bytes 3 &4  of data in the oscilloscope capture below.

DS1Z_QuickPrint7

So how do we work around this, well to be honest there isn’t a total solution. (that I’ve found) the best that I have achieved is to change the I2C clock speed  so the timing of a clock stretch happens when the clock is already low and tweak the uP interrupt software.

The clock speed change is unfortunately a trial and error process whilst monitoring the data (and probably watching with a scope). To change the I2C bus speed with a newer PI distro, you need to edit the /boot/config.txt and edit or add the line “dtparam=i2c1_baudrate=clockspeed” try clock speeds from 20000 to 400000 and follow by a reboot of the PI to make the changes active.

The bus clock speed that will work will vary with the microprocessor speed,  so there is no single solution. With my setup 200Khz and 50Khz works well (uP clock @32Mhz) where 400khz and 100khz is a total wright off.

I have also changed the I2C code in the PIC18F14K50 to give a fast set of the  BF flag after data transmission which helps minimise the clock hold time.

[code]]czoyMTpcImlmIChQSVIxYml0cy5TU1BJRikge1wiO3tbJiomXX0=[[/code] [code]]czoxMTpcIsKgwqDCoMKgIMKgXCI7e1smKiZdfQ==[[/code] [code]]czo0MjpcIsKgwqDCoMKgwqDCoMKgIGlmICghU1NQU1RBVGJpdHMuRF9OT1RfQSkge1wiO3tbJiomXX0=[[/code] [code]]czoyNTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy9cIjt7WyYqJl19[[/code] [code]]czozOTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy8gU2xhdmUgQWRkcmVzc1wiO3tbJiomXX0=[[/code] [code]]czoyNTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy9cIjt7WyYqJl19[[/code] [code]]czo0MjpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgaTJjX2J5dGVfY291bnQgPSAwO1wiO3tbJiomXX0=[[/code] [code]]czo0NDpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgaWYgKFNTUFNUQVRiaXRzLkJGKSB7XCI7e1smKiZdfQ==[[/code] [code]]czo1NTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCAvLyBEaXNjYXJkIHNsYXZlIGFkZHJlc3NcIjt7WyYqJl19[[/code] [code]]czo2NTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCBzc3BCdWYgPSBTU1BCVUY7wqDCoMKgIC8vIENsZWFyIEJGXCI7e1smKiZdfQ==[[/code] [code]]czo0ODpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCBTU1BTVEFUYml0cy5CRj0xO1wiO3tbJiomXX0=[[/code] [code]]czoyNDpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgfVwiO3tbJiomXX0=[[/code] [code]]czo0OTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgaWYgKFNTUFNUQVRiaXRzLlJfTk9UX1cpIHtcIjt7WyYqJl19[[/code] [code]]czo2NjpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCAvLyBSZWFkaW5nIC0gcmVhZCBmcm9tIHJlZ2lzdGVyIG1hcFwiO3tbJiomXX0=[[/code] [code]]czo1MjpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCBTU1BDT04xYml0cy5XQ09MID0gMDtcIjt7WyYqJl19[[/code] [code]]czo4ODpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCBTU1BCVUbCoMKgwqDCoMKgwqDCoMKgwqDCoCA9IGkyY19yZWdfbWF7WyYqJl19cFtpMmNfcmVnX2FkZHIrK107XCI7e1smKiZdfQ==[[/code] [code]]czoyNzpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCDCoFwiO3tbJiomXX0=[[/code] [code]]czoyNDpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgfVwiO3tbJiomXX0=[[/code] [code]]czoyMzpcIsKgwqDCoMKgwqDCoMKgIH0gZWxzZSB7XCI7e1smKiZdfQ==[[/code] [code]]czoyNTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy9cIjt7WyYqJl19[[/code] [code]]czozNjpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy8gRGF0YSBieXRlc1wiO3tbJiomXX0=[[/code] [code]]czoyNTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy9cIjt7WyYqJl19[[/code] [code]]czo0MDpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgaTJjX2J5dGVfY291bnQrKztcIjt7WyYqJl19[[/code] [code]]czo0NDpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgaWYgKFNTUFNUQVRiaXRzLkJGKSB7XCI7e1smKiZdfQ==[[/code] [code]]czo2NTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCBzc3BCdWYgPSBTU1BCVUY7wqDCoMKgIC8vIENsZWFyIEJGXCI7e1smKiZdfQ==[[/code] [code]]czozMTpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgwqBcIjt7WyYqJl19[[/code] [code]]czoyNDpcIsKgwqDCoMKgwqDCoMKgwqDCoMKgwqAgfVw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omXX0=[[/code] [code]]czo1NTpcIsKgwqDCoMKgwqDCoMKgIC8vIExpbWl0IGFkZHJlc3MgdG8gc2l6ZSBvZiByZWdpc3RlciBtYXBcIjt7WyYqJl19[[/code] [code]]czo1MTpcIsKgwqDCoMKgwqDCoMKgIGkyY19yZWdfYWRkciAlPSBzaXplb2YoaTJjX3JlZ19tYXApO1wiO3tbJiomXX0=[[/code] [code]]czoyNTpcIsKgwqDCoMKgwqDCoMKgIC8vIEZpbmFsbHlcIjt7WyYqJl19[[/code] [code]]czo4NzpcIsKgwqDCoMKgwqDCoMKgIFBJUjFiaXRzLlNTUElGwqAgPSAwO8KgwqDCoMKgwqDCoMKgwqDCoMKgwqAgLy8gQ2xlYXIgTVN7WyYqJl19U1AgaW50ZXJydXB0IGZsYWdcIjt7WyYqJl19[[/code] [code]]czoxMTpcIsKgwqDCoMKgIMKgXCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIsKgwqDCoCB9wqDCoMKgXCI7e1smKiZdfQ==[[/code]

With these changes, I have got (as the adverts say) up to 100% TX from a slave without error. This is about the best we can do until either Microchip change their hardware, or RPI write a I2C handler that supports clock stretch.

 

Headless Raspberry PI (PIZero) setup

The PIZero Simple setup without network or monitor. (Requires some prior knowledge of RS232 adapters and serial port setup)

First start by getting a PI SD image I used Raspian Jesse Light https://www.raspberrypi.org/downloads/raspbian/

Install into a sd card and make sure it runs (green light at least)

With a 3.3v RS232 adapter connect earth and RX/TX to your PI. Be careful some adapters are only 5V and will damage your PI’s UART pins.

PiZero

Setup your PC (I use Tera Term) serial adapter for 115200, 7 bits, even parity, 1 stop bit.

Boot your PI

If you get a screen of gobbledegook then good, otherwise swap your TX and RX around. When the gobbledegook has finished, press return a couple of times, and you should get a login prompt.

For some reason the console and boot up output at different baud rates (Caused by auto baud rate detect, settings above amended so this shouldn’t happen now)

Configure your PI the usual way. You will need some network to get your updates, but apart from that you have a working PI.

If you are brave (or idle) you can connect the +5v line from your serial adapter to power your PI too so you don’t need another usb power lead. Just be very careful that you attach it to the correct pins, otherwise your PI will be damaged.

 

Useful links.

(although I found the baud rate setting wrong everywhere I looked. Either auto baud rate detect at work, or it changes from distro to distro)

Element 14 no display using the raspberry pi serial console

RPi Serial Connection

 

Update:

The baud rate is auto detected, the native speed is 115200 (amended above) The auto detection only happens at the login prompt. The auto detection won’t pick up changes in bits/stop bits/ or parity. You can change the console getty port/speed/parity settings look at RPi serial connection above.

WiFi

If you have a wifi adapter and micro USB to USB lead, you can connect to WIFI. After connecting via RS232 edit the file [code]]czozOTpcIi9ldGMvd3BhX3N1cHBsaWNhbnQvd3BhX3N1cHBsaWNhbnQuY29uZlwiO3tbJiomXX0=[[/code] using sudo nano

add in the lines

[[code]]czoyNDg6XCJuZXR3b3JrPXsNCiAgICBzc2lkPVwiWW91cl9XSUZJX1NTSURcIg0KICAgIHBzaz1cIllvdXJfd2lmaV9wYXNzd29yZFwiDQp9e1smKiZdfQ0KDQphbmQgcmVib290IHRoZSBwaSBzdWRvIHNodXRkb3duIC1yDQoNCnJlIGxvZyBpbiB0byB0aGUgcGkgYW5kIHR5cGUgaWZjb257WyYqJl19ZmlnIHRvIGZpbnMgdGhlIHdpZmkgSVAgYWRkcmVzcy4gDQpUaGVuIHlvdSBjYW4gc3NoIHRvIHlvdXIgUEkgYW5kIGV2ZW4gYmV0dHtbJiomXX1lciB1cGRhdGUgdGhlIHNvZnR3YXJlLg0KDQpcIjt7WyYqJl19[[/code]]
Note : for Pi 3. You may need some changes to get the headless conf working, especially at non default baudrates, take a look at http://www.briandorey.com/post/Raspberry-Pi-3-UART-Boot-Overlay-Part-Two

 

 

 

 

 

 

 

Minimus 32 Dev Board – LUFA drivers and Atmel Studio 6.1

So I bought a Minimus32 Development board from [shortlink url=”http://www.modtraders.co.uk/minimus-32-avr-usb-development-board.html” title=”Mod Traders”]

minimus32

Couple of reasons for the purchase, Its was cheap, and it had a small processor and USB support out of the box.

OK, 1 and 2 are true, 3 is a little more complex.

After trawling the internet All I found was out of date information, information on the original Minimus board, or information using an old version of Atmels IDE. Having struggled in the past with old IDE’s I decided to use the newest one and brave it out.

So, For Windows…

You can download Atmel’s latest IDE free from here [shortlink url=”http://www.atmel.com/microsite/atmel_studio6/” title=”http://www.atmel.com/microsite/atmel_studio6/”] It is a Visual Studio based IDE and looks pretty good. Unfortunately there is no support for the bootloader contained in the minimus32 board.

So to get a hex image onto the Minimus 32 you need Flip downloadable for free here [shortlink url=”http://tinyurl.com/7jqeg3l” title=”http://www.atmel.com/tools/FLIP.aspx”]

Braving the new environment I cut and pasted a demo code sample (blink )


#include <avr/io.h>
#define F_CPU 16.000E6
#include <util/delay.h>
#include <avr/wdt.h>

void init_ports(void)
{
PORTB = 0b00000000;
DDRB = 0b00000000;

PORTC = 0b00000000;
DDRC = 0b00000000;

PORTD = 0b00000000;
DDRD = 0b01100000;      // Set LEDs as output
}

int main(void)
{
MCUSR &= ~(1 << WDRF);
wdt_disable();
init_ports();

while(1)
{
if (PIND & 0b10000000) {
PORTD = PORTD & ~0b01000000;
PORTD = PORTD |  0b00100000;
_delay_ms(200);

PORTD = PORTD & ~0b00100000;
PORTD = PORTD |  0b01000000;
_delay_ms(200);
} else {
PORTD = PORTD | 0b01100000;
}
}
}

I set the uP to ATMEGA32U2 hit build and It compiled first time.

Went into Flip, loaded the .hex file and sent it to the Minmus32. After a little playing with the buttons it ran.

(Tip: to put the Minimus into “Load” mode you need to press RESET, press HWB, release RESET and release HWB, just roll your finger over the buttons)

So a USB example.

I found that you can get the latest version of the LUFA libraries from here [shortlink url=”http://tinyurl.com/mx8flmo” title=”http://gallery.atmel.com/Products/Details/47ba24a4-d17b-4fed-b244-f5998b3d789d”] and the are already formatted for Studio 6

Loaded the Libraries and tried the demo project for USB to RS232

It compiled, but wouldn’t run.

Cutting a long story short there were a number of things wrong. The Processor speed was incorrect, the target board wasn’t set correctly and the LUFA libraries didn’t contain the code specific to the Minimus32 Board.

So. to fix these.

In Atmel Studio 6, Goto Project->Toolchain -> AVR/GNU C Compiler -> Symbols

Ensure they look like this

F_CPU=16000000UL
F_USB=16000000UL
ARCH=ARCH_AVR8
USE_LUFA_CONFIG_HEADER
BOARD=BOARD_MINIMUS
USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"

Tesla Coil Tuner

First light of a Tesla Coil tuner.

Old TC TunerSo I needed to get a better Tesla coil Tuner. I have been using this NE555 pinger for 10+ years.

Always said I’d put it in a box.. Never happened…

With some of the tuning problems I’ve had recently I needed a better solution.

So..

 

 

 

 

 

PIC powered USB controlled Tesla Coil Tuner using AD9850 DDS Frequency Synthesis.

Built on a PIC low pin count, USB development board (hence redundant RS232 connector)

Direct connectiontctuner to the Primary Coil with current measuring.

Three ways of measuring.

Primary and cap on its own(Secondary removed)

Secondary on its own (driven by primary with no Primary cap)

Primary Cap and Secondary (across spark gap) – Should see both current dips.

 

Tesla Coil Tuner - sec only

First plot of a small Secondary coil, using VB to drive the USB.

Plots at ~15 seconds for 1000 points (e.g will scan from 500Khz to 1.5Mhz in 1khz steps in ~15 seconds.)

 

Range 50Khz to 4Mhz (with some droop at each end)

I’m having some issues getting a plot with just a primary coil, not sure why at the moment. Also my driver (tc428) is damaged, its not driving correctly to ground. Which may be causing me some issues. But not bad for a first go.

More developments

Added an Ariel input (B)

Improved the display, and removed a whole load of noise on the traces.

Also, added the ability to take snapshots of the traces (pale colours) so you can see any change from scan to scan

Tesla Coil Tuner - sec-pri
This shows a current scan in Dark blue, with a snapshot in light blue and the Ariel input in dark green with a light green snapshot. The current scan was done with my hand near the topload hence the lower resonant frequencies.

This clearly shows the primary (C & L) as a large dip on the left and the secondary / topload as the smaller dip on the right. I’m feeding the primary coil and primary cap in parallel as this gives the best indication of the current change in the primary. For some reason driving the coil with Cpri in series shows a lumped resonance (or doesn’t show the primary resonance up at all)