0

I'm trying to connect a raspberry pi 4B to a NUCLEO-8S208RB using serial communication connecting the device through tx/rx pins. Data sent from the STM board gets received by raspberry, but messages from raspberry get lost. Transmission works fine through USB, I also tried connecting raspberry's rx and tx and it can send and receive from itself, I did with the STM and it also worked. I even tried connecting an Arduino MEGA2560 and it worked with both the Raspberry and the STM Codes are the followings: Raspberry

import serial
import time
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyAMA0', 500000, timeout=0)
ser.reset_input_buffer()
while True:
    ser.write(b"Hell\n")
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()
        print(line)
    time.sleep(1)

STM main.c

/* MAIN.C file
 * 
 * Copyright (c) 2002-2005 STMicroelectronics
 */

#include <stm8s.h>
#include "Serial.h"
#include "ISR.h"
#include "delay.h"


main()
{
    InitCLK();
    InitSer();
    Serial_Send_String("xd\n");
    InitISR();
    
    while(1){
        delay_ms(500);
    }
}

@svlreg @far @interrupt void ISR_Ser(void){                  //I've already put this in the correct irq in the interrupt vector file
    char n=UART1 -> DR;
    Serial_Tx(n);
}

Serial.c

#include "Serial.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stm8s.h>



void InitSer(){
    GPIO_DeInit(GPIOA);
    GPIO_Init(GPIOA,GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
    GPIO_Init(GPIOA,GPIO_PIN_4, GPIO_MODE_IN_PU_NO_IT);
    UART1_DeInit();
    UART1_Init(500000,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO,UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_TXRX_ENABLE);
    UART1_ITConfig(UART1_IT_RXNE, ENABLE);
    UART1_Cmd(ENABLE);
    
}
void Serial_Tx(uint8_t data){
    while(!(UART1 -> SR & UART1_SR_TC));
    UART1 -> DR = data;
}
void Serial_Send_Int(int32_t num)
{
    if(num<0)
    {
        Serial_Send_String("-");
        num=-num;
    }
    if(num==0){Serial_Tx('0');}
    else
    {
        char str[10];               // definisce una stringa sulla quale convertire il numero da trasmettere (max 10 cifre)
        char i;                     // contatore ciclo
        for(i=0;i<10;i++) str[i]=0; // cancella la stringa
        i=10;
        while (num)
        {
            str[i]=num%10+'0';      // converte il numero da trasmettere in una stringa (dalla cifra meno significativa)
            num/=10;
            i--;
        }
        for (i=0;i<10;i++)          // invia la stringa un carattere alla volta
        if (str[i]) Serial_Tx(str[i]);
    }
}
void Serial_Send_Hex(int32_t num){
    if(num<0)
    {
        Serial_Send_String("-");
        num=-num;
    }
    Serial_Send_String("0x");
    if(num==0)Serial_Tx('0');
    else{
        char str[10];               // definisce una stringa sulla quale convertire il numero da trasmettere (max 10 cifre)
        char i;                     // contatore ciclo
        long remainder;         //resto
        for(i=0;i<10;i++) str[i]=0; // cancella la stringa
        
        i=0;
        while (num)
        {
        remainder = num % 16;
        if (remainder < 10)str[i++] = 48 + remainder;
        else str[i++] = 55 + remainder;
        num/=16;
                Serial_Tx('');
        }
        for (i=0; i<10; i++)            // invia la stringa un carattere alla volta
        if (str[9-i]) Serial_Tx(str[9-i]);
    }
}
void Serial_Send_String(char *string1)
{
    int i=0;
    for(i=0;i<strlen(string1);i++)
    {
        Serial_Tx(string1[i]);
    }
}
void SerialN()
{
    Serial_Tx(13);
    Serial_Tx(10);
}

Arduino

int incomingByte = 0; // for incoming serial data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
}

I tried using UART3 instead of UART1 and it works.

Kingsley
  • 25
  • 4

0 Answers0