MOD54415 SPI and I2C question

for everything else
jediengineer
Posts: 192
Joined: Mon Dec 17, 2012 6:24 am

MOD54415 SPI and I2C question

Post by jediengineer »

Hey all,

So I'm setting up my NB unit to drive an ADC, MUX, and DAC. one uses I2C, the others use SPI. Unfortunately, the examples available are insufficient to help me understand how to set up and run the SPI and I2C on the chip. Can someone give me a quick walkthrough, without semaphores... I haven't grasped that concept yet, and currently have my back to the wall timewise so I'm going to have to stick to basic C code for now. I know that I have to set all the SPI and I2C pins for their functions, but transferring data and receiving data are a bit lost on me. I also need to know and understand how to set the I2C speed, since one device runs at 100kHz and 400kHz, and the other at 1MHz, and they all have different sized input registers. Can someone shed some light? Thanks!!

Tony
jediengineer
Posts: 192
Joined: Mon Dec 17, 2012 6:24 am

Re: MOD54415 SPI and I2C question

Post by jediengineer »

Anyone?
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: MOD54415 SPI and I2C question

Post by rnixon »

I think your topic is too broad, no way to answer on a forum. How about if you try one device at a time, creating one program for each device at a time, do some experimentation, then as a specific question along with your schematic and code?
jediengineer
Posts: 192
Joined: Mon Dec 17, 2012 6:24 am

Re: MOD54415 SPI and I2C question

Post by jediengineer »

Thanks, that's a swell idea, but maybe a better way of asking my question would be this: I can't find the documentation on how to set up and address, and use the I2C and SPI. Can anyone point me in the right direction? The examples provided in the \nburn\examples folder were no help to me.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: MOD54415 SPI and I2C question

Post by rnixon »

The runtime library docs have a section on I2C. For sending data to an address the function is:

12.3.2. I2CSendBuf
Synopsis:
BYTE I2CSendBuf( BYTE addr, PBYTE buf, int num, bool stop = true );
Description:
This function sends a buffer to an address on the I²C bus in master mode without the need of a start and stop bit.

There's a lot more there than I can copy/paste here, including how to set the frequency in the init function. So the address is the first param in the function call.
versamodule
Posts: 14
Joined: Fri Jun 08, 2012 4:59 am

Re: MOD54415 SPI and I2C question

Post by versamodule »

This code i pulled from the Nano54415, should be the same for the MOD. I leave that up to you to check.
Here is what you can do. Dont forget to check that there is at least a 2.2k resistor on both lines to 3.3v (VERY IMPORTANT!!!)
If you want to know a bit more about a function, say like I2CStart, put your mouse over it, one left click then press F3.

Code: Select all

#1) Include the following.
 
#include <sim.h>
#include <pins.h>
#include <i2cmaster.h>
#define YOUR_CHIP_ADDRESS		(0x48)			// Manufacturer Address 


#2) First figure out what peripheral pins you are using for I2C. 
For me i am using I2c Channel 0. Maybe different for the MOD54415

	// Set up pins for I2C channel 0
	Pins[27].function(PIN_27_I2C0_SCL);
	Pins[29].function(PIN_29_I2C0_SDA);

#2) Initialize the I2C

	// Init the I2C
	// NOTE: the default is for I2C0 if you need it on another port,
	// Your going to have to dig into the code and change it.
        // The parameter is the speed
	I2CInit(0x3f);


Then you can write or read. 


// Here is a write 8bit example. 

	BYTE i2cStatus;

	i2cStatus =   I2CStart( YOUR_CHIP_ADDRESS , I2C_START_WRITE );
	i2cStatus += I2CSend( data );
	i2cStatus += I2CStop();


// Now a write 16bits Example. 

	BYTE i2cStatus;

	i2cStatus = I2CStart( YOUR_CHIP_ADDRESS , I2C_START_WRITE );
	i2cStatus += I2CSend( register_loc );
	i2cStatus += I2CSend( data );
	i2cStatus += I2CStop();


// Now read 16bits back Example. 

	BYTE i2cStatus;
	BYTE data[2];


	i2cStatus =I2CStart( YOUR_CHIP_ADDRESS , I2C_START_WRITE );
	i2cStatus +=I2CSend( 0x00 );
   	i2cStatus +=I2CRestart( YOUR_CHIP_ADDRESS, I2C_START_READ );
	// true also adds the stop bit
	// Some chips need it and some do not, for you to check.
	i2cStatus +=I2CReadBuf(YOUR_CHIP_ADDRESS, data, 2,true);		


        //Now your results will be in data. 
        // if you want to make it into a WORD then do this
	// again the device may spit out LSB or MSB first so you need to check that. 

       WORD result;

       result = (WORD)data[0] << 8;
       result += (WORD)data[1];


jediengineer
Posts: 192
Joined: Mon Dec 17, 2012 6:24 am

Re: MOD54415 SPI and I2C question

Post by jediengineer »

Thanks versamodule, I appreciate it!! As for the 2.2k resistor... pullup for the SDA? or for SDA and clock?

rnixon- thanks for the code snippet, I should be able to figure out the I2C from there.


If anyone has a snippet on usig SPI, that would be awesome. Again - if possible, nothing with semaphores. I've still yet to learn to use them, and unfortunately, now isn't the time... Thanks all!!
versamodule
Posts: 14
Joined: Fri Jun 08, 2012 4:59 am

Re: MOD54415 SPI and I2C question

Post by versamodule »

As for the 2.2k resistor... pullup for the SDA? or for SDA and clock?
yes

As far as SPI goes, lets do what rnixon suggested by starting a new post just for that.
User avatar
Chris Ruff
Posts: 222
Joined: Thu Apr 24, 2008 4:09 pm
Location: topsail island, nc
Contact:

Re: MOD54415 SPI and I2C question

Post by Chris Ruff »

Ahhhh Semaphores....

VERY fun and easy to use. So are mailboxes. It will cost you possibly 20 minutes to grok the fullness...

step 1. declare it globally:
OS_SEM ossemRuffo;


step 2: initialize it in some init-time spot in some task:
OSSemInit(&ossemRuffo,0);

step 3: wait on the semaphore in some task:

// this blocks until either the semaphore times out or is posted to
void RuffoHandler()
{
if (OSSemPend(&ossemRuff0,TICKS_PER_SECOND)==OS_TIMEOUT)
{
// print if you wish initially
iprintf("\ntimeout!!\n");
}
else
{
iprintf("\nI have been posted to!!\n");
}
}

step 4: post to the semaphore in an interrupt or in a task:

// this will make the waiting semaphore go off
OSSemPost(&ossemRuffo);

.......

Now re-init the semaphore and do it all again

......

Semaphores are great because the waiting task simply releases its context to the next task until the semaphore is posted-to. Use a timeout of 0 if you don't ever want the semaphore to timeout

Chris
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
versamodule
Posts: 14
Joined: Fri Jun 08, 2012 4:59 am

Re: MOD54415 SPI and I2C question

Post by versamodule »

I would agree with Chris 100%. If your not familiar with Semaphores yet, get it working the way you know. Then once you get that under your belt implement the instructions Chris has shown. It is the best way to go about it.
Post Reply