/****************************************************************************** * Module: Llrf pad Temperature Monitoring driver * Filename: drvPadQspi.c * Author: Shantha Condamoor * Created: 8-Aug-2014 * Description: * LLRF PAD Temperature monitoring related LLRF processing via coldFire QSPI * LLRF PAD's use The slow-speed 24-bit ADC in the ADS1218 chipset * interfaces to coldFire module's slow, queued serial port interface perifpheral. * * The driver has been implemented as a seprate, low priority thread with lazy waits (delays). * per T.Straumann's recommendation. ****************************************************************************** * Modification history * Mod: Shantha Condamoor 08-Aug-2014 * Initial Release. * Uses following T.Straumann's QSPI utilities in bsp/coldfUtils.h: * coldfQspiSetup() * coldfQspiWrite() * coldfQspiRead() * It provides readbacks from the 24-bit ADC of the values read by the * temperature sensors inside the LLRF PAD chassis from 8 channels. * This driver provides the following function for EPICS device support: * llrfGetPadTemperatureValue(int channel) * *****************************************************************************/ #include #include #include #include #include #include #include #include #include #include /* for callocMustSucceed() */ #include #include #include /* for IOSCANPVT */ #include #include "drvPadQspi.h" #define BAUDRATE 500000 /* 64MHz system clk/0x40*2 => 500 KHz */ /* Chip-select mask */ /* pull CS line 0 low */ #define ADS_CS 0x0 int debug=0; /* RTEMS priority of QSPI thread */ /* The qspiPadTask is a low-priority thread that reads Temperature via the QSPI serial interface data from the ADS chip. * It has the same priority as a scan1 thread since the temperature records are * scanned once every 1 second. */ /* #define QSPI_THREAD_PRI 136 */ /* scondam: 23-Sept-2014; Try lowering RTEMS thready priority - lower than save_restore thread which is 179 */ #define QSPI_THREAD_PRI 180 epicsThreadId drvPadQspiThread = 0; static void qspiPadTask(void *arg); IOSCANPVT padQspiIoscan[PAD_QSPI_CHANNELS_ON_BOARD]; static QspiPad qspi; void llrf_padQSPISetMUXReadADC(void *arg); /* forward declaration */ /*============================================================================= Name: padQSPIReport Ret: =============================================================================*/ long padQspiReport(int level) { epicsPrintf("PAD QSPI Support for Communication with ADS1218\n"); epicsPrintf ("PQSPAR: 0x%08x should be 0x7F. If not, did you run padInitialise?\n", (unsigned int) MCF5282_GPIO_PQSPAR); epicsPrintf("QMR: 0x%08x\n", (unsigned int) MCF5282_QSPI_QMR); epicsPrintf("QDLYR: 0x%08x\n", (unsigned int) MCF5282_QSPI_QDLYR); epicsPrintf("QIR: 0x%08x\n", (unsigned int) MCF5282_QSPI_QIR); epicsPrintf("QAR: 0x%08x\n", (unsigned int) MCF5282_QSPI_QAR); epicsPrintf("QDR: 0x%08x\n", (unsigned int) MCF5282_QSPI_QDR); epicsPrintf("QWR: 0x%08x\n", (unsigned int) MCF5282_QSPI_QWR); return 0; } static void set_pri(const char *nm, rtems_id tid, rtems_task_priority p) { rtems_status_code sc; rtems_task_priority old; sc = rtems_task_set_priority(tid, p, &old); if ( RTEMS_SUCCESSFUL != sc ) { rtems_error(sc,"WARNING: Unable to adjust task priority for '%s' -- may affect RT performance!\n", nm); } } static long padQspiInit(void) { long rval; QspiPad qspi_p; int i; /* Initialize + setup QSPI driver */ if ( ( rval = coldfQspiSetup(0, BAUDRATE, 0, 0x4000, /* DRV5282_QSPI_SETUP_CS_ACTVLOW */ DRV5282_QSPI_SETUP_CLK_FALLING ) ) ) { return rval; } qspi_p = qspi = callocMustSucceed( 1, sizeof(*qspi_p), "llrfStationInitialize(): no memory"); qspi_p->mutex = epicsMutexMustCreate(); epicsMutexMustLock( qspi_p->mutex ); for (i=0; i < PAD_QSPI_CHANNELS_ON_BOARD; i++){ qspi_p->temps[i] = 0.0; scanIoInit(&padQspiIoscan[i]); } epicsMutexUnlock( qspi_p->mutex ); /* Start task that does the temperature scan work */ drvPadQspiThread = epicsThreadMustCreate( "Qspi", /* scondam: 23-Sept-2014; Try setting EPICS thread priority - same as save_restore thread which is 20 */ /* epicsThreadPriorityLow, */ 20, epicsThreadGetStackSize(epicsThreadStackMedium), (EPICSTHREADFUNC)llrf_padQSPISetMUXReadADC, 0); /* RTEMS priorities are not a accessible to epics OSI :-( */ set_pri("qspiPadTask", (rtems_id)drvPadQspiThread, QSPI_THREAD_PRI); return 0; } void llrf_padQSPISetMUXReadADC(void *arg) { long jj = 0; /* keeps track of how many loops to complete QSPI transfer (info) */ uint8_t buf[4] = {0}; int channel; QspiPad qspi_p; if ( ! (qspi_p = qspi) ) { epicsPrintf("qspiPadTask: QSPI not configured/initialized\n"); return; } while (1) { for (channel=0; channel < 8; channel++) { buf[0] = 0x51; buf[1] = 0x0; buf[2] = channel*16+8; coldfQspiWrite(buf, 3, ADS_CS); epicsThreadSleep(0.2); /* coldfQspiStatus(); */ buf[0]=0x1; buf[1]=0x0; buf[2] = 0x0; buf[3] = 0x0; coldfQspiWrite(buf, 4, ADS_CS); /* wait while transfer still in progress */ epicsThreadSleep(0.2); buf[0] = 0; buf[1] = 0; buf[2] = 0; if ( coldfQspiRead(buf,1,3) >= 0) { epicsMutexMustLock( qspi_p->mutex ); qspi_p->temps[channel] = ((buf[0] * 65536) + (buf[1] * 256) + (buf[2])); epicsMutexUnlock( qspi_p->mutex ); } scanIoRequest(padQspiIoscan[channel]); } epicsThreadSleep(1.0); /* temp records scanned at 1 second rate - so need to update data faster than that */ } } /*============================================================================= Name: llrfGetPadTemperatureValue Abs: =============================================================================*/ long llrfGetPadTemperatureValue(int channel) { long rval; QspiPad qspi_p; if ( channel < 0 || channel > PAD_QSPI_CHANNELS_ON_BOARD ) { epicsPrintf("llrfGetPadTemperatureValue: unrecognized station: %d\n", channel); return 0./0. /* NaN */; } if ( ! (qspi_p = qspi) ) { epicsPrintf("llrfGetPadTemperatureValue: QSPI not configured/initialized\n"); return 0./0. /* NaN */; } epicsMutexMustLock( qspi_p->mutex ); rval = qspi_p->temps[channel]; epicsMutexUnlock( qspi_p->mutex ); return rval; } static struct { long number; DEVSUPFUN report; DEVSUPFUN init; } drvPadQspi = { 2, padQspiReport, padQspiInit }; epicsExportAddress(drvet,drvPadQspi);