sci.c

来自「RTEMS (Real-Time Executive for Multiproc」· C语言 代码 · 共 1,681 行 · 第 1/4 页

C
1,681
字号
/****************************************************************************** File:     sci.c** Desc:     This file contains the console IO routines for the SCI port.*           There are two interfaces in this module. One is for the rtems*           termios/console code and the other is a device driver interface.*           This module works together with the termio module which is*           sometimes referred to as the "line disciplines" which implements*           terminal i/o processing like tabs, backspaces, and newlines.*           The rtems printf uses interrupt io and the rtems printk routine*           uses polled io which is better for debugging.** Index:    Documentation*           Section A  - Include Files*           Section B  - Manifest Constants*           Section C  - External Data*           Section D  - External Functions*           Section E  - Local Functions*           Section F  - Local Variables*           Section G  - A circular data buffer for rcv chars*           Section H  - RTEMS termios callbacks for the interrupt api*           Section I  - RTEMS termios callbacks for the polled api*           Section 0  - Miscellaneous routines*           Section 1  - Routines to manipulate the circular buffer*           Section 2  - Interrupt based entry points for the termios module*           Section 3  - Polling based entry points for the termios module*           Section 4  - Device driver public api entry points*           Section 5  - Hardware level routines*           Section 6  - Testing and debugging code** Refer:    Motorola QSM Reference Manual - Chapter 5 - SCI sub-module** Note:     See bsp.h,confdefs.h,system.h for installing drivers into RTEMS.** $Id: sci.c,v 1.3 2003/01/20 20:33:17 joel Exp $** $Log: sci.c,v $* Revision 1.3  2003/01/20 20:33:17  joel* 2003-01-20	Duane Gustavus <duane@unt.edu>** 	* console/sci.c, include/mrm332.h, startup/linkcmds,* 	startup/linkcmds_ROM: Various updates to make this run properly* 	from ROM.** Revision 1.2  2002/11/04 14:26:47  joel* 2002-11-04	Joel Sherrill <joel@OARcorp.com>** 	* console/sci.c, spurious/spinit.c: Removed warnings.** Revision 1.1  2002/02/28 23:10:39  joel* 2002-02-28	Mike Panetta <ahuitzot@mindspring.com>** 	* console/sci.c, console/sci.h,* 	console/console.c: Added new SCI driver.* 	* start/start.c: Removed file.* 	* start/start.S: New file, the asm portion of the updated start code.* 	* start/configure.am: Added start.S, removed start.c* 	* startup/start_c.c: New file, the C portion of the updated start code. 	Contains most of the code that was in the old start.c.* 	* startup/configure.am: Added start_c.c to C_FILES.* 	* include/bsp.h: Added include <rtems/bspIo.h>******************************************************************************//*****************************************************************************  Compiler Options for the incurably curious*****************************************************************************//*/opt/rtems/bin/m68k-rtems-gcc    --pipe                                      # use pipes, not tmp files    -B../../../../../../../../opti/lib/         # where the library is    -specs bsp_specs                            # ???    -qrtems                                     # ???    -g                                          # add debugging info    -Wall                                       # issue all warnings    -fasm                                       # allow inline asm???    -DCONSOLE_SCI                               # for opti-r box/rev b proto    -mcpu32                                     # machine = motorola cpu 32    -c                                          # compile, don't link    -O4                                         # max optimization    -fomit-frame-pointer                        # stack frames are optional    -o o-optimize/sci.o                         # the object file    ../../../../../../../../../rtems/c/src/lib/libbsp/m68k/opti/console/sci.c*//*****************************************************************************  Overview of serial port console terminal input/output*****************************************************************************//*   +-----------+                               +---------+   |    app    |                               |   app   |   +-----------+                               +---------+         |                                          |         | (printf,scanf,etc.)                      |         v                                          |   +-----------+                                    |   |    libc   |                                    |   +-----------+                                    |         |                                          |         |                                          |         |     (open,close,read,write,ioctl)        |   ======|==========================================|========================         | /dev/console                             | /dev/sci         | (stdin,stdout,stderr)                    |   ======|==========================================|========================         |                                          |         |                                          |         v                                          v   +-----------+         +-----------+         +---------+   |  console  |  <--->  |  termios  |  <--->  |   sci   |   |  driver   |         |  module   |         |  driver |   +-----------+         +-----------+         +---------+                                                    |                                                    |                                                    v                                               +---------+                                               |         |                                               |  uart   |                                               |         |                                               +---------+*//*****************************************************************************  Section A - Include Files*****************************************************************************/#include <rtems.h>#include <bsp.h>#include <rtems/bspIo.h>#include <stdio.h>#include <rtems/libio.h>#include <libchip/serial.h>#include <libchip/sersupp.h>#include "sci.h"//#include "../misc/include/cpu332.h"/*****************************************************************************  Section B - Manifest Constants*****************************************************************************/#define SCI_MINOR       0                   // minor device number// IMPORTANT - if the device driver api is opened, it means the sci is being// used for direct hardware access, so other users (like termios) get ignored#define DRIVER_CLOSED   0                   // the device driver api is closed#define DRIVER_OPENED   1                   // the device driver api is opened// system clock definitions, i dont have documentation on this...#if 0 // Not needed, this is provided in mrm332.h#define XTAL            32768.0		        // crystal frequency in Hz#define NUMB_W          0			        // system clock parameters#define NUMB_X          1//efine NUMB_Y          0x38		        // for 14.942 Mhz#define NUMB_Y          0x3F			    // for 16.777 Mhz#define SYS_CLOCK       (XTAL * 4.0 * (NUMB_Y+1) * (1 << (2 * NUMB_W + NUMB_X)))#endif/*****************************************************************************  Section C - External Data*****************************************************************************//*****************************************************************************  Section D - External Functions*****************************************************************************//*****************************************************************************  Section E - Local Functions*****************************************************************************/void SCI_output_char(char c);rtems_isr SciIsr( rtems_vector_number vector );         // interrupt handlerconst rtems_termios_callbacks * SciGetTermiosHandlers( signed32 polled );rtems_device_driver SciInitialize ();                   // device driver apirtems_device_driver SciOpen ();                         // device driver apirtems_device_driver SciClose ();                        // device driver apirtems_device_driver SciRead ();                         // device driver apirtems_device_driver SciWrite ();                        // device driver apirtems_device_driver SciControl ();                      // device driver apisigned32 SciInterruptOpen();                            // termios apisigned32 SciInterruptClose();                           // termios apisigned32 SciInterruptWrite();                           // termios apisigned32 SciSetAttributes();                            // termios apisigned32 SciPolledOpen();                               // termios apisigned32 SciPolledClose();                              // termios apisigned32 SciPolledRead();                               // termios apisigned32 SciPolledWrite();                              // termios apistatic void SciSetBaud(unsigned32 rate);                // hardware routinestatic void SciSetDataBits(unsigned16 bits);            // hardware routinestatic void SciSetParity(unsigned16 parity);            // hardware routinestatic void inline SciDisableAllInterrupts( void );     // hardware routinestatic void inline SciDisableTransmitInterrupts( void );// hardware routinestatic void inline SciDisableReceiveInterrupts( void ); // hardware routinestatic void inline SciEnableTransmitInterrupts( void ); // hardware routinestatic void inline SciEnableReceiveInterrupts( void );  // hardware routinestatic void inline SciDisableReceiver( void );          // hardware routinestatic void inline SciDisableTransmitter( void );       // hardware routinestatic void inline SciEnableReceiver( void );           // hardware routinestatic void inline SciEnableTransmitter( void );        // hardware routinevoid SciWriteCharWait  ( unsigned8 );                   // hardware routinevoid SciWriteCharNoWait( unsigned8 );                   // hardware routineunsigned8 inline SciCharAvailable( void );              // hardware routineunsigned8 inline SciReadCharWait( void );               // hardware routineunsigned8 inline SciReadCharNoWait( void );             // hardware routinevoid SciSendBreak( void );                              // test routinestatic signed8 SciRcvBufGetChar();                      // circular rcv bufstatic void    SciRcvBufPutChar( unsigned8 );           // circular rcv buf//atic void    SciRcvBufFlush( void );                  // circular rcv bufvoid SciUnitTest();                                     // test routinevoid SciPrintStats();                                   // test routine/*****************************************************************************  Section F - Local Variables*****************************************************************************/static struct rtems_termios_tty *SciTermioTty;static unsigned8 SciInited = 0;             // has the driver been initedstatic unsigned8 SciOpened;                 // has the driver been openedstatic unsigned8 SciMajor;                  // major device numberstatic unsigned16 SciBaud;                  // current value in baud registerstatic unsigned32 SciBytesIn  = 0;          // bytes receivedstatic unsigned32 SciBytesOut = 0;          // bytes transmittedstatic unsigned32 SciErrorsParity  = 0;     // error counterstatic unsigned32 SciErrorsNoise   = 0;     // error counterstatic unsigned32 SciErrorsFraming = 0;     // error counterstatic unsigned32 SciErrorsOverrun = 0;     // error counter#if defined(CONSOLE_SCI)// this is what rtems printk uses to do polling based outputBSP_output_char_function_type      BSP_output_char = SCI_output_char;BSP_polling_getchar_function_type  BSP_poll_char   = NULL;#endif// cvs id string so you can use the unix ident command on the object#ifdef ID_STRINGSstatic const char SciIdent[]="$Id: sci.c,v 1.3 2003/01/20 20:33:17 joel Exp $";#endif/*****************************************************************************  Section G - A circular buffer for rcv chars when the driver interface is used.*****************************************************************************/// it is trivial to wrap your buffer pointers when size is a power of two#define SCI_RCV_BUF_SIZE        256         // must be a power of 2 !!!// if someone opens the sci device using the device driver interface,// then the receive data interrupt handler will put characters in this buffer// instead of sending them up to the termios module for the consolestatic unsigned8 SciRcvBuffer[SCI_RCV_BUF_SIZE];static unsigned8 SciRcvBufPutIndex = 0;     // array index to put in next charstatic unsigned8 SciRcvBufGetIndex = 0;     // array index to take out next charstatic unsigned8 SciRcvBufCount = 0;        // how many bytes are in the buffer/*****************************************************************************  Section H - RTEMS termios callbacks for the interrupt version of the driver*****************************************************************************/static const rtems_termios_callbacks SciInterruptCallbacks ={    SciInterruptOpen,                       // first open    SciInterruptClose,                      // last close    NULL,                                   // polled read (not required)    SciInterruptWrite,                      // write    SciSetAttributes,                       // set attributes    NULL,                                   // stop remote xmit    NULL,                                   // start remote xmit    TRUE                                    // output uses interrupts};/*****************************************************************************  Section I - RTEMS termios callbacks for the polled version of the driver*****************************************************************************/static const rtems_termios_callbacks SciPolledCallbacks ={    SciPolledOpen,                          // first open    SciPolledClose,                         // last close    SciPolledRead,                          // polled read    SciPolledWrite,                         // write    SciSetAttributes,                       // set attributes    NULL,                                   // stop remote xmit    NULL,                                   // start remote xmit    FALSE                                   // output uses interrupts};/////////////////////////////////////////////////////////////////////////////////                              SECTION 0//                        MISCELLANEOUS ROUTINES////////////////////////////////////////////////////////////////////////////////***************************************************************************** Func:     SCI_output_char* Desc:     used by rtems printk function to send a char to the uart* Inputs:   the character to transmit* Outputs:  none* Errors:   none* Scope:    public****************************************************************************/void SCI_output_char(char c){//  ( minor device number, pointer to the character, length )    SciPolledWrite( SCI_MINOR, &c, 1);    return;}/***************************************************************************** Func:     SciGetTermiosHandlers* Desc:     returns a pointer to the table of serial io functions*           this is called from console_open with polled set to false* Inputs:   flag indicating whether we want polled or interrupt driven io* Outputs:  pointer to function table* Errors:   none* Scope:    public****************************************************************************/const rtems_termios_callbacks * SciGetTermiosHandlers( signed32 polled ){    if ( polled )    {        return &SciPolledCallbacks;             // polling based    }    else    {        return &SciInterruptCallbacks;          // interrupt driven    }}/***************************************************************************** Func:     SciIsr* Desc:     interrupt handler for serial communications interface* Inputs:   vector number - unused* Outputs:  none* Errors:   none* Scope:    public API****************************************************************************/rtems_isr SciIsr( rtems_vector_number vector ){    unsigned8 ch;    if ( (*SCSR) & SCI_ERROR_PARITY  )   SciErrorsParity  ++;    if ( (*SCSR) & SCI_ERROR_FRAMING )   SciErrorsFraming ++;    if ( (*SCSR) & SCI_ERROR_NOISE   )   SciErrorsNoise   ++;    if ( (*SCSR) & SCI_ERROR_OVERRUN )   SciErrorsOverrun ++;    // see if it was a transmit interrupt    if ( (*SCSR) & SCI_XMTR_AVAILABLE )         // data reg empty, xmt complete    {        SciDisableTransmitInterrupts();        // tell termios module that the charcter was sent        // he will call us later to transmit more if there are any

⌨️ 快捷键说明

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