Skip to content

STM32: DAC Support #2208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions ports/stm32f4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
# 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
Expand Down Expand Up @@ -128,30 +129,21 @@ CFLAGS += -DHSE_VALUE=8000000 -DCFG_TUSB_MCU=OPT_MCU_STM32F4 -DCFG_TUD_CDC_RX_BU
######################################

SRC_STM32 = \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_gpio.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_qspi.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usart.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_rcc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \
Expand All @@ -165,8 +157,21 @@ SRC_STM32 = \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_gpio.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usart.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_rcc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \
system_stm32f4xx.c


SRC_C += \
background.c \
fatfs_port.c \
Expand Down Expand Up @@ -242,12 +247,12 @@ $(BUILD)/firmware.elf: $(OBJ)
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
$(STEPECHO) "Create $@"
$(Q)$(OBJCOPY) -O binary $^ $@
# $(Q)$(OBJCOPY) -O binary -j .vectors -j .text -j .data $^ $@
# $(Q)$(OBJCOPY) -O binary -j .vectors -j .text -j .data $^ $@

$(BUILD)/firmware.hex: $(BUILD)/firmware.elf
$(STEPECHO) "Create $@"
$(Q)$(OBJCOPY) -O ihex $^ $@
# $(Q)$(OBJCOPY) -O ihex -j .vectors -j .text -j .data $^ $@
# $(Q)$(OBJCOPY) -O ihex -j .vectors -j .text -j .data $^ $@

$(BUILD)/firmware.uf2: $(BUILD)/firmware.hex
$(ECHO) "Create $@"
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/* #define HAL_CAN_MODULE_ENABLED */
/* #define HAL_CRC_MODULE_ENABLED */
/* #define HAL_CRYP_MODULE_ENABLED */
/* #define HAL_DAC_MODULE_ENABLED */
#define HAL_DAC_MODULE_ENABLED
/* #define HAL_DCMI_MODULE_ENABLED */
/* #define HAL_DMA2D_MODULE_ENABLED */
/* #define HAL_ETH_MODULE_ENABLED */
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/* #define HAL_CAN_MODULE_ENABLED */
/* #define HAL_CRC_MODULE_ENABLED */
/* #define HAL_CRYP_MODULE_ENABLED */
/* #define HAL_DAC_MODULE_ENABLED */
#define HAL_DAC_MODULE_ENABLED
/* #define HAL_DCMI_MODULE_ENABLED */
/* #define HAL_DMA2D_MODULE_ENABLED */
/* #define HAL_ETH_MODULE_ENABLED */
Expand Down
68 changes: 64 additions & 4 deletions ports/stm32f4/common-hal/analogio/AnalogOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* 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
Expand Down Expand Up @@ -34,24 +35,83 @@
#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.
//TODO: store as struct with channel info, automatically turn it off if unused
//on both channels for power save?
#if HAS_DAC
DAC_HandleTypeDef handle;
#endif

void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) {
mp_raise_ValueError(translate("DAC not supported"));
#if !(HAS_DAC)
mp_raise_ValueError(translate("No DAC on chip"));
#else
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);
#endif
}

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) {

#if HAS_DAC
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?
#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) {
// 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.
#if HAS_DAC
__HAL_RCC_DAC_CLK_DISABLE();
HAL_DAC_DeInit(&handle);
#endif
}
13 changes: 10 additions & 3 deletions ports/stm32f4/common-hal/analogio/AnalogOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
* 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
Expand All @@ -24,19 +25,25 @@
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H

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

#include "py/obj.h"
#include "stm32f4xx_hal.h"
#include "stm32f4/periph.h"

typedef struct {
mp_obj_base_t base;
#if HAS_DAC
DAC_ChannelConfTypeDef ch_handle;
#endif
const mcu_pin_obj_t * pin;
uint8_t channel;
bool deinited;
} analogio_analogout_obj_t;

void analogout_reset(void);

#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
4 changes: 4 additions & 0 deletions ports/stm32f4/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ CIRCUITPY_ANALOGIO = 1
CIRCUITPY_MICROCONTROLLER = 1
CIRCUITPY_BUSIO = 1
CIRCUITPY_OS = 1
CIRCUITPY_STORAGE = 1
CIRCUITPY_RANDOM = 1
CIRCUITPY_USB_HID = 1
CIRCUITPY_USB_MIDI = 1

#ifeq ($(MCU_SUB_VARIANT), stm32f412zx)
#endif
17 changes: 12 additions & 5 deletions ports/stm32f4/peripherals/stm32f4/periph.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,23 @@ typedef struct {
.pin = spi_pin, \
}

// TODO: SPI, UART, etc
//Starter Lines

// Choose based on chip
#ifdef STM32F412Zx
#include "stm32f412zx/periph.h"
#endif
#ifdef STM32F411xE
#define HAS_DAC 0
#include "stm32f411xe/periph.h"
#endif

#ifdef STM32F412Zx
#define HAS_DAC 0
#include "stm32f412zx/periph.h"
#endif

//Foundation Lines

#ifdef STM32F405xx
#define HAS_DAC 1
#include "stm32f405xx/periph.h"
#endif

#endif // __MICROPY_INCLUDED_STM32F4_PERIPHERALS_PERIPH_H__