m68332Sio.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:8k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* m68332Sio.c - Motorola MC68332 tty driver*/
  2. /* Copyright 1984-2002 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01c,24apr02,pmr  SPR 75161: returning int from m68332Startup() as required.
  8. 01b,09nov95,jdi  doc: style cleanup.
  9. 01a,22aug95,myz  written(based on m68332Serial.c).
  10. */
  11. /*
  12. DESCRIPTION
  13. This is the driver for the Motorola MC68332 on-chip UART.  It has only one
  14. serial channel.
  15. USAGE
  16. A M68332_CHAN structure is used to describe the chip.
  17. The BSP's sysHwInit() routine typically calls sysSerialHwInit(),
  18. which initializes all the values in the M68332_CHAN structure (except
  19. the SIO_DRV_FUNCS) before calling m68332DevInit().
  20. The BSP's sysHwInit2() routine typically calls sysSerialHwInit2(), which
  21. connects the chips interrupt (m68332Int) via intConnect().
  22. INCLUDE FILES: drv/sio/m68332Sio.h
  23. */
  24. /* includes */
  25. #include "vxWorks.h"
  26. #include "intLib.h"
  27. #include "errno.h"
  28. #include "sioLib.h"
  29. #include "drv/sio/m68332Sio.h"
  30. /* locals */
  31. static SIO_DRV_FUNCS m68332SioDrvFuncs;
  32. /* forward declarations */
  33. static STATUS m68332Ioctl (M68332_CHAN *pChan,int request,int arg);
  34. static void   m68332ResetChannel (M68332_CHAN *pChan);
  35. static int    m68332PollOutput (M68332_CHAN *,char);
  36. static int    m68332PollInput (M68332_CHAN *,char *);
  37. static int    m68332Startup (M68332_CHAN *);
  38. static int    m68332CallbackInstall (SIO_CHAN *, int, STATUS (*)(), void *);
  39. /*******************************************************************************
  40. *
  41. * m68332DevInit - initialize the SCC
  42. *
  43. * This initializes the chip to a quiescent state.
  44. *
  45. * RETURNS: N/A
  46. */
  47. void m68332DevInit
  48.     (
  49.     M68332_CHAN *pChan
  50.     )
  51.     {
  52.     *pChan->qilr &= ~QSM_QILR_SCI_MASK;         /* disable sci ints */
  53.     /* make sure the driver function pointers are installed */
  54.     if (m68332SioDrvFuncs.ioctl == NULL)
  55.         {
  56.         m68332SioDrvFuncs.txStartup   = (int (*)(SIO_CHAN *)) m68332Startup;
  57.         m68332SioDrvFuncs.pollInput   = (int (*)(SIO_CHAN *,char *))
  58.                                         m68332PollInput;
  59.         m68332SioDrvFuncs.pollOutput  = (int (*)(SIO_CHAN *,char ))
  60.                                         m68332PollOutput;
  61.         m68332SioDrvFuncs.ioctl  = (int (*)(SIO_CHAN *,int,void *))m68332Ioctl;
  62.         m68332SioDrvFuncs.callbackInstall = (int (*)()) m68332CallbackInstall;
  63.         }
  64.     pChan->pDrvFuncs = &m68332SioDrvFuncs;
  65.     }
  66. /*******************************************************************************
  67. *
  68. * m68332ResetChannel - initialize the SCC
  69. */
  70. static void m68332ResetChannel
  71.     (
  72.     M68332_CHAN *pChan
  73.     )
  74.     {
  75.     FAST int oldlevel = intLock (); /* LOCK INTERRUPTS */
  76.     /* initialize QSM registers associated with SCI */
  77.     *pChan->qilr &= ~QSM_QILR_SCI_MASK; /* disable sci ints */
  78.     /* set default baud rate */
  79. /*
  80.     *pChan->sccr0 = pChan->clockRate / (32 * pChan->baudRate);
  81. */
  82.     *pChan->sccr1 = QSM_SCCR1_RE | QSM_SCCR1_TE | QSM_SCCR1_RIE
  83.         ; /* enable rx/tx  enable receiver interrupts */
  84.     *pChan->qilr |= pChan->intVec; /* enable sci ints */
  85.     intUnlock (oldlevel); /* UNLOCK INTERRUPTS */
  86.     }
  87. /*******************************************************************************
  88. *
  89. * m68332Ioctl - special device control
  90. *
  91. * RETURNS: OK on success, EIO on device error, ENOSYS on unsupported
  92. *          request.
  93. *
  94. */
  95. LOCAL STATUS m68332Ioctl
  96.     (
  97.     M68332_CHAN *pChan, /* device to control */
  98.     int request,        /* request code */
  99.     int arg             /* some argument */
  100.     )
  101.     {
  102.     FAST STATUS status = EIO;
  103.     UINT16 baudScratch;
  104.     int oldlevel;
  105.     switch (request)
  106. {
  107. case SIO_BAUD_SET:
  108.     baudScratch = pChan->clockRate / (32 * arg);
  109.     if ((baudScratch >= 1) && (baudScratch <= 8191) && (arg <= 38400))
  110. {
  111. *pChan->sccr0 = baudScratch;
  112. status = OK;
  113. }
  114.     break;
  115.         case SIO_BAUD_GET:
  116.             *(int *)arg = pChan->baudRate;
  117.             status = OK;
  118.             break;
  119.         case SIO_MODE_SET:
  120.             if (!((int)arg == SIO_MODE_POLL || (int)arg == SIO_MODE_INT))
  121.                 break;
  122.             /* lock interrupt  */
  123.             oldlevel = intLock();
  124.             /* don't reset channel if switching between polling and int mode */
  125.             if (!pChan->channelMode)
  126.                 m68332ResetChannel(pChan);
  127.             if (arg == SIO_MODE_INT)
  128.                 *pChan->qilr |= pChan->intVec;        /* enable sci ints */
  129.             else
  130.                 *pChan->qilr &= ~QSM_QILR_SCI_MASK; /* disable sci ints */
  131.             pChan->channelMode = arg;
  132.        
  133.             status = OK;
  134.             intUnlock(oldlevel);
  135.             break;
  136.                 
  137.         case SIO_MODE_GET:
  138.             *(int *)arg = pChan->channelMode;
  139.             return (OK);
  140.         case SIO_AVAIL_MODES_GET:
  141.             *(int *)arg = SIO_MODE_INT | SIO_MODE_POLL;
  142.             return (OK);
  143.         default:
  144.             status = ENOSYS;
  145.         }
  146.     return (status);
  147.     }
  148. /*******************************************************************************
  149. *
  150. * m68332Int - handle an SCC interrupt
  151. *
  152. * This routine handles SCC interrupts.
  153. *
  154. * RETURNS: N/A
  155. */
  156. void m68332Int
  157.     (
  158.     M68332_CHAN *pChan
  159.     )
  160.     {
  161.     char outChar;
  162.     if (*pChan->scsr & QSM_SCSR_TDRE)
  163. {
  164. if ((*pChan->getTxChar) (pChan->getTxArg, &outChar) == OK)
  165.     *pChan->scdr = outChar;
  166. else
  167.     *pChan->sccr1 &= ~QSM_SCCR1_TIE;
  168.         }
  169.     if (*pChan->scsr & QSM_SCSR_RDRF)
  170.         {
  171.         outChar = *pChan->scdr;
  172.         (*pChan->putRcvChar) (pChan->putRcvArg,outChar);
  173.         }
  174.     }
  175. /*******************************************************************************
  176. *
  177. * m68332Startup - transmitter startup routine
  178. */
  179. static int m68332Startup
  180.     (
  181.     M68332_CHAN *pChan          /* ty device to start up */
  182.     )
  183.     {
  184.     char outChar;
  185.     if ((*pChan->scsr & QSM_SCSR_TDRE) &&
  186.         ((*pChan->getTxChar) (pChan->getTxArg, &outChar) == OK))
  187. {
  188. *pChan->scdr   = outChar;
  189. *pChan->sccr1 |= QSM_SCCR1_TIE;
  190. }
  191.     return (OK);
  192.     }
  193. /******************************************************************************
  194. *
  195. * m68332PollInput - poll the device for input.
  196. *
  197. * RETURNS: OK if a character arrived, ERROR on device error, EAGAIN
  198. *          if the input buffer is empty.
  199. */
  200. static int m68332PollInput
  201.     (
  202.     M68332_CHAN * pChan,
  203.     char *      thisChar
  204.     )
  205.     {
  206.    
  207.     if ( !(*pChan->scsr & QSM_SCSR_RDRF) )
  208.         return (EAGAIN);
  209.     *thisChar = *pChan->scdr;
  210.     return (OK);
  211.     }
  212. /******************************************************************************
  213. *
  214. * m68332PollOutput - output a character in polled mode.
  215. *
  216. * RETURNS: OK if a character arrived, ERROR on device error, EAGAIN
  217. *          if the output buffer if full.
  218. */
  219. static int m68332PollOutput
  220.     (
  221.     M68332_CHAN * pChan,
  222.     char        outChar
  223.     )
  224.     {
  225.     
  226.     if ( !(*pChan->scsr & QSM_SCSR_TDRE) )
  227.         return(EAGAIN);
  228.     *pChan->scdr = outChar;
  229.     return(OK);
  230.     }
  231. /******************************************************************************
  232. *
  233. * m68332CallbackInstall - install ISR callbacks to get put chars.
  234. */
  235. static int m68332CallbackInstall
  236.     (
  237.     SIO_CHAN *  pSioChan,
  238.     int         callbackType,
  239.     STATUS      (*callback)(),
  240.     void *      callbackArg
  241.     )
  242.     {
  243.     M68332_CHAN * pChan = (M68332_CHAN *)pSioChan;
  244.     switch (callbackType)
  245.         {
  246.         case SIO_CALLBACK_GET_TX_CHAR:
  247.             pChan->getTxChar    = callback;
  248.             pChan->getTxArg     = callbackArg;
  249.             return (OK);
  250.         case SIO_CALLBACK_PUT_RCV_CHAR:
  251.             pChan->putRcvChar   = callback;
  252.             pChan->putRcvArg    = callbackArg;
  253.             return (OK);
  254.         default:
  255.             return (ENOSYS);
  256.         }
  257.     }