0

i'm trying to establish an UART connection between STM32F4 and Raspberry Pi 3 to send motion sensor data.

STM32 C code:

    /* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
int8_t polje[4];

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void delay(uint32_t delay) {
    while(delay--);
}


// write to SPI1
void SPI1_Write(int8_t data)
{
  // short delay
  volatile int d = 500;
  while(d--);

  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}
  SPI_I2S_SendData(SPI1, data);

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}
  SPI_I2S_ReceiveData(SPI1);
}

// read from SPI1
int8_t SPI1_Read()
{
  // short delay
  volatile int d = 500;
  while(d--);

  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}
  SPI_I2S_SendData(SPI1, 0x00);

  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}
  return SPI_I2S_ReceiveData(SPI1);
}

void initSPI1(void)
{
  // RCC

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  // PA5, PA6, PA7 for MISO, MOSI, and SCLK
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

  // SPI1 INIT
  SPI_InitTypeDef spi;
  spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  spi.SPI_Mode = SPI_Mode_Master;
  spi.SPI_DataSize = SPI_DataSize_8b;
  spi.SPI_CPOL = SPI_CPOL_Low;
  spi.SPI_CPHA = SPI_CPHA_1Edge;
  spi.SPI_NSS = SPI_NSS_Soft;
  spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  spi.SPI_FirstBit = SPI_FirstBit_MSB;
  spi.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &spi);
  // SPI1 ENABLE
  SPI_Cmd(SPI1, ENABLE);
}

void initMotionSensor()
{
  // RCC
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

  // PE3 for slave select

  GPIO_ResetBits(GPIOE, GPIO_Pin_3);
  // configure and start sensor
  GPIO_InitTypeDef ss;
  ss.GPIO_Pin = GPIO_Pin_3;
  ss.GPIO_Mode = GPIO_Mode_OUT;
  ss.GPIO_OType = GPIO_OType_PP;
  ss.GPIO_PuPd = GPIO_PuPd_NOPULL;
  ss.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_Init(GPIOE, &ss);
  GPIO_SetBits(GPIOE, GPIO_Pin_3);

  // Aktiviram slave
  //delay(500);
  GPIO_ResetBits(GPIOE, GPIO_Pin_3);
  // Zapisem inicializacijo senzorja
  // Zapisem 0x47 na naslov 0x20
  SPI1_Write(0x20);
  SPI1_Write(0x47);
  GPIO_SetBits(GPIOE, GPIO_Pin_3);
  //delay(500);
}

void initUSART1()
{
  // RCC
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  // USART1 init
  USART_InitTypeDef USART_InitStruct;
  USART_InitStruct.USART_BaudRate = 9600;
  USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  USART_InitStruct.USART_StopBits = USART_StopBits_1;
  USART_InitStruct.USART_Parity = USART_Parity_No;
  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  USART_Init(USART1, &USART_InitStruct);

  // USART1 enable
  USART_Cmd(USART1, ENABLE);

  // PB6 and PB7 for USART1 Tx and Rx
  GPIO_InitTypeDef usart_dev;
  usart_dev.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  usart_dev.GPIO_Mode = GPIO_Mode_AF;
  usart_dev.GPIO_OType = GPIO_OType_PP;
  usart_dev.GPIO_Speed = GPIO_Speed_50MHz;
  usart_dev.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &usart_dev);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6,GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7,GPIO_AF_USART1);

}

void initDMA2()
{
  // RCC
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  // init DMA2
  DMA_InitTypeDef DMA_InitStructure;
  DMA_InitStructure.DMA_Channel = DMA_Channel_4;
  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) polje;
  DMA_InitStructure.DMA_BufferSize = 4;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART1 -> DR);
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream7, &DMA_InitStructure);
  // enable USART1 DMA Tx
  USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

  // enable DMA2 stream
  DMA_Cmd(DMA2_Stream7, ENABLE);
}      

/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    // led init
    GPIO_InitTypeDef leds;
    leds.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    leds.GPIO_Mode = GPIO_Mode_OUT;
    leds.GPIO_OType = GPIO_OType_PP;
    leds.GPIO_PuPd = GPIO_PuPd_NOPULL;
    leds.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOD, &leds);

  // SPI1
  initSPI1();

  // LIS motion sensor
  initMotionSensor();

  // USART1
  initUSART1();

  // DMA2
  initDMA2();

  while (1)
  {



    // read x1
    GPIO_ResetBits(GPIOE, GPIO_Pin_3);
    SPI1_Write(0x29 | 0x80);
    volatile int8_t x1 = SPI1_Read();
    GPIO_SetBits(GPIOE, GPIO_Pin_3);
     delay(500);


    // read y1
    GPIO_ResetBits(GPIOE, GPIO_Pin_3);
    SPI1_Write(0x2B | 0x80);
    volatile int8_t y1 = SPI1_Read();
    GPIO_SetBits(GPIOE, GPIO_Pin_3);

    if (x1 < -5) {
            // Vklopi ledico
            GPIO_SetBits(GPIOD, GPIO_Pin_12);
            delay(500);
            GPIO_ResetBits(GPIOD, GPIO_Pin_12);
    }

    if (x1 > 5) {
            // Vklopi drugo ledico
            GPIO_SetBits(GPIOD, GPIO_Pin_14);
            delay(500);
            GPIO_ResetBits(GPIOD, GPIO_Pin_14);
    }

    if (y1 < -5) {
            // Vklopi ledico
            GPIO_SetBits(GPIOD, GPIO_Pin_15);
            delay(500);
            GPIO_ResetBits(GPIOD, GPIO_Pin_15);
    }

    if (y1 > 5) {
            // Vklopi drugo ledico
            GPIO_SetBits(GPIOD, GPIO_Pin_13);
            delay(500);
            GPIO_ResetBits(GPIOD, GPIO_Pin_13);
    }

    delay(500);

    // read x
    GPIO_ResetBits(GPIOE, GPIO_Pin_3);
    SPI1_Write(0x29 | 0x80);
    volatile int8_t x2 = SPI1_Read();
    GPIO_SetBits(GPIOE, GPIO_Pin_3);

     delay(500);

    // read y
    GPIO_ResetBits(GPIOE, GPIO_Pin_3);
    SPI1_Write(0x2B | 0x80);
    volatile int8_t y2 = SPI1_Read();
    GPIO_SetBits(GPIOE, GPIO_Pin_3);
     delay(500);
    polje[0] = x1;
    polje[1] = x2;
    polje[2] = y1;
    polje[3] = y2;
    delay(500);
  }
}

Raspberry Pi code:

import time
import ctypes
import serial
import sys
from binascii import hexlify

ser = serial.Serial(

    port='/dev/ttyS0',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1,
    dsrdtr=False
)
counter = 0

if (ser.isOpen() == False):
        ser.open()

#Flush before receiving or sending any data
ser.flushInput()
ser.flushOutput()

while 1:
    x = ser.read()
    print (hexlify(x))
    time.sleep(0.3)

I already enabled the serial port interface on Raspberry Pi and disabled the terminal over serial option. I'm sure that the code on STM32 works, because I tried it with another STM32 as a receiver. I connected 2 GND pins together and STM32's Tx (PB6 in my case) to RasPi Rx (GPIO15). As an output i'm getting some numbers, but they don't change when I accelerate STM32 (the difference is seen on livewatch). I'm getting strange output on RasPi even if unplug the Tx - Rx connection.

Does anyone have an idea what could be wrong?

Thank you in advance!

2
  • Start by sending static test data, so you know exactly what to expect. Garbage received usually means bad baud rate settings, but maybe here it''s electrical interference of a damaged device? – Sean Houlihane Jan 13 '18 at 10:54
  • Could you confirm that GPIO14/15 are in the correct mode? As a test try sudo pigpiod then pigs mg 14 mg 15 to get the modes. – joan Jan 13 '18 at 12:20

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.