]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTUS_APS3_GCC/Demo/7seg.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTUS_APS3_GCC / Demo / 7seg.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 #include <stdlib.h>\r
31 \r
32 /* Scheduler include files. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* Demo program include files. */\r
37 #include "7seg.h"\r
38 #include "demoGpio.h"\r
39 \r
40 #define x7segSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
41 \r
42 static void prvRefreshTask( void *pvParameters );\r
43 static void prvCountTask( void *pvParameters );\r
44 \r
45 /* Value to output to 7 segment display\r
46 led_digits[0] is the right most digit */\r
47 static signed char seg7_digits[4];\r
48 \r
49 void vStart7SegTasks( unsigned portBASE_TYPE uxPriority )\r
50 {\r
51         xTaskCreate( prvRefreshTask, "7SegRefresh", x7segSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t *) NULL );\r
52         xTaskCreate( prvCountTask, "7SegCount", x7segSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t *) NULL );\r
53 }\r
54 /*-----------------------------------------------------------*/\r
55 \r
56 static void prvRefreshTask( void *pvParameters )\r
57 {\r
58 /* This is table 3.3 from the Spartan-3 Starter Kit board user guide */\r
59 const unsigned char bits[ 16 ] =\r
60 {\r
61         0x01,\r
62         0x4f,\r
63         0x12,\r
64         0x06,\r
65         0x4c,\r
66         0x24,\r
67         0x20,\r
68         0x0f,\r
69         0x00,\r
70         0x04,\r
71         0x08,\r
72         0x60,\r
73         0x31,\r
74         0x42,\r
75         0x30,\r
76         0x38\r
77 };\r
78 \r
79 const unsigned char apsx[4] =\r
80 {\r
81         0x06, /* 3 */\r
82         0x24, /* S */\r
83         0x18, /* P */\r
84         0x08  /* A */\r
85 };\r
86 \r
87 TickType_t xRefreshRate, xLastRefreshTime;\r
88 \r
89 /* Digit to scan */\r
90 static int d = 0;\r
91 \r
92         xRefreshRate = 2;\r
93 \r
94         /* We need to initialise xLastRefreshTime prior to the first call to\r
95         vTaskDelayUntil(). */\r
96         xLastRefreshTime = xTaskGetTickCount();\r
97 \r
98         for (;;)\r
99         {\r
100                 for( d = 0; d < 4; d++ )\r
101                 {\r
102                         vTaskDelayUntil ( &xLastRefreshTime, xRefreshRate );\r
103 \r
104                         /* Display digit */\r
105                         gpio->out.an  = -1;\r
106                         if( ( seg7_digits[ 1 ] == 4 ) || ( seg7_digits[ 1 ] == 5 ) )\r
107                         {\r
108                                 gpio->out.digit = apsx[ d ];\r
109                         }\r
110                         else\r
111                         {\r
112                                 gpio->out.digit = bits[ seg7_digits[ d ] ];\r
113                         }\r
114 \r
115                         gpio->out.dp = 1;\r
116                         gpio->out.an  = ~(1 << d);\r
117                 }\r
118         }\r
119 }\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 static void prvCountTask( void *pvParameters )\r
123 {\r
124 TickType_t xCountRate, xLastCountTime;\r
125 \r
126         /* Approximately 20HZ */\r
127         xCountRate = configTICK_RATE_HZ / 20;\r
128 \r
129         /* We need to initialise xLastCountTime prior to the first call to\r
130         vTaskDelayUntil(). */\r
131         xLastCountTime = xTaskGetTickCount();\r
132 \r
133         for (;;)\r
134         {\r
135                 vTaskDelayUntil( &xLastCountTime, xCountRate );\r
136 \r
137                 /* Really ugly way to do BCD arithmetic.... */\r
138                 seg7_digits[ 0 ] -= 1;\r
139                 if( seg7_digits[ 0 ] < 0 )\r
140                 {\r
141                         seg7_digits[ 0 ] = 9;\r
142                         seg7_digits[ 1 ] -= 1;\r
143                         if( seg7_digits[ 1 ] < 0 )\r
144                         {\r
145                                 seg7_digits[ 1 ] = 9;\r
146                                 seg7_digits[ 2 ] -= 1;\r
147                                 if( seg7_digits[ 2 ] < 0 )\r
148                                 {\r
149                                         seg7_digits[ 2 ] = 9;\r
150                                         seg7_digits[ 3 ] -= 1;\r
151                                         if( seg7_digits[ 3 ] < 0 )\r
152                                         {\r
153                                                 seg7_digits[ 3 ] = 9;\r
154                                         }\r
155                                 }\r
156                         }\r
157                 }\r
158         }\r
159 }\r
160 \r
161 \r