30 #ifdef PIOS_INCLUDE_ADC
33 enum pios_adc_dev_magic {
34 PIOS_ADC_DEV_MAGIC = 0x58375169,
38 enum pios_adc_dev_magic
magic;
43 struct pios_adc_dev * pios_adc_dev;
46 static struct pios_adc_dev * PIOS_ADC_Allocate(
void);
47 static bool PIOS_ADC_validate(
struct pios_adc_dev *);
49 #ifndef PIOS_ADC_SUB_DRIVER_MAX_INSTANCES
50 #define PIOS_ADC_SUB_DRIVER_MAX_INSTANCES 2
54 static struct sub_device_list_ {
64 static bool PIOS_ADC_validate(
struct pios_adc_dev *dev)
69 return (dev->magic == PIOS_ADC_DEV_MAGIC);
77 static struct pios_adc_dev *PIOS_ADC_Allocate(
void)
79 struct pios_adc_dev *adc_dev;
85 adc_dev->magic = PIOS_ADC_DEV_MAGIC;
102 struct pios_adc_dev * adc_dev;
104 adc_dev = (
struct pios_adc_dev *) PIOS_ADC_Allocate();
109 adc_dev->driver = driver;
110 adc_dev->lower_id = lower_id;
111 *adc_id = (uintptr_t) adc_dev;
112 sub_device_list.sub_device_pointers[sub_device_list.number_of_devices] = adc_dev;
113 sub_device_list.number_of_devices++;
129 for (uint8_t x = 0; x < sub_device_list.number_of_devices; ++x) {
130 struct pios_adc_dev * adc_dev = sub_device_list.sub_device_pointers[x];
131 if (!PIOS_ADC_validate(adc_dev)) {
134 }
else if (adc_dev->driver->number_of_channels) {
135 uint32_t num_channels_for_this_device = adc_dev->driver->number_of_channels(adc_dev->lower_id);
136 if (adc_dev->driver->get_pin && (channel < offset + num_channels_for_this_device)) {
137 return (adc_dev->driver->get_pin)(adc_dev->lower_id, channel -
offset);
139 offset += num_channels_for_this_device;
157 for (uint8_t x = 0; x < sub_device_list.number_of_devices; ++x) {
158 struct pios_adc_dev * adc_dev = sub_device_list.sub_device_pointers[x];
159 if (!PIOS_ADC_validate(adc_dev)) {
162 }
else if (adc_dev->driver->number_of_channels) {
163 uint32_t num_channels_for_this_device = adc_dev->driver->number_of_channels(adc_dev->lower_id);
164 if (adc_dev->driver->get_pin && (channel < offset + num_channels_for_this_device)) {
165 return (
float)((adc_dev->driver->get_pin)(adc_dev->lower_id, channel - offset)) * (
float)(adc_dev->driver->lsb_voltage)(adc_dev->lower_id);
167 offset += num_channels_for_this_device;
Main PiOS header to include all the compiled in PiOS options.
#define PIOS_DEBUG_Assert(test)
void * PIOS_malloc_no_dma(size_t size)
uint16_t number_of_devices
float PIOS_ADC_GetChannelVolt(uint32_t channel)
int32_t PIOS_ADC_GetChannelRaw(uint32_t channel)
int32_t PIOS_ADC_Init(uintptr_t *adc_id, const struct pios_adc_driver *driver, uintptr_t lower_id)
#define PIOS_Assert(test)
#define PIOS_ADC_SUB_DRIVER_MAX_INSTANCES