source: PlatformSupport/User_IO/util/warp_userioboard_util.c

Last change on this file was 1204, checked in by murphpo, 15 years ago

ongoing refdes13 work

  • Property svn:executable set to *
File size: 8.7 KB
Line 
1/*! \file warp_userioboard_util.c
2 \brief Provides high-level functions for using the various interfaces on the WARP User I/O Board. This file and its header file should be included in your EDK software project to help drive the User I/O board. The low-level register access functions used here are provided by the User I/O Board controller's driver.
3*/
4
5#include "warp_userioboard_util.h"
6#include "ascii_characters.h"
7#include "xparameters.h"
8
9//Only include SystemACE CF and xil_fatfs headers if the xps_sysace hardware and xil_fatfs library is included in the project
10#ifdef XPAR_SYSACE_0_DEVICE_ID
11#ifdef XILFATFS_MAXFILES
12#include "xsysace.h"
13#include "sysace_stdio.h"
14#endif
15#endif
16
17///@brief Initializes the LCD screen
18///
19///@param slotNumber Integer in [1,2,3,4] indicating which daughtercard slot holds the User I/O Board
20///@param invertColors Selects standard or inverse color map; use 0 by default
21void warp_userioboard_lcd_init(unsigned char slotNumber, unsigned char invertColors)
22{
23    //Reset the LCD via its dedicated digital reset pin, mapped through the controller
24    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESETLCD, 1);
25    usleep(10);
26    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESETLCD, 0);
27    usleep(10);
28    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESETLCD, 1);
29
30    //Write the default values to every register, as specified in the From Register masks in Sysgen
31    // (these defaults aren't honored in hardware, so we write them here)
32    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_BACKGROUNDCOLOR, 0);
33    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_CHARACTERSSELECT, 0); 
34    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_COLSET, 0x175);
35    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_CONFIGLOCATION, 0);
36    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_DIVIDERSELECT, 0);
37    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_FIRSTEND, 131);
38    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_FIRSTSTART, 0);
39    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RAMWRITE, 0x15C);
40    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESET, 0);
41    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESETLCD, 1);
42    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_ROWSET, 0x115);
43    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_SECONDEND, 131);
44    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_SECONDSTART, 0);
45    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_SEND, 0);
46    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_TOTALCMDTRANSFER, 10);
47
48    //Reset and initialize the LCD and SPI controller state machines
49    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESET, 1);
50    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_TOTALCMDTRANSFER, 50); //Number of commands in RAM by default
51    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_SEND, 0);
52    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_RESET, 0);
53    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_ROWSET, LCDCMD_CASET);
54    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_COLSET, LCDCMD_PASET);
55
56
57/* The LCD controller command buffer contains the correct initialization commands for a board in slot 1 with non-inverted colors
58 * These commands will be sent when this function asserts LCD_SEND below
59 * If you want to customize the initialization sequence, use a different slot or invert the colors, you'll need the code below
60 * Change INCLUDE_LCD_INIT_CODE to 1 in the header file, then customize the initial values for the array below
61 * If you change the total number of commands (50 by default), be sure to update the value written to LCD_TOTALCMDTRANSFER above
62*/
63#if INCLUDE_LCD_INIT_CODE
64    //Define the command buffer
65    unsigned short cmds[50];
66
67    //Populate the command buffer with the required at-boot setup command sequence
68    cmds[0] = LCDCMD_DISCTL;
69    cmds[1] = 0x03;
70    cmds[2] = 32;
71    cmds[3] = 12;
72    cmds[4] = 0x00;
73    cmds[5] = LCDCMD_COMSCN;
74    cmds[6] = 0x01;
75    cmds[7] = LCDCMD_OSCON;
76    cmds[8] = LCDCMD_SLPOUT;
77    cmds[9] = LCDCMD_VOLCTR;
78    cmds[10] = 25;
79    cmds[11] = 0x03;
80    cmds[12] = LCDCMD_PWRCTR;
81    cmds[13] = 0x0f;
82    cmds[14] = invertColors ? LCDCMD_NOP : LCDCMD_DISINV;
83    cmds[15] = LCDCMD_DATCTL;
84    cmds[16] = (slotNumber <= 2) ? 0x05 : 0x06; //Slots 3/4: rotate the screen orientation 180 degrees
85    cmds[17] = 0;
86    cmds[18] = 0x01;
87    cmds[19] = 0x00;
88    cmds[20] = LCDCMD_RGBSET8;
89    cmds[21] = 0;
90    cmds[22] = 2;
91    cmds[23] = 4;
92    cmds[24] = 6;
93    cmds[25] = 8;
94    cmds[26] = 10;
95    cmds[27] = 12;
96    cmds[28] = 15;
97    cmds[29] = 0;
98    cmds[30] = 2;
99    cmds[31] = 4;
100    cmds[32] = 6;
101    cmds[33] = 8;
102    cmds[34] = 10;
103    cmds[35] = 12;
104    cmds[36] = 15;
105    cmds[37] = 0;
106    cmds[38] = 4;
107    cmds[39] = 9;
108    cmds[40] = 15;
109    cmds[41] = LCDCMD_NOP;
110    cmds[42] = LCDCMD_PASET;
111    cmds[43] = 2;
112    cmds[44] = 130;
113    cmds[45] = LCDCMD_CASET;
114    cmds[46] = 2;
115    cmds[47] = 130;
116    cmds[48] = LCDCMD_DISON;
117    cmds[49] = LCDCMD_RAMWR;
118
119    //Copy the command buffer to the board controller
120    memcpy((void *)LCD_COMMAND_BUFFER, (void *)(&cmds), 50*sizeof(short));
121#endif
122
123    //Trigger the SPI state machine; this will write LCD_TOTALCMDTRANSFER commands to the LCD controller IC
124    XIo_Out32(USER_IO_BOARD_CONTROLLER_LCD_SEND, 1);
125
126    //Done! The LCD should now be displaying the character map (the default contents of character buffer 0)
127    //User code can now overwrite the character and character map buffers
128    return;
129}
130
131///@brief Prints characters to the LCD display
132///@param chars Pointer to array of characters to print; the first entry will be the left-most character displayed
133///@param numChars Number of characters from chars to print; must be one of [4,8,12,16]; numChars + col must not exceed 16
134///@param line Line number on the LCD to print the characters; must be integer in [0...15]
135///@param col Column to start displayng the characters; must be one of [0,4,8,12]; numChars + col must not exceed 16
136///@param charBuf Selects which character buffer in the controller to write; must be integer in [0,7]
137int warp_userio_lcd_print(unsigned char* chars, unsigned char numChars, unsigned char line, unsigned char col, unsigned char charBuf)
138{
139    int i;
140    //If any of the parameters don't make sense, return immediately
141    if(
142        (numChars == 0) | (numChars > (16 - col) ) | ((numChars & 0x3) > 0) | 
143        (line > 15) | 
144        (col > 15) | ((col & 0x3) > 0) | 
145        charBuf > 7
146    )
147        return -1;
148   
149    //Use a for loop over 32-bit writes; for some reason, memcpy is useless unless you write a full 16-character line
150    for(i = 0; i<numChars; i=i+4)
151    {
152        XIo_Out32((warp_userioboard_get_lcd_charbufaddr(charBuf) + line*16 + col + i), *(unsigned int *)(chars + i));
153    }
154
155    return 0;
156}
157
158///@brief Reads an image file from a CompactFlash card and displays it on the LCD screen; this function overwrites the chracter map and character buffer 7
159///
160///@param filename String giving the name of the file to read; the file should be FAT16/8.3 compatible name ("image.rgb", for example)
161int warp_userio_lcd_displayCFimage(const char* filename)
162{
163#ifdef XPAR_SYSACE_0_DEVICE_ID
164#ifdef XILFATFS_MAXFILES
165    int i, status;
166    void* fid;
167   
168    fid = sysace_fopen(filename, "r");
169   
170    if(fid == 0)
171        return -2; //fopen failed
172   
173    //Read 128x128 8-bit pixel values from the CF card
174    status = sysace_fread((void *)LCD_CHARMAP_BUFFER, 1, 16384, fid);
175   
176    if(status != 16384)
177        return -3; //Didn't read enough bytes; who knows what state the character map is in now
178
179    status = sysace_fclose(fid);
180   
181    if(status != 0)
182        return -4; //fclose failed; the Sysace controller is likely in a really bad state at this point
183
184    //Write [0,1...255] to character buffer 7
185    //This is required so that each block in the new "character map" (the image) can be read in order
186    //The character buffer is a 32-bit memory and must be written 4 bytes at a time (there are no byte-enables on this RAM)
187    for(i=0; i<256; i=i+4)
188    {
189        //Write 4-bytes: [i i+1 i+2 i+3]
190        XIo_Out32((warp_userioboard_get_lcd_charbufaddr(7)+i), ((i<<24) + ((i+1)<<16) + ((i+2)<<8) + ((i+3)<<0)));
191    }
192
193    //Switch the dipslay to character buffer 7
194    warp_userioboard_set_lcd_charbuf(7);
195
196    return 0; //Success
197#endif
198#endif
199
200    //If the project doesn't have a SysaceACE controller or xil_fatfs library, fail immediately
201    return -1;
202}
203
204///@brief Configures the buzzer waveform generator
205///
206///@param period Sets the period of the buzzer control signal, in units of bus clock cycles; interpreted as 18-bit unsigned integer in hardware
207///@param dutyCycle Sets the duty cycles of the buzzer control signal; intrepreted as UFix18_18 (18-bit unsigned fraction) in hardware
208void warp_userioboard_buzzer_set(unsigned int period, unsigned int dutyCycle)
209{
210    //Set the buzzer period, in units of bus clock cycles
211    // For example, given a 50MHz bus, period=50000 would generate a 1kHz tone
212    XIo_Out32(USER_IO_BOARD_CONTROLLER_BUZZER_PERIOD, (unsigned int)period);
213
214    // dutyCycle is a UFix18_18 fraction that sets the fraction of each period the buzzer is enabled
215    // The value is handed as an unsigned int here to avoid floating point code bloat
216    // The user needs to provide the int corresponding to the UFix18_18 value they want
217    // A value of 0.05 (13107 as an int) is a good starting point
218    XIo_Out32(USER_IO_BOARD_CONTROLLER_BUZZER_DUTYCYCLE, (unsigned int)dutyCycle);
219
220    return;
221}
Note: See TracBrowser for help on using the repository browser.