nimams
Posts: 20
Joined: Tue Oct 18, 2016 7:49 pm

SPI and Arduino

Thu Jun 01, 2017 4:17 pm

Hi,

I would like to make a communication between Arduino Uno or Micro and Raspberry PI. I decided to use SPI, and I have to use SPI.

I have a tested code on Raspberry PI and Arduino. However, for some reason the Arduino does not read anything from the PI.
I have checked all my wires and everything. I'm using a SparkFun logic converter to step up and down the logic.
The Arduino gets activated by PI, but it doesn't make any response. The SPI interrupt never never catch anything, although the TX LED remains ON which means the data are coming in.
I have posted my code below.
Thank you :)

#!/usr/bin/python

import time
import RPi.GPIO as GPIO
import sys
#cspin=8
#datapin= 26
#resetpin= 19
LCD_MOSI = 10
LCD_CLK = 11
LCD_CS = 8
LCD_MISO = 9

GPIO.setmode(GPIO.BCM)
GPIO.setup(LCD_CS, GPIO.OUT)
GPIO.setup(LCD_CLK, GPIO.OUT)
GPIO.setup(LCD_MOSI, GPIO.OUT)
GPIO.setup(LCD_MISO, GPIO.IN)
GPIO.output(LCD_CLK, 0) #SCK=0
GPIO.output(LCD_CS, 1) #CS=1

def readd ):
"""Send a single byte"""
if type(d)==type('a'):
d=ord(d)
GPIO.output(LCD_CS, 0) #CS=0
time.sleep(0.01)
for i in range(8):
if d&128 > 0:
GPIO.output(LCD_MOSI, 1) #MOSI=1
else:
GPIO.output(LCD_MOSI, 0) #MOSI=0
d = d<<1
GPIO.output(LCD_CLK, 0) #SCK=0
time.sleep(0.005)
GPIO.output(LCD_CLK, 1) #SCK=1
time.sleep(0.005)
GPIO.output(LCD_CS, 1) #CS=1

def dataSeq(t ):
"""Send a data sequence"""
for i in t:
read(i)



if __name__ == '__main__':
try:
while True:
time.sleep(5)
dataSeq('Hi\r')
print "ADC Result: "
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit(0)


#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;

void setup (void) {
Serial.begin (115200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
Serial.println ("HI");
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}

void loop (void) {
if (process) {
process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
}

KMyers
Posts: 35
Joined: Fri Nov 09, 2012 9:46 pm

Re: SPI and Arduino

Sat Jun 03, 2017 6:27 pm

Did you authorize spi in the raspi-config?



Ken












k

Smajdalf
Posts: 3
Joined: Mon Jun 05, 2017 4:54 pm

Re: SPI and Arduino

Mon Jun 05, 2017 4:59 pm

You should not use Serial.println in ISR. In Arduino by default in ISR all interrupts are disabled (and you should not enable them unless you know what you are doing). Serial.println should be interrupt driven and have a buffer but AFAIK it is bugged somehow - it waits for the interrupt even when the buffer is empty. So you use Serial.println in ISR -> program waits for interrupt while interrupts are disabled -> ...

nimams
Posts: 20
Joined: Tue Oct 18, 2016 7:49 pm

Re: SPI and Arduino

Wed Jun 07, 2017 10:07 pm

Thank you.
You suggestion was really great although it was not my main issue.

Return to “Advanced users”