Skip to content

Commit

Permalink
samd: Change the init sequence for adc_timed() and dac_timed().
Browse files Browse the repository at this point in the history
To leave no half-initialized device if init fails.

Signed-off-by: robert-hh <robert@hammelrath.com>
  • Loading branch information
robert-hh committed Sep 22, 2024
1 parent caad75c commit 65dc19b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions ports/samd/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ static void machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t f
mp_get_buffer_raise(values, &src, MP_BUFFER_READ);
if (src.len >= 2) {
int freq = mp_obj_get_int(freq_in);
if (self->tc_index == -1) {
self->tc_index = allocate_tc_instance();
}
if (self->dma_channel == -1) {
self->dma_channel = allocate_dma_channel();
dma_init();
dma_register_irq(self->dma_channel, adc_irq_handler);
}
if (self->tc_index == -1) {
self->tc_index = allocate_tc_instance();
}
// Set the reference voltage. Default: external AREFA.
adc->REFCTRL.reg = adc_vref_table[self->vref];
// Set Input channel and resolution
Expand Down Expand Up @@ -330,6 +330,8 @@ static void machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t f
adc->SWTRIG.bit.START = 1;
while (adc->INTFLAG.bit.RESRDY == 0) {
}
// Wait a little bit allowing the ADC to settle.
mp_hal_delay_us(15);

dma_desc[self->dma_channel].BTCTRL.reg =
DMAC_BTCTRL_VALID | DMAC_BTCTRL_BLOCKACT_NOACT |
Expand Down
6 changes: 3 additions & 3 deletions ports/samd/machine_dac.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,14 @@ static mp_obj_t dac_write_timed(size_t n_args, const mp_obj_t *args) {
}
if (src.len >= 2) {
int freq = mp_obj_get_int(args[2]);
if (self->tc_index == -1) {
self->tc_index = allocate_tc_instance();
}
if (self->dma_channel == -1) {
self->dma_channel = allocate_dma_channel();
dma_init();
dma_register_irq(self->dma_channel, dac_irq_handler);
}
if (self->tc_index == -1) {
self->tc_index = allocate_tc_instance();
}
// Configure TC; no need to check the return value
configure_tc(self->tc_index, freq, 0);
self->busy = true;
Expand Down

0 comments on commit 65dc19b

Please sign in to comment.