]> begriffs open source - cmsis-freertos/blob - Demo/MB96340_Softune/FreeRTOS_96348hs_SK16FX100PMC/Src/utility/taskutility.c
Initial commit
[cmsis-freertos] / Demo / MB96340_Softune / FreeRTOS_96348hs_SK16FX100PMC / Src / utility / taskutility.c
1 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
2
3 /* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
4
5 /* ELIGIBILITY FOR ANY PURPOSES.                                             */
6
7 /*                 (C) Fujitsu Microelectronics Europe GmbH                  */
8
9 /*------------------------------------------------------------------------
10   taskutility.C
11   -
12 -------------------------------------------------------------------------*/
13
14 /*************************@INCLUDE_START************************/
15 #include "mb96348hs.h"
16 #include "FreeRTOS.h"
17 #include "task.h"
18 #include "queue.h"
19
20 static void vUART0Task( void *pvParameters );
21
22 /**************************@INCLUDE_END*************************/
23
24 /*********************@GLOBAL_VARIABLES_START*******************/
25 const char      ASCII[] = "0123456789ABCDEF";
26
27 TaskHandle_t UART_TaskHandle;
28
29 static QueueHandle_t xQueue;
30 void InitUart0( void )
31 {
32         /* Initialize UART asynchronous mode */
33         BGR0 = configCLKP1_CLOCK_HZ / 9600; /* 9600 Baud @ CLKP1 - 56 MHz */
34
35         SCR0 = 0x17;                                            /* 8N1 */
36         SMR0 = 0x0d;                                            /* enable SOT3, Reset, normal mode */
37         SSR0 = 0x02;                                            /* LSB first, enable receive interrupts */
38
39         PIER08_IE2 = 1; /* enable input */
40         DDR08_D2 = 0;   /* switch P08_2 to input */
41         DDR08_D3 = 1;   /* switch P08_3 to output */
42 }
43
44 void Putch0( char ch )  /* sends a char */
45 {
46         while( SSR0_TDRE == 0 );
47
48         /* wait for transmit buffer empty       */
49         TDR0 = ch;      /* put ch into buffer                   */
50 }
51
52 char Getch0( void ) /* waits for and returns incomming char     */
53 {
54         volatile unsigned       ch;
55
56         while( SSR0_RDRF == 0 );
57
58         /* wait for data received       */
59         if( SSR0_ORE )          /* overrun error                */
60         {
61                 ch = RDR0;              /* reset error flags            */
62                 return ( char ) ( -1 );
63         }
64         else
65         {
66                 return( RDR0 ); /* return char                  */
67         }
68 }
69
70 void Puts0( const char *Name1 ) /* Puts a String to UART */
71 {
72         volatile short  i, len;
73         len = strlen( Name1 );
74
75         for( i = 0; i < len; i++ )      /* go through string */
76         {
77                 if( Name1[i] == 10 )
78                 {
79                         Putch0( 13 );
80                 }
81
82                 Putch0( Name1[i] );                                     /* send it out                           */
83         }
84 }
85
86 void Puthex0( unsigned long n, unsigned char digits )
87 {
88         unsigned char   digit = 0, div = 0, i;
89
90         div = ( 4 * (digits - 1) );                             /* init shift divisor */
91         for( i = 0; i < digits; i++ )
92         {
93                 digit = ( (n >> div) & 0xF );           /* get hex-digit value */
94                 Putch0( digit + ((digit < 0xA) ? '0' : 'A' - 0xA) );
95                 div -= 4;               /* next digit shift */
96         }
97 }
98
99 void Putdec0( unsigned long x, int digits )
100 {
101         short   i;
102         char    buf[10], sign = 1;
103
104         if( digits < 0 )
105         {                                       /* should be print of zero? */
106                 digits *= ( -1 );
107                 sign = 1;
108         }
109
110         buf[digits] = '\0'; /* end sign of string */
111
112         for( i = digits; i > 0; i-- )
113         {
114                 buf[i - 1] = ASCII[x % 10];
115                 x = x / 10;
116         }
117
118         if( sign )
119         {
120                 for( i = 0; buf[i] == '0'; i++ )
121                 {                               /* no print of zero */
122                         if( i < digits - 1 )
123                         {
124                                 buf[i] = ' ';
125                         }
126                 }
127         }
128
129         Puts0( buf );           /* send string */
130 }
131
132 void vUtilityStartTraceTask( unsigned portBASE_TYPE uxPriority )
133 {
134         xQueue = xQueueCreate( 5, sizeof( char ) );
135
136         if( xQueue != NULL )
137         {
138                 portENTER_CRITICAL();
139                 InitUart0();
140                 portEXIT_CRITICAL();
141                 xTaskCreate( vUART0Task, "UART1", configMINIMAL_STACK_SIZE * 3, ( void * ) NULL, uxPriority, &UART_TaskHandle );
142         }
143 }
144
145 static void vUART0Task( void *pvParameters )
146 {
147         static char     buff[ 800 ] = { 0 };
148         unsigned long   trace_len;
149         signed long             i, j, l = 0;
150
151         unsigned char   ch;
152         ( void ) pvParameters;
153
154         SSR0_RIE = 1;
155
156         Puts0( "\n -------------MB96348 FreeRTOS DEMO Task List and Trace Utility----------- \n" );
157
158         for( ;; )
159         {
160                 Puts0( "\n\rPress any of the following keys for the corresponding functionality: " );
161
162                 Puts0( "\n\r1: To call vTaskList() and display current task status " );
163
164                 /* The legacy trace is no longer supported.  Use FreeRTOS+Trace instead.
165                 Puts0( "\n\r2: To call vTaskStartTrace() and to display trace results once the trace ends" ); */
166
167                 /* Block on the semaphore.  The UART interrupt will use the semaphore to
168                 wake this task when required. */
169                 xQueueReceive( xQueue, &ch, portMAX_DELAY );
170
171                 switch( ch )
172                 {
173                         case '1':
174                                 vTaskList( buff );
175                                 Puts0( "\n\rThe current task list is as follows...." );
176                                 Puts0( "\n\r----------------------------------------------" );
177                                 Puts0( "\n\rName          State  Priority  Stack   Number" );
178                                 Puts0( "\n\r----------------------------------------------" );
179                                 Puts0( buff );
180                                 Puts0( "\r----------------------------------------------" );
181                                 break;
182
183                         /* The legacy trace is no longer supported.  Use FreeRTOS+Trace instead.
184                         case '2':
185                                 vTaskStartTrace( (signed char *) buff, sizeof( buff ) );
186                                 Puts0( "\n\rThe trace started!!" );
187                                 vTaskDelay( (TickType_t) 500 );
188                                 trace_len = ulTaskEndTrace();
189                                 Puts0( "\n\rThe trace ended!!" );
190                                 Puts0( "\n\rThe trace is as follows...." );
191                                 Puts0( "\n\r--------------------------------------------------------" );
192                                 Puts0( "\n\r  Tick     | Task Number  |     Tick     | Task Number  |" );
193                                 Puts0( "\n\r--------------------------------------------------------\n\r" );
194
195                                 for( i = 0; i < trace_len; i += 4 )
196                                 {
197                                         for( j = i + 3; j >= i; j-- )
198                                         {
199                                                 Puthex0( buff[j], 2 );
200                                         }
201
202                                         Puts0( "   |   " );
203                                         l++;
204                                         if( l == 4 )
205                                         {
206                                                 Puts0( "\n" );
207                                                 l = 0;
208                                         }
209                                 }
210
211                                 Puts0( "\r--------------------------------------------------------" );
212                                 break; */
213
214                         default:
215                                 break;
216                 }
217
218                 Puts0( "\n" );
219         }
220 }
221
222 __interrupt void UART0_TraceRxISR( void )
223 {
224 unsigned char ch;
225 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
226
227         ch = RDR0;
228         xQueueSendFromISR( xQueue, &ch, &xHigherPriorityTaskWoken );
229 }