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