4 * Created on: 7.12.2012
7 * This file contains functions to control LOUT port over SPI
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;
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]
31 // FIXME: Not sure if all those are required. Also not safe for multiple calls.
39 * @brief Set value 0 or 1 to Lout pin
40 * This function prepares command for spi, that sets value on 1 lout pin
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
46 int lout_set_pin(uint32_t pin, int val) {
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
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
71 int lout_get_pin(uint32_t pin) {
73 unsigned int msk = lout_pin_msk[pin - 1];
74 unsigned int val = __mfld2val(msk, lout_spi_cmd);
76 if (val == LOUT_CODE0) {
79 if (val == LOUT_CODE1) {
87 * @brief Set values on all pins of LOUT port
88 * This function prepares command for spi, that sets value on all lout pins.
90 * @param[in] word bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
92 void lout_set_word(uint8_t word) {
94 for (i = 0; i < 8; i++,word >>= 1) {
95 lout_set_pin(i+1, word&0x1);
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.
103 * @return bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
105 uint8_t lout_get_word() {
108 for (i = 0; i < 8; i++) {
109 word |= lout_get_pin(i+1) << i;
115 * Send prepared command to the spi, store response.
117 * @return spi response
119 int lout_spi_transfer() {
122 desc = hal_port_get_dsc(PORT_NAME_LOUT, -1);
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);
131 lout_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
132 return lout_spi_resp;
136 * Returns actual spi command
137 * @return actual spi command
139 uint32_t lout_spi_get_cmd() {
144 * Returns last spi response
145 * @return last spi response
147 uint32_t lout_spi_get_response() {
148 return lout_spi_resp;