Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | /* * Copyright (c) 2017 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ /** * @file * @brief Private API for SPI drivers */ #ifndef __SPI_DRIVER_COMMON_H__ #define __SPI_DRIVER_COMMON_H__ #include <gpio.h> #include <spi.h> #ifdef __cplusplus extern "C" { #endif struct spi_context { struct spi_config *config; struct k_sem lock; struct k_sem sync; int sync_status; #ifdef CONFIG_POLL struct k_poll_signal *signal; bool asynchronous; #endif const struct spi_buf *current_tx; size_t tx_count; struct spi_buf *current_rx; size_t rx_count; u8_t *tx_buf; size_t tx_len; u8_t *rx_buf; size_t rx_len; }; #define SPI_CONTEXT_INIT_LOCK(_data, _ctx_name) \ ._ctx_name.lock = _K_SEM_INITIALIZER(_data._ctx_name.lock, 0, 1) #define SPI_CONTEXT_INIT_SYNC(_data, _ctx_name) \ ._ctx_name.sync = _K_SEM_INITIALIZER(_data._ctx_name.sync, 0, 1) static inline bool spi_context_configured(struct spi_context *ctx, struct spi_config *config) { return !!(ctx->config == config); } static inline void spi_context_lock(struct spi_context *ctx, bool asynchronous, struct k_poll_signal *signal) { k_sem_take(&ctx->lock, K_FOREVER); #ifdef CONFIG_POLL ctx->asynchronous = asynchronous; ctx->signal = signal; #endif } static inline void spi_context_release(struct spi_context *ctx, int status) { if (!status && (ctx->config->operation & SPI_LOCK_ON)) { return; } #ifdef CONFIG_POLL if (!ctx->asynchronous || status) { k_sem_give(&ctx->lock); } #else k_sem_give(&ctx->lock); #endif } static inline void spi_context_unlock_unconditionally(struct spi_context *ctx) { if (!k_sem_count_get(&ctx->lock)) { k_sem_give(&ctx->lock); } } static inline int spi_context_wait_for_completion(struct spi_context *ctx) { int status = 0; #ifdef CONFIG_POLL if (!ctx->asynchronous) { k_sem_take(&ctx->sync, K_FOREVER); status = ctx->sync_status; } #else k_sem_take(&ctx->sync, K_FOREVER); status = ctx->sync_status; #endif return status; } static inline void spi_context_complete(struct spi_context *ctx, int status) { #ifdef CONFIG_POLL if (!ctx->asynchronous) { ctx->sync_status = status; k_sem_give(&ctx->sync); } else { if (ctx->signal) { k_poll_signal(ctx->signal, status); } if (!(ctx->config->operation & SPI_LOCK_ON)) { k_sem_give(&ctx->lock); } } #else ctx->sync_status = status; k_sem_give(&ctx->sync); #endif } static inline void spi_context_cs_configure(struct spi_context *ctx) { if (ctx->config->cs && ctx->config->cs->gpio_dev) { gpio_pin_configure(ctx->config->cs->gpio_dev, ctx->config->cs->gpio_pin, GPIO_DIR_OUT); gpio_pin_write(ctx->config->cs->gpio_dev, ctx->config->cs->gpio_pin, 1); } else { SYS_LOG_INF("CS control inhibited (no GPIO device)"); } } static inline void spi_context_cs_control(struct spi_context *ctx, bool on) { if (ctx->config->cs && ctx->config->cs->gpio_dev) { if (on) { gpio_pin_write(ctx->config->cs->gpio_dev, ctx->config->cs->gpio_pin, 0); k_busy_wait(ctx->config->cs->delay); } else { if (ctx->config->operation & SPI_HOLD_ON_CS) { return; } k_busy_wait(ctx->config->cs->delay); gpio_pin_write(ctx->config->cs->gpio_dev, ctx->config->cs->gpio_pin, 1); } } } static inline void spi_context_buffers_setup(struct spi_context *ctx, const struct spi_buf *tx_bufs, size_t tx_count, struct spi_buf *rx_bufs, size_t rx_count, u8_t dfs) { SYS_LOG_DBG("tx_bufs %p (%zu) - rx_bufs %p (%zu) - %u", tx_bufs, tx_count, rx_bufs, rx_count, dfs); ctx->current_tx = tx_bufs; ctx->tx_count = tx_count; ctx->current_rx = rx_bufs; ctx->rx_count = rx_count; if (tx_bufs) { ctx->tx_buf = tx_bufs->buf; ctx->tx_len = tx_bufs->len / dfs; } else { ctx->tx_buf = NULL; ctx->tx_len = 0; } if (rx_bufs) { ctx->rx_buf = rx_bufs->buf; ctx->rx_len = rx_bufs->len / dfs; } else { ctx->rx_buf = NULL; ctx->rx_len = 0; } ctx->sync_status = 0; SYS_LOG_DBG("current_tx %p (%zu), current_rx %p (%zu)," " tx buf/len %p/%zu, rx buf/len %p/%zu", ctx->current_tx, ctx->tx_count, ctx->current_rx, ctx->rx_count, ctx->tx_buf, ctx->tx_len, ctx->rx_buf, ctx->rx_len); } static ALWAYS_INLINE void spi_context_update_tx(struct spi_context *ctx, u8_t dfs, u32_t len) { if (!ctx->tx_len) { return; } if (len > ctx->tx_len) { SYS_LOG_ERR("Update exceeds current buffer"); return; } ctx->tx_len -= len; if (!ctx->tx_len) { ctx->current_tx++; ctx->tx_count--; if (ctx->tx_count) { ctx->tx_buf = ctx->current_tx->buf; ctx->tx_len = ctx->current_tx->len / dfs; } else { ctx->tx_buf = NULL; } } else if (ctx->tx_buf) { ctx->tx_buf += dfs * len; } SYS_LOG_DBG("tx buf/len %p/%zu", ctx->tx_buf, ctx->tx_len); } static ALWAYS_INLINE bool spi_context_tx_on(struct spi_context *ctx) { return !!(ctx->tx_buf || ctx->tx_len); } static ALWAYS_INLINE void spi_context_update_rx(struct spi_context *ctx, u8_t dfs, u32_t len) { if (!ctx->rx_len) { return; } if (len > ctx->rx_len) { SYS_LOG_ERR("Update exceeds current buffer"); return; } ctx->rx_len -= len; if (!ctx->rx_len) { ctx->current_rx++; ctx->rx_count--; if (ctx->rx_count) { ctx->rx_buf = ctx->current_rx->buf; ctx->rx_len = ctx->current_rx->len / dfs; } else { ctx->rx_buf = NULL; } } else if (ctx->rx_buf) { ctx->rx_buf += dfs * len; } SYS_LOG_DBG("rx buf/len %p/%zu", ctx->rx_buf, ctx->rx_len); } static ALWAYS_INLINE bool spi_context_rx_on(struct spi_context *ctx) { return !!(ctx->rx_buf || ctx->rx_len); } static inline size_t spi_context_longest_current_buf(struct spi_context *ctx) { if (!ctx->tx_len) { return ctx->rx_len; } else if (!ctx->rx_len) { return ctx->tx_len; } else if (ctx->tx_len < ctx->rx_len) { return ctx->tx_len; } return ctx->rx_len; } #ifdef __cplusplus } #endif #endif /* __SPI_DRIVER_COMMON_H__ */ |