gps4020_serial.c

来自「开放源码实时操作系统源码.」· C语言 代码 · 共 466 行 · 第 1/2 页

C
466
字号

DEVTAB_ENTRY(gps4020_serial_io2, 
             CYGDAT_IO_SERIAL_ARM_GPS4020_SERIAL2_NAME,
             0,                     // Does not depend on a lower level interface
             &cyg_io_serial_devio, 
             gps4020_serial_init, 
             gps4020_serial_lookup,     // Serial driver may need initializing
             &gps4020_serial_channel2
    );
#endif //  CYGPKG_IO_SERIAL_ARM_GPS4020_SERIAL2

// Internal function to actually configure the hardware to desired baud rate, etc.
static bool
gps4020_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
{
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    volatile struct _gps4020_uart *regs = (volatile struct _gps4020_uart *)gps4020_chan->regs;
    unsigned int baud_divisor = select_baud[new_config->baud];
    short word_len = select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5];
    short stop_bits = select_stop_bits[new_config->stop];
    short parity =   select_parity[new_config->parity];
    short mode = word_len | stop_bits | parity;
    int prescale = 0;

    if (mode >= 0) {
        while (baud_divisor > 0xFF) {
            prescale++;
            baud_divisor >>= 1;
        }
#ifdef CYGDBG_IO_INIT
        diag_printf("I/O MODE: %x, BAUD: %x\n", mode, baud_divisor);
        CYGACC_CALL_IF_DELAY_US((cyg_int32)2*100000);
#endif
        regs->mode = mode | SMR_DIV(prescale);
        regs->baud = baud_divisor;
        regs->modem_control = SMR_DTR | SMR_RTS;
        regs->control = SCR_TEN | SCR_REN | SCR_TIE | SCR_RIE;
        if (new_config != &chan->config) {
            chan->config = *new_config;
        }
        return true;
    } else {
        return false;
    }
}

// Function to initialize the device.  Called at bootstrap time.
static bool 
gps4020_serial_init(struct cyg_devtab_entry *tab)
{
    serial_channel *chan = (serial_channel *)tab->priv;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
#ifdef CYGDBG_IO_INIT
    diag_printf("GPS4020 SERIAL init - dev: %x.%d\n", gps4020_chan->regs, gps4020_chan->tx_int_num);
#endif
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
    if (chan->out_cbuf.len != 0) {
        cyg_drv_interrupt_create(gps4020_chan->tx_int_num,
                                 99,                     // Priority - unused
                                 (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
                                 gps4020_serial_tx_ISR,
                                 gps4020_serial_tx_DSR,
                                 &gps4020_chan->serial_tx_interrupt_handle,
                                 &gps4020_chan->serial_tx_interrupt);
        cyg_drv_interrupt_attach(gps4020_chan->serial_tx_interrupt_handle);
        cyg_drv_interrupt_mask(gps4020_chan->tx_int_num);
        gps4020_chan->tx_enabled = false;
    }
    if (chan->in_cbuf.len != 0) {
        cyg_drv_interrupt_create(gps4020_chan->rx_int_num,
                                 99,                     // Priority - unused
                                 (cyg_addrword_t)chan,   //  Data item passed to interrupt handler
                                 gps4020_serial_rx_ISR,
                                 gps4020_serial_rx_DSR,
                                 &gps4020_chan->serial_rx_interrupt_handle,
                                 &gps4020_chan->serial_rx_interrupt);
        cyg_drv_interrupt_attach(gps4020_chan->serial_rx_interrupt_handle);
        cyg_drv_interrupt_unmask(gps4020_chan->rx_int_num);
    }
    gps4020_serial_config_port(chan, &chan->config, true);
    return true;
}

// This routine is called when the device is "looked" up (i.e. attached)
static Cyg_ErrNo 
gps4020_serial_lookup(struct cyg_devtab_entry **tab, 
                  struct cyg_devtab_entry *sub_tab,
                  const char *name)
{
    serial_channel *chan = (serial_channel *)(*tab)->priv;
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
    return ENOERR;
}

// Send a character to the device output buffer.
// Return 'true' if character is sent to device
static bool
gps4020_serial_putc(serial_channel *chan, unsigned char c)
{
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    volatile struct _gps4020_uart *regs = (volatile struct _gps4020_uart *)gps4020_chan->regs;

    if ((regs->status & SSR_TxEmpty) != 0) {
        // Transmit buffer/FIFO is not full
        regs->TxRx = c;
        return true;
    } else {
        // No space
        return false;
    }
}

// Fetch a character from the device input buffer, waiting if necessary
static unsigned char 
gps4020_serial_getc(serial_channel *chan)
{
    unsigned char c;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    volatile struct _gps4020_uart *regs = (volatile struct _gps4020_uart *)gps4020_chan->regs;

    while ((regs->status & SSR_RxFull) == 0) ; // Wait for character
    c = regs->TxRx;
    return c;
}

// Set up the device characteristics; baud rate, etc.
static Cyg_ErrNo
gps4020_serial_set_config(serial_channel *chan, cyg_uint32 key,
                          const void *xbuf, cyg_uint32 *len)
{
    switch (key) {
    case CYG_IO_SET_CONFIG_SERIAL_INFO:
      {
        cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
        if ( *len < sizeof(cyg_serial_info_t) ) {
            return -EINVAL;
        }
        *len = sizeof(cyg_serial_info_t);
        if ( true != gps4020_serial_config_port(chan, config, false) )
            return -EINVAL;
      }
      break;
    default:
        return -EINVAL;
    }
    return ENOERR;
}

// Enable the transmitter (interrupt) on the device
static void
gps4020_serial_start_xmit(serial_channel *chan)
{
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    gps4020_chan->tx_enabled = true;
    cyg_drv_interrupt_unmask(gps4020_chan->tx_int_num);
}

// Disable the transmitter on the device
static void 
gps4020_serial_stop_xmit(serial_channel *chan)
{
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    cyg_drv_interrupt_mask(gps4020_chan->tx_int_num);
    gps4020_chan->tx_enabled = false;
}

// Serial I/O - low level Tx interrupt handler (ISR)
static cyg_uint32 
gps4020_serial_tx_ISR(cyg_vector_t vector, cyg_addrword_t data)
{
    serial_channel *chan = (serial_channel *)data;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    cyg_drv_interrupt_mask(gps4020_chan->tx_int_num);
    cyg_drv_interrupt_acknowledge(gps4020_chan->tx_int_num);
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
}

// Serial I/O - high level Tx interrupt handler (DSR)
static void       
gps4020_serial_tx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
{
    serial_channel *chan = (serial_channel *)data;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    (chan->callbacks->xmt_char)(chan);
    if (gps4020_chan->tx_enabled) {
        cyg_drv_interrupt_unmask(gps4020_chan->tx_int_num);
    }
}

// Serial I/O - low level Rx interrupt handler (ISR)
static cyg_uint32 
gps4020_serial_rx_ISR(cyg_vector_t vector, cyg_addrword_t data)
{
    serial_channel *chan = (serial_channel *)data;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    cyg_drv_interrupt_mask(gps4020_chan->rx_int_num);
    cyg_drv_interrupt_acknowledge(gps4020_chan->rx_int_num);
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
}

// Serial I/O - high level Rx interrupt handler (DSR)
static void       
gps4020_serial_rx_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
{
    serial_channel *chan = (serial_channel *)data;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    volatile struct _gps4020_uart *regs = (volatile struct _gps4020_uart *)gps4020_chan->regs;

    while ((regs->status & SSR_RxFull) != 0) 
        (chan->callbacks->rcv_char)(chan, regs->TxRx);
    cyg_drv_interrupt_unmask(gps4020_chan->rx_int_num);
}

#if 0 // FIXME - handle modem & errors
// Serial I/O - low level Ms interrupt handler (ISR)
static cyg_uint32 
gps4020_serial_ms_ISR(cyg_vector_t vector, cyg_addrword_t data)
{
    serial_channel *chan = (serial_channel *)data;
    gps4020_serial_info *gps4020_chan = (gps4020_serial_info *)chan->dev_priv;
    cyg_drv_interrupt_mask(gps4020_chan->ms_int_num);
    cyg_drv_interrupt_acknowledge(gps4020_chan->ms_int_num);
    return CYG_ISR_CALL_DSR;  // Cause DSR to be run
}

// Serial I/O - high level Ms interrupt handler (DSR)
static void       
gps4020_serial_ms_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
{
}
#endif

⌨️ 快捷键说明

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