circuitpython/ports/stm32f4/common-hal/analogio/AnalogOut.c

118 lines
3.9 KiB
C
Raw Normal View History

2019-09-07 15:54:16 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
2019-10-09 13:34:20 -04:00
* Copyright (c) 2019, Lucian Copeland for Adafruit Industries
2019-09-07 15:54:16 -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>
#include "py/mperrno.h"
#include "py/runtime.h"
#include "shared-bindings/analogio/AnalogOut.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "supervisor/shared/translate.h"
2019-10-09 13:34:20 -04:00
#include "common-hal/microcontroller/Pin.h"
#include "stm32f4xx_hal.h"
//DAC is shared between both channels.
//TODO: store as struct with channel info, automatically turn it off if unused
//on both channels for power save?
2019-10-09 16:06:23 -04:00
#if HAS_DAC
2019-10-09 13:34:20 -04:00
DAC_HandleTypeDef handle;
2019-10-09 14:52:30 -04:00
#endif
2019-10-09 13:34:20 -04:00
2019-09-07 15:54:16 -04:00
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) {
2019-10-09 16:06:23 -04:00
#if !(HAS_DAC)
2019-10-09 13:34:20 -04:00
mp_raise_ValueError(translate("No DAC on chip"));
2019-10-09 14:52:30 -04:00
#else
2019-10-09 13:34:20 -04:00
if (pin == &pin_PA04) {
self->channel = DAC_CHANNEL_1;
} else if (pin == &pin_PA05) {
self->channel = DAC_CHANNEL_2;
} else {
mp_raise_ValueError(translate("Invalid DAC pin supplied"));
}
//Only init if the shared DAC is empty or reset
if (handle.Instance == NULL || handle.State == HAL_DAC_STATE_RESET) {
__HAL_RCC_DAC_CLK_ENABLE();
handle.Instance = DAC;
if (HAL_DAC_Init(&handle) != HAL_OK)
{
mp_raise_ValueError(translate("DAC Device Init Error"));
}
}
//init channel specific pin
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(pin->number);
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(pin_port(pin->port), &GPIO_InitStruct);
self->ch_handle.DAC_Trigger = DAC_TRIGGER_NONE;
self->ch_handle.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
if (HAL_DAC_ConfigChannel(&handle, &self->ch_handle, self->channel) != HAL_OK) {
mp_raise_ValueError(translate("DAC Channel Init Error"));
}
self->pin = pin;
self->deinited = false;
claim_pin(pin);
2019-10-09 14:52:30 -04:00
#endif
2019-09-07 15:54:16 -04:00
}
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
return self->deinited;
}
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
2019-10-09 16:06:23 -04:00
#if HAS_DAC
2019-10-09 13:34:20 -04:00
reset_pin_number(self->pin->port,self->pin->number);
self->pin = mp_const_none;
self->deinited = true;
//TODO: if both are de-inited, should we turn off the DAC?
2019-10-09 14:52:30 -04:00
#endif
2019-09-07 15:54:16 -04:00
}
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
uint16_t value) {
2019-10-09 16:06:23 -04:00
#if HAS_DAC
2019-10-09 13:34:20 -04:00
HAL_DAC_SetValue(&handle, self->channel, DAC_ALIGN_12B_R, value >> 4);
HAL_DAC_Start(&handle, self->channel);
2019-10-09 14:52:30 -04:00
#endif
2019-09-07 15:54:16 -04:00
}
void analogout_reset(void) {
2019-10-09 16:06:23 -04:00
#if HAS_DAC
2019-10-09 13:34:20 -04:00
__HAL_RCC_DAC_CLK_DISABLE();
HAL_DAC_DeInit(&handle);
2019-10-09 14:52:30 -04:00
#endif
2019-09-07 15:54:16 -04:00
}