Karpova Lab fork of stateScript
Dependencies: SMARTWAV SOMO_II mbed
Fork of stateScript_v2_karpova by
mbedInterface/mbedInterface.cpp@9:f15ad8d18881, 2016-01-28 (annotated)
- Committer:
- alustig3
- Date:
- Thu Jan 28 16:47:32 2016 +0000
- Revision:
- 9:f15ad8d18881
- Parent:
- 8:436cf2ab961c
Added pin designations for Karpova Lab's Ephys module 1.2 and Max module 1.1.; Code is now compatible with SOMO II sound module.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mkarlsso | 0:8dbd6bd9167f | 1 | #include "mbedInterface.h" |
mkarlsso | 0:8dbd6bd9167f | 2 | #include "mbed.h" |
alustig3 | 9:f15ad8d18881 | 3 | #define EPHYSRIG //!!!!!!make sure to change NUMPORTS, NUMINPORTS, and NUMOUTPORTS in the mbedInterface.h file!!!!!!!!! |
alustig3 | 9:f15ad8d18881 | 4 | //#define TRAINRIG //!!!!!!make sure to change NUMPORTS, NUMINPORTS, and NUMOUTPORTS in the mbedInterface.h file!!!!!!!!! |
mkarlsso | 0:8dbd6bd9167f | 5 | |
mkarlsso | 0:8dbd6bd9167f | 6 | |
mkarlsso | 0:8dbd6bd9167f | 7 | //-------------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 8 | //-------------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 9 | //This section is required for all custom harware interfaces |
mkarlsso | 0:8dbd6bd9167f | 10 | |
mkarlsso | 0:8dbd6bd9167f | 11 | //globals defined in hardwareInterface.cpp |
mkarlsso | 0:8dbd6bd9167f | 12 | extern uint32_t timeKeeper; //the master clock |
mkarlsso | 3:d7b0a0890d96 | 13 | uint32_t uSec_SinceLastClockInc = 0; |
mkarlsso | 3:d7b0a0890d96 | 14 | uint32_t uSec_SinceLastReset = 0; |
mkarlsso | 3:d7b0a0890d96 | 15 | |
mkarlsso | 0:8dbd6bd9167f | 16 | extern bool resetTimer; |
mkarlsso | 0:8dbd6bd9167f | 17 | extern bool clockSlave; |
mkarlsso | 0:8dbd6bd9167f | 18 | extern bool changeToSlave; |
mkarlsso | 0:8dbd6bd9167f | 19 | extern bool changeToStandAlone; |
mkarlsso | 0:8dbd6bd9167f | 20 | extern outputStream textDisplay; |
mkarlsso | 0:8dbd6bd9167f | 21 | |
mkarlsso | 5:abee20c0bf2a | 22 | int externalIncrementMod = 1; |
mkarlsso | 0:8dbd6bd9167f | 23 | int externalIncrementCounter = 0; |
mkarlsso | 0:8dbd6bd9167f | 24 | |
mkarlsso | 0:8dbd6bd9167f | 25 | |
mkarlsso | 0:8dbd6bd9167f | 26 | #ifdef MBED_RF |
mkarlsso | 3:d7b0a0890d96 | 27 | //We are listening to an RF signal for hardware syncing---------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 28 | //Uses DB9->RJ45 connector to map the following channels: |
mkarlsso | 0:8dbd6bd9167f | 29 | //1: P28 Clock Signal In |
mkarlsso | 0:8dbd6bd9167f | 30 | //2: P27 Data Signal In |
mkarlsso | 0:8dbd6bd9167f | 31 | //3: P20 FiberLED Out Signal |
mkarlsso | 0:8dbd6bd9167f | 32 | //4: 5V |
mkarlsso | 0:8dbd6bd9167f | 33 | //5: GND |
mkarlsso | 0:8dbd6bd9167f | 34 | //6: P6 NC |
mkarlsso | 0:8dbd6bd9167f | 35 | //7: P7 NC |
mkarlsso | 0:8dbd6bd9167f | 36 | //8: P8 NC |
mkarlsso | 0:8dbd6bd9167f | 37 | |
mkarlsso | 0:8dbd6bd9167f | 38 | DigitalOut mainLED(LED1); |
mkarlsso | 0:8dbd6bd9167f | 39 | DigitalOut secondaryLED(LED2); |
mkarlsso | 0:8dbd6bd9167f | 40 | |
mkarlsso | 0:8dbd6bd9167f | 41 | bool lightOn = false; |
mkarlsso | 0:8dbd6bd9167f | 42 | bool lightOn2 = false; |
mkarlsso | 0:8dbd6bd9167f | 43 | int pulseCounter = 0; |
mkarlsso | 0:8dbd6bd9167f | 44 | uint32_t lastPulse = 0; |
mkarlsso | 0:8dbd6bd9167f | 45 | uint32_t currentRFTime = 0; |
mkarlsso | 0:8dbd6bd9167f | 46 | uint32_t lastRFTime = 0; |
mkarlsso | 0:8dbd6bd9167f | 47 | uint32_t timeKeeperAtCurrentRFTime = 0; |
mkarlsso | 0:8dbd6bd9167f | 48 | uint32_t timeKeeperAtLastRFTime = 0; |
mkarlsso | 0:8dbd6bd9167f | 49 | int RFSyncCounter = 0; |
mkarlsso | 0:8dbd6bd9167f | 50 | bool RFSyncReadable = false; |
mkarlsso | 0:8dbd6bd9167f | 51 | bool RFSyncWritable = true; |
mkarlsso | 0:8dbd6bd9167f | 52 | |
mkarlsso | 0:8dbd6bd9167f | 53 | //Recieve clock signal from RF system |
alustig3 | 9:f15ad8d18881 | 54 | InterruptIn RFClock(p25); |
alustig3 | 9:f15ad8d18881 | 55 | DigitalIn RFData(p26); |
mkarlsso | 0:8dbd6bd9167f | 56 | |
mkarlsso | 0:8dbd6bd9167f | 57 | |
mkarlsso | 0:8dbd6bd9167f | 58 | void callback_RFClock(void) { |
mkarlsso | 0:8dbd6bd9167f | 59 | |
mkarlsso | 0:8dbd6bd9167f | 60 | //if this amount of time has passed since the last pulse, we have a new timestamp |
mkarlsso | 0:8dbd6bd9167f | 61 | if ((timeKeeper-lastPulse) > 4) { |
mkarlsso | 0:8dbd6bd9167f | 62 | |
mkarlsso | 0:8dbd6bd9167f | 63 | //make sure the previous timestamp was 32 pulses |
mkarlsso | 0:8dbd6bd9167f | 64 | //if so, update lastRFTime |
mkarlsso | 0:8dbd6bd9167f | 65 | //we only process every 100th stamp (once every 10 seconds with 10 Hz timestamps) |
mkarlsso | 0:8dbd6bd9167f | 66 | if ((pulseCounter == 31) && (RFSyncCounter == 99)){ |
mkarlsso | 0:8dbd6bd9167f | 67 | if (RFSyncWritable) { |
mkarlsso | 0:8dbd6bd9167f | 68 | lastRFTime = currentRFTime; |
mkarlsso | 0:8dbd6bd9167f | 69 | timeKeeperAtLastRFTime = timeKeeperAtCurrentRFTime; |
mkarlsso | 0:8dbd6bd9167f | 70 | RFSyncReadable = true; |
mkarlsso | 0:8dbd6bd9167f | 71 | RFSyncWritable = false; |
mkarlsso | 0:8dbd6bd9167f | 72 | } |
mkarlsso | 0:8dbd6bd9167f | 73 | } |
mkarlsso | 0:8dbd6bd9167f | 74 | |
mkarlsso | 0:8dbd6bd9167f | 75 | pulseCounter = 0; |
mkarlsso | 0:8dbd6bd9167f | 76 | currentRFTime = 0; |
mkarlsso | 0:8dbd6bd9167f | 77 | timeKeeperAtCurrentRFTime = timeKeeper; |
mkarlsso | 0:8dbd6bd9167f | 78 | RFSyncCounter = (RFSyncCounter+1)%100; |
mkarlsso | 0:8dbd6bd9167f | 79 | |
mkarlsso | 0:8dbd6bd9167f | 80 | |
mkarlsso | 0:8dbd6bd9167f | 81 | if (lightOn) { |
mkarlsso | 0:8dbd6bd9167f | 82 | mainLED = 0; |
mkarlsso | 0:8dbd6bd9167f | 83 | lightOn = false; |
mkarlsso | 0:8dbd6bd9167f | 84 | } else { |
mkarlsso | 0:8dbd6bd9167f | 85 | mainLED = 1; |
mkarlsso | 0:8dbd6bd9167f | 86 | lightOn = true; |
mkarlsso | 0:8dbd6bd9167f | 87 | } |
mkarlsso | 0:8dbd6bd9167f | 88 | } else { |
mkarlsso | 0:8dbd6bd9167f | 89 | if (pulseCounter < 32) { |
mkarlsso | 0:8dbd6bd9167f | 90 | currentRFTime = (currentRFTime | ( RFData.read() << pulseCounter)); |
mkarlsso | 0:8dbd6bd9167f | 91 | pulseCounter++; |
mkarlsso | 0:8dbd6bd9167f | 92 | } |
mkarlsso | 0:8dbd6bd9167f | 93 | } |
mkarlsso | 0:8dbd6bd9167f | 94 | lastPulse = timeKeeper; |
mkarlsso | 0:8dbd6bd9167f | 95 | } |
mkarlsso | 0:8dbd6bd9167f | 96 | |
mkarlsso | 0:8dbd6bd9167f | 97 | //------------------------------------------------------------ |
mkarlsso | 0:8dbd6bd9167f | 98 | #endif |
mkarlsso | 0:8dbd6bd9167f | 99 | |
mkarlsso | 3:d7b0a0890d96 | 100 | void externalClockIncDown() { |
mkarlsso | 0:8dbd6bd9167f | 101 | |
mkarlsso | 3:d7b0a0890d96 | 102 | //The external clock increment signal pulse has come back down. If the pulse was long |
mkarlsso | 3:d7b0a0890d96 | 103 | //enough, then we condsider it a valid pulse (the pulses should be 0.5 ms long) |
mkarlsso | 3:d7b0a0890d96 | 104 | if ((clockSlave)&&(uSec_SinceLastClockInc >= 300)) { |
mkarlsso | 3:d7b0a0890d96 | 105 | uSec_SinceLastClockInc = 0; |
mkarlsso | 3:d7b0a0890d96 | 106 | timeKeeper++; |
mkarlsso | 3:d7b0a0890d96 | 107 | |
mkarlsso | 3:d7b0a0890d96 | 108 | //Clock resets happen upon update so we dont get a partial first ms |
mkarlsso | 3:d7b0a0890d96 | 109 | if (resetTimer) { |
mkarlsso | 3:d7b0a0890d96 | 110 | uSec_SinceLastReset = 0; |
mkarlsso | 3:d7b0a0890d96 | 111 | timeKeeper = 0; |
mkarlsso | 3:d7b0a0890d96 | 112 | resetTimer = false; |
mkarlsso | 3:d7b0a0890d96 | 113 | } |
mkarlsso | 3:d7b0a0890d96 | 114 | } |
mkarlsso | 3:d7b0a0890d96 | 115 | |
mkarlsso | 3:d7b0a0890d96 | 116 | } |
mkarlsso | 3:d7b0a0890d96 | 117 | |
mkarlsso | 3:d7b0a0890d96 | 118 | void externalResetDown() { |
mkarlsso | 3:d7b0a0890d96 | 119 | |
mkarlsso | 3:d7b0a0890d96 | 120 | //The external clock reset signal pulse has come back down. If the pulse was long |
mkarlsso | 3:d7b0a0890d96 | 121 | //enough, then we condsider it a valid pulse (the pulses should be 1 ms long) |
mkarlsso | 5:abee20c0bf2a | 122 | /* |
mkarlsso | 5:abee20c0bf2a | 123 | textDisplay << uSec_SinceLastReset; |
mkarlsso | 3:d7b0a0890d96 | 124 | if ((clockSlave)&&(uSec_SinceLastReset >= 700)) { |
mkarlsso | 3:d7b0a0890d96 | 125 | uSec_SinceLastReset = 0; |
mkarlsso | 3:d7b0a0890d96 | 126 | timeKeeper = 1; //It has been 1ms since the reset pulse went up |
mkarlsso | 3:d7b0a0890d96 | 127 | textDisplay << timeKeeper << " Clock reset\r\n"; |
mkarlsso | 5:abee20c0bf2a | 128 | }*/ |
mkarlsso | 3:d7b0a0890d96 | 129 | } |
mkarlsso | 0:8dbd6bd9167f | 130 | |
mkarlsso | 0:8dbd6bd9167f | 131 | //------------------------------------------------------------------------ |
mkarlsso | 0:8dbd6bd9167f | 132 | //------------------------------------------------------------------------ |
mkarlsso | 0:8dbd6bd9167f | 133 | |
mkarlsso | 0:8dbd6bd9167f | 134 | //MBED-specific stuff |
mkarlsso | 0:8dbd6bd9167f | 135 | //--------------------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 136 | |
alustig3 | 8:436cf2ab961c | 137 | ////translate pin numbers to hardware pins |
alustig3 | 8:436cf2ab961c | 138 | //PinName outPins[NUMOUTPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20,p6}; //Old board output pins |
alustig3 | 8:436cf2ab961c | 139 | ////PinName outPins[NUMPORTS] = {p18,p15,p13,p11,p29,p25,p23,p21,p20}; //New board output pins |
alustig3 | 8:436cf2ab961c | 140 | //PinName inPins[NUMINPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7}; |
alustig3 | 8:436cf2ab961c | 141 | |
alustig3 | 9:f15ad8d18881 | 142 | #ifdef TRAINRIG //!!!!!!make sure to change NUMPORTS, NUMINPORTS, and NUMOUTPORTS in the mbedInterface.h file!!!!!!!!! |
mkarlsso | 0:8dbd6bd9167f | 143 | //translate pin numbers to hardware pins |
alustig3 | 9:f15ad8d18881 | 144 | PinName outPins[NUMPORTS] = {p7,p10,p13,p16,p17,p18}; //6 outports |
alustig3 | 9:f15ad8d18881 | 145 | PinName inPins[NUMPORTS] = {p5,p6,p8,p9,p11,p12,p14,p15}; //8 inports |
alustig3 | 8:436cf2ab961c | 146 | #endif |
alustig3 | 9:f15ad8d18881 | 147 | #ifdef EPHYSRIG//!!!!!!make sure to change NUMPORTS, NUMINPORTS, and NUMOUTPORTS in the mbedInterface.h file!!!!!!!!! |
alustig3 | 8:436cf2ab961c | 148 | //translate pin numbers to hardware pins |
alustig3 | 9:f15ad8d18881 | 149 | PinName outPins[NUMPORTS] = {p7,p10,p12,p14,p16,p17,p18,p21,p22}; //9 outports |
alustig3 | 9:f15ad8d18881 | 150 | PinName inPins[NUMPORTS] = {p5,p6,p8,p9,p11,p13,p15,p23,p24}; //9 inports |
alustig3 | 8:436cf2ab961c | 151 | #endif |
mkarlsso | 3:d7b0a0890d96 | 152 | |
mkarlsso | 0:8dbd6bd9167f | 153 | //The sound output uses a SmartWav device and their simple serial library |
alustig3 | 9:f15ad8d18881 | 154 | //SMARTWAV audio(p28,p27,p19); //(TX,RX,Reset); |
alustig3 | 9:f15ad8d18881 | 155 | SOMO audio(p28,p27); //(TX,RX,Reset) |
mkarlsso | 0:8dbd6bd9167f | 156 | |
mkarlsso | 0:8dbd6bd9167f | 157 | //This is the callback for the MBED timer |
mkarlsso | 0:8dbd6bd9167f | 158 | extern "C" void TIMER0_IRQHandler (void) { |
mkarlsso | 0:8dbd6bd9167f | 159 | |
mkarlsso | 5:abee20c0bf2a | 160 | /*if (clockSlave) { |
mkarlsso | 5:abee20c0bf2a | 161 | |
mkarlsso | 3:d7b0a0890d96 | 162 | //The function is called every 100 us |
mkarlsso | 5:abee20c0bf2a | 163 | if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed |
mkarlsso | 5:abee20c0bf2a | 164 | |
mkarlsso | 5:abee20c0bf2a | 165 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
mkarlsso | 5:abee20c0bf2a | 166 | uSec_SinceLastClockInc = uSec_SinceLastClockInc+100; |
mkarlsso | 5:abee20c0bf2a | 167 | uSec_SinceLastReset = uSec_SinceLastReset+100; |
mkarlsso | 5:abee20c0bf2a | 168 | |
mkarlsso | 5:abee20c0bf2a | 169 | } |
mkarlsso | 5:abee20c0bf2a | 170 | |
mkarlsso | 5:abee20c0bf2a | 171 | |
mkarlsso | 5:abee20c0bf2a | 172 | } else {*/ |
mkarlsso | 3:d7b0a0890d96 | 173 | //The function is called every 1 ms |
mkarlsso | 3:d7b0a0890d96 | 174 | if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed |
mkarlsso | 0:8dbd6bd9167f | 175 | |
mkarlsso | 3:d7b0a0890d96 | 176 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
mkarlsso | 3:d7b0a0890d96 | 177 | timeKeeper++; |
mkarlsso | 0:8dbd6bd9167f | 178 | |
mkarlsso | 3:d7b0a0890d96 | 179 | if (resetTimer) { |
mkarlsso | 3:d7b0a0890d96 | 180 | timeKeeper = 0; |
mkarlsso | 3:d7b0a0890d96 | 181 | resetTimer = false; |
mkarlsso | 3:d7b0a0890d96 | 182 | } |
mkarlsso | 0:8dbd6bd9167f | 183 | } |
mkarlsso | 5:abee20c0bf2a | 184 | //} |
mkarlsso | 3:d7b0a0890d96 | 185 | |
mkarlsso | 0:8dbd6bd9167f | 186 | } |
mkarlsso | 0:8dbd6bd9167f | 187 | //----------------------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 188 | |
mkarlsso | 0:8dbd6bd9167f | 189 | |
mkarlsso | 0:8dbd6bd9167f | 190 | |
mkarlsso | 0:8dbd6bd9167f | 191 | MBEDSystem::MBEDSystem(): |
alustig3 | 9:f15ad8d18881 | 192 | clockResetInt(p29), |
alustig3 | 9:f15ad8d18881 | 193 | clockExternalIncrement(p30) { |
mkarlsso | 0:8dbd6bd9167f | 194 | |
mkarlsso | 0:8dbd6bd9167f | 195 | clockResetInt.rise(this, &MBEDSystem::externalClockReset); |
mkarlsso | 3:d7b0a0890d96 | 196 | clockResetInt.fall(&externalResetDown); |
mkarlsso | 0:8dbd6bd9167f | 197 | clockResetInt.mode(PullDown); |
mkarlsso | 0:8dbd6bd9167f | 198 | |
mkarlsso | 0:8dbd6bd9167f | 199 | clockExternalIncrement.mode(PullDown); |
mkarlsso | 0:8dbd6bd9167f | 200 | |
mkarlsso | 0:8dbd6bd9167f | 201 | #ifdef MBED_RF |
mkarlsso | 0:8dbd6bd9167f | 202 | //Karpova version------------- |
mkarlsso | 0:8dbd6bd9167f | 203 | //Set up callbacks for RF clock signal |
mkarlsso | 0:8dbd6bd9167f | 204 | RFClock.rise(&callback_RFClock); |
mkarlsso | 0:8dbd6bd9167f | 205 | RFClock.mode(PullDown); |
mkarlsso | 0:8dbd6bd9167f | 206 | #endif |
mkarlsso | 0:8dbd6bd9167f | 207 | |
mkarlsso | 0:8dbd6bd9167f | 208 | //------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 209 | |
mkarlsso | 0:8dbd6bd9167f | 210 | for (int i=0; i < NUMPORTS; i++) { |
mkarlsso | 0:8dbd6bd9167f | 211 | dIn[i].init(i); |
mkarlsso | 0:8dbd6bd9167f | 212 | dOut[i].init(i); |
mkarlsso | 0:8dbd6bd9167f | 213 | } |
mkarlsso | 0:8dbd6bd9167f | 214 | |
mkarlsso | 0:8dbd6bd9167f | 215 | |
alustig3 | 9:f15ad8d18881 | 216 | audio.reset(); |
mkarlsso | 0:8dbd6bd9167f | 217 | |
mkarlsso | 0:8dbd6bd9167f | 218 | } |
mkarlsso | 0:8dbd6bd9167f | 219 | |
mkarlsso | 0:8dbd6bd9167f | 220 | void MBEDSystem::timerinit() { |
mkarlsso | 0:8dbd6bd9167f | 221 | //intiatiation of timer (specific to the LPC17xx chip). This is used in |
mkarlsso | 0:8dbd6bd9167f | 222 | //standalone mode to increment the clock every ms. |
mkarlsso | 0:8dbd6bd9167f | 223 | //---------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 224 | //LPC_SC->PCLKSEL1 &= (3 << 12); //mask |
mkarlsso | 0:8dbd6bd9167f | 225 | //LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual) |
mkarlsso | 0:8dbd6bd9167f | 226 | //LPC_SC->PCLKSEL0 &= (3 << 3); //mask |
mkarlsso | 0:8dbd6bd9167f | 227 | //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual) |
mkarlsso | 3:d7b0a0890d96 | 228 | LPC_SC->PCONP |=1<1; //timer0 power on |
mkarlsso | 5:abee20c0bf2a | 229 | |
mkarlsso | 5:abee20c0bf2a | 230 | |
mkarlsso | 0:8dbd6bd9167f | 231 | LPC_TIM0->MR0 = 23980; //1 msec |
mkarlsso | 3:d7b0a0890d96 | 232 | |
mkarlsso | 5:abee20c0bf2a | 233 | |
mkarlsso | 0:8dbd6bd9167f | 234 | //LPC_TIM0->PR = (SystemCoreClock / 1000000); //microsecond steps |
mkarlsso | 0:8dbd6bd9167f | 235 | //LPC_TIM0->MR0 = 1000; //100 msec |
mkarlsso | 0:8dbd6bd9167f | 236 | //LPC_TIM0->MR0 = (SystemCoreClock / 1000000); //microsecond steps |
mkarlsso | 0:8dbd6bd9167f | 237 | LPC_TIM0->MCR = 3; //interrupt and reset control |
mkarlsso | 0:8dbd6bd9167f | 238 | //3 = Interrupt & reset timer0 on match |
mkarlsso | 0:8dbd6bd9167f | 239 | //1 = Interrupt only, no reset of timer0 |
mkarlsso | 0:8dbd6bd9167f | 240 | NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt |
mkarlsso | 0:8dbd6bd9167f | 241 | LPC_TIM0->TCR = 1; //enable Timer0 |
mkarlsso | 0:8dbd6bd9167f | 242 | //-------------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 243 | } |
mkarlsso | 0:8dbd6bd9167f | 244 | |
mkarlsso | 3:d7b0a0890d96 | 245 | void MBEDSystem::pauseInterrupts() { |
mkarlsso | 3:d7b0a0890d96 | 246 | __disable_irq(); |
mkarlsso | 3:d7b0a0890d96 | 247 | } |
mkarlsso | 3:d7b0a0890d96 | 248 | |
mkarlsso | 3:d7b0a0890d96 | 249 | void MBEDSystem::resumeInterrupts() { |
mkarlsso | 3:d7b0a0890d96 | 250 | __enable_irq(); |
mkarlsso | 3:d7b0a0890d96 | 251 | } |
mkarlsso | 3:d7b0a0890d96 | 252 | |
mkarlsso | 0:8dbd6bd9167f | 253 | void MBEDSystem::mainLoopToDo() { |
mkarlsso | 0:8dbd6bd9167f | 254 | #ifdef MBED_RF |
mkarlsso | 0:8dbd6bd9167f | 255 | //Karpova version-------------------------- |
mkarlsso | 0:8dbd6bd9167f | 256 | //Karpova lab RF addition. Print out time sync. |
mkarlsso | 0:8dbd6bd9167f | 257 | if (RFSyncReadable) { |
mkarlsso | 0:8dbd6bd9167f | 258 | /* |
mkarlsso | 0:8dbd6bd9167f | 259 | ostringstream RFTimeConvert; |
mkarlsso | 0:8dbd6bd9167f | 260 | RFTimeConvert << timeKeeperAtLastRFTime << " " << "RFsync " << lastRFTime << " "; //broadcast the earliest timestamp when a change occured |
mkarlsso | 0:8dbd6bd9167f | 261 | |
mkarlsso | 0:8dbd6bd9167f | 262 | textDisplay.send(RFTimeConvert.str() + "\r\n"); |
mkarlsso | 0:8dbd6bd9167f | 263 | RFTimeConvert.clear(); |
mkarlsso | 0:8dbd6bd9167f | 264 | RFTimeConvert.seekp(0); |
mkarlsso | 0:8dbd6bd9167f | 265 | */ |
mkarlsso | 0:8dbd6bd9167f | 266 | |
mkarlsso | 0:8dbd6bd9167f | 267 | textDisplay << timeKeeperAtLastRFTime << " RFsync " << lastRFTime << "\r\n"; |
mkarlsso | 0:8dbd6bd9167f | 268 | |
mkarlsso | 0:8dbd6bd9167f | 269 | RFSyncReadable = false; |
mkarlsso | 0:8dbd6bd9167f | 270 | RFSyncWritable = true; |
mkarlsso | 0:8dbd6bd9167f | 271 | } |
mkarlsso | 0:8dbd6bd9167f | 272 | //------------------------------------------------------------ |
mkarlsso | 0:8dbd6bd9167f | 273 | #endif |
mkarlsso | 0:8dbd6bd9167f | 274 | } |
mkarlsso | 0:8dbd6bd9167f | 275 | |
mkarlsso | 0:8dbd6bd9167f | 276 | sDigitalOut* MBEDSystem::getDigitalOutPtr(int portNum){ |
mkarlsso | 0:8dbd6bd9167f | 277 | if (portNum < NUMPORTS) { |
mkarlsso | 0:8dbd6bd9167f | 278 | return &dOut[portNum]; |
mkarlsso | 0:8dbd6bd9167f | 279 | } else { |
mkarlsso | 0:8dbd6bd9167f | 280 | return NULL; |
mkarlsso | 0:8dbd6bd9167f | 281 | } |
mkarlsso | 0:8dbd6bd9167f | 282 | } |
mkarlsso | 0:8dbd6bd9167f | 283 | |
mkarlsso | 0:8dbd6bd9167f | 284 | sDigitalIn* MBEDSystem::getDigitalInPtr(int portNum) { |
mkarlsso | 0:8dbd6bd9167f | 285 | if (portNum < NUMPORTS) { |
mkarlsso | 0:8dbd6bd9167f | 286 | return &dIn[portNum]; |
mkarlsso | 0:8dbd6bd9167f | 287 | } else { |
mkarlsso | 0:8dbd6bd9167f | 288 | return NULL; |
mkarlsso | 0:8dbd6bd9167f | 289 | } |
mkarlsso | 0:8dbd6bd9167f | 290 | } |
mkarlsso | 0:8dbd6bd9167f | 291 | |
mkarlsso | 0:8dbd6bd9167f | 292 | sSound* MBEDSystem::createNewSoundAction() { |
mkarlsso | 0:8dbd6bd9167f | 293 | MBEDSound *tmpSound = new MBEDSound(); |
mkarlsso | 0:8dbd6bd9167f | 294 | return tmpSound; |
mkarlsso | 0:8dbd6bd9167f | 295 | } |
mkarlsso | 0:8dbd6bd9167f | 296 | |
mkarlsso | 0:8dbd6bd9167f | 297 | |
mkarlsso | 0:8dbd6bd9167f | 298 | void MBEDSystem::externalClockReset() { |
mkarlsso | 0:8dbd6bd9167f | 299 | |
mkarlsso | 3:d7b0a0890d96 | 300 | //The pulse has gone high. When the pulse comes down we will check to see if it was long enough to be valid. |
mkarlsso | 3:d7b0a0890d96 | 301 | uSec_SinceLastReset = 0; |
mkarlsso | 3:d7b0a0890d96 | 302 | |
mkarlsso | 3:d7b0a0890d96 | 303 | |
mkarlsso | 5:abee20c0bf2a | 304 | |
mkarlsso | 0:8dbd6bd9167f | 305 | if (clockSlave) { |
mkarlsso | 0:8dbd6bd9167f | 306 | LPC_TIM0->TCR = 0x02; // reset timer |
mkarlsso | 0:8dbd6bd9167f | 307 | externalIncrementCounter = 0; |
mkarlsso | 5:abee20c0bf2a | 308 | |
mkarlsso | 0:8dbd6bd9167f | 309 | immediateClockReset(); |
mkarlsso | 5:abee20c0bf2a | 310 | } |
mkarlsso | 0:8dbd6bd9167f | 311 | |
mkarlsso | 0:8dbd6bd9167f | 312 | } |
mkarlsso | 0:8dbd6bd9167f | 313 | |
mkarlsso | 0:8dbd6bd9167f | 314 | void MBEDSystem::setStandAloneClock() { |
mkarlsso | 3:d7b0a0890d96 | 315 | clockSlave = false; |
mkarlsso | 3:d7b0a0890d96 | 316 | NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt |
mkarlsso | 3:d7b0a0890d96 | 317 | timerinit(); //set up and enable interrupt |
mkarlsso | 0:8dbd6bd9167f | 318 | clockExternalIncrement.rise(NULL); //remove the callback to the external interrupt |
mkarlsso | 3:d7b0a0890d96 | 319 | //clockExternalIncrement.fall(NULL); |
mkarlsso | 0:8dbd6bd9167f | 320 | changeToSlave = false; |
mkarlsso | 0:8dbd6bd9167f | 321 | changeToStandAlone = false; |
mkarlsso | 0:8dbd6bd9167f | 322 | } |
mkarlsso | 0:8dbd6bd9167f | 323 | |
mkarlsso | 0:8dbd6bd9167f | 324 | void MBEDSystem::setSlaveClock() { |
mkarlsso | 3:d7b0a0890d96 | 325 | clockSlave = true; |
mkarlsso | 0:8dbd6bd9167f | 326 | NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt |
mkarlsso | 3:d7b0a0890d96 | 327 | //timerinit(); //set up and enable interrupt |
mkarlsso | 0:8dbd6bd9167f | 328 | clockExternalIncrement.rise(this, &MBEDSystem::incrementClock); |
mkarlsso | 3:d7b0a0890d96 | 329 | //clockExternalIncrement.fall(&externalClockIncDown); |
mkarlsso | 0:8dbd6bd9167f | 330 | clockSlave = true; |
mkarlsso | 0:8dbd6bd9167f | 331 | changeToSlave = false; |
mkarlsso | 0:8dbd6bd9167f | 332 | changeToStandAlone = false; |
mkarlsso | 0:8dbd6bd9167f | 333 | } |
mkarlsso | 0:8dbd6bd9167f | 334 | |
mkarlsso | 0:8dbd6bd9167f | 335 | void MBEDSystem::incrementClock() { |
mkarlsso | 0:8dbd6bd9167f | 336 | |
mkarlsso | 0:8dbd6bd9167f | 337 | if (clockSlave) { |
mkarlsso | 3:d7b0a0890d96 | 338 | //The pulse has gone high. When the pulse comes down we will check to see if it was long enough to be valid. |
mkarlsso | 3:d7b0a0890d96 | 339 | //uSec_SinceLastClockInc = 0; |
mkarlsso | 3:d7b0a0890d96 | 340 | |
mkarlsso | 3:d7b0a0890d96 | 341 | |
mkarlsso | 3:d7b0a0890d96 | 342 | //The clock is incremented |
mkarlsso | 3:d7b0a0890d96 | 343 | |
mkarlsso | 0:8dbd6bd9167f | 344 | externalIncrementCounter = (externalIncrementCounter+1) % externalIncrementMod; |
mkarlsso | 0:8dbd6bd9167f | 345 | if (externalIncrementCounter==0) { |
mkarlsso | 0:8dbd6bd9167f | 346 | timeKeeper++; |
mkarlsso | 0:8dbd6bd9167f | 347 | } |
mkarlsso | 0:8dbd6bd9167f | 348 | |
mkarlsso | 0:8dbd6bd9167f | 349 | } else { |
mkarlsso | 0:8dbd6bd9167f | 350 | timeKeeper++; |
mkarlsso | 0:8dbd6bd9167f | 351 | } |
mkarlsso | 0:8dbd6bd9167f | 352 | //Clock resets happen upon update so we dont get a partial first ms |
mkarlsso | 3:d7b0a0890d96 | 353 | /* |
mkarlsso | 0:8dbd6bd9167f | 354 | if (resetTimer) { |
mkarlsso | 3:d7b0a0890d96 | 355 | uSec_SinceLastReset = 0; |
mkarlsso | 0:8dbd6bd9167f | 356 | timeKeeper = 0; |
mkarlsso | 0:8dbd6bd9167f | 357 | resetTimer = false; |
mkarlsso | 3:d7b0a0890d96 | 358 | }*/ |
mkarlsso | 0:8dbd6bd9167f | 359 | } |
mkarlsso | 0:8dbd6bd9167f | 360 | |
mkarlsso | 0:8dbd6bd9167f | 361 | //----------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 362 | |
mkarlsso | 0:8dbd6bd9167f | 363 | MBEDSound::MBEDSound() { |
mkarlsso | 0:8dbd6bd9167f | 364 | |
mkarlsso | 0:8dbd6bd9167f | 365 | } |
mkarlsso | 0:8dbd6bd9167f | 366 | |
mkarlsso | 0:8dbd6bd9167f | 367 | void MBEDSound::execute() { |
mkarlsso | 0:8dbd6bd9167f | 368 | if (reset) { |
alustig3 | 9:f15ad8d18881 | 369 | audio.reset(); |
mkarlsso | 0:8dbd6bd9167f | 370 | } else if (!play) { |
alustig3 | 9:f15ad8d18881 | 371 | audio.stopTrack(); |
alustig3 | 9:f15ad8d18881 | 372 | } else if (fileNum){ |
alustig3 | 9:f15ad8d18881 | 373 | audio.playTrackNum(fileNum); |
mkarlsso | 0:8dbd6bd9167f | 374 | } else { |
mkarlsso | 0:8dbd6bd9167f | 375 | if (volume > -1) { |
alustig3 | 9:f15ad8d18881 | 376 | audio.volume(volume); |
mkarlsso | 0:8dbd6bd9167f | 377 | } else if (volumePtr != NULL) { |
alustig3 | 9:f15ad8d18881 | 378 | audio.volume(*volumePtr); |
mkarlsso | 0:8dbd6bd9167f | 379 | } |
mkarlsso | 0:8dbd6bd9167f | 380 | if (fileNameExists) { |
alustig3 | 9:f15ad8d18881 | 381 | audio.stopTrack(); |
alustig3 | 9:f15ad8d18881 | 382 | wait_ms(30); //without this delay, smartWAV shield tends to freeze up |
alustig3 | 9:f15ad8d18881 | 383 | audio.playTrackName(fileName); |
mkarlsso | 0:8dbd6bd9167f | 384 | } |
mkarlsso | 0:8dbd6bd9167f | 385 | } |
mkarlsso | 0:8dbd6bd9167f | 386 | } |
mkarlsso | 0:8dbd6bd9167f | 387 | |
mkarlsso | 0:8dbd6bd9167f | 388 | //----------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 389 | MBEDDigitalOut::MBEDDigitalOut() { |
mkarlsso | 5:abee20c0bf2a | 390 | pinExists = false; |
mkarlsso | 0:8dbd6bd9167f | 391 | } |
mkarlsso | 0:8dbd6bd9167f | 392 | |
mkarlsso | 0:8dbd6bd9167f | 393 | void MBEDDigitalOut::init(int pin) { |
mkarlsso | 5:abee20c0bf2a | 394 | if (pin < NUMOUTPORTS) { |
mkarlsso | 5:abee20c0bf2a | 395 | outpin = new DigitalOut(outPins[pin]); |
mkarlsso | 5:abee20c0bf2a | 396 | pinExists = true; |
mkarlsso | 5:abee20c0bf2a | 397 | } |
mkarlsso | 0:8dbd6bd9167f | 398 | } |
mkarlsso | 0:8dbd6bd9167f | 399 | |
mkarlsso | 0:8dbd6bd9167f | 400 | int MBEDDigitalOut::read() { |
mkarlsso | 5:abee20c0bf2a | 401 | if (pinExists) { |
mkarlsso | 5:abee20c0bf2a | 402 | return outpin->read(); |
mkarlsso | 5:abee20c0bf2a | 403 | } else { |
mkarlsso | 5:abee20c0bf2a | 404 | return 0; |
mkarlsso | 5:abee20c0bf2a | 405 | } |
mkarlsso | 0:8dbd6bd9167f | 406 | } |
mkarlsso | 0:8dbd6bd9167f | 407 | |
mkarlsso | 0:8dbd6bd9167f | 408 | void MBEDDigitalOut::write(int value) { |
mkarlsso | 5:abee20c0bf2a | 409 | if (pinExists) { |
mkarlsso | 5:abee20c0bf2a | 410 | |
mkarlsso | 5:abee20c0bf2a | 411 | outpin->write(value); |
mkarlsso | 5:abee20c0bf2a | 412 | } |
mkarlsso | 0:8dbd6bd9167f | 413 | } |
mkarlsso | 0:8dbd6bd9167f | 414 | //-------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 415 | |
mkarlsso | 0:8dbd6bd9167f | 416 | MBEDDigitalIn::MBEDDigitalIn() { |
mkarlsso | 5:abee20c0bf2a | 417 | pinExists = false; |
mkarlsso | 0:8dbd6bd9167f | 418 | } |
mkarlsso | 0:8dbd6bd9167f | 419 | |
mkarlsso | 0:8dbd6bd9167f | 420 | void MBEDDigitalIn::init(int pin) { |
mkarlsso | 5:abee20c0bf2a | 421 | |
mkarlsso | 5:abee20c0bf2a | 422 | if (pin < NUMINPORTS) { |
mkarlsso | 5:abee20c0bf2a | 423 | inpin = new DigitalIn(inPins[pin]); |
mkarlsso | 5:abee20c0bf2a | 424 | inpin_interrupt = new InterruptIn(inPins[pin]); |
mkarlsso | 5:abee20c0bf2a | 425 | inpin->mode(PullDown); |
mkarlsso | 5:abee20c0bf2a | 426 | //Set up callbacks for the port interrupts |
mkarlsso | 5:abee20c0bf2a | 427 | inpin_interrupt->rise(this, &MBEDDigitalIn::interrupt_up_callback); |
mkarlsso | 5:abee20c0bf2a | 428 | inpin_interrupt->fall(this, &MBEDDigitalIn::interrupt_down_callback); |
mkarlsso | 5:abee20c0bf2a | 429 | pinExists = true; |
mkarlsso | 5:abee20c0bf2a | 430 | } |
mkarlsso | 0:8dbd6bd9167f | 431 | } |
mkarlsso | 0:8dbd6bd9167f | 432 | |
mkarlsso | 0:8dbd6bd9167f | 433 | int MBEDDigitalIn::read() { |
mkarlsso | 5:abee20c0bf2a | 434 | if (pinExists) { |
mkarlsso | 5:abee20c0bf2a | 435 | return inpin->read(); |
mkarlsso | 5:abee20c0bf2a | 436 | } else { |
mkarlsso | 5:abee20c0bf2a | 437 | return 0; |
mkarlsso | 5:abee20c0bf2a | 438 | } |
mkarlsso | 0:8dbd6bd9167f | 439 | } |
mkarlsso | 0:8dbd6bd9167f | 440 | |
mkarlsso | 0:8dbd6bd9167f | 441 | void MBEDDigitalIn::interrupt_up_callback() { |
mkarlsso | 0:8dbd6bd9167f | 442 | addStateChange(1, timeKeeper); |
mkarlsso | 0:8dbd6bd9167f | 443 | } |
mkarlsso | 0:8dbd6bd9167f | 444 | |
mkarlsso | 0:8dbd6bd9167f | 445 | void MBEDDigitalIn::interrupt_down_callback() { |
mkarlsso | 0:8dbd6bd9167f | 446 | addStateChange(0, timeKeeper); |
mkarlsso | 0:8dbd6bd9167f | 447 | } |
mkarlsso | 0:8dbd6bd9167f | 448 | |
mkarlsso | 0:8dbd6bd9167f | 449 | //---------------------------------------------------------- |
mkarlsso | 0:8dbd6bd9167f | 450 | MBEDSerialPort::MBEDSerialPort() { |
mkarlsso | 0:8dbd6bd9167f | 451 | |
mkarlsso | 0:8dbd6bd9167f | 452 | } |
mkarlsso | 0:8dbd6bd9167f | 453 | |
mkarlsso | 0:8dbd6bd9167f | 454 | void MBEDSerialPort::init() { |
mkarlsso | 0:8dbd6bd9167f | 455 | //Initialize serial communication |
mkarlsso | 0:8dbd6bd9167f | 456 | serialToPC = new Serial(USBTX, USBRX); // tx, rx |
mkarlsso | 0:8dbd6bd9167f | 457 | serialToPC->baud(115200); |
mkarlsso | 0:8dbd6bd9167f | 458 | |
mkarlsso | 0:8dbd6bd9167f | 459 | |
mkarlsso | 0:8dbd6bd9167f | 460 | } |
mkarlsso | 0:8dbd6bd9167f | 461 | |
mkarlsso | 0:8dbd6bd9167f | 462 | bool MBEDSerialPort::readable() { |
mkarlsso | 0:8dbd6bd9167f | 463 | return serialToPC->readable(); |
mkarlsso | 0:8dbd6bd9167f | 464 | } |
mkarlsso | 0:8dbd6bd9167f | 465 | |
mkarlsso | 0:8dbd6bd9167f | 466 | char MBEDSerialPort::readChar() { |
mkarlsso | 0:8dbd6bd9167f | 467 | return serialToPC->getc(); |
mkarlsso | 0:8dbd6bd9167f | 468 | } |
mkarlsso | 0:8dbd6bd9167f | 469 | |
mkarlsso | 0:8dbd6bd9167f | 470 | void MBEDSerialPort::writeChar(char s) { |
mkarlsso | 0:8dbd6bd9167f | 471 | serialToPC->printf("%c", s); |
mkarlsso | 0:8dbd6bd9167f | 472 | } |
mkarlsso | 3:d7b0a0890d96 | 473 | |
mkarlsso | 3:d7b0a0890d96 | 474 | int MBEDSerialPort::requestToWriteString(char *s, int numBytesRequested) { |
mkarlsso | 3:d7b0a0890d96 | 475 | //request to print a string to the serial output buffer |
mkarlsso | 3:d7b0a0890d96 | 476 | //function returns the number of chars actually accepted for output |
mkarlsso | 3:d7b0a0890d96 | 477 | int numBytesAccepted = 0; |
mkarlsso | 3:d7b0a0890d96 | 478 | while (numBytesAccepted < numBytesRequested) { |
mkarlsso | 3:d7b0a0890d96 | 479 | |
mkarlsso | 3:d7b0a0890d96 | 480 | writeChar(*(s+numBytesAccepted)); |
mkarlsso | 3:d7b0a0890d96 | 481 | numBytesAccepted++; |
mkarlsso | 3:d7b0a0890d96 | 482 | } |
mkarlsso | 3:d7b0a0890d96 | 483 | |
mkarlsso | 3:d7b0a0890d96 | 484 | return numBytesAccepted; |
mkarlsso | 3:d7b0a0890d96 | 485 | } |