]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/lout.c
spi: Rework the SPI interface
[pes-rpp/rpp-lib.git] / rpp / src / rpp / lout.c
1 /* Copyright (C) 2013, 2015 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : lout.c
12  * Abstract:
13  *     Logic Output RPP API implementation file.
14  *
15  * References:
16  *     lout.h
17  *     RPP API documentation.
18  */
19
20
21 #include "rpp/rpp.h"
22 #include "rpp/mutex.h"
23
24 RPP_MUTEX_DEFINE(mutex_lout);
25
26 #ifndef FREERTOS_POSIX
27 #include "drv/lout.h"
28 #include "drv/spi_def.h"
29 #endif
30
31 static boolean_t initialized = FALSE;
32
33 int8_t rpp_lout_init()
34 {
35         if (initialized)
36                 return FAILURE;
37         if (!RPP_MUTEX_INIT(mutex_lout))
38                 return FAILURE;
39         initialized = TRUE;
40 #ifndef FREERTOS_POSIX
41         spi_init();
42 #endif
43
44         // FIXME: Implement.
45         return SUCCESS;
46 }
47
48
49 static uint8_t out_cache = 0x0;
50
51 int8_t rpp_lout_set(uint8_t pin, uint8_t val)
52 {
53         if ((pin < 1) || (pin > 8))
54                 return -1;
55
56         uint8_t index = pin - 1;
57         RPP_MUTEX_LOCK(mutex_lout);
58         if (val)
59                 bit_set(out_cache, index);
60         else
61                 bit_clear(out_cache, index);
62         RPP_MUTEX_UNLOCK(mutex_lout);
63         return SUCCESS;
64 }
65
66
67 static uint8_t diag_cache = 0x0;
68
69 int8_t rpp_lout_diag(uint8_t pin)
70 {
71         if ((pin < 1) || (pin > 8))
72                 return -1;
73
74         int8_t ret_val = LOW;
75
76         RPP_MUTEX_LOCK(mutex_lout);
77         if (is_bit_set(diag_cache, pin - 1))
78                 ret_val = HIGH;
79         RPP_MUTEX_UNLOCK(mutex_lout);
80         return ret_val;
81 }
82
83
84 int8_t rpp_lout_update()
85 {
86 #ifndef FREERTOS_POSIX
87         RPP_MUTEX_LOCK(mutex_lout);
88         // Update output values
89         lout_set_word(out_cache);
90         // FIXME: Check which SPI transfer statuses could be considered errors
91         lout_spi_transfer();
92
93         // Read back diagnostic values
94         // FIXME: Implement. Dummy assign for now.
95         diag_cache = out_cache;
96
97         if (diag_cache != out_cache) {
98                 RPP_MUTEX_UNLOCK(mutex_lout);
99                 return FAILURE;
100         }
101         RPP_MUTEX_UNLOCK(mutex_lout);
102 #endif
103
104         return SUCCESS;
105 }