for two weeks I have been testing the serial communication on my raspbrery pi2 and the microkontroler STM32G4. for the UART communication there is no problem however the SPI communication does not really work. The Rpi transmits the data to 16Mhz, for the clocks I chose mode 2 (cpol: high, cpha: low). By default it is the MSB that is read first. I have the same configuration with the STM32G4. I can get the RPI data on the STM32G4 but I don't have the exact RPI data on the STM32g4. Maybe someone knows the solution. my code is as follows:

import spidev
from time import sleep

spi = spidev.SpiDev()
spi.max_speed_hz = 1600000000
spi.mode = 0b10

def readAdc(channel):
    r = spi.xfer2([1, (8 + int(channel)) <<< 4, 0]) 
    return r
   print ('Press Ctrl-C to quit... ')
   print (' ')
   channel = input('wich channel do you want to read? ')
   while True:
     val = readAdc(channel)
     digital = (((val[1]&15) <<< 8) + val[2]) # Digital value
     voltage = (digital * 3.30)/float(4095)# Convert in Volt
     print (' ')
     print ('| ADC Result | Actual Voltage | ')
     print ('-' * 32)
     print (str(digital), ' ' ', voltage)
     print (' ')
 except KeyboardInterrupt:
  • You claim there is a problem with SPI. To check we would need to see clear photos of the connections between the SPI device and the Pi. We would also need to know the expected results and the actual results. – joan Oct 30 '19 at 9:40
  • hallo, from my Raspery I send[1,144,0], which I receive on the STM32G4. the data sent by the STM32G4 are not exactly received on RPI. l send [0,3,123], which is the value provided by the ADC of the stm32g4. With the digital variable I should have 891([0,3,123]). Instead I have 3389 ([61,61,0]). – Carole Ngoumkoua Oct 30 '19 at 11:04
  • You need to edit all clarifications and extra information into your question. – joan Oct 30 '19 at 11:14
  • To troubleshoot, I would (1) use low speed, 1 MHz or even lower, (2) Mode 1. (3) Loopback test at 16 MHz. – tlfong01 Oct 30 '19 at 11:43
  • 1
    @CaroleNgoumkoua Not many people will bother to read all the comments to work out what does and does not work. You should EDIT ALL RELEVANT INFORMATION into your question. I am now making a point of not answering questions where the needed information is contained in comments. – joan Oct 30 '19 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.