Exercises/13_4/IntroToXPS: prng_example.c

File prng_example.c, 6.3 KB (added by chunter, 11 years ago)
Line 
1//Xilinx SDK includes
2#include "xparameters.h"
3#include "stdio.h"
4#include "xsysmon.h"
5#include "xtmrctr.h"
6#include "xio.h"
7
8//WARP includes
9#include "w3_userio.h"
10#include "w3_ad_controller.h"
11#include "w3_clock_controller.h"
12#include "w3_iic_eeprom.h"
13#include "radio_controller.h"
14
15//Define standard macros for pcore base addresses and device IDs
16// XPAR_ names will change with instance names in hardware
17#define USERIO_BASEADDR     XPAR_W3_USERIO_0_BASEADDR
18#define RC_BASEADDR         XPAR_RADIO_CONTROLLER_0_BASEADDR
19#define AD_BASEADDR         XPAR_W3_AD_CONTROLLER_0_BASEADDR
20#define CLK_BASEADDR        XPAR_W3_CLOCK_CONTROLLER_0_BASEADDR
21#define EEPROM_BASEADDR     XPAR_W3_IIC_EEPROM_ONBOARD_BASEADDR
22#define DRAM_BASEADDR       XPAR_DDR3_2GB_SODIMM_MPMC_BASEADDR
23#define TIMER_FREQ          XPAR_XPS_TIMER_0_CLOCK_FREQ_HZ
24#define TMRCTR_DEVICE_ID    XPAR_TMRCTR_0_DEVICE_ID
25#define CAPTURE_PERIOD      XPAR_PRNG_USERIOSRC_PLBW_0_MEMMAP_CAPTUREPERIOD
26#define CAPTURED_OUTPUT     XPAR_PRNG_USERIOSRC_PLBW_0_MEMMAP_CAPTUREDOUTPUT
27
28
29//Global variable definitions
30XTmrCtr TimerCounter; /* The instance of the Tmrctr Device */
31
32int w3_node_init();
33void userio_example();
34void usleep(u32 duration);
35
36int main() {
37    int status;
38
39    xil_printf("                \f");
40    xil_printf("WARP v3 Template Project - Lite\n");
41
42    status = w3_node_init();
43    if(status != 0) {
44        xil_printf("Error in w3_node_init()! Exiting\n");
45        return -1;
46    }
47
48    xil_printf("Board serial number: W3-a-%05d\n\n", w3_eeprom_readSerialNum(EEPROM_BASEADDR));
49
50    xil_printf("Running User I/O Example\n");
51    userio_example();
52
53    return 0;
54}
55
56void userio_example() {
57    u16 capturedValue;
58    u8 i,allowPrint;
59
60    /*
61     * We use allowPrint as a simple software "latch" to avoid unnecessary prints to the UART.
62     * When the user presses and holds the "up" pushbutton on the board, this code will print
63     * the binary string of the output of the custom core's LFSR. When the user releases the
64     * pushbutton, this code will print a series of backspaces to delete the previous print.
65     */
66    allowPrint = 1;
67
68    /*
69     * The User I/O core can be controlled from software registers or from its hardware port
70     * inputs. In this exercise, we are driving the core from its ports.
71     * The userio_set_ctrlSrc_hw() macro allows configures the core to ignore its register
72     * inputs and instead use its hardware ports.
73     */
74    userio_set_ctrlSrc_hw(USERIO_BASEADDR, (W3_USERIO_CTRLSRC_LEDS|W3_USERIO_CTRLSRC_HEXDISPS));
75
76    /*
77     * The CAPTURE_PERIOD definition simply abstracts away the address of the "capturePeriod"
78     * register that was created in the underlying custom pcore. XIo_Out32 is a simple macro
79     * that Xilinx provides that lets us write values to that memory address. In this function,
80     * we write a value that will ensure that the USER I/O LEDs are updated at 10 times per second.
81     * Reduce this value to increase the speed of the updates and increase it to slow it down further.
82     */
83    XIo_Out32(CAPTURE_PERIOD,8000000-1);
84
85    xil_printf("\t            Left Hex  -------------     -------------  Right Hex\n");
86    xil_printf("\t            Red LEDs  -------                 -------  Green LEDs\n");
87    xil_printf("\tPRNG Captured Value:                                  ");
88
89    /*
90     * This while loop will run forever. There is no exit condition to break out of this loop.
91     * This loop will continually read from the User I/O core to determine if any pushbuttons
92     * are currently being pressed. If so, it triggers a small loop to print out the output
93     * of the custom pcore's capturedOutput register. This inner loop is simply to display
94     * the output as a binary string.
95     */
96    while(1){
97        if((userio_read_inputs(USERIO_BASEADDR) & W3_USERIO_PB_U)){
98            if(allowPrint){
99                capturedValue = XIo_In32(CAPTURED_OUTPUT);
100                xil_printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
101                for (i=0;i<16;i++){
102                    xil_printf("%d ", (capturedValue>>(15-i))&1);
103                }
104                allowPrint = 0;
105            }
106        } else if(!allowPrint) {
107            xil_printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
108            xil_printf("                                ");
109            allowPrint = 1;
110        }
111    }
112    xil_printf("\n");
113    return;
114
115}
116
117int w3_node_init() {
118
119    int status;
120    XTmrCtr *TmrCtrInstancePtr = &TimerCounter;
121    int ret = XST_SUCCESS;
122
123    microblaze_enable_exceptions();
124
125    //Initialize the AD9512 clock buffers (RF reference and sampling clocks)
126    status = clk_init(CLK_BASEADDR, 2);
127    if(status != XST_SUCCESS) {
128        xil_printf("w3_node_init: Error in clk_init (%d)\n", status);
129        ret = XST_FAILURE;
130    }
131
132    //Initialize the AD9963 ADCs/DACs
133    ad_init(AD_BASEADDR, (RFA_AD_CS | RFB_AD_CS), 2);
134    if(status != XST_SUCCESS) {
135        xil_printf("w3_node_init: Error in ad_init (%d)\n", status);
136        ret = XST_FAILURE;
137    }
138
139    //Initialize the radio_controller core and MAX2829 transceivers
140    status = radio_controller_init(RC_BASEADDR, (RC_RFA | RC_RFB), 1, 1);
141    if(status != XST_SUCCESS) {
142        xil_printf("w3_node_init: Error in radioController_initialize (%d)\n", status);
143        ret = XST_FAILURE;
144    }
145
146    //Initialize the EEPROM read/write core
147    iic_eeprom_init(EEPROM_BASEADDR, 0x64);
148    if(status != XST_SUCCESS) {
149        xil_printf("w3_node_init: Error in IIC_EEPROM_init (%d)\n", status);
150        ret = XST_FAILURE;
151    }
152
153    /*
154     * Initialize the timer counter so that it's ready to use,
155     * specify the device ID that is generated in xparameters.h
156     */
157    status = XTmrCtr_Initialize(TmrCtrInstancePtr, TMRCTR_DEVICE_ID);
158    if (status != XST_SUCCESS) {
159        xil_printf("w3_node_init: Error in XtmrCtr_Initialize (%d)\n", status);
160        ret = XST_FAILURE;
161    }
162
163    /*
164     * Perform a self-test to ensure that the hardware was built
165     * correctly, use the 1st timer in the device (0)
166     */
167    status = XTmrCtr_SelfTest(TmrCtrInstancePtr, 0);
168    if (status != XST_SUCCESS) {
169        xil_printf("w3_node_init: Error in XTmrCtr_SelfTest (%d)\n", status);
170        ret = XST_FAILURE;
171    }
172
173    // Set timer 0 to into a "count down" mode
174    XTmrCtr_SetOptions(TmrCtrInstancePtr, 0, (XTC_DOWN_COUNT_OPTION));
175
176    return ret;
177}
178
179void usleep(u32 duration){
180    XTmrCtr *TmrCtrInstancePtr = &TimerCounter;
181    XTmrCtr_SetResetValue(TmrCtrInstancePtr,0,duration*(TIMER_FREQ/1000000));
182
183    XTmrCtr_Start(TmrCtrInstancePtr,0);
184
185    volatile u8 isExpired = 0;
186    while(isExpired!=1){
187        isExpired = XTmrCtr_IsExpired(TmrCtrInstancePtr,0);
188    }
189    XTmrCtr_Reset(TmrCtrInstancePtr,0);
190    return;
191}
192