mirror of
https://github.com/Ralim/IronOS.git
synced 2025-02-26 07:53:55 +00:00
Cleanup timings in I2C Driver
This commit is contained in:
@@ -46,7 +46,7 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
if (tries > 64) {
|
if (tries > 64) {
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
unlock();
|
unlock();
|
||||||
@@ -61,8 +61,8 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
/* 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) &&
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -83,8 +83,8 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
break;
|
break;
|
||||||
case I2C_SEND_ADDRESS:
|
case I2C_SEND_ADDRESS:
|
||||||
/* i2c master sends START signal successfully */
|
/* i2c master sends START signal successfully */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -104,14 +104,15 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
break;
|
break;
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
case I2C_CLEAR_ADDRESS_FLAG:
|
||||||
/* address flag set means i2c slave sends ACK */
|
/* address flag set means i2c slave sends ACK */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200)
|
||||||
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
// Address NACK'd
|
// Address NACK'd
|
||||||
@@ -126,7 +127,7 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
} else {
|
} else {
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
// Address NACK'd
|
// Address NACK'd
|
||||||
@@ -137,8 +138,8 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
case I2C_TRANSMIT_DATA:
|
case I2C_TRANSMIT_DATA:
|
||||||
if (0 == in_rx_cycle) {
|
if (0 == in_rx_cycle) {
|
||||||
/* wait until the transmit data buffer is empty */
|
/* wait until the transmit data buffer is empty */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -151,8 +152,8 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
in_rx_cycle = 0;
|
in_rx_cycle = 0;
|
||||||
}
|
}
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -185,11 +186,11 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
} else { /* more than one byte master reception procedure (DMA) */
|
} else { /* more than one byte master reception procedure (DMA) */
|
||||||
dma_deinit(DMA0, DMA_CH6);
|
dma_deinit(DMA0, DMA_CH6);
|
||||||
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
|
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
|
||||||
dma_init_struct.memory_addr = (uint32_t)p_buffer;
|
dma_init_struct.memory_addr = (uint32_t) p_buffer;
|
||||||
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
||||||
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
||||||
dma_init_struct.number = number_of_byte;
|
dma_init_struct.number = number_of_byte;
|
||||||
dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C0);
|
dma_init_struct.periph_addr = (uint32_t) &I2C_DATA(I2C0);
|
||||||
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
||||||
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
||||||
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
||||||
@@ -202,7 +203,7 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
dma_channel_enable(DMA0, DMA_CH6);
|
dma_channel_enable(DMA0, DMA_CH6);
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while (!dma_flag_get(DMA0, DMA_CH6, DMA_FLAG_FTF)) {
|
while (!dma_flag_get(DMA0, DMA_CH6, DMA_FLAG_FTF)) {
|
||||||
osDelay(1);
|
|
||||||
}
|
}
|
||||||
/* send a stop condition to I2C bus*/
|
/* send a stop condition to I2C bus*/
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
@@ -213,7 +214,7 @@ bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t read_address,
|
|||||||
break;
|
break;
|
||||||
case I2C_STOP:
|
case I2C_STOP:
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -256,7 +257,8 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
case I2C_START:
|
case I2C_START:
|
||||||
/* 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)) {
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
|
||||||
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -271,8 +273,8 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
break;
|
break;
|
||||||
case I2C_SEND_ADDRESS:
|
case I2C_SEND_ADDRESS:
|
||||||
/* i2c master sends START signal successfully */
|
/* i2c master sends START signal successfully */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -288,14 +290,15 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
break;
|
break;
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
case I2C_CLEAR_ADDRESS_FLAG:
|
||||||
/* address flag set means i2c slave sends ACK */
|
/* address flag set means i2c slave sends ACK */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200)
|
||||||
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
// Address NACK'd
|
// Address NACK'd
|
||||||
@@ -311,7 +314,7 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
// Dont retry as this means a NAK
|
// Dont retry as this means a NAK
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
unlock();
|
unlock();
|
||||||
@@ -320,7 +323,8 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
break;
|
break;
|
||||||
case I2C_TRANSMIT_DATA:
|
case I2C_TRANSMIT_DATA:
|
||||||
/* wait until the transmit data buffer is empty */
|
/* wait until the transmit data buffer is empty */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE))
|
||||||
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -338,11 +342,11 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
;
|
;
|
||||||
dma_deinit(DMA0, DMA_CH5);
|
dma_deinit(DMA0, DMA_CH5);
|
||||||
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
||||||
dma_init_struct.memory_addr = (uint32_t)p_buffer;
|
dma_init_struct.memory_addr = (uint32_t) p_buffer;
|
||||||
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
||||||
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
|
||||||
dma_init_struct.number = number_of_byte;
|
dma_init_struct.number = number_of_byte;
|
||||||
dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C0);
|
dma_init_struct.periph_addr = (uint32_t) &I2C_DATA(I2C0);
|
||||||
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
||||||
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
|
||||||
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
||||||
@@ -353,18 +357,18 @@ bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
|
|||||||
dma_channel_enable(DMA0, DMA_CH5);
|
dma_channel_enable(DMA0, DMA_CH5);
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while (!dma_flag_get(DMA0, DMA_CH5, DMA_FLAG_FTF)) {
|
while (!dma_flag_get(DMA0, DMA_CH5, DMA_FLAG_FTF)) {
|
||||||
osDelay(2);
|
|
||||||
}
|
}
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC))
|
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC)) {
|
||||||
;
|
}
|
||||||
state = I2C_STOP;
|
state = I2C_STOP;
|
||||||
break;
|
break;
|
||||||
case I2C_STOP:
|
case I2C_STOP:
|
||||||
/* send a stop condition to I2C bus */
|
/* send a stop condition to I2C bus */
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -397,20 +401,23 @@ bool FRToSI2C::probe(uint16_t DevAddress) {
|
|||||||
return Mem_Read(DevAddress, 0x00, temp, sizeof(temp));
|
return Mem_Read(DevAddress, 0x00, temp, sizeof(temp));
|
||||||
}
|
}
|
||||||
|
|
||||||
void FRToSI2C::I2C_Unstick() { unstick_I2C(); }
|
void FRToSI2C::I2C_Unstick() {
|
||||||
|
unstick_I2C();
|
||||||
|
}
|
||||||
|
|
||||||
bool FRToSI2C::lock() {
|
bool FRToSI2C::lock() {
|
||||||
if (I2CSemaphore == nullptr) {
|
if (I2CSemaphore == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return xSemaphoreTake(I2CSemaphore, 1000) == pdTRUE;
|
return xSemaphoreTake(I2CSemaphore, TICKS_SECOND) == pdTRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FRToSI2C::unlock() { xSemaphoreGive(I2CSemaphore); }
|
void FRToSI2C::unlock() {
|
||||||
|
xSemaphoreGive(I2CSemaphore);
|
||||||
|
}
|
||||||
|
|
||||||
bool FRToSI2C::writeRegistersBulk(const uint8_t address,
|
bool FRToSI2C::writeRegistersBulk(const uint8_t address,
|
||||||
const I2C_REG *registers,
|
const I2C_REG *registers, const uint8_t registersLength) {
|
||||||
const uint8_t registersLength) {
|
|
||||||
for (int index = 0; index < registersLength; index++) {
|
for (int index = 0; index < registersLength; index++) {
|
||||||
if (!I2C_RegisterWrite(address, registers[index].reg,
|
if (!I2C_RegisterWrite(address, registers[index].reg,
|
||||||
registers[index].val)) {
|
registers[index].val)) {
|
||||||
@@ -440,7 +447,8 @@ bool FRToSI2C::wakePart(uint16_t DevAddress) {
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
case I2C_START:
|
case I2C_START:
|
||||||
/* 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)) {
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
|
||||||
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -455,8 +463,8 @@ bool FRToSI2C::wakePart(uint16_t DevAddress) {
|
|||||||
break;
|
break;
|
||||||
case I2C_SEND_ADDRESS:
|
case I2C_SEND_ADDRESS:
|
||||||
/* i2c master sends START signal successfully */
|
/* i2c master sends START signal successfully */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
@@ -472,14 +480,15 @@ bool FRToSI2C::wakePart(uint16_t DevAddress) {
|
|||||||
break;
|
break;
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
case I2C_CLEAR_ADDRESS_FLAG:
|
||||||
/* address flag set means i2c slave sends ACK */
|
/* address flag set means i2c slave sends ACK */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) &&
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
|
||||||
(timeout < I2C_TIME_OUT)) {
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200)
|
||||||
|
&& (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
// Address NACK'd
|
// Address NACK'd
|
||||||
@@ -495,7 +504,7 @@ bool FRToSI2C::wakePart(uint16_t DevAddress) {
|
|||||||
// Dont retry as this means a NAK
|
// Dont retry as this means a NAK
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
unlock();
|
unlock();
|
||||||
@@ -507,7 +516,7 @@ bool FRToSI2C::wakePart(uint16_t DevAddress) {
|
|||||||
/* send a stop condition to I2C bus */
|
/* send a stop condition to I2C bus */
|
||||||
i2c_stop_on_bus(I2C0);
|
i2c_stop_on_bus(I2C0);
|
||||||
/* i2c master sends STOP signal successfully */
|
/* i2c master sends STOP signal successfully */
|
||||||
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT)) {
|
while ((I2C_CTL0(I2C0) & 0x0200) && (timeout < I2C_TIME_OUT )) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout < I2C_TIME_OUT) {
|
||||||
|
|||||||
@@ -61,16 +61,20 @@ void setup_gpio() {
|
|||||||
// OLED reset as output
|
// OLED reset as output
|
||||||
gpio_init(OLED_RESET_GPIO_Port, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ,
|
gpio_init(OLED_RESET_GPIO_Port, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ,
|
||||||
OLED_RESET_Pin);
|
OLED_RESET_Pin);
|
||||||
|
gpio_bit_set(SDA_GPIO_Port, SDA_Pin);
|
||||||
|
gpio_bit_set(SDA_GPIO_Port, SCL_Pin);
|
||||||
// I2C as AF Open Drain
|
// I2C as AF Open Drain
|
||||||
gpio_init(SDA_GPIO_Port, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ,
|
gpio_init(SDA_GPIO_Port, GPIO_MODE_AF_OD, GPIO_OSPEED_2MHZ,
|
||||||
SDA_Pin | SCL_Pin);
|
SDA_Pin | SCL_Pin);
|
||||||
// PWM output as AF Push Pull
|
// PWM output as AF Push Pull
|
||||||
gpio_init(PWM_Out_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, PWM_Out_Pin);
|
gpio_init(PWM_Out_GPIO_Port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ,
|
||||||
|
PWM_Out_Pin);
|
||||||
// Analog Inputs ... as analog inputs
|
// Analog Inputs ... as analog inputs
|
||||||
gpio_init(TMP36_INPUT_GPIO_Port, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ,
|
gpio_init(TMP36_INPUT_GPIO_Port, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ,
|
||||||
TMP36_INPUT_Pin);
|
TMP36_INPUT_Pin);
|
||||||
gpio_init(TIP_TEMP_GPIO_Port, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, TIP_TEMP_Pin);
|
gpio_init(TIP_TEMP_GPIO_Port, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ,
|
||||||
gpio_init(VIN_GPIO_Port, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, VIN_Pin);
|
TIP_TEMP_Pin);
|
||||||
|
gpio_init(VIN_GPIO_Port, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ, VIN_Pin);
|
||||||
|
|
||||||
// Remap PB4 away from JTAG NJRST
|
// Remap PB4 away from JTAG NJRST
|
||||||
gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);
|
gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);
|
||||||
@@ -111,7 +115,7 @@ void setup_i2c() {
|
|||||||
/* enable I2C0 clock */
|
/* enable I2C0 clock */
|
||||||
rcu_periph_clock_enable(RCU_I2C0);
|
rcu_periph_clock_enable(RCU_I2C0);
|
||||||
// Setup I20 at 400kHz
|
// Setup I20 at 400kHz
|
||||||
i2c_clock_config(I2C0, 400 * 1000, I2C_DTCY_16_9);
|
i2c_clock_config(I2C0, 400 * 1000, I2C_DTCY_2);
|
||||||
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x00);
|
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x00);
|
||||||
i2c_enable(I2C0);
|
i2c_enable(I2C0);
|
||||||
/* enable acknowledge */
|
/* enable acknowledge */
|
||||||
@@ -132,7 +136,7 @@ void setup_adc() {
|
|||||||
/* config ADC clock */
|
/* config ADC clock */
|
||||||
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV16);
|
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV16);
|
||||||
// Run in normal parallel + inserted parallel
|
// Run in normal parallel + inserted parallel
|
||||||
adc_mode_config(ADC0,ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL);
|
adc_mode_config(ADC0, ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL);
|
||||||
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
|
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
|
||||||
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
|
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
|
||||||
adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, ENABLE);
|
adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, ENABLE);
|
||||||
@@ -225,8 +229,10 @@ void setup_timers() {
|
|||||||
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
|
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
|
||||||
timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);
|
timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);
|
||||||
|
|
||||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, powerPWM + holdoffTicks);
|
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0,
|
||||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM1);
|
powerPWM + holdoffTicks);
|
||||||
|
timer_channel_output_mode_config(TIMER1, TIMER_CH_0,
|
||||||
|
TIMER_OC_MODE_PWM1);
|
||||||
timer_channel_output_shadow_config(TIMER1, TIMER_CH_0,
|
timer_channel_output_shadow_config(TIMER1, TIMER_CH_0,
|
||||||
TIMER_OC_SHADOW_DISABLE);
|
TIMER_OC_SHADOW_DISABLE);
|
||||||
/* CH1 used for irq */
|
/* CH1 used for irq */
|
||||||
@@ -236,7 +242,8 @@ void setup_timers() {
|
|||||||
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
|
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
|
||||||
|
|
||||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 0);
|
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 0);
|
||||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
|
timer_channel_output_mode_config(TIMER1, TIMER_CH_1,
|
||||||
|
TIMER_OC_MODE_PWM0);
|
||||||
timer_channel_output_shadow_config(TIMER1, TIMER_CH_1,
|
timer_channel_output_shadow_config(TIMER1, TIMER_CH_1,
|
||||||
TIMER_OC_SHADOW_DISABLE);
|
TIMER_OC_SHADOW_DISABLE);
|
||||||
// IRQ
|
// IRQ
|
||||||
@@ -269,7 +276,8 @@ void setup_timers() {
|
|||||||
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
|
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
|
||||||
timer_channel_output_config(TIMER2, TIMER_CH_0, &timer_ocintpara);
|
timer_channel_output_config(TIMER2, TIMER_CH_0, &timer_ocintpara);
|
||||||
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, 0);
|
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, 0);
|
||||||
timer_channel_output_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_PWM0);
|
timer_channel_output_mode_config(TIMER2, TIMER_CH_0,
|
||||||
|
TIMER_OC_MODE_PWM0);
|
||||||
timer_channel_output_shadow_config(TIMER2, TIMER_CH_0,
|
timer_channel_output_shadow_config(TIMER2, TIMER_CH_0,
|
||||||
TIMER_OC_SHADOW_DISABLE);
|
TIMER_OC_SHADOW_DISABLE);
|
||||||
timer_auto_reload_shadow_enable(TIMER2);
|
timer_auto_reload_shadow_enable(TIMER2);
|
||||||
|
|||||||
@@ -14,8 +14,9 @@ void preRToSInit() {
|
|||||||
//Normal system bringup -- GPIO etc
|
//Normal system bringup -- GPIO etc
|
||||||
|
|
||||||
hardware_init();
|
hardware_init();
|
||||||
|
delay_ms(5);
|
||||||
gpio_bit_reset(OLED_RESET_GPIO_Port, OLED_RESET_Pin);
|
gpio_bit_reset(OLED_RESET_GPIO_Port, OLED_RESET_Pin);
|
||||||
FRToSI2C::FRToSInit();
|
|
||||||
delay_ms(50);
|
delay_ms(50);
|
||||||
gpio_bit_set(OLED_RESET_GPIO_Port, OLED_RESET_Pin);
|
gpio_bit_set(OLED_RESET_GPIO_Port, OLED_RESET_Pin);
|
||||||
|
FRToSI2C::FRToSInit();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,12 +96,12 @@ void OLED::initialize() {
|
|||||||
// Set the display to be ON once the settings block is sent and send the
|
// Set the display to be ON once the settings block is sent and send the
|
||||||
// initialisation data to the OLED.
|
// initialisation data to the OLED.
|
||||||
|
|
||||||
setDisplayState(DisplayState::ON);
|
|
||||||
for (int tries = 0; tries < 10; tries++) {
|
for (int tries = 0; tries < 10; tries++) {
|
||||||
if (FRToSI2C::writeRegistersBulk(DEVICEADDR_OLED, OLED_Setup_Array, sizeof(OLED_Setup_Array) / sizeof(OLED_Setup_Array[0]))) {
|
if (FRToSI2C::writeRegistersBulk(DEVICEADDR_OLED, OLED_Setup_Array, sizeof(OLED_Setup_Array) / sizeof(OLED_Setup_Array[0]))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setDisplayState(DisplayState::ON);
|
||||||
}
|
}
|
||||||
void OLED::setFramebuffer(uint8_t *buffer) {
|
void OLED::setFramebuffer(uint8_t *buffer) {
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
@@ -171,7 +171,7 @@ void OLED::transitionSecondaryFramebuffer(bool forwardNavigation) {
|
|||||||
|
|
||||||
while (duration <= totalDuration) {
|
while (duration <= totalDuration) {
|
||||||
duration = xTaskGetTickCount() - start;
|
duration = xTaskGetTickCount() - start;
|
||||||
uint8_t progress = duration * 1000 / totalDuration;
|
uint8_t progress = duration * TICKS_SECOND / totalDuration;
|
||||||
progress = easeInOutTiming(progress);
|
progress = easeInOutTiming(progress);
|
||||||
progress = lerp(0, OLED_WIDTH, progress);
|
progress = lerp(0, OLED_WIDTH, progress);
|
||||||
if (progress > OLED_WIDTH) {
|
if (progress > OLED_WIDTH) {
|
||||||
|
|||||||
Reference in New Issue
Block a user