Community
cancel
Showing results for 
Search instead for 
Did you mean: 
idata
Community Manager
1,803 Views

Mraa SPI code causing a 'segmentation fault (core dumped)' ?

Hi,

Could somebody explain why this SPI transfer creates a 'segmentation fault (core dumped)' error when running on the Joule?

Thanks

# include "mraa.hpp"

# include

# include

int main(int argc, char** argv) {

mraa_spi_context spi;

spi = mraa_spi_init(0);

printf("Hello, SPI initialised\n");

uint8_t data[] = { 0x00, 100 };

uint8_t* recv;

while (1) {

int i;

for (i = 90; i < 130; i++) {

data[1] = i;

recv = mraa_spi_write_buf(spi, data, 2);

printf("Writing -%i", i);

printf("RECIVED-%i-%i\n", recv[0], recv[1]);

usleep(100000);

}

}

}

0 Kudos
18 Replies
FGold
New Contributor II
189 Views

Check the following:

  1. The function call "mraa_spi_write_buf()" will return a NULL pointer in cases of error. Check if (recv == NULL)
  2. Also the pointer return has to be free'd by the caller.

To simplify, I would recommend you not loop on this at first, and just do one instruction and make sure that passes successfully:

recv = mraa_spi_write_buf(spi, data, 2);

idata
Community Manager
189 Views

Hi conor100,

 

 

Thanks for your interest in the Intel Joule Platform.

 

 

The error "segmentation fault (core dumped)" is when a program attempts to access a memory location that it is not allowed to access, also it can be due to an infinite loop.

 

 

That said I would follow floydg advice to check your pointers and try not to use a loop in your code. Please let me know your results.

 

 

I will be waiting for your reply, have a nice day!

 

 

Best Regards,

 

-Jose.

 

FGold
New Contributor II
189 Views

Also I did not see the following in your code to initialize mraa "mraa_init()":

If this is the case the spi pointer is NULL, and should be tested, before being used.

1mraa_result_t rv;

2rv = mraa_init();

3if (rv != MRAA_SUCCESS)

4 <code class="plain" style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !import...
idata
Community Manager
189 Views

Thanks folks, I'll try your suggestions. Just to note that this code was just copy/pasted from the MRAA spi webpage:

https://iotdk.intel.com/docs/master/mraa/classmraa_1_1_spi.html https://iotdk.intel.com/docs/master/mraa/classmraa_1_1_spi.html

Also @floydg - are you sure 'mraa_init()' is required? It isn't called the the basic blink led example?

Conor

idata
Community Manager
189 Views

Hi conor100,

 

 

Did you already try to compile using the whole example code?

 

 

Please let me know, have a nice day!

 

 

Best Regards,

 

-Jose.

 

FGold
New Contributor II
189 Views

mraa_init() provides the following, and may aid with your issue in the scenario where initialization has failed:

Detects running platform and attempts to use included pinmap, this is run on module/library init/load but is handy to rerun to check board initialised correctly. MRAA_SUCCESS inidicates correct (first time) initialisation whilst MRAA_ERROR_PLATFORM_ALREADY_INITIALISED indicates the board is already initialised correctly

JJare1
Novice
189 Views

What version of MRAA version are you running?

I think I remember having some issues with SPI on versions prior to Version v1.5.1? if you haven't already, consider upgrading.

curl https://raw.githubusercontent.com/intel-iot-devkit/joule-code-samples/master/mraa-install.sh | sh

-J

FGold
New Contributor II
189 Views

suggestion - first verify your version:

$ mraa-gpio version

Version v1.5.1 on Intel GT Tuchuck

idata
Community Manager
189 Views

Hi there,

I can confirm that I'm running Version v1.5.1 on Intel GT Tuchuck and Mraa is initializing correctly. I can toggle the LEDs.

When I run the code below it writes the data to sdout and I assumed it would be writing data on the SPI_0_MOSI which is Pin 27 on J13. However, when I connect a logic analyser to P27 on J13, I see no activity.

Is this the correct way to initialize SPI 0 and write data on MOSI.

Thank you

mraa_spi_context spi;

spi = mraa_spi_init(0);

printf("Hello, SPI initialised\n");

uint8_t data[] = { 0x00, 100 };

while (1) {

int i;

for (i = 90; i < 130; i++) {

data[1] = i;

recv = mraa_spi_write_buf(spi, data, 2);

printf("Writing -%i", i);

}

idata
Community Manager
189 Views

Hi conor100,

Are you sure you are checking the right pin, looking at the datasheet, I don't see a Pin 27 assigned to a SPI input or output.

Joule datasheet: http://www.intel.com/content/dam/support/us/en/documents/joule-products/intel-joule-module-datasheet... http://www.intel.com/content/dam/support/us/en/documents/joule-products/intel-joule-module-datasheet... .

I hope this works, please let me know your results.

Have a nice day!

Best Regards,

 

-Jose.
idata
Community Manager
189 Views

Hi Jose,

On the expansion board connector pin 27 on J13 is listed listed as SPI_0_MOSI?

http://www.intel.com/content/www/us/en/support/boards-and-kits/intel-joule-kits/000022494.html http://www.intel.com/content/www/us/en/support/boards-and-kits/intel-joule-kits/000022494.html

I would of expected initializing SPI_0 would send data out on this pin?

Thanks

Conor

idata
Community Manager
189 Views

Hi Jose,

I cannot figure out why the code below will not write data out on the expansion board connector pin 27 on J13.

I have pin 27 (J13) connected to pin pin 29 (J13). 'mraa_spi_write_buff' returns a null pointer.

Am I doing something obviously wrong? Would you mind testing please?

Many thanks

# include "mraa.h"

# include

# include

int main(int argc, char **argv) {

mraa_init();

mraa_spi_context spi;

spi = mraa_spi_init(0);

printf("SPI initialised\n");

uint8_t data[] = {0x56, 0x00, 0x26, 0x00};

uint8_t *recv;

recv = mraa_spi_write_buf(spi, data, 4);

printf("Writing Data on SPI_0 MOSI 0x%X + 0x%X + 0x%X + 0x%X\n", data[0], data[1], data[2], data[3]);

if (recv == NULL) {

printf("Errors encountered when writing1\n");

}

else {

printf("RECEIVED 0x%X + 0x%X + 0x%X + 0x%X + 0x%X\n",recv[0], recv[1], recv[2], recv[3], recv[4]);

}

}

idata
Community Manager
189 Views

Hi conor999,

Could you be so kind to try this code:

# include "mraa.h"

 

# include

 

# include

 

int main()

 

{

 

mraa_init();

 

mraa_spi_context spi=mraa_spi_init(0);

 

mraa_spi_frequency (spi, 400000);

 

mraa_spi_mode(spi,MRAA_SPI_MODE0);

 

mraa_spi_lsbmode(spi,0);

 

mraa_spi_bit_per_word(spi,8);

 

uint16_t read_data = mraa_spi_write(spi,0xAA);

 

mraa_spi_stop(spi);

 

return MRAA_SUCCESS;

 

}

Please let me know if you still can't see anything in your logic analyzer.

Also I will test your code to see if I have the same issues as you.

Have a nice day!

Best Regards,

 

-Jose.
idata
Community Manager
189 Views

Hi Jose,

I tested your code and I still see no activity on the logic analyzer. I am testing pin 27 on connector J13.

What were the results of my code?

Thanks

Conor

idata
Community Manager
189 Views

Hi conor100,

I appreciate your patience. I have tested your example and the other two samples of SPI, one in python and the other in C. The three codes compiled successfully, I attached you the images below:

SPI of Conor100:

SPI in Python (https://github.com/intel-iot-devkit/mraa/blob/master/examples/python/spi.py mraa/spi.py at master · intel-iot-devkit/mraa · GitHub ):

SPI in C (That I provided in my previous reply):

Doing the tests, I noticed that mraa depending on the SPI interface that you selected. It assigns a set of pins, for example:

Bus 0 MOSI = 2 MISO = 4 CS = 6 CLK = 10

 

Bus 1 MOSI = 67 MISO = 69 CS0 = 59 CS1 = 61 CLK = 65

In the three codes above, we selected Bus 0: "mraa_spi_context spi=mraa_spi_init(0);". Looking at the pinout distribution in http://www.intel.com/content/www/us/en/support/boards-and-kits/intel-joule-kits/000022494.html Intel® Joule™ Expansion Board Breakout Definition for Linux* the pins 2, 4, 6 and 10; are in the Breakout 12 (J12).

Could you please test any of the codes above and check the signals in J12? Please let me know your results.

Have a nice day!

Best Regards,

 

-Jose.
idata
Community Manager
189 Views

Hi Jose,

Can you confirm that your OS is Ubuntu 16.04 LTS and not Ostro. I have tried your c and python examples. Both examples return NULL on 'mraa_spi_init(0)' and there is no activity on the pins. Has Mraa SPI been tested on Ubuntu 16.04?

The GPIOs work OK so there is no issue with my mraa installation.

Also you have reversed the MOSI and MISO pin numbers in your previous post. It should read:

Bus 0 MOSI = 4 MISO = 2 CS = 6 CLK = 10

Update: I can confirm that SPI works on Ostro. The same code does not work on Ubuntu.

Thanks

C

idata
Community Manager
189 Views

Hi conor100,

Thanks for the correction, now regarding if Mraa has been tested in Ubuntu 16.04,

 

unfortunately the scooped of our support is limited to the Ostro OS.

I will encourage you to contact the Ubuntu Community, maybe they will be able to assist you.

Ubuntu Community: https://developer.ubuntu.com/core/get-started/intel-joule Intel Joule | Developer

Have a nice day!

Best Regards,

 

-Jose.
idata
Community Manager
189 Views

Hi conor100,

 

 

Do you have any more questions? Please let me know in order to assist you.

 

 

I will be waiting for your reply, have a nice day!

 

 

Best Regards,

 

-Jose P.
Reply