Enabling and testing local loop back for UART

放肆的年华 提交于 2019-12-02 09:30:05

问题


I'm trying to do UART internal loop back testing and come up with below changes

 #include <fcntl.h>
 #include <stdio.h>
 #include <termios.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/errno.h>
 #include <sys/types.h>
 #include <unistd.h>


 #define CCSR_BASE  0xfe000000
 #define UART1_BASE 0x11c000
 #define UART1_LEN  0x1000

 static volatile unsigned long  *uartReg = MAP_FAILED;

/* Map in registers. */
static unsigned long  *initMapMem(int fd, unsigned long addr, unsigned long len)
{
     return (unsigned long *) mmap(0, len,
            PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED|MAP_LOCKED, fd, addr);
}

int uartInitialise(void)
{
    int fd;
    int i;

    fd = open("/dev/mem", O_RDWR | O_SYNC) ;

    if (fd < 0)
       {
           fprintf(stderr,"This program needs root privileges.Try sudo /n");
           return -1;
       }

   uartReg  = initMapMem(fd, ((CCSR_BASE) + (UART1_BASE))  , UART1_LEN);

   /* In local loop back mode, data written to UTHR(8 bit) can be read from the 
      receiver buffer register(URBR 8 bit) of the same UART. */

 //    *(uartReg + 0x605 )  = 0x0;

       *(uartReg + (0x600)) = 0xf;

       printf("UART_REG : %#x\n", *(int *)(uartReg + (0x600)); /*Expecting 0xf to read here if loop back mode is set */

       close(fd);

  if (uartReg == MAP_FAILED)
     {
        fprintf(stderr,"Bad, mmap failed\n");
        return -1;
     }

    return 0;
}

 int main()
 {
    int pins;
    int f = open( "/dev/ttyS1", O_RDWR);

    if (f< 0)
    {
        printf("\nout");
        close(f);
     }   

    ioctl( f, TIOCMGET, &pins);
    ioctl( f, TIOCMSET, pins | TIOCM_LOOP);

    if (uartInitialise() < 0) return 1;

    sleep(5);

   ioctl( f, TIOCMGET, &pins);
   ioctl( f, TIOCMSET, pins & ~ TIOCM_LOOP);

 }

root@amit:~# ./loop_back

UART_REG : 0

But I'm not getting expected output ,can any one point me out what needs to be done to get the UART internal loop back testing?

来源:https://stackoverflow.com/questions/29144976/enabling-and-testing-local-loop-back-for-uart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!