Author Topic: Bi directional port in a library example?  (Read 3174 times)

0 Members and 1 Guest are viewing this topic.

Zap

Bi directional port in a library example?
« on: October 31, 2011, 11:18:30 PM »

I am useing port 2 on my DIY wiring board as a bi directional port (bus) and works fine accessing it using a sketch such as below. (port 1 is used as the chip /latch select)

// function getiptbyte gets the data byte on the input latch
byte getiptbyte (){
  portMode(2, INPUT);       // flips the bus port to input
  portWrite(1, 0x80);       // sets the latch for the input buffer
  ipt = portRead (2);       // reads the data on the buss port
  portWrite(1, 0x00);        // turns off the input latch
  portMode(2, OUTPUT);      // resets the bus port to output
  return ipt;
}

However when I try to move this into a class library (As below) it does not read the data on the bus for some reason. Is this the correct way to manipulate the portmode in a libary? and is there any example librarys with a port being used as Bi-directional?

 // member: getiptbyte gets the data byte on the input latch
byte iWire::getiptbyte (){
  portMode(2, INPUT);       // flips the bus port to input
  portWrite(1, 0x80);       // sets the latch for the input buffer
  ipt = portRead (2);       // reads the data on the buss port
  portWrite(1, 0x00);       // turns off the input latch
  portMode(2, OUTPUT);      // resets the bus port to output
//  ipt = (0xAA);           // Used only to test that the return works.
  return ipt;
}


Thanks Zap.

bhagman

Re: Bi directional port in a library example?
« Reply #1 on: November 10, 2011, 11:54:52 AM »
This sounds like either some sort of compiler optimization, or something else going on in the compiler.

Have you got some way to check the ports (i.e. multimeter or oscilloscope) to see what is happening when you set the ports?

Can you provide a simple sketch to use (both with the library and without) so others can test it out?

Zap

Re: Bi directional port in a library example?
« Reply #2 on: November 14, 2011, 01:15:46 AM »
Yes it looks like compiler optimisation. I dug out the DSO and found a difference in the chip select timeing pulses comming out of port 1 ( PC7) in each case.

For a function in a sketch of only
  portWrite(1, 0x80);       // sets the latch for the input buffer
  portWrite(1, 0x00);        // turns off the input latch
the output pulse length is 1.56 us ( 25 clk cyc)

However for a lib call of the same (above),  the pulse length is 60-61 ns ( 1 clk cycle).

This shorter lib pulse has caused the latch not to be settled in time for the read. (there is also a PAL16V8B that does the binary to 1 of chip selection for the bus chips between the MCU and them)

I tried a 1 microsecond delay in the lib call to allow the latch to settle and this works OK as below.

  portWrite(1, 0x80);       // sets the latch for the input buffer
  delayMicroseconds(1);     // delay to let latch settle
  ipt = portRead (2);       // reads the data on the buss port
  portWrite(1, 0x00);        // turns off the input latch

Now that I know what the problem is,  how do I add a few Nop to the lib instead of the 1 us delay?

And Why is there such a big compiled difference ( 1 & 25 cycles) between the sketch and the lib?

Thanks Zap.


bhagman

Re: Bi directional port in a library example?
« Reply #3 on: November 14, 2011, 01:28:13 PM »
Can you run this on your sketch? (both of them)

WiringLocation/hardware/tools/avr/bin/avr-objdump.exe -h -S sketchname.elf > sketchname.lss

you will find the .elf file in the build folder of your sketch.

then compress (zip or whatever) the two .lss files and attach them to a post in here.

I want to have a look at what's going on.

Zap

Re: Bi directional port in a library example?
« Reply #4 on: November 16, 2011, 06:44:03 PM »
Brett, I had trouble at first but after copying all the cyg*.dll's to the tools\bin folder I got it to dump them.

Let me know if you need anything more,

 Zap.
 
« Last Edit: November 16, 2011, 08:33:27 PM by Zap »

Zap

Re: Bi directional port in a library example?
« Reply #5 on: November 16, 2011, 08:38:24 PM »

Brett, previous post has wrong file and cant remove it.
And due to forum file limits here is the .lss for the Lib

Zap

Re: Bi directional port in a library example?
« Reply #6 on: November 16, 2011, 08:39:29 PM »


Brett, the secon is the .lss for the function

Zap

Re: Bi directional port in a library example?
« Reply #7 on: December 07, 2011, 06:17:45 PM »
Brett , did you find anything with the files I supplied?

Thanks Zap.

bhagman

Re: Bi directional port in a library example?
« Reply #8 on: December 08, 2011, 01:58:35 PM »
I had a look at the files - unfortunately, for some reason, they don't have all the info I need.

Let's take another approach.

Can you provide the source code, and the controller that you're using - that way I can compile myself and have a look.

Zap

Re: Bi directional port in a library example?
« Reply #9 on: December 11, 2011, 10:28:02 PM »

I'm using the Atmeaga 128 @ 16mhz on home brewed 1v1 type of board.
 
The test sketch is attached, Note that to switch between function or lib for testing, comment out one of the first two lines in the main loop.

NB : Also I use the arduino liquidcrystal lib instead of the wiring one as I require 4 bit mode for my boards.(you could comment out anything not required for your tests)

Zap.