/// \file main.c VSOS3 Audio Device Driver with input/output (AUDIO.DL3)
/// \author Henrik Herranen, VLSI Solution Oy
// For further instructions on how to use the audio driver can be found
// in the VS1005 VSOS Programmer's Guide, available at
// http://www.vlsi.fi/en/support/software/vside.html

/* For free support for VSIDE, please visit www.vsdsp-forum.com */

// Starting point template for creating VSOS3 libraries and device drivers.
// This will create a <projectname>.DL3 file, which you can copy to 
// your VS1005 Developer Board's system disk's SYS subdirectory.

// DL3 files require VSOS3 kernel version 0.3x to run.


// If you add the libary name to S:CONFIG.TXT, and put the library 
// to S:SYS, it will be loaded and run during boot-up. 

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <string.h>
#include <stdlib.h>
#include <mutex.h>
#include <kernel.h>
#include <aucommon.h>
#include <stdlib.h>
#include "auxi2smv.h"

CHIPSPECIFIC(VS1005H)

u_int16 audioMutex;

FILE *OpenAudioFile(const char *mode);

void (*RomStart)(void) = (void (*)(void)) 0x8000;
s_int32 lockSampleRate = 0;

DLLENTRY(init)
ioresult init(char *parameters) {
  s_int32 sampleRate = 96000;
  int nParam = RunProgram("ParamSpl", parameters);
  char *p = parameters;
  int i;

  audioFile.op->Ioctl(&audioFile, IOCTL_RESTART, 0);

  for (i=0; i<nParam; i++) {
    if (!strcmp(p, "48000")) {
      lockSampleRate = 48000;
    } else if (!strcmp(p, "96000")) {
      lockSampleRate = 96000;
    }
    p += strlen(p)+1;
  }

  InitMutex(&audioMutex);

  audioFile.op->Ioctl(&audioFile, IOCTL_AUDIO_SET_ORATE, &sampleRate);

  return S_OK;
}


DLLENTRY(fini)
void fini(void) {
  // Add code here to force release of resources such as 
  // memory allocated with malloc, entry points, 
  // hardware locks or interrupt handler vectors.

  AudioClose();
}


DLLENTRY(OpenAudioFile)
FILE *OpenAudioFile(const char *mode) {
  if (AttemptMutex(&audioMutex)) {
    return NULL;
  }
  return &audioFile;
}


DLLENTRY(CloseAudioFile)
ioresult CloseAudioFile(FILE *audioFP) {
  ReleaseMutex(&audioMutex);
}
