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[] = {0xC000, 0x3000, 0xC00, 0x300, 0xC0000000, 0x30000000, 0xC000000, 0x3000000};
22 * @brief Set value 0 or 1 to Lout pin
23 * This function prepares command for spi, that sets value on 1 lout pin
25 * @param[in] pin number of the pin
26 * @param[in] val value to be set
27 * @return 0 when success, -1 when bad parameter
29 int lout_set_pin(uint32_t pin, int val) {
31 if (val == 0) new_val = LOUT_CODE0;
32 else if (val == 1) new_val = LOUT_CODE1;
34 uint32_t msk = lout_pin_msk[pin-1];
35 uint32_t old_val = __mfld2val(msk,lout_spi_cmd);
36 lout_spi_cmd ^= __val2mfld(msk, old_val); // Delete old unknown value
37 lout_spi_cmd |= __val2mfld(msk, new_val); // Insert new value
42 * @brief Get value from lout pin
43 * This function gets value of 1 lout pin. The value is read from the last spi command.
44 * @param[in] pin number of the pin
45 * @return 0 or 1 when succes, -1 when bad parameter
47 int lout_get_pin(uint32_t pin) {
48 unsigned int msk = lout_pin_msk[pin-1];
49 unsigned int val = __mfld2val(msk,lout_spi_cmd);
50 if (val == LOUT_CODE0) return 0;
51 if (val == LOUT_CODE1) return 1;
56 * @brief Set values on all pins of LOUT port
57 * This function prepares command for spi, that sets value on all lout pins.
59 * @param[in] word bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
61 void lout_set_word(uint8_t word) {
63 for (i = 0; i < 8; i++,word >>= 1) {
64 lout_set_pin(i+1, word&0x1);
69 * @brief Get values from all pins of LOUT port
70 * This function gets value from all lout pins. It reads the values from last spi command.
72 * @return bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
74 uint8_t lout_get_word() {
77 for (i = 0; i < 8; i++) {
78 word |= lout_get_pin(i+1) << i;
84 * Send prepared command to the spi, store response.
86 * @return spi response
88 int lout_spi_transfer() {
90 desc = port_get_dsc(PORT_NAME_LOUT, -1);
91 lout_spi_cmd_sh = lout_spi_cmd;
93 commands[0] = (lout_spi_cmd_sh & 0xFF000000) >> 24;
94 commands[1] = (lout_spi_cmd_sh & 0xFF0000) >> 16;
95 commands[2] = (lout_spi_cmd_sh & 0xFF00) >> 8;
96 commands[3] = (lout_spi_cmd_sh & 0xFF);
98 lout_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
103 * Returns actual spi command
104 * @return actual spi command
106 uint32_t lout_spi_get_cmd() {
111 * Returns last spi response
112 * @return last spi response
114 uint32_t lout_spi_get_response() {
115 return lout_spi_resp;