Page 1 of 1

timer_wait function may be solved TY :)

Posted: Sat Jun 28, 2008 1:34 pm
by eax
Hiya :)

I cant seem to get this function to work, even with the default code in the function, I gathered you pass an int in which is the number of ticks you want to wait so 18 ticks = 1sec APPROX so 54 ticks = 3 secs, so I wanted to make it wait for 3 secs and have tried every which way but will just post brans function and my call to the function originally since that should be the easiest to fix. I have tried it so many ways that Im thinking just maybe it interrupts before the 3 seonds are up which is why it is never getting out the loop? Would really appreciate if someone could explain why this code isnt working

Code: Select all


void timer_handler(struct regs *r)
{

    /* Increment our 'tick count' */

    timer_ticks++;



    /* Every 18 clocks (approximately 1 second), we will

    *  display a message on the screen */

    if (timer_ticks % 18 == 0)

    {

        puts("One second has passed\n");
        

    }
timer_wait(54); //should be 3 secs
puts("after call\n");
}

void timer_wait(int ticks)
{
unsigned long eticks;
 //ticks to wait is 54 which should be 3 secs
 eticks = timer_ticks + ticks;
while(timer_ticks < eticks );
}
Ive even done it different many ways and get the same result where it never gets out the loop , have also did the same

Code: Select all

   if (timer_ticks % 18 == 0)

    {

        puts("One second has passed\n");
        

    }
in the wait function and the iff never fires which is strange because I have done it so in theory it should be the same values getting tested, any help with this would be greatly appreciated, btw its vmware im using but I still think its more to do with something Im not considering. lol have been at this for hours

Re: timer_wait function from brans guide driving me nuts!

Posted: Sat Jun 28, 2008 1:52 pm
by piranha
My delay function (simplified, removed multitasking stuff) (which works great):

Code: Select all

void delay(int ticks) {
int start = timer_ticks;
while(timer_ticks<start+ticks);
}
-JL

Re: timer_wait function from brans <unsolved>

Posted: Sat Jun 28, 2008 1:57 pm
by eax
Thanks :)

Heres the closest Ive managed to get just after posting but its a bit of a remedial way lol

Code: Select all

void timer_wait(int ticks)

{
unsigned long eticks;
eticks = timer_ticks + ticks;
do{
if(towait == 1)
{
timer_ticks ++;
} else{


    //ticks to wait is 54 which should be 3 secs

    towait = 1;
}
}while(timer_ticks < eticks );
the reason for the towait flag is because the timer has already been incremented comming into the function and I dont want it incremented twice in one interrupt. not sure if my way is working properly , will try what you posted as well, Im doing this as much for understanding as anything else

actually brans initial way shouldnt be infinite looping , is it because he doesnt increment timer_ticks in timer_wait? what gets me is when interrupted timer_ticks should be incremented again in timer_handler but its like thats not happening, if its the case that when its in the while loop that is stopping any other timer_interups from happening then I think I can now see why It didnt work?

edit:

Nah that way isnt working, Im refering to my own code in this post and Im unsure why, its firing to fast , I will try taking that extra increment out the first if and if I cant get my head around whats causing the prob I will try your way, thanks for your reply :)

edit:

your code still isnt working for me in brans timer.c file could you tell me exactly where and how you initialise your timer_ticks variable, i.e is it in the same file as the code you gave me is it global to that file and initialised to zero? Is the int timer var thats passed to the delay function ddone so that the int ticks =18 would = 1 second.

I have no doubt your code works , I can see it should work for me but it doesnt , I still get the sameloop like some number is getting continually reset

Code: Select all

/* bkerndev - Bran's Kernel Development Tutorial

*  By:   Brandon F. ([email protected])

*  Desc: Timer driver

*

*  Notes: No warranty expressed or implied. Use at own risk. */

#include <system.h>



/* This will keep track of how many ticks that the system

*  has been running for */

int timer_ticks=0;

int runonce = 0;

/* Handles the timer. In this case, it's very simple: We

*  increment the 'timer_ticks' variable every time the

*  timer fires. By default, the timer fires 18.222 times

*  per second. Why 18.222Hz? Some engineer at IBM must've

*  been smoking something funky */

void timer_handler(struct regs *r)

{

    /* Increment our 'tick count' */

    timer_ticks++;



    /* Every 18 clocks (approximately 1 second), we will

    *  display a message on the screen */

    if (timer_ticks % 18 == 0)

    {

        puts("One second has passed\n");
        

    }
timer_wait(54); //should be 3 secs




}



/* This will continuously loop until the given time has

*  been reached */

void timer_wait(int ticks)

{
int start_timer = timer_ticks;
//void delay(int ticks) {
//int start = timer_ticks;
//while(timer_ticks<start+ticks);
//}

while(timer_ticks < start_timer+ticks);
puts("waited the TIME \n");




}



/* Sets up the system clock by installing the timer handler

*  into IRQ0 */

void timer_install()

{

    /* Installs 'timer_handler' to IRQ0 */

    irq_install_handler(0, timer_handler);
//clirq_install_handler(int irq, void (*handler)(struct regs *r))

}
edit:

Ahh crap its not because Im calling it from at the end of but inside the timer_handler function is it? , please tell me if Im wrong :) because Im trying to understand this but what I think is because I put it in the timer handler function , i.e the timer_wait(int) call that every time the timer interrupts the values are being reset? (Although that doesnt make sense to me that the timer_counter should reset?, however I guess it would overflow if it didnt get set to 0 every x amount of seconds per interrupt?Is this correct>)

edit:

That timer code stillisnt working correctly even if I call it from main the message right after the while loop never fires and says alloted time waited

edit:

What the hell:( check this one out folks, why does this not work:

Code: Select all

void timer_wait(int ticks)

{
int start_timer = timer_ticks;
}while(timer_ticks < start_timer+ticks);
puts("alloted time PASSED!!!");
brans code doesnt work, (Not needing to submit that since its above ^^ or on his page.


and yet THIS does work:

Code: Select all

void timer_wait(int ticks)

{
int start_timer = timer_ticks;
do{
puts("waiting the time Im retarded and cant print past the while\n");
}while(timer_ticks < start_timer+ticks);


puts("waited the TIME \n");


[
Also this works
void timer_wait(int ticks)
{
int start_timer = timer_ticks;
puts("waiting the time Im retarded and cant print past the while\n");
}while(timer_ticks < start_timer+ticks);
puts("alloted time PASSED!!!");

Now actually Im kinda hoping its to do with the fact I have put int start_timer= timer_ticks outside the whileloop? If this is the case that means the other timer code wouldnt work either?

Is there a better way form to do this than how I have done? On looking at all my submissions and Im thinking of when I did java here if you did like an iff with only one line after it you didnt need bracers , wander if thats anything to do with it?

Re: timer_wait function NOTSOLVED

Posted: Sat Jun 28, 2008 4:02 pm
by pcmattman
Look up the volatile keyword. It's exactly what you need.

Re: timer_wait function NOTSOLVED

Posted: Sat Jun 28, 2008 4:45 pm
by eax
Thanks, will do that , I have seen that used in the code, Im still not sure whats causing it because one min its working with just making stupid editions to my code that shouldnt have relevance(Seriously :) ) and the next its not, I also 100% saved everything this time around and it is running old code?I wander if my makefile has a problem?

Code: Select all

cross:
	@echo Now assembling, compiling, and linking your kernel

	nasm -f elf -o start.o start.asm
	#rem Remember this spot here: We will add 'i586-elf-gcc' commands here to compile C sources

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o main.o main.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o scrn.o scrn.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o gdt.o gdt.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o idt.o idt.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o isrs.o isrs.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o irq.o irq.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o timer.o timer.c

	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o kb.o kb.c
	i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o kbintst.o kbintst.c

	#rem This links all your files. Remember that as you add *.o files, you need to

	#rem add them after start.o. If you don't add them at all, they won't be in your kernel!

	i586-elf-ld -T link.ld -o kernel.bin start.o main.o scrn.o gdt.o idt.o isrs.o irq.o timer.o kb.o kbintst.o

	echo Cleaning up object files...

	rm *.o

	@echo Done
anyway I will go and check that out and then hit the sack me thinks, this has been one bad day lol. Thanks for the help I hope that helps me what you suggested since something weirds going on here with this code, values are getting cocked up even though I put them in as a straight copy and paste job from people who have working code

may as well chuck the linker file in as well although its funny this is the only problem Ive had thus far?

Code: Select all

OUTPUT_FORMAT("elf32-i386")

ENTRY(start)

phys = 0x00100000;

SECTIONS

{

  .text phys : AT(phys) {

    code = .;

    *(.text)

    *(.rodata)

    . = ALIGN(4096);

  }

  .data : AT(phys + (data - code))

  {

    data = .;

    *(.data)

    . = ALIGN(4096);

  }

  .bss : AT(phys + (bss - code))

  {

    bss = .;

    *(.bss)

    . = ALIGN(4096);

  }

  end = .;

}
If it is on the off chance that my ealier comment was correct then what Im needing is some way to set a critical region where no interrupts happen, I think thats the right way a wait is supposed to act is it not? and asm voliatile is that to do assembly in c? Either way I will check it out, thanks a bunch for sifting through 10 tone of silly code to get this thing to work :)

edit........

Ok had a quick read about the volatile keyword , I will read that thoroughly tomorrow, Im not sure what fixed it in the end though because I also suspected the way makefiles are handleddidnt help, for instance for everyfile called filename.c one other file called filename.c~ gets created, now I deleted those files as well for the first time because irrespective of insuring I had saved and removed and copied an up to date kernel.bin old text out which looped and said "In while" was being displayed. even after the last 3 times re-saving and physically deleting files then doing a fresh copy , the same thing happened. So Im unsure of exactly what was the key fix moment because if the compiler was performing certain optimisations I can totally believe that would screw things up.

On the positive side its another thing I know to look out for, thanks allot for the patience. If anyone can see any potential problems with my link script or anything bad about my makefile please say :)

Re: timer_wait function may be solved TY :)

Posted: Sat Jun 28, 2008 9:34 pm
by ChazZeromus
Make sure that the timer handler is not directly called, judging by the regs parameter I doubt it's directly called. So make sure you restore all the used registers after your fault is done. :)