2016-11-03 18:50:59 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-02-24 09:13:07 -05:00
|
|
|
#include "py/mperrno.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/analogio/AnalogOut.h"
|
2019-02-18 22:44:31 -05:00
|
|
|
#include "shared-bindings/audioio/AudioOut.h"
|
2017-11-14 21:22:16 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-11-14 21:22:16 -05:00
|
|
|
#include "atmel_start_pins.h"
|
|
|
|
#include "hal/include/hal_dac_sync.h"
|
|
|
|
#include "hpl/gclk/hpl_gclk_base.h"
|
2018-02-27 17:21:03 -05:00
|
|
|
#include "peripheral_clk_config.h"
|
2017-11-14 21:22:16 -05:00
|
|
|
|
|
|
|
#ifdef SAMD21
|
|
|
|
#include "hpl/pm/hpl_pm_base.h"
|
|
|
|
#endif
|
|
|
|
|
2020-10-17 20:08:32 -04:00
|
|
|
#define HAVE_ANALOGOUT ( \
|
2022-10-03 15:29:03 -04:00
|
|
|
(defined(PIN_PA02) && !defined(IGNORE_PIN_PA02)) || \
|
|
|
|
(defined(SAM_D5X_E5X) && defined(PIN_PA05) && !defined(IGNORE_PIN_PA05)) \
|
2021-03-15 09:57:36 -04:00
|
|
|
)
|
2020-10-17 20:08:32 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self,
|
|
|
|
const mcu_pin_obj_t *pin) {
|
2020-10-17 20:08:32 -04:00
|
|
|
#if !HAVE_ANALOGOUT
|
2018-08-16 16:34:12 -04:00
|
|
|
mp_raise_NotImplementedError(translate("No DAC on chip"));
|
2018-08-07 10:43:52 -04:00
|
|
|
#else
|
2020-10-17 20:08:32 -04:00
|
|
|
|
2021-01-10 19:19:48 -05:00
|
|
|
uint8_t channel;
|
|
|
|
switch (pin->number) {
|
|
|
|
#if defined(PIN_PA02) && !defined(IGNORE_PIN_PA02)
|
|
|
|
case PIN_PA02:
|
|
|
|
channel = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
2020-10-17 20:08:32 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if defined(SAM_D5X_E5X) && defined(PIN_PA05) && !defined(IGNORE_PIN_PA05)
|
2021-01-10 19:19:48 -05:00
|
|
|
case PIN_PA05:
|
|
|
|
channel = 1;
|
|
|
|
break;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2020-10-17 20:08:32 -04:00
|
|
|
|
2021-01-10 19:19:48 -05:00
|
|
|
default:
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_ValueError_invalid_pin();
|
2021-03-15 09:57:36 -04:00
|
|
|
return;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-11-14 21:22:16 -05:00
|
|
|
|
2020-10-17 20:08:32 -04:00
|
|
|
self->channel = channel;
|
2021-07-12 20:33:42 -04:00
|
|
|
self->pin = pin;
|
2017-11-14 21:22:16 -05:00
|
|
|
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2017-11-14 21:22:16 -05:00
|
|
|
hri_mclk_set_APBDMASK_DAC_bit(MCLK);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SAMD21
|
|
|
|
_pm_enable_bus_clock(PM_BUS_APBC, DAC);
|
|
|
|
#endif
|
|
|
|
|
2021-10-05 00:18:26 -04:00
|
|
|
// SAMD21: This clock should be <= 350 kHz, per datasheet table 36-7.
|
|
|
|
// SAMD51: This clock should be <= 12 MHz, per datasheet section 47.6.3.
|
2018-02-27 17:21:03 -05:00
|
|
|
_gclk_enable_channel(DAC_GCLK_ID, CONF_GCLK_DAC_SRC);
|
|
|
|
|
2017-11-14 21:22:16 -05:00
|
|
|
// Don't double init the DAC on the SAMD51 when both outputs are in use. We use the free state
|
|
|
|
// of each output pin to determine DAC state.
|
|
|
|
int32_t result = ERR_NONE;
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2017-11-14 21:22:16 -05:00
|
|
|
if (!common_hal_mcu_pin_is_free(&pin_PA02) || !common_hal_mcu_pin_is_free(&pin_PA05)) {
|
|
|
|
#endif
|
2021-03-15 09:57:36 -04:00
|
|
|
// Fake the descriptor if the DAC is already initialized.
|
|
|
|
self->descriptor.device.hw = DAC;
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
2017-11-14 21:22:16 -05:00
|
|
|
#endif
|
2021-03-15 09:57:36 -04:00
|
|
|
result = dac_sync_init(&self->descriptor, DAC);
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2017-11-14 21:22:16 -05:00
|
|
|
#endif
|
|
|
|
if (result != ERR_NONE) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2016-11-03 18:50:59 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-04-12 18:24:50 -04:00
|
|
|
claim_pin(pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2018-07-31 17:01:01 -04:00
|
|
|
gpio_set_pin_function(pin->number, GPIO_PIN_FUNCTION_B);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-11-14 21:22:16 -05:00
|
|
|
dac_sync_enable_channel(&self->descriptor, self->channel);
|
2018-08-07 10:43:52 -04:00
|
|
|
#endif
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
|
2020-10-17 20:08:32 -04:00
|
|
|
#if !HAVE_ANALOGOUT
|
|
|
|
return false;
|
|
|
|
#else
|
2017-10-02 20:49:40 -04:00
|
|
|
return self->deinited;
|
2020-10-17 20:08:32 -04:00
|
|
|
#endif
|
2017-10-02 20:49:40 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
|
2020-10-17 20:08:32 -04:00
|
|
|
#if HAVE_ANALOGOUT
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_analogio_analogout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-14 21:22:16 -05:00
|
|
|
dac_sync_disable_channel(&self->descriptor, self->channel);
|
2021-07-12 20:33:42 -04:00
|
|
|
reset_pin_number(self->pin->number);
|
|
|
|
self->pin = NULL;
|
|
|
|
|
2017-11-14 21:22:16 -05:00
|
|
|
// Only deinit the DAC on the SAMD51 if both outputs are free.
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2017-11-14 21:22:16 -05:00
|
|
|
if (common_hal_mcu_pin_is_free(&pin_PA02) && common_hal_mcu_pin_is_free(&pin_PA05)) {
|
|
|
|
#endif
|
2021-03-15 09:57:36 -04:00
|
|
|
dac_sync_deinit(&self->descriptor);
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2017-11-14 21:22:16 -05:00
|
|
|
#endif
|
2017-10-02 20:49:40 -04:00
|
|
|
self->deinited = true;
|
2017-11-14 21:22:16 -05:00
|
|
|
// TODO(tannewt): Turn off the DAC clocks to save power.
|
2018-08-07 10:43:52 -04:00
|
|
|
#endif
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t value) {
|
2020-10-17 20:08:32 -04:00
|
|
|
#if HAVE_ANALOGOUT
|
2018-02-27 15:44:27 -05:00
|
|
|
// Input is 16 bit so make sure and set LEFTADJ to 1 so it takes the top
|
2017-11-14 21:22:16 -05:00
|
|
|
// bits. This is currently done in asf4_conf/*/hpl_dac_config.h.
|
|
|
|
dac_sync_write(&self->descriptor, self->channel, &value, 1);
|
2020-10-17 20:08:32 -04:00
|
|
|
#endif
|
2017-11-14 21:22:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void analogout_reset(void) {
|
2019-02-18 22:44:31 -05:00
|
|
|
// audioout_reset also resets the DAC, and does a smooth ramp down to avoid clicks
|
|
|
|
// if it was enabled, so do that instead if AudioOut is enabled.
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_AUDIOIO
|
2019-02-18 22:44:31 -05:00
|
|
|
audioout_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#elif HAVE_ANALOGOUT
|
2019-02-18 22:44:31 -05:00
|
|
|
#ifdef SAMD21
|
2021-03-15 09:57:36 -04:00
|
|
|
while (DAC->STATUS.reg & DAC_STATUS_SYNCBUSY) {
|
|
|
|
}
|
2019-02-18 22:44:31 -05:00
|
|
|
#endif
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2021-03-15 09:57:36 -04:00
|
|
|
while (DAC->SYNCBUSY.reg & DAC_SYNCBUSY_SWRST) {
|
|
|
|
}
|
2019-02-18 22:44:31 -05:00
|
|
|
#endif
|
|
|
|
DAC->CTRLA.reg |= DAC_CTRLA_SWRST;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|