voicesetadconverter.c 2.29 KB
/*---------------------------------------------------------------------
 	Copyright (C) 1997, Nintendo.
 	
 	File	        voicesetadconverter.c
 	Coded    by	Koji Mitsunari.	Apr 22, 1998.
 	Modified by	Koji Mitsunari.	Jun 22, 1998.
 	Comments	Voice Recognition System
   
 	$Id: voicesetadconverter.c,v 1.1.1.1 2002/05/02 03:28:44 blythe Exp $
   ---------------------------------------------------------------------*/

#include "osint.h"
#include "controller.h"
#include "siint.h"

s32
__osVoiceSetADConverter(OSMesgQueue *mq, int channel, u8 data)
{
  s32	ret = 0;
  int	i;
  u8	*ptr;
  u8	status;
  int	retry = 2;

  /* Block to get resource token */
  __osSiGetAccess();

  do { 	/* Retry if ADRS/DATA CRC error */
    ptr = (u8 *)(&__osPfsPifRam);

    /* trigger pifmacro */
    if (__osContLastCmd != CONT_VOICE_SWRITE
	|| __osPfsLastChannel != channel) {
      __osContLastCmd = CONT_VOICE_SWRITE;
      __osPfsLastChannel = channel;

      /* Set up request command format for all channels */
      for (i = 0; i < channel; i++) *ptr++ = 0;

      /* set format flag */
      __osPfsPifRam.pifstatus = CONT_FORMAT;

      /* Setup ram read format */
      ((__OSVoiceSWriteFormat *)ptr)->txsize = 3;
      ((__OSVoiceSWriteFormat *)ptr)->rxsize = 1;
      ((__OSVoiceSWriteFormat *)ptr)->cmd = CONT_VOICE_SWRITE;
      ((__OSVoiceSWriteFormat *)ptr)->datacrc = 0;
      
      /* setup ram read format */
      ptr[sizeof(__OSVoiceSWriteFormat)] = FORMAT_END;
    } else {
      ptr += channel;
    }
    ((__OSVoiceSWriteFormat *)ptr)->data = data;
    ((__OSVoiceSWriteFormat *)ptr)->scrc = __osContAddressCrc(data<<3);

    ret = __osSiRawStartDma(OS_WRITE, &__osPfsPifRam);
    (void)osRecvMesg(mq, (OSMesg *)NULL, OS_MESG_BLOCK);

    ret = __osSiRawStartDma(OS_READ, &__osPfsPifRam);
    (void)osRecvMesg(mq, (OSMesg *)NULL, OS_MESG_BLOCK);

    ret = (s32)((((__OSVoiceSWriteFormat *)ptr)->rxsize & CON_ERR_MASK) >> 4);
    if (ret == 0) {
      /* check data CRC */
      if ( ((((__OSVoiceSWriteFormat *)ptr)->datacrc)&1) != 0) {
	      
	/* reset status */
	if ((ret = __osVoiceGetStatus(mq, channel, &status)) != 0){
	  break;
	} else {
	  ret = CONT_ERR_CONTRFAIL;
	}
      }
    } else {
      ret = CONT_ERR_NO_CONTROLLER;
    }
  } while ((ret == CONT_ERR_CONTRFAIL) && (retry-- >= 0));

  __osSiRelAccess();
  return(ret);
}