|
|
@ -167,12 +167,17 @@ uint8_t i2c_is_perif_ready(i2c_t *i2c_dev)
|
|
|
|
return ((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY);
|
|
|
|
return ((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_set_tx_counter(i2c_t *i2c_dev, uint8_t count)
|
|
|
|
void i2c_set_transfer_counter(i2c_t *i2c_dev, uint8_t count)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
I2C_BASE->CR2 &= ~(0xFF << I2C_CR2_NBYTES_Pos);
|
|
|
|
I2C_BASE->CR2 &= ~(0xFF << I2C_CR2_NBYTES_Pos);
|
|
|
|
I2C_BASE->CR2 |= (count << I2C_CR2_NBYTES_Pos);
|
|
|
|
I2C_BASE->CR2 |= (count << I2C_CR2_NBYTES_Pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t i2c_get_transfer_counter(i2c_t *i2c_dev)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return (I2C_BASE->CR2 & I2C_CR2_NBYTES) >> I2C_CR2_NBYTES_Pos;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_init_write_command(i2c_t *i2c_dev)
|
|
|
|
void i2c_init_write_command(i2c_t *i2c_dev)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
I2C_BASE->CR2 &= ~I2C_CR2_RD_WRN;
|
|
|
|
I2C_BASE->CR2 &= ~I2C_CR2_RD_WRN;
|
|
|
@ -188,9 +193,6 @@ void i2c_init_read_command(i2c_t *i2c_dev)
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Arch nemesis ! We send only one byte of data which is the register adress.
|
|
|
|
|
|
|
|
i2c_set_tx_counter(i2c_dev,1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// On This chip this shoudl be done before the start condition
|
|
|
|
// On This chip this shoudl be done before the start condition
|
|
|
|
i2c_init_write_command(i2c_dev);
|
|
|
|
i2c_init_write_command(i2c_dev);
|
|
|
|
|
|
|
|
|
|
|
@ -199,11 +201,7 @@ void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
|
|
|
|
|
|
|
|
// This device places the salve address automaticaly in the output buffer before sending the star condition
|
|
|
|
// This device places the salve address automaticaly in the output buffer before sending the star condition
|
|
|
|
i2c_send_start(i2c_dev);
|
|
|
|
i2c_send_start(i2c_dev);
|
|
|
|
i2c_dev->hardwareState = i2c_hw_out_buff_full;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
|
|
|
|
|
|
|
|
i2c_dev->periferalState = i2c_perif_address_sent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(i2c_check_nack(i2c_dev))
|
|
|
|
if(i2c_check_nack(i2c_dev))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -215,12 +213,6 @@ void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
void i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Arch nemesis ! We only setn the address
|
|
|
|
|
|
|
|
// but the counter allso socunt the bits that we have recieved
|
|
|
|
|
|
|
|
// and we want to read one byte
|
|
|
|
|
|
|
|
// every transaction counts
|
|
|
|
|
|
|
|
i2c_set_tx_counter(i2c_dev,1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// On This chip this shoudl be done before the start condition
|
|
|
|
// On This chip this shoudl be done before the start condition
|
|
|
|
i2c_init_read_command(i2c_dev);
|
|
|
|
i2c_init_read_command(i2c_dev);
|
|
|
|
|
|
|
|
|
|
|
@ -228,11 +220,7 @@ void i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress)
|
|
|
|
I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode
|
|
|
|
I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode
|
|
|
|
// This device places the salve address automaticaly in the output buffer before sending the star condition
|
|
|
|
// This device places the salve address automaticaly in the output buffer before sending the star condition
|
|
|
|
i2c_send_start(i2c_dev);
|
|
|
|
i2c_send_start(i2c_dev);
|
|
|
|
i2c_dev->hardwareState = i2c_hw_out_buff_full;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
|
|
|
|
|
|
|
|
i2c_dev->periferalState = i2c_perif_address_sent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(i2c_check_nack(i2c_dev))
|
|
|
|
if(i2c_check_nack(i2c_dev))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -263,9 +251,25 @@ uint8_t i2c_is_output_buffer_full(i2c_t *i2c_dev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t i2c_is_txis(i2c_t *i2c_dev)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(I2C_BASE->ISR & I2C_ISR_TXIS)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
i2c_dev->hardwareState = i2c_hw_out_buff_empty;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
i2c_dev->hardwareState = i2c_hw_out_buff_full;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_send_reg_address(i2c_t *i2c_dev, uint8_t *registerAddress)
|
|
|
|
void i2c_send_reg_address(i2c_t *i2c_dev, uint8_t *registerAddress)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
i2c_send_data(i2c_dev,registerAddress);
|
|
|
|
I2C_BASE->TXDR = *registerAddress;
|
|
|
|
|
|
|
|
i2c_dev->hardwareState = i2c_hw_out_buff_full;
|
|
|
|
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_send_data(i2c_t *i2c_dev, uint8_t *data)
|
|
|
|
void i2c_send_data(i2c_t *i2c_dev, uint8_t *data)
|
|
|
@ -275,7 +279,7 @@ void i2c_send_data(i2c_t *i2c_dev, uint8_t *data)
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
while(i2c_is_output_buffer_full(i2c_dev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t i2c_is_tx_complete(i2c_t *i2c_dev)
|
|
|
|
uint8_t i2c_is_transfer_complete(i2c_t *i2c_dev)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
TC: Transfer Complete (master mode)
|
|
|
|
TC: Transfer Complete (master mode)
|
|
|
@ -283,15 +287,12 @@ uint8_t i2c_is_tx_complete(i2c_t *i2c_dev)
|
|
|
|
transferred. It is cleared by software when START bit or STOP bit is set.
|
|
|
|
transferred. It is cleared by software when START bit or STOP bit is set.
|
|
|
|
Note: This bit is cleared by hardware when PE=0.
|
|
|
|
Note: This bit is cleared by hardware when PE=0.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
if(I2C_BASE->ISR & I2C_ISR_TC)
|
|
|
|
if(I2C_BASE->ISR & I2C_ISR_TC)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
i2c_dev->periferalState = i2c_perif_tx_done;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
i2c_dev->periferalState = i2c_perif_tx_ongoing;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -312,7 +313,9 @@ uint8_t i2c_is_input_buffer_full(i2c_t *i2c_dev)
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_get_input_register(i2c_t *i2c_dev, uint8_t *data)
|
|
|
|
void i2c_get_input_register(i2c_t *i2c_dev, uint8_t *data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
while(!i2c_is_input_buffer_full(i2c_dev));
|
|
|
|
*data = I2C_BASE->RXDR;
|
|
|
|
*data = I2C_BASE->RXDR;
|
|
|
|
|
|
|
|
while(!i2c_is_transfer_complete(i2c_dev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void i2c_send_nack(i2c_t *i2c_dev)
|
|
|
|
void i2c_send_nack(i2c_t *i2c_dev)
|
|
|
|