OpenCores
URL https://opencores.org/ocsvn/mips32r1/mips32r1/trunk

Subversion Repositories mips32r1

[/] [mips32r1/] [trunk/] [Software/] [demos/] [XD5_Threads/] [src/] [app/] [app.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayersg
#include <stdlib.h>
2
#include "drivers/lcd.h"
3
#include "drivers/led.h"
4
#include "os/lock.h"
5
 
6
static int lock;
7
 
8
void LED_showTID(int tid);
9
void LED_hideTID(int tid);
10
void delay(void);
11
 
12
 
13
/* Each thread will enter at main */
14
int main(void)
15
{
16
        int tid = 0;
17
        char val = 0;
18
 
19
 
20
        /* Load unique thread ID which is stored in register k1 */
21
        asm (
22
                "addu %[tid], $27, $0\n\t"
23
                : [tid] "=r"(tid)
24
                :
25
                :
26
        );
27
 
28
        /* Allow a single thread to set up the LCD screen and LEDs*/
29
        if (tid == 1) {
30
                LED_write(0);
31
                LCD_clear();
32
                LCD_setPos(0);
33
                LCD_printString("Thread: 12345678");
34
                LCD_setPos(16);
35
                LCD_printString("Work:");
36
        }
37
 
38
        /* Loop a critical section in which work is performed */
39
        while (1) {
40
                Lock(&lock, NULL, NULL);
41
                LED_showTID(tid);
42
                LCD_setPos(23 + (uint8_t)tid);
43
                LCD_printByte(val);
44
                val++;
45
                LED_hideTID(tid);
46
                Unlock(&lock);
47
                delay();
48
        }
49
 
50
        return 0;
51
}
52
 
53
 
54
/* delay:
55
 *   Create a software delay. Use this to increase or decrease
56
 *   the probability that the thread holds the lock when the
57
 *   scheduler swaps it out.
58
 */
59
void delay(void)
60
{
61
        /* A higher value of 'c' makes it less likely that
62
         * a lock will be held, but also lowers throughput. */
63
        volatile unsigned int c = 16;  // 0, 2, 8, 14, 16, 18, 500, 507
64
 
65
        while (c != 0) {
66
                c--;
67
        }
68
}
69
 
70
/* check_violation:
71
 *   Checks to see if more than one LED is lit, meaning that more
72
 *   than one is in the critical section. If this is true, the
73
 *   Error LED is lit.
74
 */
75
void check_violation(uint8_t led)
76
{
77
        int i;
78
        int found_high = 0;
79
 
80
        for (i=0; i<8; i++) {
81
                found_high += (led & 0x1);
82
                if (found_high > 1) {
83
                        LED_write(LED_read() | LED_ERROR);
84
                        break;
85
                }
86
                led >>= 1;
87
        }
88
}
89
 
90
/* LED_showTID:
91
 *   Shows the thread ID (1->8) as a single lit LED (0->7).
92
 */
93
void LED_showTID(int tid)
94
{
95
        uint32_t led;
96
 
97
        led = LED_read();
98
        led |= (0x80 >> (tid - 1));
99
        LED_write(led);
100
        check_violation(led);
101
}
102
 
103
 
104
/* LED_hideTID:
105
 *   Turns off the LED (0->7) corresponding to the thread ID (1->8).
106
 */
107
void LED_hideTID(int tid)
108
{
109
        uint32_t led;
110
 
111
        led = LED_read();
112
        led &= ~(0x80 >> (tid - 1));
113
        LED_write(led);
114
}
115
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.