1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
3 * This document contains proprietary information belonging to Czech
4 * Technical University in Prague. Passing on and copying of this
5 * document, and communication of its contents is not permitted
6 * without prior written authorization.
11 //#include "ul/ul_list.h"
12 //#include "drv/spi.h"
13 //#include "cpu_def.h"
14 //#include "ul/ul_list.h"
17 int spi_msg_rq_ins(spi_drv_t *ifc, spi_msg_head_t *msg)
19 spi_isr_lock_level_t saveif;
24 if (!(ifc->flags & SPI_IFC_ON))
28 spi_rq_queue_insert(ifc, msg);
29 spi_isr_unlock(saveif);
30 ifc->ctrl_fnc(ifc, SPI_CTRL_WAKE_RQ, NULL);
34 int spi_transfer_callback(struct spi_drv *ifc, int code, struct spi_msg_head *msg)
42 * This function is thread safe.
44 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf)
50 spi_rq_queue_init_detached(&msg);
55 msg.callback = spi_transfer_callback;
58 if (spi_msg_rq_ins(ifc, &msg) < 0)
61 /* Wait for the request completion */
66 if (msg.flags & (SPI_MSG_FAIL | SPI_MSG_ABORT))
72 #define MIN(a,b) ((a) < (b) ? (a) : (b))
74 int8_t port_spi_set(const struct port_desc *port, void *values, size_t size)
79 assert(port->numchn == 1);
80 assert(size == port->bpch/8);
81 assert(size <= sizeof(rx));
83 ifc = spi_find_drv(NULL, port->cfg.spi.ifc);
87 if (!(ifc->flags & SPI_IFC_ON))
90 spi_transfer(ifc, port->cfg.spi.cs, size, values, rx);
92 memcpy(values, rx, MIN(size, port->numchn * port->bpch/8));