Never figured out what was going wrong with the above, copy and pasted the code into i2c2, that didn't work, wrote it from scratch without any while delays and it worked fine, added the whiles back in and it it still works fine. Here is the working code.
P.s. I didn't meet any of the conditions for the issues outlined in the errata
void ll_i2c2_init(void){
ll_clocks_enable_i2c2();
ll_gpio_remap_i2c2();
I2C2->CR1 |= 16; // bottom 5 bits are frequency 16MHz
I2C2->CCR = 50;
I2C2->TRISE = 9;
I2C2->CR1 |= I2C_CR1_PE;
}
void ll_i2c2_transmit(uint8_t address, uint8_t message){
uint16_t register_reader = 0;
address <<= 1;
address &= ~0x01; //7 bit address, LSB 0 = write
I2C2->CR1 &= ~I2C_CR1_STOP;
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
I2C2->CR1 |= I2C_CR1_START;
while(!(I2C2->SR1 & I2C_SR1_SB));
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
I2C2->DR = address;
while(!(I2C2->SR1 & I2C_SR1_ADDR));
//The ADDR bit is only set after the ACK is received
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
//I2C2->CR1 |= I2C_CR1_START;
I2C2->DR = message;
//I2C2->CR1 |= I2C_CR1_START;
I2C2->CR1 |= I2C_CR1_STOP;
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
}
uint16_t ll_i2c2_read(uint8_t address){
uint16_t register_reader = 0;
uint16_t received_data = 0;
address <<= 1;
address |= 0x01; //7 bit address, LSB 1 = read
I2C2->CR1 &= ~I2C_CR1_STOP;
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
I2C2->CR1 |= (I2C_CR1_START | I2C_CR1_ACK);
while(!(I2C2->SR1 & I2C_SR1_SB));
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
I2C2->DR = address;
while(!(I2C2->SR1 & I2C_SR1_ADDR));
//The ADDR bit is only set after the ACK is received
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
//I2C2->CR1 |= I2C_CR1_START;
while(!(I2C2->SR1 & I2C_SR1_RXNE));
received_data = I2C2->DR;
received_data <<= 8;
while(!(I2C2->SR1 & I2C_SR1_RXNE));
received_data |= I2C2->DR;
//I2C2->CR1 |= I2C_CR1_START;
I2C2->CR1 |= I2C_CR1_STOP;
register_reader = I2C2->SR1;
register_reader = I2C2->SR2;
I2C2->CR1 &= ~I2C_CR1_ACK;
return received_data;
}