Merge pull request #2208 from hierophect/stm32-DAC

STM32: DAC Support
This commit is contained in:
hierophect 2019-10-10 17:08:47 -04:00 committed by GitHub
commit 0ccab508f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 34 deletions

View File

@ -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
@ -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 \
@ -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 \
@ -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 $@"

View File

@ -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 */

View File

@ -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 */

View File

@ -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
@ -34,9 +35,57 @@
#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) {
@ -44,14 +93,25 @@ bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
}
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
}

View File

@ -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
@ -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

View File

@ -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

View File

@ -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__