stm32 uart read

787562067 googcheng at qq.com
Fri Mar 29 09:04:16 UTC 2019


here is my code:


#define CONFIGURE_INIT


#include <stdio.h>
#include <stdlib.h>


#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/select.h>
#include <rtems.h>
#include <rtems/shell.h>
#include <bsp.h>






extern rtems_task Init(rtems_task_argument argument);


//#define CONFIGURE_APPLICATION_EXTRA_DRIVERS  TTY1_DRIVER_TABLE_ENTRY




#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#ifdef RTEMS_BSP_HAS_IDE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_IDE_DRIVER
#endif


//#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
//#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM


/*
 * these values are higher than needed...
 */
#define CONFIGURE_MAXIMUM_TASKS             3
//#define CONFIGURE_MAXIMUM_SEMAPHORES        20
//#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES    20
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 10
//#define STACK_CHECKER_ON
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE


//#define CONFIGURE_EXTRA_TASK_STACKS         (6 * RTEMS_MINIMUM_STACK_SIZE)




#define CONFIGURE_INIT
#include <rtems/confdefs.h>




//#define CONFIGURE_SHELL_COMMANDS_INIT
//#define CONFIGURE_SHELL_COMMANDS_ALL


//#include <rtems/shellconfig.h>


void testCom1(void);
int uart_recv(int fd, char *rcv_buf,int data_len);




void fillBufferPoll(int fd, char *buf, int len)
{
    int n;
    char ch;
    int i = 0;
    printf("in poll function.\n");
    for(;;)
    {
        n = read(fd, &ch, 1);  // non blocked read
        printf("n %d cc %x\n", n, ch);
        if (n <= 0)
        {
            rtems_task_wake_after (0.1);  // delay 10ms
        }
        else if(n == 1)
        {
            if(ch == '\r')
                continue;


            if(ch == '\n')
            {


                printf("uart %s\n", buf);
                break;
            }


            buf[i] = ch;
            printf("the %d th char %c \n", i, buf[i]);
            i++;


            if(i == len)
                break;


            
        }
    }
}


void testCom1(void) {
  char buffer[128] = {0};


  printf("*** Simple COM1 Test ***\n");


  //int fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | _FNDELAY);
  int fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NONBLOCK);


  printf("\nOpened COM1, fd=%d\n\n", fd);
  if(fd < 0)
      printf("open error\n");


  #if 0
  if(fcntl(fd, F_SETFL, 0) < 0)  
  {  
    printf("fcntl failed!\n");  
    return;  
  }       
  else  
  {  
      printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));  
  }
  #endif
  //fcntl(fd, F_SETFL, 0); // set blocked
  
  struct termios options;
  tcgetattr(fd, &options);
  bzero(&options, sizeof(options));
	/* setting the baud rate */
  cfsetispeed(&options, B115200);
  cfsetospeed(&options, B115200);


	
  options.c_cflag |= (CLOCAL | CREAD);


  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;


  options.c_iflag = IGNPAR| ICRNL;
  options.c_oflag = 0;
  options.c_lflag = ICANON;


  options.c_cc[VEOF] = 4;
  options.c_cc[VMIN] = 1;
  options.c_cc[VTIME] = 0;


  //tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &options);  
      
  if(0 == isatty(fd))  
  {  
      printf("input is not a terminal device\n");  
      //return;  
  }


  int numBytes = write(fd, "Hello, I'm waiting for input..\r\n", 33);


  if (numBytes < 0) {
    printf("\nFailed to send from COM1!\n");
  }


  
  //wait input 
  //printf("wait  \n");
  fillBufferPoll(fd, buffer, 100);
  #if 0
  //(void) rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() );
  numBytes = read(fd, buffer, 20);
  printf("read bytes %d\n", numBytes);
  strerror(errno);
  if (numBytes < 0) {
    printf("\nFailed to read from COM1!\n");


  }
  else if(numBytes == 0)
  {
    printf("read error return 0\n");
  }
  else
  {
    //buffer[numBytes] = 0; // terminate
    printf("rev: %s\n", buffer);
  }
  #endif


  //int read_cnt = uart_recv(fd, buffer, 20);
  //printf("rev: %s\n", buffer);
  //printf("read_cnt: %d\n", read_cnt);
  close(fd);
  //rtems_task_delete(RTEMS_SELF);
}






rtems_task Init(rtems_task_argument ignored) {


  testCom1();


  printf("\n====== starting shell ======\n");


  (void) rtems_task_delete( RTEMS_SELF );


#if 0
  rtems_shell_init(
    "SHLL",                          /* task_name */
    RTEMS_MINIMUM_STACK_SIZE * 4,    /* task_stacksize */
    100,                             /* task_priority */
    "/dev/console",                  /* devname */
    0,                               /* forever */
    1                                /* wait */
  );
#endif
  exit( 0 );
}





// com log




[16:53:49.578]RX←◆*** Simple COM1 Test ***


Opened COM1, fd=3


Hello, I'm waiting for input..
\0in poll function.


[16:53:52.283]TX→◇help
□



I'm sure the read is blocked, why?  stm32 uart does not use termios driver, right?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/users/attachments/20190329/ecba53fe/attachment-0001.html>


More information about the users mailing list