]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/drv/spi.c
Improve baudrate testing
[pes-rpp/rpp-lib.git] / rpp / src / drv / spi.c
1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
2  *
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.
7  *
8  * File : spi.c
9  */
10
11 //#include "ul/ul_list.h"
12 //#include "drv/spi.h"
13 //#include "cpu_def.h"
14 //#include "ul/ul_list.h"
15 #include "drv/spi.h"
16
17 int spi_msg_rq_ins(spi_drv_t *ifc, spi_msg_head_t *msg)
18 {
19         spi_isr_lock_level_t saveif;
20
21         if (!ifc)
22                 return -1;
23
24         if (!(ifc->flags & SPI_IFC_ON))
25                 return -1;
26
27         spi_isr_lock(saveif);
28         spi_rq_queue_insert(ifc, msg);
29         spi_isr_unlock(saveif);
30         ifc->ctrl_fnc(ifc, SPI_CTRL_WAKE_RQ, NULL);
31         return 0;
32 }
33
34 int spi_transfer_callback(struct spi_drv *ifc, int code, struct spi_msg_head *msg)
35 {
36         if (msg->private)
37                 msg->private = 0;
38         return 0;
39 }
40
41 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf)
42 {
43         spi_msg_head_t msg;
44
45         msg.flags = 0;
46         //msg.ifc = NULL;
47         spi_rq_queue_init_detached(&msg);
48         msg.addr = addr;
49         msg.rq_len = rq_len;
50         msg.tx_buf = tx_buf;
51         msg.rx_buf = rx_buf;
52         msg.callback = spi_transfer_callback;
53         msg.private = 1;
54
55         if (spi_msg_rq_ins(ifc, &msg) < 0)
56                 return -1;
57
58         /* Wait for the request completion */
59         while (msg.private)
60                 __memory_barrier();
61
62
63         if (msg.flags & (SPI_MSG_FAIL | SPI_MSG_ABORT))
64                 return -1;
65
66         return msg.rq_len;
67 }
68
69 #define MIN(a,b) ((a) < (b) ? (a) : (b))
70
71 int8_t port_spi_set(const struct port_desc *port, void *values, size_t size)
72 {
73         spi_drv_t *ifc;
74         uint8_t rx[4];
75
76         assert(port->numchn == 1);
77         assert(size == port->bpch/8);
78         assert(size <= sizeof(rx));
79
80         ifc = spi_find_drv(NULL, port->cfg.spi.ifc);
81         if (ifc == NULL)
82                 return FAILURE;
83
84         if (!(ifc->flags & SPI_IFC_ON))
85                 return FAILURE;
86
87         spi_transfer(ifc, port->cfg.spi.cs, size, values, rx);
88
89         memcpy(values, rx, MIN(size, port->numchn * port->bpch/8));
90         return SUCCESS;
91 }