]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/drv/spi.c
Just a few errors left on the library.
[pes-rpp/rpp-lib.git] / rpp / src / drv / spi.c
1
2 //#include "ul/ul_list.h"
3 //#include "drv/spi.h"
4 //#include "cpu_def.h"
5 //#include "ul/ul_list.h"
6 #include "drv/drv.h"
7
8 int spi_msg_rq_ins(spi_drv_t *ifc, spi_msg_head_t *msg)
9 {
10     spi_isr_lock_level_t saveif;
11
12     if (!ifc)
13         return -1;
14
15     if (!(ifc->flags & SPI_IFC_ON))
16         return -1;
17
18     spi_isr_lock(saveif);
19     spi_rq_queue_insert(ifc, msg);
20     spi_isr_unlock(saveif);
21     ifc->ctrl_fnc(ifc, SPI_CTRL_WAKE_RQ, NULL);
22     return 0;
23 }
24
25 int spi_transfer_callback(struct spi_drv *ifc, int code, struct spi_msg_head *msg)
26 {
27     if (msg->private) {
28         msg->private = 0;
29     }
30     return 0;
31 }
32
33 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf)
34 {
35     spi_msg_head_t msg;
36
37     msg.flags = 0;
38     //msg.ifc = NULL;
39     spi_rq_queue_init_detached(&msg);
40     msg.addr = addr;
41     msg.rq_len = rq_len;
42     msg.tx_buf = tx_buf;
43     msg.rx_buf = rx_buf;
44     msg.callback = spi_transfer_callback;
45     msg.private = 1;
46
47     if (spi_msg_rq_ins(ifc, &msg) < 0)
48         return -1;
49
50     /* Wait for the request completion */
51     while (msg.private) {
52         __memory_barrier();
53     }
54
55
56     if (msg.flags & (SPI_MSG_FAIL | SPI_MSG_ABORT))
57         return -1;
58
59     return msg.rq_len;
60 }
61