2021-08-20 01:26:13 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "../../bsp/include/uart.h"
|
|
|
|
#include "../../bsp/include/i2c.h"
|
|
|
|
#include "../../bsp/include/xprintf.h"
|
|
|
|
#include "../../bsp/include/utils.h"
|
|
|
|
#include "../../bsp/include/rvic.h"
|
2021-09-10 01:56:21 +00:00
|
|
|
#include "../../bsp/include/pinmux.h"
|
2021-08-20 01:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define SLAVE_ADDR (0xA0)
|
|
|
|
|
|
|
|
static volatile uint8_t i2c_done;
|
|
|
|
|
|
|
|
static void i2c_master_write(uint8_t addr, uint8_t data)
|
|
|
|
{
|
|
|
|
i2c_done = 0;
|
2021-09-29 01:24:04 +00:00
|
|
|
i2c_master_set_write(I2C0, 1);
|
|
|
|
i2c_master_set_info(I2C0, SLAVE_ADDR, addr, data);
|
|
|
|
i2c_start(I2C0);
|
2021-08-20 01:26:13 +00:00
|
|
|
|
|
|
|
while (!i2c_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t i2c_master_read(uint8_t addr)
|
|
|
|
{
|
|
|
|
i2c_done = 0;
|
2021-09-29 01:24:04 +00:00
|
|
|
i2c_master_set_write(I2C0, 0);
|
|
|
|
i2c_master_set_info(I2C0, SLAVE_ADDR, addr, 0);
|
|
|
|
i2c_start(I2C0);
|
2021-08-20 01:26:13 +00:00
|
|
|
|
|
|
|
while (!i2c_done);
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
return (i2c_master_get_data(I2C0));
|
2021-08-20 01:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
uint8_t data, i;
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
// UART0引脚配置
|
2021-09-10 01:56:21 +00:00
|
|
|
pinmux_set_io0_func(IO0_UART0_TX);
|
|
|
|
pinmux_set_io3_func(IO3_UART0_RX);
|
2021-09-29 01:24:04 +00:00
|
|
|
// I2C0引脚配置
|
2021-09-10 01:56:21 +00:00
|
|
|
pinmux_set_io6_func(IO6_I2C0_SCL);
|
|
|
|
pinmux_set_io8_func(IO8_I2C0_SDA);
|
2021-09-29 01:24:04 +00:00
|
|
|
// 初始化串口0
|
|
|
|
uart_init(UART0, uart0_putc);
|
|
|
|
// 设置I2C0时钟, 200KHZ
|
|
|
|
i2c_set_clk(I2C0, 0x7D);
|
|
|
|
// 设置I2C0 master模式
|
|
|
|
i2c_set_mode(I2C0, I2C_MODE_MASTER);
|
|
|
|
// 使能I2C0模块中断
|
|
|
|
i2c_set_interrupt_enable(I2C0, 1);
|
|
|
|
// 设置I2C0中断优先级为1
|
|
|
|
rvic_set_irq_prio_level(RVIC_I2C0_INT_ID, 1);
|
|
|
|
// 使能RVIC中断
|
|
|
|
rvic_irq_enable(RVIC_I2C0_INT_ID);
|
|
|
|
// 使能CPU中断
|
2021-08-20 01:26:13 +00:00
|
|
|
global_irq_enable();
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
// 写测试
|
2021-08-20 01:26:13 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
i2c_master_write(i, i);
|
|
|
|
xprintf("write[%d] = 0x%x\n", i, i);
|
|
|
|
busy_wait(100 * 1000);
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
// 读测试
|
2021-08-20 01:26:13 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
data = i2c_master_read(i);
|
|
|
|
xprintf("read[%d] = 0x%x\n", i, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
// I2C0中断处理函数
|
2021-08-20 01:26:13 +00:00
|
|
|
void i2c0_irq_handler()
|
|
|
|
{
|
|
|
|
i2c_done = 1;
|
|
|
|
|
2021-09-29 01:24:04 +00:00
|
|
|
i2c_clear_irq_pending(I2C0);
|
|
|
|
rvic_clear_irq_pending(RVIC_I2C0_INT_ID);
|
2021-08-20 01:26:13 +00:00
|
|
|
}
|