source: rtems/c/src/lib/libbsp/m68k/uC5282/clock/clock.c @ be5b08a

4.104.114.84.95
Last change on this file since be5b08a was be5b08a, checked in by Eric Norum <WENorum@…>, on 01/29/06 at 22:48:33

Add code to maintain CPU load average.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Use the last periodic interval timer (PIT3) as the system clock.
3 *
4 *  Author: W. Eric Norum <norume@aps.anl.gov>
5 *
6 *  COPYRIGHT (c) 2005.
7 *  On-Line Applications Research Corporation (OAR).
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.OARcorp.com/rtems/license.html.
12 *
13 *  $Id$
14 */
15
16#include <rtems.h>
17#include <bsp.h>
18#include <mcf5282/mcf5282.h>
19
20/*
21 * Use INTC0 base
22 */
23#define CLOCK_VECTOR (64+58)
24
25/*
26 * CPU load counters
27 * Place in static RAM so updates don't hit the SDRAM
28 */
29extern int __SRAMBASE[];
30#define IDLE_COUNTER      __SRAMBASE[0]
31#define FILTERED_IDLE     __SRAMBASE[1]
32#define MAX_IDLE_COUNT    __SRAMBASE[2]
33#define FILTER_SHIFT    6
34
35/*
36 * Periodic interval timer interrupt handler
37 */
38#define Clock_driver_support_at_tick()                                       \
39    do {                                                                     \
40        int idle = IDLE_COUNTER;                                             \
41        IDLE_COUNTER = 0;                                                    \
42        if (idle > MAX_IDLE_COUNT)                                           \
43            MAX_IDLE_COUNT = idle;                                           \
44        FILTERED_IDLE = idle + FILTERED_IDLE - (FILTERED_IDLE>>FILTER_SHIFT);\
45        MCF5282_PIT3_PCSR |= MCF5282_PIT_PCSR_PIF;                           \
46    } while (0)
47
48/*
49 * Attach clock interrupt handler
50 */
51#define Clock_driver_support_install_isr( _new, _old )             \
52    do {                                                           \
53        _old = (rtems_isr_entry)set_vector(_new, CLOCK_VECTOR, 1);  \
54    } while(0)
55
56/*
57 * Turn off the clock
58 */
59#define Clock_driver_support_shutdown_hardware()   \
60    do {                                           \
61        MCF5282_PIT3_PCSR &= ~MCF5282_PIT_PCSR_EN; \
62    } while(0)
63
64/*
65 * Set up the clock hardware
66 *
67 * Prescale so that it counts in microseconds
68 * System clock frequency better be 2**n (1<=n<=16) MHz!
69 */
70#define Clock_driver_support_initialize_hardware()                       \
71    do {                                                                 \
72        int level;                                                       \
73        int preScaleCode = -2;                                           \
74        int preScaleDivisor = bsp_get_CPU_clock_speed() / 1000000;       \
75        while (preScaleDivisor) {                                        \
76            preScaleDivisor >>= 1;                                       \
77            preScaleCode++;                                              \
78        }                                                                \
79        IDLE_COUNTER = 0;                                                \
80        FILTERED_IDLE = 0;                                               \
81        MAX_IDLE_COUNT = 0;                                              \
82        bsp_allocate_interrupt(PIT3_IRQ_LEVEL, PIT3_IRQ_PRIORITY);       \
83        MCF5282_INTC0_ICR58 = MCF5282_INTC_ICR_IL(PIT3_IRQ_LEVEL) |      \
84                              MCF5282_INTC_ICR_IP(PIT3_IRQ_PRIORITY);    \
85        rtems_interrupt_disable( level );                                \
86        MCF5282_INTC0_IMRH &= ~MCF5282_INTC_IMRH_INT58;                  \
87                MCF5282_PIT3_PCSR &= ~MCF5282_PIT_PCSR_EN;                       \
88        rtems_interrupt_enable( level );                                 \
89                MCF5282_PIT3_PCSR = MCF5282_PIT_PCSR_PRE(preScaleCode) |         \
90                            MCF5282_PIT_PCSR_OVW |                       \
91                            MCF5282_PIT_PCSR_PIE |                       \
92                            MCF5282_PIT_PCSR_RLD;                        \
93                MCF5282_PIT3_PMR = BSP_Configuration.microseconds_per_tick - 1;  \
94                MCF5282_PIT3_PCSR = MCF5282_PIT_PCSR_PRE(preScaleCode) |         \
95                            MCF5282_PIT_PCSR_PIE |                       \
96                            MCF5282_PIT_PCSR_RLD |                       \
97                            MCF5282_PIT_PCSR_EN;                         \
98    } while (0)
99
100/*
101 * Provide our own version of the idle task
102 */
103void _BSP_Thread_Idle_body(void)
104{
105    for(;;)
106        asm volatile ("addq.l #1,__SRAMBASE"); /* Atomic increment */
107}
108
109int rtems_bsp_cpu_load_percentage(void)
110{
111    return 100 - ((100 * (FILTERED_IDLE >> FILTER_SHIFT)) / MAX_IDLE_COUNT);
112}
113
114#include "../../../shared/clockdrv_shell.c"
Note: See TracBrowser for help on using the repository browser.