summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHierophect <hierophect@gmail.com>2019-10-09 13:34:20 -0400
committerHierophect <hierophect@gmail.com>2019-10-09 13:34:20 -0400
commitcb0ed264c4cad34c7a2a5fb822f8bbe36a23eeed (patch)
treed690efa5d4623fc0265460948e3dd663f53b1fa6
parentf4922a530a06ed62523ae333eab90fee6145fc46 (diff)
Implement DAC
-rwxr-xr-xports/stm32f4/Makefile54
-rw-r--r--ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h2
-rw-r--r--ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h2
-rw-r--r--ports/stm32f4/common-hal/analogio/AnalogOut.c59
-rw-r--r--ports/stm32f4/common-hal/analogio/AnalogOut.h5
-rw-r--r--ports/stm32f4/peripherals/stm32f4/periph.h15
6 files changed, 101 insertions, 36 deletions
diff --git a/ports/stm32f4/Makefile b/ports/stm32f4/Makefile
index bef26d662..ef5ba1931 100755
--- a/ports/stm32f4/Makefile
+++ b/ports/stm32f4/Makefile
@@ -28,7 +28,7 @@ ifeq ($(BOARD),)
$(error You must provide a BOARD parameter)
else
ifeq ($(wildcard boards/$(BOARD)/.),)
- $(error Invalid BOARD specified)
+ $(error Invalid BOARD specified)
endif
endif
@@ -128,30 +128,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 +156,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 \
@@ -200,12 +204,12 @@ SRC_S = \
boards/startup_$(MCU_SUB_VARIANT).s
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
- $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
- $(addprefix common-hal/, $(SRC_COMMON_HAL))
+ $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
+ $(addprefix common-hal/, $(SRC_COMMON_HAL))
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
- $(addprefix shared-module/, $(SRC_SHARED_MODULE)) \
- $(addprefix shared-module/, $(SRC_SHARED_MODULE_INTERNAL))
+ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) \
+ $(addprefix shared-module/, $(SRC_SHARED_MODULE_INTERNAL))
ifneq ($(FROZEN_MPY_DIR),)
@@ -242,12 +246,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 $@"
diff --git a/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h b/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h
index 019b03ca7..a13e5ecfa 100644
--- a/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h
+++ b/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h
@@ -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 */
diff --git a/ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h b/ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h
index 019b03ca7..a13e5ecfa 100644
--- a/ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h
+++ b/ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h
@@ -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 */
diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.c b/ports/stm32f4/common-hal/analogio/AnalogOut.c
index a59a8b190..75592b30e 100644
--- a/ports/stm32f4/common-hal/analogio/AnalogOut.c
+++ b/ports/stm32f4/common-hal/analogio/AnalogOut.c
@@ -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,54 @@
#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?
+DAC_HandleTypeDef handle;
+
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 !defined(HAS_DAC)
+ mp_raise_ValueError(translate("No DAC on chip"));
+ #endif
+ 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);
}
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
@@ -44,14 +90,19 @@ bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
}
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
-
+ 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?
}
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
uint16_t value) {
+ HAL_DAC_SetValue(&handle, self->channel, DAC_ALIGN_12B_R, value >> 4);
+ HAL_DAC_Start(&handle, self->channel);
}
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.
+ __HAL_RCC_DAC_CLK_DISABLE();
+ HAL_DAC_DeInit(&handle);
}
diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.h b/ports/stm32f4/common-hal/analogio/AnalogOut.h
index f2952e9f9..f5cf10c67 100644
--- a/ports/stm32f4/common-hal/analogio/AnalogOut.h
+++ b/ports/stm32f4/common-hal/analogio/AnalogOut.h
@@ -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
@@ -30,9 +31,13 @@
#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;
+ DAC_ChannelConfTypeDef ch_handle;
+ const mcu_pin_obj_t * pin;
uint8_t channel;
bool deinited;
} analogio_analogout_obj_t;
diff --git a/ports/stm32f4/peripherals/stm32f4/periph.h b/ports/stm32f4/peripherals/stm32f4/periph.h
index 06ab2d3e4..7ad59d756 100644
--- a/ports/stm32f4/peripherals/stm32f4/periph.h
+++ b/ports/stm32f4/peripherals/stm32f4/periph.h
@@ -99,16 +99,21 @@ typedef struct {
.pin = spi_pin, \
}
-// TODO: SPI, UART, etc
+//Starter Lines
-// Choose based on chip
-#ifdef STM32F412Zx
-#include "stm32f412zx/periph.h"
-#endif
#ifdef STM32F411xE
#include "stm32f411xe/periph.h"
#endif
+
+#ifdef STM32F412Zx
+#include "stm32f412zx/periph.h"
+#endif
+
+//Foundation Lines
+
#ifdef STM32F405xx
+#define HAS_DAC
#include "stm32f405xx/periph.h"
#endif
+
#endif // __MICROPY_INCLUDED_STM32F4_PERIPHERALS_PERIPH_H__