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

📄 hal_aux.c

📁 开放源码实时操作系统源码.
💻 C
📖 第 1 页 / 共 2 页
字号:
    cyg_uint32 addr;
    cyg_uint8 cfg_val = (cyg_uint8)0xFF;

#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%s(bus=%x, devfn=%x, offset=%x) = ", __FUNCTION__, bus, devfn, offset);
#endif // CYGPKG_IO_PCI_DEBUG
    addr = _cfg_sel(bus, devfn, offset);
    HAL_READ_UINT8LE(addr, cfg_val);
#if 0
    HAL_READ_UINT16(_CSB281_PCI_STAT_CMD, status);
    if (status & _CSB281_PCI_STAT_ERROR_MASK) {
        // Cycle failed - clean up and get out
        cfg_val = (cyg_uint8)0xFF;
        HAL_WRITE_UINT16(_CSB281_PCI_STAT_CMD, status & _CSB281_PCI_STAT_ERROR_MASK);
    }
#endif
#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%x\n", cfg_val);
#endif // CYGPKG_IO_PCI_DEBUG
    HAL_WRITE_UINT32(_CSB281_PCI_CONFIG_ADDR, 0);
    return cfg_val;
}

externC cyg_uint16 
_csb281_pci_cfg_read_uint16(int bus, int devfn, int offset)
{
    cyg_uint32 addr;
    cyg_uint16 cfg_val = (cyg_uint16)0xFFFF;

#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%s(bus=%x, devfn=%x, offset=%x) = ", __FUNCTION__, bus, devfn, offset);
#endif // CYGPKG_IO_PCI_DEBUG
    addr = _cfg_sel(bus, devfn, offset);    
    HAL_READ_UINT16LE(addr, cfg_val);
#if 0
    HAL_READ_UINT16LE(_CSB281_PCI_STAT_CMD, status);
    if (status & _CSB281_PCI_STAT_ERROR_MASK) {
        // Cycle failed - clean up and get out
        cfg_val = (cyg_uint16)0xFFFF;
        HAL_WRITE_UINT16(_CSB281_PCI_STAT_CMD, status & _CSB281_PCI_STAT_ERROR_MASK);
    }
#endif
#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%x\n", cfg_val);
#endif // CYGPKG_IO_PCI_DEBUG
    HAL_WRITE_UINT32(_CSB281_PCI_CONFIG_ADDR, 0);
    return cfg_val;
}

externC cyg_uint32 
_csb281_pci_cfg_read_uint32(int bus, int devfn, int offset)
{
    cyg_uint32 addr;
    cyg_uint32 cfg_val = (cyg_uint32)0xFFFFFFFF;

#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%s(bus=%x, devfn=%x, offset=%x) = ", __FUNCTION__, bus, devfn, offset);
#endif // CYGPKG_IO_PCI_DEBUG
    addr = _cfg_sel(bus, devfn, offset);
    HAL_READ_UINT32LE(addr, cfg_val);
#if 0
    HAL_READ_UINT16(_CSB281_PCI_STAT_CMD, status);
    if (status & _CSB281_PCI_STAT_ERROR_MASK) {
        // Cycle failed - clean up and get out
        cfg_val = (cyg_uint32)0xFFFFFFFF;
        HAL_WRITE_UINT16(_CSB281_PCI_STAT_CMD, status & _CSB281_PCI_STAT_ERROR_MASK);
    }
#endif
#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%x\n", cfg_val);
#endif // CYGPKG_IO_PCI_DEBUG
    HAL_WRITE_UINT32(_CSB281_PCI_CONFIG_ADDR, 0);
    return cfg_val;
}

externC void
_csb281_pci_cfg_write_uint8(int bus, int devfn, int offset, cyg_uint8 cfg_val)
{
    cyg_uint32 addr;

#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%s(bus=%x, devfn=%x, offset=%x, val=%x)\n", __FUNCTION__, bus, devfn, offset, cfg_val);
#endif // CYGPKG_IO_PCI_DEBUG
    addr = _cfg_sel(bus, devfn, offset);
    HAL_WRITE_UINT8LE(addr, cfg_val);
#if 0
    HAL_READ_UINT16(_CSB281_PCI_STAT_CMD, status);
    if (status & _CSB281_PCI_STAT_ERROR_MASK) {
        // Cycle failed - clean up and get out
        HAL_WRITE_UINT16(_CSB281_PCI_STAT_CMD, status & _CSB281_PCI_STAT_ERROR_MASK);
    }
#endif
    HAL_WRITE_UINT32(_CSB281_PCI_CONFIG_ADDR, 0);
}

externC void
_csb281_pci_cfg_write_uint16(int bus, int devfn, int offset, cyg_uint16 cfg_val)
{
    cyg_uint32 addr;

#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%s(bus=%x, devfn=%x, offset=%x, val=%x)\n", __FUNCTION__, bus, devfn, offset, cfg_val);
#endif // CYGPKG_IO_PCI_DEBUG
    addr = _cfg_sel(bus, devfn, offset);
    HAL_WRITE_UINT16LE(addr, cfg_val);
#if 0
    HAL_READ_UINT16(_CSB281_PCI_STAT_CMD, status);
    if (status & _CSB281_PCI_STAT_ERROR_MASK) {
        // Cycle failed - clean up and get out
        HAL_WRITE_UINT16(_CSB281_PCI_STAT_CMD, status & _CSB281_PCI_STAT_ERROR_MASK);
    }
#endif
    HAL_WRITE_UINT32(_CSB281_PCI_CONFIG_ADDR, 0);
}

externC void
_csb281_pci_cfg_write_uint32(int bus, int devfn, int offset, cyg_uint32 cfg_val)
{
    cyg_uint32 addr;

#ifdef CYGPKG_IO_PCI_DEBUG
    diag_printf("%s(bus=%x, devfn=%x, offset=%x, val=%x)\n", __FUNCTION__, bus, devfn, offset, cfg_val);
#endif // CYGPKG_IO_PCI_DEBUG
    addr = _cfg_sel(bus, devfn, offset);
    HAL_WRITE_UINT32LE(addr, cfg_val);
#if 0
    HAL_READ_UINT16(_CSB281_PCI_STAT_CMD, status);
    if (status & _CSB281_PCI_STAT_ERROR_MASK) {
        // Cycle failed - clean up and get out
        HAL_WRITE_UINT16(_CSB281_PCI_STAT_CMD, status & _CSB281_PCI_STAT_ERROR_MASK);
    }
#endif
    HAL_WRITE_UINT32(_CSB281_PCI_CONFIG_ADDR, 0);
}

//--------------------------------------------------------------------------
// I2C support
static void
_csb281_i2c_init(void)
{
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_ALL);   // SDA=1, SCL=1
}

static void
_csb281_i2c_delay(void)
{
    int ctr;

    for (ctr = 0;  ctr < 100*10;  ctr++);
}

// Issue start sequence which is SDA(1->0) with SCL(1)
static void
_csb281_i2c_start(void)
{
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SDA);   // SDA=1, SCL=?
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SCL);   // SDA=1, SCL=1
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SDA);   // SDA=0, SCL=1
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SCL);   // SDA=0, SCL=0
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SDA);   // SDA=1, SCL=1
    _csb281_i2c_delay();
}

// Issue stop sequence which is SDA(0->1) with SCL(1)
static void
_csb281_i2c_stop(void)
{
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SDA);   // SDA=0, SCL=?
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SCL);   // SDA=1, SCL=1
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SDA);   // SDA=1, SCL=1
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SCL);   // SDA=0, SCL=0
    _csb281_i2c_delay();
}

// Send an 8-bit value, MSB first, SCL(1->0) clocks the data
static int
_csb281_i2c_put(unsigned char val)
{
    int bit, csr;

    for (bit = 7;  bit >= 0;  bit--) {
        if ((val & (1 << bit))) {
            HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SDA);   // SDA=1, SCL=?
        } else {
            HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SDA);   // SDA=0, SCL=?
        }
        _csb281_i2c_delay();
        HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SCL);   // SDA=?, SCL=1
        _csb281_i2c_delay();
        HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SCL);   // SDA=?, SCL=0
    }
    // Now wait for ACK
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SDA);   // SDA=1, SCL=0
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SCL);   // SDA=1, SCL=1
    _csb281_i2c_delay();
    HAL_READ_UINT32(_CSB281_2WCSR, csr);  // Read current state
    if ((csr & _CSB281_2WCSR_GET_SDA)) {
        // No ACK!
        return -1;
    }
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SCL);   // SDA=?, SCL=0
    _csb281_i2c_delay();
    return 0;
}

static unsigned char
_csb281_i2c_get(void)
{
    unsigned char val = 0;
    int bit, csr;

    for (bit = 7;  bit >= 0;  bit--) {
        HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SCL);   // SDA=?, SCL=1
        _csb281_i2c_delay();
        HAL_READ_UINT32(_CSB281_2WCSR, csr);  // Read current state
        if ((csr & _CSB281_2WCSR_GET_SDA)) {
            val |= (1 << bit);
        }
        HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SCL);   // SDA=?, SCL=0
        _csb281_i2c_delay();
    }
    // Need extra transition (for ACK time slot)
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_SET_SCL);   // SDA=?, SCL=0
    _csb281_i2c_delay();
    HAL_WRITE_UINT32(_CSB281_2WCSR, _CSB281_2WCSR_CLR_SCL);   // SDA=?, SCL=0
    _csb281_i2c_delay();
    return val;
}

int
_csb281_i2c_write_reg(int addr, int reg, unsigned char val)
{
    _csb281_i2c_start();
    if (_csb281_i2c_put(addr << 1) < 0) {
        return -1;
    }
    if (_csb281_i2c_put(reg) < 0) {
        return -1;
    }
    if (_csb281_i2c_put(val) < 0) {
        return -1;
    }
    _csb281_i2c_stop();
    return 0;
}

int
_csb281_i2c_read_reg(int addr, int reg)
{
    unsigned char val;
    _csb281_i2c_start();
    if (_csb281_i2c_put(addr << 1) < 0) {
        return -1;
    }
    if (_csb281_i2c_put(reg) < 0) {
        return -1;
    }
    _csb281_i2c_start();
    if (_csb281_i2c_put((addr << 1) | 0x01) < 0) {
        return -1;
    }
    val = _csb281_i2c_get();
    _csb281_i2c_stop();
    return val;
}

//--------------------------------------------------------------------------
// FS6377 Clock generator support

static unsigned char _fs6377_init_data[] = {
    0x28, 0xEF, 0x53, 0x03, 0x4B, 0x80, 0x32, 0x80,
    0x94, 0x32, 0x80, 0xD4, 0x56, 0xF6, 0xF6, 0xE0
};

// Setup for CRT mode 640x480 @75Hz
static unsigned char _fs6377_init_data_CRT[] = {
    0x10, 0x3b, 0x49, 0x03, 0x4B, 0x80, 0x32, 0x80,
    0x94, 0x32, 0x80, 0xD4, 0x66, 0xF6, 0xF6, 0xE0 
};

void
_csb281_fs6377_init(int mode)
{
    int reg;
    unsigned char *data;

    if (mode) {
        data = _fs6377_init_data_CRT;
    } else {
        data = _fs6377_init_data;
    }
    for (reg = 0;  reg < 16;  reg++) {
        if (_csb281_i2c_write_reg(_CSB281_FS6377_DEV, reg, *data++) < 0) {
            diag_printf("** Can't write FS6377 register %d\n", reg);
            return;
        }
    }
}

//--------------------------------------------------------------------------
// Blink the value 'val' into the LEDs
//   LED0 - clock
//   LED1 - value

static void
_set_leds(int led0, int led1)
{
    cyg_uint32 bcsr;

    HAL_READ_UINT32(_CSB281_BCSR, bcsr);
    bcsr = _one_bit(bcsr, (_CSB281_BCSR_LED0 | _CSB281_BCSR_LED1));
    if (led0) bcsr = _zero_bit(bcsr, _CSB281_BCSR_LED0);
    if (led1) bcsr = _zero_bit(bcsr, _CSB281_BCSR_LED1);
    HAL_WRITE_UINT32(_CSB281_BCSR, bcsr);
}

static void
_led_delay(int len)
{
    int ctr, limit;
    int cache_state;

    HAL_ICACHE_IS_ENABLED(cache_state);
    limit = cache_state ? 0x100000 : 0x40000;
    while (len--) {
        for (ctr = 0;  ctr < limit;  ctr++);
    }
}

void
_csb281_led(int val)
{
    int bit, ctr;

    for (ctr = 0;  ctr < 8;  ctr++) {
        _set_leds(0,0);
        _led_delay(1);
        _set_leds(0,1);
        _led_delay(1);
    }
    _set_leds(0,0);
    _led_delay(16);
    for (bit = 7;  bit >= 0;  bit--) {
        _set_leds(1, val & (1<<bit));
        _led_delay(8);
        _set_leds(0, 0);
        _led_delay(8);
    }
}

// EOF hal_aux.c

⌨️ 快捷键说明

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