/* * analogtst.c: project main test application * * This application configures UART 16550 to baud rate 9600. * PS7 UART (Zynq) is not initialized by this application, since * bootrom/bsp configures it to baud rate 115200 */ #include #include "platform.h" #include "xil_printf.h" #include "xparameters.h" // memory mapped zedboard peripherals #define ZBOBPreg(k) (*(volatile u32 *)(XPAR_ZBPBSWLED_0_S_AXI_BASEADDR+4*k)) // memory mapped analog converter #define ADDAreg(k) (*(volatile u32 *)(XPAR_PMODADDA_0_S_AXI_BASEADDR+4*k)) #define CMDBUF_SIZE 64 static char Cmd_Buf[CMDBUF_SIZE]; // ring buffer stuff #define RINGB_SIZE 1024 #define RINGB_COLS 4 static int Rb_inP, Rb_outP; static u16 RingbData[RINGB_SIZE][RINGB_COLS]; static void RbInit() { Rb_inP = 0; Rb_outP = 0; } static void rbPush(u16 x0, u16 x1, u16 x2, u16 x3) { RingbData[Rb_inP][0] = x0; RingbData[Rb_inP][1] = x1; RingbData[Rb_inP][2] = x2; RingbData[Rb_inP][3] = x3; Rb_inP = (Rb_inP + 1) % RINGB_SIZE; if (Rb_inP == Rb_outP) { Rb_outP = (Rb_outP + 1) % RINGB_SIZE; } } static void rbPop() { unsigned int x0, x1, x2, x3; if (Rb_inP == Rb_outP) { print("###\n"); } else { x0 = RingbData[Rb_outP][0]; x1 = RingbData[Rb_outP][1]; x2 = RingbData[Rb_outP][2]; x3 = RingbData[Rb_outP][3]; Rb_outP = (Rb_outP + 1) % RINGB_SIZE; xil_printf("~ %d %d %d %d\n", x0, x1, x2, x3); } } static int rbValues() { int n; n = Rb_inP - Rb_outP; if (n < 0) { n += RINGB_SIZE; } return n; } // keep last value of DAC static u32 DacVal; // write dac values (12bits) static int DacWrite(int newval, u16 da1, u16 da0) { int retc; u32 x; if (newval) { DacVal = da0; DacVal |= da1 << 16; } ADDAreg(0) = DacVal; retc = 0; do { x = ADDAreg(1); retc++; } while (!(x & 1) && (retc < 1000)); return retc; } // get adc values (12bits) static void AdcConv(u16 *ad1, u16 *ad0) { u32 x; DacWrite(0, 0, 0); x = ADDAreg(0); *ad1 = (x >> 16) & 0x0fff; *ad0 = x & 0x0fff; } // program entry point int main() { int cmd_loop; char cmd_c; u32 x, da1, da0; u16 ad1, ad0; int retc; init_platform(); print("--- Project Template Test v0.0a ---\n"); RbInit(); cmd_loop = 1; do { // print("prj>> "); fflush(stdout); fgets(Cmd_Buf, CMDBUF_SIZE, stdin); fflush(stdin); cmd_c = Cmd_Buf[0]; if (cmd_c > ' ') { switch (cmd_c) { case 'h': print("Help!\n"); break; case 'l': print("set LEDs:\n"); if ((sscanf(&Cmd_Buf[1], "%x", (unsigned int *)&x)) != 1) { print(" *** unable to convert string to hex value.\n"); } else { xil_printf(" o sending LED=%x\n", x); ZBOBPreg(0) = x; } break; case 'd': print("set DA:\n"); if ((sscanf(&Cmd_Buf[1], "%x %x", (unsigned int *)&da1, (unsigned int *)&da0)) != 2) { print(" *** unable to convert string to hex values.\n"); } else { xil_printf(" o sending DA1=%x / DA0=%x\n", da1, da0); if (da1 > 0x0fff) { print(" *** da1 out of range.\n"); } else if (da0 > 0x0fff) { print(" *** da0 out of range.\n"); } else { retc = DacWrite(1, (u16)da1, (u16)da0); xil_printf(" DacWrite retcode = %d\n", retc); AdcConv(&ad1, &ad0); xil_printf("AD1 = %x AD0 = %x\n", ad1, ad0); } } break; case 'r': print("registers:\n"); x = ZBOBPreg(0); xil_printf("PushB / Switch = %x\n", x); x = ADDAreg(1); xil_printf("ADDA status = %x\n", x); x = ADDAreg(0); xil_printf("AD values = %x\n", x); break; case 'b': xil_printf("=> ring buffer values: %d\n", rbValues()); break; case 'c': if ((sscanf(&Cmd_Buf[1], "%d %d", (unsigned int *)&da1, (unsigned int *)&da0)) != 2) { print(" *** unable to convert string to hex values.\n"); } else { if (da1 > 0x0fff) { print(" *** da1 out of range.\n"); } else if (da0 > 0x0fff) { print(" *** da0 out of range.\n"); } else { retc = DacWrite(1, (u16)da1, (u16)da0); xil_printf(" DacWrite retcode = %d\n", retc); AdcConv(&ad1, &ad0); xil_printf("pushing %d %d %d %d\n", da0, da1, ad0, ad1); rbPush((u16)da0, (u16)da1, ad0, ad1); } } break; case 'p': rbPop(); break; case 'x': cmd_loop = 0; break; default: xil_printf(" *** unknown command [%s]\n", Cmd_Buf); } } } while (cmd_loop); print("Thank you for using Project Template Test.\n"); cleanup_platform(); return 0; }