1 /* Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
4 * - Michal Horn <hornmich@fel.cvut.cz>
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.
14 * This file contains functions to control LOUT port over SPI
17 //#include "drv/lout.h"
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;
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]
41 // FIXME: Not sure if all those are required. Also not safe for multiple calls.
49 * @brief Set value 0 or 1 to Lout pin
50 * This function prepares command for spi, that sets value on 1 lout pin
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
56 int lout_set_pin(uint32_t pin, int val)
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
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
81 int lout_get_pin(uint32_t pin)
84 unsigned int msk = lout_pin_msk[pin - 1];
85 unsigned int val = __mfld2val(msk, lout_spi_cmd);
87 if (val == LOUT_CODE0)
89 if (val == LOUT_CODE1)
96 * @brief Set values on all pins of LOUT port
97 * This function prepares command for spi, that sets value on all lout pins.
99 * @param[in] word bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
101 void lout_set_word(uint8_t word)
105 for (i = 0; i < 8; i++,word >>= 1) {
106 lout_set_pin(i+1, word&0x1);
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.
114 * @return bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
116 uint8_t lout_get_word()
121 for (i = 0; i < 8; i++) {
122 word |= lout_get_pin(i+1) << i;
128 * Send prepared command to the spi, store response.
130 * @return spi response
132 int lout_spi_transfer()
135 const struct port_desc *port;
137 port = &port_desc[PORT_ID_LOUT];
139 lout_spi_cmd_sh = lout_spi_cmd;
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);
146 return lout_spi_resp = port->set(port, commands, sizeof(commands));
150 * Returns actual spi command
151 * @return actual spi command
153 uint32_t lout_spi_get_cmd()
159 * Returns last spi response
160 * @return last spi response
162 uint32_t lout_spi_get_response()
164 return lout_spi_resp;