pull/35/head
Simon Kueppers 2023-03-11 12:29:09 +01:00
commit 09bc9a667b
11 zmienionych plików z 160 dodań i 19 usunięć

Wyświetl plik

@ -123,7 +123,7 @@
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactExtension="elf" artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1169813092" name="Release" parent="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release">
<configuration artifactExtension="elf" artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1169813092" name="Release" parent="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release" postbuildStep="$(MAKE) dfu-suffix FILENAME=${BuildArtifactFileBaseName}.bin PID=7388 VID=1209 DID=0100">
<folderInfo id="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.1169813092." name="/" resourcePath="">
<toolChain id="com.st.stm32cube.ide.mcu.gnu.managedbuild.toolchain.exe.release.1126198335" name="MCU ARM GCC" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.toolchain.exe.release">
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_mcu.406945528" name="MCU" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_mcu" useByScannerDiscovery="true" value="STM32F302CBTx" valueType="string"/>
@ -136,7 +136,7 @@
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.floatabi.1666201234" name="Floating-point ABI" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.floatabi" useByScannerDiscovery="true" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.floatabi.value.hard" valueType="enumerated"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.listfile.1567413305" name="Generate list file" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.listfile" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.nanoprintffloat.162803032" name="Use float with printf from newlib-nano (-u _printf_float)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.nanoprintffloat" useByScannerDiscovery="false" value="false" valueType="boolean"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.convertbinary.1793122184" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.convertbinary" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.convertbinary.1793122184" name="Convert to binary file (-O binary)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.convertbinary" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform.1688800353" isAbstract="false" osList="all" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform"/>
<builder buildPath="${workspace_loc:/aioc-fw}/Release" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder.823068587" keepEnvironmentInBuildfile="false" name="Gnu Make Builder" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder"/>
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.309469290" name="MCU GCC Assembler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler">
@ -226,7 +226,14 @@
<project id="aioc-fw.com.atollic.truestudio.exe.26619622" name="Executable"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope"/>
<storageModule moduleId="refreshScope" versionNumber="2">
<configuration configurationName="Debug">
<resource resourceType="PROJECT" workspacePath="/aioc-fw"/>
</configuration>
<configuration configurationName="Release">
<resource resourceType="PROJECT" workspacePath="/aioc-fw"/>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>

Wyświetl plik

@ -53,7 +53,7 @@
/* #define HAL_I2C_MODULE_ENABLED */
/* #define HAL_I2S_MODULE_ENABLED */
/* #define HAL_IRDA_MODULE_ENABLED */
/* #define HAL_IWDG_MODULE_ENABLED */
#define HAL_IWDG_MODULE_ENABLED
/* #define HAL_OPAMP_MODULE_ENABLED */
/* #define HAL_PCD_MODULE_ENABLED */
#define HAL_PWR_MODULE_ENABLED

Wyświetl plik

@ -98,6 +98,7 @@
#define CFG_TUD_AUDIO 1
#define CFG_TUD_CDC 1
#define CFG_TUD_HID 1
#define CFG_TUD_DFU_RUNTIME 1
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE 128

Wyświetl plik

@ -6,6 +6,16 @@
#include <assert.h>
#include <stdio.h>
// from ST application note AN2606
// Table 171: Bootloader device-dependent parameters
#if defined(STM32F302xB) || defined(STM32F302xC) || \
defined(STM32F303xB) || defined(STM32F303xC) || \
defined(STM32F373xC)
#define SYSTEM_MEMORY_BASE 0x1FFFD800
#else
#warning Live DFU reboot not supported on this MCU
#endif
static void SystemClock_Config(void)
{
HAL_StatusTypeDef status;
@ -63,6 +73,56 @@ static void SystemClock_Config(void)
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_PLLCLK_DIV2, RCC_MCODIV_1);
}
static void SystemReset(void) {
uint32_t resetFlags = RCC->CSR;
/* Clear reset flags */
RCC->CSR |= RCC_CSR_RMVF;
#if defined(SYSTEM_MEMORY_BASE)
if (resetFlags & RCC_CSR_IWDGRSTF) {
/* Reset cause was watchdog, which is used for rebooting into the bootloader.
Set stack pointer to *SYSTEM_MEMORY_BASE
and jump to *(SYSTEM_MEMORY_BASE + 4)
https://stackoverflow.com/a/42031657 */
asm volatile (
" msr msp, %[sp] \n"
" bx %[pc] \n"
:: [sp] "r" (*( (uint32_t*)(SYSTEM_MEMORY_BASE) )),
[pc] "r" (*( (uint32_t*)(SYSTEM_MEMORY_BASE + 4) ))
);
}
#else
while(1)
;
#endif
/* Initialize HAL */
HAL_Init();
/* Enable Clock to SYSCFG */
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* Enable SWO debug output */
GPIO_InitTypeDef GpioSWOInit = {
.Pin = GPIO_PIN_3,
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_LOW,
.Alternate = GPIO_AF0_TRACE
};
HAL_GPIO_Init(GPIOB, &GpioSWOInit);
/* Reset USB if necessary */
if (!(resetFlags & RCC_CSR_PORRSTF)) {
/* Since the USB Pullup is hardwired to the supply voltage,
* the host (re-)enumerates our USB device only during Power-On-Reset.
* For all other reset causes, do a manual USB reset. */
USB_Reset();
}
}
int _write(int file, char *ptr, int len)
{
for (uint32_t i=0; i<len; i++) {
@ -74,20 +134,9 @@ int _write(int file, char *ptr, int len)
int main(void)
{
HAL_Init();
SystemReset();
SystemClock_Config();
__HAL_RCC_SYSCFG_CLK_ENABLE();
GPIO_InitTypeDef GpioSWOInit = {
.Pin = GPIO_PIN_3,
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_LOW,
.Alternate = GPIO_AF0_TRACE
};
HAL_GPIO_Init(GPIOB, &GpioSWOInit);
LED_Init();
LED_MODE(0, LED_MODE_SLOWPULSE2X);
LED_MODE(1, LED_MODE_SLOWPULSE2X);

Wyświetl plik

@ -121,6 +121,23 @@ void GPIO_Init(void)
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void USB_Reset(void)
{
/* pull USB DP pins low to simulate disconnect
to force the host to re-enumerate when a new program is loaded */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIOA->BRR = GPIO_PIN_12;
HAL_Delay(USB_RESET_DELAY);
}
void USB_Init(void)
{
__HAL_REMAPINTERRUPT_USB_ENABLE();

Wyświetl plik

@ -3,11 +3,14 @@
#include "usb_audio.h"
#include "usb_serial.h"
#include "usb_dfu.h"
#define USB_RESET_DELAY 100 /* milliseconds */
#define USB_SOF_TIMER TIM2
#define USB_SOF_TIMER_CNT TIM2->CNT
#define USB_SOF_TIMER_HZ 72000000UL
void USB_Reset(void);
void USB_Init(void);
void USB_Task(void);

Wyświetl plik

@ -112,7 +112,8 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t itf)
TUD_CONFIG_DESC_LEN + \
AIOC_AUDIO_DESC_LEN + \
AIOC_HID_DESC_LEN + \
AIOC_CDC_DESC_LEN \
AIOC_CDC_DESC_LEN + \
AIOC_DFU_RT_DESC_LEN \
)
uint8_t const desc_fs_configuration[] = {
@ -155,6 +156,16 @@ uint8_t const desc_fs_configuration[] = {
/* _epout */ EPNUM_CDC_0_OUT,
/* _epin */ EPNUM_CDC_0_IN,
/* _epsize */ CFG_TUD_CDC_EP_BUFSIZE
),
AIOC_DFU_RT_DESCRIPTOR(
/* _itfnum */ ITF_NUM_DFU_RT,
/* _stridx */ STR_IDX_DFU_RT,
/* _attr */ DFU_ATTR_WILL_DETACH | \
DFU_ATTR_CAN_UPLOAD | \
DFU_ATTR_CAN_DOWNLOAD,
/* _timeout */ 255, /* not used if WILL_DETACH */
/* _xfer_size */ 2048 /* max size for stm32 dfu bootloader */
)
};
@ -280,6 +291,10 @@ const uint16_t * tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
len = ascii_to_utf16(ptr, len, USB_STRING_HIDITF);
break;
case STR_IDX_DFU_RT:
len = ascii_to_utf16(ptr, len, USB_STRING_DFU_RT);
break;
default:
TU_ASSERT(0, NULL);
break;

Wyświetl plik

@ -9,6 +9,7 @@ enum USB_DESCRIPTORS_ITF {
ITF_NUM_HID, /* For CM108 compatibility make this interface #3 */
ITF_NUM_CDC_0,
ITF_NUM_CDC_0_DATA,
ITF_NUM_DFU_RT,
ITF_NUM_TOTAL
};
@ -26,7 +27,8 @@ enum USB_STRING_IDX {
STR_IDX_AUDIOINCHAN,
STR_IDX_AUDIOOUTCHAN,
STR_IDX_HIDITF,
STR_IDX_CDCITF
STR_IDX_CDCITF,
STR_IDX_DFU_RT
};
#define USB_VID 0x1209
@ -44,6 +46,7 @@ enum USB_STRING_IDX {
#define USB_STRING_AUDIOOUTCHAN "AIOC Audio Out Channel"
#define USB_STRING_CDCITF "AIOC CDC"
#define USB_STRING_HIDITF "AIOC HID"
#define USB_STRING_DFU_RT "AIOC DFU Runtime"
/* Endpoints */
#define EPNUM_AUDIO_IN 0x81
@ -55,7 +58,6 @@ enum USB_STRING_IDX {
#define EPNUM_CDC_0_IN 0x84
#define EPNUM_CDC_0_NOTIF 0x85
/* Custom Audio Descriptor.
* Courtesy of https://github.com/hathach/tinyusb/issues/1249#issuecomment-1148727765 */
#define AUDIO_CTRL_ID_SPK_INPUT_STREAM 0x01
@ -168,4 +170,8 @@ enum USB_STRING_IDX {
#define AIOC_CDC_DESCRIPTOR TUD_CDC_DESCRIPTOR
#define AIOC_DFU_RT_DESC_LEN TUD_DFU_RT_DESC_LEN
#define AIOC_DFU_RT_DESCRIPTOR TUD_DFU_RT_DESCRIPTOR
#endif /* USB_DESCRIPTORS_H_ */

Wyświetl plik

@ -0,0 +1,26 @@
#include "usb_dfu.h"
#include "stm32f3xx_hal.h"
#include "tusb.h"
#include "usb.h"
// Invoked on DFU_DETACH request to reboot to the bootloader
void tud_dfu_runtime_reboot_to_dfu_cb(void)
{
/* Reset USB and force reset via watchdog timer. */
USB_Reset();
IWDG_HandleTypeDef IWDGHandle = {
.Instance = IWDG,
.Init = {
.Prescaler = IWDG_PRESCALER_4,
.Reload = 128,
.Window = 0x0FFF
}
};
HAL_IWDG_Init(&IWDGHandle);
while(1) {
/* Wait for Reset */
}
}

Wyświetl plik

@ -0,0 +1,4 @@
#ifndef USB_DFU_H_
#define USB_DFU_H_
#endif /* USB_DFU_H_ */

Wyświetl plik

@ -0,0 +1,13 @@
dfu-suffix: $(OBJCOPY_BIN)
ifneq ("$(wildcard /usr/bin/dfu-suffix)","")
dfu-suffix -c $(FILENAME) || dfu-suffix -a $(FILENAME) -p $(PID) -v $(VID) -d $(DID)
else
@echo "Skipping DFU suffix generation, because dfu-suffix tool was not found"
endif
dfu-util: dfu-suffix
ifneq ("$(wildcard /usr/bin/dfu-util)","")
dfu-util -d $(VID):$(PID) -a 0 -s 0x08000000:leave -D $(FILENAME)
else
@echo "Skipping DFU programming, because dfu-util tool was not found"
endif