Skip to content

Commit

Permalink
Port to Teensy4
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcombriat committed Nov 24, 2021
1 parent 6e6db73 commit 131d81e
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 33 deletions.
1 change: 1 addition & 0 deletions AudioConfigTeensy3_12bit.h
@@ -1,6 +1,7 @@
#ifndef AUDIOCONFIGTEENSY3_12BIT_H
#define AUDIOCONFIGTEENSY3_12BIT_H

#warning If you get a compilation error you should probably update Teensyduino to its latest version

/** @ingroup core
*/
Expand Down
17 changes: 17 additions & 0 deletions AudioConfigTeensy4_10bitPwm.h
@@ -0,0 +1,17 @@
#ifndef AUDIOCONFIGTEENSY4_10BITPWM_H
#define AUDIOCONFIGTEENSY4_10BITPWM_H

#warning If you get a compilation error you should probably update Teensyduino to its latest version

/** @ingroup core
*/
/* Used internally to put the 0-biased generated audio into the centre of the output range (10 bits on Teensy 4 using PWM) */
#define AUDIO_BIAS ((uint16_t) 512)
#define AUDIO_BITS 10

#define AUDIO_CHANNEL_1_PIN A8
#define AUDIO_CHANNEL_2_PIN A9


#endif // #ifndef AUDIOCONFIGTEENSY4_10BITPWM_H

13 changes: 13 additions & 0 deletions AudioOutput.h
Expand Up @@ -241,6 +241,19 @@ inline void audioOutput(const AudioOutput f)
#endif


///////////////////// TEENSY4
#if IS_TEENSY4()
#include "AudioConfigTeensy4_10bitPwm.h"
inline void audioOutput(const AudioOutput f)
{
analogWrite(AUDIO_CHANNEL_1_PIN, f.l()+AUDIO_BIAS);
#if (AUDIO_CHANNELS > 1)
analogWrite(AUDIO_CHANNEL_2_PIN, f.r()+AUDIO_BIAS);
#endif
}
#endif


///////////////////// STM32
#if IS_STM32()
#include "AudioConfigSTM32.h"
Expand Down
41 changes: 25 additions & 16 deletions MozziGuts.cpp
Expand Up @@ -27,7 +27,7 @@
#if IS_AVR()
#include "FrequencyTimer2.h"
#include "TimerOne.h"
#elif IS_TEENSY3()
#elif (IS_TEENSY3() || IS_TEENSY4())
// required from http://github.com/pedvide/ADC for Teensy 3.*
#include "IntervalTimer.h"
#include <ADC.h>
Expand All @@ -49,7 +49,7 @@ uint16_t output_buffer_size = 0;
"Mozzi has been tested with a cpu clock speed of 16MHz on Arduino and 48MHz on Teensy 3! Results may vary with other speeds."
#endif

#if IS_TEENSY3()
#if (IS_TEENSY3() || IS_TEENSY4())
ADC *adc; // adc object
uint8_t teensy_pin;
#elif IS_STM32()
Expand Down Expand Up @@ -150,8 +150,8 @@ uint8_t adc_count = 0;
int getAudioInput() { return audio_input; }

static void startFirstAudioADC() {
#if IS_TEENSY3()
adc->startSingleRead(
#if (IS_TEENSY3() || IS_TEENSY4())
adc->adc0->startSingleRead(
AUDIO_INPUT_PIN); // ADC lib converts pin/channel in startSingleRead
#elif IS_STM32()
uint8_t dummy = AUDIO_INPUT_PIN;
Expand All @@ -170,8 +170,8 @@ static void receiveFirstAudioADC()
*/

static void startSecondAudioADC() {
#if IS_TEENSY3()
adc->startSingleRead(AUDIO_INPUT_PIN);
#if (IS_TEENSY3() || IS_TEENSY4())
adc->adc0->startSingleRead(AUDIO_INPUT_PIN);
#elif IS_STM32()
uint8_t dummy = AUDIO_INPUT_PIN;
adc.setPins(&dummy, 1);
Expand All @@ -183,17 +183,17 @@ static void startSecondAudioADC() {

static void receiveSecondAudioADC() {
if (!input_buffer.isFull())
#if IS_TEENSY3()
input_buffer.write(adc->readSingle());
#if (IS_TEENSY3() || IS_TEENSY4())
input_buffer.write(adc->adc0->readSingle());
#elif IS_STM32()
input_buffer.write(adc.getData());
#else
input_buffer.write(ADC);
#endif
}

#if IS_TEENSY3() || IS_STM32() || IS_AVR()
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_STM32() || IS_AVR() || IS_TEENSY4()
#if IS_TEENSY3() || IS_TEENSY4()
void adc0_isr(void)
#elif IS_STM32()
void stm32_adc_eoc_handler()
Expand Down Expand Up @@ -362,7 +362,7 @@ static void CACHED_FUNCTION_ATTR defaultAudioOutput() {
#endif

#if (AUDIO_MODE == STANDARD) || (AUDIO_MODE == STANDARD_PLUS) || IS_STM32()
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
IntervalTimer timer1;
#elif IS_STM32() && (EXTERNAL_AUDIO_OUTPUT == true)
HardwareTimer audio_update_timer(2);
Expand Down Expand Up @@ -395,13 +395,22 @@ void samd21AudioOutput() {
#if !IS_AVR()
static void startAudioStandard() {

#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
adc->adc0->setAveraging(0);
adc->adc0->setConversionSpeed(
ADC_CONVERSION_SPEED::MED_SPEED); // could be HIGH_SPEED, noisier

ADC_CONVERSION_SPEED::MED_SPEED); // could be HIGH_SPEED, noisier
#if IS_TEENSY3()
analogWriteResolution(12);
timer1.begin(defaultAudioOutput, 1000000UL / AUDIO_RATE);
#elif IS_TEENSY4()
analogWriteResolution(10);
#if (!EXTERNAL_AUDIO_OUTPUT)
analogWriteFrequency(AUDIO_CHANNEL_1_PIN, 146484.38f);
#if (AUDIO_CHANNELS > 1)
analogWriteFrequency(AUDIO_CHANNEL_2_PIN, 146484.38f);
#endif // end #if (AUDIO_CHANNELS > 1)
#endif // end #if (!EXTERNAL_AUDIO_OUTPUT)
#endif
timer1.begin(defaultAudioOutput, 1000000. / AUDIO_RATE);
#elif IS_SAMD21()
#ifdef ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
{
Expand Down Expand Up @@ -702,7 +711,7 @@ void startMozzi(int control_rate_hz) {
}

void stopMozzi() {
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
timer1.end();
#elif IS_STM32()
audio_update_timer.pause();
Expand Down
4 changes: 3 additions & 1 deletion MozziGuts.h
Expand Up @@ -22,7 +22,7 @@

#include "hardware_defines.h"

#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
// required from http://github.com/pedvide/ADC for Teensy 3.*
#include <ADC.h>
#endif
Expand Down Expand Up @@ -191,6 +191,8 @@ HIFI is not available/not required on Teensy 3.* or ARM.
#if (EXTERNAL_AUDIO_OUTPUT != true)
#if IS_TEENSY3()
#include "AudioConfigTeensy3_12bit.h"
#elif IS_TEENSY4()
#include "AudioConfigTeensy4_10bitPwm.h"
#elif IS_STM32()
#include "AudioConfigSTM32.h"
#elif IS_ESP8266()
Expand Down
Expand Up @@ -79,7 +79,7 @@ void setup() {

// Initialising the SPI connection on default port
SPI.begin();
SPI.beginTransaction(SPISettings(200000000, MSBFIRST, SPI_MODE0)); //MSB first, according to the DAC spec
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0)); //MSB first, according to the DAC spec


aCos1.setFreq(440.f);
Expand Down
Expand Up @@ -73,7 +73,7 @@ void setup() {


mySPI.begin();
mySPI.beginTransaction(SPISettings(200000000, MSBFIRST, SPI_MODE0)); //MSB first, according to the DAC spec
mySPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0)); //MSB first, according to the DAC spec


aCos1.setFreq(440.f);
Expand Down
7 changes: 4 additions & 3 deletions hardware_defines.h
Expand Up @@ -14,12 +14,13 @@

#define IS_AVR() (defined(__AVR__)) // "Classic" Arduino boards
#define IS_SAMD21() (defined(ARDUINO_ARCH_SAMD))
#define IS_TEENSY3() (defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__) ) // 32bit arm-based Teensy
#define IS_STM32() (defined(__arm__) && !IS_TEENSY3() && !IS_SAMD21()) // STM32 boards (note that only the maple based core is supported at this time. If another cores is to be supported in the future, this define should be split.
#define IS_TEENSY3() (defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__)) // 32bit arm-based Teensy
#define IS_TEENSY4() (defined(__IMXRT1062__)) // Teensy4 (no DAC)
#define IS_STM32() (defined(__arm__) && !IS_TEENSY3() && !IS_SAMD21() && !IS_TEENSY4()) // STM32 boards (note that only the maple based core is supported at this time. If another cores is to be supported in the future, this define should be split.
#define IS_ESP8266() (defined(ESP8266))
#define IS_ESP32() (defined(ESP32))

#if !(IS_AVR() || IS_TEENSY3() || IS_STM32() || IS_ESP8266() || IS_SAMD21() || IS_ESP32())
#if !(IS_AVR() || IS_TEENSY3() || IS_TEENSY4() || IS_STM32() || IS_ESP8266() || IS_SAMD21() || IS_ESP32())
#error Your hardware is not supported by Mozzi or not recognized. Edit hardware_defines.h to proceed.
#endif

Expand Down
21 changes: 11 additions & 10 deletions mozzi_analog.cpp
Expand Up @@ -17,15 +17,15 @@
//#include "mozzi_utils.h"

#include "hardware_defines.h"
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
// required from http://github.com/pedvide/ADC for Teensy 3.*
#include <ADC.h>
#elif IS_STM32()
//#include <STM32ADC.h>
#endif

// defined in Mozziguts.cpp
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
extern ADC *adc; // adc object
extern uint8_t teensy_pin;
#elif IS_STM32()
Expand Down Expand Up @@ -69,9 +69,10 @@ void adcEnableInterrupt(){


void setupMozziADC(int8_t speed) {
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
adc = new ADC();
adc->adc0->enableInterrupts(ADC_0);
//adc->adc0->enableInterrupts(ADC_0);
adc->adc0->enableInterrupts(adc0_isr);
#elif IS_STM32()
adc.calibrate();
setupFastAnalogRead(speed);
Expand Down Expand Up @@ -170,7 +171,7 @@ static void adcSetChannel(uint8_t channel) {
// basically analogRead() chopped in half so the ADC conversion
// can be started here and received by another function.
void adcStartConversion(uint8_t channel) {
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
teensy_pin = channel; // remember for second startSingleRead
adc->startSingleRead(teensy_pin); // channel/pin gets converted every time in startSingleRead
#elif IS_STM32()
Expand Down Expand Up @@ -249,7 +250,7 @@ void receiveFirstControlADC(){


void startSecondControlADC() {
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
adc->startSingleRead(teensy_pin);
#elif IS_STM32()
adc.setPins(&stm32_current_adc_pin, 1);
Expand All @@ -261,8 +262,8 @@ void startSecondControlADC() {


void receiveSecondControlADC(){
#if IS_TEENSY3()
analog_readings[current_channel] = adc->readSingle();
#if IS_TEENSY3() || IS_TEENSY4()
analog_readings[current_channel] = adc->adc0->readSingle();
#elif IS_STM32()
analog_readings[current_channel] = adc.getData();
#elif IS_AVR()
Expand All @@ -278,14 +279,14 @@ because the first conversion after changing channels is often inaccurate (on atm
The version for USE_AUDIO_INPUT==true is in MozziGuts.cpp... compilation reasons...
*/
#if(USE_AUDIO_INPUT==false)
#if IS_TEENSY3()
#if IS_TEENSY3() || IS_TEENSY4()
void adc0_isr(void)
#elif IS_STM32()
void stm32_adc_eoc_handler()
#elif IS_AVR()
ISR(ADC_vect, ISR_BLOCK)
#endif
#if IS_TEENSY3() || IS_STM32() || IS_AVR()
#if IS_TEENSY3() || IS_STM32() || IS_AVR() || IS_TEENSY4()
{
if (first)
{
Expand Down
8 changes: 8 additions & 0 deletions mozzi_analog.h
Expand Up @@ -18,6 +18,8 @@
#include "WProgram.h"
#endif

#include "hardware_defines.h"

#if (USE_AUDIO_INPUT==true)
#warning "Using AUDIO_INPUT_PIN defined in mozzi_config.h for audio input."
#endif
Expand Down Expand Up @@ -182,4 +184,10 @@ void adcStartReadCycle();

uint8_t adcPinToChannelNum(uint8_t pin);


#if IS_TEENSY3() || IS_TEENSY4()
void adc0_isr(void);
#endif


#endif /* MOZZI_ANALOG_H_ */
2 changes: 1 addition & 1 deletion mozzi_config.h
Expand Up @@ -79,7 +79,7 @@ This sets which analog input channel to use for audio input, if you have
\#define USE_AUDIO_INPUT true
in mozz_config.h
*/
#define AUDIO_INPUT_PIN 0
//#define AUDIO_INPUT_PIN 11

//AUDIO_INPUT_CHANNEL = analogPinToChannel(AUDIO_INPUT_PIN)

Expand Down

0 comments on commit 131d81e

Please sign in to comment.