4 * Created on: 7.12.2012
7 * This file contains functions to control LOUT port over SPI
10 //#include "drv/lout.h"
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;
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]
32 // FIXME: Not sure if all those are required. Also not safe for multiple calls.
40 * @brief Set value 0 or 1 to Lout pin
41 * This function prepares command for spi, that sets value on 1 lout pin
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
47 int lout_set_pin(uint32_t pin, int val) {
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
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
72 int lout_get_pin(uint32_t pin) {
74 unsigned int msk = lout_pin_msk[pin - 1];
75 unsigned int val = __mfld2val(msk, lout_spi_cmd);
77 if (val == LOUT_CODE0) {
80 if (val == LOUT_CODE1) {
88 * @brief Set values on all pins of LOUT port
89 * This function prepares command for spi, that sets value on all lout pins.
91 * @param[in] word bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
93 void lout_set_word(uint8_t word) {
95 for (i = 0; i < 8; i++,word >>= 1) {
96 lout_set_pin(i+1, word&0x1);
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.
104 * @return bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
106 uint8_t lout_get_word() {
109 for (i = 0; i < 8; i++) {
110 word |= lout_get_pin(i+1) << i;
116 * Send prepared command to the spi, store response.
118 * @return spi response
120 int lout_spi_transfer() {
123 desc = hal_port_get_dsc(PORT_NAME_LOUT, -1);
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);
132 lout_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
133 return lout_spi_resp;
137 * Returns actual spi command
138 * @return actual spi command
140 uint32_t lout_spi_get_cmd() {
145 * Returns last spi response
146 * @return last spi response
148 uint32_t lout_spi_get_response() {
149 return lout_spi_resp;