View unanswered posts | View active topics It is currently Tue Jul 29, 2014 4:05 pm






Reply to topic  [ 5 posts ] 
Cortex i2c [FTFY] 
Author Message
Rookie

Joined: Sun Sep 28, 2008 1:06 pm
Posts: 27
Post Cortex i2c [FTFY]
To my knowledge, Xander has not yet released his I2C implementation for the Cortex microcontroller. I do not like waiting, and my Cortex should be arriving any time now so I went ahead and threw together some C code (untested due to lack of Cortex!).

Code:
#pragma config(Sensor, dgtl1,  sda,              sensorDigitalOut)
#pragma config(Sensor, dgtl2,  scl,              sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#define I2C_DELAY_COUNT 300
#define READ  false
#define WRITE true

void i2c_delay() {
  for (int i = 0; i < I2C_DELAY_COUNT; ++i) {}
}

void i2c_set_lines(bool direction) {
  SensorType[sda] = (direction) ? sensorDigitalOut : sensorDigitalIn;
  SensorType[scl] = (direction) ? sensorDigitalOut : sensorDigitalIn;
}

void i2c_start() {
  i2c_set_lines(WRITE);
  SensorValue[sda] = 0;
  SensorValue[scl] = 1;
  i2c_delay();
  SensorValue[sda] = 1;
  SensorValue[scl] = 0;
}

void i2c_stop() {
  i2c_set_lines(WRITE);
  SensorValue[sda] = 1;
  SensorValue[scl] = 1;
  i2c_delay();
}

void i2c_send_bit(bool bit) {
  i2c_set_lines(WRITE);
  SensorValue[sda] = bit;
  SensorValue[scl] = 1;
  i2c_delay();
  SensorValue[scl] = 0;
}

void i2c_send_byte(unsigned char byte_to_send, bool should_send_start = false, bool should_send_stop = false) {
  if (should_send_start) {
    i2c_start();
  }
 
  unsigned char temp_byte = byte_to_send;
  for (int b = 0; b < 8; ++b) {
    /*
     * 0x80 is 10000000 in binary, here a bit mask is used to set
     * every bit besides the MSB to 0.  If the MSB is 1 then the &
     * logic will set our temporary bit to 1, otherwise 0.  The
     * temp byte is then shifted left to make the next bit the MSB.
     */
    bool bit = (temp_byte & 0x80);
    temp_byte <<= 1;
    i2c_send_bit(bit);
  }
 
  if (should_send_stop) {
    i2c_stop();
  }
}

bool i2c_read_bit() {
  i2c_set_lines(READ);
  int timeout = time1[T1] + 5; // 5mS timeout.
  while (SensorValue[scl] == 0 || time1[T1] > timeout); // Wait for clock to become high.
  return (bool)SensorValue[sda];
}

unsigned char i2c_read_byte(bool should_send_stop = false) {
  unsigned char temp_byte = 0;
  for (int b = 0; b < 8; ++b) {
    /*
     * The i2c protocol is specified as MSB first so the temp byte
     * is always shifted left.  The LSB is essentially ORed with
     * the read bit.
     */
    temp_byte |=  i2c_read_bit();
    temp_byte <<= 1;
  }
 
  if (should_send_stop) {
    i2c_stop();
  }
  return temp_byte;
}

void i2c_send_string(string value) {
  bool i2c_start;
  bool i2c_stop;
 
  for (int idx = 0; idx < strlen(value); ++idx) {
    i2c_start = (idx == 0);
    i2c_stop  = (idx == strlen(value) - 1);
    i2c_send_byte(strIndex(value, idx), i2c_start, i2c_stop);
  }
}

task main() {
  i2c_send_string("Testing!");
}


EDIT: Added i2c_send_string(), added a timeout to i2c_read_bit. Renamed i2c_write_* to i2c_send_*. I'm writing this as I want to use my old Vex .5 microcontroller as an I/O slave for the Cortex (extra motor ports yay).

It does not currently do acknowledgement. Anybody up for some peer review? ;)


Fri May 06, 2011 12:05 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: Cortex i2c [FTFY]
Hey BasicXman,

Take a look here: http://dl.dropbox.com/u/13282461/NXTCamVEX.zip. The common.h is pretty much a 1:1 representation of what's in the NXT's common.h. This driver was used for this: http://www.robotc.net/blog/?p=1179

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat May 07, 2011 2:46 am
Profile WWW
Rookie

Joined: Sun Sep 28, 2008 1:06 pm
Posts: 27
Post Re: Cortex i2c [FTFY]
mightor wrote:
The common.h is pretty much a 1:1 representation of what's in the NXT's common.h.


Thanks Xander, our implementations are quite similar (especially given that I refreshed my knowledge on I2C using the wikipedia page ;)).

My Cortex should be around Monday so I'll be doing some testing...I have to implement this library in easyC for my old .5 microcontroller (don't particularly want to pay for a license of RobotC for PIC when I already have easyC).


Sat May 07, 2011 11:10 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: Cortex i2c [FTFY]
Please note that my implementation has not been tested on a PIC, only Cortex.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun May 08, 2011 2:31 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: Cortex i2c [FTFY]
Oh and let me know what cool stuff you've made with it!

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun May 08, 2011 2:33 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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 post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.