//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
//// ////
|
//// ////
|
//// ////
|
//// ////
|
//// OR1K Timer Demo Software ////
|
//// OR1K Timer Demo Software ////
|
//// ////
|
//// ////
|
//// Description: ////
|
//// Description: ////
|
//// Demonstrate timer. ////
|
//// Demonstrate timer. ////
|
//// First block main loop while checking ticks, then ////
|
//// First block main loop while checking ticks, then ////
|
//// install custom tick handler and do other things ////
|
//// install custom tick handler and do other things ////
|
//// while tick timer fires. The "other things" use ////
|
//// while tick timer fires. The "other things" use ////
|
//// the UART, so too does the custom tick handler, ////
|
//// the UART, so too does the custom tick handler, ////
|
//// so the tick interrupt is blocked while the main ////
|
//// so the tick interrupt is blocked while the main ////
|
//// loop is using the UART. ////
|
//// loop is using the UART. ////
|
//// ////
|
//// ////
|
//// Author(s): ////
|
//// Author(s): ////
|
//// Julius Baxter, julius@opencores.org ////
|
//// Julius Baxter, julius@opencores.org ////
|
//// ////
|
//// ////
|
//// ////
|
//// ////
|
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
//// ////
|
//// ////
|
//// Copyright (C) 2011 Authors and OPENCORES.ORG ////
|
//// Copyright (C) 2011 Authors and OPENCORES.ORG ////
|
//// ////
|
//// ////
|
//// This source file may be used and distributed without ////
|
//// This source file may be used and distributed without ////
|
//// restriction provided that this copyright statement is not ////
|
//// restriction provided that this copyright statement is not ////
|
//// removed from the file and that any derivative work contains ////
|
//// removed from the file and that any derivative work contains ////
|
//// the original copyright notice and the associated disclaimer. ////
|
//// the original copyright notice and the associated disclaimer. ////
|
//// ////
|
//// ////
|
//// This source file is free software; you can redistribute it ////
|
//// This source file is free software; you can redistribute it ////
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
//// Public License as published by the Free Software Foundation; ////
|
//// Public License as published by the Free Software Foundation; ////
|
//// either version 2.1 of the License, or (at your option) any ////
|
//// either version 2.1 of the License, or (at your option) any ////
|
//// later version. ////
|
//// later version. ////
|
//// ////
|
//// ////
|
//// This source is distributed in the hope that it will be ////
|
//// This source is distributed in the hope that it will be ////
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
//// details. ////
|
//// details. ////
|
//// ////
|
//// ////
|
//// You should have received a copy of the GNU Lesser General ////
|
//// You should have received a copy of the GNU Lesser General ////
|
//// Public License along with this source; if not, download it ////
|
//// Public License along with this source; if not, download it ////
|
//// from http://www.opencores.org/lgpl.shtml ////
|
//// from http://www.opencores.org/lgpl.shtml ////
|
//// ////
|
//// ////
|
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
|
|
#include "cpu-utils.h"
|
#include "cpu-utils.h"
|
#include "spr-defs.h"
|
#include "spr-defs.h"
|
#include "board.h"
|
#include "board.h"
|
#include "uart.h"
|
#include "uart.h"
|
#include "printf.h"
|
#include "printf.h"
|
|
|
/* RTOS-like critical section enter and exit functions */
|
/* RTOS-like critical section enter and exit functions */
|
static inline void disable_ttint(void)
|
static inline void disable_ttint(void)
|
{
|
{
|
// Disable timer interrupt in supervisor register
|
// Disable timer interrupt in supervisor register
|
mtspr (SPR_SR, mfspr (SPR_SR) & ~SPR_SR_TEE);
|
mtspr (SPR_SR, mfspr (SPR_SR) & ~SPR_SR_TEE);
|
// Disable timer interrupt generation in tick timer mode register
|
// Disable timer interrupt generation in tick timer mode register
|
//mtspr(SPR_TTMR, mfspr (SPR_TTMR) & ~SPR_TTMR_IE);
|
//mtspr(SPR_TTMR, mfspr (SPR_TTMR) & ~SPR_TTMR_IE);
|
}
|
}
|
|
|
static inline void enable_ttint(void)
|
static inline void enable_ttint(void)
|
{
|
{
|
// Enable timer interrupt in supervisor register
|
// Enable timer interrupt in supervisor register
|
mtspr(SPR_SR, SPR_SR_TEE | mfspr(SPR_SR));
|
mtspr(SPR_SR, SPR_SR_TEE | mfspr(SPR_SR));
|
// Enable timer interrupt generation in tick timer mode register
|
// Enable timer interrupt generation in tick timer mode register
|
//mtspr(SPR_TTMR, mfspr (SPR_TTMR) | SPR_TTMR_IE);
|
//mtspr(SPR_TTMR, mfspr (SPR_TTMR) | SPR_TTMR_IE);
|
}
|
}
|
|
|
#define MAIN_PRINT_ENTER disable_ttint
|
#define MAIN_PRINT_ENTER disable_ttint
|
#define MAIN_PRINT_EXIT enable_ttint
|
#define MAIN_PRINT_EXIT enable_ttint
|
|
|
|
|
void
|
void
|
print_time(void)
|
print_time(void)
|
{
|
{
|
static int ms_counter = 0;
|
static int ms_counter = 0;
|
static int s_counter = 0;
|
static int s_counter = 0;
|
// Position the cursor on the line and print the time so far.
|
// Position the cursor on the line and print the time so far.
|
|
|
// Usually we go on 100 ticks per second, which is 10ms each:
|
// Usually we go on 100 ticks per second, which is 10ms each:
|
if (TICKS_PER_SEC == 100)
|
if (TICKS_PER_SEC == 100)
|
ms_counter += 10;
|
ms_counter += 10;
|
|
|
if (ms_counter >= 1000)
|
if (ms_counter >= 1000)
|
{
|
{
|
s_counter++;
|
s_counter++;
|
ms_counter = 0;
|
ms_counter = 0;
|
}
|
}
|
|
|
// Sometimes print hasn't finished properly...
|
// Sometimes print hasn't finished properly...
|
printf("\r");
|
printf("\r");
|
// ANSI Escape sequence "\esc[40C" - cursor forward 40 places
|
// ANSI Escape sequence "\esc[40C" - cursor forward 40 places
|
uart_putc(DEFAULT_UART,0x1b);
|
uart_putc(DEFAULT_UART,0x1b);
|
uart_putc(DEFAULT_UART,0x5b);
|
uart_putc(DEFAULT_UART,0x5b);
|
uart_putc(DEFAULT_UART,'4');
|
uart_putc(DEFAULT_UART,'4');
|
uart_putc(DEFAULT_UART,'0');
|
uart_putc(DEFAULT_UART,'0');
|
uart_putc(DEFAULT_UART,'C');
|
uart_putc(DEFAULT_UART,'C');
|
// ANSI Escape sequence "\esc[K" - delete rest of line
|
// ANSI Escape sequence "\esc[K" - delete rest of line
|
uart_putc(DEFAULT_UART,0x1b);
|
uart_putc(DEFAULT_UART,0x1b);
|
uart_putc(DEFAULT_UART,0x5b);
|
uart_putc(DEFAULT_UART,0x5b);
|
uart_putc(DEFAULT_UART,'K');
|
uart_putc(DEFAULT_UART,'K');
|
printf("%2d.%03d",s_counter,ms_counter);
|
printf("%2d.%03d",s_counter,ms_counter);
|
printf("\r");
|
printf("\r");
|
|
|
}
|
}
|
|
|
void our_timer_handler(void *);
|
void our_timer_handler(void);
|
|
|
void our_timer_handler(void *ptr)
|
void our_timer_handler(void)
|
{
|
{
|
// Call time output function
|
// Call time output function
|
print_time();
|
print_time();
|
|
|
// can potentially also call cpu_timer_tick() here to hook back into
|
// can potentially also call cpu_timer_tick() here to hook back into
|
// the CPU's timer tick function.
|
// the CPU's timer tick function.
|
|
|
// Reset timer mode register to interrupt with same interval
|
// Reset timer mode register to interrupt with same interval
|
mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
|
mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
|
((IN_CLK/TICKS_PER_SEC) & SPR_TTMR_PERIOD));
|
((IN_CLK/TICKS_PER_SEC) & SPR_TTMR_PERIOD));
|
}
|
}
|
|
|
|
|
int
|
int
|
main (void)
|
main (void)
|
{
|
{
|
|
|
int seconds;
|
int seconds;
|
unsigned long *adr;
|
unsigned long *adr;
|
unsigned long data;
|
unsigned long data;
|
volatile int i;
|
volatile int i;
|
|
|
uart_init(DEFAULT_UART);
|
uart_init(DEFAULT_UART);
|
|
|
printf("\nOR1200 Timer Demo\n");
|
printf("\nOR1200 Timer Demo\n");
|
|
|
printf("\nInitialising Timer\n");
|
printf("\nInitialising Timer\n");
|
// Reset timing variables
|
// Reset timing variables
|
seconds = 0;
|
seconds = 0;
|
cpu_reset_timer_ticks();
|
cpu_reset_timer_ticks();
|
cpu_enable_timer();
|
cpu_enable_timer();
|
|
|
printf("\nBlocking main() loop for 5 seconds\n");
|
printf("\nBlocking main() loop for 5 seconds\n");
|
|
|
printf("Elapsed: %ds",seconds);
|
printf("Elapsed: %ds",seconds);
|
|
|
while(seconds < 5)
|
while(seconds < 5)
|
{
|
{
|
while (cpu_get_timer_ticks() < (TICKS_PER_SEC * (seconds+1)));
|
while (cpu_get_timer_ticks() < (TICKS_PER_SEC * (seconds+1)));
|
seconds++;
|
seconds++;
|
printf("\rElapsed: %ds",seconds);
|
printf("\rElapsed: %ds",seconds);
|
}
|
}
|
printf("\n");
|
printf("\n");
|
|
|
printf("\nInstalling our timer handler\n");
|
printf("\nInstalling our timer handler\n");
|
|
|
printf("\nmain() loop will dump mem. contents, timer interrupt will print time\n");
|
printf("\nmain() loop will dump mem. contents, timer interrupt will print time\n");
|
|
|
add_handler(0x5 /* Timer */, our_timer_handler);
|
add_handler(0x5 /* Timer */, our_timer_handler);
|
|
|
adr=0;
|
adr=0;
|
while(1)
|
while(1)
|
{
|
{
|
data = *(adr++);
|
data = *(adr++);
|
|
|
// Disable tick timer interrupt here, as UART printing is not
|
// Disable tick timer interrupt here, as UART printing is not
|
// re-entrant!
|
// re-entrant!
|
MAIN_PRINT_ENTER;
|
MAIN_PRINT_ENTER;
|
printf("0x%08x: 0x%08x\r",adr,data);
|
printf("0x%08x: 0x%08x\r",adr,data);
|
MAIN_PRINT_EXIT;
|
MAIN_PRINT_EXIT;
|
// Delay a little bit..
|
// Delay a little bit..
|
for(i=0;i<40000;i++);
|
for(i=0;i<40000;i++);
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|