Commit a1dfb4ef authored by Jacob Vosmaer's avatar Jacob Vosmaer

Put dac config in struct

parent f3104f3a
......@@ -35,13 +35,20 @@ uint16_t dac_pitch_delta(uint8_t semitones) {
return dac_semitone_offset(semitones);
}
uint8_t spi_io(uint8_t data) {
SPDR = data;
loop_until_bit_is_set(SPSR, SPIF);
return SPDR;
}
struct {
volatile uint8_t *port;
volatile uint8_t *ddr;
uint8_t bit;
} dac_sync = {.port = &PORTB, .ddr = &DDRB, .bit = PORTB1};
#define DAC_SYNC PORTB1
void dac_sync_enable(void) { *dac_sync.port &= ~_BV(dac_sync.bit); }
void dac_sync_disable(void) { *dac_sync.port |= _BV(dac_sync.bit); }
void dac_sync_init(void) {
*dac_sync.ddr |= _BV(dac_sync.bit);
dac_sync_disable();
_delay_ms(1); // let dac boot up
}
uint8_t dac_command_set(uint8_t v) {
struct dac *d = dac(v);
......@@ -52,12 +59,17 @@ uint8_t dac_command_set(uint8_t v) {
return 0b00110000 | d->channel;
}
void spi_send(uint8_t data) {
SPDR = data;
loop_until_bit_is_set(SPSR, SPIF);
}
void dac_send(uint8_t command, uint16_t data) {
PORTB &= ~_BV(DAC_SYNC);
spi_io(command);
spi_io(data >> 8);
spi_io(data & 0xff);
PORTB |= _BV(DAC_SYNC);
dac_sync_enable();
spi_send(command);
spi_send(data >> 8);
spi_send(data & 0xff);
dac_sync_disable();
}
void spi_init(void) {
......@@ -74,14 +86,8 @@ void spi_init(void) {
}
void dac_init(void) {
DDRB |= _BV(DAC_SYNC);
dac_sync_init();
spi_init();
// dac sync high for power saving
PORTB |= _BV(DAC_SYNC);
_delay_ms(1); // let dac boot up
}
uint16_t dac_note_pitch(uint8_t v, uint8_t note) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment