4 * Created on: 17.12.2012
7 * Refactored by: Martin Koubek
8 * martin.koubek@porsche-engineering.com
11 * This file is written for 33972 Multiple Switch
12 * http://www.freescale.com/files/analog/doc/data_sheet/MC33972.pdf
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
21 /******************************************************************************
23 ******************************************************************************/
28 /******************************************************************************
29 * Static Variable Definitions
30 ******************************************************************************/
31 /** Stored response from SPI */
32 static uint32_t din_spi_resp = 0;
34 /** Store commands in shadow registers */
35 static uint16_t shadow_reg_list[DIN_NUM_SPI_CMD];
38 const static uint32_t dsc_pin_map[8U] = {
50 /******************************************************************************
52 ******************************************************************************/
54 * Switch copy command, prepared by other functions, to shadow variable,
55 * convert command to MSB,
56 * transfer command to DIN
60 int din_spi_transfer_mst(const uint32_t din_spi_cmd);
62 /******************************************************************************
63 * Close variable declaration sections
64 ******************************************************************************/
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))
74 * This find an index in register list
78 static uint32_t enum2cmd(const enum SpiCmdTable index)
80 if (index == DIN_RESET_CMD)
86 /* Public functions */
87 /* ---------------- */
90 enum SpiCmdTable spi_cmd_index, uint16_t clear_mask, uint16_t xor_mask)
92 if (spi_cmd_index >= DIN_NUM_SPI_CMD) {
96 shadow_reg_list[spi_cmd_index] =
97 shadow_reg_list[spi_cmd_index] & ~clear_mask ^ xor_mask;
99 uint32_t din_spi_cmd = enum2cmd(spi_cmd_index) | shadow_reg_list[spi_cmd_index];
101 return din_spi_transfer_mst(din_spi_cmd);
104 int8_t drv_din_ref(uint16_t ref_a, uint16_t ref_b)
110 uint32_t commands[2];
111 port_desc_t* desc = hal_port_get_dsc(PORT_NAME_DACDREF, -1);
113 // Send command for DAC A
114 cmd = DACA_INIT_VAL | (ref_a & 0x0FFF);
116 commands[0] = (cmd & 0xFF00) >> 8;
117 commands[1] = (cmd & 0xFF);
118 desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
120 // Send command for DAC B
121 cmd = DACB_INIT_VAL | (ref_b & 0x0FFF);
123 commands[0] = (cmd & 0xFF00) >> 8;
124 commands[1] = (cmd & 0xFF);
125 desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
127 // Fixme: check SPI return value.
132 int8_t drv_din_get_varthr(uint8_t pin) {
135 if((pin < 8) || (pin > 15)) {
139 return hal_gpio_pin_get_value(dsc_pin_map[pin - 8]);
142 uint16_t din_get_val_word()
145 //uint16_t sp = ((din_spi_resp >> 14) & 0x00FF);
146 //uint16_t sg = ((din_spi_resp << 8 ) & 0xFF00);
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]
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;
162 int din_spi_response() {
167 /* Private functions */
168 /* ----------------- */
170 * Switch copy command, prepared by other functions, to shadow variable,
171 * convert command to MSB,
172 * transfer command to DIN
174 * return spi response
176 int din_spi_transfer_mst(const uint32_t din_spi_cmd) {
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
185 din_spi_resp = desc->port_setfnc_ptr(desc->config, desc->numValues, \