]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/drv/lout.c
Uncrustify
[pes-rpp/rpp-lib.git] / rpp / src / drv / lout.c
1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Michal Horn <hornmich@fel.cvut.cz>
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  *
13  * Abstract:
14  *      This file contains functions to control LOUT port over SPI
15  */
16
17 //#include "drv/lout.h"
18 #include "drv/drv.h"
19 #include "drv/port.h"
20
21 /** Prepared spi command */
22 uint32_t lout_spi_cmd = LOUT_SPICMD_INIT_VAL;
23 /** Shadow variable used during spi sending */
24 uint32_t lout_spi_cmd_sh = LOUT_SPICMD_INIT_VAL;
25 /** Spi response */
26 uint32_t lout_spi_resp = 0;
27 /** Pin mask definitions, those masks are used by val2mfld and mfld2val macros to set and get values into commands */
28 static const uint32_t lout_pin_msk[]  = {
29         0xC000,     // B [0000 0000] [0000 0000] [1100 0000] [0000 0000]
30         0x3000,     // B [0000 0000] [0000 0000] [0011 0000] [0000 0000]
31         0xC00,      // B [0000 0000] [0000 0000] [0000 1100] [0000 0000]
32         0x300,      // B [0000 0000] [0000 0000] [0000 0011] [0000 0000]
33         0xC0000000, // B [1100 0000] [0000 0000] [0000 0000] [0000 0000]
34         0x30000000, // B [0011 0000] [0000 0000] [0000 0000] [0000 0000]
35         0xC000000,  // B [0000 1100] [0000 0000] [0000 0000] [0000 0000]
36         0x3000000   // B [0000 0011] [0000 0000] [0000 0000] [0000 0000]
37 };
38
39 void lout_init()
40 {
41         // FIXME: Not sure if all those are required. Also not safe for multiple calls.
42         dmmInit();
43         gioInit();
44         hetInit();
45         //spi_tms570_init();
46 }
47
48 /**
49  * @brief Set value 0 or 1 to Lout pin
50  * This function prepares command for spi, that sets value on 1 lout pin
51  *
52  * @param[in]   pin number of the pin
53  * @param[in]   val value to be set
54  * @return 0 when success, -1 when bad parameter
55  */
56 int lout_set_pin(uint32_t pin, int val)
57 {
58
59         int new_val;
60
61         if (val == 0)
62                 new_val = LOUT_CODE0;
63         else if (val == 1)
64                 new_val = LOUT_CODE1;
65         else
66                 return -1;
67
68         uint32_t msk = lout_pin_msk[pin - 1];
69         uint32_t old_val = __mfld2val(msk, lout_spi_cmd);
70         lout_spi_cmd ^= __val2mfld(msk, old_val);   // Delete old unknown value
71         lout_spi_cmd |= __val2mfld(msk, new_val);   // Insert new value
72         return 0;
73 }
74
75 /**
76  * @brief Get value from lout pin
77  * This function gets value of 1 lout pin. The value is read from the last spi command.
78  * @param[in]   pin number of the pin
79  * @return 0 or 1 when succes, -1 when bad parameter
80  */
81 int lout_get_pin(uint32_t pin)
82 {
83
84         unsigned int msk = lout_pin_msk[pin - 1];
85         unsigned int val = __mfld2val(msk, lout_spi_cmd);
86
87         if (val == LOUT_CODE0)
88                 return 0;
89         if (val == LOUT_CODE1)
90                 return 1;
91
92         return -1;
93 }
94
95 /**
96  * @brief Set values on all pins of LOUT port
97  * This function prepares command for spi, that sets value on all lout pins.
98  *
99  * @param[in]   word    bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
100  */
101 void lout_set_word(uint8_t word)
102 {
103         int i;
104
105         for (i = 0; i < 8; i++,word >>= 1) {
106                 lout_set_pin(i+1, word&0x1);
107         }
108 }
109
110 /**
111  * @brief Get values from all pins of LOUT port
112  * This function gets value from all lout pins. It reads the values from last spi command.
113  *
114  * @return  bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
115  */
116 uint8_t lout_get_word()
117 {
118         uint8_t word = 0;
119         int i;
120
121         for (i = 0; i < 8; i++) {
122                 word |= lout_get_pin(i+1) << i;
123         }
124         return word;
125 }
126
127 /**
128  *  Send prepared command to the spi, store response.
129  *
130  *  @return spi response
131  */
132 int lout_spi_transfer()
133 {
134
135         const struct port_desc *port;
136
137         port = &port_desc[PORT_ID_LOUT];
138
139         lout_spi_cmd_sh = lout_spi_cmd;
140         char commands[4];
141         commands[0] = (lout_spi_cmd_sh & 0xFF000000) >> 24;
142         commands[1] = (lout_spi_cmd_sh & 0xFF0000) >> 16;
143         commands[2] = (lout_spi_cmd_sh & 0xFF00) >> 8;
144         commands[3] = (lout_spi_cmd_sh & 0xFF);
145
146         return lout_spi_resp = port->set(port, commands, sizeof(commands));
147 }
148
149 /**
150  * Returns actual spi command
151  * @return actual spi command
152  */
153 uint32_t lout_spi_get_cmd()
154 {
155         return lout_spi_cmd;
156 }
157
158 /**
159  * Returns last spi response
160  * @return last spi response
161  */
162 uint32_t lout_spi_get_response()
163 {
164         return lout_spi_resp;
165 }