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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_STM32F103_Keil/] [main.c] - Blame information for rev 594

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 582 jeremybenn
/*
2
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
3
 
4
    ***************************************************************************
5
    *                                                                         *
6
    * If you are:                                                             *
7
    *                                                                         *
8
    *    + New to FreeRTOS,                                                   *
9
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
10
    *    + Looking for basic training,                                        *
11
    *    + Wanting to improve your FreeRTOS skills and productivity           *
12
    *                                                                         *
13
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
14
    *                                                                         *
15
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
16
    *                  http://www.FreeRTOS.org/Documentation                  *
17
    *                                                                         *
18
    * A pdf reference manual is also available.  Both are usually delivered   *
19
    * to your inbox within 20 minutes to two hours when purchased between 8am *
20
    * and 8pm GMT (although please allow up to 24 hours in case of            *
21
    * exceptional circumstances).  Thank you for your support!                *
22
    *                                                                         *
23
    ***************************************************************************
24
 
25
    This file is part of the FreeRTOS distribution.
26
 
27
    FreeRTOS is free software; you can redistribute it and/or modify it under
28
    the terms of the GNU General Public License (version 2) as published by the
29
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30
    ***NOTE*** The exception to the GPL is included to allow you to distribute
31
    a combined work that includes FreeRTOS without being obliged to provide the
32
    source code for proprietary components outside of the FreeRTOS kernel.
33
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
36
    more details. You should have received a copy of the GNU General Public
37
    License and the FreeRTOS license exception along with FreeRTOS; if not it
38
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
39
    by writing to Richard Barry, contact details for whom are available on the
40
    FreeRTOS WEB site.
41
 
42
    1 tab == 4 spaces!
43
 
44
    http://www.FreeRTOS.org - Documentation, latest information, license and
45
    contact details.
46
 
47
    http://www.SafeRTOS.com - A version that is certified for use in safety
48
    critical systems.
49
 
50
    http://www.OpenRTOS.com - Commercial support, development, porting,
51
    licensing and training services.
52
*/
53
 
54
/*
55
 * Creates all the demo application tasks, then starts the scheduler.  The WEB
56
 * documentation provides more details of the standard demo application tasks.
57
 * In addition to the standard demo tasks, the following tasks and tests are
58
 * defined and/or created within this file:
59
 *
60
 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
61
 * using a free running timer to demonstrate the use of the
62
 * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt
63
 * service routine measures the number of processor clocks that occur between
64
 * each interrupt - and in so doing measures the jitter in the interrupt timing.
65
 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
66
 * displayed on the LCD by the 'Check' task as described below.  The
67
 * fast interrupt is configured and handled in the timertest.c source file.
68
 *
69
 * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that
70
 * is permitted to access the display directly.  Other tasks wishing to write a
71
 * message to the LCD send the message on a queue to the LCD task instead of
72
 * accessing the LCD themselves.  The LCD task just blocks on the queue waiting
73
 * for messages - waking and displaying the messages as they arrive.
74
 *
75
 * "Check" task -  This only executes every five seconds but has the highest
76
 * priority so is guaranteed to get processor time.  Its main function is to
77
 * check that all the standard demo tasks are still operational.  Should any
78
 * unexpected behaviour within a demo task be discovered the 'check' task will
79
 * write an error to the LCD (via the LCD task).  If all the demo tasks are
80
 * executing with their expected behaviour then the check task writes PASS
81
 * along with the max jitter time to the LCD (again via the LCD task), as
82
 * described above.
83
 *
84
 */
85
 
86
/* Standard includes. */
87
#include <stdio.h>
88
 
89
/* Scheduler includes. */
90
#include "FreeRTOS.h"
91
#include "task.h"
92
#include "queue.h"
93
 
94
/* Library includes. */
95
#include "stm32f10x_it.h"
96
 
97
/* Demo app includes. */
98
#include "lcd.h"
99
#include "LCD_Message.h"
100
#include "BlockQ.h"
101
#include "death.h"
102
#include "integer.h"
103
#include "blocktim.h"
104
#include "partest.h"
105
#include "semtest.h"
106
#include "PollQ.h"
107
#include "flash.h"
108
#include "comtest2.h"
109
 
110
/* Task priorities. */
111
#define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
112
#define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
113
#define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
114
#define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
115
#define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )
116
#define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )
117
#define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
118
#define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )
119
 
120
/* Constants related to the LCD. */
121
#define mainMAX_LINE                                            ( 240 )
122
#define mainROW_INCREMENT                                       ( 24 )
123
#define mainMAX_COLUMN                                          ( 20 )
124
#define mainCOLUMN_START                                        ( 319 )
125
#define mainCOLUMN_INCREMENT                            ( 16 )
126
 
127
/* The maximum number of message that can be waiting for display at any one
128
time. */
129
#define mainLCD_QUEUE_SIZE                                      ( 3 )
130
 
131
/* The check task uses the sprintf function so requires a little more stack. */
132
#define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )
133
 
134
/* Dimensions the buffer into which the jitter time is written. */
135
#define mainMAX_MSG_LEN                                         25
136
 
137
/* The time between cycles of the 'check' task. */
138
#define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )
139
 
140
/* The number of nano seconds between each processor clock. */
141
#define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
142
 
143
/* Baud rate used by the comtest tasks. */
144
#define mainCOM_TEST_BAUD_RATE          ( 115200 )
145
 
146
/* The LED used by the comtest tasks. See the comtest.c file for more
147
information. */
148
#define mainCOM_TEST_LED                        ( 3 )
149
 
150
/*-----------------------------------------------------------*/
151
 
152
/*
153
 * Configure the clocks, GPIO and other peripherals as required by the demo.
154
 */
155
static void prvSetupHardware( void );
156
 
157
/*
158
 * Configure the LCD as required by the demo.
159
 */
160
static void prvConfigureLCD( void );
161
 
162
/*
163
 * The LCD is written two by more than one task so is controlled by a
164
 * 'gatekeeper' task.  This is the only task that is actually permitted to
165
 * access the LCD directly.  Other tasks wanting to display a message send
166
 * the message to the gatekeeper.
167
 */
168
static void vLCDTask( void *pvParameters );
169
 
170
/*
171
 * Retargets the C library printf function to the USART.
172
 */
173
int fputc( int ch, FILE *f );
174
 
175
/*
176
 * Checks the status of all the demo tasks then prints a message to the
177
 * display.  The message will be either PASS - and include in brackets the
178
 * maximum measured jitter time (as described at the to of the file), or a
179
 * message that describes which of the standard demo tasks an error has been
180
 * discovered in.
181
 *
182
 * Messages are not written directly to the terminal, but passed to vLCDTask
183
 * via a queue.
184
 */
185
static void vCheckTask( void *pvParameters );
186
 
187
/*
188
 * Configures the timers and interrupts for the fast interrupt test as
189
 * described at the top of this file.
190
 */
191
extern void vSetupTimerTest( void );
192
 
193
/*-----------------------------------------------------------*/
194
 
195
/* The queue used to send messages to the LCD task. */
196
xQueueHandle xLCDQueue;
197
 
198
/*-----------------------------------------------------------*/
199
 
200
int main( void )
201
{
202
#ifdef DEBUG
203
  debug();
204
#endif
205
 
206
        prvSetupHardware();
207
 
208
        /* Create the queue used by the LCD task.  Messages for display on the LCD
209
        are received via this queue. */
210
        xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
211
 
212
        /* Start the standard demo tasks. */
213
        vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
214
    vCreateBlockTimeTasks();
215
    vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
216
    vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
217
    vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
218
        vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
219
        vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
220
 
221
        /* Start the tasks defined within this file/specific to this demo. */
222
    xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
223
        xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
224
 
225
        /* The suicide tasks must be created last as they need to know how many
226
        tasks were running prior to their creation in order to ascertain whether
227
        or not the correct/expected number of tasks are running at any given time. */
228
    vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
229
 
230
        /* Configure the timers used by the fast interrupt timer test. */
231
        vSetupTimerTest();
232
 
233
        /* Start the scheduler. */
234
        vTaskStartScheduler();
235
 
236
        /* Will only get here if there was not enough heap space to create the
237
        idle task. */
238
        return 0;
239
}
240
/*-----------------------------------------------------------*/
241
 
242
void vLCDTask( void *pvParameters )
243
{
244
xLCDMessage xMessage;
245
 
246
        /* Initialise the LCD and display a startup message. */
247
        prvConfigureLCD();
248
        LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );
249
 
250
        for( ;; )
251
        {
252
                /* Wait for a message to arrive that requires displaying. */
253
                while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
254
 
255
                /* Display the message.  Print each message to a different position. */
256
                printf( ( portCHAR const * ) xMessage.pcMessage );
257
        }
258
}
259
/*-----------------------------------------------------------*/
260
 
261
static void vCheckTask( void *pvParameters )
262
{
263
portTickType xLastExecutionTime;
264
xLCDMessage xMessage;
265
static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
266
extern unsigned portSHORT usMaxJitter;
267
 
268
        xLastExecutionTime = xTaskGetTickCount();
269
        xMessage.pcMessage = cPassMessage;
270
 
271
    for( ;; )
272
        {
273
                /* Perform this check every mainCHECK_DELAY milliseconds. */
274
                vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
275
 
276
                /* Has an error been found in any task? */
277
 
278
        if( xAreBlockingQueuesStillRunning() != pdTRUE )
279
                {
280
                        xMessage.pcMessage = "ERROR IN BLOCK Q\n";
281
                }
282
                else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
283
                {
284
                        xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
285
                }
286
        else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
287
        {
288
            xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
289
        }
290
        else if( xArePollingQueuesStillRunning() != pdTRUE )
291
        {
292
            xMessage.pcMessage = "ERROR IN POLL Q\n";
293
        }
294
        else if( xIsCreateTaskStillRunning() != pdTRUE )
295
        {
296
            xMessage.pcMessage = "ERROR IN CREATE\n";
297
        }
298
        else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
299
        {
300
            xMessage.pcMessage = "ERROR IN MATH\n";
301
        }
302
                else if( xAreComTestTasksStillRunning() != pdTRUE )
303
                {
304
                        xMessage.pcMessage = "ERROR IN COM TEST\n";
305
                }
306
                else
307
                {
308
                        sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
309
                }
310
 
311
                /* Send the message to the LCD gatekeeper for display. */
312
                xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
313
        }
314
}
315
/*-----------------------------------------------------------*/
316
 
317
static void prvSetupHardware( void )
318
{
319
        /* Start with the clocks in their expected state. */
320
        RCC_DeInit();
321
 
322
        /* Enable HSE (high speed external clock). */
323
        RCC_HSEConfig( RCC_HSE_ON );
324
 
325
        /* Wait till HSE is ready. */
326
        while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
327
        {
328
        }
329
 
330
        /* 2 wait states required on the flash. */
331
        *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
332
 
333
        /* HCLK = SYSCLK */
334
        RCC_HCLKConfig( RCC_SYSCLK_Div1 );
335
 
336
        /* PCLK2 = HCLK */
337
        RCC_PCLK2Config( RCC_HCLK_Div1 );
338
 
339
        /* PCLK1 = HCLK/2 */
340
        RCC_PCLK1Config( RCC_HCLK_Div2 );
341
 
342
        /* PLLCLK = 8MHz * 9 = 72 MHz. */
343
        RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
344
 
345
        /* Enable PLL. */
346
        RCC_PLLCmd( ENABLE );
347
 
348
        /* Wait till PLL is ready. */
349
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
350
        {
351
        }
352
 
353
        /* Select PLL as system clock source. */
354
        RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
355
 
356
        /* Wait till PLL is used as system clock source. */
357
        while( RCC_GetSYSCLKSource() != 0x08 )
358
        {
359
        }
360
 
361
        /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
362
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
363
                                                        | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
364
 
365
        /* SPI2 Periph clock enable */
366
        RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
367
 
368
 
369
        /* Set the Vector Table base address at 0x08000000 */
370
        NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
371
 
372
        NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
373
 
374
        /* Configure HCLK clock as SysTick clock source. */
375
        SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
376
 
377
        vParTestInitialise();
378
}
379
/*-----------------------------------------------------------*/
380
 
381
static void prvConfigureLCD( void )
382
{
383
GPIO_InitTypeDef GPIO_InitStructure;
384
 
385
        /* Configure LCD Back Light (PA8) as output push-pull */
386
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
387
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
388
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
389
        GPIO_Init( GPIOA, &GPIO_InitStructure );
390
 
391
        /* Set the Backlight Pin */
392
        GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
393
 
394
        /* Initialize the LCD */
395
        LCD_Init();
396
 
397
        /* Set the Back Color */
398
        LCD_SetBackColor( White );
399
 
400
        /* Set the Text Color */
401
        LCD_SetTextColor( 0x051F );
402
 
403
        LCD_Clear();
404
}
405
/*-----------------------------------------------------------*/
406
 
407
int fputc( int ch, FILE *f )
408
{
409
static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;
410
static unsigned portCHAR ucLine = 0;
411
 
412
        if( ( usColumn == 0 ) && ( ucLine == 0 ) )
413
        {
414
                LCD_Clear();
415
        }
416
 
417
        if( ch != '\n' )
418
        {
419
                /* Display one character on LCD */
420
                LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
421
 
422
                /* Decrement the column position by 16 */
423
                usRefColumn -= mainCOLUMN_INCREMENT;
424
 
425
                /* Increment the character counter */
426
                usColumn++;
427
                if( usColumn == mainMAX_COLUMN )
428
                {
429
                        ucLine += mainROW_INCREMENT;
430
                        usRefColumn = mainCOLUMN_START;
431
                        usColumn = 0;
432
                }
433
        }
434
        else
435
        {
436
                /* Move back to the first column of the next line. */
437
                ucLine += mainROW_INCREMENT;
438
                usRefColumn = mainCOLUMN_START;
439
                usColumn = 0;
440
        }
441
 
442
        /* Wrap back to the top of the display. */
443
        if( ucLine >= mainMAX_LINE )
444
        {
445
                ucLine = 0;
446
        }
447
 
448
        return ch;
449
}
450
/*-----------------------------------------------------------*/
451
 
452
#ifdef  DEBUG
453
/* Keep the linker happy. */
454
void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )
455
{
456
        for( ;; )
457
        {
458
        }
459
}
460
#endif

powered by: WebSVN 2.1.0

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