|
| 1 | +/***************************************************************************//** |
| 2 | + * @file drv_iic.cpp |
| 3 | + * @brief Arduino RT-Thread library IIC device driver |
| 4 | + * @author onelife <onelife.real[at]gmail.com> |
| 5 | + ******************************************************************************/ |
| 6 | +/* Includes ------------------------------------------------------------------*/ |
| 7 | +extern "C" { |
| 8 | + |
| 9 | +#include "include/rtthread.h" |
| 10 | + |
| 11 | +#if defined(CONFIG_ARDUINO) && (CONFIG_USING_IIC0 || CONFIG_USING_IIC1) |
| 12 | +} |
| 13 | + |
| 14 | +#include <Arduino.h> |
| 15 | +#include <Wire.h> /* Arduino library */ |
| 16 | + |
| 17 | +extern "C" { |
| 18 | + |
| 19 | +#include "drv_iic.h" |
| 20 | + |
| 21 | +/***************************************************************************//** |
| 22 | + * @addtogroup Arduino |
| 23 | + * @{ |
| 24 | + ******************************************************************************/ |
| 25 | + |
| 26 | +/* Private typedef -----------------------------------------------------------*/ |
| 27 | +/* Private define ------------------------------------------------------------*/ |
| 28 | +#ifdef RT_USING_ULOG |
| 29 | +# ifdef BSP_IIC_DEBUG |
| 30 | +# define LOG_LVL LOG_LVL_DBG |
| 31 | +# else |
| 32 | +# define LOG_LVL LOG_LVL_INFO |
| 33 | +# endif |
| 34 | +# define LOG_TAG "IIC" |
| 35 | +# include "components/utilities/ulog/ulog.h" |
| 36 | +#else /* RT_USING_ULOG */ |
| 37 | +# define LOG_E(format, args...) rt_kprintf(format "\n", ##args) |
| 38 | +# define LOG_W LOG_E |
| 39 | +# ifdef BSP_SPI_DEBUG |
| 40 | +# define LOG_I(format, args...) rt_kprintf(format "\n", ##args) |
| 41 | +# else |
| 42 | +# define LOG_I(format, args...) |
| 43 | +# endif |
| 44 | +# define LOG_D LOG_I |
| 45 | +# define LOG_HEX(format, args...) |
| 46 | +#endif /* RT_USING_ULOG */ |
| 47 | + |
| 48 | +#if CONFIG_USING_IIC0 |
| 49 | +# define _IIC0 Wire |
| 50 | +#endif |
| 51 | +#if CONFIG_USING_IIC1 |
| 52 | +# define _IIC1 Wire1 |
| 53 | +#endif |
| 54 | +#define IIC_NAME(ch) "IIC"#ch |
| 55 | +#define IIC_CTX(idx) iic_ctx[idx] |
| 56 | +#define IIC_DEV(ctx) ((TwoWire *)((ctx)->ldev)) |
| 57 | +#define IIC_TX_START(ctx, addr) \ |
| 58 | + LOG_D("[IIC%d] TX START: %02x", ctx->chn, addr); \ |
| 59 | + IIC_DEV(ctx)->beginTransmission(addr) |
| 60 | +#define IIC_TX_STOP(ctx) \ |
| 61 | + LOG_D("[IIC%d] TX STOP", ctx->chn); \ |
| 62 | + (void)IIC_DEV(ctx)->endTransmission() |
| 63 | +#define IIC_TX(ctx, data) (void)IIC_DEV(ctx)->write((rt_uint8_t)data) |
| 64 | +#define IIC_RX_START(ctx, addr, sz, stop) \ |
| 65 | + LOG_D("[IIC%d] RX START: %02x, %d, %d", ctx->chn, addr, sz, stop); \ |
| 66 | + IIC_DEV(ctx)->requestFrom(addr, sz, stop) |
| 67 | +#define IIC_RX(ctx, buf) { \ |
| 68 | + while (!IIC_DEV(ctx)->available()); \ |
| 69 | + *(buf) = IIC_DEV(ctx)->read(); \ |
| 70 | +} |
| 71 | +#define SLAVE_ADDR(addr) (rt_uint8_t)(addr & 0x000000ff) |
| 72 | +#define ACCESS_ADDR(flags) (rt_uint8_t)((flags & 0x0000ff00) >> 8) |
| 73 | +#define MAX_RX_SIZE (0xff) |
| 74 | + |
| 75 | +/* Private variables ---------------------------------------------------------*/ |
| 76 | +static struct bsp_iic_contex iic_ctx[IIC_CH_NUM]; |
| 77 | + |
| 78 | +/* Private function prototypes -----------------------------------------------*/ |
| 79 | +/* Private functions ---------------------------------------------------------*/ |
| 80 | +static rt_err_t bsp_iic_open(rt_device_t dev, rt_uint16_t oflag) { |
| 81 | + struct bsp_iic_contex *ctx = (struct bsp_iic_contex *)(dev->user_data); |
| 82 | + rt_err_t ret; |
| 83 | + |
| 84 | + do { |
| 85 | + ret = rt_mutex_take(&ctx->lok, RT_WAITING_NO); |
| 86 | + if (RT_EOK != ret) break; |
| 87 | + |
| 88 | + ctx->dev.open_flag = oflag & RT_DEVICE_OFLAG_MASK; |
| 89 | + LOG_D("[IIC%d] open with flag %x", ctx->chn, oflag); |
| 90 | + } while (0); |
| 91 | + |
| 92 | + if (RT_EOK != ret) { |
| 93 | + LOG_W("[IIC%d E] open err [%08x]", ctx->chn, ret); |
| 94 | + } |
| 95 | + |
| 96 | + return ret; |
| 97 | +} |
| 98 | + |
| 99 | +static rt_err_t bsp_iic_close(rt_device_t dev) { |
| 100 | + struct bsp_iic_contex *ctx = (struct bsp_iic_contex *)(dev->user_data); |
| 101 | + rt_err_t ret; |
| 102 | + |
| 103 | + do { |
| 104 | + ret = rt_mutex_release(&ctx->lok); |
| 105 | + if (RT_EOK != ret) break; |
| 106 | + |
| 107 | + LOG_D("[IIC%d] closed", ctx->chn); |
| 108 | + } while (0); |
| 109 | + |
| 110 | + if (RT_EOK != ret) { |
| 111 | + LOG_W("[IIC%d E] close err [%08x]", ctx->chn, ret); |
| 112 | + } |
| 113 | + return ret; |
| 114 | +} |
| 115 | + |
| 116 | +static rt_size_t bsp_iic_read(rt_device_t dev, rt_off_t _addr, void *_buf, |
| 117 | + rt_size_t size) { |
| 118 | + struct bsp_iic_contex *ctx = (struct bsp_iic_contex *)(dev->user_data); |
| 119 | + rt_bool_t locked; |
| 120 | + rt_err_t err; |
| 121 | + rt_size_t ret; |
| 122 | + |
| 123 | + if (RT_NULL == _addr) return -RT_EINVAL; |
| 124 | + |
| 125 | + do { |
| 126 | + rt_uint8_t i; |
| 127 | + rt_uint8_t slave = SLAVE_ADDR(_addr); |
| 128 | + rt_uint8_t addr = ACCESS_ADDR(_addr); |
| 129 | + rt_uint8_t *buf = (rt_uint8_t *)_buf; |
| 130 | + |
| 131 | + locked = RT_FALSE; |
| 132 | + ret = 0; |
| 133 | + |
| 134 | + if ((RT_NULL == _addr) || \ |
| 135 | + (RT_DEVICE_OFLAG_WRONLY == (ctx->dev.open_flag & 0x0003))) { |
| 136 | + err = -RT_EINVAL; |
| 137 | + break; |
| 138 | + } |
| 139 | + |
| 140 | + err = rt_mutex_take(&ctx->lok, RT_WAITING_NO); |
| 141 | + if (RT_EOK != err) break; |
| 142 | + locked = RT_TRUE; |
| 143 | + |
| 144 | + /* send slave addr */ |
| 145 | + IIC_TX_START(ctx, slave); |
| 146 | + IIC_TX(ctx, addr); |
| 147 | + IIC_TX_STOP(ctx); |
| 148 | + |
| 149 | + /* receive data */ |
| 150 | + ret = size; |
| 151 | + while (size) { |
| 152 | + rt_uint8_t stop = (size > MAX_RX_SIZE) ? 0 : 1; |
| 153 | + rt_uint8_t sz = stop ? size : MAX_RX_SIZE; |
| 154 | + IIC_RX_START(ctx, slave, sz, stop); |
| 155 | + for (i = 0; i < sz; i++) { |
| 156 | + IIC_RX(ctx, buf++); |
| 157 | + } |
| 158 | + size -= sz; |
| 159 | + } |
| 160 | + } while (0); |
| 161 | + |
| 162 | + if (locked) rt_mutex_release(&ctx->lok); |
| 163 | + if (RT_EOK != err) rt_set_errno(err); |
| 164 | + |
| 165 | + return ret; |
| 166 | +} |
| 167 | + |
| 168 | +static rt_size_t bsp_iic_write(rt_device_t dev, rt_off_t _addr, |
| 169 | + const void *_buf, rt_size_t size) { |
| 170 | + struct bsp_iic_contex *ctx = (struct bsp_iic_contex *)(dev->user_data); |
| 171 | + rt_bool_t locked; |
| 172 | + rt_err_t err; |
| 173 | + rt_size_t ret; |
| 174 | + |
| 175 | + if (RT_NULL == _addr) return -RT_EINVAL; |
| 176 | + if (RT_DEVICE_OFLAG_RDONLY == (ctx->dev.open_flag & 0x0003)) |
| 177 | + return -RT_EINVAL; |
| 178 | + |
| 179 | + do { |
| 180 | + rt_size_t i; |
| 181 | + rt_uint8_t slave = SLAVE_ADDR(_addr); |
| 182 | + rt_uint8_t addr = ACCESS_ADDR(_addr); |
| 183 | + rt_uint8_t *buf = (rt_uint8_t *)_buf; |
| 184 | + |
| 185 | + locked = RT_FALSE; |
| 186 | + ret = 0; |
| 187 | + |
| 188 | + err = rt_mutex_take(&ctx->lok, RT_WAITING_NO); |
| 189 | + if (RT_EOK != err) break; |
| 190 | + locked = RT_TRUE; |
| 191 | + |
| 192 | + /* send slave addr and data */ |
| 193 | + IIC_TX_START(ctx, slave); |
| 194 | + IIC_TX(ctx, addr); |
| 195 | + for (i = 0; i < size; i++) { |
| 196 | + IIC_TX(ctx, *(buf++)); |
| 197 | + } |
| 198 | + IIC_TX_STOP(ctx); |
| 199 | + |
| 200 | + ret = size; |
| 201 | + } while (0); |
| 202 | + |
| 203 | + if (locked) rt_mutex_release(&ctx->lok); |
| 204 | + if (RT_EOK != err) rt_set_errno(err); |
| 205 | + |
| 206 | + return ret; |
| 207 | +} |
| 208 | + |
| 209 | +/***************************************************************************//** |
| 210 | + * @brief Initialize IIC contex |
| 211 | + * |
| 212 | + * @param[in] struct bsp_iic_contex *ctx - Pointer to IIC contex |
| 213 | + * |
| 214 | + * @param[in] rt_uint8_t chn - IIC channel number |
| 215 | + * |
| 216 | + * @param[in] const char *name - Pointer to IIC name |
| 217 | + * |
| 218 | + * @param[in] void *ldev - Pointer to lower level device |
| 219 | + * |
| 220 | + * @return rt_err_t - Error code |
| 221 | + * |
| 222 | + ******************************************************************************/ |
| 223 | +static rt_err_t bsp_iic_contex_init(struct bsp_iic_contex *ctx, rt_uint8_t chn, |
| 224 | + const char *name, void *ldev) { |
| 225 | + rt_err_t ret; |
| 226 | + |
| 227 | + do { |
| 228 | + ctx->chn = chn; |
| 229 | + // ctx->start = RT_FALSE; |
| 230 | + ctx->ldev = ldev; |
| 231 | + |
| 232 | + /* init lock */ |
| 233 | + ret = rt_mutex_init(&ctx->lok, name, RT_IPC_FLAG_FIFO); |
| 234 | + if (RT_EOK != ret) break; |
| 235 | + |
| 236 | + /* register device */ |
| 237 | + ctx->dev.type = RT_Device_Class_I2CBUS; |
| 238 | + ctx->dev.rx_indicate = RT_NULL; |
| 239 | + ctx->dev.tx_complete = RT_NULL; |
| 240 | + ctx->dev.init = RT_NULL; |
| 241 | + ctx->dev.open = bsp_iic_open; |
| 242 | + ctx->dev.close = bsp_iic_close; |
| 243 | + ctx->dev.read = bsp_iic_read; |
| 244 | + ctx->dev.write = bsp_iic_write; |
| 245 | + ctx->dev.control = RT_NULL; |
| 246 | + ctx->dev.user_data = (void *)ctx; |
| 247 | + ret = rt_device_register(&ctx->dev, name, RT_DEVICE_FLAG_RDWR); |
| 248 | + } while (0); |
| 249 | + |
| 250 | + return ret; |
| 251 | +} |
| 252 | + |
| 253 | +/* Public functions ----------------------------------------------------------*/ |
| 254 | +/***************************************************************************//** |
| 255 | + * @brief - initialize IIC contex and hardware |
| 256 | + * |
| 257 | + * @return rt_err_t - Error code |
| 258 | + * |
| 259 | + ******************************************************************************/ |
| 260 | +rt_err_t bsp_hw_iic_init(void) { |
| 261 | + rt_uint8_t iic_chs[IIC_CH_NUM] = { |
| 262 | + #if CONFIG_USING_IIC0 |
| 263 | + IIC_CH0, |
| 264 | + #endif |
| 265 | + #if CONFIG_USING_IIC1 |
| 266 | + IIC_CH1, |
| 267 | + #endif |
| 268 | + }; |
| 269 | + void *ldev; |
| 270 | + const char *name; |
| 271 | + rt_uint8_t i; |
| 272 | + rt_uint8_t chn; |
| 273 | + rt_err_t ret; |
| 274 | + |
| 275 | + for (i = 0; i < sizeof(iic_chs); i++) { |
| 276 | + chn = iic_chs[i]; |
| 277 | + |
| 278 | + switch (chn) { |
| 279 | + #if CONFIG_USING_IIC0 |
| 280 | + case 0: |
| 281 | + ldev = (void *)&_IIC0; |
| 282 | + name = IIC_NAME(0); |
| 283 | + break; |
| 284 | + #endif |
| 285 | + #if CONFIG_USING_IIC1 |
| 286 | + case 1: |
| 287 | + ldev = (void *)&_IIC1; |
| 288 | + name = IIC_NAME(1); |
| 289 | + break; |
| 290 | + #endif |
| 291 | + default: |
| 292 | + return -RT_ERROR; |
| 293 | + } |
| 294 | + |
| 295 | + ret = bsp_iic_contex_init(&IIC_CTX(i), chn, name, ldev); |
| 296 | + if (RT_EOK != ret) break; |
| 297 | + |
| 298 | + IIC_DEV(&IIC_CTX(i))->begin(); |
| 299 | + LOG_D("[IIC%d] h/w init ok!", chn); |
| 300 | + } |
| 301 | + |
| 302 | + if (RT_EOK != ret) |
| 303 | + LOG_E("[IIC%d E] h/w init failed!", chn); |
| 304 | + |
| 305 | + return ret; |
| 306 | +} |
| 307 | + |
| 308 | +/***************************************************************************//** |
| 309 | + * @} |
| 310 | + ******************************************************************************/ |
| 311 | + |
| 312 | +#endif /* defined(CONFIG_ARDUINO) && (CONFIG_USING_IIC0 || CONFIG_USING_IIC1) */ |
| 313 | + |
| 314 | +} /* extern "C" */ |
0 commit comments