/*******************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only
 * intended for use with Renesas products. No other uses are authorized. This
 * software is owned by Renesas Electronics Corporation and is protected under
 * all applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software
 * and to discontinue the availability of this software. By using this software,
 * you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer*
 * Copyright (C) 2013 Renesas Electronics Corporation. All rights reserved.
 *******************************************************************************/
/**************************************************************************//**
* @file         main.c
* $Rev: 879 $
* $Date:: 2013-07-03 09:29:09 +0900#$
* @brief        BSP main task
******************************************************************************/

/******************************************************************************
Includes   <System Includes> , "Project Includes"
******************************************************************************/
#include <stdio.h>
#include "cmsis_os.h"

#include "Serial.h"

#include "bsp_util.h"

#include "usb_hdrv_rza1.h"

#include "fat_fsi.h"

#include "usb_audio.h"
/******************************************************************************
Macro definitions
******************************************************************************/
#define USB_AUDIO_CONNECT    0x01
#define USB_MSCD_CONNECT     0x02

/******************************************************************************
Imported global variables and functions (from other files)
******************************************************************************/
extern void Main_Thread(int32_t DriveNo, uint32_t DevAddr);
extern int32_t MAIN_Create_Main_MailQ(void);
extern void Terminate_dec_thread (void);
bool_t Get_USB_Connect_Info(void);
extern int32_t OSTime_Init(void);
/******************************************************************************
Private global variables and functions
******************************************************************************/
uint8_t USB_Connect;

static int32_t  get_drivenum(void);
static uint32_t get_devadr(void);

/******************************************************************************
* Function Name: main
* @brief         CMSIS RTOS Task : Main task
*
*                Description:<br>
*                Application initialize task
* @param         none
* @retval        none
******************************************************************************/
void main(void)
{
    int32_t drive_no;
    uint32_t dev_addr;

    /***********************************************************************/
    /* Initialise Debug UART */
    /***********************************************************************/

    SER_Init();

    USB_Connect = false;

    /***********************************************************************/
    /* Print Board Information */
    /***********************************************************************/

    /* ==== Initialize periheral clock ==== */
    STB_Init();

    /* ==== PORT setting ==== */
    PORT_Init();

    /* ==== Initialize Terminal ==== */
    /* SCIF 3ch */
    /* P1ƒÓ=66.67MHz CKS=0 SCBRR=17 Bit rate error=0.46% => Baud rate=115200bps */
    IoInitScif3();

    OSTime_Init();

    /* start usb host channel 0 */
    usb0_init(USB_USER_TRANS_MODE);

    while (1)
    {
        drive_no = -1;

        /* ==== USB attach proccess ==== */
        printf("Please insert USB Device.\n");
        /* wait usb attach */
        while (Get_USB_Connect_Info() == false)
        {
            osDelay(1);
        }
        /* Create thread and mail queue */
        DEC_CreateThread();
        AOUT_CreateThread();
        MAIN_Create_Main_MailQ();

        /* Get memory drive from terminal */
        while (drive_no == -1)
        {
            drive_no = get_drivenum();
        }

        /* Get audio device address */
        dev_addr = get_devadr();
        printf("Attach USB Device.\n");

        /* Start USB audio out */
        if (AOUT_Start(dev_addr) == 0)
        {
            /* start application */
            Main_Thread(drive_no, dev_addr);
        }
        else
        {
            printf("Prease remove USB Device.\n");
            while (Get_USB_Connect_Info() == true)
            {
                osDelay(1);
            }
        }
        /* ==== USB detach proccess ==== */
        /* Terminate thread */
        AOUT_TerminateThread();
        DEC_TerminateThread ();
    }
}

/******************************************************************************
* Function Name: Set_USB_Audio_Connect_Info
* Description  : none
* Arguments    : none
* Return Value : none
******************************************************************************/
void Set_USB_Audio_Connect_Info(bool_t connect)
{
    if (connect == true)
    {
        USB_Connect |= USB_AUDIO_CONNECT;
    }
    else
    {
        USB_Connect &= ~USB_AUDIO_CONNECT;
    }
}

/******************************************************************************
* Function Name: Set_USB_MSCD_Connect_Info
* Description  : none
* Arguments    : none
* Return Value : none
******************************************************************************/
void Set_USB_MSCD_Connect_Info(bool_t connect)
{
    if (connect == true)
    {
        USB_Connect |= USB_MSCD_CONNECT;
    }
    else
    {
        USB_Connect &= ~USB_MSCD_CONNECT;
    }
}

/******************************************************************************
* Function Name: Get_USB_Connect_Info
* Description  : none
* Arguments    : none
* Return Value : none
******************************************************************************/
bool_t Get_USB_Connect_Info(void)
{
    if (USB_Connect == (USB_AUDIO_CONNECT|USB_MSCD_CONNECT))
    {
        return true;
    }
    else
    {
        return false;
    }
}

/******************************************************************************
* Function Name: GetDriveNum
* Description  : none
* Arguments    : none
* Return Value : none
******************************************************************************/
static int32_t get_drivenum(void)
{
    int32_t  temp_drive = -1;

    printf("Please select Drive number of USB memory:");
    scanf("%d", &temp_drive);
    printf("\n");

    if ( temp_drive != FAT_DRV_USB0_0 )
    {
        printf("ERROR : Drive Number has to be 0\n");
        temp_drive = -1;
    }
    else
    {
        printf("Attach USB drive %d\n", temp_drive);
    }

    return temp_drive;
}

/******************************************************************************
* Function Name: GetDevAdr
* Description  : none
* Arguments    : none
* Return Value : none
******************************************************************************/
static uint32_t get_devadr(void)
{
    uint32_t  temp_devadr;

    printf("\n");
    printf("Please select USB Audio device address:");
    scanf("%d", &temp_devadr);
    printf("\n");

    return temp_devadr;
}

/* End of File */
