static int receive(byte* out, int len, int utime, int countOut) { int ret = 0; byte buf = 0; int count = 0; do { waitUs(utime); buf = spi_next_transmit(0x00); if(buf == READY) break; waitUs(utime); ++count; } while ((countOut == 0) || (count < countOut)); if (buf == READY) { for(int i = 0; i< len; i++) out[i] = spi_next_transmit(0x00); //ret = read(out, len); ret = 0; } else if (buf == BUSY){ ret = TIME_OUT_REACHED; } else { ret = buf; } return ret; }
void spi_next_init() { spi_next = NEXT_SPI_Init(NULL); NEXT_RESET_ClrVal(NULL); //Waitms(1); waitUs(1000); NEXT_RESET_SetVal(NULL); }
uint8_t spi_next_transmit_bloc(uint8_t *data, uint8_t data_length, uint8_t *buffer, uint16_t buffer_length) { uint8_t retval = 0; NEXT_SPI_ReceiveBlock(spi_next, buffer, buffer_length); NEXT_SPI_SendBlock(spi_next, data, data_length); while(NEXT_SPI_GetReceivedDataNum(spi_next) != buffer_length) waitUs(1000); //WAIT1_Waitms(1); return retval; }
uint8_t spi_next_transmit(uint8_t octet) { uint8_t retval = 0; NEXT_SPI_ReceiveBlock(spi_next, &retval, 1); NEXT_SPI_SendBlock(spi_next, &octet,1); while(NEXT_SPI_GetReceivedDataNum(spi_next) == 0) waitUs(1000); //WAIT1_Waitms(1); return retval; }
Bool private_e2p_writebuffer(UInt32 addr, UInt32 size, UInt8 *buffer, UInt32 totalSize) { UInt32 j=0; Int32 remain = size; UInt32 r = 0; Int32 count = 0; if((addr+size) >= totalSize) { return False; } while( remain > 0 ) { i2c_soft_start(); i2c_soft_send_byte(E2P_I2C_ADDR); i2c_soft_send_byte(addr>>8); i2c_soft_send_byte(addr); if(remain >= PAGE_SIZE) { for(j=0; j<PAGE_SIZE; j++) { i2c_soft_send_byte(buffer[count]); count++; remain--; } } else { r = remain; for(j=0; j<r; j++) { i2c_soft_send_byte(buffer[count]); count++; remain--; } } i2c_soft_stop(); waitUs(5000); addr+=PAGE_SIZE; } return True; }
UInt32 private_i2c0_send(UInt8 data) { UInt32 cnt = I2C_CNT; while( i2c0_write(data) == I2C_BUSY) { cnt--; waitUs(1); if(cnt==0) { return I2C_TIMEOUT; } } return 0; }
UInt32 private_i2c0_getStat(void) { UInt32 cnt = I2C_CNT; while( (I2C_I2CONSET & 0x08) == 0) { cnt--; waitUs(1); if(cnt==0) { return I2C_TIMEOUT; } } return 0; }
void i2c0_write_wait(UInt8 data) { UInt32 cnt = I2C_CNT; while( i2c0_write(data) == I2C_BUSY) { cnt--; waitUs(1); if(cnt==0) { //DebugPrintf("i2c0_write_wait timeout\r\n"); return; } } i2c0_wait_after_write(); }
static byte sync_send(byte query[4], byte* in, byte* out, int len, int utime, int countOut) { int ret = 0; byte needRestart = 0; ret = send(query, in, query[3]); waitUs(100); if(ret == 0) ret = receive(out, len, utime, countOut); if(ret != 0) needRestart = Next_clear(); if(needRestart != 0) { spi_next_resart(); } return ret; }
void i2c0_stop(void) { UInt32 cnt = I2C_CNT; I2C_I2CONSET |= 0x10; //STO = 1, set stop flag I2C_I2CONCLR = 0x08; //clear SI flag //wait for STOP detected (while STO = 1) while((I2C_I2CONSET & 0x10) == 0x10) { cnt--; waitUs(1); if(cnt==0) { return; } } }