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