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)
41 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf)
47 spi_rq_queue_init_detached(&msg);
52 msg.callback = spi_transfer_callback;
55 if (spi_msg_rq_ins(ifc, &msg) < 0)
58 /* Wait for the request completion */
63 if (msg.flags & (SPI_MSG_FAIL | SPI_MSG_ABORT))
69 #define MIN(a,b) ((a) < (b) ? (a) : (b))
71 int8_t port_spi_set(const struct port_desc *port, void *values, size_t size)
76 assert(port->numchn == 1);
77 assert(size == port->bpch/8);
78 assert(size <= sizeof(rx));
80 ifc = spi_find_drv(NULL, port->cfg.spi.ifc);
84 if (!(ifc->flags & SPI_IFC_ON))
87 spi_transfer(ifc, port->cfg.spi.cs, size, values, rx);
89 memcpy(values, rx, MIN(size, port->numchn * port->bpch/8));