Problem with Hardware SPI

08 Nov 2010

Hi

i wrote a small test programm, before porting my whole stack to my mbed. I tried it with the hardware SPI class first, it didn't work so i wrote a Software SPI for testing.

#include "mbed.h"

DigitalOut sel(p10);
DigitalIn irq(p9);
DigitalOut sdi(p11);
DigitalIn sdo(p12);
DigitalOut sck(p13);

void InitRFModule()
{
    sel.write(1);
    sdi.write(1);
    sck.write(0);

    wait(1);
    RFWrite(0x80D8);
    RFWrite(0x8201);
    RFWrite(0xA640);
    RFWrite(0xC647);
    RFWrite(0x94A0);
    RFWrite(0xC2AC);
    RFWrite(0xCA81);
    RFWrite(0xCED4);
    RFWrite(0xC483);
    RFWrite(0x9850);
    RFWrite(0xCC77);
    RFWrite(0xE000);
    RFWrite(0xC800);
    RFWrite(0xC040);
    wait(1);
}

short RFWrite(short b)
{
    char i;
    short temp = 0;

    sck.write(0);
    sel.write(0);
    
    for(i = 0; i < 16; i++)
    {
        if(b & 0x8000)
        {
            sdi.write(1);
        }
        else
        {
            sdi.write(0);
        }

        sck.write(1);
        temp <<= 1;

        if(sdo.read())
        {
            temp |= 0x0001;
        }

        sck.write(0);
        b <<= 1;
    }
    sel.write(1);
    return temp;
}

void RFSend(char b)
{
    while(irq.read());
    RFWrite(0xB800 + b);
}

void SEND()
{
    int i;
    char checksum;
    RFWrite(0x0000);
    RFWrite(0x8239);

    checksum = 0;

    RFSend(0xAA);    //Preamble
    RFSend(0xAA);
    RFSend(0xAA);
    RFSend(0x2D);    //Sync HI
    RFSend(0xD4);    //Sync LO

    for(i = 0; i < 16; i++)
    {
        RFSend(0x20 + i);
        checksum += (0x20 + i);
    }

    RFSend(checksum);
    RFSend(0xAA);
    RFSend(0xAA);
    RFSend(0xAA);
    RFWrite(0x8201);
}

int main() 
{
    InitRFModule();
    for(;;)
    {
        wait(1);
        SEND();
    }  
}
it worked well, and my receiver got all the data.

So now im trying to use the hardware SPI class. I added following code

SPI spi(p11, p12, p13);

//modified my RFWrite
short RFWrite(short b)
{
    short temp = 0;
    sel.write(0);
    
    temp = spi.write( b );

    sel.write(1);
    return temp;
}

int main() 
{
    //Added SPI Init to main method
    spi.format( 8, 0 );
    spi.frequency( 125000 );

    //...
}
bot the RF Module doesn't send anything, it doesn't even get initalized.
I cant find my mistake, mode 0 should be right (isn't it?).

can anyone help me out?

 

dominik

08 Nov 2010

Looks to me like your first test program is sending a short (16bit) value. But then with the Mbed library version you set it up to be 8bit, pass in a short, and then only send 8 bits?

09 Nov 2010

thanks for your answer.

you are completely right. i also tried it with 16... i just experimented a bit with the mode and the bits and it doesnt work with 16 or with 8 when i send two packages after another...

any other ideas?

regards

 

Dominik

09 Nov 2010

btw: should be (or get an stack) for the RFM12 you can buy at Pollin...

16 Nov 2010

Hello

I work with the RFM 12 too and use this functions. I have no Problems with it.

/

// Send Command to RFM12
int rfm_cmd(short int Wert ) {
    cs = 0;                         //select the Chip PIN 8
    wait( 0.001);                   //wait a little bit, or it will not work
    spi.write(Wert);
    wait(0.001);
    cs = 1;
    return  0;
}

//Read one byte from the RFM
unsigned char rfm_read() {
    unsigned char data;
    cs =0;
    data = spi.write(0xB000);
    cs = 1;
    return data;
}
Spi is like this

spi.format(16,0);
spi.frequency(2000000);

to use

rfm_cmd(0x0000);        //read Status

Hope i could help

Dieter