| 1 |
488 |
julius |
//////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//// ////
|
| 3 |
|
|
//// ////
|
| 4 |
|
|
//// OR1K Timer Demo Software ////
|
| 5 |
|
|
//// ////
|
| 6 |
|
|
//// Description: ////
|
| 7 |
|
|
//// Demonstrate timer. ////
|
| 8 |
|
|
//// First block main loop while checking ticks, then ////
|
| 9 |
|
|
//// install custom tick handler and do other things ////
|
| 10 |
|
|
//// while tick timer fires. The "other things" use ////
|
| 11 |
|
|
//// the UART, so too does the custom tick handler, ////
|
| 12 |
|
|
//// so the tick interrupt is blocked while the main ////
|
| 13 |
|
|
//// loop is using the UART. ////
|
| 14 |
|
|
//// ////
|
| 15 |
|
|
//// Author(s): ////
|
| 16 |
|
|
//// Julius Baxter, julius@opencores.org ////
|
| 17 |
|
|
//// ////
|
| 18 |
|
|
//// ////
|
| 19 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 20 |
|
|
//// ////
|
| 21 |
|
|
//// Copyright (C) 2011 Authors and OPENCORES.ORG ////
|
| 22 |
|
|
//// ////
|
| 23 |
|
|
//// This source file may be used and distributed without ////
|
| 24 |
|
|
//// restriction provided that this copyright statement is not ////
|
| 25 |
|
|
//// removed from the file and that any derivative work contains ////
|
| 26 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
| 27 |
|
|
//// ////
|
| 28 |
|
|
//// This source file is free software; you can redistribute it ////
|
| 29 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
| 30 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
| 31 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
| 32 |
|
|
//// later version. ////
|
| 33 |
|
|
//// ////
|
| 34 |
|
|
//// This source is distributed in the hope that it will be ////
|
| 35 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
| 36 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
| 37 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
| 38 |
|
|
//// details. ////
|
| 39 |
|
|
//// ////
|
| 40 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
| 41 |
|
|
//// Public License along with this source; if not, download it ////
|
| 42 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
| 43 |
|
|
//// ////
|
| 44 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 45 |
|
|
|
| 46 |
|
|
#include "cpu-utils.h"
|
| 47 |
|
|
#include "spr-defs.h"
|
| 48 |
|
|
#include "board.h"
|
| 49 |
|
|
#include "uart.h"
|
| 50 |
|
|
#include "printf.h"
|
| 51 |
|
|
|
| 52 |
|
|
/* RTOS-like critical section enter and exit functions */
|
| 53 |
|
|
static inline void disable_ttint(void)
|
| 54 |
|
|
{
|
| 55 |
|
|
// Disable timer interrupt in supervisor register
|
| 56 |
|
|
mtspr (SPR_SR, mfspr (SPR_SR) & ~SPR_SR_TEE);
|
| 57 |
|
|
// Disable timer interrupt generation in tick timer mode register
|
| 58 |
|
|
//mtspr(SPR_TTMR, mfspr (SPR_TTMR) & ~SPR_TTMR_IE);
|
| 59 |
|
|
}
|
| 60 |
|
|
|
| 61 |
|
|
static inline void enable_ttint(void)
|
| 62 |
|
|
{
|
| 63 |
|
|
// Enable timer interrupt in supervisor register
|
| 64 |
|
|
mtspr(SPR_SR, SPR_SR_TEE | mfspr(SPR_SR));
|
| 65 |
|
|
// Enable timer interrupt generation in tick timer mode register
|
| 66 |
|
|
//mtspr(SPR_TTMR, mfspr (SPR_TTMR) | SPR_TTMR_IE);
|
| 67 |
|
|
}
|
| 68 |
|
|
|
| 69 |
|
|
#define MAIN_PRINT_ENTER disable_ttint
|
| 70 |
|
|
#define MAIN_PRINT_EXIT enable_ttint
|
| 71 |
|
|
|
| 72 |
|
|
|
| 73 |
|
|
void
|
| 74 |
|
|
print_time(void)
|
| 75 |
|
|
{
|
| 76 |
|
|
static int ms_counter = 0;
|
| 77 |
|
|
static int s_counter = 0;
|
| 78 |
|
|
// Position the cursor on the line and print the time so far.
|
| 79 |
|
|
|
| 80 |
|
|
// Usually we go on 100 ticks per second, which is 10ms each:
|
| 81 |
|
|
if (TICKS_PER_SEC == 100)
|
| 82 |
|
|
ms_counter += 10;
|
| 83 |
|
|
|
| 84 |
|
|
if (ms_counter >= 1000)
|
| 85 |
|
|
{
|
| 86 |
|
|
s_counter++;
|
| 87 |
|
|
ms_counter = 0;
|
| 88 |
|
|
}
|
| 89 |
|
|
|
| 90 |
|
|
// Sometimes print hasn't finished properly...
|
| 91 |
|
|
printf("\r");
|
| 92 |
|
|
// ANSI Escape sequence "\esc[40C" - cursor forward 40 places
|
| 93 |
|
|
uart_putc(DEFAULT_UART,0x1b);
|
| 94 |
|
|
uart_putc(DEFAULT_UART,0x5b);
|
| 95 |
|
|
uart_putc(DEFAULT_UART,'4');
|
| 96 |
|
|
uart_putc(DEFAULT_UART,'0');
|
| 97 |
|
|
uart_putc(DEFAULT_UART,'C');
|
| 98 |
|
|
// ANSI Escape sequence "\esc[K" - delete rest of line
|
| 99 |
|
|
uart_putc(DEFAULT_UART,0x1b);
|
| 100 |
|
|
uart_putc(DEFAULT_UART,0x5b);
|
| 101 |
|
|
uart_putc(DEFAULT_UART,'K');
|
| 102 |
|
|
printf("%2d.%03d",s_counter,ms_counter);
|
| 103 |
|
|
printf("\r");
|
| 104 |
|
|
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
489 |
julius |
void our_timer_handler(void);
|
| 108 |
488 |
julius |
|
| 109 |
489 |
julius |
void our_timer_handler(void)
|
| 110 |
488 |
julius |
{
|
| 111 |
|
|
// Call time output function
|
| 112 |
|
|
print_time();
|
| 113 |
|
|
|
| 114 |
|
|
// can potentially also call cpu_timer_tick() here to hook back into
|
| 115 |
|
|
// the CPU's timer tick function.
|
| 116 |
|
|
|
| 117 |
|
|
// Reset timer mode register to interrupt with same interval
|
| 118 |
|
|
mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
|
| 119 |
|
|
((IN_CLK/TICKS_PER_SEC) & SPR_TTMR_PERIOD));
|
| 120 |
|
|
}
|
| 121 |
|
|
|
| 122 |
|
|
|
| 123 |
|
|
int
|
| 124 |
|
|
main (void)
|
| 125 |
|
|
{
|
| 126 |
|
|
|
| 127 |
|
|
int seconds;
|
| 128 |
|
|
unsigned long *adr;
|
| 129 |
|
|
unsigned long data;
|
| 130 |
|
|
volatile int i;
|
| 131 |
|
|
|
| 132 |
|
|
uart_init(DEFAULT_UART);
|
| 133 |
|
|
|
| 134 |
|
|
printf("\nOR1200 Timer Demo\n");
|
| 135 |
|
|
|
| 136 |
|
|
printf("\nInitialising Timer\n");
|
| 137 |
|
|
// Reset timing variables
|
| 138 |
|
|
seconds = 0;
|
| 139 |
|
|
cpu_reset_timer_ticks();
|
| 140 |
|
|
cpu_enable_timer();
|
| 141 |
|
|
|
| 142 |
|
|
printf("\nBlocking main() loop for 5 seconds\n");
|
| 143 |
|
|
|
| 144 |
|
|
printf("Elapsed: %ds",seconds);
|
| 145 |
|
|
|
| 146 |
|
|
while(seconds < 5)
|
| 147 |
|
|
{
|
| 148 |
|
|
while (cpu_get_timer_ticks() < (TICKS_PER_SEC * (seconds+1)));
|
| 149 |
|
|
seconds++;
|
| 150 |
|
|
printf("\rElapsed: %ds",seconds);
|
| 151 |
|
|
}
|
| 152 |
|
|
printf("\n");
|
| 153 |
|
|
|
| 154 |
|
|
printf("\nInstalling our timer handler\n");
|
| 155 |
|
|
|
| 156 |
|
|
printf("\nmain() loop will dump mem. contents, timer interrupt will print time\n");
|
| 157 |
|
|
|
| 158 |
|
|
add_handler(0x5 /* Timer */, our_timer_handler);
|
| 159 |
|
|
|
| 160 |
|
|
adr=0;
|
| 161 |
|
|
while(1)
|
| 162 |
|
|
{
|
| 163 |
|
|
data = *(adr++);
|
| 164 |
|
|
|
| 165 |
|
|
// Disable tick timer interrupt here, as UART printing is not
|
| 166 |
|
|
// re-entrant!
|
| 167 |
|
|
MAIN_PRINT_ENTER;
|
| 168 |
|
|
printf("0x%08x: 0x%08x\r",adr,data);
|
| 169 |
|
|
MAIN_PRINT_EXIT;
|
| 170 |
|
|
// Delay a little bit..
|
| 171 |
|
|
for(i=0;i<40000;i++);
|
| 172 |
|
|
}
|
| 173 |
|
|
|
| 174 |
|
|
return 0;
|
| 175 |
|
|
}
|
| 176 |
|
|
|