/*******************************************************************************
MPLAB Harmony Application Source File
Company:
Microchip Technology Inc.
File Name:
app.c
Summary:
This file contains the source code for the MPLAB Harmony application.
Description:
This file contains the source code for the MPLAB Harmony application. It
implements the logic of the application's state machine and it may call
API routines of other MPLAB Harmony modules in the system, such as drivers,
system services, and middleware. However, it does not call any of the
system interfaces (such as the "Initialize" and "Tasks" functions) of any of
the modules in the system or make any assumptions about when those functions
are called. That is the responsibility of the configuration-specific system
files.
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include "app.h"
#include "stdio.h"
#include "1lcd_lib_XC32.h"
char Buf[32]; //1液晶表示バッファー
char tempBuf[1];
char tempBuf2[512]; //ファイル読み書きバッファ
char tempBuf3[512]; //ファイル読み書きバッファ
int eof; //ファイルのEOF
int ix = 0;
int Num = 0; //USBメモリ 読み出し回数
char myData[] = "How are you? "; //ファイルへの書き込み文字列
int delay_Clock = 200000000; //200MHz
void delay_us(volatile unsigned int usec) //1μsec遅延
{
volatile int count;
count = (int)(delay_Clock/20000000)*usec;
do //実測 at 200MH (Clock=200000000)
{ //delay_us(1000):1000.4μsec delay_us(100):100.6μsec delay_us(10):10.5μsec delay_us(1):1.5μsec
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP");
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
count--;
}while(count != 0);
}
void delay_ms(volatile unsigned int msec) //1msec遅延
{
volatile unsigned int i; //実測:at200MH (Clock=200000000)//delay_ms(1): 1.0006msec delay_ms(100):100.04msec
for(i=0; i<msec; i++)
delay_us(1000);
}
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
This structure should be initialized by the APP_Initialize function.
Application strings and buffers are be defined outside this structure.
*/
APP_DATA appData;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary callback functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary local functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
/*******************************************************************************
Function:
void APP_Initialize ( void )
Remarks:
See prototype in app.h.
*/
void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_BUS_ENABLE;
appData.deviceIsConnected = false;
/* TODO: Initialize your application's state machine and other
* parameters.
*/
lcd_init(); // LCD初期化
lcd_cmd(0b00001100); // カーソル:OFF ブリンク:OFF
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"USB MSD ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," File RW start ! "); //
lcd_str(Buf); // 開始メッセージ1行目表示
delay_ms(2000);
}
//ホストレーヤイベントチェック
USB_HOST_EVENT_RESPONSE APP_USBHostEventHandler (USB_HOST_EVENT event, void * eventData, uintptr_t context)
{
switch (event)
{
case USB_HOST_EVENT_DEVICE_UNSUPPORTED:
break;
default:
break;
}
return(USB_HOST_EVENT_RESPONSE_NONE);
}
//ファイルシステムマウントチェック
void APP_SYSFSEventHandler(SYS_FS_EVENT event, void * eventData, uintptr_t context)
{
switch(event)
{
case SYS_FS_EVENT_MOUNT: //マウント検出の場合
appData.deviceIsConnected = true;
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"USB Memory ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," has connected "); //
lcd_str(Buf); // 開始メッセージ1行目表示
delay_ms(1500);
break;
case SYS_FS_EVENT_UNMOUNT:
appData.deviceIsConnected = false;
break;
default:
break;
}
}
/******************************************************************************
Function:
void APP_Tasks ( void )
Remarks:
See prototype in app.h.
*/
void APP_Tasks ( void )
{
switch(appData.state)
{
case APP_STATE_BUS_ENABLE:
/* Set the event handler and enable the bus */
SYS_FS_EventHandlerSet(APP_SYSFSEventHandler, (uintptr_t)NULL);//FSマウントチェック用コールバック関数APP_SYSFSEventHandler()を設定
USB_HOST_EventHandlerSet(APP_USBHostEventHandler, 0);//ホストレーヤイベントチェック用コールバック関数APP_USBHostEventHandler()を設定
USB_HOST_BusEnable(0); //USBホストバス有効化
appData.state = APP_STATE_WAIT_FOR_BUS_ENABLE_COMPLETE;
break;
case APP_STATE_WAIT_FOR_BUS_ENABLE_COMPLETE:
if(USB_HOST_BusIsEnabled(0))
{
appData.state = APP_STATE_WAIT_FOR_DEVICE_ATTACH;
}
break;
case APP_STATE_WAIT_FOR_DEVICE_ATTACH: //USBメモリのアタッチを待つ
/* Wait for device attach. The state machine will move
* to the next state when the attach event
* is received. */
if(appData.deviceIsConnected) //USBデバイスがアタッチされた場合
{
appData.state = APP_STATE_DEVICE_CONNECTED;
}
break;
case APP_STATE_DEVICE_CONNECTED:
/* Device was connected. We can try mounting the disk */
appData.state = APP_STATE_OPEN_FILE;
break;
case APP_STATE_OPEN_FILE:
/* Try opening the file for append */
appData.fileHandle = SYS_FS_FileOpen("/mnt/myDrive1/LongFileName.txt", (SYS_FS_FILE_OPEN_APPEND_PLUS));
if(appData.fileHandle == SYS_FS_HANDLE_INVALID)
{
/* Could not open the file. Error out*/
appData.state = APP_STATE_ERROR;
}
else
{
/* File opened successfully. Write to file */
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"FileOpened !! ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf,"LongFileName.txt"); //
lcd_str(Buf); // 開始メッセージ1行目表示
delay_ms(1500);
appData.state = APP_STATE_WRITE_TO_FILE;
}
break;
case APP_STATE_WRITE_TO_FILE:
/* Try writing to the file */
if (SYS_FS_FileWrite( appData.fileHandle, myData, sizeof(myData) ) == -1)
{
/* Write was not successful. Close the file
* and error out.*/
SYS_FS_FileClose(appData.fileHandle);
appData.state = APP_STATE_ERROR;
}
else
{
/* We are done writing. Close the file */
appData.state = APP_STATE_CLOSE_FILE;
}
break;
case APP_STATE_CLOSE_FILE:
/* Close the file */
SYS_FS_FileClose(appData.fileHandle);
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"Writed ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," and Closed "); //
lcd_str(Buf); // 開始メッセージ1行目表示
delay_ms(1500);
SYS_FS_HANDLE fileHandle2 = SYS_FS_FileOpen("/mnt/myDrive1/LongFileName.txt", (SYS_FS_FILE_OPEN_READ));
do
{
SYS_FS_FileRead(fileHandle2, tempBuf, 1); //1バイトづつ読出し
eof = SYS_FS_FileEOF(fileHandle2); //EOFチェック
if(eof != true)
{
tempBuf2[ix] = tempBuf[0];
ix++;
}
}while(eof != true); //ファイルエンドでない場合は、繰り返し読む
SYS_FS_FileClose(fileHandle2); //ファイルを閉じる
Num++;
lcd_cmd(0x80); //1目の先頭へ
sprintf(tempBuf3,"%s ",tempBuf2);
lcd_str(tempBuf3);
lcd_cmd(0xC0); //2目の先頭へ
sprintf(Buf,"Number=%d ",Num); //液晶に"Write OK !" を表示
lcd_str(Buf);
/* The test was successful. Lets idle. */
appData.state = APP_STATE_IDLE;
break;
case APP_STATE_IDLE:
/* The application comes here when the demo has completed
* successfully. Provide LED indication. Wait for device detach
* and if detached, wait for attach. */
if(appData.deviceIsConnected == false) //USBメモリを抜くと 挿入検出待ちモードに移行する。
{
appData.state = APP_STATE_WAIT_FOR_DEVICE_ATTACH;
}
break;
case APP_STATE_ERROR:
/* The application comes here when the demo
* has failed. Provide LED indication .*/
if(SYS_FS_Unmount("/mnt/myDrive") != 0)
{
/* The disk could not be un mounted. Try
* un mounting again untill success. */
appData.state = APP_STATE_ERROR;
}
else
{
/* UnMount was successful. Wait for device attach */
appData.state = APP_STATE_WAIT_FOR_DEVICE_ATTACH;
}
break;
default:
break;
}
}
/*******************************************************************************
End of File
*/