圏9研究所 工作室

圏9研究所の開発情報資料など

STM32F411 BlackPill USB Speaker ISO(5) コード2/2

3)usbd_sp
(1)<usbd_sp.h>

/**
 Project Name: STM170_USB_SP_ISO

 File Name: usbd_sp.h

 Description:
 USB speaker with isochronous transfer
 Generation Information :
 Device			:  STM32F411CEU6
 Version			:  170
 Created on		: 2023/11/24
 */
/**
 Copyright (c) 2023 luke24e-hbid

 This software is released under the MIT License.
 http://opensource.org/licenses/mit-license.php
 */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef INC_USBD_SP_H_
#define INC_USBD_SP_H_

/* Private defines -----------------------------------------------------------*/
#define SYSTEM_FREQ 96000000U
#ifndef USBD_AUDIO_FREQ
/* AUDIO Class Config */
#define USBD_AUDIO_FREQ 48000U
#endif /* USBD_AUDIO_FREQ */
#define DAC_BUF_SIZE ((USBD_AUDIO_FREQ * AUDIO_OUT_PACKET_NUM) / 1000U)
#define TIM2_P_BASE ((uint32_t)(SYSTEM_FREQ / USBD_AUDIO_FREQ))
// #define TIM2_P_BASE			2000
#define AUDIO_ISO_FS_INIT 48000U
#define AUDIO_ISO_FS_DMAX 750U
#define TIM1_P_BASE DAC_BUF_SIZE
#define DMA_BUF_SIZE2 ((uint16_t)(DAC_BUF_SIZE * 2))
#define DAC_BUF_SIZE4 ((int32_t)(DAC_BUF_SIZE * 4))

// Isochronous Async endpoint
/* Input endpoint is for feedback. See USB 1.1 Spec, 5.10.4.2 Feedback. */
#define AUDIO_IN_PACKET 3U
#define USBD_EP_TYPE_ISOC_ASYNC 0x05U
#define AUDIO_IN_EP 0x81U
#define SOF_RATE 0x02U // bRefresh 4ms = 2^2

// Main Sequence enum
typedef enum
{
	SEQ_IDLE,
	SEQ_START,
	SEQ_PLAY,
	SEQ_PAUSE,
} SEQ_MAIN;

// USBD AUDIO DataOut Sequence enum
typedef enum
{
	RD_IDLE,
	RD_START,
	RD_PLAY,
} SEQ_RDEN;

// USBD AUDIO DataOut wr_ptr status
typedef enum
{
	WR_PTR_IDLE,
	WR_PTR_1st,
	WR_PTR_INQ,
} STATE_WR_PTR;

// USBD AUDIO Data decode union
typedef union
{
	int16_t i16t;
	uint8_t u8t[2];
} U8T2I16T;

// ISO Control Value
typedef struct
{
	uint32_t fs;							 // ISO Feed Back Value
	volatile uint32_t tx_flag; // ISO busy flag
	volatile uint32_t fnsof;	 // OTG_FS_DSTS.FNSOF:Frame number of the received SOF
	uint32_t ofs_packet;			 // Offset of Packet
	uint16_t usbintn;					 // Number of USB INT.
	//	uint16_t			ofs_dac;		// Offset of DAC buffer
	//	int32_t				ofs_diff;		// Difference between Packet and DAC buffer
} ISO_ControlTypeDef;

/* Function prototype declaration ------------------------------------------*/
STATE_WR_PTR usbd_audio_wrinq(STATE_WR_PTR stin);
void usbd_sp_iso(void);

#endif /* INC_USBD_SP_H_ */