/* I2C state machine */ static void I2C1_TransferSM(void) { IFS0CLR = _IFS0_I2C1MIF_MASK; switch (i2c1Obj.state) { case I2C_STATE_START_CONDITION: //开始连接 /* Generate Start Condition */ I2C1CONSET = _I2C1CON_SEN_MASK; //启动条件使能位 IEC0SET = _IEC0_I2C1MIE_MASK; //主器件中断允许控制位 IEC0SET = _IEC0_I2C1BIE_MASK; //总线冲突中断允许控制位 i2c1Obj.state = I2C_STATE_ADDR_BYTE_1_SEND; break; case I2C_STATE_ADDR_BYTE_1_SEND: //发送第一字节地址 /* Is transmit buffer full? */ if (!(I2C1STAT & _I2C1STAT_TBF_MASK)) //发送缓冲区满状态位 { if (i2c1Obj.address > 0x007F) //有两个字节的地址,10位 { /* Transmit the MSB 2 bits of the 10-bit slave address, with R/W = 0 */ I2C1TRN = ( 0xF0 | (((uint8_t*)&i2c1Obj.address)[1] << 1)); //发送数据寄存器 i2c1Obj.state = I2C_STATE_ADDR_BYTE_2_SEND; } else //只有一个字节的地址 7位 { /* 8-bit addressing mode */ I2C1TRN = ((i2c1Obj.address << 1) | i2c1Obj.transferType); if (i2c1Obj.transferType == I2C_TRANSFER_TYPE_WRITE) //读写 { i2c1Obj.state = I2C_STATE_WRITE; } else { i2c1Obj.state = I2C_STATE_READ; } } } break; case I2C_STATE_ADDR_BYTE_2_SEND: //第二个字节地址 /* Transmit the 2nd byte of the 10-bit slave address */ if (!(I2C1STAT & _I2C1STAT_ACKSTAT_MASK)) //应答状态位 { if (!(I2C1STAT & _I2C1STAT_TBF_MASK)) { /* Transmit the remaining 8-bits of the 10-bit address */ I2C1TRN = i2c1Obj.address; //低的8位 if (i2c1Obj.transferType == I2C_TRANSFER_TYPE_WRITE) { i2c1Obj.state = I2C_STATE_WRITE; } else { i2c1Obj.state = I2C_STATE_READ_10BIT_MODE; } } } else //发送第一个字节地址后,没有收到应答 { /* NAK received. Generate Stop Condition. */ i2c1Obj.error = I2C_ERROR_NACK; //无应答错误 I2C1CONSET = _I2C1CON_PEN_MASK; //停止条件使能位 i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } break; case I2C_STATE_READ_10BIT_MODE: //发送10位地址后,读取10bit模式 if (!(I2C1STAT & _I2C1STAT_ACKSTAT_MASK)) //应答位状态 { /* Generate repeated start condition */ I2C1CONSET = _I2C1CON_RSEN_MASK; //重复启动条件使能位 i2c1Obj.state = I2C_STATE_ADDR_BYTE_1_SEND_10BIT_ONLY; } else { /* NAK received. Generate Stop Condition. */ i2c1Obj.error = I2C_ERROR_NACK; I2C1CONSET = _I2C1CON_PEN_MASK; i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } break; case I2C_STATE_ADDR_BYTE_1_SEND_10BIT_ONLY: /* Is transmit buffer full? */ if (!(I2C1STAT & _I2C1STAT_TBF_MASK)) //发送缓冲区满状态位 { /* Transmit the first byte of the 10-bit slave address, with R/W = 1 */ I2C1TRN = ( 0xF1 | ((((uint8_t*)&i2c1Obj.address)[1] << 1))); i2c1Obj.state = I2C_STATE_READ; } else { /* NAK received. Generate Stop Condition. */ i2c1Obj.error = I2C_ERROR_NACK; I2C1CONSET = _I2C1CON_PEN_MASK; i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } break; case I2C_STATE_WRITE: if (!(I2C1STAT & _I2C1STAT_ACKSTAT_MASK)) //应答位状态 { /* ACK received */ if (i2c1Obj.writeCount < i2c1Obj.writeSize) { if (!(I2C1STAT & _I2C1STAT_TBF_MASK)) //发送缓冲区满状态位 { /* Transmit the data from writeBuffer[] */ I2C1TRN = i2c1Obj.writeBuffer[i2c1Obj.writeCount++]; } } else { if (i2c1Obj.readCount < i2c1Obj.readSize) { /* Generate repeated start condition */ I2C1CONSET = _I2C1CON_RSEN_MASK; //重复启动条件使能位 i2c1Obj.transferType = I2C_TRANSFER_TYPE_READ; if (i2c1Obj.address > 0x007F) //10bit地址 { /* Send the I2C slave address with R/W = 1 */ i2c1Obj.state = I2C_STATE_ADDR_BYTE_1_SEND_10BIT_ONLY; } else { /* Send the I2C slave address with R/W = 1 */ i2c1Obj.state = I2C_STATE_ADDR_BYTE_1_SEND; } } else //数据接收完成 { /* Transfer Complete. Generate Stop Condition */ I2C1CONSET = _I2C1CON_PEN_MASK; i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } } } else { /* NAK received. Generate Stop Condition. */ i2c1Obj.error = I2C_ERROR_NACK; I2C1CONSET = _I2C1CON_PEN_MASK; i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } break; case I2C_STATE_READ: if (!(I2C1STAT & _I2C1STAT_ACKSTAT_MASK)) //应答位状态 { /* Slave ACK'd the device address. Enable receiver. */ I2C1CONSET = _I2C1CON_RCEN_MASK; //接收使能位 i2c1Obj.state = I2C_STATE_READ_BYTE; } else { /* NAK received. Generate Stop Condition. */ i2c1Obj.error = I2C_ERROR_NACK; I2C1CONSET = _I2C1CON_PEN_MASK; i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } break; case I2C_STATE_READ_BYTE: /* Data received from the slave */ if (I2C1STAT & _I2C1STAT_RBF_MASK) //接收缓冲区满状态位 { i2c1Obj.readBuffer[i2c1Obj.readCount++] = I2C1RCV; //接收数据寄存器 if (i2c1Obj.readCount == i2c1Obj.readSize) { /* Send NAK */ I2C1CONSET = _I2C1CON_ACKDT_MASK; //应答数据位 发送NACK I2C1CONSET = _I2C1CON_ACKEN_MASK; //应答序列使能位 } else { /* Send ACK */ I2C1CONCLR = _I2C1CON_ACKDT_MASK; //应答数据位 发送ACK I2C1CONSET = _I2C1CON_ACKEN_MASK; //应答序列使能位 } i2c1Obj.state = I2C_STATE_WAIT_ACK_COMPLETE; } break; case I2C_STATE_WAIT_ACK_COMPLETE: /* ACK or NAK sent to the I2C slave */ if (i2c1Obj.readCount < i2c1Obj.readSize) { /* Enable receiver */ I2C1CONSET = _I2C1CON_RCEN_MASK; //接收使能位 i2c1Obj.state = I2C_STATE_READ_BYTE; } else { /* Generate Stop Condition */ I2C1CONSET = _I2C1CON_PEN_MASK; //停止条件使能位 i2c1Obj.state = I2C_STATE_WAIT_STOP_CONDITION_COMPLETE; } break; case I2C_STATE_WAIT_STOP_CONDITION_COMPLETE: i2c1Obj.state = I2C_STATE_IDLE; IEC0CLR = _IEC0_I2C1MIE_MASK; //主器件中断允许控制位 IEC0CLR = _IEC0_I2C1BIE_MASK; //总线冲突中断允许控制位 if (i2c1Obj.callback != NULL) { i2c1Obj.callback(i2c1Obj.context); } break; default: break; } }
|