📄 emi2c.c
字号:
RMuint8 *pData, RMuint32 uByteCount){ RMstatus er; RMuint32 uCount; RMuint8 nack; RMuint8 sa; assert(pData != 0); assert(pC != 0); sa = uSlaveAddress & 0xFE; if( uSlaveAddress & 0x01 ) DPRINT(("emi2c_write_subaddress - WARNING, original Slave address LSB=1, not typical write address, changed with &0xFE\n")); if( (er = emi2c_start(pC)) != RM_OK) { DPRINT(("emi2c_write_subaddress - emi2c_start() error %d\n", er)); goto emi2c_write_subaddress_error; } /* slave address W, need & 0xFE*/ if ( (er = emi2c_sendbyte(pC, sa, &nack)) != RM_OK ) { DPRINT(("emi2c_write_subaddress - Slave Address %x error %d\n", sa, er )); goto emi2c_write_subaddress_error; } if( nack != 0 ) { DPRINT(("emi2c_write_subaddress - Slave Address %x NACK %d\n", sa, nack)); goto emi2c_write_subaddress_error; } if ( (er = emi2c_sendbyte(pC, uSubAddress, &nack)) != RM_OK ) { DPRINT(("emi2c_write_subaddress - Sub Address %x error %d\n", uSubAddress, er)); goto emi2c_write_subaddress_error; } if( nack != 0 ) { DPRINT(("emi2c_write_subaddress - Sub Address %x NACK %d\n", uSubAddress, nack)); goto emi2c_write_subaddress_error; } for( uCount = 0; uCount < uByteCount; uCount++ ) { if( (er = emi2c_sendbyte(pC, pData[uCount], &nack)) != RM_OK ) { DPRINT(("emi2c_write_subaddress - Slave %x Sub %x Data %x Count %ld error %d\n", sa, uSubAddress, pData[uCount], uCount, er)); goto emi2c_write_subaddress_error; } if( nack != 0 ) { DPRINT(("emi2c_write_subaddress - Slave %x Sub %x Data %x Count %ld NACK %d\n", sa, uSubAddress, pData[uCount], uCount, nack)); goto emi2c_write_subaddress_error; } } if( (er = emi2c_stop(pC)) != RM_OK ) { DPRINT(("emi2c_write_subaddress - emi2c_stop() error %d\n", er)); return -1; } return RM_OK;emi2c_write_subaddress_error: if( (er = emi2c_stop(pC)) != RM_OK ) DPRINT(("emi2c_write_subaddress - emi2c_stop() error %d\n", er)); return -1;}RMstatus emi2c_read_subaddress( EMI2C_CONFIG* pC, RMuint8 uSlaveAddress, RMuint8 uSubAddress, RMuint8 *pData, RMuint32 *pByteCount, RMuint32 uFlags){ RMstatus er; RMuint32 uCount = 0; RMuint8 nack; RMuint8 saW; RMuint8 saR; assert(pData != 0); assert(pC != 0); assert(pByteCount != 0 ); assert(*pByteCount != 0); // no stray flags. assert( (uFlags & (~(EMI2C_RD_SUB_STOPONRESTART | EMI2C_RD_SUB_NONACKLASTBYTE )) ) == 0); saW = uSlaveAddress & 0xFE; saR = uSlaveAddress | 0x01; if( (er = emi2c_start(pC)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - emi2c_start() error %d\n", er)); goto emi2c_read_subaddress_error; } if ( (er = emi2c_sendbyte(pC, saW, &nack)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - Slave Address %x error %d\n", saW, er )); goto emi2c_read_subaddress_error; } if( nack != 0 ) { DPRINT(("emi2c_read_subaddress - Slave Address %x NACK %d\n", saW, nack)); goto emi2c_read_subaddress_error; } if ( (er = emi2c_sendbyte(pC, uSubAddress, &nack)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - Sub Address %x error %d\n", uSubAddress, er)); goto emi2c_read_subaddress_error; } if( nack != 0 ) { DPRINT(("emi2c_read_subaddress - Sub Address %x NACK %d\n", uSubAddress, nack)); goto emi2c_read_subaddress_error; } if( uFlags & EMI2C_RD_SUB_STOPONRESTART ) { /* Sr Condition */ if( (er = emi2c_stop(pC)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - emi2c_stop()SR error %d\n", er)); goto emi2c_read_subaddress_error; } } if( (er = emi2c_start(pC)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - emi2c_start()SR error %d\n", er)); goto emi2c_read_subaddress_error; } if ( (er = emi2c_sendbyte(pC, saR, &nack)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - Slave Address %x error %d\n", saR, er )); goto emi2c_read_subaddress_error; } if( nack != 0 ) { DPRINT(("emi2c_read_subaddress - Slave Address %x NACK %d\n", saR, nack)); goto emi2c_read_subaddress_error; } for( uCount = 0; uCount < (*pByteCount); uCount++ ) { RMuint32 f = 0; // nack last byte, that is part of i2c spec if( ((uCount+1) == (*pByteCount)) ) { if( uFlags & EMI2C_RD_SUB_NONACKLASTBYTE ) f = 0; else f = EMI2C_SEND_NACK; } if( (er = emi2c_readbyte(pC, pData+uCount, f)) != RM_OK ) { // Return the byteCount on Error *pByteCount = uCount; DPRINT(("emi2c_read_subaddress - Slave %x Sub %x Data %x Count %ld error %d\n", saR, uSubAddress, pData[uCount], uCount, er)); goto emi2c_read_subaddress_error; } } // Return the byteCount after we've read all bytes. *pByteCount = uCount; if( (er = emi2c_stop(pC)) != RM_OK ) { DPRINT(("emi2c_read_subaddress - emi2c_stop()SR error %d\n", er)); return -1; } return RM_OK;emi2c_read_subaddress_error: if( (er = emi2c_stop(pC)) != RM_OK ) DPRINT(("emi2c_read_subaddress - emi2c_stop() error %d\n", er)); return -1;}/*RMstatus emi2c_send_table( EMI2C_CONFIG* pC, RMuint8 uSlaveAddress, RMuint8 *pData, RMuint32 uByteCount){ RMuint32 uCount; assert(pC != 0); assert(pData != 0); assert(uByteCount%2 == 0); assert(uByteCount != 0); DPRINT(("emi2c_send_table - attempt to send %d bytes\n", uByteCount)); for( uCount = 0; uCount < uByteCount; uCount+=2 ) { if( emi2c_write_subaddress(pC, uSlaveAddress, pData[uCount], pData+uCount+1, 1) != RM_OK) { DPRINT(("emi2c_send_table - emi2c_write_subaddress error at Count %d\n", uCount)); return -1; } } return RM_OK;}*//*---------------RM Abstraction Layer-------------*/RMstatus I2C_Write( struct i2c* pI2C, RMuint8 addr, RMuint8* pData, RMuint32 n){ EMI2C_CONFIG C; assert(pI2C); memset(&C, 0, sizeof(EMI2C_CONFIG)); C.pGBus = pI2C->pGBus; C.RegBase = pI2C->RegBase; C.PIO_Clock = pI2C->PioClock; C.PIO_Data = pI2C->PioData; C.AdditionalDelay = pI2C->DelayUs & 0xFF; C.SclHiTimeout = (pI2C->DelayUs >> 8) & 0xFFFF; return emi2c_write_subaddress(&C, pI2C->WrAddr, addr, pData, n);}RMstatus I2C_Read( struct i2c* pI2C, RMuint8 addr, RMuint8* pData, RMuint32 n){ EMI2C_CONFIG C; RMuint32 byteCount = n; RMstatus nRetval; assert(pI2C); memset(&C, 0, sizeof(EMI2C_CONFIG)); C.pGBus = pI2C->pGBus; C.RegBase = pI2C->RegBase; C.PIO_Clock = pI2C->PioClock; C.PIO_Data = pI2C->PioData; C.AdditionalDelay = pI2C->DelayUs & 0xFF; C.SclHiTimeout = (pI2C->DelayUs >> 8) & 0xFFFF; nRetval = emi2c_read_subaddress(&C, pI2C->RdAddr, addr, pData, &byteCount, 0); if( byteCount != n ) { DPRINT(("I2C_Read emi2c_readsubaddress byteCount %ld != requested %ld\n", byteCount, n)); return RM_ERROR; } return nRetval;}RMstatus I2C_Write_NoSubAddr( struct i2c* pI2C, RMuint8* pData, RMuint32 n){ EMI2C_CONFIG C; RMstatus nRetval; RMuint8 nack; RMuint32 i; assert(pI2C); memset(&C, 0, sizeof(EMI2C_CONFIG)); C.pGBus = pI2C->pGBus; C.RegBase = pI2C->RegBase; C.PIO_Clock = pI2C->PioClock; C.PIO_Data = pI2C->PioData; C.AdditionalDelay = pI2C->DelayUs & 0xFF; C.SclHiTimeout = (pI2C->DelayUs >> 8) & 0xFFFF; assert(pData != NULL); assert( (pI2C->WrAddr & 0x01) == 0 ); if( (nRetval = emi2c_start(&C)) != RM_OK ) { DPRINT(("I2C_Write_NoSubAddr emi2c_start failed %d\n", nRetval)); return RM_ERROR; } /* LSB = 0 for writing */ if( (nRetval = emi2c_sendbyte( &C, pI2C->WrAddr, &nack)) != RM_OK ) { emi2c_stop( &C ); DPRINT(("I2C_Write_NoSubAddr emi2c_sendbyte failed %d sending WrAddr\nemi2c_stop() called!\n", nRetval)); return RM_ERROR; } /* check for NACK */ if( nack != 0 ) { emi2c_stop( &C ); DPRINT(("I2C_Write_NoSubAddr emi2c_sendbyte NACK=%x sending WrAddr\nemi2c_stop() called!\n", nack)); return RM_ERROR; } for ( i = 0; i < n; i++) { if( (nRetval = emi2c_sendbyte( &C, pData[i], &nack)) != RM_OK ) { emi2c_stop( &C ); DPRINT(("I2C_Write_NoSubAddr emi2c_sendbyte failed %d at byteCount = %ld\nemi2c_stop() called!\n", nRetval, i)); return RM_ERROR; } /* check for NACK */ if( nack != 0 ) { emi2c_stop( &C ); DPRINT(("I2C_Write_NoSubAddr emi2c_sendbyte NACK=%x sending data[%ld]\nemi2c_stop() called!\n", nack, i)); return RM_ERROR; } } if( (nRetval = emi2c_stop( &C )) != RM_OK ) { DPRINT(("I2C_Write_NoSubAddr emi2c_stop failed %d\n", nRetval)); return RM_ERROR; } return RM_OK;}RMstatus I2C_Read_NoSubAddr( struct i2c* pI2C, RMuint8* pData, RMuint32 n){ EMI2C_CONFIG C; RMstatus nRetval; RMuint8 nack; RMuint8 data; RMuint32 i; RMuint32 nackConfiguration = 0; /* don't send nack in the beginning */ assert(pI2C); memset(&C, 0, sizeof(EMI2C_CONFIG)); C.pGBus = pI2C->pGBus; C.RegBase = pI2C->RegBase; C.PIO_Clock = pI2C->PioClock; C.PIO_Data = pI2C->PioData; C.AdditionalDelay = pI2C->DelayUs & 0xFF; C.SclHiTimeout = (pI2C->DelayUs >> 8) & 0xFFFF; assert( (pI2C->RdAddr & 0x01) == 1 ); if( (nRetval = emi2c_start( &C )) != RM_OK ) { DPRINT(("I2C_Read_NoSubAddr emi2c_start failed %d\n", nRetval)); return RM_ERROR; } /* LSB = 1 for reading the data */ if( (nRetval = emi2c_sendbyte( &C, pI2C->RdAddr, &nack)) != RM_OK ) { emi2c_stop( &C ); DPRINT(("I2C_Read_NoSubAddr emi2c_sendbyte failed %d sending RdAddr\nemi2c_stop() called!\n", nRetval)); return RM_ERROR; } /* check for NACK */ if( nack != 0 ) { emi2c_stop( &C ); DPRINT(("I2C_Read_NoSubAddr emi2c_sendbyte NACK=%x sending RdAddr\nemi2c_stop()called!\n", nack)); return RM_ERROR; } for ( i = 0; i < n; i++) { /* iic spec says nack last byte, ONLY if you are MASTER */ if( i == (n -1) ) nackConfiguration = EMI2C_SEND_NACK; /* nack starts at 0 */ if( (nRetval = emi2c_readbyte( &C, &data, nackConfiguration)) != RM_OK ) { emi2c_stop( &C ); DPRINT(("I2C_Read_NoSubAddr emi2c_readbyte failed %d at byteCount = %ld\nemi2c_stop() called!\n", nRetval, i)); return RM_ERROR; } else pData[i] = data; } if( (nRetval = emi2c_stop( &C )) != RM_OK ) { DPRINT(("I2C_Read_NoSubAddr emi2c_stop failed %d\n", nRetval)); return RM_ERROR; } return RM_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -