|
|
View previous topic :: View next topic |
Author |
Message |
soonc
Joined: 03 Dec 2013 Posts: 215
|
Bad Example code Ex_Slave.c or what ? |
Posted: Mon Dec 29, 2014 9:45 pm |
|
|
I'm having problems understanding the Ex_Slave.c example given by CCS.
It's copyrighted so I can't publish all of it here, but I'll reference portions to clarify my questions.
The ssp_interrupt() uses a global variable named "address".
address is not initialize in main() or anywhere else as far as I can see.
There is also a global array named buffer[16].
In the ssp_interrupt() code
Code: |
if(state >= 0x80) //Master is requesting data
{
i2c_write(buffer[++address]);
} |
When I set a break point on i2c_write(buffer[++address]); the value for address is far greater than 16 !
How can this ever work ? I don't see it ever can work !
Is there a better i2c master (software) to i2c slave (hardware) ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 29, 2014 11:21 pm |
|
|
At the start of ex_slave.c it says this:
Quote: |
//// This program uses the PIC in I2C slave mode to emulate the
//// 24LC01 EEPROM. You can write to addresses 00h to 0Fh with it.
//// This program is to be used in conjunction with the ex_extee.c
//// sample. Use the "#include <2402.C>" or "#include <2401.c>".
//// Only 16 bytes of address space are implemented, however.
|
CCS only wants you to use the addresses 0 to 0x0F. Don't attempt
to read/write any higher address. I agree there is no range checking
on the address. It's just a cheap little demo. If you stay within the
limits that it says, it will work OK.
This little master program will write and read to the first address only:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3
Quote: | When I set a break point on i2c_write(buffer[++address]); the value for address is far greater than 16 ! |
The 'address' variable will be set if you use the 24LC01-type byte protocol.
It sends the eeprom address byte of 0 and 'address' will be set to that value.
See the link I gave above. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Dec 30, 2014 2:40 am |
|
|
Several of the examples are 'weak'. In ex_sisr for example, it is not explained that with the code as supplied, the buffer size must be 'binary' (2,4,8,16 etc.). There are lots of similar "oh dear" problems with examples, but most are enough to point in the right direction.
In this case, address is initialised from the I2C packet. The code does not error check, so if a wrong address is sent, 'splat'. The packet has to be laid out as if it were for a 2401 or 2402 EEPROM. The reason for the small size supported, is that the code was written to be useable on even the smallest chips. Given how little RAM some have, a 16byte limit makes sense.
There is always a 'confusing complexity' in I2C, between the 'device address', and the 'register address'. The standard I2C packet comprises:
Start
Device address (W)
Register address
Then for a write, the bytes to write
Or for a read, 'restart', device address (R), and bytes then read.
Followed by a stop.
The master should NACK the last byte being read on a read transaction.
This format is described in the I2C documentation, and reading this must be a starting point.
However key thing is that you must read the header of the example. As PCM_programmer points out, this tells you what driver to use for the master, and the address limitations.
Used as a pair with the 2401 driver, the code works. If you send it an invalid register address, then it fails. |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
Thanks |
Posted: Tue Dec 30, 2014 9:05 am |
|
|
Thanks for the explanation.
I got it all to work by adding my own code to do what was lacking without using the "other" files.
I'd prefer if an example has a main() then it should be complete. Reading example code is frustrating enough without switching from file to file.
All on one page makes more sense to me.
Anyway: PCM programmer's I2C scanner did the trick again...
Thanks everyone.
Happy New Year. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|