1 /* Copyright (C) 2012-2013 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"
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;
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]
40 // FIXME: Not sure if all those are required. Also not safe for multiple calls.
48 * @brief Set value 0 or 1 to Lout pin
49 * This function prepares command for spi, that sets value on 1 lout pin
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
55 int lout_set_pin(uint32_t pin, int val)
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
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
80 int lout_get_pin(uint32_t pin)
83 unsigned int msk = lout_pin_msk[pin - 1];
84 unsigned int val = __mfld2val(msk, lout_spi_cmd);
86 if (val == LOUT_CODE0)
88 if (val == LOUT_CODE1)
95 * @brief Set values on all pins of LOUT port
96 * This function prepares command for spi, that sets value on all lout pins.
98 * @param[in] word bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
100 void lout_set_word(uint8_t word)
104 for (i = 0; i < 8; i++,word >>= 1) {
105 lout_set_pin(i+1, word&0x1);
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.
113 * @return bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
115 uint8_t lout_get_word()
120 for (i = 0; i < 8; i++) {
121 word |= lout_get_pin(i+1) << i;
127 * Send prepared command to the spi, store response.
129 * @return spi response
131 int lout_spi_transfer()
134 dio_port_desc_t *desc;
136 desc = dio_port_get_dsc(DIO_PORT_NAME_LOUT, -1);
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);
145 lout_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
146 return lout_spi_resp;
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;