Karpova Lab fork of stateScript
Dependencies: SMARTWAV SOMO_II mbed
Fork of stateScript_v2_karpova by
Diff: mbedInterface/mbedInterface.cpp
- Revision:
- 3:d7b0a0890d96
- Parent:
- 0:8dbd6bd9167f
- Child:
- 5:abee20c0bf2a
diff -r 35266b266eaa -r d7b0a0890d96 mbedInterface/mbedInterface.cpp --- a/mbedInterface/mbedInterface.cpp Wed Jun 03 23:41:16 2015 +0000 +++ b/mbedInterface/mbedInterface.cpp Sat Oct 10 22:37:17 2015 +0000 @@ -9,6 +9,9 @@ //globals defined in hardwareInterface.cpp extern uint32_t timeKeeper; //the master clock +uint32_t uSec_SinceLastClockInc = 0; +uint32_t uSec_SinceLastReset = 0; + extern bool resetTimer; extern bool clockSlave; extern bool changeToSlave; @@ -20,7 +23,7 @@ #ifdef MBED_RF -//Karpova version---------------------------------------- +//We are listening to an RF signal for hardware syncing---------------------------------------- //Uses DB9->RJ45 connector to map the following channels: //1: P28 Clock Signal In //2: P27 Data Signal In @@ -93,7 +96,34 @@ //------------------------------------------------------------ #endif +void externalClockIncDown() { + //The external clock increment signal pulse has come back down. If the pulse was long + //enough, then we condsider it a valid pulse (the pulses should be 0.5 ms long) + if ((clockSlave)&&(uSec_SinceLastClockInc >= 300)) { + uSec_SinceLastClockInc = 0; + timeKeeper++; + + //Clock resets happen upon update so we dont get a partial first ms + if (resetTimer) { + uSec_SinceLastReset = 0; + timeKeeper = 0; + resetTimer = false; + } + } + +} + +void externalResetDown() { + + //The external clock reset signal pulse has come back down. If the pulse was long + //enough, then we condsider it a valid pulse (the pulses should be 1 ms long) + if ((clockSlave)&&(uSec_SinceLastReset >= 700)) { + uSec_SinceLastReset = 0; + timeKeeper = 1; //It has been 1ms since the reset pulse went up + textDisplay << timeKeeper << " Clock reset\r\n"; + } +} //------------------------------------------------------------------------ //------------------------------------------------------------------------ @@ -102,27 +132,37 @@ //--------------------------------------------------------------------- //translate pin numbers to hardware pins -PinName outPins[NUMPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20}; +//PinName outPins[NUMPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20}; +PinName outPins[NUMPORTS] = {p18,p15,p13,p11,p29,p25,p23,p21,p20}; PinName inPins[NUMPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7}; + //The sound output uses a SmartWav device and their simple serial library SMARTWAV sWav(p9,p10,p19); //(TX,RX,Reset); //This is the callback for the MBED timer extern "C" void TIMER0_IRQHandler (void) { - if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed + if (clockSlave) { + //The function is called every 100 us + uSec_SinceLastClockInc = uSec_SinceLastClockInc+100; + uSec_SinceLastReset = uSec_SinceLastReset+100; + } else { + //The function is called every 1 ms + if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed - LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag - timeKeeper++; + LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag + timeKeeper++; - if (resetTimer) { - timeKeeper = 0; - resetTimer = false; + if (resetTimer) { + timeKeeper = 0; + resetTimer = false; + } } } + } //----------------------------------------------------------------------- @@ -133,6 +173,7 @@ clockExternalIncrement(p8) { clockResetInt.rise(this, &MBEDSystem::externalClockReset); + clockResetInt.fall(&externalResetDown); clockResetInt.mode(PullDown); clockExternalIncrement.mode(PullDown); @@ -165,8 +206,9 @@ //LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual) //LPC_SC->PCLKSEL0 &= (3 << 3); //mask //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual) - LPC_SC->PCONP |=1<1; //timer0 power on + LPC_SC->PCONP |=1<1; //timer0 power on LPC_TIM0->MR0 = 23980; //1 msec + //LPC_TIM0->PR = (SystemCoreClock / 1000000); //microsecond steps //LPC_TIM0->MR0 = 1000; //100 msec //LPC_TIM0->MR0 = (SystemCoreClock / 1000000); //microsecond steps @@ -178,6 +220,14 @@ //-------------------------------------------------------------- } +void MBEDSystem::pauseInterrupts() { + __disable_irq(); +} + +void MBEDSystem::resumeInterrupts() { + __enable_irq(); +} + void MBEDSystem::mainLoopToDo() { #ifdef MBED_RF //Karpova version-------------------------- @@ -225,25 +275,35 @@ void MBEDSystem::externalClockReset() { + //The pulse has gone high. When the pulse comes down we will check to see if it was long enough to be valid. + uSec_SinceLastReset = 0; + + + /* if (clockSlave) { LPC_TIM0->TCR = 0x02; // reset timer externalIncrementCounter = 0; immediateClockReset(); - } + }*/ } void MBEDSystem::setStandAloneClock() { - timerinit(); + clockSlave = false; + NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt + timerinit(); //set up and enable interrupt clockExternalIncrement.rise(NULL); //remove the callback to the external interrupt - clockSlave = false; + //clockExternalIncrement.fall(NULL); changeToSlave = false; changeToStandAlone = false; } void MBEDSystem::setSlaveClock() { + clockSlave = true; NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt + //timerinit(); //set up and enable interrupt clockExternalIncrement.rise(this, &MBEDSystem::incrementClock); + //clockExternalIncrement.fall(&externalClockIncDown); clockSlave = true; changeToSlave = false; changeToStandAlone = false; @@ -252,6 +312,12 @@ void MBEDSystem::incrementClock() { if (clockSlave) { + //The pulse has gone high. When the pulse comes down we will check to see if it was long enough to be valid. + //uSec_SinceLastClockInc = 0; + + + //The clock is incremented + externalIncrementCounter = (externalIncrementCounter+1) % externalIncrementMod; if (externalIncrementCounter==0) { timeKeeper++; @@ -261,10 +327,12 @@ timeKeeper++; } //Clock resets happen upon update so we dont get a partial first ms + /* if (resetTimer) { + uSec_SinceLastReset = 0; timeKeeper = 0; resetTimer = false; - } + }*/ } //----------------------------------------------------- @@ -360,3 +428,16 @@ void MBEDSerialPort::writeChar(char s) { serialToPC->printf("%c", s); } + +int MBEDSerialPort::requestToWriteString(char *s, int numBytesRequested) { + //request to print a string to the serial output buffer + //function returns the number of chars actually accepted for output + int numBytesAccepted = 0; + while (numBytesAccepted < numBytesRequested) { + + writeChar(*(s+numBytesAccepted)); + numBytesAccepted++; + } + + return numBytesAccepted; +}