]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/drv/din.c
Merge branch 'master' of rtime.felk.cvut.cz:pes-rpp/rpp-lib
[pes-rpp/rpp-lib.git] / rpp / src / drv / din.c
1 /*
2  * din.c
3  *
4  *  Created on: 17.12.2012
5  *      Author: Michal Horn
6  *
7  *  Refactored by: Martin Koubek
8  *      martin.koubek@porsche-engineering.com
9  *      18.9.2013
10  *
11  *  This file is written for 33972 Multiple Switch
12  *  http://www.freescale.com/files/analog/doc/data_sheet/MC33972.pdf
13  *
14  *  This file contains functions to control DIN
15  *  Voltage on each pin can be set
16  *  switch to ground or to battery on programable pins can be set
17  *  interrupts on each pins can be disabled and enabled
18  */
19
20
21 /******************************************************************************
22 *   Include Files
23 ******************************************************************************/
24 #include "drv/drv.h"
25
26
27
28 /******************************************************************************
29 *   Static Variable Definitions
30 ******************************************************************************/
31 /** Stored response from SPI */
32 static uint32_t din_spi_resp = 0;
33
34 /** Store commands in shadow registers */
35 static uint16_t shadow_reg_list[DIN_NUM_SPI_CMD];
36
37
38 const static uint32_t dsc_pin_map[8U] = {
39     PIN_DSC_DIN8,
40     PIN_DSC_DIN9,
41     PIN_DSC_DIN10,
42     PIN_DSC_DIN11,
43     PIN_DSC_DIN12,
44     PIN_DSC_DIN13,
45     PIN_DSC_DIN14,
46     PIN_DSC_DIN15
47 };
48
49
50 /******************************************************************************
51 *   Function Prototypes
52 ******************************************************************************/
53 /**
54  * Switch copy command, prepared by other functions, to shadow variable,
55  * convert command to MSB,
56  * transfer command to DIN
57  * store spi response
58  * return spi response
59  */
60 int din_spi_transfer_mst(const uint32_t din_spi_cmd);
61
62 /******************************************************************************
63 *   Close variable declaration sections
64 ******************************************************************************/
65 /* Private defines */
66 /* --------------- */
67 /** Options: */
68 /**   Bit 13: Output Gain Selection bit set = 1x (VOUT = VREF * D/4096)   */
69 /**   Bit 15: DACA (0) or DACB (1) Selection bit.  */
70 #define DACA_INIT_VAL   (_BV(13) | _BV(12)          )
71 #define DACB_INIT_VAL   (_BV(13) | _BV(12) | _BV(15))
72
73 /**
74  * This find an index in register list
75  * @param command
76  * @return index
77  */
78 static uint32_t enum2cmd(const enum SpiCmdTable index)
79 {
80         if (index == DIN_RESET_CMD)
81                 return 0x007F0000;
82         else
83                 return index << 16;
84 }
85
86 /* Public functions */
87 /* ---------------- */
88
89 uint16_t din_set_reg(
90                 enum SpiCmdTable spi_cmd_index, uint16_t clear_mask, uint16_t xor_mask)
91 {
92         if (spi_cmd_index >= DIN_NUM_SPI_CMD) {
93                 return 0;
94         }
95
96         shadow_reg_list[spi_cmd_index] =
97                         shadow_reg_list[spi_cmd_index] & ~clear_mask ^ xor_mask;
98
99         uint32_t din_spi_cmd = enum2cmd(spi_cmd_index) | shadow_reg_list[spi_cmd_index];
100
101         return din_spi_transfer_mst(din_spi_cmd);
102 }
103
104 int8_t drv_din_ref(uint16_t ref_a, uint16_t ref_b)
105 {
106
107     uint16_t cmd;
108
109     // Get descriptor
110     uint32_t commands[2];
111     port_desc_t* desc = hal_port_get_dsc(PORT_NAME_DACDREF, -1);
112
113     // Send command for DAC A
114     cmd = DACA_INIT_VAL | (ref_a & 0x0FFF);
115
116     commands[0] = (cmd & 0xFF00) >> 8;
117     commands[1] = (cmd & 0xFF);
118     desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
119
120     // Send command for DAC B
121     cmd = DACB_INIT_VAL | (ref_b & 0x0FFF);
122
123     commands[0] = (cmd & 0xFF00) >> 8;
124     commands[1] = (cmd & 0xFF);
125     desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
126
127     // Fixme: check SPI return value.
128     return SUCCESS;
129 }
130
131
132 int8_t drv_din_get_varthr(uint8_t pin) {
133
134     // Check range
135     if((pin < 8) || (pin > 15)) {
136         return FAILURE;
137     }
138
139     return hal_gpio_pin_get_value(dsc_pin_map[pin - 8]);
140 }
141
142 uint16_t din_get_val_word()
143 {
144     // How it should be.
145     //uint16_t sp = ((din_spi_resp >> 14) & 0x00FF);
146     //uint16_t sg = ((din_spi_resp << 8 ) & 0xFF00);
147
148     // How it actually is.
149     // Ignore datasheet, this is the actual response from the SPI driver:
150     // [xxxx xxxx][SG7-SG0][SP1 SP0 yy yyyy][zz SP7-SP2]
151     //  x: Unknown.
152     //  y: Maybe SG13-SG8, but untested.
153     //  z: Maybe therm flag and int flag.
154     // For SP: First get SP7-SP2 right, then add SP1 and SP0
155     uint16_t sp = ((din_spi_resp << 2) & 0x00FF) | ((din_spi_resp >> 14) & 0x3);
156     uint16_t sg = ((din_spi_resp >> 8) & 0xFF00);
157     uint16_t word = sg | sp;
158     return word;
159 }
160
161
162 int din_spi_response() {
163     return din_spi_resp;
164 }
165
166
167 /* Private functions */
168 /* ----------------- */
169 /**
170  * Switch copy command, prepared by other functions, to shadow variable,
171  * convert command to MSB,
172  * transfer command to DIN
173  * store spi response
174  * return spi response
175  */
176 int din_spi_transfer_mst(const uint32_t din_spi_cmd) {
177     port_desc_t* desc;
178
179     desc = hal_port_get_dsc(PORT_NAME_DINSPI, -1);
180     uint32_t commands[3];
181     commands[0] = (din_spi_cmd & 0xFF0000) >> 16;       // command
182     commands[1] = (din_spi_cmd & 0xFF00) >> 8;          // 1.st B of data
183     commands[2] = (din_spi_cmd & 0xFF);                         // 2.nd B of data
184
185     din_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, \
186                 commands);
187     return din_spi_resp;
188 }