Rework writes to use the new states
This commit is contained in:
@@ -31,7 +31,7 @@ enum i2c_step {
|
|||||||
Write_device_address, //start sent, send device address
|
Write_device_address, //start sent, send device address
|
||||||
Write_device_memory_address, //device address sent, write the memory location
|
Write_device_memory_address, //device address sent, write the memory location
|
||||||
Write_device_data_start, // Write all of the remaining data using DMA
|
Write_device_data_start, // Write all of the remaining data using DMA
|
||||||
Write_device_data_wait, // Write all of the remaining data using DMA
|
Write_device_data_finish, // Write all of the remaining data using DMA
|
||||||
|
|
||||||
Read_start, //second read
|
Read_start, //second read
|
||||||
Read_device_address, // Send device address again for the read
|
Read_device_address, // Send device address again for the read
|
||||||
@@ -208,143 +208,115 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *p_bu
|
|||||||
i2c_interrupt_disable(I2C0, I2C_INT_ERR);
|
i2c_interrupt_disable(I2C0, I2C_INT_ERR);
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_EV);
|
i2c_interrupt_disable(I2C0, I2C_INT_EV);
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_BUF);
|
i2c_interrupt_disable(I2C0, I2C_INT_BUF);
|
||||||
dma_parameter_struct dma_init_struct;
|
dma_deinit(DMA0, DMA_CH5);
|
||||||
|
currentState.dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
||||||
uint8_t state = I2C_START;
|
currentState.dma_init_struct.memory_addr = (uint32_t) p_buffer;
|
||||||
uint16_t timeout = 0;
|
currentState.dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
||||||
bool done = false;
|
currentState.dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
||||||
bool timedout = false;
|
currentState.dma_init_struct.number = number_of_byte;
|
||||||
while (!(done || timedout)) {
|
currentState.dma_init_struct.periph_addr = (uint32_t) &I2C_DATA(I2C0);
|
||||||
switch (state) {
|
currentState.dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
||||||
case I2C_START:
|
currentState.dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
currentState.dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT )) {
|
dma_init(DMA0, DMA_CH5, (dma_parameter_struct*) ¤tState.dma_init_struct);
|
||||||
timeout++;
|
currentState.currentStep = Write_start;
|
||||||
}
|
TickType_t timeout = xTaskGetTickCount() + TICKS_SECOND;
|
||||||
if (timeout < I2C_TIME_OUT) {
|
while ((currentState.currentStep != Done) && (currentState.currentStep != Error_occured)) {
|
||||||
i2c_start_on_bus(I2C0);
|
if (xTaskGetTickCount() > timeout) {
|
||||||
timeout = 0;
|
|
||||||
state = I2C_SEND_ADDRESS;
|
|
||||||
} else {
|
|
||||||
I2C_Unstick();
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_SEND_ADDRESS:
|
|
||||||
/* i2c master sends START signal successfully */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT )) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_master_addressing(I2C0, DevAddress, I2C_TRANSMITTER);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_CLEAR_ADDRESS_FLAG;
|
|
||||||
} else {
|
|
||||||
timedout = true;
|
|
||||||
done = true;
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
|
||||||
/* address flag set means i2c slave sends ACK */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT )) {
|
|
||||||
timeout++;
|
|
||||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
/* i2c master sends STOP signal successfully */
|
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
// Address NACK'd
|
|
||||||
unlock();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
timeout = 0;
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
state = I2C_TRANSMIT_DATA;
|
|
||||||
} else {
|
|
||||||
// Dont retry as this means a NAK
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
/* i2c master sends STOP signal successfully */
|
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
unlock();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_TRANSMIT_DATA:
|
|
||||||
/* wait until the transmit data buffer is empty */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT )) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
/* send the EEPROM's internal address to write to : only one byte
|
|
||||||
* address */
|
|
||||||
i2c_data_transmit(I2C0, MemAddress);
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timedout = true;
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
}
|
|
||||||
/* wait until BTC bit is set */
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC))
|
|
||||||
;
|
|
||||||
dma_deinit(DMA0, DMA_CH5);
|
|
||||||
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
|
||||||
dma_init_struct.memory_addr = (uint32_t) p_buffer;
|
|
||||||
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
|
||||||
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
|
||||||
dma_init_struct.number = number_of_byte;
|
|
||||||
dma_init_struct.periph_addr = (uint32_t) &I2C_DATA(I2C0);
|
|
||||||
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
|
||||||
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
|
||||||
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
|
||||||
dma_init(DMA0, DMA_CH5, &dma_init_struct);
|
|
||||||
/* enable I2C0 DMA */
|
|
||||||
i2c_dma_enable(I2C0, I2C_DMA_ON);
|
|
||||||
/* enable DMA0 channel5 */
|
|
||||||
dma_channel_enable(DMA0, DMA_CH5);
|
|
||||||
/* wait until BTC bit is set */
|
|
||||||
while (!dma_flag_get(DMA0, DMA_CH5, DMA_FLAG_FTF)) {
|
|
||||||
}
|
|
||||||
/* wait until BTC bit is set */
|
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
|
||||||
}
|
|
||||||
state = I2C_STOP;
|
|
||||||
break;
|
|
||||||
case I2C_STOP:
|
|
||||||
/* send a stop condition to I2C bus */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
I2C_Unstick();
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
unlock();
|
||||||
timeout++;
|
return false;
|
||||||
|
}
|
||||||
|
switch (currentState.currentStep) {
|
||||||
|
|
||||||
|
case Error_occured:
|
||||||
|
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
I2C_Unstick();
|
||||||
|
unlock();
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case Write_start:
|
||||||
|
|
||||||
|
/* disable I2C0 */
|
||||||
|
i2c_disable(I2C0);
|
||||||
|
/* enable I2C0 */
|
||||||
|
i2c_enable(I2C0);
|
||||||
|
|
||||||
|
/* enable acknowledge */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
/* i2c master sends start signal only when the bus is idle */
|
||||||
|
if (!i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)) {
|
||||||
|
/* send the start signal */
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
currentState.currentStep = Write_device_address;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
break;
|
||||||
timeout = 0;
|
case Write_device_address:
|
||||||
state = I2C_END;
|
/* i2c master sends START signal successfully */
|
||||||
done = true;
|
if (i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
||||||
} else {
|
i2c_master_addressing(I2C0, DevAddress, I2C_TRANSMITTER);
|
||||||
timedout = true;
|
currentState.currentStep = Write_device_memory_address;
|
||||||
done = true;
|
}
|
||||||
timeout = 0;
|
break;
|
||||||
state = I2C_START;
|
case Write_device_memory_address:
|
||||||
|
//Send the device memory location
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { //addr sent
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
//Arb error - we lost the bus / nacked
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
}
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_TBE)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
// Write out the 8 byte address
|
||||||
|
i2c_data_transmit(I2C0, MemAddress);
|
||||||
|
currentState.currentStep = Write_device_data_start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Write_device_data_start:
|
||||||
|
|
||||||
|
/* wait until BTC bit is set */
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
||||||
|
|
||||||
|
/* enable I2C0 DMA */
|
||||||
|
i2c_dma_enable(I2C0, I2C_DMA_ON);
|
||||||
|
/* enable DMA0 channel5 */
|
||||||
|
dma_channel_enable(DMA0, DMA_CH5);
|
||||||
|
currentState.currentStep = Write_device_data_finish;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Write_device_data_finish: //Wait for complete then goto stop
|
||||||
|
/* wait until BTC bit is set */
|
||||||
|
if (dma_flag_get(DMA0, DMA_CH5, DMA_FLAG_FTF)) {
|
||||||
|
/* wait until BTC bit is set */
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
||||||
|
currentState.currentStep = Send_stop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case Send_stop:
|
||||||
|
/* send a stop condition to I2C bus*/
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
currentState.currentStep = Wait_stop;
|
||||||
|
break;
|
||||||
|
case Wait_stop:
|
||||||
|
/* i2c master sends STOP signal successfully */
|
||||||
|
if ((I2C_CTL0(I2C0) & 0x0200) != 0x0200) {
|
||||||
|
currentState.currentStep = Done;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
state = I2C_START;
|
//If we get here something is amiss
|
||||||
timeout = 0;
|
unlock();
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unlock();
|
unlock();
|
||||||
return timedout == false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
|
bool FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
|
||||||
|
|||||||
Reference in New Issue
Block a user