/*******************************************************************************
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[17]; //液晶表示バッファー
int delay_Clock = 200000000; //200MHz
int SdMode = 0; //SDカードに対する操作モード
char Hellow[] = "Hellow World !!";
int N_Write; //書き込み回数戻り値
SYS_FS_HANDLE fileHandle; //ファイルハンドル
SYS_FS_HANDLE fileHandle2; //ファイルハンドル
char tempBuf[32];
char tempBuf2[32];
char tempBuf3[32];
char str_FileName[255]; //ロングファイル名
int eof;
int ix = 0;
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);
}
void SD_RW(void) //SDカード 挿入・ライトプロテクトチェック、及び 読み書き
{
switch(SdMode)
{
case 0: //カード挿入検出・ライトプロテクトチェック
if(SYS_FS_Mount("/dev/mmcblka1", "/mnt/myMountName", FAT, 0, NULL) != 0) //SYS_FS_RES_SUCCESS = 0
//SYS_FS_Mount("/dev/デバイス名", "/mnt/マウント名", ファイルシステムタイプ, Reserved, Reserved)
// /dev/と/mnt/は、名前の前に必ずつけること
//成功の場合: 戻り値 = 0
//mmcblka1:SDカード //変更不可
{ //失敗の場合 //カードがマウントされていても8回以上実行される
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"SD notMounted ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," "); //
lcd_str(Buf); //液晶表示
SdMode = 0;
}
else
{ //成功の場合
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"SD mounted ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," "); //
lcd_str(Buf); //液晶表示
delay_ms(2000);
/* //ライトプロテクトをチェックがある場合(例)
if(PORTBbits.RB9 != 0) //ライトプロテクトの場合 // writeProtectStatus = DRV_SDCARD_WriteProtectionIsEnabled(fileHandle);
{
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"WriteProtected ");
lcd_str(Buf);
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," "); //
lcd_str(Buf); //液晶表示
while(1);
}
else
{ //ライトプロテクトが解除されている場合
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"WriteReleased "); //液晶に"WriteReleased " を表示
lcd_str(Buf);
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," "); //
lcd_str(Buf); //液晶表示
delay_ms(3000);
}
*/
SdMode = 1;
}
break;
case 1: // ファイルを書き込みモードでオープン 但しファイルがなければ新規ファイル作成
fileHandle = SYS_FS_FileOpen("myTestData.txt", (SYS_FS_FILE_OPEN_WRITE));
if(fileHandle == SYS_FS_HANDLE_INVALID)
{ //ファイルオープンエラーの場合
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"FileOpen_w failed ");//
lcd_str(Buf); //液晶表示
while(1);
}
else
{
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"FileOpen_w OK !! "); //液晶に"W_ModeOpen OK" を表示
lcd_str(Buf); //液晶表示
delay_ms(2000);
SdMode = 2;
}
break;
case 2: //データの書き込み
N_Write = SYS_FS_FileWrite(fileHandle, Hellow, sizeof(Hellow));//ファイルにデータ書き込み
if(N_Write != -1) //正常な場合:書き込み回数が返ってくる
{
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"Write OK !! "); //液晶に"Write OK !" を表示
lcd_str(Buf);
delay_ms(2000);
}
else //異常な場合
{
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"Write NG !! "); //液晶に"Write NG !" を表示
lcd_str(Buf);
while(1);
}
SYS_FS_FileClose(fileHandle); //ファイルクローズ
delay_ms(1000);
SdMode = 3;
break;
case 3: //読出しファイルオープン
fileHandle2 = SYS_FS_FileOpen("myTestData.txt", (SYS_FS_FILE_OPEN_READ));
if(fileHandle2 == -1) //ファイルオープン 失敗の場合
//if(fileHandle2 == SYS_FS_HANDLE_INVALID) //ファイルオープン 失敗の場合
{
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"FileOpen_r failed");//
lcd_str(Buf); //液晶表示
while(1);
}
else SdMode = 4;
break;
case 4: //ファイル読出し・表示
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_FileNameGet(fileHandle2, str_FileName, 32 ); //ファイル名取得
SYS_FS_FileClose(fileHandle2); //ファイルを閉じる
lcd_cmd(0x80); //1目の先頭へ
sprintf(tempBuf3,"%s ",tempBuf2);
lcd_str(tempBuf3);
lcd_cmd(0xC0); //1目の先頭へ
sprintf(Buf,"%s",str_FileName); //液晶に"Write OK !" を表示
lcd_str(Buf);
SdMode = 5;
break;
case 5: //アイドル状態
break;
default:
break;
}
}
// *****************************************************************************
// *****************************************************************************
// 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_INIT;
/* TODO: Initialize your application's state machine and other
* parameters.
*/
lcd_init(); // LCD初期化
lcd_cmd(0b00001100); // カーソル:OFF ブリンク:OFF
lcd_cmd(0x80); //1目の先頭へ
sprintf(Buf,"SD read write ");//
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf," Start !! "); //
lcd_str(Buf); //液晶表示
delay_ms(2000);
}
/******************************************************************************
Function:
void APP_Tasks ( void )
Remarks:
See prototype in app.h.
*/
void APP_Tasks ( void )
{
/* Check the application's current state. */
switch ( appData.state )
{
/* Application's initial state. */
case APP_STATE_INIT:
{
bool appInitialized = true;
if (appInitialized)
{
appData.state = APP_STATE_SERVICE_TASKS;
}
break;
}
case APP_STATE_SERVICE_TASKS:
{
SD_RW(); ////SDカード 挿入・ライトプロテクトチェック、及び 読み書き
break;
}
/* TODO: implement your application state machine.*/
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
/*******************************************************************************
End of File
*/