]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - source/drv_lout.c
Source and Header files modified
[pes-rpp/rpp-test-sw.git] / source / drv_lout.c
1 /*
2  * lout_spi.c
3  *
4  *  Created on: 7.12.2012
5  *      Author: Michal Horn
6  *
7  *  This file contains functions to control LOUT port over SPI
8  */
9
10 #include "drv_lout.h"
11
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;
16 /** Spi response */
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};
20
21 /**
22  * @brief Set value 0 or 1 to Lout pin
23  * This function prepares command for spi, that sets value on 1 lout pin
24  *
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
28  */
29 int lout_set_pin(uint32_t pin, int val) {
30         int new_val;
31         if (val == 0) new_val = LOUT_CODE0;
32         else if (val == 1) new_val = LOUT_CODE1;
33         else return -1;
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
38         return 0;
39 }
40
41 /**
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
46  */
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;
52         else return -1;
53 }
54
55 /**
56  * @brief Set values on all pins of LOUT port
57  * This function prepares command for spi, that sets value on all lout pins.
58  *
59  * @param[in]   word    bits of the word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
60  */
61 void lout_set_word(uint8_t word) {
62         int i;
63         for (i = 0; i < 8; i++,word >>= 1) {
64                 lout_set_pin(i+1, word&0x1);
65         }
66 }
67
68 /**
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.
71  *
72  * @return      bits of the returned word are assigned to LOUT pins. 1st bit -> LOUT1, 2nd bit -> LOUT2 ...
73  */
74 uint8_t lout_get_word() {
75         uint8_t word = 0;
76         int i;
77         for (i = 0; i < 8; i++) {
78                 word |= lout_get_pin(i+1) << i;
79         }
80         return word;
81 }
82
83 /**
84  *      Send prepared command to the spi, store response.
85  *
86  *      @return spi response
87  */
88 int lout_spi_transfer() {
89         port_desc_t* desc;
90     desc = port_get_dsc(PORT_NAME_LOUT, -1);
91     lout_spi_cmd_sh = lout_spi_cmd;
92     uint32_t commands[4];
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);
97
98     lout_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
99         return lout_spi_resp;
100 }
101
102 /**
103  * Returns actual spi command
104  * @return actual spi command
105  */
106 uint32_t lout_spi_get_cmd() {
107         return lout_spi_cmd;
108 }
109
110 /**
111  * Returns last spi response
112  * @return last spi response
113  */
114 uint32_t lout_spi_get_response() {
115         return lout_spi_resp;
116 }