This walkthrough adds three RAM-backed files to the generated Integration program. Use the three-file setup first. The code works with polling or the generated USB interrupt handler. Keep the generated startup and interrupt code; replace only the application example parts described below.
Declare these variables once. The generated example already has bMscByte and bMscRequest, so keep only one copy of each declaration. The library already declares USB_MSC_Data and the request fields.
Dim bFileDemo[64] As Byte
Dim bFileStatus[128] As Byte
Dim bFileSettings[64] As Byte
Dim bMscByte As Byte
Dim bMscValue As Byte
Dim bMscRequest As Byte
Dim bMscFile As Byte
Dim bMscCount As Byte
Dim bMscStatus As Byte
Dim wMscPosition As Word
Dim wMscFileSize As Word
Dim dwMscOffset As Dword
Dim dwMscRoom As Dword
Dim sMscText As String * 48
Put this code after the generated startup's Clear, before USB attachment and before enabling interrupts. Unused text-file bytes are spaces. The binary settings file is filled with zeroes; byte 0 is format version 1, and byte 1 is an example brightness value of 50.
For bMscByte = 0 To 63
bFileDemo[bMscByte] = 32
bFileSettings[bMscByte] = 0
Next
For bMscByte = 0 To 127
bFileStatus[bMscByte] = 32
Next
sMscText = "Hello from the PIC!" + 13 + 10
For bMscByte = 0 To Len(sMscText) - 1
bMscValue = sMscText[bMscByte]
bFileDemo[bMscByte] = bMscValue
Next
sMscText = "name,value" + 13 + 10 + "temperature,23.5" + 13 + 10
For bMscByte = 0 To Len(sMscText) - 1
bMscValue = sMscText[bMscByte]
bFileStatus[bMscByte] = bMscValue
Next
bFileSettings[0] = 1
bFileSettings[1] = 50
Replace the generated example's main loop with this code. It selects the file for each request, checks the requested range, and keeps STATUS.CSV read-only.
Do
USB_MSCService()
' Snapshot the operation, especially its read/write direction.
bMscRequest = USB_MSC_Request
bMscFile = USB_MSC_File
dwMscOffset = USB_MSC_Offset
bMscCount = USB_MSC_Length
Select bMscRequest
Case USB_MSC_READ, USB_MSC_WRITE
bMscStatus = USB_OK
wMscFileSize = 0
Select bMscFile
Case 0
wMscFileSize = 64
Case 1
wMscFileSize = 128
If bMscRequest = USB_MSC_WRITE Then
bMscStatus = USB_NOT_SUPPORTED
EndIf
Case 2
wMscFileSize = 64
Case Else
bMscStatus = USB_NOT_SUPPORTED
EndSelect
If bMscStatus = USB_OK Then
If bMscCount = 0 Then
bMscStatus = USB_BAD_LENGTH
EndIf
If bMscCount > 64 Then
bMscStatus = USB_BAD_LENGTH
EndIf
EndIf
' Check the Dword offset before narrowing it to an array index.
If bMscStatus = USB_OK Then
If dwMscOffset >= wMscFileSize Then
bMscStatus = USB_BAD_LENGTH
Else
dwMscRoom = wMscFileSize
dwMscRoom = dwMscRoom - dwMscOffset
If bMscCount > dwMscRoom Then
bMscStatus = USB_BAD_LENGTH
EndIf
EndIf
EndIf
If bMscStatus = USB_OK Then
wMscPosition = dwMscOffset
For bMscByte = 0 To bMscCount - 1
If bMscRequest = USB_MSC_READ Then
Select bMscFile
Case 0
bMscValue = bFileDemo[wMscPosition]
Case 1
bMscValue = bFileStatus[wMscPosition]
Case 2
bMscValue = bFileSettings[wMscPosition]
EndSelect
USB_MSC_Data[bMscByte] = bMscValue
Else
bMscValue = USB_MSC_Data[bMscByte]
Select bMscFile
Case 0
bFileDemo[wMscPosition] = bMscValue
Case 2
bFileSettings[wMscPosition] = bMscValue
EndSelect
EndIf
Inc wMscPosition
Next
EndIf
USB_MSC_Complete(bMscStatus)
Case USB_MSC_FLUSH
' These files promise RAM storage; no delayed writes remain.
USB_MSC_Complete(USB_OK)
EndSelect
' Add short foreground tasks here.
Loop
The code copies the request direction before handling its bytes. This matters in interrupt mode: a USB reset can clear the live request field. Rechecking that changing field inside the byte loop could send a read through the write branch. The completion call checks for cancellation, but cannot undo file bytes already changed.
These example files live in RAM. The three file arrays need 256 bytes, plus the text string and working variables. Count them in your RAM budget. A reset or power loss restores their starting contents. To keep changes after power is removed, replace the relevant array operations with code for non-volatile storage.
Created with the Personal Edition of HelpNDoc: Effortlessly Convert Your Word Doc to an eBook: A Step-by-Step Guide