summaryrefslogtreecommitdiff
path: root/third_party/FreeRTOS/Demo/CORTEX_LM3S316_IAR/hw_include/pdc.c
blob: dfcf418bce4c49b3bb6bd2b6639a3f37a3b124b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//*****************************************************************************
//
// pdc.c - Driver for the Peripheral Device Controller (PDC) on the Stellaris
//         development board.
//
// Copyright (c) 2005,2006 Luminary Micro, Inc.  All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's Stellaris Family of microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 635 of the Stellaris Driver Library.
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup utilities_api
//! @{
//
//*****************************************************************************

#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "ssi.h"
#include "sysctl.h"
#include "pdc.h"

//*****************************************************************************
//
//! Initializes the connection to the PDC.
//!
//! This function will enable clocking to the SSI and GPIO A modules, configure
//! the GPIO pins to be used for an SSI interface, and it will configure the
//! SSI as a 1 Mbps master device, operating in MOTO mode.  It will also enable
//! the SSI module, and will enable the chip select for the PDC on the
//! Stellaris development board.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCInit(void)
{
    //
    // Enable the peripherals used to drive the PDC.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Configure the appropriate pins to be SSI instead of GPIO.
    //
    GPIODirModeSet(GPIO_PORTA_BASE, SSI_CLK | SSI_TX | SSI_RX,
                   GPIO_DIR_MODE_HW);
    GPIODirModeSet(GPIO_PORTA_BASE, SSI_CS, GPIO_DIR_MODE_OUT);
    GPIOPadConfigSet(GPIO_PORTA_BASE, SSI_CLK, GPIO_STRENGTH_4MA,
                     GPIO_PIN_TYPE_STD_WPU);

    //
    // Configure the SSI port.
    //
    SSIConfigSetExpClk(SSI_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
                       SSI_MODE_MASTER, 1000000, 8);
    SSIEnable(SSI_BASE);

    //
    // Reset the PDC SSI state machine.  The chip select needs to be held low
    // for 100ns; the procedure call overhead more than accounts for this time.
    //
    GPIOPinWrite(GPIO_PORTA_BASE, PDC_CS, 0);
    GPIOPinWrite(GPIO_PORTA_BASE, PDC_CS, PDC_CS);
}

//*****************************************************************************
//
//! Read a PDC register.
//!
//! \param ucAddr specifies the PDC register to read.
//!
//! This function will perform the SSI transfers required to read a register in
//! the PDC on the Stellaris development board.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return Returns the value read from the PDC.
//
//*****************************************************************************
unsigned char
PDCRead(unsigned char ucAddr)
{
    unsigned long ulTemp;

    //
    // Send address and read command.
    //
    SSIDataPut(SSI_BASE, (ucAddr & 0x0F) | PDC_RD);

    //
    // Dummy write to force read.
    //
    SSIDataPut(SSI_BASE, 0x00);

    //
    // Flush data read during address write.
    //
    SSIDataGet(SSI_BASE, &ulTemp);

    //
    // If the LCD control register or RAM is being read, then an additional
    // byte needs to be transferred.
    //
    if((ucAddr == PDC_LCD_CSR) || (ucAddr == PDC_LCD_RAM))
    {
        //
        // Dummy write to force read.
        //
        SSIDataPut(SSI_BASE, 0x00);

        //
        // Flush read data.
        //
        SSIDataGet(SSI_BASE, &ulTemp);
    }

    //
    // Read valid data.
    //
    SSIDataGet(SSI_BASE, &ulTemp);

    //
    // Return the data read.
    //
    return(ulTemp & 0xFF);
}

//*****************************************************************************
//
//! Write a PDC register.
//!
//! \param ucAddr specifies the PDC register to write.
//! \param ucData specifies the data to write.
//!
//! This function will perform the SSI transfers required to write a register
//! in the PDC on the Stellaris development board.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCWrite(unsigned char ucAddr, unsigned char ucData)
{
    unsigned long ulTemp;

    //
    // Send address and write command.
    //
    SSIDataPut(SSI_BASE, (ucAddr & 0x0F) | PDC_WR);

    //
    // Write the data.
    //
    SSIDataPut(SSI_BASE, ucData);

    //
    // Flush data read during address write.
    //
    SSIDataGet(SSI_BASE, &ulTemp);

    //
    // Flush data read during data write.
    //
    SSIDataGet(SSI_BASE, &ulTemp);
}

//*****************************************************************************
//
//! Read the current value of the PDC DIP switches.
//!
//! This function will read the current value of the DIP switches attached to
//! the PDC on the Stellaris development board.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return The current state of the DIP switches.
//
//*****************************************************************************
unsigned char
PDCDIPRead(void)
{
    return(PDCRead(PDC_DSW));
}

//*****************************************************************************
//
//! Write to the PDC LEDs.
//!
//! \param ucLED value to write to the LEDs.
//!
//! This function set the state of the LEDs connected to the PDC on the
//! Stellaris development board.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLEDWrite(unsigned char ucLED)
{
    PDCWrite(PDC_LED, ucLED);
}

//*****************************************************************************
//
//! Read the current status of the PDC LEDs.
//!
//! This function will read the state of the LEDs connected to the PDC on the
//! Stellaris development board.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return The value currently displayed by the LEDs.
//
//*****************************************************************************
unsigned char
PDCLEDRead(void)
{
    return(PDCRead(PDC_LED));
}

//*****************************************************************************
//
//! Initializes the LCD display.
//!
//! This function will set up the LCD display for writing. It will set the
//! data bus to 8 bits, set the number of lines to 2, and the font size to
//! 5x10. It will also turn the display off, clear the display, turn the
//! display back on, and enable the backlight.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \note The PDC must be initialized via the PDCInit() function before this
//! function can be called.  Also, it may be necessary to adjust the contrast
//! potentiometer in order to discern any output on the LCD display.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDInit(void)
{
    unsigned char pucCfg[] =
    {
        0x3C,   // Number of lines = 2 / font = 5x10
        0x08,   // Display off
        0x01,   // Display clear
        0x06,   // Entry mode [cursor dir][shift]
        0x0C,   // Display on [display on][curson on][blinking on]
    };
    unsigned long ulIdx;

    //
    // Set the data bus width to eight bits.
    //
    PDCWrite(PDC_LCD_CSR, 0x30);

    //
    // Wait for 4.1ms by reading the PDC version register enough times to
    // guarantee that amount of time has passed.
    //
    for(ulIdx = 0; ulIdx < 257; ulIdx++)
    {
        PDCRead(PDC_VER);
    }

    //
    // Set the data bus width to eight bits.
    //
    PDCWrite(PDC_LCD_CSR, 0x30);

    //
    // Wait for 100us by reading the PDC version register enough times to
    // guarantee that amount of time has passed.  This works out to 112us plus
    // overhead.
    //
    for(ulIdx = 0; ulIdx < 7; ulIdx++)
    {
        PDCRead(PDC_VER);
    }

    //
    // Set the data bus width to eight bits.
    //
    PDCWrite(PDC_LCD_CSR, 0x30);

    //
    // Configure the LCD.
    //
    for(ulIdx = 0; ulIdx < (sizeof(pucCfg) / sizeof(pucCfg[0])); ulIdx++)
    {
        //
        // Wait until the LCD has finished executing any previous command.
        //
        while((PDCRead(PDC_LCD_CSR) & LCD_B_BUSY))
        {
        }

        //
        // Write the next configuration byte.
        //
        PDCWrite(PDC_LCD_CSR, pucCfg[ulIdx]);
    }
}

//*****************************************************************************
//
//! Turns on the backlight.
//!
//! This function turns on the backlight on the LCD.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDBacklightOn(void)
{
    PDCWrite(PDC_CSR, 0x01);
}

//*****************************************************************************
//
//! Turn off the backlight.
//!
//! This function turns off the backlight on the LCD.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDBacklightOff(void)
{
    PDCWrite(PDC_CSR, 0x00);
}

//*****************************************************************************
//
//! Clear the screen.
//!
//! This function clears the contents of the LCD screen.  The cursor will be
//! returned to the upper left corner.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDClear(void)
{
    //
    // Wait until the LCD has finished executing any previous command.
    //
    while((PDCRead(PDC_LCD_CSR) & LCD_B_BUSY))
    {
    }

    //
    // Write the clear display command.
    //
    PDCWrite(PDC_LCD_CSR, LCD_CLEAR);
}

//*****************************************************************************
//
//! Write a character pattern to the LCD.
//!
//! \param ucChar is the character index to create.  Valid values are zero
//! through seven.
//! \param pucData is the data for the character pattern.  It contains eight
//! bytes, with the first byte being the top row of the pattern.  In each byte,
//! the LSB is the right pixel of the pattern.
//!
//! This function will write a character pattern into the LCD for use as a
//! character to be displayed.  After writing the pattern, it can be used on
//! the LCD by writing the corresponding character index to the display.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDCreateChar(unsigned char ucChar, unsigned char *pucData)
{
    //
    // Check the arguments.
    //
    ASSERT(ucChar < 8);

    //
    // Wait until the LCD has finished executing any previous command.
    //
    while((PDCRead(PDC_LCD_CSR) & LCD_B_BUSY))
    {
    }

    //
    // Write the character pattern memory address.
    //
    PDCWrite(PDC_LCD_CSR, LCD_CGADDR + (ucChar * 8));

    //
    // Write the pattern to chacter pattern memory.
    //
    for(ucChar = 0; ucChar < 8; ucChar++)
    {
        //
        // Wait until the LCD has finished executing any previous command.
        //
        while((PDCRead(PDC_LCD_CSR) & LCD_B_BUSY))
        {
        }

        //
        // Write this row of the pattern.
        //
        PDCWrite(PDC_LCD_RAM, *pucData++);
    }
}

//*****************************************************************************
//
//! Set the position of the cursor.
//!
//! \param ucX is the horizontal position.  Valid values are zero through
//! fifteen.
//! \param ucY is the vertical position.. Valid values are zero and one.
//!
//! This function will move the cursor to the specified position.  All
//! characters written to the LCD are placed at the current cursor position,
//! which is automatically advanced.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDSetPos(unsigned char ucX, unsigned char ucY)
{
    //
    // Check the arguments.
    //
    ASSERT(ucX < 16);
    ASSERT(ucY < 2);

    //
    // Wait until the LCD has finished executing any previous command.
    //
    while((PDCRead(PDC_LCD_CSR) & LCD_B_BUSY))
    {
    }

    //
    // Set the cursor position.
    //
    PDCWrite(PDC_LCD_CSR, LCD_DDADDR | (0x40 * ucY) + ucX);
}

//*****************************************************************************
//
//! Writes a string to the LCD display.
//!
//! \param pcStr pointer to the string to be displayed.
//! \param ulCount is the number of characters to be displayed.
//!
//! This function will display a string on the LCD at the current cursor
//! position.  It is the caller's responsibility to position the cursor to the
//! place where the string should be displayed (either explicitly via
//! PDCLCDSetPos() or implicitly from where the cursor was left after a
//! previous call to PDCLCDWrite()), and to properly account for the LCD
//! boundary (line wrapping is not automatically performed).  Null characters
//! are not treated special and are written to the LCD, which interprets it as
//! a special programmable character glyph (see PDCLCDCreateChar()).
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCLCDWrite(const char *pcStr, unsigned long ulCount)
{
    //
    // Write the string to the LCD.
    //
    while(ulCount--)
    {
        //
        // Wait until the LCD has finished executing any previous command.
        //
        while((PDCRead(PDC_LCD_CSR) & LCD_B_BUSY))
        {
        }

        //
        // Write this character to the LCD.
        //
        PDCWrite(PDC_LCD_RAM, *pcStr++);
    }
}

//*****************************************************************************
//
//! Reads a GPIO direction register.
//!
//! \param ucIdx is the index of the GPIO direction register to read; valid
//! values are 0, 1, and 2.
//!
//! This function reads one of the GPIO direction registers in the PDC.  The
//! direction bit is set for pins that are outputs and clear for pins that are
//! inputs.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return The contents of the direction register.
//
//*****************************************************************************
unsigned char
PDCGPIODirRead(unsigned char ucIdx)
{
    //
    // Check the argument.
    //
    ASSERT((ucIdx == 0) || (ucIdx == 1) || (ucIdx == 2));

    //
    // Read the requested direction register.
    //
    if(ucIdx == 0)
    {
        return(PDCRead(PDC_GPXDIR));
    }
    else if(ucIdx == 1)
    {
        return(PDCRead(PDC_GPYDIR));
    }
    else
    {
        return(PDCRead(PDC_GPZDIR));
    }
}

//*****************************************************************************
//
//! Write a GPIO direction register.
//!
//! \param ucIdx is the index of the GPIO direction register to write; valid
//! values are 0, 1, and 2.
//! \param ucValue is the value to write to the GPIO direction register.
//!
//! This function writes ones of the GPIO direction registers in the PDC.  The
//! direction bit should be set for pins that are to be outputs and clear for
//! pins that are to be inputs.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCGPIODirWrite(unsigned char ucIdx, unsigned char ucValue)
{
    //
    // Check the arguments.
    //
    ASSERT((ucIdx == 0) || (ucIdx == 1) || (ucIdx == 2));

    //
    // Write the requested direction register.
    //
    if(ucIdx == 0)
    {
        PDCWrite(PDC_GPXDIR, ucValue);
    }
    else if(ucIdx == 1)
    {
        PDCWrite(PDC_GPYDIR, ucValue);
    }
    else
    {
        PDCWrite(PDC_GPZDIR, ucValue);
    }
}

//*****************************************************************************
//
//! Reads a GPIO data register.
//!
//! \param ucIdx is the index of the GPIO direction register to read; valid
//! values are 0, 1, and 2.
//!
//! This function reads one of the GPIO data registers in the PDC.  The value
//! returned for a pin is the value being driven out for outputs or the value
//! being read for inputs.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return The contents of the data register.
//
//*****************************************************************************
unsigned char
PDCGPIORead(unsigned char ucIdx)
{
    //
    // Check the argument.
    //
    ASSERT((ucIdx == 0) || (ucIdx == 1) || (ucIdx == 2));

    //
    // Read the requested data register.
    //
    if(ucIdx == 0)
    {
        return(PDCRead(PDC_GPXDAT));
    }
    else if(ucIdx == 1)
    {
        return(PDCRead(PDC_GPYDAT));
    }
    else
    {
        return(PDCRead(PDC_GPZDAT));
    }
}

//*****************************************************************************
//
//! Write a GPIO data register.
//!
//! \param ucIdx is the index of the GPIO data register to write; valid values
//! are 0, 1, and 2.
//! \param ucValue is the value to write to the GPIO data register.
//!
//! This function writes one of the GPIO direction registers in the PDC.  The
//! written to a pin is driven out for output pins and ignored for input pins.
//!
//! This function is contained in <tt>utils/pdc.c</tt>, with
//! <tt>utils/pdc.h</tt> containing the API definition for use by applications.
//!
//! \return None.
//
//*****************************************************************************
void
PDCGPIOWrite(unsigned char ucIdx, unsigned char ucValue)
{
    //
    // Check the arguments.
    //
    ASSERT((ucIdx == 0) || (ucIdx == 1) || (ucIdx == 2));

    //
    // Write the requested data register.
    //
    if(ucIdx == 0)
    {
        PDCWrite(PDC_GPXDAT, ucValue);
    }
    else if(ucIdx == 1)
    {
        PDCWrite(PDC_GPYDAT, ucValue);
    }
    else
    {
        PDCWrite(PDC_GPZDAT, ucValue);
    }
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************