This commit is contained in:
Steven Dan
2025-12-11 09:43:42 +08:00
commit d8b2974133
1822 changed files with 280037 additions and 0 deletions

43
lib_xua/lib_xua/api/xua.h Normal file
View File

@@ -0,0 +1,43 @@
// Copyright 2017-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_H_
#define _XUA_H_
#include <xs1.h>
#ifndef __ASSEMBLER__
#include <xccompat.h>
/* Missing items from xccompat.h */
#if defined( __XC__)
#define NULLABLE_CLIENT_INTERFACE(tag, name) client interface tag ?name
#define NULLABLE_SERVER_INTERFACE(tag, name) server interface tag ?name
typedef in port in_port_t;
typedef out port out_port_t;
#elif defined(__STDC__) || defined(__DOXYGEN__)
#define NULLABLE_CLIENT_INTERFACE(type, name) unsigned *name
#define NULLABLE_SERVER_INTERFACE(tag, name) unsigned *name
typedef unsigned clock;
typedef unsigned in_port_t;
typedef unsigned out_port_t;
#endif
#endif
#include "xua_conf_full.h"
#ifndef __ASSEMBLER__
#include "xua_audiohub.h"
#include "xua_endpoint0.h"
#include "xua_buffer.h"
#include "xua_mixer.h"
#endif
#ifdef __XC__
#include "xua_clocking.h"
#include "xua_midi.h"
#if XUA_NUM_PDM_MICS > 0
#include "xua_pdm_mic.h"
#endif
#endif
#endif

View File

@@ -0,0 +1,109 @@
// Copyright 2011-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_AUDIOHUB_H_
#define _XUA_AUDIOHUB_H_
#if(defined __XC__ || defined __DOXYGEN__)
#include "xua.h"
#if XUA_USB_EN
#include "dfu_interface.h"
#endif
#if CODEC_MASTER
#define i2s_clk_port_type in_buffered_port_32_t
#else
#define i2s_clk_port_type out_buffered_port_32_t
#endif
/** The audio driver thread.
*
* This function drives I2S ports and handles samples to/from other digital
* I/O threads.
*
* \param c_aud Audio sample channel connected to the mixer() thread or the
* decouple() thread
*
* \param clk_audio_mclk Nullable clockblock to be clocked from master clock
*
* \param clk_audio_bclk Nullable clockblock to be clocked from i2s bit clock
*
* \param p_mclk_in Master clock inport port (must be 1-bit). Use null when xcore is slave
*
* \param p_lrclk Nullable port for I2S sample clock
*
* \param p_bclk Nullable port for I2S bit clock
*
* \param p_i2s_dac Nullable array of ports for I2S data output lines
*
* \param p_i2s_adc Nullable array of ports for I2S data input lines
*
* \param i_SoftPll Interface to software PLL task
*
* \param c_spdif_tx Channel connected to S/PDIF transmitter core from lib_spdif
*
* \param c_dig Channel connected to the clockGen() thread for
* receiving/transmitting samples
*
* \param c_audio_rate_change Channel notifying ep_buffer of an mclk frequency change and sync for stable clock
*
* \param dfuInterface Interface supporting DFU methods
*
* \param c_pdm_in Channel for receiving decimated PDM samples
*/
void XUA_AudioHub(
NULLABLE_RESOURCE(chanend, c_aud),
NULLABLE_RESOURCE(clock, clk_audio_mclk),
NULLABLE_RESOURCE(clock, clk_audio_bclk), NULLABLE_RESOURCE(in_port_t, p_mclk_in)
, NULLABLE_RESOURCE(i2s_clk_port_type, p_lrclk)
, NULLABLE_RESOURCE(i2s_clk_port_type, p_bclk)
, NULLABLE_ARRAY_OF_SIZE(out_buffered_port_32_t, p_i2s_dac, I2S_WIRES_DAC)
, NULLABLE_ARRAY_OF_SIZE(in_buffered_port_32_t, p_i2s_adc, I2S_WIRES_ADC)
#if (XUA_SPDIF_TX_EN) || defined(__DOXYGEN__)
, chanend c_spdif_tx
#endif
#if (XUA_SPDIF_RX_EN || XUA_ADAT_RX_EN || defined(__DOXYGEN__))
, chanend c_dig
#endif
#if (XUA_SYNCMODE == XUA_SYNCMODE_SYNC || XUA_SPDIF_RX_EN || XUA_ADAT_RX_EN || defined(__DOXYGEN__))
, chanend c_audio_rate_change
#endif
#if (((XUD_TILE != 0) && (AUDIO_IO_TILE == 0) && (XUA_DFU_EN == 1)) || defined(__DOXYGEN__))
, NULLABLE_SERVER_INTERFACE(i_dfu, dfuInterface)
#endif
#if (XUA_NUM_PDM_MICS > 0 || defined(__DOXYGEN__))
, chanend c_pdm_in
#endif
);
void SpdifTxWrapper(chanend c_spdif_tx);
#endif // __XC__
/**
* @brief User buffer management code
*
* This function is called at the sample rate of the USB Audio stack (e.g,. 48 kHz) and between the two parameter arrays
* contain a full multi-channel audio-frame. The first array carries all the data that has been received from the USB host
* and is to be presented to the audio interfaces. The second array carries all the data received from the interfaces and
* is to be presented to the USB host. The user can chose to intercept and overwrite the samples stored in these arrays.
*
* \param sampsFromUsbToAudio Samples received from USB host and to be presented to audio interfaces
*
* \param sampsFromAudioToUsb Samples received from the audio interfaces and to be presented to the USB host
*/
void UserBufferManagement(unsigned sampsFromUsbToAudio[], unsigned sampsFromAudioToUsb[]);
/**
* @brief User buffer managment init code
*
* This function is called once, before the first call to UserBufferManagement(), and can be used to initialise any
* related user state
*
* \param sampFreq The initial sample frequency
*
*/
void UserBufferManagementInit(unsigned sampFreq);
#endif // _XUA_AUDIOHUB_H_

View File

@@ -0,0 +1,123 @@
// Copyright 2011-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_BUFFER_H_
#define _XUA_BUFFER_H_
#if (__XC__ || defined __DOXYGEN__)
#include "xccompat.h"
#include "xua_clocking.h" /* Required for pll_ref_if */
/** USB Audio Buffering Core(s).
*
* This function buffers USB audio data between the XUD and the audio subsystem.
* Most of the chanend parameters to the function should be connected to
* XUD_Manager(). The uses two cores.
*
* \param c_aud_out Audio OUT endpoint channel connected to the XUD
* \param c_aud_in Audio IN endpoint channel connected to the XUD
* \param c_aud_fb Audio feedback endpoint channel connected to the XUD
* \param c_midi_from_host MIDI OUT endpoint channel connected to the XUD
* \param c_midi_to_host MIDI IN endpoint channel connected to the XUD
* \param c_midi Channel connected to MIDI core
* \param c_int Audio clocking interrupt endpoint channel connected to the XUD
* \param c_clk_int Optional chanend connected to the clockGen() thread if present
* \param c_sof Start of frame channel connected to the XUD
* \param c_aud_ctl Audio control channel connected to Endpoint0()
* \param p_off_mclk A port that is clocked of the MCLK input (not the MCLK input itself)
* \param c_aud Channel connected to XUA_AudioHub() core
* \param c_audio_rate_change Channel to notify and synchronise on audio rate change
* \param i_pll_ref Interface to task that toggles reference pin to CS2100
* \param c_swpll_update Channel connected to software PLL task. Expects master clock counts based on USB frames.
*/
void XUA_Buffer(
#if (NUM_USB_CHAN_OUT > 0)
chanend c_aud_out,
#endif
#if (NUM_USB_CHAN_IN > 0) || defined(__DOXYGEN__)
chanend c_aud_in,
#endif
#if ((NUM_USB_CHAN_OUT > 0) && ((NUM_USB_CHAN_IN == 0) || defined(UAC_FORCE_FEEDBACK_EP))) || defined(__DOXYGEN__)
chanend c_aud_fb,
#endif
#if defined(MIDI) || defined(__DOXYGEN__)
chanend c_midi_from_host,
chanend c_midi_to_host,
chanend c_midi,
#endif
#if XUA_SPDIF_RX_EN || XUA_ADAT_RX_EN || defined(__DOXYGEN__)
NULLABLE_RESOURCE(chanend, c_int),
NULLABLE_RESOURCE(chanend, c_clk_int),
#endif
chanend c_sof,
chanend c_aud_ctl,
in_port_t p_off_mclk
#if (HID_CONTROLS)
, chanend c_hid
#endif
, chanend c_aud
#if (XUA_SYNCMODE == XUA_SYNCMODE_SYNC) || defined(__DOYXGEN__)
, chanend c_audio_rate_change
#if (!XUA_USE_SW_PLL) || defined(__DOXYGEN__)
, CLIENT_INTERFACE(pll_ref_if, i_pll_ref)
#endif
#if (XUA_USE_SW_PLL) || defined(__DOXYGEN__)
, chanend c_swpll_update
#endif
#endif
);
void XUA_Buffer_Ep(
#if (NUM_USB_CHAN_OUT > 0)
chanend c_aud_out,
#endif
#if (NUM_USB_CHAN_IN > 0) || defined(__DOXYGEN__)
chanend c_aud_in,
#endif
#if ((NUM_USB_CHAN_OUT > 0) && ((NUM_USB_CHAN_IN == 0) || defined(UAC_FORCE_FEEDBACK_EP))) || defined(__DOXYGEN__)
chanend c_aud_fb,
#endif
#ifdef MIDI
chanend c_midi_from_host,
chanend c_midi_to_host,
chanend c_midi,
#endif
#if (XUA_SPDIF_RX_EN) || (XUA_ADAT_RX_EN)
chanend ?c_int,
chanend ?c_clk_int,
#endif
chanend c_sof,
chanend c_aud_ctl,
in port p_off_mclk
#if (HID_CONTROLS)
, chanend c_hid
#endif
#ifdef CHAN_BUFF_CTRL
, chanend c_buff_ctrl
#endif
#if (XUA_SYNCMODE == XUA_SYNCMODE_SYNC) || defined(__DOYXGEN__)
, chanend c_audio_rate_change
#if (!XUA_USE_SW_PLL) || defined(__DOXYGEN__)
, client interface pll_ref_if i_pll_ref
#endif
#if (XUA_USE_SW_PLL) || defined(__DOXYGEN__)
, chanend c_swpll_update
#endif
#endif
);
/** Manage the data transfer between the USB audio buffer and the
* Audio I/O driver.
*
* \param c_audio_out Channel connected to the audio() or mixer() threads
*/
void XUA_Buffer_Decouple(chanend c_audio_out
#ifdef CHAN_BUFF_CTRL
, chanend c_buff_ctrl
#endif
);
#endif
#endif

View File

@@ -0,0 +1,50 @@
// Copyright 2011-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _CLOCKING_H_
#define _CLOCKING_H_
#include <xs1.h>
#include "sw_pll_wrapper.h"
interface pll_ref_if
{
void toggle();
void init();
void toggle_timed(int relative);
};
[[distributable]]
void PllRefPinTask(server interface pll_ref_if i_pll_ref, out port p_sync);
/** Clock generation and digital audio I/O handling.
*
* \param c_spdif_rx channel connected to S/PDIF receive thread
* \param c_adat_rx channel connect to ADAT receive thread
* \param i_pll_ref interface to taslk that outputs clock signal to drive external frequency synthesizer
* \param c_audio channel connected to the audio() thread
* \param c_clk_ctl channel connected to Endpoint0() for configuration of the
* clock
* \param c_clk_int channel connected to the decouple() thread for clock
* interrupts
* \param c_audio_rate_change channel to notify of master clock change
* \param p_for_mclk_count_aud port used for counting mclk and providing a timestamp
* \param c_sw_pll channel used to communicate with software PLL task
*
*/
void clockGen( NULLABLE_RESOURCE(streaming_chanend_t, c_spdif_rx),
NULLABLE_RESOURCE(streaming_chanend_t, c_adat_rx),
CLIENT_INTERFACE(pll_ref_if, i_pll_ref),
chanend c_audio,
chanend c_clk_ctl,
chanend c_clk_int,
chanend c_audio_rate_change
#if (XUA_USE_SW_PLL || defined __DOYXGEN__)
, port p_for_mclk_count_aud
, chanend c_sw_pll
#endif
);
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
// Copyright 2017-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_CONF_FULL_H_
#define _XUA_CONF_FULL_H_
#ifdef __xua_conf_h_exists__
#include "xua_conf.h"
#endif
#include "xua_conf_default.h"
#endif

View File

@@ -0,0 +1,146 @@
// Copyright 2011-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_ENDPOINT0_H_
#define _XUA_ENDPOINT0_H_
#include "dfu_interface.h"
#include "vendorrequests.h"
#include "xccompat.h"
#if (__XC__ || defined __DOXYGEN__)
/** Function implementing Endpoint 0 for enumeration, control and configuration
* of USB audio devices. It uses the descriptors defined in ``xua_ep0_descriptors.h``.
*
* \param c_ep0_out Chanend connected to the XUD_Manager() out endpoint array
* \param c_ep0_in Chanend connected to the XUD_Manager() in endpoint array
* \param c_aud_ctl Chanend connected to the decouple thread for control
* audio (sample rate changes etc.). Note when nulled, the
* audio device only supports single sample rate/format and
* DFU is not supported either since this channel is used
* to carry messages about format, rate and DFU state
* \param c_mix_ctl Optional chanend to be connected to the mixer core(s) if
* present
* \param c_clk_ctl Optional chanend to be connected to the clockgen core if
* present
* \param c_EANativeTransport_ctrl Optional chanend to be connected to EA Native
* endpoint manager if present
* \param dfuInterface Interface to DFU task (this task must be run on a tile
* connected to boot flash.
*/
void XUA_Endpoint0(chanend c_ep0_out,
#if (USE_EX3D == 1) && (HID_CONTROLS == 1)
chanend c_ep0_in, chanend c_hidRcvData, NULLABLE_RESOURCE(chanend, c_aud_ctl),
#else
chanend c_ep0_in, NULLABLE_RESOURCE(chanend, c_aud_ctl),
#endif
NULLABLE_RESOURCE(chanend, c_mix_ctl), NULLABLE_RESOURCE(chanend, c_clk_ctl),
NULLABLE_RESOURCE(chanend, c_EANativeTransport_ctrl), NULLABLE_CLIENT_INTERFACE(i_dfu, dfuInterface)
#if !defined(__DOXYGEN__)
VENDOR_REQUESTS_PARAMS_DEC_
#endif
);
/** Function to set the Vendor ID value
*
* \param vid vendor ID value to set
*/
void XUA_Endpoint0_setVendorId(unsigned short vid);
/** Function to set the Product ID value
*
* \param pid Product ID value to set
*/
void XUA_Endpoint0_setProductId(unsigned short pid);
/** Function to set the Vendor string
*
* \param vendor_str Vendor string to set
*/
#ifdef __XC__
void XUA_Endpoint0_setVendorStr(char * unsafe vendor_str);
#else
void XUA_Endpoint0_setVendorStr(char * vendor_str);
#endif
/** Function to set the Product string
*
* \param product_str Product string to set
*/
#ifdef __XC__
void XUA_Endpoint0_setProductStr(char * unsafe product_str);
#else
void XUA_Endpoint0_setProductStr(char * product_str);
#endif
/** Function to set the Serial string
*
* \param serial_str Serial string to set
*/
#ifdef __XC__
void XUA_Endpoint0_setSerialStr(char * unsafe serial_str);
#else
void XUA_Endpoint0_setSerialStr(char * serial_str);
#endif
/** Function to set the BCD device
*
* \param bcdDevice BCD device to set
*/
void XUA_Endpoint0_setBcdDevice(unsigned short bcdDevice);
/** Function to get the Vendor string
*
* \return vendor string
*/
#ifdef __XC__
char * unsafe XUA_Endpoint0_getVendorStr();
#else
char * XUA_Endpoint0_getVendorStr();
#endif
/** Function to get the Product string
*
* \return Product string
*/
#ifdef __XC__
char * unsafe XUA_Endpoint0_getProductStr();
#else
char * XUA_Endpoint0_getProductStr();
#endif
/** Function to get the Serial Number string
*
* \return Serial string
*/
#ifdef __XC__
char * unsafe XUA_Endpoint0_getSerialStr();
#else
char * XUA_Endpoint0_getSerialStr();
#endif
/** Function to get the Vendor ID
*
* \return Vendor ID
*/
unsigned short XUA_Endpoint0_getVendorId();
/** Function to get the Product ID
*
* \return Product ID
*/
unsigned short XUA_Endpoint0_getProductId();
/** Function to get the BCD device
*
* \return BCD device
*/
unsigned short XUA_Endpoint0_getBcdDevice();
#endif
#endif

View File

@@ -0,0 +1,82 @@
// Copyright 2011-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_MIDI_H_
#define _XUA_MIDI_H_
#include "xua.h"
#ifndef MIDI_SHIFT_TX
#define MIDI_SHIFT_TX (0)
#endif
/** USB MIDI I/O task.
*
* This function passes MIDI data between XUA_Buffer and MIDI UART I/O.
*
* \param p_midi_in 1-bit input port for MIDI
* \param p_midi_out 1-bit output port for MIDI
* \param clk_midi Clock block used for clockin the UART; should have
* a rate of 100MHz
* \param c_midi Chanend connected to the decouple() thread
* \param cable_number The cable number of the MIDI implementation.
* This should be set to 0.
**/
void usb_midi(
#if (MIDI_RX_PORT_WIDTH == 4)
NULLABLE_RESOURCE(in_buffered_port_4_t, p_midi_in),
#else
NULLABLE_RESOURCE(in_buffered_port_1_t, p_midi_in),
#endif
NULLABLE_RESOURCE(port, p_midi_out),
NULLABLE_RESOURCE(clock, clk_midi),
NULLABLE_RESOURCE(chanend, c_midi),
unsigned cable_number
#ifdef IAP
, chanend ?c_iap, chanend ?c_i2c, // iOS stuff
port ?p_scl, port ?p_sda
#endif
);
#define MAX_USB_MIDI_PACKET_SIZE 1024
#define MIDI_USB_BUFFER_FROM_HOST_FIFO_SIZE (512+1024)
#define MIDI_USB_BUFFER_TO_HOST_SIZE (256)
#define MIDI_ACK 20
#define USB_MIDI_DEVICE_OUT_FIFO_SIZE (1024)
#ifdef __MIDI_IMPL
#define INLINE
#else
#define INLINE inline
#endif
#ifdef NO_INLINE_MIDI_SELECT_HANDLER
#pragma select handler
void midi_get_ack_or_data(chanend c, int &is_ack, unsigned int &datum);
#else
#pragma select handler
INLINE void midi_get_ack_or_data(chanend c, int &is_ack, unsigned int &datum) {
if (testct(c)) {
is_ack = 1;
chkct(c, XS1_CT_END);
}
else {
is_ack = 0;
datum = inuint(c);
chkct(c, XS1_CT_END);
}
}
#endif
INLINE void midi_send_ack(chanend c) {
outct(c, XS1_CT_END);
}
INLINE void midi_send_data(chanend c, unsigned int datum) {
outuint(c, datum);
outct(c, XS1_CT_END);
}
#define MIDI_RATE (31250)
#define MIDI_BITTIME (XS1_TIMER_MHZ * 1000000 / MIDI_RATE)
#define MIDI_BITTIME_2 (MIDI_BITTIME>>1)
#endif // _XUA_MIDI_H_

View File

@@ -0,0 +1,46 @@
// Copyright 2011-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef _XUA_MIXER_H_
#define _XUA_MIXER_H_
#include "xua.h"
enum mix_ctl_cmd {
SET_SAMPLES_TO_HOST_MAP,
SET_SAMPLES_TO_DEVICE_MAP,
SET_MIX_MULT,
SET_MIX_MAP,
SET_MIX_IN_VOL,
SET_MIX_OUT_VOL,
GET_INPUT_LEVELS,
GET_STREAM_LEVELS,
GET_OUTPUT_LEVELS
};
/** Digital sample mixer.
*
* This thread mixes audio streams between the decouple() thread and
* the audio() thread.
*
* \param c_to_host a chanend connected to the decouple() thread for
* receiving/transmitting samples
* \param c_to_audio a chanend connected to the audio() thread for
* receiving/transmitting samples
* \param c_mix_ctl a chanend connected to the Endpoint0() thread for
* receiving control commands
*
*/
void mixer(chanend c_to_host, chanend c_to_audio, chanend c_mix_ctl);
#define XUA_MIXER_OFFSET_OUT (0)
#define XUA_MIXER_OFFSET_IN (NUM_USB_CHAN_OUT)
#define XUA_MIXER_OFFSET_MIX (NUM_USB_CHAN_OUT + NUM_USB_CHAN_IN)
#define XUA_MIXER_OFFSET_OFF (NUM_USB_CHAN_OUT + NUM_USB_CHAN_IN + MAX_MIX_COUNT)
/* Defines uses for DB to actual muliplier conversion */
#define XUA_MIXER_MULT_FRAC_BITS (25)
#define XUA_MIXER_DB_FRAC_BITS (8)
#define XUA_MIXER_MAX_MULT (1<<XUA_MIXER_MULT_FRAC_BITS) /* i.e. multiply by 0 */
#endif

View File

@@ -0,0 +1,52 @@
// Copyright 2015-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef XUA_PDM_MIC_H
#define XUA_PDM_MIC_H
#include <xccompat.h>
#include <stdint.h>
#include "mic_array_conf.h"
#include "mic_array.h"
#ifdef __cplusplus
extern "C" {
#endif
void ma_init(unsigned mic_samp_rate);
void ma_task(chanend c_mic_to_audio);
#ifdef __cplusplus
}
#endif
/** USB PDM Mic task.
*
* This task runs the PDM rx and decimators and passes PCM samples to XUA.
* It runs forever and currently supports a single sample rate of
* 48 kHz, 32 kHz or 16 kHz
*
* \param c_mic_to_audio 1-bit input port for MIDI
*
**/
void mic_array_task(chanend c_mic_to_audio);
/** User pre-PDM mic function callback (optional).
* Use to initialise any PDM related hardware.
*
**/
void user_pdm_init();
/** USB PDM Mic PCM sample post processing callback (optional).
*
* This is called after the raw PCM samples are received from mic_array.
* It can be used to modify the samples (gain, filter etc.) before sending
* to XUA audiohub. Please note this is called from Audiohub (I2S) and
* so any processing must take significantly less than on half of a sample
* period else I2S will break timing.
*
* \param mic_audio Array of samples for in-place processing
*
**/void user_pdm_process(int32_t mic_audio[MIC_ARRAY_CONFIG_MIC_COUNT]);
#endif

View File

@@ -0,0 +1,19 @@
// Copyright 2017-2024 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#ifndef __XUA_API_H__
#define __XUA_API_H__
#include <stdint.h>
void set_usb_to_device_rate(uint32_t rate);
void set_device_to_usb_rate(uint32_t rate);
void set_usb_to_device_bit_res(uint32_t rate);
void set_device_to_usb_bit_res(uint32_t rate);
uint32_t get_usb_to_device_rate();
uint32_t get_device_to_usb_rate();
uint32_t get_usb_to_device_bit_res();
uint32_t get_device_to_usb_bit_res();
#endif //__XUA_API_H__