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