📄 sysmoti2c.c
字号:
}/******************************************************************************** i2cDoOpPCA9555 - write the specified number of blocks** This function's purpose is to write the specified number of* blocks to the specified device.** NOTE:* Writing: read contents of register, mask bit positions and OR in data * and write out. writes only occur to output ports and config regs.** Reading: only from input ports and config regs.* * RETURNS: Number of bytes written, or ERROR if bad request.** ERRNO*/int i2cDoOpPCA9555 ( int unit, UINT32 deviceAddress, /* device I2C bus address */ i2cCmdPckt_t * pI2cCmdPacket /* pointer to command packet */ ) { i2cDrvRoutines_t *pRoutine; /* low level routines table pointer */ int byteCount; /* byte counter */ int statusVariable; /* local status variable */ unsigned char *pWriteData; /* pointer to write data buffer */ /* Initialize pointer to driver routines table. */ pRoutine = i2cDrvRoutinesTables[I2C_DRV_TYPE]; /* * read operation, * */ if ( pI2cCmdPacket->command == I2C_READOP ) { /* Read the specified number of bytes from the EEPROM. */ /* NOTE: random read has a dummy write first. */ statusVariable = 0; if ( I2C_KNOWN_STATE(unit) ) { pI2cCmdPacket->status = I2C_ERROR_KNOWN_STATE; return ERROR ; } if ( I2C_CYCLE_START(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_START ; return ERROR ; } /* device address - write, R/W bit = 0 */ if ( I2C_CYCLE_WRITE(unit,i2cAddressMunge(deviceAddress)) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_WRITE; return ERROR ; } if ( I2C_CYCLE_ACKIN(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_ACKIN ; return ERROR ; } /* command byte (register #) */ if ( I2C_CYCLE_WRITE( unit, (pI2cCmdPacket->blockNumber&0x7) ) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_WRITE; return ERROR ; } if ( I2C_CYCLE_ACKIN(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_ACKIN; return ERROR ; } if ( I2C_CYCLE_START(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_START; return ERROR ; } /* device address - read, R/W bit = 1 */ if ( I2C_CYCLE_WRITE(unit,i2cAddressMunge(deviceAddress)|0x1) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_WRITE; return ERROR ; } if ( I2C_CYCLE_ACKIN(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_ACKIN; return ERROR ; } /* nBlocks should be a power of 2. *//* generate ack on next to last byte; generate stop on last byte */ for ( byteCount = 0; byteCount < pI2cCmdPacket->nBlocks; byteCount++ ) { if (byteCount == (pI2cCmdPacket->nBlocks-2)) { /* Send an ACK on next to last transfer */ if ( I2C_CYCLE_READ(unit,(unsigned char *)pI2cCmdPacket->memoryAddress+byteCount,1) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_READ; return ERROR ; } } else { if (byteCount == (pI2cCmdPacket->nBlocks-1)) { /* send a STOP on last transfer */ if ( I2C_CYCLE_STOP(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_STOP; return ERROR ; } } /* No acks on all other transfers */ if ( I2C_CYCLE_READ(unit,(unsigned char *)pI2cCmdPacket->memoryAddress+byteCount,0) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_READ; return ERROR ; } } /* Increment the actual count of the command packet. */ pI2cCmdPacket->aCount += 1;#ifdef I2C_DRIVER_DEBUG logMsg("byteCount - 0x%x.\n", pI2cCmdPacket->aCount,2,3,4,5,6);#endif } /* * update the caller's command packet with status of * the operation */ pI2cCmdPacket->status = statusVariable; } /* * write operation * * */ else if ( pI2cCmdPacket->command == I2C_WRITOP ) { /* Initialize pointer to caller's write data buffer. */ pWriteData = (unsigned char *)pI2cCmdPacket->memoryAddress; /* Write the specified number of bytes from the EEPROM. */ statusVariable = 0; if ( I2C_KNOWN_STATE(unit) ) { pI2cCmdPacket->status = I2C_ERROR_KNOWN_STATE; return ERROR ; } if ( I2C_CYCLE_START(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_START; return ERROR ; } /* device address - write, R/W bit = 0 */ if ( I2C_CYCLE_WRITE(unit,i2cAddressMunge(deviceAddress)) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_WRITE; return ERROR ; } if ( I2C_CYCLE_ACKIN(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_ACKIN; return ERROR ; } /* command byte (register #) */ if ( I2C_CYCLE_WRITE( unit, (pI2cCmdPacket->blockNumber&0x7) ) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_WRITE; return ERROR ; } if ( I2C_CYCLE_ACKIN(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_ACKIN; return ERROR ; } /* nBlocks should be a power of 2 */ for ( byteCount = 0; byteCount < pI2cCmdPacket->nBlocks; byteCount++ ) { /* write data */ if ( I2C_CYCLE_WRITE(unit, pWriteData[byteCount]) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_WRITE; return ERROR ; } if ( I2C_CYCLE_ACKIN(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_ACKIN; return ERROR ; } /* Increment the actual count of the command packet. */ pI2cCmdPacket->aCount += 1;#ifdef I2C_DRIVER_DEBUG logMsg("byteCount - 0x%x.\n", pI2cCmdPacket->aCount,2,3,4,5,6);#endif } if ( I2C_CYCLE_STOP(unit) ) { pI2cCmdPacket->status = I2C_ERROR_CYCLE_STOP; return ERROR ; } } else { I2C_KNOWN_STATE(unit); return ERROR ; } /* Leave the I2C bus in a known state. */ I2C_KNOWN_STATE(unit); /* * update the caller's command packet with status of * the operation */ pI2cCmdPacket->status = statusVariable; return OK ; }/******************************************************************************** i2cDoOpAT24C256 - write the specified number of blocks** This function's purpose is to write the specified number of* blocks to the specified device.** NOTE: This driver only uses random byte reads and writes. It can be* upgraded to use page mode which is faster.** RETURNS: Number of bytes written, or ERROR if bad request.** ERRNO*/int i2cDoOpAT24C256 ( int unit, UINT32 deviceAddress, /* device I2C bus address */ i2cCmdPckt_t *pI2cCmdPacket /* pointer to command packet */ ) { i2cDrvRoutines_t *pRoutine; /* low level routines table pointer */ int byteCount; /* byte counter */ int statusVariable; /* local status variable */ unsigned char *pWriteData; /* pointer to write data buffer */ /* Initialize pointer to driver routines table. */ pRoutine = i2cDrvRoutinesTables[I2C_DRV_TYPE]; /* * read operation (EEPROM type devices), for each byte * perform the random read operation */ if ( pI2cCmdPacket->command == I2C_READOP ) { /* Read the specified number of bytes from the EEPROM. */ /* NOTE: random read has a dummy write first. */ statusVariable = 0; if ( I2C_KNOWN_STATE(unit) ) { statusVariable = I2C_ERROR_KNOWN_STATE; byteCount = pI2cCmdPacket->nBlocks; } for ( byteCount = 0; byteCount < pI2cCmdPacket->nBlocks; byteCount++ ) { if ( I2C_CYCLE_START(unit) ) { statusVariable = I2C_ERROR_CYCLE_START; break; } /* device address - write */ if ( I2C_CYCLE_WRITE(unit,i2cAddressMunge(deviceAddress)) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } /* 1st word address */ if ( I2C_CYCLE_WRITE(unit, (((pI2cCmdPacket->blockNumber+byteCount)>>8)&0x7f)) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } /* 2nd word address... */ if ( I2C_CYCLE_WRITE(unit,((pI2cCmdPacket->blockNumber+byteCount)&0xff)) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } if ( I2C_CYCLE_START(unit) ) { statusVariable = I2C_ERROR_CYCLE_START; break; } /* device address - read */ if ( I2C_CYCLE_WRITE(unit,i2cAddressMunge(deviceAddress)|0x1) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } if ( I2C_CYCLE_READ(unit,(unsigned char *)pI2cCmdPacket->memoryAddress+byteCount,0) ) { statusVariable = I2C_ERROR_CYCLE_READ; break; } if ( I2C_CYCLE_STOP(unit) ) { statusVariable = I2C_ERROR_CYCLE_STOP; break; } /* Increment the actual count of the command packet. */ pI2cCmdPacket->aCount += 1; } /* * update the caller's command packet with status of * the operation */ pI2cCmdPacket->status = statusVariable; } /* * write operation (EEPROM type devices), for each byte * perform the byte write operation, a delay must be * exercised following each byte write */ else if ( pI2cCmdPacket->command == I2C_WRITOP ) { /* Initialize pointer to caller's write data buffer. */ pWriteData = (unsigned char *)pI2cCmdPacket->memoryAddress; /* Write the specified number of bytes from the EEPROM. */ statusVariable = 0; if ( I2C_KNOWN_STATE(unit) ) { statusVariable = I2C_ERROR_KNOWN_STATE; byteCount = pI2cCmdPacket->nBlocks; } for ( byteCount = 0; byteCount < pI2cCmdPacket->nBlocks; byteCount++ ) { if ( I2C_CYCLE_START(unit) ) { statusVariable = I2C_ERROR_CYCLE_START; break; } /* device address */ if ( I2C_CYCLE_WRITE(unit,i2cAddressMunge(deviceAddress )) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } /* 1st word address */ if ( I2C_CYCLE_WRITE(unit,(((pI2cCmdPacket->blockNumber+byteCount)>>8)&0x7f)) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } /* 2nd word address... */ if ( I2C_CYCLE_WRITE(unit,((pI2cCmdPacket->blockNumber+byteCount)&0xff)) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } /* write data */ if ( I2C_CYCLE_WRITE(unit,*(pWriteData + byteCount)) ) { statusVariable = I2C_ERROR_CYCLE_WRITE; break; } if ( I2C_CYCLE_ACKIN(unit) ) { statusVariable = I2C_ERROR_CYCLE_ACKIN; break; } if ( I2C_CYCLE_STOP(unit) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -