为UART启用和测试本地环回

时间:2015-03-19 12:45:48

标签: c linux-kernel linux-device-driver uart

我尝试进行UART内部回送测试并提出以下更改

 #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

但我没有得到预期的输出,任何人都可以指出需要做些什么才能让UART内部回路测试?

0 个答案:

没有答案