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

Subversion Repositories openrisc

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

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.  Messages
74
 * can either be a text string to display, or an instruction to update MEMS
75
 * input.  The MEMS input is used to display a ball that can be moved around
76
 * LCD by tilting the STM32 Primer.  45% is taken as the neutral position.
77
 *
78
 * "Check" task -  This only executes every five seconds but has the highest
79
 * priority so is guaranteed to get processor time.  Its main function is to
80
 * check that all the standard demo tasks are still operational.  Should any
81
 * unexpected behaviour within a demo task be discovered the 'check' task will
82
 * write an error to the LCD (via the LCD task).  If all the demo tasks are
83
 * executing with their expected behaviour then the check task writes PASS
84
 * along with the max jitter time to the LCD (again via the LCD task), as
85
 * described above.
86
 *
87
 * Tick Hook - A tick hook is provided just for demonstration purposes.  In
88
 * this case it is used to periodically send an instruction to updated the
89
 * MEMS input to the LCD task.
90
 *
91
 */
92
 
93
/* CircleOS includes.  Some of the CircleOS peripheral functionality is
94
utilised, although CircleOS itself is not used. */
95
#include "circle.h"
96
 
97
/* Standard includes. */
98
#include <string.h>
99
 
100
/* Scheduler includes. */
101
#include "FreeRTOS.h"
102
#include "task.h"
103
#include "queue.h"
104
 
105
/* Demo app includes. */
106
#include "BlockQ.h"
107
#include "blocktim.h"
108
#include "GenQTest.h"
109
#include "partest.h"
110
#include "QPeek.h"
111
 
112
/* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format
113
and therefore takes up a large proportion of the Flash space.  Setting this
114
parameter to 0 excludes the bitmap from the build, freeing up Flash space for
115
extra code. */
116
#define mainINCLUDE_BITMAP                                      0
117
 
118
#if mainINCLUDE_BITMAP == 1
119
        #include "bitmap.h"
120
#endif
121
 
122
/* Task priorities. */
123
#define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
124
#define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
125
#define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
126
#define mainGEN_Q_PRIORITY                                      ( tskIDLE_PRIORITY + 0 )
127
#define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
128
 
129
/* Splash screen related constants. */
130
#define mainBITMAP_Y                                            ( 38 )
131
#define mainBITMAP_X                                            ( 18 )
132
#define mainURL_Y                                                       ( 8 )
133
#define mainURL_X                                                       ( 78 )
134
#define mainSPLASH_SCREEN_DELAY         ( 2000 / portTICK_RATE_MS )
135
 
136
/* Text drawing related constants. */
137
#define mainLCD_CHAR_HEIGHT                     ( 13 )
138
#define mainLCD_MAX_Y                           ( 110 )
139
 
140
/* The maximum number of message that can be waiting for display at any one
141
time. */
142
#define mainLCD_QUEUE_SIZE                                      ( 3 )
143
 
144
/* The check task uses the sprintf function so requires a little more stack. */
145
#define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )
146
 
147
/* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),
148
these can require a larger stack. */
149
#define configLCD_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )
150
 
151
/* Dimensions the buffer into which the jitter time is written. */
152
#define mainMAX_MSG_LEN                                         25
153
 
154
/* The time between cycles of the 'check' task. */
155
#define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )
156
 
157
/* The period at which the MEMS input should be updated. */
158
#define mainMEMS_DELAY                                          ( ( portTickType ) 100 / portTICK_RATE_MS )
159
 
160
/* The rate at which the flash task toggles the LED. */
161
#define mainFLASH_DELAY                                         ( ( portTickType ) 1000 / portTICK_RATE_MS )
162
 
163
/* The number of nano seconds between each processor clock. */
164
#define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
165
 
166
/* The two types of message that can be sent to the LCD task. */
167
#define mainUPDATE_BALL_MESSAGE                         ( 0 )
168
#define mainWRITE_STRING_MESSAGE                        ( 1 )
169
 
170
/* Type of the message sent to the LCD task. */
171
typedef struct
172
{
173
        portBASE_TYPE xMessageType;
174
        signed char *pcMessage;
175
} xLCDMessage;
176
 
177
/*-----------------------------------------------------------*/
178
 
179
/*
180
 * Configure the clocks, GPIO and other peripherals as required by the demo.
181
 */
182
static void prvSetupHardware( void );
183
 
184
/*
185
 * The LCD is written two by more than one task so is controlled by a
186
 * 'gatekeeper' task.  This is the only task that is actually permitted to
187
 * access the LCD directly.  Other tasks wanting to display a message send
188
 * the message to the gatekeeper.
189
 */
190
static void prvLCDTask( void *pvParameters );
191
 
192
/*
193
 * Checks the status of all the demo tasks then prints a message to the
194
 * display.  The message will be either PASS - and include in brackets the
195
 * maximum measured jitter time (as described at the to of the file), or a
196
 * message that describes which of the standard demo tasks an error has been
197
 * discovered in.
198
 *
199
 * Messages are not written directly to the terminal, but passed to prvLCDTask
200
 * via a queue.
201
 *
202
 * The check task also receives instructions to update the MEMS input, which
203
 * in turn can also lead to the LCD being updated.
204
 */
205
static void prvCheckTask( void *pvParameters );
206
 
207
/*
208
 * Configures the timers and interrupts for the fast interrupt test as
209
 * described at the top of this file.
210
 */
211
extern void vSetupTimerTest( void );
212
 
213
/*
214
 * A cut down version of sprintf() used to percent the HUGE GCC library
215
 * equivalent from being included in the binary image.
216
 */
217
extern int sprintf(char *out, const char *format, ...);
218
 
219
/*
220
 * Simple toggle the LED periodically for timing verification.
221
 */
222
static void prvFlashTask( void *pvParameters );
223
 
224
/*-----------------------------------------------------------*/
225
 
226
/* The queue used to send messages to the LCD task. */
227
xQueueHandle xLCDQueue;
228
 
229
/*-----------------------------------------------------------*/
230
 
231
int main( void )
232
{
233
        #ifdef DEBUG
234
                debug();
235
        #endif
236
 
237
        prvSetupHardware();
238
 
239
        /* Create the queue used by the LCD task.  Messages for display on the LCD
240
        are received via this queue. */
241
        xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
242
 
243
        /* Start the standard demo tasks. */
244
        vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
245
    vCreateBlockTimeTasks();
246
        vStartGenericQueueTasks( mainGEN_Q_PRIORITY );
247
        vStartQueuePeekTasks();
248
        vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
249
 
250
        /* Start the tasks defined within this file/specific to this demo. */
251
    xTaskCreate( prvCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
252
        xTaskCreate( prvLCDTask, ( signed portCHAR * ) "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
253
        xTaskCreate( prvFlashTask, ( signed portCHAR * ) "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );
254
 
255
        /* Configure the timers used by the fast interrupt timer test. */
256
        vSetupTimerTest();
257
 
258
        /* Start the scheduler. */
259
        vTaskStartScheduler();
260
 
261
        /* Will only get here if there was not enough heap space to create the
262
        idle task. */
263
        return 0;
264
}
265
/*-----------------------------------------------------------*/
266
 
267
void prvLCDTask( void *pvParameters )
268
{
269
xLCDMessage xMessage;
270
portCHAR cY = mainLCD_CHAR_HEIGHT;
271
const portCHAR * const pcString = "www.FreeRTOS.org";
272
const portCHAR * const pcBlankLine = "                  ";
273
 
274
        DRAW_Init();
275
 
276
        #if mainINCLUDE_BITMAP == 1
277
                DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );
278
        #endif
279
 
280
        LCD_SetScreenOrientation( V9 );
281
        DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );
282
        vTaskDelay( mainSPLASH_SCREEN_DELAY );
283
        LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );
284
 
285
        for( ;; )
286
        {
287
                /* Wait for a message to arrive that requires displaying. */
288
                while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
289
 
290
                /* Check the message type. */
291
                if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )
292
                {
293
                        /* Read the MEMS and update the ball display on the LCD if required. */
294
                        MEMS_Handler();
295
                        POINTER_Handler();
296
                }
297
                else
298
                {
299
                        /* A text string was sent.  First blank off the old text string, then
300
                        draw the new text on the next line down. */
301
                        DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );
302
 
303
                        cY -= mainLCD_CHAR_HEIGHT;
304
                        if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )
305
                        {
306
                                /* Wrap the line onto which we are going to write the text. */
307
                                cY = mainLCD_MAX_Y;
308
                        }
309
 
310
                        /* Display the message. */
311
                        DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );
312
                }
313
        }
314
}
315
/*-----------------------------------------------------------*/
316
 
317
static void prvCheckTask( void *pvParameters )
318
{
319
portTickType xLastExecutionTime;
320
xLCDMessage xMessage;
321
static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
322
extern unsigned portSHORT usMaxJitter;
323
 
324
        /* Initialise the xLastExecutionTime variable on task entry. */
325
        xLastExecutionTime = xTaskGetTickCount();
326
 
327
        /* Setup the message we are going to send to the LCD task. */
328
        xMessage.xMessageType = mainWRITE_STRING_MESSAGE;
329
        xMessage.pcMessage = cPassMessage;
330
 
331
    for( ;; )
332
        {
333
                /* Perform this check every mainCHECK_DELAY milliseconds. */
334
                vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
335
 
336
                /* Has an error been found in any task?   If so then point the text
337
                we are going to send to the LCD task to an error message instead of
338
                the PASS message. */
339
                if( xAreGenericQueueTasksStillRunning() != pdTRUE )
340
                {
341
                        xMessage.pcMessage = "ERROR IN GEN Q";
342
                }
343
        if( xAreBlockingQueuesStillRunning() != pdTRUE )
344
                {
345
                        xMessage.pcMessage = "ERROR IN BLOCK Q";
346
                }
347
                else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
348
                {
349
                        xMessage.pcMessage = "ERROR IN BLOCK TIME";
350
                }
351
        else if( xArePollingQueuesStillRunning() != pdTRUE )
352
        {
353
            xMessage.pcMessage = "ERROR IN POLL Q";
354
        }
355
                else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
356
                {
357
                        xMessage.pcMessage = "ERROR IN PEEK Q";
358
                }
359
                else
360
                {
361
                        /* No errors were found in any task, so send a pass message
362
                        with the max measured jitter time also included (as per the
363
                        fast interrupt test described at the top of this file and on
364
                        the online documentation page for this demo application). */
365
                        sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
366
                }
367
 
368
                /* Send the message to the LCD gatekeeper for display. */
369
                xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
370
        }
371
}
372
/*-----------------------------------------------------------*/
373
 
374
void vApplicationTickHook( void )
375
{
376
static unsigned portLONG ulCallCount;
377
static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };
378
static portBASE_TYPE xHigherPriorityTaskWoken;
379
 
380
        /* Periodically send a message to the LCD task telling it to update
381
        the MEMS input, and then if necessary the LCD. */
382
        ulCallCount++;
383
        if( ulCallCount >= mainMEMS_DELAY )
384
        {
385
                ulCallCount = 0;
386
                xHigherPriorityTaskWoken = pdFALSE;
387
                xQueueSendFromISR( xLCDQueue, &xMemsMessage, &xHigherPriorityTaskWoken );
388
        }
389
}
390
/*-----------------------------------------------------------*/
391
 
392
static void prvSetupHardware( void )
393
{
394
        /* Start with the clocks in their expected state. */
395
        RCC_DeInit();
396
 
397
        /* Enable HSE (high speed external clock). */
398
        RCC_HSEConfig( RCC_HSE_ON );
399
 
400
        /* Wait till HSE is ready. */
401
        while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
402
        {
403
        }
404
 
405
        /* 2 wait states required on the flash. */
406
        *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
407
 
408
        /* HCLK = SYSCLK */
409
        RCC_HCLKConfig( RCC_SYSCLK_Div1 );
410
 
411
        /* PCLK2 = HCLK */
412
        RCC_PCLK2Config( RCC_HCLK_Div1 );
413
 
414
        /* PCLK1 = HCLK/2 */
415
        RCC_PCLK1Config( RCC_HCLK_Div2 );
416
 
417
        /* PLLCLK = 12MHz * 6 = 72 MHz. */
418
        RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );
419
 
420
        /* Enable PLL. */
421
        RCC_PLLCmd( ENABLE );
422
 
423
        /* Wait till PLL is ready. */
424
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
425
        {
426
        }
427
 
428
        /* Select PLL as system clock source. */
429
        RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
430
 
431
        /* Wait till PLL is used as system clock source. */
432
        while( RCC_GetSYSCLKSource() != 0x08 )
433
        {
434
        }
435
 
436
        /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
437
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
438
                                                        | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
439
 
440
        /* SPI2 Periph clock enable */
441
        RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
442
 
443
 
444
        /* Set the Vector Table base address at 0x08000000 */
445
        NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
446
 
447
        NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
448
 
449
        /* Configure HCLK clock as SysTick clock source. */
450
        SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
451
 
452
        /* Misc initialisation, including some of the CircleOS features.  Note
453
        that CircleOS itself is not used. */
454
        vParTestInitialise();
455
        MEMS_Init();
456
        POINTER_Init();
457
        POINTER_SetMode( POINTER_RESTORE_LESS );
458
}
459
/*-----------------------------------------------------------*/
460
 
461
static void prvFlashTask( void *pvParameters )
462
{
463
portTickType xLastExecutionTime;
464
 
465
        /* Initialise the xLastExecutionTime variable on task entry. */
466
        xLastExecutionTime = xTaskGetTickCount();
467
 
468
    for( ;; )
469
        {
470
                /* Simple toggle the LED periodically.  This just provides some timing
471
                verification. */
472
                vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );
473
                vParTestToggleLED( 0 );
474
        }
475
}
476
/*-----------------------------------------------------------*/
477
 
478
void starting_delay( unsigned long ul )
479
{
480
        vTaskDelay( ( portTickType ) ul );
481
}
482
 
483
 
484
 

powered by: WebSVN 2.1.0

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