2021-08-19 01:45:40 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "../include/i2c.h"
|
|
|
|
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_set_clk(uint32_t base, uint16_t clk_div)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(I2C_CTRL_CLK_DIV_MASK << I2C_CTRL_CLK_DIV_OFFSET);
|
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= clk_div << I2C_CTRL_CLK_DIV_OFFSET;
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_set_mode(uint32_t base, i2c_mode_e mode)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
|
|
|
if (mode == I2C_MODE_MASTER)
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(1 << I2C_CTRL_MODE_BIT);
|
2021-08-19 01:45:40 +00:00
|
|
|
else
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= 1 << I2C_CTRL_MODE_BIT;
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_master_set_write(uint32_t base, uint8_t yes)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
|
|
|
if (yes)
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(1 << I2C_CTRL_WRITE_BIT);
|
2021-08-19 01:45:40 +00:00
|
|
|
else
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= 1 << I2C_CTRL_WRITE_BIT;
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_set_interrupt_enable(uint32_t base, uint8_t en)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
|
|
|
if (en)
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= 1 << I2C_CTRL_INT_EN_BIT;
|
2021-08-19 01:45:40 +00:00
|
|
|
else
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(1 << I2C_CTRL_INT_EN_BIT);
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_clear_irq_pending(uint32_t base)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= 1 << I2C_CTRL_INT_PENDING_BIT;
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
uint8_t i2c_get_irq_pending(uint32_t base)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
if (I2C_REG(base, I2C_CTRL_REG_OFFSET) & (1 << I2C_CTRL_INT_PENDING_BIT))
|
2021-08-19 01:45:40 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_master_set_info(uint32_t base, uint8_t addr, uint8_t reg, uint8_t data)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_MASTER_DATA_REG_OFFSET) = (addr << I2C_MASTER_DATA_ADDRESS_OFFSET) |
|
|
|
|
(reg << I2C_MASTER_DATA_REGREG_OFFSET) |
|
|
|
|
(data << I2C_MASTER_DATA_DATA_OFFSET);
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
uint8_t i2c_master_get_data(uint32_t base)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
data = (I2C_REG(base, I2C_MASTER_DATA_REG_OFFSET) >> I2C_MASTER_DATA_DATA_OFFSET) & I2C_MASTER_DATA_DATA_MASK;
|
2021-08-19 01:45:40 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_slave_set_address(uint32_t base, uint8_t addr)
|
2021-08-20 06:17:01 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(I2C_CTRL_SLAVE_ADDR_MASK << I2C_CTRL_SLAVE_ADDR_OFFSET);
|
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= addr << I2C_CTRL_SLAVE_ADDR_OFFSET;
|
2021-08-20 06:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_slave_set_ready(uint32_t base, uint8_t yes)
|
2021-08-20 06:17:01 +00:00
|
|
|
{
|
|
|
|
if (yes)
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= 1 << I2C_CTRL_SLAVE_RDY_BIT;
|
2021-08-20 06:17:01 +00:00
|
|
|
else
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(1 << I2C_CTRL_SLAVE_RDY_BIT);
|
2021-08-20 06:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
uint8_t i2c_slave_op_read(uint32_t base)
|
2021-08-20 06:17:01 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
if (I2C_REG(base, I2C_CTRL_REG_OFFSET) & (1 << I2C_CTRL_SLAVE_WR_BIT))
|
2021-08-20 06:17:01 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
uint32_t i2c_slave_get_op_address(uint32_t base)
|
2021-08-20 06:17:01 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
return (I2C_REG(base, I2C_SLAVE_ADDR_REG_OFFSET));
|
2021-08-20 06:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
uint32_t i2c_slave_get_op_data(uint32_t base)
|
2021-08-20 06:17:01 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
return (I2C_REG(base, I2C_SLAVE_WDATA_REG_OFFSET));
|
2021-08-20 06:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_slave_set_rsp_data(uint32_t base, uint32_t data)
|
2021-08-20 06:17:01 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_SLAVE_RDATA_REG_OFFSET) = data;
|
2021-08-20 06:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_start(uint32_t base)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) |= 1 << I2C_CTRL_START_BIT;
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
void i2c_stop(uint32_t base)
|
2021-08-19 01:45:40 +00:00
|
|
|
{
|
2021-09-29 01:24:04 +00:00
|
|
|
I2C_REG(base, I2C_CTRL_REG_OFFSET) &= ~(1 << I2C_CTRL_START_BIT);
|
2021-08-19 01:45:40 +00:00
|
|
|
}
|