|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2022-02-22 airm2m first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rtdevice.h> |
| 12 | +#include <rtthread.h> |
| 13 | +#include "board.h" |
| 14 | + |
| 15 | +#include <stdlib.h> |
| 16 | + |
| 17 | +#ifdef BSP_USING_HW_I2C |
| 18 | + |
| 19 | +#define DBG_TAG "drv.hwi2c" |
| 20 | +#ifdef DRV_DEBUG |
| 21 | + #define DBG_LVL DBG_LOG |
| 22 | +#else |
| 23 | + #define DBG_LVL DBG_INFO |
| 24 | +#endif /* DRV_DEBUG */ |
| 25 | +#include <rtdbg.h> |
| 26 | + |
| 27 | +#include <hal_data.h> |
| 28 | + |
| 29 | +static struct rt_i2c_bus_device prv_ra_i2c; |
| 30 | +static volatile i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED; |
| 31 | + |
| 32 | +void i2c_master_callback(i2c_master_callback_args_t *p_args) |
| 33 | +{ |
| 34 | + if (NULL != p_args) |
| 35 | + { |
| 36 | + /* capture callback event for validating the i2c transfer event*/ |
| 37 | + i2c_event = p_args->event; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static fsp_err_t validate_i2c_event(void) |
| 42 | +{ |
| 43 | + uint16_t local_time_out = UINT16_MAX; |
| 44 | + |
| 45 | + /* resetting call back event capture variable */ |
| 46 | + i2c_event = (i2c_master_event_t)0; |
| 47 | + |
| 48 | + do |
| 49 | + { |
| 50 | + /* This is to avoid infinite loop */ |
| 51 | + --local_time_out; |
| 52 | + |
| 53 | + if(0 == local_time_out) |
| 54 | + { |
| 55 | + return FSP_ERR_TRANSFER_ABORTED; |
| 56 | + } |
| 57 | + |
| 58 | + }while(i2c_event == 0); |
| 59 | + |
| 60 | + if(i2c_event != I2C_MASTER_EVENT_ABORTED) |
| 61 | + { |
| 62 | + /* Make sure this is always Reset before return*/ |
| 63 | + i2c_event = (i2c_master_event_t)0; |
| 64 | + return FSP_SUCCESS; |
| 65 | + } |
| 66 | + |
| 67 | + /* Make sure this is always Reset before return */ |
| 68 | + i2c_event = (i2c_master_event_t)0; |
| 69 | + return FSP_ERR_TRANSFER_ABORTED; |
| 70 | +} |
| 71 | + |
| 72 | +static rt_size_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus, |
| 73 | + struct rt_i2c_msg msgs[], |
| 74 | + rt_uint32_t num) |
| 75 | +{ |
| 76 | + rt_size_t i; |
| 77 | + struct rt_i2c_msg *msg = msgs; |
| 78 | + RT_ASSERT(bus != RT_NULL); |
| 79 | + fsp_err_t err = FSP_SUCCESS; |
| 80 | + bool restart = false; |
| 81 | + |
| 82 | + for (i = 0; i < num; i++) |
| 83 | + { |
| 84 | + if (msg[i].flags & RT_I2C_NO_START) |
| 85 | + { |
| 86 | + restart = true; |
| 87 | + } |
| 88 | + if (msg[i].flags & RT_I2C_ADDR_10BIT) |
| 89 | + { |
| 90 | + LOG_E("10Bit not support"); |
| 91 | + break; |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + g_i2c_master1_ctrl.slave = msg[i].addr; |
| 96 | + } |
| 97 | + |
| 98 | + if (msg[i].flags & RT_I2C_RD) |
| 99 | + { |
| 100 | + err = R_IIC_MASTER_Read(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart); |
| 101 | + if (FSP_SUCCESS == err) |
| 102 | + { |
| 103 | + err = validate_i2c_event(); |
| 104 | + /* handle error */ |
| 105 | + if(FSP_ERR_TRANSFER_ABORTED == err) |
| 106 | + { |
| 107 | + LOG_E("POWER_CTL reg I2C read failed"); |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + /* handle error */ |
| 112 | + else |
| 113 | + { |
| 114 | + /* Write API returns itself is not successful */ |
| 115 | + LOG_E("R_IIC_MASTER_Write API failed"); |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + err = R_IIC_MASTER_Write(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart); |
| 122 | + if (FSP_SUCCESS == err) |
| 123 | + { |
| 124 | + err = validate_i2c_event(); |
| 125 | + /* handle error */ |
| 126 | + if(FSP_ERR_TRANSFER_ABORTED == err) |
| 127 | + { |
| 128 | + LOG_E("POWER_CTL reg I2C write failed"); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + /* handle error */ |
| 133 | + else |
| 134 | + { |
| 135 | + /* Write API returns itself is not successful */ |
| 136 | + LOG_E("R_IIC_MASTER_Write API failed"); |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return i; |
| 142 | +} |
| 143 | + |
| 144 | +static const struct rt_i2c_bus_device_ops ra_i2c_ops = |
| 145 | +{ |
| 146 | + .master_xfer = ra_i2c_mst_xfer, |
| 147 | + .slave_xfer = RT_NULL, |
| 148 | + .i2c_bus_control = RT_NULL |
| 149 | +}; |
| 150 | + |
| 151 | +int ra_hw_i2c_init(void) |
| 152 | +{ |
| 153 | + fsp_err_t err = FSP_SUCCESS; |
| 154 | + prv_ra_i2c.ops = &ra_i2c_ops; |
| 155 | + prv_ra_i2c.priv = 0; |
| 156 | + /* opening IIC master module */ |
| 157 | + err = R_IIC_MASTER_Open(&g_i2c_master1_ctrl, &g_i2c_master1_cfg); |
| 158 | + /* handle error */ |
| 159 | + if (FSP_SUCCESS != err) |
| 160 | + { |
| 161 | + LOG_E("R_IIC_MASTER_Open API failed"); |
| 162 | + return err; |
| 163 | + } |
| 164 | + rt_i2c_bus_device_register(&prv_ra_i2c, "i2c1"); |
| 165 | + |
| 166 | + return 0; |
| 167 | +} |
| 168 | +INIT_DEVICE_EXPORT(ra_hw_i2c_init); |
| 169 | + |
| 170 | +#endif /* BSP_USING_I2C */ |
0 commit comments