EOWError ow_reset(void) { uint8_t test, ret; i2c_write(ds2482_address, &ds2482_reset, 1 ); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } ow_wait_on_busy(); i2c_write_read(ds2482_address, NULL, 0, &test, 1); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } test |= ds2482_pulse_detect; if (test == ds2482_busy) { return eOWSuccess; } else { return eOWNoPresencePulse; } }
static int mpc_write(struct mpc_i2c *i2c, int target, const u8 * data, int length, int restart) { int i; unsigned timeout = i2c->adap.timeout; u32 flags = restart ? CCR_RSTA : 0; /* Start with MEN */ if (!restart) writeccr(i2c, CCR_MEN); /* Start as master */ writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags); /* Write target byte */ writeb((target << 1), i2c->base + MPC_I2C_DR); if (i2c_wait(i2c, timeout, 1) < 0) return -1; for (i = 0; i < length; i++) { /* Write data byte */ writeb(data[i], i2c->base + MPC_I2C_DR); if (i2c_wait(i2c, timeout, 1) < 0) return -1; } return 0; }
uint8 hal_dev_mag3110_read_reg(uint8 addr) { uint8 result; i2c_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_WRITE); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_write_byte(I2C_MAG, addr); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_repeated_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_READ); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_set_rx_mode(I2C_MAG); i2c_give_nack(I2C_MAG); result = i2c_read_byte(I2C_MAG); i2c_wait(I2C_MAG); i2c_stop(I2C_MAG); result = i2c_read_byte(I2C_MAG); pause(); return result; }
void irom i2c_init(void) { uint8_t i; i2c_set_dc(1, 0); i2c_wait(5); // when SCL = 0, toggle SDA to clear up i2c_set_dc(0, 0); i2c_wait(5); i2c_set_dc(1, 0); i2c_wait(5); // set data_cnt to max value for (i = 0; i < 28; i++) { i2c_set_dc(1, 0); i2c_wait(5); // sda 1, scl 0 i2c_set_dc(1, 1); i2c_wait(5); // sda 1, scl 1 } // reset all i2c_stop(); return; }
int8_t ow_triplet(uint8_t* direction, uint8_t* first_bit, uint8_t* second_bit) { uint8_t send_buf[2], recv_buf; send_buf[0] = 0x78; // TODO why? if (*direction) { *direction = 0xFF; } send_buf[1] = *direction; // TODO Prüfe, ob hier write und read in einem schnurz gemacht werden darf! i2c_write(ds2482_address, send_buf, 2); uint8_t result = i2c_wait(); if ( result != TW_NO_INFO) { // According to the application notes of the DS2482 something is fishy here. // For now we just ignore the I2C bus error, it seems to work anyway... } i2c_write_read(ds2482_address, NULL, 0, &recv_buf, 1); if (i2c_wait() != TW_NO_INFO) { return eOWNoResponse; } *first_bit = ((recv_buf & 0x20) != 0); *second_bit = ((recv_buf & 0x40) != 0); *direction = ((recv_buf & 0x80) != 0); return eOWSuccess; }
int8_t ow_read_bit(void) { uint8_t buffer[2], ret; ow_write_bit(1); buffer[0] = ds2482_read_pointer_cmd; buffer[1] = ds2482_status_register; i2c_write(ds2482_address, buffer, 2); ret = i2c_wait(); if (ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } // TODO Prüfe, ob hier write und read in einem schnurz gemacht werden könnte! i2c_write_read(ds2482_address, NULL, 0, buffer, 1); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } if (buffer[0] & 0x20) { return 1; } else { return 0; } }
EOWError ow_write_byte(uint8_t write_byte) { uint8_t buffer[2]; buffer[0] = ds2482_read_pointer_cmd; buffer[1] = ds2482_status_register; i2c_write(ds2482_address, buffer, 2); uint8_t ret = i2c_wait(); if (ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } ow_wait_on_busy(); buffer[0] = ds2482_write_byte_cmd; buffer[1] = write_byte; i2c_write(ds2482_address, buffer, 2); ret = i2c_wait(); if (ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } return ow_wait_on_busy(); }
uint8_t I2C_ReadOneByte(uint8_t SlaveAddr, uint8_t RegAddr) { uint8_t result; i2c_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, RegAddr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_repeated_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_READ); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_set_rx_mode(I2C0_B); i2c_give_nack(I2C0_B); result = i2c_read_byte(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); result = i2c_read_byte(I2C0_B); pause(40); return result; }
unsigned char i2c_send_daten(unsigned char K1,unsigned char K2) { unsigned char m1=P0M1; //wert sichern unsigned char m2=P0M2; //wert sichern P0M1=0xFF; //eing�nge umschalten wegen Tastererweiterung P0M2=0x00; //eing�nge umschalten wegen Tastererweiterung I2SCLH=17; I2SCLL=17; I2CON=0; I2EN=1; //Master Transmit mode CRSEL=0; //CRSEL=0 STA=1; //send Start MASTER if(i2c_wait()!=0) return 0xff; STA=0; I2DAT = 0xa0+0;//a0= adresse i2c eeprom 0=schreiben SLA_W; //Adresse senden MASTER if(i2c_wait()!=0) return 0xff; I2DAT = 0x00; //Daten Senden MASTER if(i2c_wait()!=0) return 0xff; I2DAT = K1; //Daten Senden MASTER if(i2c_wait()!=0) return 0xff; I2DAT = K2; //Daten Senden MASTER //AA=0; if(i2c_wait()!=0) return 0xff; STO=1; //send Stop MASTER P0M1=m1; //eing�nge umschalten wegen Tastererweiterung P0M2=m2; //eing�nge umschalten wegen Tastererweiterung return 0; }
uint8_t i2cFlashRead(uint16_t addr, int numBytes, char *dataPtr){ int ret; int index; index = 0; numBytes--; i2c_start(intI2c); ret = i2c_write(intI2c, i2cFlashAddr | I2C_WRITE); //if (ret) return 1; ret = i2c_write(intI2c, ((addr) >> 8));// upper byte first //if (ret) return 1; ret = i2c_write(intI2c, ((addr) & 0xFF)); //if (ret) return 1; i2c_repeated_start(intI2c); i2c_write(intI2c, i2cFlashAddr | I2C_READ); // Send the read. i2c_set_rx(intI2c); dataPtr[0] = i2c_read(intI2c); // Dummy read i2c_wait(intI2c); while (numBytes--){ dataPtr[index++] = i2c_read(intI2c); i2c_give_ack(intI2c); i2c_wait(intI2c); } dataPtr[index++] = i2c_read(intI2c); i2c_give_nack(intI2c); i2c_stop(intI2c); return index; }
/** * @brief Reads multiple 8-bit registers from an I2C slave * @param[in] slaveId The slave device ID * @param[in] startRegisterAddress The first register address * @param[in] registerCount The number of registers to read; Must be greater than or equal to two. * @param[out] buffer The buffer to write into * @return 0 is successful, 1 if not */ static int i2c_read_registers_internal(register uint8_t slaveId, register uint8_t startRegisterAddress, register uint8_t registerCount, uint8_t *const buffer){ if(registerCount < 2) return -1; /* loop while the bus is still busy */ i2c_wait_while_busy(); /* send I2C start signal and set write direction, also enables ACK */ i2c_send_start(); /* send the slave address and wait for the I2C bus operation to complete */ i2c_send_byte(I2C_WRITE_ADDRESS(slaveId)); /* send the register address */ i2c_send_byte(startRegisterAddress); /* signal a repeated start condition */ i2c_send_repeated_start(); /* send the read address */ i2c_send_byte(I2C_READ_ADDRESS(slaveId)); /* switch to receive mode and assume more than one register */ i2c_enter_receive_mode_with_ack(); /* read a dummy byte to drive the clock */ i2c_read_dummy_byte(); /* for all remaining bytes, read */ --registerCount; uint8_t index = 0; while (--registerCount > 0) { /* fetch and store value */ register uint8_t value = I2C0->D; buffer[index++] = value; /* wait for completion */ i2c_wait(); } /* disable ACK and read second-to-last byte */ i2c_disable_ack(); /* fetch and store value */ buffer[index++] = I2C0->D; /* wait for completion */ i2c_wait(); /* stop signal */ i2c_send_stop(); /* fetch the last received byte */ buffer[index++] = I2C0->D; return 0; }
void irom i2c_start(void) { i2c_set_dc(1, m_nLastSCL); i2c_wait(5); i2c_set_dc(1, 1); i2c_wait(5); // sda 1, scl 1 i2c_set_dc(0, 1); i2c_wait(5); // sda 0, scl 1 }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop) { uint8_t tmp __attribute__((unused)); uint8_t status, count=0; rxBufferIndex = 0; rxBufferLength = 0; //serial_print("requestFrom\n"); // clear the status flags I2C0_S = I2C_S_IICIF | I2C_S_ARBL; // now take control of the bus... if (I2C0_C1 & I2C_C1_MST) { // we are already the bus master, so send a repeated start I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX; } else { // we are not currently the bus master, so wait for bus ready while (i2c_status() & I2C_S_BUSY) ; // become the bus master in transmit mode (send start) slave_mode = 0; I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX; } // send the address I2C0_D = (address << 1) | 1; i2c_wait(); status = i2c_status(); if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) { // the slave device did not acknowledge // or we lost bus arbitration to another master I2C0_C1 = I2C_C1_IICEN; return 0; } if (length == 0) { // TODO: does anybody really do zero length reads? // if so, does this code really work? I2C0_C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST); return 0; } else if (length == 1) { I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK; } else { I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST; } tmp = I2C0_D; // initiate the first receive while (length > 1) { i2c_wait(); length--; if (length == 1) I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK; rxBuffer[count++] = I2C0_D; } i2c_wait(); I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX; rxBuffer[count++] = I2C0_D; if (sendStop) I2C0_C1 = I2C_C1_IICEN; rxBufferLength = count; return count; }
uint8 i2c_ReadMultiByte(uint8 SlaveAddress,uint8 AccessAddress,uint8 *Data,uint8 Length) { uint8 i; I2C0_C1 |= 0x10; send_signal('S'); //发送开始信号 I2C0_D = SlaveAddress; //发送设备地址,并通知从机接收数据 if (i2c_wait('T')) //等待一个字节数据传送完成 return 1; //没有传送成功,读一个字节失败 if (i2c_wait('A')) //等待从机应答信号 return 1; //没有等到应答信号,读一个字节失败 I2C0_D = AccessAddress; //发送访问地址 if (i2c_wait('T')) //等待一个字节数据传送完成 return 1; //没有传送成功,读一个字节失败 if (i2c_wait('A')) //等待从机应答信号 return 1; //pause(); I2C0_C1 |= I2C_C1_RSTA_MASK; I2C0_D = SlaveAddress+1; //通知从机改为发送数据 if (i2c_wait('T')) //等待一个字节数据传送完成 return 1; //没有传送成功,读一个字节失败 if (i2c_wait('A')) //等待从机应答信号 return 1; //没有等到应答信号,读一个字节失败 I2C0_C1 &= ~I2C_C1_TX_MASK; I2C0_C1 &= ~I2C_C1_TXAK_MASK; *Data = I2C0_D; if (i2c_wait('T')) //等待一个字节数据传送完成 return 1; //没有传送成功,读一个字节失败 for(i=0;i<Length-2;i++) { *(Data+i)=I2C0_D; if (i2c_wait('T')) //等待一个字节数据传送完成 return 1; } //没有传送成功,读一个字节失败 *(Data+Length-2) = I2C0_D; I2C0_C1 |= I2C_C1_TXAK_MASK; if (i2c_wait('T')) //等待一个字节数据传送完成 return 1; //没有传送成功,读一个字节失败 i2c0_Stop(); *(Data+Length-1) = I2C0_D; pause(); return 0; }
int8_t i2c_write(uint8_t data) { i2c_wait(); SSPBUF = data; if (SSP1CON1bits.WCOL) return -1; return 0; }
static __inline__ int __i2c_read (u8 *data, int length) { int i; writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), &I2C->cr); /* dummy read */ readb(&I2C->dr); for (i=0; i < length; i++) { if (i2c_wait (I2C_READ) < 0) break; /* Generate ack on last next to last byte */ if (i == length - 2) writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, &I2C->cr); /* Generate stop on last byte */ if (i == length - 1) writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr); data[i] = readb(&I2C->dr); } return i; }
/** * Datentransfer per I2C-Bus * \param *pData Container mit den Daten fuer den Treiber * \return Resultat der Aktion */ uint8_t Send_to_TWI(tx_type_t * pData) { uint8_t toRead = 0; uint8_t toWrite = 0; uint8_t * pWrite = NULL; uint8_t * pRead = NULL; tx_type_t * ptr = pData; if (ptr == NULL) return TW_BUS_ERROR; /* Datenformat einlesen */ for (; ptr->slave_adr != OWN_ADR; ptr++) { if ((ptr->slave_adr & 0x1) == 1) { /* zu lesende Daten */ toRead = ptr->size; pRead = ptr->data_ptr; } else { /* zu sendende Daten */ toWrite = ptr->size; pWrite = ptr->data_ptr; } } /* Daten senden und empfangen per i2c-Treiber (blockierend) */ i2c_write_read(pData->slave_adr, pWrite, toWrite, pRead, toRead); uint8_t result = i2c_wait(); return (uint8_t) (result == TW_NO_INFO ? SUCCESS : result); }
static __inline__ int __i2c_read (u8 * data, int length) { int i; writel (M83xx_CCR_MEN | M83xx_CCR_MSTA | ((length == 1) ? M83xx_CCR_TXAK : 0), I2CCCR); /* dummy read */ readl (I2CCDR); for (i = 0; i < length; i++) { if (i2c_wait (I2C_READ) < 0) break; /* Generate ack on last next to last byte */ if (i == length - 2) writel (M83xx_CCR_MEN | M83xx_CCR_MSTA | M83xx_CCR_TXAK, I2CCCR); /* Generate stop on last byte */ if (i == length - 1) writel (M83xx_CCR_MEN | M83xx_CCR_TXAK, I2CCCR); data[i] = readl (I2CCDR); } return i; }
inline int i2c_write(I2C_MemMapPtr p, uint8_t data) { // Send data, wait, and return ACK status p->D = data; i2c_wait(p); return ((p->S & I2C_S_RXAK_MASK) == 0); }
struct i2c_config i2c_setup(uint32_t bus, uint32_t rate, uint8_t addr) { Sercom *sercom = sercom_enable_pclock(bus); sercom_i2c_pins(bus); SercomI2cm *si = &sercom->I2CM; i2c_init(bus, si); return (struct i2c_config){ .si=si, .addr=addr<<1 }; } static void i2c_wait(SercomI2cm *si) { for (;;) { uint32_t intflag = si->INTFLAG.reg; if (!(intflag & SERCOM_I2CM_INTFLAG_MB)) { if (si->STATUS.reg & SERCOM_I2CM_STATUS_BUSERR) shutdown("i2c buserror"); continue; } if (intflag & SERCOM_I2CM_INTFLAG_ERROR) shutdown("i2c error"); break; } } static void i2c_start(SercomI2cm *si, uint8_t addr) { si->ADDR.reg = addr; i2c_wait(si); }
void i2c_stop() { SSPIF = 0; SSPCON2bits.PEN = 1; //while (SSPCON2bits.PEN == 0); while (SSPIF == 0); SSPIF =0; i2c_wait(); }
EOWError ow_read_byte(uint8_t* read_byte) { uint8_t buffer[2], ret; buffer[0] = ds2482_read_pointer_cmd; buffer[1] = ds2482_status_register; i2c_write(ds2482_address, buffer, 2); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } ow_wait_on_busy(); buffer[0] = ds2482_read_byte_cmd; i2c_write(ds2482_address, buffer, 1); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } buffer[0] = ds2482_read_pointer_cmd; buffer[1] = ds2482_read_data_register; i2c_write(ds2482_address, buffer, 2); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } // TODO Prüfe, ob hier write und read in einem schnurz gemacht werden könnte! i2c_write_read(ds2482_address, NULL, 0, read_byte, 1); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } return eOWSuccess; }
uint8_t irom i2c_get_ack(void) { uint8_t retVal; i2c_set_dc(m_nLastSDA, 0); i2c_wait(5); i2c_set_dc(1, 0); i2c_wait(5); i2c_set_dc(1, 1); i2c_wait(5); retVal = i2c_get_dc(); i2c_wait(5); i2c_set_dc(1, 0); i2c_wait(5); return retVal; }
/*------------------------------------------------------------------------------ * Description: * Send one byte of data over I2C to a slave * * Parameters: * data - The byte of data to be sent to the slave * * Returns: * none -----------------------------------------------------------------------------*/ void i2c_tx_byte(uint8_t data) { //Dump data to be sent into buffer SSP1BUF = data; //Wait for data to send while(SSP1STATbits.BF); i2c_wait(); }
unsigned char i2c_send_byte(unsigned char byte) { SSPIF = 0; SSPBUF = byte; //while (BF); while (SSPIF == 0); SSPIF =0; i2c_wait(); return (SSPCON2bits.ACKSTAT == 1 ? 1 : 0); }
static int mpc_read(struct mpc_i2c *i2c, int target, u8 * data, int length, int restart) { unsigned timeout = i2c->adap.timeout; int i, result; u32 flags = restart ? CCR_RSTA : 0; /* Start with MEN */ if (!restart) writeccr(i2c, CCR_MEN); /* Switch to read - restart */ writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags); /* Write target address byte - this time with the read flag set */ writeb((target << 1) | 1, i2c->base + MPC_I2C_DR); result = i2c_wait(i2c, timeout, 1); if (result < 0) return result; if (length) { if (length == 1) writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK); else writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA); /* Dummy read */ readb(i2c->base + MPC_I2C_DR); } for (i = 0; i < length; i++) { result = i2c_wait(i2c, timeout, 0); if (result < 0) return result; /* Generate txack on next to last byte */ if (i == length - 2) writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK); /* Generate stop on last byte */ if (i == length - 1) writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK); data[i] = readb(i2c->base + MPC_I2C_DR); } return length; }
void I2C_WriteOneByte(uint8_t SlaveAddr, uint8_t RegAddr, uint8_t data) { i2c_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1)|I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, RegAddr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, data); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_stop(I2C0_B); pause(40); }
void hal_dev_mag3110_write_reg(uint8 addr, uint8 data) { i2c_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS|I2C_WRITE); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_write_byte(I2C_MAG, addr); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_write_byte(I2C_MAG, data); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_stop(I2C_MAG); pause(); }
unsigned char i2c_receive_byte_wo_ack() { // Receive Enalbe unsigned char ch; SSPCON2bits.RCEN = 1; while (!BF); ch = SSPBUF; i2c_wait(); i2c_nack(); return ch; }
//---------------------------------------------------------- // 1バイト受信 unsigned char i2c_readbyte(char noack) { if (noack) { TWCR = (1<<TWINT)|(1<<TWEN); //NO ACK } else { TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA); //ACK } i2c_wait(); return (TWDR); }