2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
ADS7844 register emulation
|
|
|
|
Code by Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _SITL_ADC_H
|
|
|
|
#define _SITL_ADC_H
|
|
|
|
|
2011-11-28 01:01:23 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-02-27 04:29:45 -04:00
|
|
|
#define NOISE_BITS 8
|
2012-02-18 02:45:56 -04:00
|
|
|
|
|
|
|
static inline float noise_generator(void)
|
|
|
|
{
|
2012-02-27 04:29:45 -04:00
|
|
|
extern bool sitl_motor_running;
|
|
|
|
if (!sitl_motor_running) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-18 02:45:56 -04:00
|
|
|
float noise = ((unsigned)random()) & ((1<<NOISE_BITS)-1);
|
|
|
|
noise -= 0.5*(1<<NOISE_BITS);
|
|
|
|
return noise;
|
|
|
|
}
|
|
|
|
|
2011-11-16 21:49:56 -04:00
|
|
|
// this implements the UDR2 register
|
|
|
|
struct ADC_UDR2 {
|
|
|
|
uint16_t value, next_value;
|
|
|
|
uint8_t idx;
|
2011-11-28 01:01:23 -04:00
|
|
|
float channels[8];
|
2011-11-16 21:49:56 -04:00
|
|
|
|
|
|
|
ADC_UDR2() {
|
|
|
|
// constructor
|
|
|
|
for (uint8_t i=0; i<8; i++) {
|
|
|
|
channels[i] = 0xFFFF;
|
|
|
|
}
|
|
|
|
value = next_value = 0;
|
|
|
|
idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
assignment of UDR2 selects which ADC channel
|
|
|
|
to output next
|
|
|
|
*/
|
|
|
|
ADC_UDR2& operator=(uint8_t cmd) {
|
2011-11-28 01:01:23 -04:00
|
|
|
float next_analog;
|
2011-11-16 21:49:56 -04:00
|
|
|
switch (cmd) {
|
2011-11-28 01:01:23 -04:00
|
|
|
case 0x87: next_analog = channels[0]; break;
|
|
|
|
case 0xC7: next_analog = channels[1]; break;
|
|
|
|
case 0x97: next_analog = channels[2]; break;
|
|
|
|
case 0xD7: next_analog = channels[3]; break;
|
|
|
|
case 0xA7: next_analog = channels[4]; break;
|
|
|
|
case 0xE7: next_analog = channels[5]; break;
|
|
|
|
case 0xB7: next_analog = channels[6]; break;
|
|
|
|
case 0xF7: next_analog = channels[7]; break;
|
|
|
|
case 0:
|
|
|
|
default: return *this;
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
2011-11-28 01:01:23 -04:00
|
|
|
idx = 1;
|
2012-02-18 02:45:56 -04:00
|
|
|
next_value = (unsigned)(next_analog + noise_generator() + 0.5);
|
2011-11-28 01:01:23 -04:00
|
|
|
if (next_value >= 0x1000) {
|
|
|
|
next_value = 0xFFF;
|
2011-11-25 00:48:52 -04:00
|
|
|
}
|
2011-11-28 01:01:23 -04:00
|
|
|
next_value = (next_value << 3);
|
2011-11-16 21:49:56 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read from UDR2 fetches a byte from the channel
|
|
|
|
*/
|
|
|
|
operator int() {
|
2011-11-25 00:48:52 -04:00
|
|
|
uint8_t ret;
|
|
|
|
if (idx & 1) {
|
|
|
|
ret = (value&0xFF);
|
|
|
|
value = next_value;
|
|
|
|
} else {
|
|
|
|
ret = (value>>8);
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
2011-11-25 00:48:52 -04:00
|
|
|
idx ^= 1;
|
|
|
|
return ret;
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
interface to set a channel value from SITL
|
|
|
|
*/
|
2011-11-28 01:01:23 -04:00
|
|
|
void set(uint8_t i, float v) {
|
2011-11-16 21:49:56 -04:00
|
|
|
channels[i] = v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _SITL_ADC_H
|