Mozzi  version v1.1.0
sound synthesis library for Arduino
MozziGuts_impl_SAMD.hpp
1 /*
2  * MozziGuts.cpp
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi by Tim Barrass is licensed under a Creative Commons
9  * Attribution-NonCommercial-ShareAlike 4.0 International License.
10  *
11  */
12 
13 #if !(IS_SAMD21())
14 # error "Wrong implementation included for this platform"
15 #endif
16 
17 ////// BEGIN analog input code ////////
18 //#define MOZZI_FAST_ANALOG_IMPLEMENTED // not yet
19 #define getADCReading() 0
20 #define channelNumToIndex(channel) channel
21 uint8_t adcPinToChannelNum(uint8_t pin) {
22  return pin;
23 }
24 void adcStartConversion(uint8_t channel) {
25 #warning Fast analog read not implemented on this platform
26 }
27 void startSecondADCReadOnCurrentChannel() {
28 #warning Fast analog read not implemented on this platform
29 }
30 void setupFastAnalogRead(int8_t speed) {
31 #warning Fast analog read not implemented on this platform
32 }
33 void setupMozziADC(int8_t speed) {
34 #warning Fast analog read not implemented on this platform
35 }
36 ////// END analog input code ////////
37 
38 
39 
40 //// BEGIN AUDIO OUTPUT code ///////
41 // These are ARM SAMD21 Timer 5 routines to establish a sample rate interrupt
42 static bool tcIsSyncing() {
43  return TC5->COUNT16.STATUS.reg & TC_STATUS_SYNCBUSY;
44 }
45 
46 static void tcReset() {
47  // Reset TCx
48  TC5->COUNT16.CTRLA.reg = TC_CTRLA_SWRST;
49  while (tcIsSyncing())
50  ;
51  while (TC5->COUNT16.CTRLA.bit.SWRST)
52  ;
53 }
54 /* Not currently used, and does not compile with EXTERNAL_AUDIO_OUTPUT
55 static void tcEnd() {
56  // Disable TC5
57  TC5->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
58  while (tcIsSyncing())
59  ;
60  tcReset();
61  analogWrite(AUDIO_CHANNEL_1_PIN, 0);
62 } */
63 
64 static void tcConfigure(uint32_t sampleRate) {
65  // Enable GCLK for TCC2 and TC5 (timer counter input clock)
66  GCLK->CLKCTRL.reg = (uint16_t)(GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 |
67  GCLK_CLKCTRL_ID(GCM_TC4_TC5));
68  while (GCLK->STATUS.bit.SYNCBUSY)
69  ;
70 
71  tcReset();
72 
73  // Set Timer counter Mode to 16 bits
74  TC5->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
75 
76  // Set TC5 mode as match frequency
77  TC5->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
78 
79  TC5->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1 | TC_CTRLA_ENABLE;
80 
81  TC5->COUNT16.CC[0].reg = (uint16_t)(SystemCoreClock / sampleRate - 1);
82  while (tcIsSyncing())
83  ;
84 
85  // Configure interrupt request
86  NVIC_DisableIRQ(TC5_IRQn);
87  NVIC_ClearPendingIRQ(TC5_IRQn);
88  NVIC_SetPriority(TC5_IRQn, 0);
89  NVIC_EnableIRQ(TC5_IRQn);
90 
91  // Enable the TC5 interrupt request
92  TC5->COUNT16.INTENSET.bit.MC0 = 1;
93  while (tcIsSyncing())
94  ;
95 }
96 
97 void TC5_Handler(void) __attribute__((weak, alias("samd21AudioOutput")));
98 
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102 void samd21AudioOutput() {
103  defaultAudioOutput();
104  TC5->COUNT16.INTFLAG.bit.MC0 = 1;
105 }
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #if (EXTERNAL_AUDIO_OUTPUT != true) // otherwise, the last stage - audioOutput() - will be provided by the user
111 #include "AudioConfigSAMD21.h"
112 inline void audioOutput(const AudioOutput f) {
113  analogWrite(AUDIO_CHANNEL_1_PIN, f.l()+AUDIO_BIAS);
114 }
115 #endif
116 
117 static void startAudio() {
118 #ifdef ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
119  {
120  static const int CPLAY_SPEAKER_SHUTDOWN = 11;
121  pinMode(CPLAY_SPEAKER_SHUTDOWN, OUTPUT);
122  digitalWrite(CPLAY_SPEAKER_SHUTDOWN, HIGH);
123  }
124 
125 #endif
126  analogWriteResolution(10);
127 #if (EXTERNAL_AUDIO_OUTPUT != true)
128  analogWrite(AUDIO_CHANNEL_1_PIN, 0);
129 #endif
130 
131  tcConfigure(AUDIO_RATE);
132 }
133 
134 void stopMozzi() {
135  // TODO: implement me
136  interrupts();
137 }
138 //// END AUDIO OUTPUT code ///////
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
#define AUDIO_CHANNEL_1_PIN
void setupFastAnalogRead(int8_t speed)
NOTE: Code needed to set up faster than usual analog reads, e.g.
#define AUDIO_BIAS
Definition: MozziGuts.h:229