|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2019-01-24 wangyq the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rthw.h> |
| 12 | +#include <rtthread.h> |
| 13 | +#include <rtdevice.h> |
| 14 | +#include "board.h" |
| 15 | +#include "drv_i2c.h" |
| 16 | +#include <ald_i2c.h> |
| 17 | +#include <ald_gpio.h> |
| 18 | + |
| 19 | +#ifdef RT_USING_I2C |
| 20 | + |
| 21 | +#define TIMEOUT 0x0FFF |
| 22 | + |
| 23 | +/*define i2c Instance*/ |
| 24 | +struct rt_i2c_bus_device _i2c_device0; |
| 25 | +struct rt_i2c_bus_device _i2c_device1; |
| 26 | +i2c_handle_t _h_i2c0, _h_i2c1; |
| 27 | + |
| 28 | +static void _i2c_init(void) |
| 29 | +{ |
| 30 | + gpio_init_t gpio_instruct; //i2c function init |
| 31 | + |
| 32 | + /* Initialize I2C Pin*/ |
| 33 | + gpio_instruct.mode = GPIO_MODE_OUTPUT; |
| 34 | + gpio_instruct.odos = GPIO_PUSH_PULL; |
| 35 | + gpio_instruct.pupd = GPIO_PUSH_UP; |
| 36 | + gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL; |
| 37 | + gpio_instruct.flt = GPIO_FILTER_DISABLE; |
| 38 | + gpio_instruct.type = GPIO_TYPE_CMOS; |
| 39 | + gpio_instruct.func = GPIO_FUNC_5; |
| 40 | + |
| 41 | +#ifdef BSP_USING_I2C0 |
| 42 | + /* Initialize I2C Function */ |
| 43 | + _h_i2c0.perh = I2C0; |
| 44 | + _h_i2c0.init.clk_speed = 100000; |
| 45 | + _h_i2c0.init.duty = I2C_DUTYCYCLE_2; |
| 46 | + _h_i2c0.init.own_addr1 = 0x0A; |
| 47 | + _h_i2c0.init.addr_mode = I2C_ADDR_7BIT; |
| 48 | + _h_i2c0.init.general_call = I2C_GENERALCALL_DISABLE; |
| 49 | + _h_i2c0.init.no_stretch = I2C_NOSTRETCH_ENABLE; |
| 50 | + |
| 51 | + i2c_reset(&_h_i2c0); |
| 52 | + i2c_init(&_h_i2c0); |
| 53 | + /* I2C0_SCL->PB8, I2C0_SDA->PB9 */ |
| 54 | + gpio_init(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, &gpio_instruct); |
| 55 | +#endif/*BSP_USING_I2C0*/ |
| 56 | + |
| 57 | +#ifdef BSP_USING_I2C1 |
| 58 | + /* Initialize i2c function */ |
| 59 | + _h_i2c1.perh = I2C1; |
| 60 | + _h_i2c1.init.clk_speed = 100000; |
| 61 | + _h_i2c1.init.duty = I2C_DUTYCYCLE_2; |
| 62 | + _h_i2c1.init.own_addr1 = 0xA0; |
| 63 | + _h_i2c1.init.addr_mode = I2C_ADDR_7BIT; |
| 64 | + _h_i2c1.init.general_call = I2C_GENERALCALL_DISABLE; |
| 65 | + _h_i2c1.init.no_stretch = I2C_NOSTRETCH_ENABLE; |
| 66 | + |
| 67 | + i2c_reset(&_h_i2c1); |
| 68 | + i2c_init(&_h_i2c1); |
| 69 | + /* I2C1_SCL->PB10, I2C1_SDA->PB11 */ |
| 70 | + gpio_init(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, &gpio_instruct); |
| 71 | +#endif/*BSP_USING_I2C1*/ |
| 72 | +} |
| 73 | + |
| 74 | +static rt_size_t es32f0_master_xfer(struct rt_i2c_bus_device *bus, |
| 75 | + struct rt_i2c_msg msgs[], |
| 76 | + rt_uint32_t num) |
| 77 | +{ |
| 78 | + struct rt_i2c_msg *msg; |
| 79 | + rt_uint32_t i; |
| 80 | + rt_err_t ret = RT_ERROR; |
| 81 | + |
| 82 | + for (i = 0; i < num; i++) |
| 83 | + { |
| 84 | + msg = &msgs[i]; |
| 85 | + if (msg->flags & RT_I2C_RD) |
| 86 | + { |
| 87 | + if (i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0) |
| 88 | + { |
| 89 | + i2c_dbg("i2c bus write failed,i2c bus stop!\n"); |
| 90 | + goto out; |
| 91 | + } |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + if (i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0) |
| 96 | + { |
| 97 | + i2c_dbg("i2c bus write failed,i2c bus stop!\n"); |
| 98 | + goto out; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + ret = i; |
| 104 | + |
| 105 | +out: |
| 106 | + i2c_dbg("send stop condition\n"); |
| 107 | + |
| 108 | + return ret; |
| 109 | +} |
| 110 | + |
| 111 | +const struct rt_i2c_bus_device_ops es32f0_i2c_ops = |
| 112 | +{ |
| 113 | + es32f0_master_xfer, |
| 114 | + RT_NULL, |
| 115 | + RT_NULL, |
| 116 | +}; |
| 117 | + |
| 118 | +int rt_hw_i2c_init(void) |
| 119 | +{ |
| 120 | + _i2c_init(); |
| 121 | + |
| 122 | +#ifdef BSP_USING_I2C0 |
| 123 | + rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device)); |
| 124 | + _i2c_device0.ops = &es32f0_i2c_ops; |
| 125 | + _i2c_device0.priv = &_h_i2c0; |
| 126 | + rt_i2c_bus_device_register(&_i2c_device0, "i2c0"); |
| 127 | +#endif |
| 128 | + |
| 129 | +#ifdef BSP_USING_I2C1 |
| 130 | + rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device)); |
| 131 | + _i2c_device1.ops = &es32f0_i2c_ops; |
| 132 | + _i2c_device1.priv = &_h_i2c1; |
| 133 | + rt_i2c_bus_device_register(&_i2c_device1, "i2c1"); |
| 134 | +#endif |
| 135 | + |
| 136 | + return RT_EOK; |
| 137 | +} |
| 138 | +INIT_DEVICE_EXPORT(rt_hw_i2c_init); |
| 139 | +/* end of i2c driver */ |
| 140 | +#endif |
0 commit comments