1 2 Previous Next 17 Replies Latest reply on Aug 13, 2015 4:01 AM by Frederic Philips

    MRAA SPI transmits wierd data

    Frederic Philips

      Hi guys,

      I am trying to send a 8-bit data through the SPI of Intel Edison and it seems to be working only that the correct data(simply 0b10101010) is not being transmitted.

      Instead some random bytes were seen, when I sniffed the data through the logic analyzer. I followed these examples: mraa/spi_max7219.c at ffe40033d4b03d8ceb227aa092142fc08be16ae8 · intel-iot-devkit/mraa · GitHub

      and mraa/spi_mcp4261.c at 9d488c8e869e59e1dff2c68218a8f38e9b959cd7 · intel-iot-devkit/mraa · GitHub

      Source code:

      (note: For my application, the Chip Select needs to be high during transmission, so I used a normal GPIO pin to do the job)

      #include "mraa.h"
      #include <unistd.h>
      #include <stdint.h>
      //SPI Clock frequency in Hz
      #define fSCLK 1000000
      void GPIO_Init(void);
      void SPI_Init(void);
      mraa_gpio_context SCS;
      mraa_spi_context spi;
      int main(int argc, char **argv)
          mraa_gpio_write(SCS, 1); //Select CS
          mraa_spi_write(spi, 0b10101010); //Send Data
          mraa_gpio_write(SCS, 0); //Deselect CS
      void GPIO_Init(void)
          SCS = mraa_gpio_init(7);
          printf("Ports initialized\n");
          mraa_gpio_dir(SCS, MRAA_GPIO_OUT);
          printf("Port directions set\n");
      void SPI_Init(void)
          spi = mraa_spi_init(0);
          if (spi == NULL) 
              printf("SPI initialization failed, check syslog for details, exit...\n");
          printf("SPI initialised successfully\n");
          mraa_spi_frequency (spi, fSCLK);
          printf("SPI clock frequency set to %iHz\n", fSCLK);
          CPOL = 0, CPHA = 0, 
          Clock idle low, data is clocked in on rising edge, output data (change) on falling edge 
          mraa_spi_mode(spi, MRAA_SPI_MODE0);
          printf("SPI set to MODE0\n");
          //MSB Transmission
          mraa_spi_lsbmode(spi, 0);


      Logic Analyzer Output:

      SPI mraa problem.png


      Thanks in advance!

        1 2 Previous Next