Simplify the data transfer.

This commit is contained in:
Jonathan Naylor 2021-04-11 23:24:23 +01:00
parent 639caa03ee
commit 51b275d4c7
2 changed files with 12 additions and 41 deletions

View File

@ -24,8 +24,6 @@
#include "Globals.h" #include "Globals.h"
const uint16_t MAX_NBYTES_SIZE = 255U;
CI2C3::CI2C3() CI2C3::CI2C3()
{ {
} }
@ -75,66 +73,39 @@ void CI2C3::init()
I2C_Cmd(I2C3, ENABLE); I2C_Cmd(I2C3, ENABLE);
} }
uint8_t CI2C3::write(uint8_t addr, const uint8_t* data, uint16_t length) void CI2C3::write(uint8_t addr, const uint8_t* data, uint16_t length)
{ {
DEBUG2("OLED Data", addr); DEBUG2("OLED Data", addr);
DEBUG_DUMP(data, length); DEBUG_DUMP(data, length);
// Wait for the I2C transmitter to become free // Wait for the I2C transmitter to become free
if (waitISRFlagsSet(I2C_ISR_BUSY)) waitISRFlagsSet(I2C_ISR_BUSY);
return 6U;
// Configure the data transfer // Configure the data transfer
uint16_t size; configureDataTransfer(length, addr);
if (length > MAX_NBYTES_SIZE)
size = MAX_NBYTES_SIZE;
else
size = length;
configureDataTransfer(size, addr);
// Start Writing Data // Start Writing Data
while (length > 0U) { for (uint16_t i = 0U; i < length; i++) {
// Wait for the TXIS flag to be set // Wait for the TXIS flag to be set
if (waitISRFlagsSet(I2C_ISR_TXIS)) waitISRFlagsSet(I2C_ISR_TXIS);
return 6U;
// Write the byte to the TXDR // Write the byte to the TXDR
I2C3->TXDR = *data++; I2C3->TXDR = data[i];
length--;
size--;
// Configure the data transfer
if (size == 0U && length > 0U) {
if (length > MAX_NBYTES_SIZE)
size = MAX_NBYTES_SIZE;
else
size = length;
configureDataTransfer(size, addr);
}
} }
if (waitISRFlagsSet(I2C_ISR_STOPF)) waitISRFlagsSet(I2C_ISR_STOPF);
return 6U;
I2C3->ISR &= ~I2C_ISR_STOPF; I2C3->ISR &= ~I2C_ISR_STOPF;
I2C3->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN)); I2C3->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_HEAD10R | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_RD_WRN));
return 0U;
} }
bool CI2C3::waitISRFlagsSet(uint32_t flags) void CI2C3::waitISRFlagsSet(uint32_t flags)
{ {
// Wait till the specified ISR Bits are set // Wait till the specified ISR Bits are set
// More than 1 Flag can be "or"ed. // More than 1 Flag can be "or"ed.
uint32_t timeOut = HSI_VALUE; while ((I2C3->ISR & flags) != flags)
;
while ((I2C3->ISR & flags) != flags) {
if (!(timeOut--))
return false;
}
return true;
} }
void CI2C3::configureDataTransfer(uint8_t size, uint8_t addr) void CI2C3::configureDataTransfer(uint8_t size, uint8_t addr)

4
I2C3.h
View File

@ -35,10 +35,10 @@ public:
void init(); void init();
uint8_t write(uint8_t addr, const uint8_t* data, uint16_t length); void write(uint8_t addr, const uint8_t* data, uint16_t length);
private: private:
bool waitISRFlagsSet(uint32_t flags); void waitISRFlagsSet(uint32_t flags);
void configureDataTransfer(uint8_t size, uint8_t addr); void configureDataTransfer(uint8_t size, uint8_t addr);
}; };