int main() { int aliveLed = 0; static int BlinkCount = 0; init_platform(); if(SetupPeripherals() != XST_SUCCESS) return -1; if ( Chilipepper_Initialize() != 0 ) return -1; Chilipepper_SetPA( 1 ); Chilipepper_SetTxRxSw( 1 ); // 0- transmit, 1-receive while (1) { Chilipepper_ControlAgc(); //update the Chilipepper AGC BlinkCount += 1; if (BlinkCount > 500000) { if (aliveLed == 0) aliveLed = 1; else aliveLed = 0; BlinkCount = 1; XGpio_DiscreteWrite(&gpio_blinky, 2, aliveLed); //blink LEDs XGpio_DiscreteWrite(&gpio_blinky, 1, ~aliveLed); } } cleanup_platform(); return 0; }
int main ( void ) { /** pin buttons locals */ volatile bool regPinButton1; volatile bool regPinButton2; /** speed local */ short iRefSpeed; // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 8*40/(2*2)= 80Mhz for 8M input clock PLLFBD = 38; // M=40 CLKDIVbits.PLLPOST = 0; // N1=2 CLKDIVbits.PLLPRE = 0; // N2=2 // while(OSCCONbits.LOCK != 1) {}; // Wait for PLL to loc /** setup dsPIC ports */ SetupPorts(); /** init user parameters */ InitUserParms(); /** init user specified parms and stop on error */ if( SetupPeripherals() ) { /* Error */ return 0; } uGF.Word = 0; // clear flags while(1) { // init Mode eStateControl = CNTRL_STOP; iLockLoopCnt = 0; /** clear the variables other than STOP command */ uGF.bit.TLock = 0; uGF.bit.Btn1Pressed = 0; uGF.bit.Btn2Pressed = 0; uGF.bit.DoSnap = 0; uGF.bit.SnapDone = 0; /** ============= Open Loop ======================*/ OpenLoopParm.qVelMech = dqOL_VelMech; CtrlParm.qVelRef = OpenLoopParm.qVelMech; iRefSpeed = CtrlParm.qVelRef; InitOpenLoop(); /** Inital offsets for currents */ InitMeasCompCurr( 450, 730 ); // init board SetupBoard(); // Enable ADC interrupt and begin main loop timing IFS0bits.AD1IF = 0; IEC0bits.AD1IE = 1; /** Initialize private variables used by CalcVelIrp. */ InitCalcVel(); /** Initialize private variables used by CurrModel */ InitCurModel(); // zero out i sums PIParmD.qdSum = 0; PIParmQ.qdSum = 0; PIParmQref.qdSum = 0; // zero out i out PIParmD.qOut = 0; PIParmQ.qOut = 0; PIParmQref.qOut = 0; /** if state is stop */ if(eStateControl == CNTRL_STOP) { //wait here until button 1 is pressed or stop time elapsed while(uGF.bit.TStop){}; while(!pinButton1) { /** make sure that the STOP do not occur due to this loop */ iStopLoopCnt = 0; // Start offset accumulation //and accumulate current offset while waiting MeasCompCurr(); } while(pinButton1); //when button 1 is released eStateControl = CNTRL_OPEN_LOOP; //then start motor in open loop } // Run the motor uGF.bit.ChangeMode = 1; // Enable the driver IC on the motor control PCB pinPWMOutputEnable_ = 0; //Run Motor loop while(1) { /** algorithm was stopped before*/ if(uGF.bit.TStop) { /* break the while */ break; } /* for logic of this application 2 buttons are used S3 and S6. If both buttons are pressed the state of the application is commuted between OPEN_LOOP, CLOSED_LOOP and CLOSED_LOOP_DOUBLE_SPEED. The touch of only one button will increase the speed in one of the states enumerated above */ /* read the buttons state */ /* while no buttons are pushed */ do{ regPinButton1 = pinButton1; /* pin button 1 */ /* retain if pressed */ if(regPinButton1 == 1) { uGF.bit.Btn1Pressed=1; } regPinButton2 = pinButton2; /* pin button 2 */ /* retain if pressed */ if(regPinButton2 == 1) { uGF.bit.Btn2Pressed=1; } /** make sure that the STOP do not occur due to this loop */ iStopLoopCnt = 0; }while((!regPinButton1)&&(!regPinButton2)); /* while buttons are released */ do{ regPinButton1 = pinButton1; /* pin button 1 */ /* retain if pressed */ if(regPinButton1 == 1) { uGF.bit.Btn1Pressed=1; } regPinButton2 = pinButton2; /* pin button 2 */ /* retain if pressed */ if(regPinButton2 == 1) { uGF.bit.Btn2Pressed=1; } }while(pinButton1||pinButton2); /** the counter for STOP is cleared - if buttons released */ iStopLoopCnt = 0; /* check if one of the buttons is pressed */ /* check if both buttons are pressed */ if ((uGF.bit.Btn1Pressed)&&(uGF.bit.Btn2Pressed)) { /* if yes */ /* clear both flags indicating the buttons were pressed */ uGF.bit.Btn1Pressed = 0; uGF.bit.Btn2Pressed = 0; /* and command not locked */ /* check if command not locked */ if(!uGF.bit.TLock) { /* lock command for Tlock_multiple */ uGF.bit.TLock = 1; /* command locked */ iLockLoopCnt = 0; /* reset counter */ /* state machine for control algorithm */ switch(eStateControl) { /* the previous state was STOP */ case CNTRL_OPEN_LOOP: { /* swich the state machine */ eStateControl = CNTRL_CLOSED_LOOP; /* first switch the global flag */ uGF.bit.ChangeMode = 1; break; } case CNTRL_CLOSED_LOOP: { /* double the speed of the motor */ /* and swich the state machine */ eStateControl = CNTRL_CLOSED_LOOP_DBL_SPEED; /* double the speed */ iRefSpeed += iRefSpeed; #ifdef SNAPSHOT uGF.bit.DoSnap = 1; SnapCount = 0; #endif break; } case CNTRL_CLOSED_LOOP_DBL_SPEED: { /* divide by 2 the speed of the motor */ /* and swich the state machine */ eStateControl = CNTRL_CLOSED_LOOP; /* divide the speed by 2 */ iRefSpeed -= iRefSpeed/2; #ifdef SNAPSHOT uGF.bit.DoSnap = 1; SnapCount = 0; #endif break; } default: { /* undefined state, stop */ eStateControl = CNTRL_STOP; /* disable the PWM module */ pinPWMOutputEnable_ = 1; break; } } } } /* check which button was pressed */ /* perhaps button 1 was the one */ else if(uGF.bit.Btn1Pressed) { /* if yes */ /* clear the button flag for future usage */ uGF.bit.Btn1Pressed = 0; /* and command not locked */ /* check if command not locked */ if(!uGF.bit.TLock) { /* lock command for Tlock_multiple */ uGF.bit.TLock = 1; /* command locked */ iLockLoopCnt = 0; /* reset counter */ /* increase speed with SPEED_STEP */ iRefSpeed += SPEED_STEP; } } /* it was button 2, but checking */ else if(uGF.bit.Btn2Pressed) { /* if yes */ /* clear the button flag for future usage */ uGF.bit.Btn2Pressed = 0; /* and command not locked */ /* check if command not locked */ if(!uGF.bit.TLock) { /* lock command for Tlock_multiple */ uGF.bit.TLock = 1; /* command locked */ iLockLoopCnt = 0; /* reset counter */ /* decrease speed with SPEED_STEP */ iRefSpeed -= SPEED_STEP; } } /** limit the reference to 60Hz */ if( iRefSpeed > MAX_SPEED_UP ) { iRefSpeed = MAX_SPEED_UP; } /** limit the reference to 60Hz */ if((signed short)iRefSpeed < (signed short)MAX_SPEED_DOWN ) { iRefSpeed = MAX_SPEED_DOWN; } /** update the global speed reference */ CtrlParm.qVelRef = iRefSpeed; if( uGF.bit.SnapDone ) { uGF.bit.SnapDone=0; } } // End of Run Motor loop } // End of Main loop // should never get here while(1){} }
int main() { int sentCount; int aliveLed = 0; int numBytes; int currentGain; unsigned char id, prevId; int sw, i1; int success; int pa, prevPa; static int testBlinkCount; int txCount = 0, txTryCount = 0, txSuccess; unsigned char numUartRead, curValue; unsigned char rxBuf[256], txBuf[256]; init_platform(); if(SetupPeripherals() != XST_SUCCESS) return -1; if ( Chilipepper_Initialize() != 0 ) return -1; // by default we are in receive Chilipepper_SetPA( 1 ); Chilipepper_SetTxRxSw( 1 ); // 0- transmit, 1-receive // enable the Chilipepper LED to indicate we are operational Chilipepper_SetLed( 1 ); xil_printf("\r\n\r\nWelcome to Toyon's Chilipepper QPSK demo. This demo was written in MATLAB using Mathworks HDL Coder.\r\n\r\n"); //Chilipepper_SetRxGain( 20 ); prevPa = 0; prevId = 0; //Chilipepper_Reset(); while (1) { currentGain = Chilipepper_ControlAgc(); WriteLedGain( currentGain ); pa = XGpio_DiscreteRead(&gpio_sw_tx_pa, 1); if (pa != prevPa) Chilipepper_SetTxGain( pa ); prevPa = pa; sw = XGpio_DiscreteRead(&gpio_sw_test, 1); switch (sw) { case 0: // normal operation // during normal operation adjust the AGC // main priority is to parse OTA packets numBytes = Chilipepper_ReadPacket( rxBuf, &id ); if (numBytes > 0) XGpio_DiscreteWrite(&gpio_blinky, 1, 1); // if ID is zero then this is an ACK - should never see this here // if ID is not zero we need to send a packet back with the payload being the ID // Here we've received the same packet as last time. This means the sender must not // have gotten the ACK we sent. So, let's just send the ACK again, but don't write to UART. if (id != 0 && numBytes > 0 && id == prevId) { // received the same packet again so transmitter must not have gotten ACK Chilipepper_WriteAckPacket( txBuf, id ); XGpio_DiscreteWrite(&gpio_blinky, 1, 0); } // This is a normal receive situation. We get a packet, write it to UART, and send ACK else if ( id != 0 && numBytes > 0) { // first thing we need to do if ID is not zero is send back ACK with payload // being id Chilipepper_WriteAckPacket( txBuf, id ); sentCount = 0; while (sentCount < numBytes) { curValue = rxBuf[sentCount+4]; sentCount += XUartPs_Send(&uartPs, &curValue, 1); } prevId = id; XGpio_DiscreteWrite(&gpio_blinky, 1, 0); } // now that we've gotten the radio stuff out of the way, let's parse things coming over the UART do { numUartRead = XUartPs_Recv(&uartPs, &txBuf[txCount+4], 1); if (numUartRead == 1) txCount++; } while(numUartRead == 1); // only attempt to send something if we have something to send if (txCount > 0) { XGpio_DiscreteWrite(&gpio_blinky, 1, 1); if (txCount >= 10 || txTryCount > 100000) { txSuccess = Chilipepper_WritePacketWithAck( txBuf, txCount, rxBuf ); if (txSuccess == 1) { XGpio_DiscreteWrite(&gpio_blinky, 1, 0); } txCount = 0; txTryCount = 0; } } txTryCount++; // flip the LED1 so the user knows the processor is alive testBlinkCount += 1; if (testBlinkCount > 100000) { if (aliveLed == 0) aliveLed = 1; else aliveLed = 0; testBlinkCount = 1; XGpio_DiscreteWrite(&gpio_blinky, 2, aliveLed); } break; case 1: // continuously send out packets // do it once and then stall for a bit for (i1=0; i1<5000; i1++) { if (i1 == 0) { XGpio_DiscreteWrite(&gpio_blinky, 1, 1); Chilipepper_WriteTestPacket( 1 ); XGpio_DiscreteWrite(&gpio_blinky, 1, 0); } } // flip the LED1 so the user knows the processor is alive testBlinkCount += 1; if (testBlinkCount > 2000) { if (aliveLed == 0) aliveLed = 1; else aliveLed = 0; testBlinkCount = 1; XGpio_DiscreteWrite(&gpio_blinky, 2, aliveLed); } break; case 2: // initiate packet transmission with a button press // flip the LED1 so the user knows the processor is alive testBlinkCount += 1; if (testBlinkCount > 200000) { if (aliveLed == 0) aliveLed = 1; else aliveLed = 0; testBlinkCount = 1; XGpio_DiscreteWrite(&gpio_blinky, 2, aliveLed); } if (DebouncButton() == 0) break; XGpio_DiscreteWrite(&gpio_blinky, 1, 1); success = Chilipepper_WriteTestPacketWithAck( rxBuf ); if (success == 1) XGpio_DiscreteWrite(&gpio_blinky, 1, 0); else { Chilipepper_Reset(); } break; default: break; } } cleanup_platform(); return 0; }
int main() { int sentCount; int aliveLed = 0, statusLed = 0; int numBytes; int sw, i1; static int BlinkCount; int txCount = 0, txTryCount = 0; unsigned char numUartRead, curValue, id; unsigned char rxBuf[256], txBuf[256]; init_platform(); if(SetupPeripherals() != XST_SUCCESS) return -1; if ( Chilipepper_Initialize() != 0 ) return -1; // by default we are in receive Chilipepper_SetPA( 1 ); Chilipepper_SetTxRxSw( 1 ); // 0- transmit, 1-receive Chilipepper_SetDCOC(1); // enable dc offset correction // enable the Chilipepper LED to indicate we are operational Chilipepper_SetLed( 1 ); Chilipepper_printf(&uartPs, "\r\n\r\nWelcome to Toyon's Chilipepper QPSK demo. This demo was written in MATLAB using Mathworks HDL Coder.\r\n\r\n"); while (1) { Chilipepper_ControlAgc(); // main priority is to parse OTA packets numBytes = Chilipepper_ReadPacket( rxBuf, &id ); // This is a normal receive situation. // We get a packet, write it to UART. if (numBytes > 0) { sentCount = 0; while (sentCount < numBytes) { curValue = rxBuf[sentCount+4]; sentCount += XUartPs_Send(&uartPs, &curValue, 1); } statusLed = ~statusLed; XGpio_DiscreteWrite(&gpio_blinky, 1, statusLed); } // flip the LED1 so the user knows the processor is alive BlinkCount += 1; if (BlinkCount > 200000) { aliveLed = ~aliveLed; BlinkCount = 1; XGpio_DiscreteWrite(&gpio_blinky, 2, aliveLed); } } cleanup_platform(); return 0; }