int8_t rpp_dac_init()
{
- if(initialized) {
- return FAILURE;
- }
- initialized = TRUE;
+ if (initialized)
+ return FAILURE;
+ initialized = TRUE;
#ifndef FREERTOS_POSIX
- spi_tms570_init();
+ spi_tms570_init();
#endif
- // Configure board
- // FIXME find out why board has default output of ~3.8V
- //rpp_dac_setup(1, FALSE);
- //rpp_dac_setup(2, FALSE);
- //rpp_dac_setup(3, FALSE);
- //rpp_dac_setup(4, FALSE);
-
- return SUCCESS;
+ // Configure board
+ // FIXME find out why board has default output of ~3.8V
+ //rpp_dac_setup(1, FALSE);
+ //rpp_dac_setup(2, FALSE);
+ //rpp_dac_setup(3, FALSE);
+ //rpp_dac_setup(4, FALSE);
+
+ return SUCCESS;
}
static boolean_t changed_st[4] = {
- FALSE, FALSE, FALSE, FALSE
- };
+ FALSE, FALSE, FALSE, FALSE
+};
static boolean_t enabled_cache[4] = {
- FALSE, FALSE, FALSE, FALSE
- };
+ FALSE, FALSE, FALSE, FALSE
+};
static uint16_t out_cache[4] = {
- 0, 0, 0, 0
- };
+ 0, 0, 0, 0
+};
int8_t rpp_dac_setup(uint8_t pin, boolean_t enabled)
{
- // Check range
- if((pin < 1) || (pin > 4)) {
- return -1;
- }
+ // Check range
+ if ((pin < 1) || (pin > 4))
+ return -1;
- uint8_t index = pin - 1;
+ uint8_t index = pin - 1;
- // Mark state
- enabled_cache[index] = enabled;
+ // Mark state
+ enabled_cache[index] = enabled;
- // Mark as changed
- changed_st[index] = TRUE;
+ // Mark as changed
+ changed_st[index] = TRUE;
- return SUCCESS;
+ return SUCCESS;
}
// Value that tops the DAC output
const static uint16_t dac_top =
- (uint16_t)(4095.0 * (12000.0 / 1000.0) / (RPP_DAC_OA * RPP_DAC_VREF));
+ (uint16_t)(4095.0 * (12000.0 / 1000.0) / (RPP_DAC_OA * RPP_DAC_VREF));
// Simple mapping function
static int32_t map(int32_t x,
- int32_t in_min, int32_t in_max,
- int32_t out_min, int32_t out_max)
+ int32_t in_min, int32_t in_max,
+ int32_t out_min, int32_t out_max)
{
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
int8_t rpp_dac_set(uint8_t pin, uint16_t val)
{
- // Check pin range
- if((pin < 1) || (pin > 4)) {
- return -1;
- }
+ // Check pin range
+ if ((pin < 1) || (pin > 4))
+ return -1;
- // Map value
- val = map(val, 0, 4095, 0, dac_top);
+ // Map value
+ val = map(val, 0, 4095, 0, dac_top);
- // Check value range
- if(val > 4095) {
- return -2;
- }
+ // Check value range
+ if (val > 4095)
+ return -2;
- uint8_t index = pin - 1;
+ uint8_t index = pin - 1;
- // Set value to output cache
- out_cache[index] = val;
+ // Set value to output cache
+ out_cache[index] = val;
- // Mark as changed
- changed_st[index] = TRUE;
+ // Mark as changed
+ changed_st[index] = TRUE;
- return SUCCESS;
+ return SUCCESS;
}
int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
{
- // Check pin range
- if((pin < 1) || (pin > 4)) {
- return -1;
- }
+ // Check pin range
+ if ((pin < 1) || (pin > 4))
+ return -1;
- // Check millivolts range
- if(mv > 12000) {
- return -2;
- }
+ // Check millivolts range
+ if (mv > 12000)
+ return -2;
- // Calculate millivolts -> value
- int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
- if(val > 4095) {
- val = 4095;
- }
+ // Calculate millivolts -> value
+ int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
+ if (val > 4095)
+ val = 4095;
- uint8_t index = pin - 1;
+ uint8_t index = pin - 1;
- // Set value to output cache
- out_cache[index] = val;
+ // Set value to output cache
+ out_cache[index] = val;
- // Mark as changed
- changed_st[index] = TRUE;
+ // Mark as changed
+ changed_st[index] = TRUE;
- return SUCCESS;
+ return SUCCESS;
}
int8_t rpp_dac_update()
{
- int i = 0;
- for(i = 0; i < 4; i++) {
+ int i = 0;
- // If changed commit changes to hardware
- if(changed_st[i]) {
+ for (i = 0; i < 4; i++) {
+
+ // If changed commit changes to hardware
+ if (changed_st[i]) {
#ifndef FREERTOS_POSIX
- // TODO: Confirm via returned SPI code that transfer was successful
- drv_dac_spi_transfer(i, enabled_cache[i], out_cache[i]);
+ // TODO: Confirm via returned SPI code that transfer was successful
+ drv_dac_spi_transfer(i, enabled_cache[i], out_cache[i]);
#else
- UNUSED(enabled_cache);
- UNUSED(out_cache);
+ UNUSED(enabled_cache);
+ UNUSED(out_cache);
#endif
- changed_st[i] = FALSE;
- }
- }
+ changed_st[i] = FALSE;
+ }
+ }
- return SUCCESS;
+ return SUCCESS;
}