/* CAMAC.c */ #include #include #include "GPIO.h" #include "CAMAC.h" /* Read Operation: 1) Write A subaddress into Register 3 2) Write F command into Register 4 3) Write N station number into Register 5 4) Initiate a CAMAC cycle by addressing Register 7 5) Check Q & X responses by reading Register 8 6) Read R24-R17 byte from Register 9 7) Read R16-R9 byte from Register 10 8) Read R8-R1 byte from Register 11 */ /* Write Operation: 1) Write W24-W17 byte into Register 0 2) Write W16-W9 byte into Register 1 3) Write W8-W1 byte into Register 2 4) Write A subaddress into Register 3 5) Write F command into Register 4 6) Write N station into Register 5 7) Initiate a CAMAC cycle by addressing Register 7 8) Check Q & X responses by reading Register 8 */ // BDC (Device Select) must be low to access the unit /* https://man7.org/linux/man-pages/man2/nanosleep.2.html */ /*============================================================================*/ bool isInit = false; const gpioPinFunction_t camacReadPins[] = { PIN_INPUT, //data bits PIN_INPUT, PIN_INPUT, PIN_INPUT, PIN_INPUT, PIN_INPUT, PIN_INPUT, PIN_INPUT, PIN_OUTPUT, //read / write select PIN_OUTPUT, //device select PIN_OUTPUT, //address bits PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, //Timing Strobe PIN_INPUT, //Q status last camac cycle PIN_INPUT, //LAM PIN_OUTPUT //start camac cycle }; const gpioPinFunction_t camacWritePins[] = { PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, //read / write select PIN_OUTPUT, //device select PIN_OUTPUT, //address bits PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, PIN_OUTPUT, //Timing Strobe PIN_INPUT, //Q status last camac cycle PIN_INPUT, //LAM PIN_OUTPUT //start camac cycle }; //sleep for 100 nanoseconds const struct timespec sleepStruct = {0,100}; const char* warningStrings[] = { "WARNING: Attempted to use a camac function without calling camacInit\n\0", "WARNING: Attempted to read a write only register.\n\0", "WARNING: Attempted to write to a read only register.\n\0", "WARNING: nanosleep failure, some external signal interrupted sleep.\n\0" }; /*============================================================================*/ void printCableData(uint32_t data){ printf("%i: Start CAMAC Without DB1-8\n", (data >> 17) % 2); printf("%i: LAM\n", (data >> 16) % 2); printf("%i: Q status\n", (data >> 15) % 2); printf("%i: Strobe (writing) / start CAMAC cycle (reading)\n", (data >> 14) % 2); printf("%i: Address register 8\n", (data >> 13) % 2); printf("%i: Address register 4\n", (data >> 12) % 2); printf("%i: Address register 2\n", (data >> 11) % 2); printf("%i: Address register 1\n", (data >> 10) % 2); printf("%i: Device Select\n", (data >> 9) % 2); printf("%i: Read / Write \n", (data >> 8) % 2); printf("%i: Data Bit 8\n", (data >> 7) % 2); printf("%i: Data Bit 7\n", (data >> 6) % 2); printf("%i: Data Bit 6\n", (data >> 5) % 2); printf("%i: Data Bit 5\n", (data >> 4) % 2); printf("%i: Data Bit 4\n", (data >> 3) % 2); printf("%i: Data Bit 3\n", (data >> 2) % 2); printf("%i: Data Bit 2\n", (data >> 1) % 2); printf("%i: Data Bit 1\n", (data >> 0) % 2); } /*============================================================================*/ void printData(uint32_t data){ printf("Writing 0x%05x\n", data); printCableData(data); } void writeDebug(uint32_t data){ printData(data); gpioSetRawLogicLevels(data); // write data } /*============================================================================*/ bool readCamacRegister(uint8_t* result, CAMACFunction_t command){ size_t i; int nanoSleepResult; uint32_t fullCommand; if(!isInit){ fprintf(stderr,warningStrings[0]); return false; } for( i = 0; i < 18; i++) gpioSetPinType(i,camacReadPins[i]); fullCommand = command << 10; // fullCommand |= 0x00020000; //Keep CAMAC and strobe pin pin high writeDebug(fullCommand); //returns 0 on success nanoSleepResult = nanosleep(&sleepStruct, NULL); if(nanoSleepResult){ fprintf(stderr,warningStrings[3]); return false; } writeDebug(fullCommand | 0x00000100); fullCommand = gpioGetRawLogicLevels(); printData(fullCommand); if(result) *result = (uint8_t)fullCommand; return true; } /*============================================================================*/ void writeCamacRegister(uint8_t data, CAMACFunction_t command){ size_t i; uint32_t fullCommand; if(!isInit){ fprintf(stderr,warningStrings[0]); return; } for( i = 0; i < 18; i++) gpioSetPinType(i,camacWritePins[i]); fullCommand = command << 10; fullCommand |= data; fullCommand |= 0x00020000; gpioSetRawLogicLevels(fullCommand); nanosleep(&sleepStruct, NULL); gpioSetRawLogicLevels(fullCommand | 0x00004000); } /*============================================================================*/ bool camacInit(void){ size_t i; bool result; result = false; //mmap the GPIO registers to memory if(!gpioInit()) puts("gpioInit() returned false"); else { for (i = 0; i < 18; i++) gpioSetPinType(i,PIN_INPUT); result = true; isInit = true; } return result; } /*============================================================================*/