Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add I2C library and example for CC2538 #677

Closed
wants to merge 24 commits into from
Closed

Add I2C library and example for CC2538 #677

wants to merge 24 commits into from

Conversation

medmig
Copy link

@medmig medmig commented May 14, 2014

Add one header file and one implementation file to use the I2C peripheral of the CC2538 platform.
Add a line in the CC2538 makefile to include the I2C library for compilation.
Also add an example to use the I2C library with two different sensors (TCN75 from Microchip and SHT21 from Texas Instruments).

This adaptation of I2C library was made by Mehdi Migault for the 4MOD Technology company, as part of an internship with the IT department of the "Institut Universitaire et Technologique" in La Rochelle.
Parts of the sources come from Contiki and Texas Instruments.

It include an example for the I2C library that work with two different I2C sensors, the TCN75 from Microchip and the SHT21 from Texas Instruments. It measure temperature and send the value over the serial port.

Mehdi added 12 commits May 13, 2014 12:02
This file contais the define and function declarations to use the I2C peripheral of the CC2538 platform
This file contains the implementation of functions declared in i2c.h to allow to use the I2C peripheral of the CC2538 platform.
Add the i2c.c file to the CONTIKI_CPU_SOURCEFILES for compilation. It allow to use the I2C library in other projects.
This file is an exemple of I2C usage for the CC2538 platform. It can be used with a TCN75 and/or a SHT21 sensor.
This file is the Makefile for the i2c-example project. It enable the low power mode for the CC2538.
This file set the default target to be the cc2538dk for the i2c-example.
Add a function to print float (with value and precision for parameters), for the SHT21 measure.
Remove the STARTUP_CONF_VERBOSE = 0 line, because implicit when CC2538_CONF_QUIET = 1
Use the DEFINES= macro to assign value to configuration variables instead of juste variable=value
Change the print_float function to display the two number at same time. It improve the reliability for displaying over serial port.
@bthebaudeau
Copy link
Member

This is a PR concurrent to #397. I have not compared these two PRs, but you will probably have to discuss with @areaarea in order to choose which parts of each PR are the best.

Regarding the print_float() function introduced here, it might be better to just fix float support in cpu/arm/common/dbg-io/strformat.c.

Mehdi added 8 commits May 16, 2014 10:19
Temperature and relative humidity measurement for cave use case
Temperature and relative humidity measurement for cave use case
@g-oikonomou g-oikonomou self-assigned this May 18, 2014
@ademirci
Copy link

ademirci commented Jun 3, 2014

I have tested this driver with CC2538 and MPU9150 setup. No problems so far
Thanks.

i2c_master_command(I2C_MASTER_CMD_SINGLE_RECEIVE);
while(i2c_master_busy()) {
}
if(temp = i2c_master_error()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line causes GCC warnings to be emitted when -Wextra is used, as the single equals in a conditional is in many causes a bug (but not this case). You can silence it by wrapping up the conditional in a second set of parenthes:

if((temp = i2c_master_error())) {

Mehdi added 4 commits June 5, 2014 11:32
Add parenthesis around assignement used  as truth value
Uncomment the line 81, which make sûr the passed value is a boolean, to prevent slave address error
Add the volatile keyword line 19, to prevent the compiler from optimizing the loop variable
Rewrite the i2c_master_set_slave_address function
@wangchu92
Copy link

For a beginer like I am, it is worth checking charpter 20 on http://www.ti.com/lit/ug/swru319c/swru319c.pdf

@dazhbog
Copy link

dazhbog commented Nov 12, 2014

I can confirm that this works for my custom board with an SHT21. Thanks medmig!

@wwhuang
Copy link
Contributor

wwhuang commented Nov 19, 2014

This code works pretty well on my custom cc2538 platform. However, I think it should be user configurable whether or not the internal pull up resistors are used. I know some i2c peripherals only work with external pull ups, but the rtc I use works with just the internal pull up resistors.

@g-oikonomou
Copy link
Contributor

Noted, thanks.

I think that with those positive confirmations we can merge this, even if it doesn't have everything that one might desire. I'll have a closer look as soon as I get a chance in view of merging this ultimately.

@dazhbog
Copy link

dazhbog commented Nov 19, 2014

@wwhuang That would be a nice addition, but its interesting what you say that about some peripherals. Are you sure its not just the value of the pull-ups? What is the value you use? Usually weak pull-ups result in corrupted data, especially when you have many devices on the bus.

@dazhbog
Copy link

dazhbog commented Jan 11, 2015

Hi medmig,

I have been using the code for a while, I was getting more resets from this code so I changed the following.

while(i2c_master_busy()) {}

to

uint8_t 
while_busy(void){
    uint16_t count     = 0;
    uint16_t count_max = 1000;
    while( (i2c_master_busy() && (count<count_max)) ){
        count++;
        clock_delay_usec(10);   
    }
    if (count>=count_max)
        return 1;
    else
        return 0 ;
}

Also I havent figured out how to send bytes to a device. Should it be something like this?

uint8_t
writeByte(uint8_t address, uint8_t reg, uint8_t val){
    i2c_master_set_slave_address(address, I2C_SEND);
    i2c_master_data_put(reg);
    i2c_master_command(I2C_MASTER_CMD_BURST_SEND_START);
    if (while_busy()) return I2CM_STAT_BUSBSY;
    i2c_master_data_put(val);
    if (while_busy()) return I2CM_STAT_BUSBSY;
    return I2C_MASTER_ERR_NONE;
}

Thanks!!

@medmig
Copy link
Author

medmig commented Jan 14, 2015

Hi @dazhbog ,

Sorry I'm late, and I agree with you, because the waiting loop is an active loop, the watchdog may reboot. I have no sufficient experience to say if it's a good thing to had a timeout, and if the way you do this is the good way, but I think it can be a good idea (even if I think that the waiting loop should be finished (the I2C should be free) before the watchdog reboot).

Also, there are differents way to send data to a device, depending on the number of bytes you want to send. I've written few examples, but if they didn't help you, I suggest that you read the TI CC2538's documentation to get more precisions about the differents way to send data. If you want to send more than 1 byte you will have to use the i2c_master_command function, combined with the I2C_MASTER_CMD_BURST_SEND_* defines. Otherwise, you can use the i2c_single_send function.
In fact, the method to use depends of your slave (if he is waiting for a STOP or not, etc.), and if you are waiting for data in response or not, etc. In your case, I think that you should add "i2c_master_command(I2C_MASTER_CMD_BURST_SEND_FINISH);" or "i2c_master_command(I2C_MASTER_CMD_BURST_SEND_CONT);" after the second "i2c_master_data_put".

I'm sorry if I'm confusing, but I'm not working on this project anymore, and I'm not an English native speaker. I'll try to answer faster if you have any other question.

PS : I'm glad this code was usefull for someone :)

@g-oikonomou
Copy link
Contributor

Hi, @medmig. We're looking at this now and it seems to work fine. We'll probably open a new PR based on this one since you are not working on this.

I see there is no license header in the .c file. Can you confirm that you're happy for this to be 3-clause BSD?

@medmig
Copy link
Author

medmig commented Feb 16, 2015

Hi, @g-oikonomou .
I confirm that I'm happy for this to be 3-clause BSD, and that someone works on it.

@bradjc
Copy link
Contributor

bradjc commented Mar 20, 2015

@g-oikonomou Has there been any progress on this? The CC2538 should probably have an I2C library, even if it's not perfect.

alignan added a commit to alignan/contiki that referenced this pull request Mar 25, 2015
alignan added a commit to alignan/contiki that referenced this pull request Mar 25, 2015
@g-oikonomou
Copy link
Contributor

#1005

alignan added a commit to alignan/contiki that referenced this pull request Mar 25, 2015
ioannisg pushed a commit to ioannisg/contiki that referenced this pull request Apr 18, 2015
TheGeorge pushed a commit to TheGeorge/contiki that referenced this pull request May 7, 2015
vsaw pushed a commit to tadodotcom/contiki that referenced this pull request Aug 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.