Editing the Variant.h file seems to be the simplest solution.
An alternative is to select a board with I2S activated by default. (and the same processor)
I got my XIAO running with I2S by selecting an Adafruit Itsybitsy M0 as board.
Advantage is you don’t need to worry about board updates that remove your modifications to Variants.h
Disadvantage is that it’s a small puzzle to find out on whitch XIAO pins the Itsybitsy pins are.
example program:
/*
This example generates a square wave based tone at a specified frequency
and sample rate. Then outputs the data using the I2S interface to a
MAX98357 I2S Amp Breakout board.
Circuit:
* Seeed studio XIAO
* MAX98357:
*
* GND connected GND
* VIN connected 5V
* LRC connected to pin D3
* BCLK connected to pin D2
* DIN connected to pin D8
created 17 November 2016
by Sandeep Mistry
Modified for Seeed studio XIAO
18 August 2022
by G.Wolters
*/
// intended for Seeed studio XIAO
// use Adafruit Itsybitsy M0 as Board setting to get I2S support
// ItsyBitsy M0 pin & functions Processor pin XIAO pin
// Dig/ Adc / special / int / touch / Sercom / Sercom alt / Timer / Timer alt
// 14 / A0 / Vout / 2 / Y0 / / / / PA02 D0
// 17 / A3 / Vrefb / 4 / Y2 / / / / PA04 D1
// 18 / A4 / / 5 / Y3 / / S0.1 / TCC0[1] / PA05 D9
// 8 / A10 / / 6 / Y4 / / S0.2 / TCC1[0] / PA06 D10
// 9 / A11 / I2S_SD0 / 7 / Y5 / / S0.3 / TCC1[1] / PA07 D8
// 4 / A8 / I2S_SD1 / NMI / / S0.0 / S2.0 / TCC0[0] / TCC1[2] PA08 D4
// 3 / A9 / I2S_MCK0 / 9 / / S0.1 / S2.0 / TCC0[1] / TCC1[3] PA09 D5
// 1 / A7 / I2S_SCK0 / 10 / / S0.2 / S2.2 / TCC1[0] / TCC0[2] PA10 D2
// 0 / A6 / I2S_FS0 / 11 / / S0.3 / S2.3 / TCC1[1] / TCC0[3] PA11 D3
// 15 / A1 / / 8 / Y14 / / S4.0 / TC4[0] / PB8 D6
// 16 / A2 / / 9 / Y15 / / S4.1 / TC4[1] / PB9 D7
// on-board leds, ON when output is LOW
// 13 PA17 yellow led
// 10 PA18 blue Rx led
// 12 PA19 blue Tx led
// Remark: PA06 is not used on the Itsybitsy, but is defined as 8 / A10
// Remark: Serial1 is using Sercom0 on PA10/PA11, these are available on the XIAO (D2/D3)
// Because PA10/PA11 are needed for I2s, this is not a usable configuration.
// Alternative is Sercom0 on PA4/PA5 Sercom0 on PA8/PA9 or Sercom4 on PB8/PB9
// Remark: I2C (wire) is using Sercom3 on PA22/PA23 which are not available on the XIAO
// If I2c is needed, Sercom4 on PB8/PB9 can be used
// Remark: SPI is using Sercom4 on PA12/PB10/PB11 which are not available on the XIAO
// If SPI is needed, Sercom0 on PA4/PA5/PA6 can be used.
#include <I2S.h>
const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 500; // amplitude of square wave
const int sampleRate = 8000; // sample rate in Hz
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
short sample = amplitude; // current sample value
int count = 0;
void setup() {
Serial.begin(9600);
Serial.println("I2S simple tone");
// start I2S at the sample rate with 16-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
}
void loop() {
if (count % halfWavelength == 0) {
// invert the sample every half wavelength count multiple to generate square wave
sample = -1 * sample;
}
// write the same sample twice, once for left and once for the right channel
I2S.write(sample);
I2S.write(sample);
// increment the counter for the next sample
count++;
}