Skip to content

Commit

Permalink
return values adjusted to common return values
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brummer committed Nov 20, 2014
1 parent ca6787e commit 6d785ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
41 changes: 27 additions & 14 deletions cpu/stm32f4/periph/dac.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,41 @@ int8_t dac_init(dac_t dev, dac_precision_t precision)
DAC_0_PORT->PUPDR &= ~(3 << (DAC_0_CH0_PIN * 2) | 3 << (DAC_0_CH1_PIN * 2));
break;
#endif
default:
/* Unknown Device */
return -1;
}

/* Select Shift value to normalize given Value */
switch(precision) {
case DAC_RES_6BIT:
dac_config[dev].shift_mod = 0x06; /* 2^6 << 6 = 2^12 */
break;
case DAC_RES_8BIT:
dac_config[dev].shift_mod = 0x04; /* 2^8 << 4 = 2^12 */
case DAC_RES_8BIT:
dac_config[dev].shift_mod = 0x04; /* 2^8 << 4 = 2^12 */
break;
case DAC_RES_10BIT:
case DAC_RES_10BIT:
dac_config[dev].shift_mod = 0x02; /* 2^10 << 2 = 2^12 */
break;
case DAC_RES_12BIT:
dac_config[dev].shift_mod = 0x00; /* 2^12 << 0 = 2^12 */
dac_config[dev].shift_mod = 0x00; /* 2^12 << 0 = 2^12 */
break;
/* Not Supported Resolutions */
case DAC_RES_14BIT:
case DAC_RES_16BIT:
default:
dac_poweroff(dev);
return -1;
return -2;
break;
}

/* Enable Channels, Clear Output */
/* Enable Channels, Clear Output */
dac->CR = 0;
dac->CR |= (DAC_CR_EN1 | DAC_CR_EN2);
dac->DHR12R1 = 0;
dac->DHR12R2 = 0;

return 0;
return 0;
}

int8_t dac_write(dac_t dev, uint8_t channel, uint16_t value)
Expand All @@ -92,9 +96,9 @@ int8_t dac_write(dac_t dev, uint8_t channel, uint16_t value)
dac = DAC_0_DEV;
val = value << dac_config[dev].shift_mod;

/* Check if given value is in Range. */
if( DAC_MAX_12BIT < val ){
return -1;
/* Value out of Range */
return -3;
}

switch(channel){
Expand All @@ -104,12 +108,15 @@ int8_t dac_write(dac_t dev, uint8_t channel, uint16_t value)
case 1:
dac->DHR12R2 = val;
break;
/* Unknown Channel */
/* Invalid Channel */
default:
return -2;
}
break;
#endif
/* Unknown Device */
default:
return -1;
}
return 0;
}
Expand All @@ -120,10 +127,13 @@ int8_t dac_poweron(dac_t dev)
#if DAC_0_EN
case DAC_0:
DAC_0_CLKEN();
return 0;
break;
#endif
default:
/* Unknown Device */
return -1;
}
return -1;
return 0;
}

int8_t dac_poweroff(dac_t dev)
Expand All @@ -132,10 +142,13 @@ int8_t dac_poweroff(dac_t dev)
#if DAC_0_EN
case DAC_0:
DAC_0_CLKDIS();
return 0;
break;
#endif
default:
/* Unknown Device */
return -1;
}
return -1;
return 0;
}

uint16_t dac_map(dac_t dev, int value, int min, int max)
Expand Down
10 changes: 6 additions & 4 deletions drivers/include/periph/dac.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ typedef enum {
* @param[in] precision the precision to use for conversion
*
* @return 0 on success
* @return -1 on precision not available
* @return -1 on unknown DAC Device
* @return -2 on precision not available
*/
int8_t dac_init(dac_t dev, dac_precision_t precision);

Expand All @@ -86,8 +87,9 @@ int8_t dac_init(dac_t dev, dac_precision_t precision);
* @param[in] value the value to write onto DAC.
*
* @return 0 on success
* @return -1 if value is out of range.
* @return -1 on unknown DAC Device
* @return -2 on invalid channel
* @return -3 if value is out of range.
*/
int8_t dac_write(dac_t dev, uint8_t channel, uint16_t value);

Expand Down Expand Up @@ -124,7 +126,7 @@ int8_t dac_poweroff(dac_t dev);
* @param[in] min the lower bound of the target interval
* @param[in] max the upper bound of the target interval
*
* @return the mapped value
* @return the mapped value, in valid DAC range
*/
uint16_t dac_map(dac_t dev, int value, int min, int max);

Expand All @@ -138,7 +140,7 @@ uint16_t dac_map(dac_t dev, int value, int min, int max);
* @param[in] min the lower bound of the target interval
* @param[in] max the upper bound of the target interval
*
* @return the mapped value
* @return the mapped value, in valid DAC range
*/
uint16_t dac_mapf(dac_t dev, float value, float min, float max);

Expand Down

0 comments on commit 6d785ed

Please sign in to comment.