KL25z mbed SPI issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KL25z mbed SPI issue

1,537 Views
gchen
Contributor I

Hey guys,

     I'm new to the KL25z, and just picked it up and started programming it with the mBed libraries. There's one bug that I noticed, that I don't know how to fix. For some reason, when I program board to enable SPI communications with CPOL = 0 and CPHA = 1, the board doesn't adjust the phase correctly. The chip I'm interfacing with is the TI ADS1292, which communicates in that mode. When I interface the chip with an Arduino, it works just fine. Any suggestions on how I can fix this problem?

Thanks.

Tags (4)
0 Kudos
2 Replies

684 Views
c0170
Senior Contributor III

Hello George Chen,

best would be to write this question on mbed directly, where we can help you much faster.

Can you share code,how you enable SPI with CPOL = 1 and CPHA = 1? Please, start a discussion there. Thanks.

Regards,

0xc0170

0 Kudos

684 Views
gchen
Contributor I

Thanks for responding,

I'm using the SPI library in mbed, which has a function format where I can specify the clock speed and mode

mbed-SPI Code

lines 31-36

void SPI::format(int bits, int mode) {

    _bits = bits;

    _mode = mode;

    SPI::_owner = NULL; // Not that elegant, but works. rmeyer

    aquire();

}

lines 47-53

void SPI::aquire() {

     if (_owner != this) {

        spi_format(&_spi, _bits, _mode, 0);

        spi_frequency(&_spi, _hz);

        _owner = this;

    }

}

the function spi_format() called in aquire() is used to set up the registers for the clock speed and SPI mode.

from lines 95-113 in mbed-source code for spi_api.c

void spi_format(spi_t *obj, int bits, int mode, int slave) {

   if (bits != 8) {

  error("Only 8bits SPI supported");

  }

   if ((mode < 0) || (mode > 3)) {

  error("SPI mode unsupported");

  }

   uint8_t polarity = (mode & 0x2) ? 1 : 0;

   uint8_t phase = (mode & 0x1) ? 1 : 0;

   uint8_t c1_data = ((!slave) << 4) | (polarity << 3) | (phase << 2);

   // clear MSTR, CPOL and CPHA bits

  obj->spi->C1 &= ~(0x7 << 2);

   // write new value

  obj->spi->C1 |= c1_data;

}

This code appears to be working fine, and I have been able to properly set up SPI in mode 0 and mode 3. It just doesn't seem to work for mode 1. I typically get zeros from the chip.

Below is my code.

#include "mbed.h"

#define RREG_COMMAND    0x20 //read command

#define WREG_COMMAND    0x40 //write command

SPI spi(PTC6, PTC7, PTC5);  // mosi, miso, sclk

DigitalOut cs(PTD5); //chip select

DigitalOut afe(PTD0); //other SPI chip's chip select, set high throughout code to insure it doesn't interfere.

//Serial pc(USBTX, USBRX);

#define ID_ADDRESS          0x00 //register address to get ID of chip

#define CONFIG1_ADDRESS     0x01 //config 1 address

#define CONFIG2_ADDRESS     0x02 //config 2 address

#define LOFF_ADDRESS        0x03

#define CH1SET_ADDRESS      0x04

#define CH2SET_ADDRESS      0x05

#define RLD_SENS_ADDRESS    0x06

#define LOFF_SENS_ADDRESS   0x07

#define LOFF_STAT_ADDRESS   0x08

#define RESP1_ADDRESS       0x09

#define RESP2_ADDRESS       0x0A

#define GPIO_ADDRESS        0x0B

int read_register(int address) {

    cs = 0;

    wait(1);

    int firstCommandByte = RREG_COMMAND | (address & 0x1F);

    int secondCommandByte = 0x00;

    printf("Sending 1 bit: %d\n",firstCommandByte);

    printf("Sending 2 bit: %d\n",secondCommandByte);

    spi.write(firstCommandByte);

    spi.write(secondCommandByte);

    int registerVal = spi.write(0x00);

    printf("Result is %d\n",registerVal);

    wait(1);

    cs  = 1;

    return registerVal;

}

void write_register(int address, int value) {

    cs = 0;

    wait(1);

    int firstCommandByte = WREG_COMMAND | (address & 0x1F);

    int secondCommandByte = 0x00;

    spi.write(firstCommandByte);

    spi.write(secondCommandByte);

    spi.write(value);

    printf("Wrote %d in %d register\n",value,address);

   

    wait(1);

    cs = 1;

}

int main() {

    cs =1;

    afe = 1;

    // Setup the spi for 8 bit data, mode 1

    spi.format(8,1);

    spi.frequency(2000000);

   

    while(1) {

        int value = read_register(ID_ADDRESS);

        if (value == 115) {

            printf("SUCCESS!");

        }

        else {

            printf("%d\n",value);

        }

       

        value = read_register(CONFIG1_ADDRESS);

        printf("%d\n",value);

        wait(2);

       

     //check if it's an issue with the function

        cs = 0;

        wait_ms(1000);

        int firstCommandByte = RREG_COMMAND | (0x01 & 0x1F);

        int secondCommandByte = 0x00;

        printf("Sending 1 bit: %d\n",firstCommandByte);

        printf("Sending 2 bit: %d\n",secondCommandByte);

        spi.write(firstCommandByte);

        spi.write(secondCommandByte);

        int registerVal = spi.write(0x00);

        printf("Result is %d\n",registerVal);

        wait_ms(1000);

        cs = 1;

       

        write_register(CONFIG1_ADDRESS,0x01);

        int confVal = read_register(CONFIG1_ADDRESS);

       

       

       

    }

}

0 Kudos