Merge pull request #856 from Ralim/pinecil/reworki2c
Pinecil | Rework I2C into much cleaner state machine
This commit is contained in:
@@ -88,7 +88,8 @@ uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
|
|||||||
void unstick_I2C() {
|
void unstick_I2C() {
|
||||||
/* configure SDA/SCL for GPIO */
|
/* configure SDA/SCL for GPIO */
|
||||||
GPIO_BC(GPIOB) |= SDA_Pin | SCL_Pin;
|
GPIO_BC(GPIOB) |= SDA_Pin | SCL_Pin;
|
||||||
gpio_init(SDA_GPIO_Port, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SDA_Pin | SCL_Pin);
|
gpio_init(SDA_GPIO_Port, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, SDA_Pin | SCL_Pin);
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
asm("nop");
|
asm("nop");
|
||||||
asm("nop");
|
asm("nop");
|
||||||
asm("nop");
|
asm("nop");
|
||||||
@@ -100,7 +101,8 @@ void unstick_I2C() {
|
|||||||
asm("nop");
|
asm("nop");
|
||||||
asm("nop");
|
asm("nop");
|
||||||
asm("nop");
|
asm("nop");
|
||||||
GPIO_BOP(GPIOB) |= SDA_Pin;
|
GPIO_BOP(GPIOB) &= SCL_Pin;
|
||||||
|
}
|
||||||
/* connect PB6 to I2C0_SCL */
|
/* connect PB6 to I2C0_SCL */
|
||||||
/* connect PB7 to I2C0_SDA */
|
/* connect PB7 to I2C0_SDA */
|
||||||
gpio_init(SDA_GPIO_Port, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, SDA_Pin | SCL_Pin);
|
gpio_init(SDA_GPIO_Port, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, SDA_Pin | SCL_Pin);
|
||||||
|
|||||||
@@ -23,353 +23,281 @@ uint8_t FRToSI2C::I2C_RegisterRead(uint8_t add, uint8_t reg) {
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address, uint8_t *p_buffer, uint16_t number_of_byte) {
|
enum i2c_step {
|
||||||
if (!lock())
|
// Write+read steps
|
||||||
return false;
|
Write_start, // Sending start on bus
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_ERR);
|
Write_device_address, // start sent, send device address
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_BUF);
|
Write_device_memory_address, // device address sent, write the memory location
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_EV);
|
Write_device_data_start, // Write all of the remaining data using DMA
|
||||||
dma_parameter_struct dma_init_struct;
|
Write_device_data_finish, // Write all of the remaining data using DMA
|
||||||
|
|
||||||
uint8_t state = I2C_START;
|
Read_start, // second read
|
||||||
uint8_t in_rx_cycle = 0;
|
Read_device_address, // Send device address again for the read
|
||||||
uint16_t timeout = 0;
|
Read_device_data_start, // read device data via DMA
|
||||||
uint8_t tries = 0;
|
Read_device_data_finish, // read device data via DMA
|
||||||
uint8_t i2c_timeout_flag = 0;
|
Send_stop, // send the stop at the end of the transaction
|
||||||
while (!(i2c_timeout_flag)) {
|
Wait_stop, // Wait for stop to send and we are done
|
||||||
switch (state) {
|
Done, // Finished
|
||||||
case I2C_START:
|
Error_occured, // Error occured on the bus
|
||||||
tries++;
|
|
||||||
if (tries > 64) {
|
};
|
||||||
|
struct i2c_state {
|
||||||
|
i2c_step currentStep;
|
||||||
|
bool isMemoryWrite;
|
||||||
|
bool wakePart;
|
||||||
|
uint8_t deviceAddress;
|
||||||
|
uint8_t memoryAddress;
|
||||||
|
uint8_t * buffer;
|
||||||
|
uint16_t numberOfBytes;
|
||||||
|
dma_parameter_struct dma_init_struct;
|
||||||
|
};
|
||||||
|
volatile i2c_state currentState;
|
||||||
|
|
||||||
|
void perform_i2c_step() {
|
||||||
|
// Performs next step of the i2c state machine
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
// Arb error - we lost the bus / nacked
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||||
|
// Bus Error
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
||||||
|
// Bus Error
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_PECERR)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_PECERR);
|
||||||
|
// Bus Error
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
}
|
||||||
|
switch (currentState.currentStep) {
|
||||||
|
case Error_occured:
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
break;
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
case Write_start:
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
unlock();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (0 == in_rx_cycle) {
|
|
||||||
/* disable I2C0 */
|
|
||||||
i2c_disable(I2C0);
|
|
||||||
/* enable I2C0 */
|
|
||||||
i2c_enable(I2C0);
|
|
||||||
|
|
||||||
/* enable acknowledge */
|
/* enable acknowledge */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
/* i2c master sends start signal only when the bus is idle */
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
if (!i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
/* send the start signal */
|
/* send the start signal */
|
||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
timeout = 0;
|
currentState.currentStep = Write_device_address;
|
||||||
state = I2C_SEND_ADDRESS;
|
|
||||||
} else {
|
|
||||||
I2C_Unstick();
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_SEND_ADDRESS;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_SEND_ADDRESS:
|
|
||||||
|
case Write_device_address:
|
||||||
/* i2c master sends START signal successfully */
|
/* i2c master sends START signal successfully */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
if (RESET == in_rx_cycle) {
|
|
||||||
i2c_master_addressing(I2C0, DevAddress, I2C_TRANSMITTER);
|
|
||||||
state = I2C_CLEAR_ADDRESS_FLAG;
|
|
||||||
} else {
|
|
||||||
i2c_master_addressing(I2C0, DevAddress, I2C_RECEIVER);
|
|
||||||
state = I2C_CLEAR_ADDRESS_FLAG;
|
|
||||||
}
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
in_rx_cycle = 0;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
timeout = 0;
|
i2c_master_addressing(I2C0, currentState.deviceAddress, I2C_TRANSMITTER);
|
||||||
state = I2C_TRANSMIT_DATA;
|
currentState.currentStep = Write_device_memory_address;
|
||||||
} else {
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_TRANSMIT_DATA:
|
case Write_device_memory_address:
|
||||||
if (0 == in_rx_cycle) {
|
// Send the device memory location
|
||||||
/* wait until the transmit data buffer is empty */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { // addr sent
|
||||||
timeout++;
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_BERR)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||||
|
// Bus Error
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
// Arb error - we lost the bus / nacked
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
|
} else if (currentState.wakePart) {
|
||||||
|
// We are stopping here
|
||||||
|
currentState.currentStep = Send_stop;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_TBE)) {
|
||||||
// Write out the 8 byte address
|
// Write out the 8 byte address
|
||||||
i2c_data_transmit(I2C0, read_address);
|
i2c_data_transmit(I2C0, currentState.memoryAddress);
|
||||||
timeout = 0;
|
|
||||||
|
if (currentState.isMemoryWrite) {
|
||||||
|
currentState.currentStep = Write_device_data_start;
|
||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
currentState.currentStep = Read_start;
|
||||||
state = I2C_START;
|
|
||||||
in_rx_cycle = 0;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case Write_device_data_start:
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
||||||
timeout++;
|
/* 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;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
break;
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
case Write_device_data_finish: // Wait for complete then goto stop
|
||||||
in_rx_cycle = 1;
|
/* wait until BTC bit is set */
|
||||||
} else {
|
if (dma_flag_get(DMA0, DMA_CH5, DMA_FLAG_FTF)) {
|
||||||
timeout = 0;
|
/* wait until BTC bit is set */
|
||||||
state = I2C_START;
|
if (i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
||||||
in_rx_cycle = 0;
|
currentState.currentStep = Send_stop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Read_start:
|
||||||
|
/* wait until BTC bit is set */
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
currentState.currentStep = Read_device_address;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Read_device_address:
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
i2c_master_addressing(I2C0, currentState.deviceAddress, I2C_RECEIVER);
|
||||||
|
currentState.currentStep = Read_device_data_start;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Read_device_data_start:
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { // addr sent
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
// Arb error - we lost the bus / nacked
|
||||||
|
currentState.currentStep = Error_occured;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
/* one byte master reception procedure (polling) */
|
/* one byte master reception procedure (polling) */
|
||||||
if (number_of_byte < 2) {
|
if (currentState.numberOfBytes == 0) {
|
||||||
|
currentState.currentStep = Send_stop;
|
||||||
|
} else if (currentState.numberOfBytes == 1) {
|
||||||
/* disable acknowledge */
|
/* disable acknowledge */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
/* clear ADDSEND register by reading I2C_STAT0 then I2C_STAT1 register
|
/* clear ADDSEND register by reading I2C_STAT0 then I2C_STAT1 register
|
||||||
* (I2C_STAT0 has already been read) */
|
* (I2C_STAT0 has already been read) */
|
||||||
i2c_flag_get(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_get(I2C0, I2C_FLAG_ADDSEND); // sat0
|
||||||
|
i2c_flag_get(I2C0, I2C_FLAG_I2CBSY); // sat1
|
||||||
/* send a stop condition to I2C bus*/
|
/* send a stop condition to I2C bus*/
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* wait for the byte to be received */
|
/* wait for the byte to be received */
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE))
|
||||||
;
|
;
|
||||||
/* read the byte received from the EEPROM */
|
/* read the byte received from the EEPROM */
|
||||||
*p_buffer = i2c_data_receive(I2C0);
|
*currentState.buffer = i2c_data_receive(I2C0);
|
||||||
/* decrement the read bytes counter */
|
currentState.currentStep = Wait_stop;
|
||||||
number_of_byte--;
|
|
||||||
timeout = 0;
|
|
||||||
} else { /* more than one byte master reception procedure (DMA) */
|
} else { /* more than one byte master reception procedure (DMA) */
|
||||||
dma_deinit(DMA0, DMA_CH6);
|
|
||||||
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
|
|
||||||
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_CH6, &dma_init_struct);
|
|
||||||
|
|
||||||
i2c_dma_last_transfer_config(I2C0, I2C_DMALST_ON);
|
|
||||||
/* enable I2C0 DMA */
|
/* enable I2C0 DMA */
|
||||||
i2c_dma_enable(I2C0, I2C_DMA_ON);
|
i2c_dma_enable(I2C0, I2C_DMA_ON);
|
||||||
/* enable DMA0 channel5 */
|
/* enable DMA0 channel5 */
|
||||||
dma_channel_enable(DMA0, DMA_CH6);
|
dma_channel_enable(DMA0, DMA_CH6);
|
||||||
/* wait until BTC bit is set */
|
currentState.currentStep = Read_device_data_finish;
|
||||||
while (!dma_flag_get(DMA0, DMA_CH6, DMA_FLAG_FTF)) {}
|
|
||||||
/* send a stop condition to I2C bus*/
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
}
|
}
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STOP;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_STOP:
|
case Read_device_data_finish: // Wait for complete then goto stop
|
||||||
/* i2c master sends STOP signal successfully */
|
/* wait until BTC bit is set */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
if (dma_flag_get(DMA0, DMA_CH6, DMA_FLAG_FTF)) {
|
||||||
timeout++;
|
currentState.currentStep = Send_stop;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
timeout = 0;
|
break;
|
||||||
state = I2C_END;
|
case Send_stop:
|
||||||
i2c_timeout_flag = I2C_OK;
|
/* send a stop condition to I2C bus*/
|
||||||
} else {
|
i2c_stop_on_bus(I2C0);
|
||||||
timeout = 0;
|
currentState.currentStep = Wait_stop;
|
||||||
state = I2C_START;
|
break;
|
||||||
in_rx_cycle = 0;
|
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
|
||||||
in_rx_cycle = 0;
|
return;
|
||||||
i2c_timeout_flag = I2C_OK;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool perform_i2c_transaction(uint16_t DevAddress, uint16_t memory_address, uint8_t *p_buffer, uint16_t number_of_byte, bool isWrite, bool isWakeOnly) {
|
||||||
|
{
|
||||||
|
// TODO is this required
|
||||||
|
/* disable I2C0 */
|
||||||
|
i2c_disable(I2C0);
|
||||||
|
/* enable I2C0 */
|
||||||
|
i2c_enable(I2C0);
|
||||||
|
}
|
||||||
|
i2c_interrupt_disable(I2C0, I2C_INT_ERR);
|
||||||
|
i2c_interrupt_disable(I2C0, I2C_INT_BUF);
|
||||||
|
i2c_interrupt_disable(I2C0, I2C_INT_EV);
|
||||||
|
|
||||||
|
currentState.isMemoryWrite = isWrite;
|
||||||
|
currentState.wakePart = isWakeOnly;
|
||||||
|
currentState.deviceAddress = DevAddress;
|
||||||
|
currentState.memoryAddress = memory_address;
|
||||||
|
currentState.numberOfBytes = number_of_byte;
|
||||||
|
currentState.buffer = p_buffer;
|
||||||
|
if (!isWakeOnly) {
|
||||||
|
// Setup DMA
|
||||||
|
currentState.dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
||||||
|
currentState.dma_init_struct.memory_addr = (uint32_t)p_buffer;
|
||||||
|
currentState.dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
||||||
|
currentState.dma_init_struct.number = number_of_byte;
|
||||||
|
currentState.dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C0);
|
||||||
|
currentState.dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
||||||
|
currentState.dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
||||||
|
currentState.dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
||||||
|
if (currentState.isMemoryWrite) {
|
||||||
|
dma_deinit(DMA0, DMA_CH5);
|
||||||
|
currentState.dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
||||||
|
dma_init(DMA0, DMA_CH5, (dma_parameter_struct *)¤tState.dma_init_struct);
|
||||||
|
} else {
|
||||||
|
dma_deinit(DMA0, DMA_CH6);
|
||||||
|
currentState.dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
|
||||||
|
dma_init(DMA0, DMA_CH6, (dma_parameter_struct *)¤tState.dma_init_struct);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentState.isMemoryWrite) {
|
||||||
|
i2c_dma_last_transfer_config(I2C0, I2C_DMALST_ON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Clear flags
|
||||||
|
I2C_STAT0(I2C0) = 0;
|
||||||
|
I2C_STAT1(I2C0) = 0;
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
|
||||||
|
currentState.currentStep = Write_start; // Always start in write mode
|
||||||
|
TickType_t timeout = xTaskGetTickCount() + TICKS_SECOND;
|
||||||
|
while ((currentState.currentStep != Done) && (currentState.currentStep != Error_occured)) {
|
||||||
|
if (xTaskGetTickCount() > timeout) {
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
perform_i2c_step();
|
||||||
|
}
|
||||||
|
return currentState.currentStep == Done;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address, uint8_t *p_buffer, uint16_t number_of_byte) {
|
||||||
|
if (!lock())
|
||||||
|
return false;
|
||||||
|
bool res = perform_i2c_transaction(DevAddress, read_address, p_buffer, number_of_byte, false, false);
|
||||||
|
if (!res) {
|
||||||
|
I2C_Unstick();
|
||||||
|
}
|
||||||
unlock();
|
unlock();
|
||||||
return true;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *p_buffer, uint16_t number_of_byte) {
|
bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *p_buffer, uint16_t number_of_byte) {
|
||||||
if (!lock())
|
if (!lock())
|
||||||
return false;
|
return false;
|
||||||
|
bool res = perform_i2c_transaction(DevAddress, MemAddress, p_buffer, number_of_byte, true, false);
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_ERR);
|
if (!res) {
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_EV);
|
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_BUF);
|
|
||||||
dma_parameter_struct dma_init_struct;
|
|
||||||
|
|
||||||
uint8_t state = I2C_START;
|
|
||||||
uint16_t timeout = 0;
|
|
||||||
bool done = false;
|
|
||||||
bool timedout = false;
|
|
||||||
while (!(done || timedout)) {
|
|
||||||
switch (state) {
|
|
||||||
case I2C_START:
|
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_SEND_ADDRESS;
|
|
||||||
} else {
|
|
||||||
I2C_Unstick();
|
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();
|
unlock();
|
||||||
return false;
|
return res;
|
||||||
}
|
|
||||||
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 master sends STOP signal successfully */
|
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_END;
|
|
||||||
done = true;
|
|
||||||
} else {
|
|
||||||
timedout = true;
|
|
||||||
done = true;
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
state = I2C_START;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unlock();
|
|
||||||
return timedout == false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { return Mem_Write(DevAddress, pData[0], pData + 1, Size - 1); }
|
bool FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { return Mem_Write(DevAddress, pData[0], pData + 1, Size - 1); }
|
||||||
@@ -406,104 +334,15 @@ bool FRToSI2C::wakePart(uint16_t DevAddress) {
|
|||||||
// wakepart is a special case where only the device address is sent
|
// wakepart is a special case where only the device address is sent
|
||||||
if (!lock())
|
if (!lock())
|
||||||
return false;
|
return false;
|
||||||
|
bool res = perform_i2c_transaction(DevAddress, 0, NULL, 0, false, true);
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_ERR);
|
if (!res) {
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_EV);
|
|
||||||
i2c_interrupt_disable(I2C0, I2C_INT_BUF);
|
|
||||||
|
|
||||||
uint8_t state = I2C_START;
|
|
||||||
uint16_t timeout = 0;
|
|
||||||
bool done = false;
|
|
||||||
bool timedout = false;
|
|
||||||
while (!(done || timedout)) {
|
|
||||||
switch (state) {
|
|
||||||
case I2C_START:
|
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_start_on_bus(I2C0);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_SEND_ADDRESS;
|
|
||||||
} else {
|
|
||||||
I2C_Unstick();
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STOP;
|
|
||||||
} 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();
|
unlock();
|
||||||
return false;
|
return res;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case I2C_STOP:
|
void I2C_EV_IRQ() {}
|
||||||
/* send a stop condition to I2C bus */
|
void I2C_ER_IRQ() {
|
||||||
i2c_stop_on_bus(I2C0);
|
// Error callbacks
|
||||||
/* i2c master sends STOP signal successfully */
|
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_END;
|
|
||||||
done = true;
|
|
||||||
} else {
|
|
||||||
timedout = true;
|
|
||||||
done = true;
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
state = I2C_START;
|
|
||||||
timeout = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unlock();
|
|
||||||
return timedout == false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -411,10 +411,9 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_ready() {
|
|||||||
|
|
||||||
return PESinkSendSoftReset;
|
return PESinkSendSoftReset;
|
||||||
}
|
}
|
||||||
/* If we got an unknown message, send a soft reset ??? */
|
|
||||||
} else {
|
} else {
|
||||||
|
/* if we get an unknown message code, silently ignore it*/
|
||||||
return PESinkSendSoftReset;
|
return PESinkReady;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,7 +165,8 @@ const menuitem UIMenu[] = {
|
|||||||
* Cooldown blink
|
* Cooldown blink
|
||||||
* Reverse Temp change buttons + -
|
* Reverse Temp change buttons + -
|
||||||
*/
|
*/
|
||||||
{(const char *)SettingsDescriptions[5], settings_setTempF, settings_displayTempF}, /* Temperature units, this has to be the first element in the array to work with the logic in settings_enterUIMenu() */
|
{(const char *)SettingsDescriptions[5], settings_setTempF,
|
||||||
|
settings_displayTempF}, /* Temperature units, this has to be the first element in the array to work with the logic in settings_enterUIMenu() */
|
||||||
{(const char *)SettingsDescriptions[7], settings_setDisplayRotation, settings_displayDisplayRotation}, /*Display Rotation*/
|
{(const char *)SettingsDescriptions[7], settings_setDisplayRotation, settings_displayDisplayRotation}, /*Display Rotation*/
|
||||||
{(const char *)SettingsDescriptions[10], settings_setCoolingBlinkEnabled, settings_displayCoolingBlinkEnabled}, /*Cooling blink warning*/
|
{(const char *)SettingsDescriptions[10], settings_setCoolingBlinkEnabled, settings_displayCoolingBlinkEnabled}, /*Cooling blink warning*/
|
||||||
{(const char *)SettingsDescriptions[15], settings_setScrollSpeed, settings_displayScrollSpeed}, /*Scroll Speed for descriptions*/
|
{(const char *)SettingsDescriptions[15], settings_setScrollSpeed, settings_displayScrollSpeed}, /*Scroll Speed for descriptions*/
|
||||||
|
|||||||
@@ -70,8 +70,7 @@ void gui_drawTipTemp(bool symbol) {
|
|||||||
uint32_t Temp = 0;
|
uint32_t Temp = 0;
|
||||||
if (systemSettings.temperatureInF) {
|
if (systemSettings.temperatureInF) {
|
||||||
Temp = TipThermoModel::getTipInF();
|
Temp = TipThermoModel::getTipInF();
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
Temp = TipThermoModel::getTipInC();
|
Temp = TipThermoModel::getTipInC();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,8 +273,7 @@ static void gui_solderingTempAdjust() {
|
|||||||
OLED::printNumber(systemSettings.SolderingTemp, 3);
|
OLED::printNumber(systemSettings.SolderingTemp, 3);
|
||||||
if (systemSettings.temperatureInF)
|
if (systemSettings.temperatureInF)
|
||||||
OLED::drawSymbol(0);
|
OLED::drawSymbol(0);
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
OLED::drawSymbol(1);
|
OLED::drawSymbol(1);
|
||||||
}
|
}
|
||||||
OLED::print(SymbolSpace);
|
OLED::print(SymbolSpace);
|
||||||
@@ -410,7 +408,7 @@ static bool shouldBeSleeping(bool inAutoStart) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (lastMovementTime > 0 || lastButtonTime > 0) {
|
if (lastMovementTime > 0 || lastButtonTime > 0) {
|
||||||
if ((xTaskGetTickCount() - lastMovementTime) > getSleepTimeout() && (xTaskGetTickCount() - lastButtonTime) > getSleepTimeout()) {
|
if (((xTaskGetTickCount() - lastMovementTime) > getSleepTimeout()) && ((xTaskGetTickCount() - lastButtonTime) > getSleepTimeout())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -419,7 +417,7 @@ static bool shouldBeSleeping(bool inAutoStart) {
|
|||||||
#ifdef HALL_SENSOR
|
#ifdef HALL_SENSOR
|
||||||
// If the hall effect sensor is enabled in the build, check if its over
|
// If the hall effect sensor is enabled in the build, check if its over
|
||||||
// threshold, and if so then we force sleep
|
// threshold, and if so then we force sleep
|
||||||
if (lookupHallEffectThreshold()) {
|
if (getHallSensorFitted() && lookupHallEffectThreshold()) {
|
||||||
int16_t hallEffectStrength = getRawHallEffect();
|
int16_t hallEffectStrength = getRawHallEffect();
|
||||||
if (hallEffectStrength < 0)
|
if (hallEffectStrength < 0)
|
||||||
hallEffectStrength = -hallEffectStrength;
|
hallEffectStrength = -hallEffectStrength;
|
||||||
|
|||||||
Reference in New Issue
Block a user