/** * Unreserve the interrupt from FPGA and disable timer IRQ IO. * * @param[in] irqChannel A structure containing the registers and settings * for timer IRQ IO to modify. * @param[in] irqContext IRQ context under which you unreserve the IRQ. * @return the configuration status. */ int32_t Irq_UnregisterTimerIrq(MyRio_IrqTimer* irqChannel, NiFpga_IrqContext irqContext) { int32_t status; /* * Check if the specified IRQ resource is registered. */ status = Irq_CheckReserved(irqChannel->timerChannel, TIMERIRQNO); if (status == NiMyrio_Status_Success) { /* * Did not find the resource in the list. */ printf("You didn't register an interrupt with this IRQ number.\n"); return NiMyrio_Status_Success; } /* * Write the value to the IRQTIMERSETTIME register. */ status = NiFpga_WriteU32(myrio_session, irqChannel->timerWrite, 0); status = NiFpga_WriteBool(myrio_session, irqChannel->timerSet, NiFpga_True); /* * Check if there was an error writing to the IRQTIMERSETTIME register. * * If there was an error then print an error message to stdout and return configuration status. */ MyRio_ReturnStatusIfNotSuccess(status, "Could not write to IRQTIMERSETTIME register!") /* * Delete the reserved resource in the list. */ status = Irq_RemoveReserved(TIMERIRQNO); /* * Check if there was an error releasing the resource from list. * * If there was an error then print an error message to stdout. */ MyRio_ReturnStatusIfNotSuccess(status, "Could not release the irq resource!") /* * Unreserve an IRQ context obtained from Irq_ReserveIrqContext. * The returned NiFpga_Status value is stored for error checking. */ status = NiFpga_UnreserveIrqContext(myrio_session, irqContext); /* * Check if there was an error when unreserve an IRQ. * * If there was an error then print an error message to stdout and return configuration status. */ MyRio_ReturnStatusIfNotSuccess(status, "A required NiFpga_IrqContext was not unreserved.") return NiMyrio_Status_Success; }
void FPGA_SetMotorStatus(int value) { if(value) { NiFpga_MergeStatus(&FPGA_Status,NiFpga_WriteBool(FPGA_Session,NiFpga_mainFPGA_ControlBool_enableMotors,NiFpga_True)); if (NiFpga_IsError(FPGA_Status)) { LOG.ERR("Set Motor Status Failed."); } } else { NiFpga_MergeStatus(&FPGA_Status,NiFpga_WriteBool(FPGA_Session,NiFpga_mainFPGA_ControlBool_enableMotors,NiFpga_False)); if (NiFpga_IsError(FPGA_Status)) { LOG.ERR("Set Motor Status Failed."); } } }
int main() { double desired_angle; int32_t desired_tick; int32_t Kd = .5 * ACCURACY; int32_t Kp = 5 * ACCURACY; int32_t Ki = .0001 * ACCURACY; int32_t error=0xFFFF; /* must be called before any other calls */ printf("Initializing...\n"); NiFpga_Status status = NiFpga_Initialize(); if (NiFpga_IsNotError(status)) { NiFpga_Session session; /* opens a session, downloads the bitstream, and runs the FPGA */ printf("Opening a session...\n"); NiFpga_MergeStatus(&status, NiFpga_Open(NiFpga_Math_Pid_Bitfile, NiFpga_Math_Pid_Signature, "rio://146.6.84.251/RIO0", NiFpga_OpenAttribute_NoRun,&session)); if (NiFpga_IsNotError(status)) { /* run the FPGA application */ printf("Running the FPGA...\n"); NiFpga_MergeStatus(&status, NiFpga_Run(session, 0)); /********************************************************************/ /* Code goes here */ NiFpga_MergeStatus(&status, NiFpga_WriteBool(session,NiFpga_Encoder_Reset,0)); NiFpga_MergeStatus(&status, NiFpga_WriteI32(session,NiFpga_Kd,Kd)); NiFpga_MergeStatus(&status, NiFpga_WriteI32(session,NiFpga_Kp,Kp)); NiFpga_MergeStatus(&status, NiFpga_WriteI32(session,NiFpga_Ki,Ki)); NiFpga_MergeStatus(&status, NiFpga_WriteI32(session,NiFpga_Accuracy,ACCURACY)); printf("Enter Desired(in degrees): "); scanf("%lf",&desired_angle); desired_tick = degree_tick(desired_angle); printf("Desired(in ticks) %d\n", desired_tick); NiFpga_MergeStatus(&status, NiFpga_WriteI32(session,NiFpga_Desired,desired_tick)); printf("Entering loop\n"); while(error != 0) { NiFpga_MergeStatus(&status, NiFpga_ReadI32(session,NiFpga_RealError,&error)); printf("Error:%d\n", error); } /********************************************************************/ /* Stopping */ printf("Press <Enter> to stop and quit..."); getchar(); /* stop the FPGA loops */ printf("Stopping the FPGA...\n"); /* close the session now that we're done */ printf("Closing the session...\n"); NiFpga_MergeStatus(&status, NiFpga_Close(session, 0)); } /* must be called after all other calls */ printf("Finalizing...\n"); NiFpga_MergeStatus(&status, NiFpga_Finalize()); } /* check if anything went wrong */ if (NiFpga_IsError(status)) { printf("Error %d!\n", status); printf("Press <Enter> to quit...\n"); getchar(); } return status; }
/** * Reserve the interrupt from FPGA and configure Timer IRQ. * * @param[in] irqChannel A structure containing the registers and settings for timer IRQ IO to modify. * @param[in] irqContext IRQ context under which you reserve IRQ. * @param[in] timeout The time, in microseconds, after which the IRQ occurs. * @return the configuration status. */ int32_t Irq_RegisterTimerIrq(MyRio_IrqTimer* irqChannel, NiFpga_IrqContext* irqContext, uint32_t timeout) { int32_t status; /* * Reserve an IRQ context. IRQ contexts are single-threaded; only one thread * can wait with a particular context at any given time. To minimize jitter * when first waiting on IRQs, reserve as many contexts as the application requires. * If a context is successfully reserved, you must unreserve it later. * Otherwise a memory leak will occur. */ status = NiFpga_ReserveIrqContext(myrio_session, irqContext); /* * Check if there was an error when you reserved the IRQ. * If there was an error, print an error message to stdout and return the configuration status. */ MyRio_ReturnStatusIfNotSuccess(status, "A required NiFpga_IrqContext was not reserved.") /* * Check if the IRQ number or channel value already exists in the resource list, * return the configuration status and print an error message. */ status = Irq_CheckReserved(irqChannel->timerChannel, TIMERIRQNO); if (status == NiMyrio_Status_IrqNumberNotUsable || status == NiMyrio_Status_IrqChannelNotUsable) { printf("You have already registered the only timer interrupt.\n"); return status; } /* * Write the value to the TIMERWRITE register. * * The returned NiFpga_Status value is stored for error checking. */ status = NiFpga_WriteU32(myrio_session, irqChannel->timerWrite, timeout); /* * Check if there was an error when you reserved the IRQ. * * If there was an error, print an error message to stdout and return the configuration status.s. */ MyRio_ReturnStatusIfNotSuccess(status, "Could not write to TIMERWRITE register!") /* * Write the value to the TIMERSETTIME register. */ status = NiFpga_WriteBool(myrio_session, irqChannel->timerSet, NiFpga_True); /* * Check if there was an error when you reserved the IRQ. * * If there was an error, print an error message to stdout and return the configuration status. */ MyRio_ReturnStatusIfNotSuccess(status, "Could not write to TIMERSETTIME register!") /* * Add the channel value and IRQ number to the list. */ Irq_AddReserved(irqChannel->timerChannel, TIMERIRQNO); return NiMyrio_Status_Success; }
bool nifpga::WriteBool(uint32_t control, NiFpga_Bool value) { if (sessionOpen) return HandleStatus(NiFpga_WriteBool(sessionHandle, control, value)); return false; }