4 * Created on: 12.2.2013
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,
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
22 /** Prepared command for SPI */
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;
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
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));
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
47 int dac_set_pin_on(uint8_t pin) {
48 if (pin > DAC_PIN_NUM) return -1;
49 dac_pin_stat[pin] |= 1 << 12;
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
58 int dac_set_pin_off(uint8_t pin) {
59 if (pin > DAC_PIN_NUM) return -1;
60 dac_pin_stat[pin] &= ~(1 << 12);
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
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;
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
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;
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
93 int dac_spi_transfer(uint8_t pin) {
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;
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);
105 return desc->port_setfnc_ptr(desc->config, desc->numValues, commands);