Report
4 years, 2 months ago.

SPI Nucleo and raspberry pi

Hi I am trying to use spi for Nucleo f401re I am using this code :

  1. include "mbed.h" SPISlave device(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); int main() {

device.format(8, 3); device.frequency(1000000);

device.reply(0x00);

int reply = 1;

while (1) { if (device.receive()) { int valueFromMaster = device.read(); device.reply(reply); reply++; } } }

I connect it to raspberry pi as master with this code :

import time import sys import spidev

spi = spidev.SpiDev() spi.open(0,0)

def buildReadCommand(channel): startBit = 0x01 singleEnded = 0x08

  1. Return python list of 3 bytes
  2. Build a python list using [1, 2, 3]
  3. First byte is the start bit
  4. Second byte contains single ended along with channel #
  5. 3rd byte is 0 return []

def processAdcValue(result): '''Take in result as array of three bytes. Return the two lowest bits of the 2nd byte and all of the third byte''' pass

def readAdc(channel): if ((channel > 7) or (channel < 0)): return -1 r = spi.xfer2(buildReadCommand(channel)) return processAdcValue(r)

if name == 'main': try: while True: val = readAdc(0) print "ADC Result: ", str(val) time.sleep(5) except KeyboardInterrupt: spi.close() sys.exit(0)

I supposed to get some value as reply in raspberry. I am confused what is wrong because I got nothing. I am sure my wiring is right. Thank you

Please repost using <<code>> and <</code> so that your code is readable.

posted by Andy A 05 Feb 2015
Comment on this question

1 Answer

4 years ago.

hey natan you got any solution for your problem i am facing same problem. Please reply .

I'm in the same too.

posted by BELABDI Malik 16 Apr 2015

To post an answer, please log in.