⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 MANTIS是由科罗拉多大学开发的传感器网络嵌入式操作系统。 这是mantis的0.9.5版本的源码。
💻 C
字号:
//  This file is part of MANTIS OS, Operating System//  See http://mantis.cs.colorado.edu/////  Copyright (C) 2003,2004,2005 University of Colorado, Boulder////  This program is free software; you can redistribute it and/or//  modify it under the terms of the mos license (see file LICENSE)/**************************************************************************//* File:      mos.h                                                       *//* Author     Jeff Rose & Brian Shucker                                   *//*   Date: 05/12/03                                                       *//*                                                                        *//**************************************************************************/#include "mos.h"#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "uart.h"#define BAUDRATE B57600    // 19200 now matches nymph/mica2 default uart baud#define NUM_UARTS 2       // number of uartschar *device [NUM_UARTS] = {"/dev/ttyS0","/dev/ttyS1"}; // serial device locationsint uartFd [NUM_UARTS];struct termios oldtio,newtio; //terminal settings/** Startup the uart with default settings */int8_t mos_uart_open(uint8_t uart){    if(uart > NUM_UARTS) return -1;    uartFd[uart] = open(device[uart], O_RDWR | O_NOCTTY);   if (uartFd <0) {perror(device[uart]); exit(-1); }    tcgetattr(uartFd[uart],&oldtio); /* save current port settings */    bzero(&newtio, sizeof(newtio));  newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;  newtio.c_iflag = IGNPAR | IXANY;  newtio.c_oflag = 0;    /* set input mode (non-canonical, no echo,...) */  newtio.c_lflag = 0;    newtio.c_cc[VTIME]    = 0;   /* min time between chars (*.1sec)*/  newtio.c_cc[VMIN]     = 0;   /* min number of chars for read */    tcflush(uartFd[uart], TCIFLUSH);  tcsetattr(uartFd[uart],TCSANOW,&newtio);  return 0;}int mos_uart_getfd(uint8_t uart){  return uartFd[uart];}/** Send a byte to the uart * @param uart uart to send the byte on * @param byte Byte to send */void mos_uart_send(uint8_t uart, uint8_t byte){  write(uartFd[uart], &byte, 1);  tcdrain(uartFd[uart]);}/** Get a byte from the uart * @return Byte received */uint8_t mos_uart_recv(uint8_t uart){  uint8_t byte;  while(!read(uartFd[uart], &byte, 1))    ;  return byte;}/** Turn off the uart */void mos_uart_close(uint8_t uart){  tcsetattr(uartFd[uart],TCSANOW,&oldtio);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -