A powerful platform for tiny circuits

Using I2C and SPI interfaces together on mBuino

25 Sep 2014

Hello all!

I am trying to use the I2C and SPI interfaces at the same time. This isn't working well for me.

If I use one or the other everything is fine. But use both and lockup.

25 Sep 2014

Code Follows:

LCD Test App

#include    "mbed.h"
//#include    "DS3231.h"
#include    "ST7565_SPI_LCD.h"
//#include    "_24LCXXX.h"
#include    "BMP180.h"

//  Definition ------------------------------------------------------------------------------------

float delayTime = .1;
//I2C i2c(P0_5, P0_4);            // sda, scl

//  Object ----------------------------------------------------------------------------------------
    // LCD
        ST7565      lcd(P0_21,P1_15,P0_11,P0_15,P0_13, ST7565::AD12864SPI); // mosi, sck, reset, a0, ncs
    // RTC Chip
//        DS3231      rtc(P0_5, P0_4);
   // BMP180 Sensor
        BMP180      bmp(P0_5, P0_4);
    
//  RAM -------------------------------------------------------------------------------------------

//  ROM / Constant data ---------------------------------------------------------------------------
    // EEPROM on DS3231 Board
    //    _24LCXXX eeprom(&i2c, 0x57);

//  Main Program
int main() {

    lcd.cls();
    lcd.set_contrast(0x14);
    lcd.printf("Dan Brown / LM6059B\r\n" );
    lcd.printf("ABCDEFG 1234567890\r\n" );
    lcd.printf("W:%d  H:%d\r\n", lcd.width(), lcd.height());
   
    long temp = 1;
    long pressure = 2;
    int error = 0;

    while(0) {
        error = bmp.readTP(&temp,&pressure,OVERSAMPLING_ULTRA_HIGH_RESOLUTION);

        lcd.locate(0,36);
        lcd.printf("Temp is %ld\r\n",temp);
        lcd.locate(0,46);
        lcd.printf("Pressure is %ld\r\n",pressure);
        lcd.locate(0,56);
        lcd.printf("Error is %d\r\n\r\n",error);
        wait(2);
    } 
}

When I get this all working, I intend to have a clock with a few weather features. But I need to be able to talk with those sensors first.

Dan

25 Sep 2014

How far does your program run? And if you only comment line 18 and 40, then it does run? How do you know it runs without the LCD? (Do you print to USBSerial, blink an LED, etc?)

25 Sep 2014

Erik - wrote:

How far does your program run? And if you only comment line 18 and 40, then it does run?

Yes, commenting those line does allow it to run.

Erik - wrote:

How do you know it runs without the LCD? (Do you print to USBSerial, blink an LED, etc?)

The LCD is the only output ATM, so I'm not sure if I2C is working when by itself. I will have to setup USBSerial and test that way. Is there a preferred library to use?

25 Sep 2014

Just use the official USBdevice lib (http://mbed.org/users/mbed_official/code/USBDevice/) for that.

But an initial test you can do is already to include the lib and create it, and then in a loop blink an LED and read some values of the sensor. Then you can at least see if the code runs. A general way to lock up when using I2C is forgetting pull up resistors.

25 Sep 2014

Thanks Erik

There are pullups on the mBuino according to the schematics. Also, I believe both modules, the BMA180 and the RTC both have pullups. I have used the RTC without additional pullups on an Arduino that doesn't have any and it works there, so fairly certain it has them on board that module.

I will be testing more this evening and let you know.

Dan

26 Sep 2014

Well, I can talk to several different sensors via I2C with no problems, turn LEDs on and off and communicate via USBSerial.

But use the SPI interface and anything else (USBSerial, DigitalOut, I2C) and it locks up after one char is written to the LCD Screen via SPI. I've moved all the control pins to other pins and the screen by itself is ok. But still not with anything else. I've got about 20 #ifdef statements to allow different case tests. (I can upload new code if wanted.)

At this point I can only guess the issue is a Short on the board somewhere, or a bad library.

The BIN file is only at 20K with everything in it, so I'm not running out of FLASH space. I'm getting no errors/warnings with the compile.

Anyone having ideas is appreciated!

Dan

26 Sep 2014

With LCDTEST not defined, This code runs fine:

Test Code with USB Serial / RTC / Pressure

//  Defines ---------------------------------------------------------------------------------------
//#define LCDTEST
#define CLOCK_TEST
#define DIGOUT
#define BMPTEST
#define USBTEST

//  Include ---------------------------------------------------------------------------------------
#include    "mbed.h"
#ifdef CLOCK_TEST
#include    "DS3231.h"
#endif
#ifdef LCDTEST
#include    "ST7565_SPI_LCD.h"
#endif
//#include    "_24LCXXX.h"
#ifdef BMPTEST
#include    "BMP180.h"
#endif
//#include    "Arial28x28.h"
#ifdef USBTEST
#include     "USBSerial.h"
#endif
//  Definition ------------------------------------------------------------------------------------

float delayTime = .01;
//I2C i2c(P0_5, P0_4);            // sda, scl

//  Object ----------------------------------------------------------------------------------------
    // LED
#ifdef DIGOUT
        DigitalOut  myled0(P0_23);
        DigitalOut  myled1(P0_17);
        DigitalOut  myled2(P1_19);
        DigitalOut  myled3(P0_20);
        DigitalOut  myled4(P0_2);
        DigitalOut  myled5(P0_8);
        DigitalOut  myled6(P0_7);
#endif
    // LCD
#ifdef LCDTEST
        ST7565      lcd(P0_21,P1_15,P0_10,P0_15,P0_9, ST7565::AD12864SPI); // mosi, sck, reset, a0, ncs
#endif

    // RTC Chip
#ifdef CLOCK_TEST
        DS3231      rtc(P0_5, P0_4);
#endif

    // BMP180 Sensor
#ifdef BMPTEST
        BMP180      bmp(P0_5, P0_4);
#endif

    // Internal USB Interface
#ifdef USBTEST
        USBSerial pc(USBTX, USBRX); // tx, rx
#endif
//  RAM -------------------------------------------------------------------------------------------

//  ROM / Constant data ---------------------------------------------------------------------------
    // EEPROM on DS3231 Board
    //    _24LCXXX eeprom(&i2c, 0x57);

//  Function prototypes ---------------------------------------------------------------------------
#ifdef CLOCK_TEST
void drawDigitalTime(void);
#endif

//  Main Program
int main() {

#ifdef DIGOUT
    myled0 = 0;
    myled1 = 1;
    myled2 = 0;
    myled3 = 1;
    myled4 = 0;
    myled5 = 1;
    myled6 = 0;
#endif
#ifdef LCDTEST
    lcd.cls();
    lcd.set_contrast(0x14);
    lcd.printf("Dan Brown / LM6059B\r\n" );
    lcd.printf("ABCDEFG 1234567890\r\n" );
    lcd.printf("W:%d  H:%d\r\n", lcd.width(), lcd.height());
#endif
    long temp = 1;
    long pressure = 2;
    int error = 0;

    while(1) {
#ifdef BMPTEST
        error = bmp.readTP(&temp,&pressure,OVERSAMPLING_ULTRA_HIGH_RESOLUTION);
#endif
#ifdef CLOCK_TEST
        drawDigitalTime();
#endif
#ifdef USBTEST
        pc.printf("Temp is %ld\r\n",temp);
        pc.printf("Pressure is %ld\r\n",pressure);
        pc.printf("Error is %d\r\n\r\n",error);
        wait(2);
#endif
#ifdef DIGOUT
        myled0 = 1;
        wait(delayTime);
        myled0 = 0;
        myled1 = 1;
        wait(delayTime);
        myled1 = 0;
        myled2 = 1;
        wait(delayTime);
        myled2 = 0;
        myled3 = 1;
        wait(delayTime);
        myled3 = 0;
        myled4 = 1;
        wait(delayTime);
        myled4 = 0;
        myled5 = 1;
        wait(delayTime);
        myled5 = 0;
        myled6 = 1;
        wait(delayTime);
        myled6 = 0;
#endif
    }
}

#ifdef CLOCK_TEST
void drawDigitalTime()
{
    //SetTime(); // Call only once to set date and time on DS3231
    int date, month, year, hour, minute, second, dayOfWeek;

    rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
    year=year+100;
    
//    lcd.set_font((unsigned char*) Arial28x28);  // select the font

#ifdef LCDTEST
    if (date > 0) {
        lcd.locate(0,36);
        lcd.printf("%02i.%02i.%i  ",date,month,year);
        lcd.locate(64,36);
        lcd.printf("%02i:%02d\r\n",hour,minute);
    } else {
        lcd.locate(0,0);
        lcd.printf("Error read RTC\r\n");
    }
#endif
#ifdef USBTEST
    if (date > 0) {
        pc.printf("%02i.%02i.%i  ",date,month,year);
        pc.printf("%02i:%02d\r\n",hour,minute);
    } else {
        pc.printf("Error read RTC");
    }
#endif
}
#endif
26 Sep 2014

That display uses quite a bit of RAM, still it should fit. But not it works without the LCDTest included, and if everything except the LCDTest is not included, so only LCD? Then it works also? And not anymore when any other one besides LCDTest is also included?

Thats an irritating problem. You can have the constructors also in the main function, then either with USBSerial or with LEDs you can see how far your program runs, and on which line exactly it is stopping.