📄 sa1100sio.c
字号:
* Set the baud rate. Return EIO for an invalid baud rate, or * OK on success. * * Calculate the baud rate divisor: it must be non-zero and must * fit in a 12-bit register. */ brd = (pChan->xtal / (16 * arg)) - 1; if (brd & ~0xFFF) { status = EIO; /* baud rate out of range */ break; } /* disable interrupts during chip access */ oldlevel = intLock (); SA1100_UART_REG_READ (pChan, UTCR3, cr3); /* save current cr3 */ SA1100_UART_REG_WRITE (pChan, UTCR3, 0); /* disable UART */ /* Set baud rate divisor in UART */ SA1100_UART_REG_WRITE (pChan, UTCR1, (brd >> 8) & 0xF); SA1100_UART_REG_WRITE (pChan, UTCR2, brd & 0xFF); /* reenable UART and any enabled interrupts */ SA1100_UART_REG_WRITE (pChan, UTCR3, cr3); pChan->baudRate = arg; intUnlock (oldlevel); break; case SIO_BAUD_GET: /* Get the baud rate and return OK */ *(int *)arg = pChan->baudRate; break; case SIO_MODE_SET: /* * Set the mode (e.g., to interrupt or polled). Return OK * or EIO for an unknown or unsupported mode. */ if ((arg != SIO_MODE_POLL) && (arg != SIO_MODE_INT)) { status = EIO; break; } oldlevel = intLock (); if (arg == SIO_MODE_INT) { /* Enable appropriate interrupt */ intEnable (pChan->level); } else { /* Disable the interrupt */ intDisable (pChan->level); } pChan->channelMode = arg; intUnlock (oldlevel); break; case SIO_MODE_GET: /* Get the current mode and return OK */ *(int *)arg = pChan->channelMode; break; case SIO_AVAIL_MODES_GET: /* Get the available modes and return OK */ *(int *)arg = SIO_MODE_INT | SIO_MODE_POLL; break; case SIO_HW_OPTS_SET: /* * Optional command to set the hardware options (as defined * in sioLib.h). * Return OK, or ENOSYS if this command is not implemented. * Note: several hardware options are specified at once. * This routine should set as many as it can and then return * OK. The SIO_HW_OPTS_GET is used to find out which options * were actually set. */ case SIO_HW_OPTS_GET: /* * Optional command to get the hardware options (as defined * in sioLib.h). Return OK or ENOSYS if this command is not * implemented. Note: if this command is unimplemented, it * will be assumed that the driver options are CREAD | CS8 * (e.g., eight data bits, one stop bit, no parity, ints enabled). */ default: status = ENOSYS; } return status; }/********************************************************************************* sa1100Int - handle an interrupt** This routine handles interrupts from the UART.** RETURNS: N/A*/ void sa1100Int ( SA1100_CHAN * pChan /* ptr to SA1100_CHAN describing this channel */ ) { UINT32 ch; /* Possible char to be in/output */ UINT32 statusReg0; /* Status from UART */ UINT32 statusReg1; /* Status from UART */ int gotOne; /* read status registers */ SA1100_UART_REG_READ (pChan, UTSR0, statusReg0); SA1100_UART_REG_READ (pChan, UTSR1, statusReg1); /* clear sticky interrupts */ SA1100_UART_REG_WRITE (pChan, UTSR0, UTSR0_RID | UTSR0_RBB | UTSR0_REB); /* * loop, removing characters from the rx FIFO (other Sio drivers do this * so it must be OK) */ while (statusReg1 & UTSR1_RNE) /* data? */ { SA1100_UART_REG_READ (pChan, UTDR, ch); /* read character */ (*pChan->putRcvChar) (pChan->putRcvArg, (char)ch); /* pass it on */ SA1100_UART_REG_READ (pChan, UTSR1, statusReg1); /* update status */ } /* * if tx FIFO is interrupting, loop, writing characters to the FIFO * until the FIFO is full or there are no more characters to transmit */ if (statusReg0 & UTSR0_TFS) /* tx FIFO needs filling? */ { do { gotOne = (*pChan->getTxChar) (pChan->getTxArg, &ch) != ERROR; if (gotOne) SA1100_UART_REG_WRITE(pChan, UTDR, ch & 0xFF); /* tx char */ SA1100_UART_REG_READ (pChan, UTSR1, statusReg1); /* get status */ } while (gotOne && (statusReg1 & UTSR1_TNF)); /* while not full */ if (!gotOne) { /* no more chars to send - disable transmitter interrupts */ SA1100_UART_REG_READ (pChan, UTCR3, ch); ch &= ~UTCR3_TIM; SA1100_UART_REG_WRITE (pChan, UTCR3, ch); } } }/********************************************************************************* sa1100TxStartup - transmitter startup routine** Call interrupt level char output routine and enable interrupt** RETURNS: OK on success, ENOSYS if the device is polled-only, or* EIO on hardware error.*/LOCAL int sa1100TxStartup ( SIO_CHAN * pSioChan /* ptr to SIO_CHAN describing this channel */ ) { UINT32 cr3; SA1100_CHAN * pChan = (SA1100_CHAN *)pSioChan; if (pChan->channelMode == SIO_MODE_INT) { /* Enable Transmitter interrupts */ SA1100_UART_REG_READ (pChan, UTCR3, cr3); cr3 |= UTCR3_TIM; SA1100_UART_REG_WRITE (pChan, UTCR3, cr3); return OK; } else return ENOSYS; }/******************************************************************************** sa1100PollOutput - output a character in polled mode.** RETURNS: OK if a character arrived, EIO on device error, EAGAIN* if the output buffer is full, ENOSYS if the device is interrupt-only.*/LOCAL int sa1100PollOutput ( SIO_CHAN * pSioChan, /* ptr to SIO_CHAN describing this channel */ char outChar /* char to output */ ) { SA1100_CHAN * pChan = (SA1100_CHAN *)pSioChan; UINT32 pollStatus; SA1100_UART_REG_READ (pChan, UTSR1, pollStatus); /* is the transmitter ready to accept a character? */ if ((pollStatus & UTSR1_TNF) == 0x00) return EAGAIN; /* write out the character */ SA1100_UART_REG_WRITE (pChan, UTDR, outChar); /* transmit character */ return OK; }/******************************************************************************** sa1100PollInput - poll the device for input.** RETURNS: OK if a character arrived, EIO on device error, EAGAIN* if the input buffer is empty, ENOSYS if the device is interrupt-only.*/LOCAL int sa1100PollInput ( SIO_CHAN * pSioChan, /* ptr to SIO_CHAN describing this channel */ char * thisChar /* pointer to where to return character */ ) { SA1100_CHAN * pChan = (SA1100_CHAN *)pSioChan; UINT32 pollStatus; SA1100_UART_REG_READ (pChan, UTSR1, pollStatus); if ((pollStatus & UTSR1_RNE) == 0x00) return EAGAIN; /* got a character */ SA1100_UART_REG_READ (pChan, UTDR, *thisChar); return OK; }/******************************************************************************** sa1100CallbackInstall - install ISR callbacks to get/put chars.** This routine installs interrupt callbacks for transmitting characters* and receiving characters.** RETURNS: OK on success, or ENOSYS for an unsupported callback type.**/LOCAL int sa1100CallbackInstall ( SIO_CHAN * pSioChan, /* ptr to SIO_CHAN describing this channel */ int callbackType, /* type of callback */ STATUS (*callback)(), /* callback */ void * callbackArg /* parameter to callback */ ) { SA1100_CHAN * pChan = (SA1100_CHAN *)pSioChan; switch (callbackType) { case SIO_CALLBACK_GET_TX_CHAR: pChan->getTxChar = callback; pChan->getTxArg = callbackArg; return OK; case SIO_CALLBACK_PUT_RCV_CHAR: pChan->putRcvChar = callback; pChan->putRcvArg = callbackArg; return OK; default: return ENOSYS; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -