PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Tue Jan 30 10:51:42 2018 +0000
Revision:
32:6d5a63b6d5b6
Parent:
31:f4b9b85c7b62
Child:
33:7d20a1e51908
Fixed also GBSOUND

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 31:f4b9b85c7b62 1 /**************************************************************************/
Pokitto 31:f4b9b85c7b62 2 /*!
Pokitto 31:f4b9b85c7b62 3 @file HWSound.cpp
Pokitto 31:f4b9b85c7b62 4 @author Jonne Valola
Pokitto 31:f4b9b85c7b62 5
Pokitto 31:f4b9b85c7b62 6 @section LICENSE
Pokitto 31:f4b9b85c7b62 7
Pokitto 31:f4b9b85c7b62 8 Software License Agreement (BSD License)
Pokitto 31:f4b9b85c7b62 9
Pokitto 31:f4b9b85c7b62 10 Copyright (c) 2016, Jonne Valola
Pokitto 31:f4b9b85c7b62 11 All rights reserved.
Pokitto 31:f4b9b85c7b62 12
Pokitto 31:f4b9b85c7b62 13 Redistribution and use in source and binary forms, with or without
Pokitto 31:f4b9b85c7b62 14 modification, are permitted provided that the following conditions are met:
Pokitto 31:f4b9b85c7b62 15 1. Redistributions of source code must retain the above copyright
Pokitto 31:f4b9b85c7b62 16 notice, this list of conditions and the following disclaimer.
Pokitto 31:f4b9b85c7b62 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 31:f4b9b85c7b62 18 notice, this list of conditions and the following disclaimer in the
Pokitto 31:f4b9b85c7b62 19 documentation and/or other materials provided with the distribution.
Pokitto 31:f4b9b85c7b62 20 3. Neither the name of the copyright holders nor the
Pokitto 31:f4b9b85c7b62 21 names of its contributors may be used to endorse or promote products
Pokitto 31:f4b9b85c7b62 22 derived from this software without specific prior written permission.
Pokitto 31:f4b9b85c7b62 23
Pokitto 31:f4b9b85c7b62 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 31:f4b9b85c7b62 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 31:f4b9b85c7b62 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 31:f4b9b85c7b62 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 31:f4b9b85c7b62 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 31:f4b9b85c7b62 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 31:f4b9b85c7b62 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 31:f4b9b85c7b62 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 31:f4b9b85c7b62 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 31:f4b9b85c7b62 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 31:f4b9b85c7b62 34 */
Pokitto 31:f4b9b85c7b62 35 /**************************************************************************/
Pokitto 31:f4b9b85c7b62 36
Pokitto 31:f4b9b85c7b62 37 #include "mbed.h"
Pokitto 31:f4b9b85c7b62 38 #include "HWSound.h"
Pokitto 31:f4b9b85c7b62 39 //#include "MCP4018.h"
Pokitto 31:f4b9b85c7b62 40 #include "SoftwareI2C.h"
Pokitto 31:f4b9b85c7b62 41 #include "Pokitto_settings.h"
Pokitto 31:f4b9b85c7b62 42 #include "PokittoDisk.h"
Pokitto 31:f4b9b85c7b62 43 #include "PokittoGlobs.h"
Pokitto 31:f4b9b85c7b62 44 #include "Synth.h"
Pokitto 31:f4b9b85c7b62 45 #include "timer_11u6x.h"
Pokitto 31:f4b9b85c7b62 46 #include "clock_11u6x.h"
Pokitto 31:f4b9b85c7b62 47 //#include "beat_11025.h"
Pokitto 31:f4b9b85c7b62 48
Pokitto 31:f4b9b85c7b62 49
Pokitto 31:f4b9b85c7b62 50
Pokitto 31:f4b9b85c7b62 51
Pokitto 31:f4b9b85c7b62 52 using namespace Pokitto;
Pokitto 31:f4b9b85c7b62 53
Pokitto 31:f4b9b85c7b62 54 #ifndef POK_SIM
Pokitto 31:f4b9b85c7b62 55 pwmout_t* obj = &audiopwm;
Pokitto 31:f4b9b85c7b62 56 #endif
Pokitto 31:f4b9b85c7b62 57
Pokitto 31:f4b9b85c7b62 58 /** Sound Variables **/
Pokitto 31:f4b9b85c7b62 59 #if (POK_STREAMING_MUSIC > 0)
Pokitto 31:f4b9b85c7b62 60 unsigned char buffers[4][BUFFER_SIZE];
Pokitto 31:f4b9b85c7b62 61 volatile int currentBuffer = 0, oldBuffer = 0;
Pokitto 31:f4b9b85c7b62 62 volatile int bufindex = 0, vol=1;
Pokitto 31:f4b9b85c7b62 63 volatile unsigned char * currentPtr;
Pokitto 31:f4b9b85c7b62 64 volatile unsigned char * endPtr;
Pokitto 31:f4b9b85c7b62 65 int8_t streamvol=3;
Pokitto 31:f4b9b85c7b62 66 uint32_t streamcounter=0;
Pokitto 31:f4b9b85c7b62 67 uint8_t streamstep=0;
Pokitto 31:f4b9b85c7b62 68 #endif
Pokitto 31:f4b9b85c7b62 69
Pokitto 31:f4b9b85c7b62 70 #if POK_ENABLE_SOUND > 0
Pokitto 31:f4b9b85c7b62 71 pwmout_t Pokitto::audiopwm; // this way (instead of PwmOut class) pwm doesn't start screaming until it is initialized !
Pokitto 31:f4b9b85c7b62 72 //Ticker Pokitto::audio;
Pokitto 31:f4b9b85c7b62 73
Pokitto 31:f4b9b85c7b62 74 using namespace Pokitto;
Pokitto 31:f4b9b85c7b62 75
Pokitto 31:f4b9b85c7b62 76 /** stream output and status */
Pokitto 31:f4b9b85c7b62 77 uint8_t Pokitto::streambyte,Pokitto::streamon;
Pokitto 31:f4b9b85c7b62 78
Pokitto 31:f4b9b85c7b62 79 uint8_t soundbuf[256], soundbufindex=0, Pokitto::HWvolume=0;
Pokitto 31:f4b9b85c7b62 80 bool volpotError=false; //test for broken MCP4018
Pokitto 31:f4b9b85c7b62 81
Pokitto 31:f4b9b85c7b62 82 uint16_t soundbyte;
Pokitto 31:f4b9b85c7b62 83
Pokitto 31:f4b9b85c7b62 84
Pokitto 31:f4b9b85c7b62 85 #if POK_USE_DAC > 0
Pokitto 31:f4b9b85c7b62 86 #if POK_BOARDREV == 1
Pokitto 31:f4b9b85c7b62 87 /** 2-layer board rev 1.3 **/
Pokitto 31:f4b9b85c7b62 88 DigitalOut dac0(P1_6);
Pokitto 31:f4b9b85c7b62 89 DigitalOut dac1(P1_0);
Pokitto 31:f4b9b85c7b62 90 DigitalOut dac2(P1_16);
Pokitto 31:f4b9b85c7b62 91 DigitalOut dac3(P0_19);
Pokitto 31:f4b9b85c7b62 92 DigitalOut dac4(P0_17);
Pokitto 31:f4b9b85c7b62 93 DigitalOut dac5(P1_12);
Pokitto 31:f4b9b85c7b62 94 DigitalOut dac6(P1_15);
Pokitto 31:f4b9b85c7b62 95 DigitalOut dac7(P1_8);
Pokitto 31:f4b9b85c7b62 96 #else
Pokitto 31:f4b9b85c7b62 97 /** 4-layer board rev 2.1 **/
Pokitto 31:f4b9b85c7b62 98 DigitalOut dac0(P1_28);
Pokitto 31:f4b9b85c7b62 99 DigitalOut dac1(P1_29);
Pokitto 31:f4b9b85c7b62 100 DigitalOut dac2(P1_30);
Pokitto 31:f4b9b85c7b62 101 DigitalOut dac3(P1_31);
Pokitto 31:f4b9b85c7b62 102 /* has daniel made a mistake ?*/
Pokitto 31:f4b9b85c7b62 103 DigitalOut dac4(P2_20);
Pokitto 31:f4b9b85c7b62 104 DigitalOut dac5(P2_21);
Pokitto 31:f4b9b85c7b62 105 DigitalOut dac6(P2_22);
Pokitto 31:f4b9b85c7b62 106 DigitalOut dac7(P2_23);
Pokitto 31:f4b9b85c7b62 107
Pokitto 31:f4b9b85c7b62 108 DigitalOut amp(P1_17);
Pokitto 31:f4b9b85c7b62 109
Pokitto 31:f4b9b85c7b62 110 #endif // POK_BOARDREV
Pokitto 31:f4b9b85c7b62 111 #endif // POK_USE_DAC
Pokitto 31:f4b9b85c7b62 112
Pokitto 31:f4b9b85c7b62 113 #if POK_BOARDREV == 2
Pokitto 31:f4b9b85c7b62 114 //MCP4018 volpot(P0_5,P0_4);
Pokitto 31:f4b9b85c7b62 115
Pokitto 31:f4b9b85c7b62 116 /**
Pokitto 31:f4b9b85c7b62 117 * @brief Handle interrupt from 32-bit timer 0
Pokitto 31:f4b9b85c7b62 118 * @return Nothing
Pokitto 31:f4b9b85c7b62 119 */
Pokitto 31:f4b9b85c7b62 120
Pokitto 31:f4b9b85c7b62 121 uint32_t p=0;
Pokitto 31:f4b9b85c7b62 122
Pokitto 31:f4b9b85c7b62 123 extern "C" void TIMER32_0_IRQHandler(void)
Pokitto 31:f4b9b85c7b62 124 {
Pokitto 31:f4b9b85c7b62 125 if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) {
Pokitto 31:f4b9b85c7b62 126 Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1);
Pokitto 31:f4b9b85c7b62 127 //pokSoundBufferedIRQ();
Pokitto 32:6d5a63b6d5b6 128 #if POK_GBSOUND > 0
Pokitto 32:6d5a63b6d5b6 129 /** GAMEBUINO SOUND **/
Pokitto 32:6d5a63b6d5b6 130 Pokitto::audio_IRQ();
Pokitto 32:6d5a63b6d5b6 131 #else
Pokitto 32:6d5a63b6d5b6 132 /** NOT GAMEBUINO SOUND **/
Pokitto 31:f4b9b85c7b62 133 pokSoundIRQ();
Pokitto 32:6d5a63b6d5b6 134 #endif
Pokitto 31:f4b9b85c7b62 135 }
Pokitto 31:f4b9b85c7b62 136 }
Pokitto 31:f4b9b85c7b62 137
Pokitto 31:f4b9b85c7b62 138
Pokitto 31:f4b9b85c7b62 139 void initHWvolumecontrol() {
Pokitto 31:f4b9b85c7b62 140 HWvolume=0;
Pokitto 31:f4b9b85c7b62 141 volpotError=true;
Pokitto 31:f4b9b85c7b62 142 //if (volpot.put(HWvolume)) volpotError=true; //try if MCP4018 answers
Pokitto 31:f4b9b85c7b62 143 setHWvolume(VOLUME_STARTUP);
Pokitto 31:f4b9b85c7b62 144 }
Pokitto 31:f4b9b85c7b62 145
Pokitto 31:f4b9b85c7b62 146 int Pokitto::setHWvolume(uint8_t v) {
Pokitto 31:f4b9b85c7b62 147 HWvolume = 0x7F&v;
Pokitto 31:f4b9b85c7b62 148 //if (!volpotError) return volpot.put(HWvolume); //use normal I2C
Pokitto 31:f4b9b85c7b62 149 /* fallback method for broken MCP4018 */
Pokitto 31:f4b9b85c7b62 150 SoftwareI2C swvolpot(P0_4, P0_5); //swapped SDA,SCL
Pokitto 31:f4b9b85c7b62 151 swvolpot.write(0x5e,HWvolume);
Pokitto 31:f4b9b85c7b62 152 return HWvolume;
Pokitto 31:f4b9b85c7b62 153 }
Pokitto 31:f4b9b85c7b62 154
Pokitto 31:f4b9b85c7b62 155 uint8_t Pokitto::getHWvolume() {
Pokitto 31:f4b9b85c7b62 156 return HWvolume;
Pokitto 31:f4b9b85c7b62 157 }
Pokitto 31:f4b9b85c7b62 158
Pokitto 31:f4b9b85c7b62 159 void Pokitto::changeHWvolume(int8_t v) {
Pokitto 31:f4b9b85c7b62 160 int temp = HWvolume + v;
Pokitto 31:f4b9b85c7b62 161 if (temp < 0) temp = 0; //prevent volume "looparound" than can make a massive crack
Pokitto 31:f4b9b85c7b62 162 if (temp > 127) temp = 127;
Pokitto 31:f4b9b85c7b62 163 setHWvolume(temp);
Pokitto 31:f4b9b85c7b62 164 }
Pokitto 31:f4b9b85c7b62 165
Pokitto 31:f4b9b85c7b62 166 uint8_t Pokitto::ampIsOn() {
Pokitto 31:f4b9b85c7b62 167 return amp;
Pokitto 31:f4b9b85c7b62 168 }
Pokitto 31:f4b9b85c7b62 169
Pokitto 31:f4b9b85c7b62 170 void Pokitto::ampEnable(uint8_t v) {
Pokitto 31:f4b9b85c7b62 171 if (v>1) v=1; // limit against funny values
Pokitto 31:f4b9b85c7b62 172 amp=v;
Pokitto 31:f4b9b85c7b62 173 }
Pokitto 31:f4b9b85c7b62 174 #endif // POK_BOARDREV == 2
Pokitto 31:f4b9b85c7b62 175
Pokitto 31:f4b9b85c7b62 176 void Pokitto::dac_write(uint8_t value) {
Pokitto 31:f4b9b85c7b62 177 #if POK_USE_DAC > 0
Pokitto 31:f4b9b85c7b62 178 #if POK_BOARDREV == 1 // was 1
Pokitto 31:f4b9b85c7b62 179 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 31:f4b9b85c7b62 180 value >>= 1;
Pokitto 31:f4b9b85c7b62 181 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 31:f4b9b85c7b62 182 value >>= 1;
Pokitto 31:f4b9b85c7b62 183 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 31:f4b9b85c7b62 184 value >>= 1;
Pokitto 31:f4b9b85c7b62 185 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 31:f4b9b85c7b62 186 value >>= 1;
Pokitto 31:f4b9b85c7b62 187 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 31:f4b9b85c7b62 188 value >>= 1;
Pokitto 31:f4b9b85c7b62 189 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 31:f4b9b85c7b62 190 value >>= 1;
Pokitto 31:f4b9b85c7b62 191 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 31:f4b9b85c7b62 192 value >>= 1;
Pokitto 31:f4b9b85c7b62 193 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 31:f4b9b85c7b62 194 #else
Pokitto 31:f4b9b85c7b62 195 //uint32_t val;
Pokitto 31:f4b9b85c7b62 196 //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 31:f4b9b85c7b62 197 //val += value<<(15-4); //higher 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 31:f4b9b85c7b62 198 /* daniel has made a mistake with ports */
Pokitto 31:f4b9b85c7b62 199 //val = ((value&0x70)<<(28-4)); //higher 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 31:f4b9b85c7b62 200 //val += value<<(15); //lower 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 31:f4b9b85c7b62 201 //SET_MASK_DAC;
Pokitto 31:f4b9b85c7b62 202 //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port
Pokitto 31:f4b9b85c7b62 203 //CLR_MASK_DAC;
Pokitto 31:f4b9b85c7b62 204 /* fixed here */
Pokitto 31:f4b9b85c7b62 205 /*val=value;
Pokitto 31:f4b9b85c7b62 206 SET_MASK_DAC_LO;
Pokitto 31:f4b9b85c7b62 207 LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port
Pokitto 31:f4b9b85c7b62 208 CLR_MASK_DAC_LO;
Pokitto 31:f4b9b85c7b62 209 SET_MASK_DAC_HI;
Pokitto 31:f4b9b85c7b62 210 LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port
Pokitto 31:f4b9b85c7b62 211 CLR_MASK_DAC_HI; */
Pokitto 31:f4b9b85c7b62 212 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 31:f4b9b85c7b62 213 value >>= 1;
Pokitto 31:f4b9b85c7b62 214 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 31:f4b9b85c7b62 215 value >>= 1;
Pokitto 31:f4b9b85c7b62 216 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 31:f4b9b85c7b62 217 value >>= 1;
Pokitto 31:f4b9b85c7b62 218 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 31:f4b9b85c7b62 219 value >>= 1;
Pokitto 31:f4b9b85c7b62 220 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 31:f4b9b85c7b62 221 value >>= 1;
Pokitto 31:f4b9b85c7b62 222 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 31:f4b9b85c7b62 223 value >>= 1;
Pokitto 31:f4b9b85c7b62 224 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 31:f4b9b85c7b62 225 value >>= 1;
Pokitto 31:f4b9b85c7b62 226 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 31:f4b9b85c7b62 227 //CLR_MASK_DAC;
Pokitto 31:f4b9b85c7b62 228 #endif // BOARDREV
Pokitto 31:f4b9b85c7b62 229 #endif
Pokitto 31:f4b9b85c7b62 230 }
Pokitto 31:f4b9b85c7b62 231
Pokitto 31:f4b9b85c7b62 232 /** SOUND INIT **/
Pokitto 31:f4b9b85c7b62 233 void Pokitto::soundInit() {
Pokitto 31:f4b9b85c7b62 234 uint32_t timerFreq;
Pokitto 31:f4b9b85c7b62 235 pwmout_init(&audiopwm,POK_AUD_PIN);
Pokitto 31:f4b9b85c7b62 236 pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us
Pokitto 31:f4b9b85c7b62 237 pwmout_write(&audiopwm,0.1f);
Pokitto 31:f4b9b85c7b62 238
Pokitto 32:6d5a63b6d5b6 239 //#if POK_GBSOUND > 0
Pokitto 31:f4b9b85c7b62 240 /** GAMEBUINO SOUND **/
Pokitto 32:6d5a63b6d5b6 241 //audio.attach_us(&audio_IRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 32:6d5a63b6d5b6 242 //#else
Pokitto 31:f4b9b85c7b62 243 /** NOT GAMEBUINO SOUND **/
Pokitto 31:f4b9b85c7b62 244 //audio.attach_us(&pokSoundBufferedIRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 31:f4b9b85c7b62 245 /* Initialize 32-bit timer 0 clock */
Pokitto 31:f4b9b85c7b62 246 Chip_TIMER_Init(LPC_TIMER32_0);
Pokitto 31:f4b9b85c7b62 247
Pokitto 31:f4b9b85c7b62 248 /* Timer rate is system clock rate */
Pokitto 31:f4b9b85c7b62 249 timerFreq = Chip_Clock_GetSystemClockRate();
Pokitto 31:f4b9b85c7b62 250
Pokitto 31:f4b9b85c7b62 251 /* Timer setup for match and interrupt at TICKRATE_HZ */
Pokitto 31:f4b9b85c7b62 252 Chip_TIMER_Reset(LPC_TIMER32_0);
Pokitto 31:f4b9b85c7b62 253
Pokitto 31:f4b9b85c7b62 254 /* Enable both timers to generate interrupts when time matches */
Pokitto 31:f4b9b85c7b62 255 Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1);
Pokitto 31:f4b9b85c7b62 256
Pokitto 31:f4b9b85c7b62 257 /* Setup 32-bit timer's duration (32-bit match time) */
Pokitto 31:f4b9b85c7b62 258 Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (timerFreq / POK_AUD_FREQ));
Pokitto 31:f4b9b85c7b62 259
Pokitto 31:f4b9b85c7b62 260 /* Setup both timers to restart when match occurs */
Pokitto 31:f4b9b85c7b62 261 Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1);
Pokitto 31:f4b9b85c7b62 262
Pokitto 31:f4b9b85c7b62 263 /* Start both timers */
Pokitto 31:f4b9b85c7b62 264 Chip_TIMER_Enable(LPC_TIMER32_0);
Pokitto 31:f4b9b85c7b62 265
Pokitto 31:f4b9b85c7b62 266 /* Clear both timers of any pending interrupts */
Pokitto 31:f4b9b85c7b62 267 #define TIMER_32_0_IRQn 18
Pokitto 31:f4b9b85c7b62 268 NVIC_ClearPendingIRQ((IRQn_Type)TIMER_32_0_IRQn);
Pokitto 31:f4b9b85c7b62 269
Pokitto 31:f4b9b85c7b62 270 /* Redirect IRQ vector - Jonne*/
Pokitto 31:f4b9b85c7b62 271 NVIC_SetVector((IRQn_Type)TIMER_32_0_IRQn, (uint32_t)&TIMER32_0_IRQHandler);
Pokitto 31:f4b9b85c7b62 272
Pokitto 31:f4b9b85c7b62 273 /* Enable both timer interrupts */
Pokitto 31:f4b9b85c7b62 274 NVIC_EnableIRQ((IRQn_Type)TIMER_32_0_IRQn);
Pokitto 32:6d5a63b6d5b6 275 //#endif // POK_GAMEBUINO_SUPPORT
Pokitto 31:f4b9b85c7b62 276
Pokitto 31:f4b9b85c7b62 277 //emptySong();
Pokitto 31:f4b9b85c7b62 278 //emptyOscillators();
Pokitto 31:f4b9b85c7b62 279 //emptyBlocks();
Pokitto 31:f4b9b85c7b62 280 //emptyPatches();
Pokitto 31:f4b9b85c7b62 281 #ifdef TEST_SOUND
Pokitto 31:f4b9b85c7b62 282 testOsc();
Pokitto 31:f4b9b85c7b62 283 #endif // TEST_SOUND
Pokitto 31:f4b9b85c7b62 284 #if POK_BOARDREV == 2
Pokitto 31:f4b9b85c7b62 285 initHWvolumecontrol();
Pokitto 31:f4b9b85c7b62 286 #endif
Pokitto 31:f4b9b85c7b62 287
Pokitto 31:f4b9b85c7b62 288 }
Pokitto 31:f4b9b85c7b62 289
Pokitto 31:f4b9b85c7b62 290
Pokitto 31:f4b9b85c7b62 291 uint8_t Pokitto::streamPaused() {
Pokitto 31:f4b9b85c7b62 292 return !streamon;
Pokitto 31:f4b9b85c7b62 293 }
Pokitto 31:f4b9b85c7b62 294
Pokitto 31:f4b9b85c7b62 295 void Pokitto::pauseStream() {
Pokitto 31:f4b9b85c7b62 296 streamon=0;
Pokitto 31:f4b9b85c7b62 297 }
Pokitto 31:f4b9b85c7b62 298
Pokitto 31:f4b9b85c7b62 299 void Pokitto::playStream() {
Pokitto 31:f4b9b85c7b62 300 streamon=1;
Pokitto 31:f4b9b85c7b62 301 }
Pokitto 31:f4b9b85c7b62 302
Pokitto 31:f4b9b85c7b62 303
Pokitto 31:f4b9b85c7b62 304 void pokPauseStream() {
Pokitto 31:f4b9b85c7b62 305 streamon=0;
Pokitto 31:f4b9b85c7b62 306 }
Pokitto 31:f4b9b85c7b62 307
Pokitto 31:f4b9b85c7b62 308 void pokPlayStream() {
Pokitto 31:f4b9b85c7b62 309 streamon=1;
Pokitto 31:f4b9b85c7b62 310 }
Pokitto 31:f4b9b85c7b62 311
Pokitto 31:f4b9b85c7b62 312
Pokitto 31:f4b9b85c7b62 313
Pokitto 31:f4b9b85c7b62 314 void pokSoundBufferedIRQ() {
Pokitto 31:f4b9b85c7b62 315 uint8_t output = soundbuf[soundbufindex++];
Pokitto 31:f4b9b85c7b62 316 //if (p==sizeof(beat_11025_raw)) p=0;
Pokitto 31:f4b9b85c7b62 317 //soundbuf[soundbufindex++] = output;
Pokitto 31:f4b9b85c7b62 318 uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
Pokitto 31:f4b9b85c7b62 319 obj->pwm->MATCHREL1 = t_on;
Pokitto 31:f4b9b85c7b62 320 }
Pokitto 31:f4b9b85c7b62 321
Pokitto 31:f4b9b85c7b62 322 void pokSoundIRQ() {
Pokitto 31:f4b9b85c7b62 323 //#define TICKY 0xFFFF //160
Pokitto 31:f4b9b85c7b62 324 //#define INCY 409
Pokitto 31:f4b9b85c7b62 325 uint8_t output=0;
Pokitto 31:f4b9b85c7b62 326 //if (test==TICKY) test=0;
Pokitto 31:f4b9b85c7b62 327 //if (test<(TICKY/2)) { tpin=1; pwmout_write(&audiopwm,(float)0/(float)255);}//dac_write(0);}
Pokitto 31:f4b9b85c7b62 328 //else {tpin=0; pwmout_write(&audiopwm,(float)255/(float)255);}//dac_write(64);}
Pokitto 31:f4b9b85c7b62 329 //test+=INCY;
Pokitto 31:f4b9b85c7b62 330 //return;
Pokitto 31:f4b9b85c7b62 331 #ifndef POK_SIM
Pokitto 31:f4b9b85c7b62 332 pwmout_t* obj = &audiopwm;
Pokitto 31:f4b9b85c7b62 333 #endif
Pokitto 31:f4b9b85c7b62 334 #if POK_STREAMING_MUSIC > 0
Pokitto 31:f4b9b85c7b62 335 #if POK_STREAMFREQ_HALVE
Pokitto 31:f4b9b85c7b62 336 streamstep = 1-streamstep;
Pokitto 31:f4b9b85c7b62 337 #else
Pokitto 31:f4b9b85c7b62 338 streamstep = 1;
Pokitto 31:f4b9b85c7b62 339 #endif // POK_STREAMFREQ_HALVE
Pokitto 31:f4b9b85c7b62 340 streamstep &= streamon; // streamon is used to toggle SD music streaming on and off
Pokitto 31:f4b9b85c7b62 341 if (streamstep) {
Pokitto 31:f4b9b85c7b62 342 output = (*currentPtr++);
Pokitto 31:f4b9b85c7b62 343 if(streamvol && streamon) {
Pokitto 31:f4b9b85c7b62 344 output >>= 3-streamvol;
Pokitto 31:f4b9b85c7b62 345 streambyte = output;
Pokitto 31:f4b9b85c7b62 346 } else {
Pokitto 31:f4b9b85c7b62 347 streambyte = 0; // duty cycle
Pokitto 31:f4b9b85c7b62 348 output = 0;
Pokitto 31:f4b9b85c7b62 349 }
Pokitto 31:f4b9b85c7b62 350 if (currentPtr >= endPtr)
Pokitto 31:f4b9b85c7b62 351 {
Pokitto 31:f4b9b85c7b62 352 currentBuffer++;
Pokitto 31:f4b9b85c7b62 353 if (currentBuffer==4) currentBuffer=0;
Pokitto 31:f4b9b85c7b62 354 currentPtr = buffers[currentBuffer];
Pokitto 31:f4b9b85c7b62 355 endPtr = currentPtr + BUFFER_SIZE;
Pokitto 31:f4b9b85c7b62 356 }
Pokitto 31:f4b9b85c7b62 357 }
Pokitto 31:f4b9b85c7b62 358 #endif // POK_STREAMING_MUSIC
Pokitto 31:f4b9b85c7b62 359
Pokitto 31:f4b9b85c7b62 360 /** DO ADDITIONAL SOUND PROCESSING (NOT STREAM) OF SOUND HERE **/
Pokitto 31:f4b9b85c7b62 361
Pokitto 31:f4b9b85c7b62 362 #if POK_ENABLE_SYNTH
Pokitto 31:f4b9b85c7b62 363 /** if song is being played from sd **/
Pokitto 31:f4b9b85c7b62 364 if (playing) {
Pokitto 31:f4b9b85c7b62 365 notetick++;
Pokitto 31:f4b9b85c7b62 366 updatePlaybackSD(playerpos&7);
Pokitto 31:f4b9b85c7b62 367 }
Pokitto 31:f4b9b85c7b62 368 /** oscillators update **/
Pokitto 31:f4b9b85c7b62 369 osc1.count += osc1.cinc + (osc1.pitchbend >> 4); // counts to 65535 and overflows to zero WAS 8 !
Pokitto 31:f4b9b85c7b62 370 osc2.count += osc2.cinc + (osc2.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 31:f4b9b85c7b62 371 osc3.count += osc3.cinc + (osc3.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 31:f4b9b85c7b62 372 Marr[tick](); // call mixing function
Pokitto 31:f4b9b85c7b62 373 --tick;
Pokitto 31:f4b9b85c7b62 374
Pokitto 31:f4b9b85c7b62 375 /** mixing oscillator output **/
Pokitto 31:f4b9b85c7b62 376
Pokitto 31:f4b9b85c7b62 377 uint16_t op = (uint16_t) ((osc1.output)*(osc1.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 31:f4b9b85c7b62 378 op += (uint16_t) ((osc2.output)*(osc2.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 31:f4b9b85c7b62 379 op += (uint16_t) ((osc3.output)*(osc3.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 31:f4b9b85c7b62 380 output = (uint8_t) op;
Pokitto 31:f4b9b85c7b62 381
Pokitto 31:f4b9b85c7b62 382 #endif // POK_ENABLE_SYNTH
Pokitto 31:f4b9b85c7b62 383
Pokitto 31:f4b9b85c7b62 384 #ifndef POK_SIM
Pokitto 31:f4b9b85c7b62 385 /** HARDWARE **/
Pokitto 31:f4b9b85c7b62 386 #if POK_ENABLE_SOUND > 0
Pokitto 31:f4b9b85c7b62 387 #if POK_STREAMING_MUSIC > 0
Pokitto 31:f4b9b85c7b62 388 /** sound is enabled, streaming is enabled */
Pokitto 31:f4b9b85c7b62 389 #if POK_STREAM_TO_DAC > 0
Pokitto 31:f4b9b85c7b62 390 /** stream goes to DAC */
Pokitto 31:f4b9b85c7b62 391 #if POK_USE_DAC > 0
Pokitto 31:f4b9b85c7b62 392 if (streamstep) dac_write((uint8_t)streambyte); // duty cycle
Pokitto 31:f4b9b85c7b62 393 #endif // POK_USE_DAC
Pokitto 31:f4b9b85c7b62 394 #else
Pokitto 31:f4b9b85c7b62 395 /** stream goes to PWM */
Pokitto 31:f4b9b85c7b62 396 if (streamstep) {
Pokitto 31:f4b9b85c7b62 397 //pwmout_write(&audiopwm,(float)streambyte/(float)255);
Pokitto 31:f4b9b85c7b62 398 uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*streambyte)>>8); //cut out float
Pokitto 31:f4b9b85c7b62 399 obj->pwm->MATCHREL1 = t_on;
Pokitto 31:f4b9b85c7b62 400 //dac_write((uint8_t)streambyte); // duty cycle
Pokitto 31:f4b9b85c7b62 401 }
Pokitto 31:f4b9b85c7b62 402 #endif // POK_STREAM_TO_DAC
Pokitto 31:f4b9b85c7b62 403 #endif // POK_STREAMING_MUSIC
Pokitto 31:f4b9b85c7b62 404 #if POK_STREAM_TO_DAC > 0
Pokitto 31:f4b9b85c7b62 405 /** synth goes to PWM */
Pokitto 31:f4b9b85c7b62 406 //pwmout_write(&audiopwm,(float)output/(float)255);
Pokitto 31:f4b9b85c7b62 407 uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
Pokitto 31:f4b9b85c7b62 408 obj->pwm->MATCHREL1 = t_on;
Pokitto 31:f4b9b85c7b62 409 #else
Pokitto 31:f4b9b85c7b62 410 dac_write((uint8_t)output);
Pokitto 31:f4b9b85c7b62 411 #endif // decide where synth is output
Pokitto 31:f4b9b85c7b62 412 soundbyte = (output+streambyte)>>1;
Pokitto 31:f4b9b85c7b62 413 soundbuf[soundbufindex++]=soundbyte;
Pokitto 31:f4b9b85c7b62 414 #endif //POK_ENABLE_SOUND
Pokitto 31:f4b9b85c7b62 415 #endif // HARDWARE
Pokitto 31:f4b9b85c7b62 416 }
Pokitto 31:f4b9b85c7b62 417
Pokitto 31:f4b9b85c7b62 418
Pokitto 31:f4b9b85c7b62 419 void Pokitto::updateSDAudioStream() {
Pokitto 31:f4b9b85c7b62 420 if (streamPaused()) return;
Pokitto 31:f4b9b85c7b62 421
Pokitto 31:f4b9b85c7b62 422 #if POK_STREAMING_MUSIC > 0
Pokitto 31:f4b9b85c7b62 423 if (oldBuffer != currentBuffer) {
Pokitto 31:f4b9b85c7b62 424 if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE);
Pokitto 31:f4b9b85c7b62 425 else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE);
Pokitto 31:f4b9b85c7b62 426 else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE);
Pokitto 31:f4b9b85c7b62 427 else fileReadBytes(&buffers[2][0],BUFFER_SIZE);
Pokitto 31:f4b9b85c7b62 428 oldBuffer = currentBuffer;
Pokitto 31:f4b9b85c7b62 429 streamcounter += BUFFER_SIZE;
Pokitto 31:f4b9b85c7b62 430 } else return;
Pokitto 31:f4b9b85c7b62 431
Pokitto 31:f4b9b85c7b62 432 #ifndef POK_SIM
Pokitto 31:f4b9b85c7b62 433 if ( streamcounter > fs.fsize - (BUFFER_SIZE*6)) {
Pokitto 31:f4b9b85c7b62 434 #else
Pokitto 31:f4b9b85c7b62 435 if ( streamcounter > getFileLength() - (BUFFER_SIZE*6)) {
Pokitto 31:f4b9b85c7b62 436 #endif
Pokitto 31:f4b9b85c7b62 437 streamcounter=0;
Pokitto 31:f4b9b85c7b62 438 #if POK_STREAM_LOOP
Pokitto 31:f4b9b85c7b62 439 fileRewind();
Pokitto 31:f4b9b85c7b62 440 #else
Pokitto 31:f4b9b85c7b62 441 pokPauseStream();
Pokitto 31:f4b9b85c7b62 442 #endif
Pokitto 31:f4b9b85c7b62 443 }
Pokitto 31:f4b9b85c7b62 444 #endif
Pokitto 31:f4b9b85c7b62 445 }
Pokitto 31:f4b9b85c7b62 446
Pokitto 31:f4b9b85c7b62 447
Pokitto 31:f4b9b85c7b62 448 #endif // POK_ENABLE_SOUND
Pokitto 31:f4b9b85c7b62 449
Pokitto 31:f4b9b85c7b62 450
Pokitto 31:f4b9b85c7b62 451