summaryrefslogtreecommitdiff
path: root/ports/stm/common-hal/analogio/AnalogOut.c
blob: 1a3817a35dbbe0e82877d71cfebb68373b91ae03 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
 * Copyright (c) 2019, Lucian Copeland for Adafruit Industries
 *
 * 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"

#include "common-hal/microcontroller/Pin.h"

#include "stm32f4xx_hal.h"

// DAC is shared between both channels.
#if HAS_DAC
DAC_HandleTypeDef handle;
#endif

STATIC bool dac_on[2];

void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self,
    const mcu_pin_obj_t *pin) {
    #if !(HAS_DAC)
    mp_raise_ValueError(translate("No DAC on chip"));
    #else
    if (pin == &pin_PA04) {
        self->channel = DAC_CHANNEL_1;
        self->dac_index = 0;
    } else if (pin == &pin_PA05) {
        self->channel = DAC_CHANNEL_2;
        self->dac_index = 1;
    } 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"));
    }

    dac_on[self->dac_index] = true;
    self->pin = pin;
    common_hal_mcu_pin_claim(pin);
    #endif
}

bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
    return !dac_on[self->dac_index];
}

void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
    #if HAS_DAC
    reset_pin_number(self->pin->port,self->pin->number);
    self->pin = NULL;
    dac_on[self->dac_index] = false;

    // turn off the DAC if both channels are off
    if (dac_on[0] == false && dac_on[1] == false) {
        __HAL_RCC_DAC_CLK_DISABLE();
        HAL_DAC_DeInit(&handle);
    }
    #endif
}

void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
    uint16_t value) {
    #if HAS_DAC
    HAL_DAC_SetValue(&handle, self->channel, DAC_ALIGN_12B_R, value >> 4);
    HAL_DAC_Start(&handle, self->channel);
    #endif
}

void analogout_reset(void) {
    #if HAS_DAC
    __HAL_RCC_DAC_CLK_DISABLE();
    HAL_DAC_DeInit(&handle);
    #endif
}