STM32和Raspberry Pi 3之间的UART连接

时间:2018-01-12 18:17:27

标签: python raspberry-pi stm32 uart usart

我试图在STM32F4和Raspberry Pi 3之间建立UART连接以发送运动传感器数据。

STM32 C代码:

    /* 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代码:

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)

我已经在Raspberry Pi上启用了串行端口接口,并禁用了终端串口选项。我确信STM32上的代码可以工作,因为我尝试将另一个STM32用作接收器。我将2个GND引脚和STM32的Tx(在我的情况下为PB6)连接到RasPi Rx(GPIO15)。作为输出我得到了一些数字,但是当我加速STM32时它们不会改变(在现场观察中可以看到差异)。即使拔掉Tx-Rx连接,我也会在RasPi上获得奇怪的输出。

有没有人知道可能出现什么问题?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

不确定它可能是原因,但是当我做同样的工作时,我在Raspberry Pi 3上使用了/dev/ttyAMA0。所以请检查系统这一侧的UART。

我禁用蓝牙,因此PIN 14/15现在可以作为UART TX / RX运行。这导致/dev/ttyAMA0现在开始作为UART端口运行,而不是/dev/ttyS0enter image description here 有关official site的更多信息,请参阅。

这是我的Raspberry示例代码(简化):

import serial

ser = serial.Serial()
ser.port = '/dev/ttyAMA0'
ser.baudrate = 115200
ser.timeout = 60  # 1 min
ser.open()

msg = ''
while True:
    char = ser.read(1)  # 1 byte
    msg = msg+char.decode('utf-8')
    if char == b'\r':
        break

对于STM32F031K6:

<强> usart.c:

...
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
...

<强> main.c中:

...
MX_USART1_UART_Init();

uint8_t str[STR_SIZE];
str_len = sprintf(str, "some message\r");
HAL_UART_Transmit(&huart1, str, (uint32_t)str_len, 0xFFFF);
...