/*
- Copyright (c) 2006-2021, RT-Thread Development Team
- SPDX-License-Identifier: Apache-2.0
- Change Logs:
- Date Author Notes
- 2019-03-08 obito0 first version
*/
include <rtthread.h>
include <rtdevice.h>
include <board.h>
include <stdio.h>
include <string.h>
/* defined the LED0 pin: PB5 */
define LED0_PIN GET_PIN(B, 5)
/* defined the LED1 pin: PE5 */
define LED1_PIN GET_PIN(E, 5)
static struct rt_i2c_bus_device i2c_bus = RT_NULL; / I2C总线设备句柄 */
static void led_init(void)
{
/* set LED0 pin mode to output /
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
/ set LED1 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_pin_write(LED0_PIN, PIN_HIGH);
rt_pin_write(LED1_PIN, PIN_HIGH);
}
rt_size_t stm32_master_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num)
{
rt_kprintf("stm32_master_xfer send mesg\r\n");
return 0;
}
rt_size_t stm32_slave_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num)
{
}
static const struct rt_i2c_bus_device_ops stm32_i2c_ops =
{
stm32_master_xfer,
stm32_slave_xfer,
NULL
};
rt_err_t register_stm32hw_i2c(struct rt_i2c_bus_device *bus, char bus_name)
{
/ 一定先要进行分配,不然直接来设置,会进入硬件hard fault */
bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
RT_ASSERT(bus != RT_NULL);
rt_memset(bus, 0, sizeof(struct rt_i2c_bus_device));
bus->ops = &stm32_i2c_ops;
return rt_i2c_bus_device_register(bus, bus_name);
}
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
rt_uint8_t buf[] = {0};
struct rt_i2c_msg msgs;
rt_uint32_t buf_size = 1;
msgs.addr = 0xa0;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = buf_size;
/* 调用I2C设备接口传输数据 */
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs;
msgs.addr = 0xa0; /* 从机地址 */
msgs.flags = RT_I2C_RD; /* 读标志 */
msgs.buf = buf; /* 读写数据缓冲区指针 */
msgs.len = len; /* 读写数据字节数 */
/* 调用I2C设备接口传输数据 */
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
int main(void)
{
char *name = "i2c1";
rt_err_t ret;
uint32_t SystemClockFreq = 0x00;
rt_uint8_t data = 0;
SystemClockFreq = HAL_RCC_GetSysClockFreq();
rt_kprintf ("System Clk is : %d MHz \r\n",SystemClockFreq/1000000);
while (1)
{
}
}
