Timer module test
This commit is contained in:
		
							parent
							
								
									e9ef03890f
								
							
						
					
					
						commit
						d6f774eaea
					
				| 
						 | 
				
			
			@ -1,58 +0,0 @@
 | 
			
		|||
TARGET   = timer
 | 
			
		||||
 | 
			
		||||
TARGET_ARCH=riscv32
 | 
			
		||||
 | 
			
		||||
CC       = riscv32-unknown-elf-gcc
 | 
			
		||||
 | 
			
		||||
# compiling flags here
 | 
			
		||||
CFLAGS = -Wall -I. -O0 -static -march=rv32imac -mabi=ilp32 --specs=nosys.specs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
LINKER   = riscv32-unknown-linux-gnu-gcc
 | 
			
		||||
# linking flags here
 | 
			
		||||
LDFLAGS   = -I. --entry main -L/opt/riscv/riscv32-unknown-elf/lib/ -T ld_script.ld
 | 
			
		||||
LIBS   = $(EXTRA_LIBS)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# change these to proper directories where each file should be
 | 
			
		||||
SRCDIR   = ./
 | 
			
		||||
OBJDIR   = .
 | 
			
		||||
BINDIR   = ./
 | 
			
		||||
INCDIR = -I.
 | 
			
		||||
LIBDIR = -L.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
SOURCES  := $(wildcard $(SRCDIR)/*.c)
 | 
			
		||||
INCLUDES := $(wildcard $(INCDIR)/*.h)
 | 
			
		||||
OBJECTS  := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
 | 
			
		||||
#OBJECTS := main.o timerasm.o
 | 
			
		||||
 | 
			
		||||
rm       = rm -f
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$(BINDIR)/$(TARGET): $(OBJECTS)
 | 
			
		||||
#	$(LINKER) $(OBJECTS) $(LDFLAGS) $(LIBS) $(LIBDIR) -o $@
 | 
			
		||||
	riscv32-unknown-linux-gnu-objdump -d $< > dump
 | 
			
		||||
	objcopy -Oihex $< $(TARGET).hex
 | 
			
		||||
#	@echo "Linking complete!"
 | 
			
		||||
 | 
			
		||||
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
 | 
			
		||||
	@echo "Compiling "$<" ..."
 | 
			
		||||
#	$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@
 | 
			
		||||
	$(CC) $(CFLAGS) $(INCDIR) $< -o $@
 | 
			
		||||
	@echo "Done!"
 | 
			
		||||
 | 
			
		||||
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.S
 | 
			
		||||
	@echo "Assembling "$<" ..."
 | 
			
		||||
	$(CC) $(CFLAGS) $(INCDIR) $< -o $@
 | 
			
		||||
	echo "Done!"
 | 
			
		||||
 | 
			
		||||
.PHONY: clean
 | 
			
		||||
clean:
 | 
			
		||||
	@$(rm) $(OBJECTS) *.hex dump
 | 
			
		||||
	@echo "Cleanup complete!"
 | 
			
		||||
 | 
			
		||||
.PHONY: remove
 | 
			
		||||
remove: clean
 | 
			
		||||
	@$(rm) $(BINDIR)/$(TARGET)
 | 
			
		||||
	@echo "Executable removed!"
 | 
			
		||||
							
								
								
									
										262381
									
								
								tests/C/timer/log
								
								
								
								
							
							
						
						
									
										262381
									
								
								tests/C/timer/log
								
								
								
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -1,58 +0,0 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#define TIMER (*(uint32_t *)0x40004000)
 | 
			
		||||
#define TIMER_CMP (*(uint32_t *)0x40004008)
 | 
			
		||||
#define TRACE (*(unsigned char *)0x40000000)
 | 
			
		||||
 | 
			
		||||
volatile int ticks = 0;
 | 
			
		||||
 | 
			
		||||
int _write(int file, const char *ptr, int len) {
 | 
			
		||||
  int x;
 | 
			
		||||
 | 
			
		||||
  for (x = 0; x < len; x++) {
 | 
			
		||||
    TRACE =  *ptr++;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return (len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void timer_ISR() {
 | 
			
		||||
  ticks++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void register_timer_isr() {
 | 
			
		||||
  asm volatile("la t0, timer_ISR\n csrw mtvec, t0");
 | 
			
		||||
  asm volatile("li t1, 0x888\ncsrw mie, t1");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(void) {
 | 
			
		||||
 | 
			
		||||
  uint32_t timer_value;
 | 
			
		||||
  uint32_t start_time;
 | 
			
		||||
 | 
			
		||||
  register_timer_isr();
 | 
			
		||||
 | 
			
		||||
  timer_value = TIMER;
 | 
			
		||||
  printf("Time: %ld ns\n", timer_value);
 | 
			
		||||
 | 
			
		||||
  timer_value = TIMER;
 | 
			
		||||
  printf("Time: %ld ns\n", timer_value);
 | 
			
		||||
 | 
			
		||||
  timer_value = TIMER;
 | 
			
		||||
  printf("Time: %ld ns\n", timer_value);
 | 
			
		||||
 | 
			
		||||
  timer_value = TIMER;
 | 
			
		||||
  printf("Time: %ld ns\n", timer_value);
 | 
			
		||||
 | 
			
		||||
  start_time = TIMER;
 | 
			
		||||
  TIMER_CMP = start_time + 1000;
 | 
			
		||||
  do {
 | 
			
		||||
    timer_value = TIMER;
 | 
			
		||||
  } while (timer_value < start_time + 50000);
 | 
			
		||||
  asm volatile ("ecall");
 | 
			
		||||
 | 
			
		||||
  printf("Timer: %ld ns\n", timer_value);
 | 
			
		||||
  asm volatile ("ecall");
 | 
			
		||||
  return 0;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,61 +0,0 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#define TIMER (*(uint64_t *)0x40004000)
 | 
			
		||||
#define TIMER_CMP (*(uint64_t *)0x40004008)
 | 
			
		||||
#define TRACE (*(unsigned char *)0x40000000)
 | 
			
		||||
 | 
			
		||||
volatile int ticks = 0;
 | 
			
		||||
 | 
			
		||||
int _write(int file, const char *ptr, int len) {
 | 
			
		||||
  int x;
 | 
			
		||||
 | 
			
		||||
  for (x = 0; x < len; x++) {
 | 
			
		||||
    TRACE =  *ptr++;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return (len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void timer_ISR() {
 | 
			
		||||
  uint32_t timer_value;
 | 
			
		||||
 | 
			
		||||
  ticks++;
 | 
			
		||||
//  timer_value = TIMER;
 | 
			
		||||
//  TIMER_CMP = timer_value + 1000;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void register_timer_isr() {
 | 
			
		||||
  asm volatile("la t0, TIMER_CMP_INT \n csrw mtvec, t0");
 | 
			
		||||
  asm volatile("li t1, 0x888 \n csrw mie, t1");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(void) {
 | 
			
		||||
 | 
			
		||||
  uint32_t timer_value;
 | 
			
		||||
  uint32_t start_time;
 | 
			
		||||
 | 
			
		||||
  register_timer_isr();
 | 
			
		||||
 | 
			
		||||
  start_time = TIMER;
 | 
			
		||||
  TIMER_CMP = start_time + 10000;
 | 
			
		||||
  printf("set timer to %ld ns\n", start_time + 10000);
 | 
			
		||||
 
 | 
			
		||||
  do {
 | 
			
		||||
    timer_value = TIMER;
 | 
			
		||||
  } while (timer_value < start_time + 200000);
 | 
			
		||||
 | 
			
		||||
  start_time = TIMER;
 | 
			
		||||
  TIMER_CMP = start_time + 10000;
 | 
			
		||||
  printf("set timer to %ld ns\n", start_time + 10000); 
 | 
			
		||||
 | 
			
		||||
  do {
 | 
			
		||||
    timer_value = TIMER;
 | 
			
		||||
  } while (timer_value < start_time + 200000);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  printf("Timer: %ld ns\n", timer_value);
 | 
			
		||||
  printf("ticks: %ld\n", ticks);
 | 
			
		||||
  asm volatile ("ecall");
 | 
			
		||||
  return 0;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue