Skip to content

Commit 0ccab50

Browse files
authored
Merge pull request #2208 from hierophect/stm32-DAC
STM32: DAC Support
2 parents 18e91d9 + 06b2fed commit 0ccab50

File tree

7 files changed

+117
-34
lines changed

7 files changed

+117
-34
lines changed

ports/stm32f4/Makefile

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# The MIT License (MIT)
44
#
55
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
6+
# Copyright (c) 2019 Lucian Copeland for Adafruit Industries
67
#
78
# Permission is hereby granted, free of charge, to any person obtaining a copy
89
# of this software and associated documentation files (the "Software"), to deal
@@ -128,30 +129,21 @@ CFLAGS += -DHSE_VALUE=8000000 -DCFG_TUSB_MCU=OPT_MCU_STM32F4 -DCFG_TUD_CDC_RX_BU
128129
######################################
129130

130131
SRC_STM32 = \
131-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_gpio.c \
132-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c \
133132
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c \
134-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c \
135-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd_ex.c \
136-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \
137-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c \
138-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c \
133+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c \
134+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c \
135+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c \
139136
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \
140-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \
141-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c \
142-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma.c \
143-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c \
144-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c \
137+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c \
145138
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_qspi.c \
146-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c \
147-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c \
139+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c \
140+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c \
141+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c \
142+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \
148143
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
149144
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \
150-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usart.c \
151-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_rcc.c \
152-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \
153-
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \
154145
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \
146+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c \
155147
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
156148
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
157149
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \
@@ -165,8 +157,21 @@ SRC_STM32 = \
165157
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \
166158
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
167159
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \
160+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c \
161+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_gpio.c \
162+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c \
163+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \
164+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c \
165+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c \
166+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma.c \
167+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c \
168+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usart.c \
169+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_rcc.c \
170+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \
171+
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \
168172
system_stm32f4xx.c
169173

174+
170175
SRC_C += \
171176
background.c \
172177
fatfs_port.c \
@@ -242,12 +247,12 @@ $(BUILD)/firmware.elf: $(OBJ)
242247
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
243248
$(STEPECHO) "Create $@"
244249
$(Q)$(OBJCOPY) -O binary $^ $@
245-
# $(Q)$(OBJCOPY) -O binary -j .vectors -j .text -j .data $^ $@
250+
# $(Q)$(OBJCOPY) -O binary -j .vectors -j .text -j .data $^ $@
246251

247252
$(BUILD)/firmware.hex: $(BUILD)/firmware.elf
248253
$(STEPECHO) "Create $@"
249254
$(Q)$(OBJCOPY) -O ihex $^ $@
250-
# $(Q)$(OBJCOPY) -O ihex -j .vectors -j .text -j .data $^ $@
255+
# $(Q)$(OBJCOPY) -O ihex -j .vectors -j .text -j .data $^ $@
251256

252257
$(BUILD)/firmware.uf2: $(BUILD)/firmware.hex
253258
$(ECHO) "Create $@"

ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/* #define HAL_CAN_MODULE_ENABLED */
4242
/* #define HAL_CRC_MODULE_ENABLED */
4343
/* #define HAL_CRYP_MODULE_ENABLED */
44-
/* #define HAL_DAC_MODULE_ENABLED */
44+
#define HAL_DAC_MODULE_ENABLED
4545
/* #define HAL_DCMI_MODULE_ENABLED */
4646
/* #define HAL_DMA2D_MODULE_ENABLED */
4747
/* #define HAL_ETH_MODULE_ENABLED */

ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/* #define HAL_CAN_MODULE_ENABLED */
4242
/* #define HAL_CRC_MODULE_ENABLED */
4343
/* #define HAL_CRYP_MODULE_ENABLED */
44-
/* #define HAL_DAC_MODULE_ENABLED */
44+
#define HAL_DAC_MODULE_ENABLED
4545
/* #define HAL_DCMI_MODULE_ENABLED */
4646
/* #define HAL_DMA2D_MODULE_ENABLED */
4747
/* #define HAL_ETH_MODULE_ENABLED */

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2019, Lucian Copeland for Adafruit Industries
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -34,24 +35,83 @@
3435
#include "shared-bindings/microcontroller/Pin.h"
3536
#include "supervisor/shared/translate.h"
3637

38+
#include "common-hal/microcontroller/Pin.h"
39+
40+
#include "stm32f4xx_hal.h"
41+
42+
//DAC is shared between both channels.
43+
//TODO: store as struct with channel info, automatically turn it off if unused
44+
//on both channels for power save?
45+
#if HAS_DAC
46+
DAC_HandleTypeDef handle;
47+
#endif
48+
3749
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
3850
const mcu_pin_obj_t *pin) {
39-
mp_raise_ValueError(translate("DAC not supported"));
51+
#if !(HAS_DAC)
52+
mp_raise_ValueError(translate("No DAC on chip"));
53+
#else
54+
if (pin == &pin_PA04) {
55+
self->channel = DAC_CHANNEL_1;
56+
} else if (pin == &pin_PA05) {
57+
self->channel = DAC_CHANNEL_2;
58+
} else {
59+
mp_raise_ValueError(translate("Invalid DAC pin supplied"));
60+
}
61+
62+
//Only init if the shared DAC is empty or reset
63+
if (handle.Instance == NULL || handle.State == HAL_DAC_STATE_RESET) {
64+
__HAL_RCC_DAC_CLK_ENABLE();
65+
handle.Instance = DAC;
66+
if (HAL_DAC_Init(&handle) != HAL_OK)
67+
{
68+
mp_raise_ValueError(translate("DAC Device Init Error"));
69+
}
70+
}
71+
72+
//init channel specific pin
73+
GPIO_InitTypeDef GPIO_InitStruct = {0};
74+
GPIO_InitStruct.Pin = pin_mask(pin->number);
75+
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
76+
GPIO_InitStruct.Pull = GPIO_NOPULL;
77+
HAL_GPIO_Init(pin_port(pin->port), &GPIO_InitStruct);
78+
79+
self->ch_handle.DAC_Trigger = DAC_TRIGGER_NONE;
80+
self->ch_handle.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
81+
if (HAL_DAC_ConfigChannel(&handle, &self->ch_handle, self->channel) != HAL_OK) {
82+
mp_raise_ValueError(translate("DAC Channel Init Error"));
83+
}
84+
85+
self->pin = pin;
86+
self->deinited = false;
87+
claim_pin(pin);
88+
#endif
4089
}
4190

4291
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
4392
return self->deinited;
4493
}
4594

4695
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
47-
96+
#if HAS_DAC
97+
reset_pin_number(self->pin->port,self->pin->number);
98+
self->pin = mp_const_none;
99+
self->deinited = true;
100+
//TODO: if both are de-inited, should we turn off the DAC?
101+
#endif
48102
}
49103

50104
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
51105
uint16_t value) {
106+
#if HAS_DAC
107+
HAL_DAC_SetValue(&handle, self->channel, DAC_ALIGN_12B_R, value >> 4);
108+
HAL_DAC_Start(&handle, self->channel);
109+
#endif
52110
}
53111

54112
void analogout_reset(void) {
55-
// audioout_reset also resets the DAC, and does a smooth ramp down to avoid clicks
56-
// if it was enabled, so do that instead if AudioOut is enabled.
113+
#if HAS_DAC
114+
__HAL_RCC_DAC_CLK_DISABLE();
115+
HAL_DAC_DeInit(&handle);
116+
#endif
57117
}

ports/stm32f4/common-hal/analogio/AnalogOut.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2016 Scott Shawcroft
7+
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -24,19 +25,25 @@
2425
* THE SOFTWARE.
2526
*/
2627

27-
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
28-
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
28+
#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
29+
#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
2930

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

3233
#include "py/obj.h"
34+
#include "stm32f4xx_hal.h"
35+
#include "stm32f4/periph.h"
3336

3437
typedef struct {
3538
mp_obj_base_t base;
39+
#if HAS_DAC
40+
DAC_ChannelConfTypeDef ch_handle;
41+
#endif
42+
const mcu_pin_obj_t * pin;
3643
uint8_t channel;
3744
bool deinited;
3845
} analogio_analogout_obj_t;
3946

4047
void analogout_reset(void);
4148

42-
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
49+
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H

ports/stm32f4/mpconfigport.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ CIRCUITPY_ANALOGIO = 1
2121
CIRCUITPY_MICROCONTROLLER = 1
2222
CIRCUITPY_BUSIO = 1
2323
CIRCUITPY_OS = 1
24+
CIRCUITPY_STORAGE = 1
25+
CIRCUITPY_RANDOM = 1
26+
CIRCUITPY_USB_HID = 1
27+
CIRCUITPY_USB_MIDI = 1
2428

2529
#ifeq ($(MCU_SUB_VARIANT), stm32f412zx)
2630
#endif

ports/stm32f4/peripherals/stm32f4/periph.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,23 @@ typedef struct {
9999
.pin = spi_pin, \
100100
}
101101

102-
// TODO: SPI, UART, etc
102+
//Starter Lines
103103

104-
// Choose based on chip
105-
#ifdef STM32F412Zx
106-
#include "stm32f412zx/periph.h"
107-
#endif
108104
#ifdef STM32F411xE
105+
#define HAS_DAC 0
109106
#include "stm32f411xe/periph.h"
110107
#endif
108+
109+
#ifdef STM32F412Zx
110+
#define HAS_DAC 0
111+
#include "stm32f412zx/periph.h"
112+
#endif
113+
114+
//Foundation Lines
115+
111116
#ifdef STM32F405xx
117+
#define HAS_DAC 1
112118
#include "stm32f405xx/periph.h"
113119
#endif
120+
114121
#endif // __MICROPY_INCLUDED_STM32F4_PERIPHERALS_PERIPH_H__

0 commit comments

Comments
 (0)