📄 mpfs.c
字号:
*
* Input: None
*
* Output: Data byte from current address.
*
* Side Effects: None
*
* Overview: Reads a byte from current address.
*
* Note: Caller must call MPFSIsEOF() to check for end of
* file condition
********************************************************************/
BYTE MPFSGet(void)
{
BYTE t;
#if defined(MPFS_USE_EEPROM)
t = XEERead();
_currentHandle++;
#else
#if defined(__C30__)
{
DWORD_VAL i;
// The uppermost byte, ((DWORD_VAL*)&_currentHandle)->v[3]), is the byte lane to read from.
// 16 bit PICs have a 24 bit wide Flash program word. Bytes 0-2 are the actual address, but
// odd addresses aren't implemented.
i.Val = ReadProgramMemory(_currentHandle & 0x00FFFFFF);
t = i.v[((DWORD_VAL*)&_currentHandle)->v[3]++];
if(((DWORD_VAL*)&_currentHandle)->v[3] >= 3)
{
_currentHandle = (_currentHandle + 2) & 0x00FFFFFF;
}
}
#else
t = (BYTE)*_currentHandle;
_currentHandle++;
#endif
#endif
if(t == MPFS_DLE)
{
#if defined(MPFS_USE_EEPROM)
t = XEERead();
_currentHandle++;
#else
#if defined(__C30__)
{
DWORD_VAL i;
// The uppermost byte, ((DWORD_VAL*)&_currentHandle)->v[3]), is the byte lane to read from.
// 16 bit PICs have a 24 bit wide Flash program word. Bytes 0-2 are the actual address, but
// odd addresses aren't implemented.
i.Val = ReadProgramMemory(_currentHandle & 0x00FFFFFF);
t = i.v[((DWORD_VAL*)&_currentHandle)->v[3]++];
if(((DWORD_VAL*)&_currentHandle)->v[3] >= 3)
{
_currentHandle = (_currentHandle + 2) & 0x00FFFFFF;
}
}
#else
t = (BYTE)*_currentHandle;
_currentHandle++;
#endif
#endif
}
else if(t == MPFS_ETX)
{
_currentHandle = MPFS_INVALID;
}
return t;
}
#if defined(__C30__) && !defined(MPFS_USE_EEPROM)
/*********************************************************************
* Function: static DWORD ReadProgramMemory(DWORD address)
*
* PreCondition: None
*
* Input: Program memory address to read from. Should be
* an even number.
*
* Output: Program word at the specified address. For the
* PIC24, dsPIC, etc. which have a 24 bit program
* word size, the upper byte is 0x00.
*
* Side Effects: None
*
* Overview: Modifies and restores TBLPAG. Make sure that if
* using interrupts and the PSV feature of the CPU
* in an ISR that the TBLPAG register is preloaded
* with the correct value (rather than assuming
* TBLPAG is always pointing to the .const section.
*
* Note: None
********************************************************************/
static DWORD ReadProgramMemory(DWORD address)
{
asm("mov TBLPAG, W2 ; Save TBLPAG\r\n"
"mov W1, TBLPAG ; TBLPAG = HIGHWORD(address)\r\n"
"tblrdh [W0], W1 ; W1:W0 = *address\r\n"
"tblrdl [W0], W0\r\n"
"mov W2, TBLPAG ; Restore TBLPAG\r\n");
}
#endif
/*********************************************************************
* Function: MPFS MPFSGetEnd(void)
*
* PreCondition: MPFSOpen() != MPFS_INVALID &&
* MPFSGetBegin() = TRUE
*
* Input: None
*
* Output: Current mpfs handle.
*
* Side Effects: None
*
* Overview: Ends on-going read cycle.
* MPFS handle that is returned must be used
* for subsequent begin gets..
*
* Note: None
********************************************************************/
#if defined(MPFS_USE_EEPROM)
MPFS MPFSGetEnd(void)
{
XEEEndRead();
return _currentHandle;
}
#endif
/*********************************************************************
* Function: MPFS MPFSFormat(void)
*
* PreCondition: None
*
* Input: None
*
* Output: A valid MPFS handle that can be used for MPFSPut
*
* Side Effects: None
*
* Overview: Prepares MPFS image to get re-written
* Declares MPFS as in use.
*
* Note: MPFS will be unaccessible until MPFSClose is
* called.
********************************************************************/
MPFS MPFSFormat(void)
{
mpfsFlags.bits.bNotAvailable = TRUE;
return (MPFS)MPFS_RESERVE_BLOCK;
}
/*********************************************************************
* Function: BOOL MPFSPutBegin(MPFS handle)
*
* PreCondition: MPFSInit() and MPFSFormat() are already called.
*
* Input: handle - handle to where put to begin
*
* Output: TRUE if successful
* !TRUE otherwise
*
* Side Effects: None
*
* Overview: Prepares MPFS image to get re-written
*
* Note: MPFS will be unaccessible until MPFSClose is
* called.
********************************************************************/
#if defined(MPFS_USE_EEPROM)
BOOL MPFSPutBegin(MPFS handle)
{
//_currentCount = 0;
_currentHandle = handle;
_currentCount = (BYTE)handle;
_currentCount &= (MPFS_WRITE_PAGE_SIZE-1);
return (XEEBeginWrite(handle) == XEE_SUCCESS);
}
#endif
/*********************************************************************
* Function: BOOL MPFSPut(BYTE b)
*
* PreCondition: MPFSFormat() or MPFSCreate() must be called
* MPFSPutBegin() is already called.
*
* Input: b - data to write.
*
* Output: TRUE if successfull
* !TRUE if failed.
*
* Side Effects: Original MPFS handle is no longer valid.
* Updated MPFS handle must be obtained by calling
* MPFSPutEnd().
*
* Overview: None
*
* Note: Actual write may not get started until internal
* write page is full. To ensure that previously
* data gets written, caller must call MPFSPutEnd()
* after last call to MPFSPut().
********************************************************************/
BOOL MPFSPut(BYTE b)
{
#if defined(MPFS_USE_EEPROM)
if ( XEEWrite(b) )
return FALSE;
_currentCount++;
_currentHandle++;
if ( _currentCount >= MPFS_WRITE_PAGE_SIZE )
{
MPFSPutEnd();
XEEBeginWrite(_currentHandle);
}
#endif
return TRUE;
}
/*********************************************************************
* Function: MPFS MPFSPutEnd(void)
*
* PreCondition: MPFSPutBegin() is already called.
*
* Input: None
*
* Output: Up-to-date MPFS handle
*
* Side Effects: Original MPFS handle is no longer valid.
* Updated MPFS handle must be obtained by calling
* MPFSPutEnd().
*
* Overview: None
*
* Note: Actual write may not get started until internal
* write page is full. To ensure that previously
* data gets written, caller must call MPFSPutEnd()
* after last call to MPFSPut().
********************************************************************/
MPFS MPFSPutEnd(void)
{
#if defined(MPFS_USE_EEPROM)
_currentCount = 0;
XEEEndWrite();
while(XEEIsBusy());
#endif
return _currentHandle;
}
/*********************************************************************
* Function: MPFS MPFSSeek(MPFS offset)
*
* PreCondition: MPFSGetBegin() is already called.
*
* Input: offset - Offset from current pointer
*
* Output: New MPFS handle located to given offset
*
* Side Effects: None.
*
* Overview: None
*
* Note: None.
********************************************************************/
MPFS MPFSSeek(MPFS offset)
{
MPFS i;
MPFSGetBegin(_currentFile);
i = (MPFS)0;
while(i++ != offset)
MPFSGet();
MPFSGetEnd();
return _currentHandle;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -