element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci Fi Your Pi
  • Challenges & Projects
  • Design Challenges
  • Sci Fi Your Pi
  • More
  • Cancel
Sci Fi Your Pi
Blog QuadCOP: ChipKit Pi and I2C, A Custom Block Protocol
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: screamingtiger
  • Date Created: 27 Aug 2015 4:59 PM Date Created
  • Views 1137 views
  • Likes 2 likes
  • Comments 4 comments
  • quadcop_project
  • design_challenge
  • i2c
  • i2c_issue
  • chipkit_pi
  • sci_fi_your_pi
Related
Recommended

QuadCOP: ChipKit Pi and I2C, A Custom Block Protocol

screamingtiger
screamingtiger
27 Aug 2015

Previous Posts Here:

http://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/authors/screamingtiger?ICID=DCH-SciFiPi-challengers

 

I am going to get into some technical details which hopefully isn't boring for the audience of this contest.  If anybody, it is for the judges as it is important to understand the amount of work I have put into this on a technical level.

 

I had mentioned in a previous post that the chip kit pi has some issues that make it different that an Atmel ATMega (Arduino).

The Wire library for arduino allows you to send and receive multiple bytes of data between one stop and start sequence.

However, the ChipKit Pi (Pic 32) has an issue doing this.

 

Because I am sending commands to the ControlSwitch from the RPFS via I2C, I need to keep command intact.  The commands consist of a controlbyte, a checkbyte, and other data up to 10 bytes.  I worry about missing a byte or getting out of sequence.  By using the wire library I though the built in I2C protocol would be suffice.  Alas, I can only send 1 byte at time.  This means that if I sent the ChipKit Pi 10 bytes, the OnReceive event handler will be called 10 times.  So I have to keep "track" of what is going on in order to completely process a command.  That is, there needs to be persistence between the event handler calls to keep track of the current status of a command to ensure all information is received and valid before doing what the command tells it to do.

 

So I had to get creative and created a block algorithm for this.  I used the block algorithm for all I2c communications except for the Magnetometer.

 

A high level general description is that a specific byte is sent that represents a block start.  Another specific byte is then sent as a stop byte.  The block algorithm does not allow repeated starts. That is that once a block is started, it must be specifically ended.  If another start byte is sent before a stop byte, the start byte is counted as data in the block.

 

 

The down side to this is that the stopbyte can never be used for data.  That is 1 value out of 256 values so I decided that is ok.  Actually there is another byte that cannot be used either, the resetbyte.

 

This is sent to reset the block status and let the receiver know a brand new block is coming in.  If the receiver is in the middle of receiving a block, that block is reset.

 

The resetbyte cannot be used as data either.  So 2 values out of 256 cannot be used.

 

So A block of data:

[StartByte] [data1..10] [StopByte]

 

Up to 10 bytes can be sent at a time.  The protocol is set so that way only 1 complete block can be had at one time.  That is, if the receiver has not processed the completed block, a new block cannot be sent.  To ensure this does not create a lock, the sender sends a reset block every time before sending a new block.

 

A quick flow chart of the block algorithm.

 

image

 

 

I apologize, the code display is not working well for me and stripping out my indentations.  I will work on fixing it later.

 

Here is a snapshot of the code that sends a block of data. This code is running on the RPFS (Raspberry Pi)

//Block Definitions

#define BLOCKSTART 204
define BLOCKSTOP 190
define BLOCKRESET 195



int SendBlock(int address,int *data,int count)

bool error = false;
//Send Block Reset

if(wiringPiI2CWrite(fd,BLOCKRESET) == -1)
error = true;

//Send Block Start

if(wiringPiI2CWrite(fd,BLOCKSTART) == -1)

error = true;

//Send the address

if(wiringPiI2CWrite(fd,address) == -1)

error = true;

 
for(int i=0;i<count && !error;i++)

if(wiringPiI2CWrite(fd,data[i]) == -1)
error = true;

if(wiringPiI2CWrite(fd,BLOCKSTOP) == -1)
error = true;


if(error)
return -1;
else
return 0;

Here is an example of how the to receive a block, one byte at a time.

 

 

//Block setup variables
bool blockStarted = false;

int block[10];
int blockCounter = 0;
bool blockCompleted = false;
bool blockBlock = false;
int blockSkipped = 0 ;

//For chipkit Pi, numBytes = 1 always
void I2CReceiveEventBlock(int numBytes)

{

//Every 2 bytes are our data pairs, writes come in groups of 3
unsigned char cb, cbc, reg;
cb = Wire.receive();
//If blockBlock is set we cannot get a new block until this one is processed
if(!blockBlock)
{
//Block Start
if(cb == 204)
{
blockStarted = true;
blockCounter = 0;
return;
}
//Block End
if(cb == 190)
{
if(blockStarted)
{
blockStarted = false;
blockCompleted = true;
blockBlock = true;
return;
}
}
//Block Reset
if(cb == 195)
{
blockStarted = false;

blockCompleted = false;
blockCounter = 0;
}
//Data
if(blockStarted)
{
block[blockCounter++] = cb;
blockCounter %= 10;
}
}
else

blockSkipped++;
}

 

When a block is completed, you can see a variable on line 41 that is set, blockCompleted = true.

In  the main loop of the code, you check this variable routinely and if it toggles to true, then you know you have a new block of data to process.

 

 

The ProcessBlock function in the whole code takes the data portion of the block and parses it out.  The first byte of the data portion is usually the "register" I wish to read or write to.  The important thing is that after a block is processed, that the variable blockBlock is set back to false so the event handler can resume getting a new block.  You can see on line 18 above if this variable is set to true, the protocol will start skipping bytes.

 

The files I2C.cpp and I2C.h contain the sending protocol.  The receiving protocol is part of the ControlSwitch and is embedded in that code.  It is also embedded into the Arduino code.

 

ToDo:  Make this a library that can be used for both the ChipKit Pi, and the Arduino.

  • Sign in to reply

Top Comments

  • balearicdynamics
    balearicdynamics over 10 years ago in reply to screamingtiger +1
    Joey, your approach - I mean - is more than correct! The point is that it is not to complain that this had to be developed, it is the "just plug and it will work" that has represented a bad habit. I…
Parents
  • balearicdynamics
    balearicdynamics over 10 years ago

    Hi Joey,

     

    in my personal opinion, the problem of receiving multiple bytes at a time is another storture of the Arduino behaviour. I prefer, especially when working with low level devices based on microcontrollers, to have the full control of the engine; there is probably the disadvantage of loosing some simplifications but the possibility to develop the parts exactly as they are needed I think it has no price.

    Take in account that most of the things that seems "just a joke" with Arduino libraries it is not because the AVR 8 bit slow micro is better than the 80 MHz more powerful PIC microcontroller but because someone though (no comment) to the Arduino developers creating a sort of ecosystem based on the excessive simplification. I repeat, this is my personal opinion but I had proved this by experience many times: every time I had to develop something serious with the AVR micro I had to ignore the libraries and in the better case rebuild or hack one and in the worst write down real code.

     

    Without forgetting that the entire concept of the Arduino IDE - as I have already discussed with phoenixcomm - is far from the concept of programming and put so many limiations to be unusable for serious projects like your.

     

    ToDo:  Make this a library that can be used for both the chipkit, and the arduino.


    Ti hink that making an unified library (this or any other, included those already existing) for both the devices is not the worth; IMHO this means a downsizing of the features for sure.


    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • balearicdynamics
    balearicdynamics over 10 years ago

    Hi Joey,

     

    in my personal opinion, the problem of receiving multiple bytes at a time is another storture of the Arduino behaviour. I prefer, especially when working with low level devices based on microcontrollers, to have the full control of the engine; there is probably the disadvantage of loosing some simplifications but the possibility to develop the parts exactly as they are needed I think it has no price.

    Take in account that most of the things that seems "just a joke" with Arduino libraries it is not because the AVR 8 bit slow micro is better than the 80 MHz more powerful PIC microcontroller but because someone though (no comment) to the Arduino developers creating a sort of ecosystem based on the excessive simplification. I repeat, this is my personal opinion but I had proved this by experience many times: every time I had to develop something serious with the AVR micro I had to ignore the libraries and in the better case rebuild or hack one and in the worst write down real code.

     

    Without forgetting that the entire concept of the Arduino IDE - as I have already discussed with phoenixcomm - is far from the concept of programming and put so many limiations to be unusable for serious projects like your.

     

    ToDo:  Make this a library that can be used for both the chipkit, and the arduino.


    Ti hink that making an unified library (this or any other, included those already existing) for both the devices is not the worth; IMHO this means a downsizing of the features for sure.


    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • screamingtiger
    screamingtiger over 10 years ago in reply to balearicdynamics

    Whether or not the I2C allows for multi byte sends would require research on the specific protocol.  Regardless, the protocol is needed for any project that needs to have a chunk of data sent over.

     

    Protocols exist for serial communications for this very reason.  You must have a way to send chunks of data, and validate those chunks.  That is the purpose of this code and it would be worthwhile to port over to any system using I2C that needs to send complex  or compound data.  The main purpose of the library is for maintainability reasons.  Right now I have two copies of the code on 2 different devices and I would rather just have 1 copy that is used by both devices.

     

    In the case of a stream concept, like for IC to IC, it probably is not needed and the overhead would be too much.  But for my purpose it was required!

     

    I agree Arduino promotes ignorance.  But if the ChipKit Pi is suppose to be Arduino "compatible" then they have failed.

     

    You should join up with the ChipKit forum, as Arduino is a bad word over there  image  I got beat up on there just like you beat me up LOL.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to screamingtiger

    Joey,

     

    your approach - I mean - is more than correct! The point is that it is not to complain that this had to be developed, it is the "just plug and it will work" that has represented a bad habit.

     

    I agree Arduino promotes ignorance.  But if the ChipKit Pi is suppose to be Arduino "compatible" then they have failed.

     

    This is a wrong assumption that probably they have used to promote and breach an almost close market of the Arduino fanatics. But I have never found something more different. And this is the real added value of the ChipKit. In my vision the ChipKit class of boards is the quality jump that was missed until now to introduce seriously and in a reliable way the PIC micro in the world of the experimenters. Arduino is already on the descending curve. Despite how much they are doing to re-animate it.

     

    Enrico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube