49 lines
1.5 KiB
C
49 lines
1.5 KiB
C
|
// This wants to be a "generic" library for ad9833, ad9850 and ad9851.
|
||
|
// currently, only ad9851 is supported
|
||
|
#include <stdint.h>
|
||
|
#include <libopencm3/stm32/gpio.h>
|
||
|
#include "lib/ad98xx.h"
|
||
|
#include "lib/gpio_utils.h"
|
||
|
|
||
|
|
||
|
/* ad98_init will initialize pins, not the whole port.
|
||
|
* That is, you'll need to
|
||
|
* rcc_periph_clock_enable(RCC_${dds.gpioport})
|
||
|
* */
|
||
|
void ad98_init(ad98_dds dds) {
|
||
|
gpio_set_mode(dds.gpioport, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
|
||
|
dds.reset | dds.clock | dds.load | dds.data);
|
||
|
gpio_pulseHigh(dds.gpioport, dds.reset);
|
||
|
gpio_pulseHigh(dds.gpioport, dds.clock);
|
||
|
gpio_pulseHigh(dds.gpioport, dds.load);
|
||
|
}
|
||
|
|
||
|
static void _send_byte(ad98_dds dds, unsigned char byte) {
|
||
|
for(int i=0; i<8; i++) { // every bit
|
||
|
char bit = ((byte >> i) & 1);
|
||
|
if(bit) {
|
||
|
gpio_set(dds.gpioport, dds.data);
|
||
|
} else {
|
||
|
gpio_clear(dds.gpioport, dds.data);
|
||
|
}
|
||
|
gpio_pulseHigh(dds.gpioport, dds.clock);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const float tunings[AD98_MODELS_NUM] = {AD98_AD9833_TUNING,
|
||
|
AD98_AD9850_TUNING,
|
||
|
AD98_AD9851_TUNING};
|
||
|
|
||
|
void ad98_set_frequency(ad98_dds dds, double frequency) { // requires dds_init to be called before it
|
||
|
// support AD98_MODEL_AD9850
|
||
|
uint32_t freqWord;
|
||
|
freqWord = frequency * tunings[dds.model];
|
||
|
for(char i=0; i<4; i++) {
|
||
|
_send_byte(dds, freqWord & 0xFF);
|
||
|
freqWord >>= 8;
|
||
|
}
|
||
|
_send_byte(dds, 0x01);
|
||
|
gpio_pulseHigh(dds.gpioport, dds.load);
|
||
|
}
|
||
|
|