08-F746-Demo_UsbMscHost (STM32F746)

Mit diesem Demo-Projekt wird FATFS auf USB und MMC des STM32F746-Discovery-Board getestet. Mehr Infos zu FATFS findet Ihr hier: 14-FATFS-Library (STM32F746).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//--------------------------------------------------------------
// File     : main.c
// Datum    : 05.09.2015
// Version  : 1.1
// Autor    : UB
// EMail    : mc-4u(@)t-online.de
// Web      : www.mikrocontroller-4u.de
// CPU      : STM32F746
// Board    : STM32F746-Discovery-Board
// IDE      : OpenSTM32
// GCC      : 4.9 2015q2
// Module   : CubeHAL
// Funktion : Hauptprogramm
//--------------------------------------------------------------
 
 
#include "stm32_ub_system.h"
#include "stm32_ub_lcd_480x272.h"
#include "stm32_ub_font.h"
#include "stm32_ub_usb_msc_host.h"
#include "stm32_ub_fatfs.h"
 
 
// Info : to use directory commands in fatfs like "f_chdir"
// you must set the define "_FS_RPATH" in "ffconf.h" to '1'
 
//--------------------------------------------------------------
USB_MSC_HOST_STATUS_t status;
void clearDisplay(void);
void usbDemo(void);
void mmcDemo(void);
FRESULT makeDirInUSBRoot(void);
FRESULT deleteDirInUSBRoot(void);
 
int main(void)
{
  // init vom System
  UB_System_Init();
 
  // init vom LCD
  UB_LCD_Init();
  UB_LCD_LayerInit_Fullscreen();
  UB_LCD_SetLayer_2();
 
  clearDisplay();
 
  // init vom USB als MSC-Host
  UB_USB_MSC_HOST_Init();
 
  // init vom FATFS-System
  UB_Fatfs_Init();
 
  while(1) {
    UB_USB_MSC_HOST_Do();
    if(UB_Fatfs_CheckMedia(USB_0)==FATFS_OK) usbDemo();
    if(UB_Fatfs_CheckMedia(MMC_1)==FATFS_OK) mmcDemo();
  }
}
 
void clearDisplay(void)
{
	UB_LCD_FillLayer(RGB_COL_GREEN);
	UB_Font_DrawString(10,20,"F746 USB-MSC/SD-MMC-Demo (05.09.2015 / UB)",&Arial_10x15,RGB_COL_RED,RGB_COL_BLACK);
#ifdef USE_USB_FS
	UB_Font_DrawString(10,60,"USB-FS at CN13",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
	UB_Font_DrawString(10,120,"please insert USB-Drive or MMC-Drive...",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
#elif USE_USB_HS
	UB_Font_DrawString(10,60,"USB-HS at CN12",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
	UB_Font_DrawString(10,120,"please insert USB-Drive  or MMC-Drive...",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
#else
	UB_Font_DrawString(10,60,"please define USE_USB_FS or USE_USB_HS",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
	while(1);
#endif
 
}
 
 
void usbDemo(void)
{
	FIL myFile;
	FATFS_t check;
 
	UB_LCD_FillLayer(RGB_COL_GREEN);
	UB_Font_DrawString(10,20,"USB-Demo",&Arial_10x15,RGB_COL_RED,RGB_COL_BLACK);
 
   	// Media mounten
   	if(UB_Fatfs_Mount(USB_0)==FATFS_OK) {
      UB_Font_DrawString(10,60,"mount ok",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
      // File zum schreiben im root neu anlegen
      if(UB_Fatfs_OpenFile(&myFile, "0:/USB_File.txt", F_WR_CLEAR)==FATFS_OK) {
        UB_Font_DrawString(10,80,"file open : USB_File.txt",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
    	// ein paar Textzeilen in das File schreiben
        UB_Fatfs_WriteString(&myFile,"Test der WriteString-Funktion");
        UB_Fatfs_WriteString(&myFile,"hier Zeile zwei");
        check=UB_Fatfs_WriteString(&myFile,"ENDE");
        if(check==FATFS_OK) {
        	UB_Font_DrawString(10,100,"write in file : ok",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
        }
        else {
        	UB_Font_DrawString(10,100,"write in file : error",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
        }
        // File schliessen
        UB_Fatfs_CloseFile(&myFile);
        UB_Font_DrawString(10,120,"file closed",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
      }
      // Media unmounten
      UB_Fatfs_UnMount(USB_0);
      UB_Font_DrawString(10,140,"device unmounted",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
    }
   	else UB_Font_DrawString(10,60,"mount error",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
 
   	UB_Font_DrawString(10,200,"please unplug USB-Device...",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
 
	do {
		status=UB_USB_MSC_HOST_Do();
	}while(status==USB_MSC_DEV_CONNECTED);
 
	clearDisplay();
}
 
void mmcDemo(void)
{
	FIL myFile;
	FATFS_t check;
 
	UB_LCD_FillLayer(RGB_COL_GREEN);
	UB_Font_DrawString(10,20,"MMC-Demo",&Arial_10x15,RGB_COL_RED,RGB_COL_BLACK);
 
   	// Media mounten
   	if(UB_Fatfs_Mount(MMC_1)==FATFS_OK) {
      UB_Font_DrawString(10,60,"mount ok",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
      // File zum schreiben im root neu anlegen
      if(UB_Fatfs_OpenFile(&myFile, "1:/MMC_File.txt", F_WR_CLEAR)==FATFS_OK) {
        UB_Font_DrawString(10,80,"file open : MMC_File.txt",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
    	// ein paar Textzeilen in das File schreiben
        UB_Fatfs_WriteString(&myFile,"Test der WriteString-Funktion");
        UB_Fatfs_WriteString(&myFile,"hier Zeile zwei");
        check=UB_Fatfs_WriteString(&myFile,"ENDE");
        if(check==FATFS_OK) {
        	UB_Font_DrawString(10,100,"write in file : ok",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
        }
        else {
        	UB_Font_DrawString(10,100,"write in file : error",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
        }
        // File schliessen
        UB_Fatfs_CloseFile(&myFile);
        UB_Font_DrawString(10,120,"file closed",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
      }
      // Media unmounten
      UB_Fatfs_UnMount(MMC_1);
      UB_Font_DrawString(10,140,"device unmounted",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
    }
   	else UB_Font_DrawString(10,60,"mount error",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
 
   	UB_Font_DrawString(10,200,"please unplug MMC-Device...",&Arial_10x15,RGB_COL_BLACK,RGB_COL_GREEN);
 
	do {
		UB_USB_MSC_HOST_Do();
	}while(UB_Fatfs_CheckMedia(MMC_1)==FATFS_OK);
 
	clearDisplay();
}
 
FRESULT makeDirInUSBRoot(void)
{
	FRESULT res=FR_DISK_ERR;
 
	if(UB_Fatfs_CheckMedia(USB_0)==FATFS_OK) {
		if(UB_Fatfs_Mount(USB_0)==FATFS_OK) {
			// make directory in root
			res=f_mkdir("dir1");
 
			UB_Fatfs_UnMount(USB_0);
		}
	}
 
	return res;
}
 
FRESULT deleteDirInUSBRoot(void)
{
	FRESULT res=FR_DISK_ERR;
 
	if(UB_Fatfs_CheckMedia(USB_0)==FATFS_OK) {
		if(UB_Fatfs_Mount(USB_0)==FATFS_OK) {
			// delete directory in root
			res=f_unlink("dir1");
 
			UB_Fatfs_UnMount(USB_0);
		}
	}
 
	return res;
}

Hier der komplette OpenSTM32-Projektordner zum Download :

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.