]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - source/drv_dac.c
Source and Header files modified
[pes-rpp/rpp-test-sw.git] / source / drv_dac.c
1 /*
2  * dac_spi.c
3  *
4  *  Created on: 12.2.2013
5  *      Author: Michal Horn
6  */
7
8
9 #include "drv_dac.h"
10
11 /** Pin status for each DAC pin, the structure of each field is defined as spi command structure. */
12 uint16_t dac_pin_stat[DAC_PIN_NUM] = {  DAC1_INIT_VAL,
13                                                                                 DAC2_INIT_VAL,
14                                                                                 DAC3_INIT_VAL,
15                                                                                 DAC4_INIT_VAL
16                                                                          };
17
18 /** Port names for each DAC port, to be easily accessible by indexing */
19 const char* dac_port_names[DAC_PIN_NUM] = {     PORT_NAME_DAC1_2, PORT_NAME_DAC1_2,
20                                                                                         PORT_NAME_DAC3_4, PORT_NAME_DAC3_4
21                                                                                   };
22 /** Prepared command for SPI */
23 uint32_t dac_spi_cmd;
24 /** Shadow variable of spi command being sent */
25 uint32_t dac_spi_cmd_sh;
26 /** Response from SPI */
27 uint32_t dac_spi_response;
28
29 /**
30  *      @brief  Set voltage to the DA pin
31  *      @param[in] pin  Number of the pin to be set <0;3>
32  *      @param[in]      value   Voltae in mV to be set on specified pin
33  *      @return 0 when success, -1 if bad parameters
34  */
35 int dac_set_pin_voltage(uint8_t pin, uint32_t value) {
36         if (pin > DAC_PIN_NUM) return -1;
37         dac_pin_stat[pin] = dac_pin_stat[pin] & 0xF000;
38         dac_pin_stat[pin] |= (int)(4096*(value/(float)1000)/(DAC_OA_MULT*DAC_VREF));
39         return 0;
40 }
41
42 /**
43  * @brief       Enable DA pin
44  *      @param[in] pin  Number of the pin to be set <0;3>
45  *      @return 0 when success, -1 if bad parameters
46  */
47 int dac_set_pin_on(uint8_t pin) {
48         if (pin > DAC_PIN_NUM) return -1;
49         dac_pin_stat[pin] |= 1 << 12;
50         return 0;
51 }
52
53 /**
54  * @brief       Disable DA pin
55  *      @param[in] pin  Number of the pin to be set <0;3>
56  *      @return 0 when success, -1 if bad parameters
57  */
58 int dac_set_pin_off(uint8_t pin) {
59         if (pin > DAC_PIN_NUM) return -1;
60         dac_pin_stat[pin] &= ~(1 << 12);
61         return 0;
62 }
63
64 /**
65  * @brief       Get state of DA pin
66  *      @param[in] pin  Number of the pin to be set <0;3>
67  *      @return 1 when pin is enabled, 0 when pin is disabled, -1 if bad parameters
68  */
69 int dac_pin_is_on(uint8_t pin) {
70         if (pin > DAC_PIN_NUM) return -1;
71         if (dac_pin_stat[pin] & (1<<12)) return 1;
72         else return 0;
73 }
74
75 /**
76  * @brief       Get voltage on DA pin
77  * This function returns voltage setted on DA pin by dac_set_pin_voltage function. External changes are not watched.
78  *      @param[in] pin  Number of the pin to be set <0;3>
79  *      @return Voltage on specified pin in mV, -1 if bad parameters
80  */
81 int dac_get_pin_voltage(uint8_t pin) {
82         if (pin > DAC_PIN_NUM) return -1;
83         uint32_t voltage = dac_pin_stat[pin] & 0x0FFF;
84         return DAC_VREF*DAC_OA_MULT*voltage*1000/4096;
85 }
86
87 /**
88  * @brief       Send command on SPI
89  * This function translates DIN pin status into command and sends it.
90  *      @param[in] pin  Number of the pin to be set <0;3>
91  *      @return spi response or -1 if bad parameters
92  */
93 int dac_spi_transfer(uint8_t pin) {
94     uint32_t commands[2];
95         if (pin > DAC_PIN_NUM) return -1;
96         // Warning!!! Can be "optimized" by compiler
97         dac_spi_cmd = dac_pin_stat[pin];
98     dac_spi_cmd_sh = dac_spi_cmd;
99     //--
100         port_desc_t* desc;
101     desc = port_get_dsc(dac_port_names[pin], -1);
102     commands[0] = (dac_spi_cmd_sh & 0xFF00) >> 8;
103     commands[1] = (dac_spi_cmd_sh & 0xFF);
104
105     return desc->port_setfnc_ptr(desc->config, desc->numValues, commands);
106 }
107