Program exit, timers, VSOS files.

Discussion about writing software for VS1005 and the VSOS Operating System. Also posts about VS1005-related hardware design and device drivers should be posted here.
Post Reply
jedm@vpitech.com
Senior User
Posts: 50
Joined: Wed 2016-11-02 22:50

Program exit, timers, VSOS files.

Post by jedm@vpitech.com » Wed 2017-01-18 17:14

I. My application requires many different programs for different activities which requires swapping programs in and out using RunLibraryFunction. How much cleanup should be done on program exit and how much is handled by VSOS?

1) GPIO. I've registered several pins as inputs in one program. Do these stay as inputs when the program exits or does VSOS restore them?
2) SPI. I'm using the second SPI channel to talk to an accelerometer. Are these restored to their original state when a program exits?
3) Interrupts. Is the interrupt vector restored to VSOS state when a program exits or should I disable ones I've enabled?
4) Tasks. Should all the user tasks (CreateTaskAndStack) be freed before exit?
5) malloc and friends. Should all this be freed?
6) Peripherals. I'm using the VHMON of the 10 bit A/D. Does this get reset when a program exits?
7) Files. Are file handles automatically closed on exit?

II. Which timers are used by VSOS? (I see Timer 2 mentioned).

III. There are comments in the VSOS code about 10 files being open and some difference between simple files and the others. I'm assuming the 6 files are for SD/SPI storage and the 4 others are for serial ports, audio, etc. Is this the case?


Thank you in advance.
Jed

Hannu
Senior User
Posts: 79
Joined: Mon 2016-05-30 11:54

Re: Program exit, timers, VSOS files.

Post by Hannu » Thu 2017-01-19 9:38

jedm@vpitech.com wrote:I. My application requires many different programs for different activities which requires swapping programs in and out using RunLibraryFunction. How much cleanup should be done on program exit and how much is handled by VSOS?
1) GPIO. I've registered several pins as inputs in one program. Do these stay as inputs when the program exits or does VSOS restore them?
They stay as you left them. Leaving ports as inputs might not be the best idea. If the other device sets its pins also input, both ends are floating and power consumption rises. Depends on your circuit and application.
2) SPI. I'm using the second SPI channel to talk to an accelerometer. Are these restored to their original state when a program exits?
Nope.
3) Interrupts. Is the interrupt vector restored to VSOS state when a program exits or should I disable ones I've enabled?
Nope. You need to reset them.
4) Tasks. Should all the user tasks (CreateTaskAndStack) be freed before exit?
MyTaskLoop() is the worker task and fini() is from the parent library.

Code: Select all

void fini(){
  if(myTaskstate == MY_TASK_DEAD)goto finally;
  while(myTaskState != MY_TASK_DEAD){
    myTaskState = MY_TASK_DIE;
    Delay(1);
  }
  FreeTaskAndStack(myTaskTAS);
}
void MyTaskLoop(){
  while(myTaskState != MY_TASK_DIE){
    printf("All work no play makes Jack a dull boy.\n");
    Delay(10);
  }
  myTaskState = MY_TASK_DEAD;
}
5) malloc and friends. Should all this be freed?
Yes. If application malloc(), it needs to free() the reserved memory too.
6) Peripherals. I'm using the VHMON of the 10 bit A/D. Does this get reset when a program exits?
nope
7) Files. Are file handles automatically closed on exit?
They are left open and consumes resources. Only stdin, stdout, stderr, stdaudioin, stdtaudioout shouldn't be closed
II. Which timers are used by VSOS? (I see Timer 2 mentioned).
Tick counter uses one and I can't tell another right away. Inttrap uses another if I remember correctly but it's a library.
III. There are comments in the VSOS code about 10 files being open and some difference between simple files and the others. I'm assuming the 6 files are for SD/SPI storage and the 4 others are for serial ports, audio, etc. Is this the case?
In my vsos.h there is #define __FOPEN_MAX_FILES 6 So it is possible to fopen 6 files. Devices are A to Z and aren't fopen()ed when accessed.

And just for the record, vsos loads application, allocates memory for static stuff in X,Y and I. When exiting those are freed. Everything else is left on the responsibility of user.

One quite nice pattern is starting with DL3 template. I wrote small and stupid (Instead of file think it as some peripheral) pattern. in init() reserve resources and free in fini(). When starting main() just reset resources. Another common pattern though out VSOS is in error handling. If something fails, goto finally is called and the escalation of error is prevented and locally needed resources are freed. This leads to code which is always in known state and can be executed many times.

Code: Select all

File *myFile;
void init(){
  //Reserve resources and init them
  myFile = fopen("s:needed.dat", "rb");
}
int main(char * p){
  if(myFile == NULL){
    printf("E: There is a problem.\n");
    goto finally;
  }
  rewind(myFile);
finally:  
  return 0;
}
void fini(){
  //Free the resources and set them as they were
  fclose(myFile);
}

User avatar
Panu
VLSI Staff. Currently on holiday.
Posts: 2727
Joined: Tue 2010-06-22 13:43

Re: Program exit, timers, VSOS files.

Post by Panu » Thu 2017-01-19 13:24

Hi!

What Hannu writes is largely correct. Here's my own quick answers:

1) GPIO states are not restored by VSOS. If you use GPIO's, you should lock these using ObtainHwLocksBIP() and free them in your fini().

2) SPI or any other peripheral states are not restored, but if you use HwSpi driver, it initializes them anyway as needed, so you should not need to restore them. Unless, of course, I'm proven wrong by things not working :D

3) Since version 3.33 VSOS restores all interrupt handlers that point to your code to ROM defaults on program exit and disables them. You don't need to do anything to interrupt handlers or vectors or enables in your fini() since that would be a waste of program code space.

4) You must definitely free all tasks that you create before you exit!

5) All malloc()ated buffers must be freed by you!

6) Peripheral states are not restored by the loader. Lock and unlock them using ObtainHwLocksBIP() and ReleaseHwLocksBIP() so you don't need to worry about them too much. In fini() do simple things to be nice before releasing the lock, e.g. if you start some peripheral that needs power, switch it off before you unlock. Don't worry about things such as modes and clockspeeds; set them after locking the perip for yourself and rely on others doing the same. Again; until I am proven wrong by something not working. :)

7) File handles are not automatically closed at app exit by VSOS, although I'm thinking on how to do that efficiently in some future VSOS version.

II) Timer 2 is the system interrupt, which handles tasks, updates timecount and calls dispatcher. Timer 1 is used to track that address 0 in X memory remains zero and is not overwritten by any program. This catches a large number of programming errors and makes the system safer by making sure that method calls for uninitialized objects always land to ZeroPtrCall trap, but using Timer 1 for your own purposes doesn't break anything. Timer 0 is not used by the kernel.

III) There are 6 simple files and 6 "full" files that can be opened. Simple file descriptor is only 7 words long and is powerful enough to use character and stream devices. A "full" file is needed by disk files. This definition is a little simplified, for the full definition, you should see vo_fopen() source code, it's actually quite readable. fopen() returns a SIMPLE_FILE* if it can and a FILE* otherwise. Please see struct simple_file_descriptor in vsos.h, it's bit compatible with struct file_descriptor for flags, Identify, file operations, file position, ungetc and the device pointer. A simple file has a corresponding flag set in the flags.

Hope this is useful!
-Panu
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

jedm@vpitech.com
Senior User
Posts: 50
Joined: Wed 2016-11-02 22:50

Re: Program exit, timers, VSOS files.

Post by jedm@vpitech.com » Thu 2017-01-19 16:54

Excellent! Thank you.

1) Am I assume that fopen("d:", "s"), does not consume one of the full file handles? Does it consume a simple one?

2) What does RunLibraryFunction("s:myapp.ap3", ...) use? I managed to run this with 6 full files open which seems counter intuitive. I see in VSOS/apploader.c that it uses fopen.

I vacillate on whether files should be closed automatically by the OS. If a program crashes, it's great because you can get at what was written. But in the VSOS case, the control block and bits of VSOS could well be destroyed (I've managed to do this more than once) and closing automatically would cause mayhem. On an 8051 OS I wrote I closed automatically, but the code was in ROM and running an interpreter. Even then a malicious user could trash the block pointers.

User avatar
Henrik
VLSI Staff
Posts: 1150
Joined: Tue 2010-06-22 14:10

Re: Program exit, timers, VSOS files.

Post by Henrik » Fri 2017-01-20 13:03

jedm,

I don't have any new information to offer, but just wanted to say that the points you raise in the last section are good ones.

Kind regards,
- Henrik
Good signatures never die. They just fade away.

Post Reply