2

I have referred many tutorials to get UART working in bare metal environment on RPI 3.

  1. https://github.com/dwelch67/raspberrypi/tree/master/uart05

  2. https://github.com/mrvn/RaspberryPi-baremetal/tree/master/003-can-you-c-me

  3. https://wiki.osdev.org/Raspberry_Pi_Bare_Bones

I tried all these codes nothing worked. Nothing gets sent/received from host computer serial terminal to RPI.

I have following files in my sd card.

  1. kernel7.img (uart code)
  2. fixup.dat
  3. start.elf
  4. config.txt
  5. cmdline.txt(I think it's not necessary for bare metal tutorials)
  6. bootcode.bin

I am not able to find out why it's not working for me. I read about 2 UARTS in RPI3

  1. miniUART or UART1 : Which is default linux console etc ttyS0 (weaker has baud rate linked to core_freq)
  2. UART0 or PL011: ttyAMA0 (more powerful)

I tried all the following options for uart in config.txt

  1. Add dtoverlay=pi3-disable-bt or dtoverlay=pi3-miniuart-bt
  2. enable_uart=1
  3. core_freq=250

Can someone help me figure out the problem? and if there is problem in config.txt or cmdline.txt then please point me in right direction.

I am really confused between what config.txt parameter turn on which UART hardware.

Thanks in advance

9
  • I can see no evidence that any of the tutorials mentioned are relevant to the Pi3. Did the Pi3 even exist 4 years ago?
    – joan
    Commented Sep 29, 2018 at 22:22
  • Does it matter because only part changed is config.txt and base address gpio as far as UART is concerned.
    – zero
    Commented Sep 30, 2018 at 5:58
  • @joan please help me finding the part I am missing. It's headache with RPI 3b
    – zero
    Commented Sep 30, 2018 at 7:03
  • I don't do bare metal. Have you looked through raspberrypi.org/forums/viewforum.php?f=72
    – joan
    Commented Sep 30, 2018 at 8:30
  • Yes I went through. There are limited resources about RPI3b. @joan can you at least help me figure out correct uart config. I think I am executing code for different UART and RPI GPU enables different one. I am lost there.
    – zero
    Commented Sep 30, 2018 at 8:57

1 Answer 1

1

It is a bit late but I may have a solution for you.

I have managed to write a loopback program in C to test if the RaspberryPi4 is receiving any data.

Receiver.c

#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <stdlib.h>


//Read Function
void receiveMessage();

//Loop Function
void loopFunctionReceivedMessage(int input);


int main()
{
    loopFunctionReceivedMessage(3);
    return 0;
}

void loopFunctionReceivedMessage(int input)
{
    time_t start_time = time(NULL);
    while(1)
    {
            time_t current_time = time(NULL);
            double elapsed = difftime(current_time, start_time);
            if(elapsed >= input)
            {
                    receivedMessage();
                    start_time = current_time;
            }
    }
 }


 void receivedMessage()
 {
     int file, count;
    if((file=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY| O_NONBLOCK))<0)
    {
            perror("UART: Failed to open the file.\n");
            return -1;
    }

    fcntl(file, F_SETFL, 0);

    struct termios options;
    tcgetattr(file, &options);
    options.c_cflag= B9600 | CS8 | CREAD | CLOCAL| CSTOPB;
    options.c_iflag =  ECHO | ECHONL | ICANON | IEXTEN | ISIG |IGNPAR | ICRNL;
    tcflush(file, TCIFLUSH);
    tcsetattr(file, TCSANOW, &options);

    char receive[30];
    if((count = read(file,(void*)receive,30))<0)
    {
            perror("UART: Failed to read from the port :(.\n");
            return -1;
    }
    printf("Received: %s", receive);
    close(file);
}

Sender.c

#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <time.h>

//Sending Function
void sendMessage(char *input_ptr);

//LoopFunction
void loopFunctionMessage(int input);

int main(int argc, char *argv[])
{

    loopFunctionMessage(3);
    return 0;
}

void loopFunctionMessage(int input)
{
    time_t start_time = time(NULL);
    while(1)
    {
            time_t current_time = time(NULL);
            double elapsed = difftime(current_time, start_time);
            if(elapsed >=input)
            {
                    sendMessage("Michael Jackson\n");
                    start_time= current_time;
            }
    }
}

  void sendMessage(char *input_ptr)
  {
    printf("You have entered: %s\n",input_ptr);
     int file, count;
    if ((file=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY|  O_NONBLOCK))<0)
    {
            perror("UART: Failed to open the file.\n");
            return -1;
    }

    struct termios options;
    tcgetattr(file, &options);
    options.c_cflag = B9600 | CS8 | CREAD | CLOCAL| CSTOPB;
    options.c_iflag = ECHO | ECHONL | ICANON | IEXTEN | ISIG |IGNPAR | ICRNL;
    tcflush(file, TCIFLUSH);
    tcsetattr(file, TCSANOW, &options);

    char command[100];
    sprintf(command, "echo -e '%s' > /dev/ttyS0",input_ptr);
    system(command);
           if((count = write(file, input_ptr, 70))<0)
    {
            perror("UART: Failed to write to the output.\n");
            return -1;
    }

    close(file);
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.