Lines Matching refs:spi
23 static aos_status_t transfer_sequence(aos_spi_t *spi, const aos_spi_msg_t *msgs, size_t num_msgs) in transfer_sequence() argument
29 …memcpy(spi->tx_buf[0], msgs[0].tx_buf, AOS_SPI_BUF_SIZE < msgs[0].count ? AOS_SPI_BUF_SIZE : msgs[… in transfer_sequence()
41 spi->x.cfg = msg->cfg; in transfer_sequence()
42 spi->x.cs = msg->cs; in transfer_sequence()
43 spi->x.hz = msg->hz; in transfer_sequence()
44 spi->x.pre_cs = msg->pre_cs; in transfer_sequence()
45 spi->x.post_cs = msg->post_cs; in transfer_sequence()
46 spi->x.pre_clk = msg->pre_clk; in transfer_sequence()
47 spi->x.post_clk = msg->post_clk; in transfer_sequence()
50 spi->x.timeout = XFER_TIMEOUT; in transfer_sequence()
51 spi->x.len = (pos + AOS_SPI_BUF_SIZE < msg->count) ? AOS_SPI_BUF_SIZE : msg->count - pos; in transfer_sequence()
52 spi->x.pos[0] = 0; in transfer_sequence()
53 spi->x.pos[1] = 0; in transfer_sequence()
54 msg_tail = (pos + spi->x.len == msg->count); in transfer_sequence()
55 spi->x.flags = (j & 0x1) ? AOS_SPI_XF_BUF_IDX : 0; in transfer_sequence()
56 spi->x.flags |= (pos == 0) ? AOS_SPI_XF_MSG_HEAD : 0; in transfer_sequence()
57 spi->x.flags |= msg_tail ? AOS_SPI_XF_MSG_TAIL : 0; in transfer_sequence()
58 spi->x.flags |= (j == 0) ? AOS_SPI_XF_SEQ_HEAD : 0; in transfer_sequence()
59 spi->x.flags |= (msg_tail && i + 1 == num_msgs) ? AOS_SPI_XF_SEQ_TAIL : 0; in transfer_sequence()
60 spi->x.flags |= msg->rx_buf ? AOS_SPI_XF_RX : 0; in transfer_sequence()
61 spi->x.flags |= msg->tx_buf ? AOS_SPI_XF_TX : 0; in transfer_sequence()
63 ret = spi->ops->start_xfer(spi); in transfer_sequence()
75 memcpy(&((uint8_t *)last_msg->rx_buf)[last_pos], spi->rx_buf[(j - 1) & 0x1], last_len); in transfer_sequence()
83 next_pos = pos + spi->x.len; in transfer_sequence()
94 … memcpy(spi->tx_buf[(j + 1) & 0x1], &((const uint8_t *)next_msg->tx_buf)[next_pos], next_len); in transfer_sequence()
98 …if (aos_event_get(&spi->event, mask, AOS_EVENT_OR, &val, spi->x.timeout) || (val & EVENT_XFER_ERRO… in transfer_sequence()
99 spi->ops->abort_xfer(spi); in transfer_sequence()
100 aos_event_set(&spi->event, 0, AOS_EVENT_AND); in transfer_sequence()
103 spi->ops->finish_xfer(spi); in transfer_sequence()
104 aos_event_set(&spi->event, 0, AOS_EVENT_AND); in transfer_sequence()
114 pos += spi->x.len; in transfer_sequence()
119 int index = (spi->x.flags & AOS_SPI_XF_BUF_IDX) ? 1 : 0; in transfer_sequence()
120 memcpy(&((uint8_t *)last_msg->rx_buf)[last_pos], spi->rx_buf[index], spi->x.len); in transfer_sequence()
128 aos_spi_t *spi; in aos_spi_transfer() local
134 spi = aos_container_of(ref->dev, aos_spi_t, dev); in aos_spi_transfer()
139 …if ((msg->cfg & AOS_SPI_MCFG_MODE_MASK) == AOS_SPI_MCFG_MODE_0 && !(spi->flags & AOS_SPI_F_MODE_0)) in aos_spi_transfer()
142 …if ((msg->cfg & AOS_SPI_MCFG_MODE_MASK) == AOS_SPI_MCFG_MODE_1 && !(spi->flags & AOS_SPI_F_MODE_1)) in aos_spi_transfer()
145 …if ((msg->cfg & AOS_SPI_MCFG_MODE_MASK) == AOS_SPI_MCFG_MODE_2 && !(spi->flags & AOS_SPI_F_MODE_2)) in aos_spi_transfer()
148 …if ((msg->cfg & AOS_SPI_MCFG_MODE_MASK) == AOS_SPI_MCFG_MODE_3 && !(spi->flags & AOS_SPI_F_MODE_3)) in aos_spi_transfer()
151 if ((msg->cfg & AOS_SPI_MCFG_LSB_FIRST) && !(spi->flags & AOS_SPI_F_LSB_FIRST)) in aos_spi_transfer()
154 if (!(msg->cfg & AOS_SPI_MCFG_LSB_FIRST) && !(spi->flags & AOS_SPI_F_MSB_FIRST)) in aos_spi_transfer()
161 …((msg->rx_buf && !(spi->flags & AOS_SPI_F_RX_1)) || (msg->tx_buf && !(spi->flags & AOS_SPI_F_TX_1)… in aos_spi_transfer()
162 (msg->rx_buf && msg->tx_buf && !(spi->flags & AOS_SPI_F_FULL_DUPLEX_1)))) in aos_spi_transfer()
166 …((msg->rx_buf && !(spi->flags & AOS_SPI_F_RX_2)) || (msg->tx_buf && !(spi->flags & AOS_SPI_F_TX_2)… in aos_spi_transfer()
167 (msg->rx_buf && msg->tx_buf && !(spi->flags & AOS_SPI_F_FULL_DUPLEX_2)))) in aos_spi_transfer()
171 …((msg->rx_buf && !(spi->flags & AOS_SPI_F_RX_4)) || (msg->tx_buf && !(spi->flags & AOS_SPI_F_TX_4)… in aos_spi_transfer()
172 (msg->rx_buf && msg->tx_buf && !(spi->flags & AOS_SPI_F_FULL_DUPLEX_4)))) in aos_spi_transfer()
176 …((msg->rx_buf && !(spi->flags & AOS_SPI_F_RX_8)) || (msg->tx_buf && !(spi->flags & AOS_SPI_F_TX_8)… in aos_spi_transfer()
177 (msg->rx_buf && msg->tx_buf && !(spi->flags & AOS_SPI_F_FULL_DUPLEX_8)))) in aos_spi_transfer()
180 if (msg->cs >= spi->num_cs) in aos_spi_transfer()
183 if (msg->hz < spi->min_hz || msg->hz > spi->max_hz) in aos_spi_transfer()
193 ret = transfer_sequence(spi, &msgs[j], i + 1 - j); in aos_spi_transfer()
206 aos_spi_t *spi = aos_container_of(dev, aos_spi_t, dev); in dev_spi_unregister() local
208 aos_event_free(&spi->event); in dev_spi_unregister()
210 if (spi->ops->unregister) in dev_spi_unregister()
211 spi->ops->unregister(spi); in dev_spi_unregister()
216 aos_spi_t *spi = aos_container_of(ref->dev, aos_spi_t, dev); in dev_spi_get() local
222 ret = spi->ops->startup(spi); in dev_spi_get()
231 aos_spi_t *spi = aos_container_of(ref->dev, aos_spi_t, dev); in dev_spi_put() local
236 spi->ops->shutdown(spi); in dev_spi_put()
245 aos_status_t aos_spi_register(aos_spi_t *spi) in aos_spi_register() argument
249 if (!spi) in aos_spi_register()
252 if (!spi->ops || !spi->ops->startup || !spi->ops->shutdown || in aos_spi_register()
253 !spi->ops->start_xfer || !spi->ops->finish_xfer || !spi->ops->abort_xfer) in aos_spi_register()
256 if (!(spi->flags & AOS_SPI_F_MODE_0) && !(spi->flags & AOS_SPI_F_MODE_1) && in aos_spi_register()
257 !(spi->flags & AOS_SPI_F_MODE_2) && !(spi->flags & AOS_SPI_F_MODE_3)) in aos_spi_register()
260 if (!(spi->flags & AOS_SPI_F_RX_1) && !(spi->flags & AOS_SPI_F_RX_2) && in aos_spi_register()
261 !(spi->flags & AOS_SPI_F_RX_4) && !(spi->flags & AOS_SPI_F_RX_8)) in aos_spi_register()
264 if (!(spi->flags & AOS_SPI_F_TX_1) && !(spi->flags & AOS_SPI_F_TX_2) && in aos_spi_register()
265 !(spi->flags & AOS_SPI_F_TX_4) && !(spi->flags & AOS_SPI_F_TX_8)) in aos_spi_register()
268 if (!(spi->flags & AOS_SPI_F_RX_1) || !(spi->flags & AOS_SPI_F_TX_1)) in aos_spi_register()
269 spi->flags &= ~AOS_SPI_F_FULL_DUPLEX_1; in aos_spi_register()
271 if (!(spi->flags & AOS_SPI_F_RX_2) || !(spi->flags & AOS_SPI_F_TX_2)) in aos_spi_register()
272 spi->flags &= ~AOS_SPI_F_FULL_DUPLEX_2; in aos_spi_register()
274 if (!(spi->flags & AOS_SPI_F_RX_4) || !(spi->flags & AOS_SPI_F_TX_4)) in aos_spi_register()
275 spi->flags &= ~AOS_SPI_F_FULL_DUPLEX_4; in aos_spi_register()
277 if (!(spi->flags & AOS_SPI_F_RX_8) || !(spi->flags & AOS_SPI_F_TX_8)) in aos_spi_register()
278 spi->flags &= ~AOS_SPI_F_FULL_DUPLEX_8; in aos_spi_register()
280 if (!(spi->flags & AOS_SPI_F_MSB_FIRST) && !(spi->flags & AOS_SPI_F_LSB_FIRST)) in aos_spi_register()
283 if (spi->flags & AOS_SPI_F_NO_CS) in aos_spi_register()
284 spi->num_cs = 1; in aos_spi_register()
286 if (spi->num_cs == 0) in aos_spi_register()
289 if (spi->min_hz == 0 || spi->min_hz > spi->max_hz) in aos_spi_register()
292 spi->dev.type = AOS_DEV_TYPE_SPI; in aos_spi_register()
293 spi->dev.ops = &dev_spi_ops; in aos_spi_register()
295 spi->dev.vfs_helper.name[0] = '\0'; in aos_spi_register()
296 spi->dev.vfs_helper.ops = NULL; in aos_spi_register()
298 aos_spin_lock_init(&spi->lock); in aos_spi_register()
300 ret = aos_event_new(&spi->event, 0); in aos_spi_register()
304 ret = aos_dev_register(&spi->dev); in aos_spi_register()
306 aos_event_free(&spi->event); in aos_spi_register()
318 size_t aos_spi_hard_push(aos_spi_t *spi, void *tx_buf, size_t count) in aos_spi_hard_push() argument
320 if (spi->x.pos[0] + count > spi->x.len) in aos_spi_hard_push()
321 count = spi->x.len - spi->x.pos[0]; in aos_spi_hard_push()
323 if ((spi->x.flags & AOS_SPI_XF_TX) && tx_buf) { in aos_spi_hard_push()
324 const uint8_t *buf = spi->tx_buf[(spi->x.flags & AOS_SPI_XF_BUF_IDX) ? 1 : 0]; in aos_spi_hard_push()
327 ((uint8_t *)tx_buf)[i] = buf[spi->x.pos[0] + i]; in aos_spi_hard_push()
330 spi->x.pos[0] += count; in aos_spi_hard_push()
335 bool aos_spi_hard_pull(aos_spi_t *spi, const void *rx_buf, size_t count) in aos_spi_hard_pull() argument
337 if (spi->x.pos[1] + count > spi->x.pos[0]) in aos_spi_hard_pull()
338 count = spi->x.pos[0] - spi->x.pos[1]; in aos_spi_hard_pull()
340 if ((spi->x.flags & AOS_SPI_XF_RX) && rx_buf) { in aos_spi_hard_pull()
341 uint8_t *buf = spi->rx_buf[(spi->x.flags & AOS_SPI_XF_BUF_IDX) ? 1 : 0]; in aos_spi_hard_pull()
344 buf[spi->x.pos[1] + i] = ((const uint8_t *)rx_buf)[i]; in aos_spi_hard_pull()
347 spi->x.pos[1] += count; in aos_spi_hard_pull()
348 if (spi->x.pos[1] == spi->x.len) { in aos_spi_hard_pull()
349 aos_event_set(&spi->event, EVENT_XFER_COMPLETE, AOS_EVENT_OR); in aos_spi_hard_pull()
356 void aos_spi_hard_fail(aos_spi_t *spi) in aos_spi_hard_fail() argument
358 aos_event_set(&spi->event, EVENT_XFER_ERROR, AOS_EVENT_OR); in aos_spi_hard_fail()