dac.c 17.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdio.h>
Damien George's avatar
Damien George committed
28
29
30
#include <stdint.h>
#include <string.h>

31
#include STM32_HAL_H
Damien George's avatar
Damien George committed
32

33
34
#include "py/nlr.h"
#include "py/runtime.h"
35
#include "timer.h"
Damien George's avatar
Damien George committed
36
#include "dac.h"
37
#include "dma.h"
38
39
#include "pin.h"
#include "genhdr/pins.h"
Damien George's avatar
Damien George committed
40

41
42
43
/// \moduleref pyb
/// \class DAC - digital to analog conversion
///
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// The DAC is used to output analog values (a specific voltage) on pin X5 or pin X6.
/// The voltage will be between 0 and 3.3V.
///
/// *This module will undergo changes to the API.*
///
/// Example usage:
///
///     from pyb import DAC
///
///     dac = DAC(1)            # create DAC 1 on pin X5
///     dac.write(128)          # write a value to the DAC (makes X5 1.65V)
///
/// To output a continuous sine-wave:
///
///     import math
///     from pyb import DAC
///
///     # create a buffer containing a sine-wave
///     buf = bytearray(100)
///     for i in range(len(buf)):
64
///         buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
65
66
67
68
///
///     # output the sine-wave at 400Hz
///     dac = DAC(1)
///     dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
69

70
#if defined(MICROPY_HW_ENABLE_DAC) && MICROPY_HW_ENABLE_DAC
71

Damien George's avatar
Damien George committed
72
73
74
STATIC DAC_HandleTypeDef DAC_Handle;

void dac_init(void) {
75
    memset(&DAC_Handle, 0, sizeof DAC_Handle);
Damien George's avatar
Damien George committed
76
77
78
79
80
    DAC_Handle.Instance = DAC;
    DAC_Handle.State = HAL_DAC_STATE_RESET;
    HAL_DAC_Init(&DAC_Handle);
}

81
#if defined(TIM6)
Damien George's avatar
Damien George committed
82
STATIC void TIM6_Config(uint freq) {
83
    // Init TIM6 at the required frequency (in Hz)
84
    TIM_HandleTypeDef *tim = timer_tim6_init(freq);
Damien George's avatar
Damien George committed
85
86
87
88
89

    // TIM6 TRGO selection
    TIM_MasterConfigTypeDef config;
    config.MasterOutputTrigger = TIM_TRGO_UPDATE;
    config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
90
    HAL_TIMEx_MasterConfigSynchronization(tim, &config);
Damien George's avatar
Damien George committed
91
92

    // TIM6 start counter
93
    HAL_TIM_Base_Start(tim);
Damien George's avatar
Damien George committed
94
}
95
#endif
Damien George's avatar
Damien George committed
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
STATIC uint32_t TIMx_Config(mp_obj_t timer) {
    // TRGO selection to trigger DAC
    TIM_HandleTypeDef *tim = pyb_timer_get_handle(timer);
    TIM_MasterConfigTypeDef config;
    config.MasterOutputTrigger = TIM_TRGO_UPDATE;
    config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    HAL_TIMEx_MasterConfigSynchronization(tim, &config);

    // work out the trigger channel (only certain ones are supported)
    if (tim->Instance == TIM2) {
        return DAC_TRIGGER_T2_TRGO;
    } else if (tim->Instance == TIM4) {
        return DAC_TRIGGER_T4_TRGO;
    } else if (tim->Instance == TIM5) {
        return DAC_TRIGGER_T5_TRGO;
    #if defined(TIM6)
    } else if (tim->Instance == TIM6) {
        return DAC_TRIGGER_T6_TRGO;
    #endif
    #if defined(TIM7)
    } else if (tim->Instance == TIM7) {
        return DAC_TRIGGER_T7_TRGO;
    #endif
    #if defined(TIM8)
    } else if (tim->Instance == TIM8) {
        return DAC_TRIGGER_T8_TRGO;
    #endif
    } else {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Timer does not support DAC triggering"));
    }
}

Damien George's avatar
Damien George committed
129
130
131
/******************************************************************************/
// Micro Python bindings

132
133
134
135
typedef enum {
    DAC_STATE_RESET,
    DAC_STATE_WRITE_SINGLE,
    DAC_STATE_BUILTIN_WAVEFORM,
136
    DAC_STATE_DMA_WAVEFORM, // should be last enum since we use space beyond it
137
138
} pyb_dac_state_t;

Damien George's avatar
Damien George committed
139
140
141
typedef struct _pyb_dac_obj_t {
    mp_obj_base_t base;
    uint32_t dac_channel; // DAC_CHANNEL_1 or DAC_CHANNEL_2
142
    const dma_descr_t *tx_dma_descr;
143
144
145
    uint16_t pin; // GPIO_PIN_4 or GPIO_PIN_5
    uint8_t bits; // 8 or 12
    uint8_t state;
Damien George's avatar
Damien George committed
146
147
} pyb_dac_obj_t;

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
STATIC mp_obj_t pyb_dac_init_helper(pyb_dac_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} },
    };

    // parse args
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    // GPIO configuration
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = self->pin;
    GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

    // DAC peripheral clock
165
    #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7)
166
    __DAC_CLK_ENABLE();
167
168
169
170
171
    #elif defined(MCU_SERIES_L4)
    __HAL_RCC_DAC1_CLK_ENABLE();
    #else
    #error Unsupported Processor
    #endif
172
173

    // stop anything already going on
174
175
176
177
178
179
180
181
    __DMA1_CLK_ENABLE();
    DMA_HandleTypeDef DMA_Handle;
    /* Get currently configured dma */
    dma_init_handle(&DMA_Handle, self->tx_dma_descr, (void*)NULL);
    // Need to deinit DMA first
    DMA_Handle.State = HAL_DMA_STATE_READY;
    HAL_DMA_DeInit(&DMA_Handle);

182
183
184
185
186
187
188
189
190
191
    HAL_DAC_Stop(&DAC_Handle, self->dac_channel);
    if ((self->dac_channel == DAC_CHANNEL_1 && DAC_Handle.DMA_Handle1 != NULL)
            || (self->dac_channel == DAC_CHANNEL_2 && DAC_Handle.DMA_Handle2 != NULL)) {
        HAL_DAC_Stop_DMA(&DAC_Handle, self->dac_channel);
    }

    // set bit resolution
    if (args[0].u_int == 8 || args[0].u_int == 12) {
        self->bits = args[0].u_int;
    } else {
192
        mp_raise_ValueError("unsupported bits");
193
194
195
196
197
198
199
200
    }

    // reset state of DAC
    self->state = DAC_STATE_RESET;

    return mp_const_none;
}

Damien George's avatar
Damien George committed
201
202
203
// create the dac object
// currently support either DAC1 on X5 (id = 1) or DAC2 on X6 (id = 2)

204
/// \classmethod \constructor(port)
205
206
/// Construct a new DAC object.
///
207
208
/// `port` can be a pin object, or an integer (1 or 2).
/// DAC(1) is on pin X5 and DAC(2) is on pin X6.
209
STATIC mp_obj_t pyb_dac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George's avatar
Damien George committed
210
    // check arguments
211
    mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
Damien George's avatar
Damien George committed
212

213
214
215
216
217
218
219
220
221
222
223
    // get pin/channel to output on
    mp_int_t dac_id;
    if (MP_OBJ_IS_INT(args[0])) {
        dac_id = mp_obj_get_int(args[0]);
    } else {
        const pin_obj_t *pin = pin_find(args[0]);
        if (pin == &pin_A4) {
            dac_id = 1;
        } else if (pin == &pin_A5) {
            dac_id = 2;
        } else {
224
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin(%q) doesn't have DAC capabilities", pin->name));
225
226
227
        }
    }

228
229
230
    pyb_dac_obj_t *dac = m_new_obj(pyb_dac_obj_t);
    dac->base.type = &pyb_dac_type;

Damien George's avatar
Damien George committed
231
    if (dac_id == 1) {
232
        dac->pin = GPIO_PIN_4;
233
        dac->dac_channel = DAC_CHANNEL_1;
234
        dac->tx_dma_descr = &dma_DAC_1_TX;
Damien George's avatar
Damien George committed
235
    } else if (dac_id == 2) {
236
        dac->pin = GPIO_PIN_5;
237
        dac->dac_channel = DAC_CHANNEL_2;
238
        dac->tx_dma_descr = &dma_DAC_2_TX;
Damien George's avatar
Damien George committed
239
    } else {
240
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "DAC(%d) doesn't exist", dac_id));
Damien George's avatar
Damien George committed
241
242
    }

243
244
245
246
    // configure the peripheral
    mp_map_t kw_args;
    mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
    pyb_dac_init_helper(dac, n_args - 1, args + 1, &kw_args);
Damien George's avatar
Damien George committed
247
248

    // return object
249
    return dac;
Damien George's avatar
Damien George committed
250
251
}

252
253
254
255
256
STATIC mp_obj_t pyb_dac_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    return pyb_dac_init_helper(args[0], n_args - 1, args + 1, kw_args);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_dac_init_obj, 1, pyb_dac_init);

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/// \method deinit()
/// Turn off the DAC, enable other use of pin.
STATIC mp_obj_t pyb_dac_deinit(mp_obj_t self_in) {
    pyb_dac_obj_t *self = self_in;
    if (self->dac_channel == DAC_CHANNEL_1) {
        DAC_Handle.Instance->CR &= ~DAC_CR_EN1;
        DAC_Handle.Instance->CR |= DAC_CR_BOFF1;
    } else {
        DAC_Handle.Instance->CR &= ~DAC_CR_EN2;
        DAC_Handle.Instance->CR |= DAC_CR_BOFF2;
    }
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_dac_deinit_obj, pyb_dac_deinit);

272
#if defined(TIM6)
273
274
275
/// \method noise(freq)
/// Generate a pseudo-random noise signal.  A new random sample is written
/// to the DAC output at the given frequency.
Damien George's avatar
Damien George committed
276
277
278
279
280
281
STATIC mp_obj_t pyb_dac_noise(mp_obj_t self_in, mp_obj_t freq) {
    pyb_dac_obj_t *self = self_in;

    // set TIM6 to trigger the DAC at the given frequency
    TIM6_Config(mp_obj_get_int(freq));

282
    if (self->state != DAC_STATE_BUILTIN_WAVEFORM) {
Damien George's avatar
Damien George committed
283
284
285
286
287
        // configure DAC to trigger via TIM6
        DAC_ChannelConfTypeDef config;
        config.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
        config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
        HAL_DAC_ConfigChannel(&DAC_Handle, &config, self->dac_channel);
288
        self->state = DAC_STATE_BUILTIN_WAVEFORM;
Damien George's avatar
Damien George committed
289
290
291
292
293
294
295
296
297
298
    }

    // set noise wave generation
    HAL_DACEx_NoiseWaveGenerate(&DAC_Handle, self->dac_channel, DAC_LFSRUNMASK_BITS10_0);
    HAL_DAC_SetValue(&DAC_Handle, self->dac_channel, DAC_ALIGN_12B_L, 0x7ff0);
    HAL_DAC_Start(&DAC_Handle, self->dac_channel);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_noise_obj, pyb_dac_noise);
299
#endif
Damien George's avatar
Damien George committed
300

301
#if defined(TIM6)
302
303
304
305
/// \method triangle(freq)
/// Generate a triangle wave.  The value on the DAC output changes at
/// the given frequency, and the frequence of the repeating triangle wave
/// itself is 256 (or 1024, need to check) times smaller.
Damien George's avatar
Damien George committed
306
307
308
309
310
311
STATIC mp_obj_t pyb_dac_triangle(mp_obj_t self_in, mp_obj_t freq) {
    pyb_dac_obj_t *self = self_in;

    // set TIM6 to trigger the DAC at the given frequency
    TIM6_Config(mp_obj_get_int(freq));

312
    if (self->state != DAC_STATE_BUILTIN_WAVEFORM) {
Damien George's avatar
Damien George committed
313
314
315
316
317
        // configure DAC to trigger via TIM6
        DAC_ChannelConfTypeDef config;
        config.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
        config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
        HAL_DAC_ConfigChannel(&DAC_Handle, &config, self->dac_channel);
318
        self->state = DAC_STATE_BUILTIN_WAVEFORM;
Damien George's avatar
Damien George committed
319
320
321
322
323
324
325
326
327
328
    }

    // set triangle wave generation
    HAL_DACEx_TriangleWaveGenerate(&DAC_Handle, self->dac_channel, DAC_TRIANGLEAMPLITUDE_1023);
    HAL_DAC_SetValue(&DAC_Handle, self->dac_channel, DAC_ALIGN_12B_R, 0x100);
    HAL_DAC_Start(&DAC_Handle, self->dac_channel);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_triangle_obj, pyb_dac_triangle);
329
#endif
Damien George's avatar
Damien George committed
330

331
332
/// \method write(value)
/// Direct access to the DAC output (8 bit only at the moment).
Damien George's avatar
Damien George committed
333
334
335
STATIC mp_obj_t pyb_dac_write(mp_obj_t self_in, mp_obj_t val) {
    pyb_dac_obj_t *self = self_in;

336
    if (self->state != DAC_STATE_WRITE_SINGLE) {
Damien George's avatar
Damien George committed
337
338
339
340
        DAC_ChannelConfTypeDef config;
        config.DAC_Trigger = DAC_TRIGGER_NONE;
        config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
        HAL_DAC_ConfigChannel(&DAC_Handle, &config, self->dac_channel);
341
        self->state = DAC_STATE_WRITE_SINGLE;
Damien George's avatar
Damien George committed
342
343
    }

344
345
346
347
348
    // DAC output is always 12-bit at the hardware level, and we provide support
    // for multiple bit "resolutions" simply by shifting the input value.
    HAL_DAC_SetValue(&DAC_Handle, self->dac_channel, DAC_ALIGN_12B_R,
        mp_obj_get_int(val) << (12 - self->bits));

Damien George's avatar
Damien George committed
349
350
351
352
353
354
    HAL_DAC_Start(&DAC_Handle, self->dac_channel);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_write_obj, pyb_dac_write);

355
#if defined(TIM6)
356
357
358
359
/// \method write_timed(data, freq, *, mode=DAC.NORMAL)
/// Initiates a burst of RAM to DAC using a DMA transfer.
/// The input data is treated as an array of bytes (8 bit data).
///
360
361
362
363
364
/// `freq` can be an integer specifying the frequency to write the DAC
/// samples at, using Timer(6).  Or it can be an already-initialised
/// Timer object which is used to trigger the DAC sample.  Valid timers
/// are 2, 4, 5, 6, 7 and 8.
///
365
366
/// `mode` can be `DAC.NORMAL` or `DAC.CIRCULAR`.
///
367
368
// TODO add callback argument, to call when transfer is finished
// TODO add double buffer argument
369
370
371
372
373
374
//
// TODO reconsider API, eg: write_trig(data, *, trig=None, loop=False)
// Then trigger can be timer (preinitialised with desired freq) or pin (extint9),
// and we can reuse the same timer for both DACs (and maybe also ADC) without
// setting the freq twice.
// Can still do 1-liner: dac.write_trig(buf, trig=Timer(6, freq=100), loop=True)
375
376
377
mp_obj_t pyb_dac_write_timed(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
378
        { MP_QSTR_freq, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
379
380
        { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DMA_NORMAL} },
    };
Damien George's avatar
Damien George committed
381

382
    // parse args
383
384
385
    pyb_dac_obj_t *self = pos_args[0];
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
Damien George's avatar
Damien George committed
386

387
    // get the data to write
388
    mp_buffer_info_t bufinfo;
389
    mp_get_buffer_raise(args[0].u_obj, &bufinfo, MP_BUFFER_READ);
390

391
392
393
394
395
396
397
398
399
    uint32_t dac_trigger;
    if (mp_obj_is_integer(args[1].u_obj)) {
        // set TIM6 to trigger the DAC at the given frequency
        TIM6_Config(mp_obj_get_int(args[1].u_obj));
        dac_trigger = DAC_TRIGGER_T6_TRGO;
    } else {
        // set the supplied timer to trigger the DAC (timer should be initialised)
        dac_trigger = TIMx_Config(args[1].u_obj);
    }
Damien George's avatar
Damien George committed
400
401
402

    __DMA1_CLK_ENABLE();

403
404
405
    DMA_HandleTypeDef DMA_Handle;
    /* Get currently configured dma */
    dma_init_handle(&DMA_Handle, self->tx_dma_descr, (void*)NULL);
Damien George's avatar
Damien George committed
406
    /*
407
408
    DMA_Cmd(DMA_Handle->Instance, DISABLE);
    while (DMA_GetCmdStatus(DMA_Handle->Instance) != DISABLE) {
Damien George's avatar
Damien George committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
    }

    DAC_Cmd(self->dac_channel, DISABLE);
    */

    /*
    // DAC channel configuration
    DAC_InitTypeDef DAC_InitStructure;
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_T7_TRGO;
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1; // unused, but need to set it to a valid value
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
    DAC_Init(self->dac_channel, &DAC_InitStructure);
    */

424
425
426
427
    // Need to deinit DMA first
    DMA_Handle.State = HAL_DMA_STATE_READY;
    HAL_DMA_DeInit(&DMA_Handle);

428
429
430
431
432
433
434
    if (self->bits == 8) {
        DMA_Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        DMA_Handle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    } else {
        DMA_Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
        DMA_Handle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    }
435
    DMA_Handle.Init.Mode = args[2].u_int;
Damien George's avatar
Damien George committed
436
437
    HAL_DMA_Init(&DMA_Handle);

438
439
440
441
442
    if (self->dac_channel == DAC_CHANNEL_1) {
        __HAL_LINKDMA(&DAC_Handle, DMA_Handle1, DMA_Handle);
    } else {
        __HAL_LINKDMA(&DAC_Handle, DMA_Handle2, DMA_Handle);
    }
Damien George's avatar
Damien George committed
443

444
445
446
447
    DAC_Handle.Instance = DAC;
    DAC_Handle.State = HAL_DAC_STATE_RESET;
    HAL_DAC_Init(&DAC_Handle);

448
    if (self->state != DAC_STATE_DMA_WAVEFORM + dac_trigger) {
449
        DAC_ChannelConfTypeDef config;
450
        config.DAC_Trigger = dac_trigger;
451
452
        config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
        HAL_DAC_ConfigChannel(&DAC_Handle, &config, self->dac_channel);
453
        self->state = DAC_STATE_DMA_WAVEFORM + dac_trigger;
454
455
    }

456
457
458
459
460
461
462
    if (self->bits == 8) {
        HAL_DAC_Start_DMA(&DAC_Handle, self->dac_channel,
            (uint32_t*)bufinfo.buf, bufinfo.len, DAC_ALIGN_8B_R);
    } else {
        HAL_DAC_Start_DMA(&DAC_Handle, self->dac_channel,
            (uint32_t*)bufinfo.buf, bufinfo.len / 2, DAC_ALIGN_12B_R);
    }
Damien George's avatar
Damien George committed
463
464
465

    /*
    // enable DMA stream
466
467
    DMA_Cmd(DMA_Handle->Instance, ENABLE);
    while (DMA_GetCmdStatus(DMA_Handle->Instance) == DISABLE) {
Damien George's avatar
Damien George committed
468
469
470
471
472
473
474
475
476
477
478
479
480
    }

    // enable DAC channel
    DAC_Cmd(self->dac_channel, ENABLE);

    // enable DMA for DAC channel
    DAC_DMACmd(self->dac_channel, ENABLE);
    */

    //printf("DMA: %p %lu\n", bufinfo.buf, bufinfo.len);

    return mp_const_none;
}
481
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_dac_write_timed_obj, 1, pyb_dac_write_timed);
482
#endif
Damien George's avatar
Damien George committed
483

484
STATIC const mp_rom_map_elem_t pyb_dac_locals_dict_table[] = {
485
    // instance methods
486
487
488
    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_dac_init_obj) },
    { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_dac_deinit_obj) },
    { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_dac_write_obj) },
489
    #if defined(TIM6)
490
491
492
    { MP_ROM_QSTR(MP_QSTR_noise), MP_ROM_PTR(&pyb_dac_noise_obj) },
    { MP_ROM_QSTR(MP_QSTR_triangle), MP_ROM_PTR(&pyb_dac_triangle_obj) },
    { MP_ROM_QSTR(MP_QSTR_write_timed), MP_ROM_PTR(&pyb_dac_write_timed_obj) },
493
    #endif
494
495

    // class constants
496
497
    { MP_ROM_QSTR(MP_QSTR_NORMAL), MP_ROM_INT(DMA_NORMAL) },
    { MP_ROM_QSTR(MP_QSTR_CIRCULAR), MP_ROM_INT(DMA_CIRCULAR) },
Damien George's avatar
Damien George committed
498
499
};

500
501
STATIC MP_DEFINE_CONST_DICT(pyb_dac_locals_dict, pyb_dac_locals_dict_table);

Damien George's avatar
Damien George committed
502
503
504
505
const mp_obj_type_t pyb_dac_type = {
    { &mp_type_type },
    .name = MP_QSTR_DAC,
    .make_new = pyb_dac_make_new,
506
    .locals_dict = (mp_obj_dict_t*)&pyb_dac_locals_dict,
Damien George's avatar
Damien George committed
507
};
508
509

#endif // MICROPY_HW_ENABLE_DAC