📄 bsio.c
字号:
{ long int stat = bsio_hold[ssel].last_stat; return (stat & BSIO_STAT_READERR_MASK) >> BSIO_STAT_READERR_OFFSET;} intbsio_stat_valbytes(int ssel){ long int stat = bsio_hold[ssel].last_stat; return (stat & BSIO_STAT_VALBYTES_MASK) >> BSIO_STAT_VALBYTES_OFFSET;} intbsio_stat_rdready(int ssel){ long int stat = bsio_hold[ssel].last_stat; return (stat & BSIO_STAT_RDREADY_MASK) >> BSIO_STAT_RDREADY_OFFSET;} intbsio_stat_wrready(int ssel){ long int stat = bsio_hold[ssel].last_stat; return (stat & BSIO_STAT_WRREADY_MASK) >> BSIO_STAT_WRREADY_OFFSET;}intbsio_trsfr_wrrem(int ssel){ long int trsfr = bsio_hold[ssel].trsfr; return (trsfr & BSIO_TRSFR_WRREM_MASK) >> BSIO_TRSFR_WRREM_OFFSET;}intbsio_trsfr_rdrem(int ssel){ long int trsfr = bsio_hold[ssel].trsfr; return (trsfr & BSIO_TRSFR_RDREM_MASK) >> BSIO_TRSFR_RDREM_OFFSET;}/////////////////////////////////////////////////////////////// This initializes the intc (interrupt control value in the // bsio_hold array. The array MUST STILL BE WRITTEN to the // actual bsio device.//////////////////////////////////////////////////////////////voidbsio_set_intc_register(long int ssel, long int opcompen, long int writerren, long int readerren, long int rdreadyen, long int wrreadyen ){ long int temp = 0; // clear the interrupt control register bsio_hold[ssel].intc = 0; temp = (opcompen << BSIO_INTC_OPCOMPEN_OFFSET) & BSIO_INTC_OPCOMPEN_MASK; bsio_hold[ssel].intc |= temp; temp = (writerren << BSIO_INTC_WRITERREN_OFFSET) & BSIO_INTC_WRITERREN_MASK; bsio_hold[ssel].intc |= temp; temp = (readerren << BSIO_INTC_READERREN_OFFSET) & BSIO_INTC_READERREN_MASK; bsio_hold[ssel].intc |= temp; temp = (rdreadyen << BSIO_INTC_RDREADYEN_OFFSET) & BSIO_INTC_RDREADYEN_MASK; bsio_hold[ssel].intc |= temp; temp = (wrreadyen << BSIO_INTC_WRREADYEN_OFFSET) & BSIO_INTC_WRREADYEN_MASK; bsio_hold[ssel].intc |= temp;}voidbsio_write_registers(int ssel){ bsio_registers* bsio = (void *) BSIO_BASE_ADDRESS; if ((ssel == 0) || (ssel ==1)) { bsio->config = bsio_hold[ssel].config; // transfer all controlling data from the appropriate bsio->mode = bsio_hold[ssel].mode; if (ssel == 0) { bsio->slave0 = bsio_hold[ssel].slave; // There are separate slave registers for the } // two chip selects else { bsio->slave1 = bsio_hold[ssel].slave; } bsio->intc = bsio_hold[ssel].intc; bsio_hold[ssel].last_stat = bsio->stat; // get back the current chip stat bits }}// typical use: bsio_initialize(0, 156250);cyg_boolbsio_initialize(spi_mode mode, int clock_freq){ int ssel = 0; int cpol = 0; int wrpol = 0; int rdpol = 0; int enpol = 0; // Setthe clock polarities for both slaves (for now) switch (mode) { case SPI_MODE_0: cpol = 0; wrpol = 0; rdpol = 1; break; case SPI_MODE_1: cpol = 0; wrpol = 1; rdpol = 0; break; case SPI_MODE_2: cpol = 1; wrpol = 1; rdpol = 0; break; case SPI_MODE_3: cpol = 1; wrpol = 0; rdpol = 1; break; } for (ssel = 0; ssel <= 1; ssel++) { bsio_set_config_register(ssel, 0, 0, clock_freq, 0, 0, 1); bsio_set_slave_register(ssel, 0, 0, cpol, wrpol, rdpol, enpol); bsio_set_intc_register(ssel, 0, 0, 0, 0, 0); } // Write the register block for chip-select 0 to the actual bsio registers bsio_write_registers(0); return 0;}cyg_boolbsio_write(int chip_select, long int control_word, int control_bit_length, char *write1_data, int write1_length, char *write_data, int write_length){ return bsio_write_read(chip_select, control_word, control_bit_length, write1_data, write1_length, write_data, write_length, 0, 0);}cyg_boolbsio_read(int chip_select, long int control_word, int control_bit_length, char *write1_data, int write1_length, char *read_data, int read_length ){ return bsio_write_read(chip_select, control_word, control_bit_length, write1_data, write1_length, 0, 0, read_data, read_length);}cyg_boolbsio_write_read(int chip_select, long int control_word, int control_length, char *write1_data, int write1_length, char *write_data, int write_length, char *read_data, int read_length ){ int i = 0; int write_data_count = 0; int write1_data_count = 0; int read_data_count = 0; long int hold_data = 0; int valid_bytes = 0; bsio_registers* bsio = (void *) BSIO_BASE_ADDRESS; if (chip_select != last_ssel) bsio_write_registers(chip_select); if (((write_length + write1_length) > 1023) || (read_length > 1023)) return 1; // First prime the "pump" with the control word and the first output word bsio->cwbuf = control_word; // Configure the control word transmission via the mode register bsio_write_mode_register(1, control_length); // Write up to 4 bytes hold_data = 0; for (i = 0; i < 4; i++) { if (i > 0) hold_data = hold_data << 8; if (write1_length > 0) { hold_data |= write1_data[write1_data_count++]; write1_length--; } if ((write1_length <= 0) && (write_length > 0)) { hold_data |= write_data[write_data_count++]; write_length--; } } bsio->rwbuf = hold_data; // Start the data transfer by writing to the transfer register bsio_write_trsfr_register(32, 32, 1, (write_length + write1_length), read_length); // keep the data pump full bsio_hold[chip_select].last_stat = bsio->stat; bsio_hold_errors.write_errors += bsio_stat_writerr(chip_select); bsio_hold[chip_select].trsfr = bsio->trsfr; // Continue while the write operation is in progress while (bsio_trsfr_wrrem(chip_select) > 0) { // if the bsio is ready for another word, then write it if (bsio_stat_wrready(chip_select) == 1) { // Write up to 4 bytes hold_data = 0; for (i = 0; i < 4; i++) { if (i > 0) hold_data = hold_data << 8; if (write1_length > 0) { hold_data |= write1_data[write1_data_count++]; write1_length--; } if ((write1_length <= 0) && (write_length > 0)) { hold_data |= write_data[write_data_count++]; write_length--; } } bsio->rwbuf = hold_data; } bsio_hold[chip_select].last_stat = bsio->stat; bsio_hold_errors.write_errors += bsio_stat_writerr(chip_select); bsio_hold[chip_select].trsfr = bsio->trsfr; } // Continue while the read operation is in progress while (bsio_trsfr_rdrem(chip_select) > 0) { if (bsio_stat_rdready(chip_select) == 1) { // read up to 4 bytes hold_data = bsio->rwbuf; valid_bytes = bsio_stat_valbytes(chip_select) + 1; while ((read_data_count < read_length) && (valid_bytes > 0)) { read_data[read_data_count++] = ((hold_data >> 24) & 0x000000FF); hold_data = hold_data << 8; valid_bytes--; } } bsio_hold[chip_select].last_stat = bsio->stat; bsio_hold_errors.read_errors += bsio_stat_readerr(chip_select); bsio_hold[chip_select].trsfr = bsio->trsfr; } return 0;}bsio_errors*bsio_get_error_counts(){ return &bsio_hold_errors;}voidbsio_reset_error_counts(){ bsio_hold_errors.write_errors = 0; bsio_hold_errors.read_errors = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -