DAC

The DAC peripheral driver allows access to the MCU’s built-in DAC. More information about accessing peripheral IO is in the Device Interface section.

The following is an example of how to write the DAC in an OS environment:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include "mcu/mcu.h"

int write_dac(dac_sample_t * src, int samples){
     int fd;
     dac_attr_t ctl;

     fd = open("/dev/dac0", O_RDWR);
     if ( fd < 0 ){
          printf("Error opening peripheral (%d)\n", errno);
     } else {
     ctl.enabled_channels = (1<<0); //enable channel zero
          ctl.freq = 20000; //20KHz output
          ctl.pin_assign = 0; //Use GPIO configuration zero (see device specific documentation for details)
          if( ioctl(fd, I_SETATTR, &ctl) < 0 ){
               printf("Failed to set peripheral configuration (%d)\n", errno);
               return -1;
     }

     //now write the samples of the DAC
     lseek(fd, 0, SEEK_SET); //this sets the channel to 0, 'c' device does not auto-increment on write
     if ( write(fd, src, sizeof(dac_sample_t)*samples) < 0 ){
          printf("Error writing peripheral (%d)\n", errno);
          return -1;
     }
     }
     close(fd);
     return 0;
}

Details

define DAC_VERSION

define DAC_IOC_IDENT_CHAR

define I_DAC_GETVERSION

define I_DAC_GETINFO

This requests reads the DAC attributes.

Example:

#include <dev/adc.h>
dac_info_t info;
int fd;
...
ioctl(fd, I_DAC_GETINFO, &info);

define I_DAC_SETATTR

This requests writes the DAC attributes.

Example:

#include <dev/dac.h>
dac_attr_t attr;
int fd;
...
ioctl(fd, I_ADC_SETATTR, &attr);

define I_DAC_SETACTION

define I_DAC_GET

This IOCTL arg value causes the ioctl call to return the current output value of the DAC.

Example:

mcu_channel_t value;
value.channel = 0;
ioctl(dac_fd, I_DAC_GET, &value);

define I_DAC_SET

This IOCTL arg value causes the ioctl call to set the output of the DAC

Example:

mcu_channel_t value;
value.channel = 0;
value.value = DAC_MAX / 2;
ioctl(dac_fd, I_DAC_SET, &value);

define I_DAC_TOTAL

enum dac_flags
  • DAC_FLAG_NONE
  • DAC_FLAG_SET_CONVERTER
  • DAC_FLAG_IS_LEFT_JUSTIFIED
  • DAC_FLAG_IS_RIGHT_JUSTIFIED
  • DAC_FLAG_SET_CHANNELS
  • DAC_FLAG_IS_OUTPUT_BUFFERED
  • DAC_FLAG_IS_ON_CHIP
  • DAC_FLAG_IS_SAMPLE_AND_HOLD
  • DAC_FLAG_IS_TRIGGER_EINT
  • DAC_FLAG_IS_TRIGGER_EINT_RISING
  • DAC_FLAG_IS_TRIGGER_EINT_FALLING
  • DAC_FLAG_IS_TRIGGER_TMR

struct::dac_info_t

u32 o_flags

u32 o_events

u32 freq

u32 maximum

u32 reference_mv

u8 resolution

u8 bytes_per_sample

u16 resd0

u32 resd

struct::dac_pin_assignment_t

mcu_pin_t channel

struct::dac_attr_t

u32 o_flags

dac_pin_assignment_t pin_assignment

u32 freq

mcu_pin_t trigger

Pin or Timer trigger

u8 width

u8 channel

u32 resd

X

Thanks for Coming!

Subscribe to news and updates