chassis_er1_reply ChassisEr1::SendCmd(unsigned char paddress, unsigned char pcode) { chassis_er1_cmd cmd; cmd = SetCmd(paddress, pcode); return _SendCmd(&cmd); }
chassis_er1_reply ChassisEr1::SendCmd_with_arg(unsigned char paddress, unsigned char pcode, int wData1) { chassis_er1_cmd cmd; cmd=SetCmd_with_arg(paddress, pcode, wData1); return _SendCmd(&cmd); }
/***************************************************************************** Function: void SPIFlashEraseSector(DWORD dwAddr) Summary: Erases a sector. Description: This function erases a sector in the Flash part. It is called internally by the SPIFlashWrite functions whenever a write is attempted on the first byte in a sector. Precondition: SPIFlashInit has been called. Parameters: dwAddr - The address of the sector to be erased. Returns: None Remarks: See Remarks in SPIFlashBeginWrite for important information about Flash memory parts. ***************************************************************************/ void SPIFlashEraseSector(DWORD dwAddr) { volatile BYTE Dummy; BYTE vSPIONSave; #if defined(__18CXX) BYTE SPICON1Save; #elif defined(__C30__) WORD SPICON1Save; #else DWORD SPICON1Save; #endif // Save SPI state (clock speed) SPICON1Save = SPIFLASH_SPICON1; vSPIONSave = SPI_ON_BIT; // Configure SPI SPI_ON_BIT = 0; SPIFLASH_SPICON1 = PROPER_SPICON1; SPI_ON_BIT = 1; // Enable writing _SendCmd(WREN); // Activate the chip select SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue ERASE command with address SPIFLASH_SSPBUF = ERASE_4K; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwAddr)[2]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwAddr)[1]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwAddr)[0]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; // Deactivate chip select to perform the erase SPIFLASH_CS_IO = 1; // Wait for erase to complete _WaitWhileBusy(); // Restore SPI state SPI_ON_BIT = 0; SPIFLASH_SPICON1 = SPICON1Save; SPI_ON_BIT = vSPIONSave; }
/***************************************************************************** Function: void SPIFlashWriteArray(BYTE* vData, WORD wLen) Summary: Writes an array of bytes to the SPI Flash part. Description: This function writes an array of bytes to the SPI Flash part. When the address pointer crosses a sector boundary (and has more data to write), the next sector will automatically be erased. If the current address pointer indicates an address that is not a sector boundary and is not already erased, the chip will silently ignore the write command until the next sector boundary is crossed. Precondition: SPIFlashInit and SPIFlashBeginWrite have been called, and the current address is either the front of a sector or has already been erased. Parameters: vData - The array to write to the next memory location wLen - The length of the data to be written Returns: None Remarks: See Remarks in SPIFlashBeginWrite for important information about Flash memory parts. ***************************************************************************/ void SPIFlashWriteArray(BYTE* vData, WORD wLen) { volatile BYTE Dummy; BYTE vSPIONSave; #if defined(__18CXX) BYTE SPICON1Save; #elif defined(__C30__) WORD SPICON1Save; #else DWORD SPICON1Save; #endif BOOL isStarted; BYTE vOpcode; BYTE i; // Do nothing if no data to process if(wLen == 0u) return; // Save SPI state (clock speed) SPICON1Save = SPIFLASH_SPICON1; vSPIONSave = SPI_ON_BIT; // Configure SPI SPI_ON_BIT = 0; SPIFLASH_SPICON1 = PROPER_SPICON1; SPI_ON_BIT = 1; // If starting at an odd address, write a single byte if((dwWriteAddr & 0x01) && wLen) { SPIFlashWrite(*vData); vData++; wLen--; } // Assume we are using AAI Word program mode unless changed later vOpcode = WRITE_WORD_STREAM; isStarted = FALSE; // Loop over all remaining WORDs while(wLen > 1) { // Don't do anything until chip is ready _WaitWhileBusy(); // If address is a sector boundary if((dwWriteAddr & SPI_FLASH_SECTOR_MASK) == 0) SPIFlashEraseSector(dwWriteAddr); // If not yet started, initiate AAI mode if(!isStarted) { // Enable writing _SendCmd(WREN); // Select appropriate programming opcode. The WRITE_WORD_STREAM // mode is the default if neither of these flags are set. if(deviceCaps.bits.bWriteByteStream) vOpcode = WRITE_BYTE_STREAM; else if(deviceCaps.bits.bPageProgram) { // Note: Writing one byte at a time is extremely slow (ex: ~667 // bytes/second write speed on SST SST25VF064C). You can // improve this by over a couple of orders of magnitude by // writing a function to write full pages of up to 256 bytes at // a time. This is implemented this way only because I don't // have an SST25VF064C handy to test with right now. -HS while(wLen--) SPIFlashWrite(*vData++); return; } // Activate the chip select SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue WRITE_xxx_STREAM command with address SPIFLASH_SSPBUF = vOpcode; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[2]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[1]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[0]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; isStarted = TRUE; } // Otherwise, just write the AAI command again else { // Assert the chip select pin SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue the WRITE_STREAM command for continuation SPIFLASH_SSPBUF = vOpcode; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; } // Write a byte or two for(i = 0; i <= deviceCaps.bits.bWriteWordStream; i++) { SPIFLASH_SSPBUF = *vData++; dwWriteAddr++; wLen--; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; } // Release the chip select to begin the write SPIFLASH_CS_IO = 1; // If a boundary was reached, end the write if((dwWriteAddr & SPI_FLASH_SECTOR_MASK) == 0) { _WaitWhileBusy(); _SendCmd(WRDI); isStarted = FALSE; } } // Wait for write to complete, then exit AAI mode _WaitWhileBusy(); _SendCmd(WRDI); // If a byte remains, write the odd address if(wLen) SPIFlashWrite(*vData); // Restore SPI state SPI_ON_BIT = 0; SPIFLASH_SPICON1 = SPICON1Save; SPI_ON_BIT = vSPIONSave; }
/***************************************************************************** Function: void SPIFlashWrite(BYTE vData) Summary: Writes a byte to the SPI Flash part. Description: This function writes a byte to the SPI Flash part. If the current address pointer indicates the beginning of a 4kB sector, the entire sector will first be erased to allow writes to proceed. If the current address pointer indicates elsewhere, it will be assumed that the sector has already been erased. If this is not true, the chip will silently ignore the write command. Precondition: SPIFlashInit and SPIFlashBeginWrite have been called, and the current address is either the front of a 4kB sector or has already been erased. Parameters: vData - The byte to write to the next memory location. Returns: None Remarks: See Remarks in SPIFlashBeginWrite for important information about Flash memory parts. ***************************************************************************/ void SPIFlashWrite(BYTE vData) { volatile BYTE Dummy; BYTE vSPIONSave; #if defined(__18CXX) BYTE SPICON1Save; #elif defined(__C30__) WORD SPICON1Save; #else DWORD SPICON1Save; #endif // Save SPI state (clock speed) SPICON1Save = SPIFLASH_SPICON1; vSPIONSave = SPI_ON_BIT; // Configure SPI SPI_ON_BIT = 0; SPIFLASH_SPICON1 = PROPER_SPICON1; SPI_ON_BIT = 1; // If address is a boundary, erase a sector first if((dwWriteAddr & SPI_FLASH_SECTOR_MASK) == 0u) SPIFlashEraseSector(dwWriteAddr); // Enable writing _SendCmd(WREN); // Activate the chip select SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue WRITE command with address SPIFLASH_SSPBUF = WRITE; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[2]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[1]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[0]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; // Write the byte SPIFLASH_SSPBUF = vData; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; dwWriteAddr++; // Deactivate chip select and wait for write to complete SPIFLASH_CS_IO = 1; _WaitWhileBusy(); // Restore SPI state SPI_ON_BIT = 0; SPIFLASH_SPICON1 = SPICON1Save; SPI_ON_BIT = vSPIONSave; }
/***************************************************************************** Function: void SPIFlashInit(void) Description: Initializes SPI Flash module. Precondition: None Parameters: None Returns: None Remarks: This function is only called once during the lifetime of the application. Internal: This function sends WRDI to clear any pending write operation, and also clears the software write-protect on all memory locations. ***************************************************************************/ void SPIFlashInit(void) { BYTE i; volatile BYTE Dummy; BYTE vSPIONSave; #if defined(__18CXX) BYTE SPICON1Save; #elif defined(__C30__) WORD SPICON1Save; #else DWORD SPICON1Save; #endif SPIFLASH_CS_IO = 1; SPIFLASH_CS_TRIS = 0; // Drive SPI Flash chip select pin SPIFLASH_SCK_TRIS = 0; // Set SCK pin as an output SPIFLASH_SDI_TRIS = 1; // Make sure SDI pin is an input SPIFLASH_SDO_TRIS = 0; // Set SDO pin as an output // Save SPI state (clock speed) SPICON1Save = SPIFLASH_SPICON1; vSPIONSave = SPI_ON_BIT; // Configure SPI SPI_ON_BIT = 0; SPIFLASH_SPICON1 = PROPER_SPICON1; SPI_ON_BIT = 1; ClearSPIDoneFlag(); #if defined(__C30__) SPIFLASH_SPICON2 = 0; SPIFLASH_SPISTAT = 0; // clear SPI SPIFLASH_SPISTATbits.SPIEN = 1; #elif defined(__C32__) SPIFLASH_SPIBRG = (GetPeripheralClock()-1ul)/2ul/SPIFLASH_MAX_SPI_FREQ; #elif defined(__18CXX) SPIFLASH_SPISTATbits.CKE = 1; // Transmit data on rising edge of clock SPIFLASH_SPISTATbits.SMP = 0; // Input sampled at middle of data output time #endif // Read Device ID code to determine supported device capabilities/instructions { // Activate chip select SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Send instruction SPIFLASH_SSPBUF = RDID; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; // Send 3 byte address (0x000000), discard Manufacture ID, get Device ID for(i = 0; i < 5; i++) { SPIFLASH_SSPBUF = 0x00; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; } // Deactivate chip select SPIFLASH_CS_IO = 1; // Decode Device Capabilities Flags from Device ID deviceCaps.v = 0x00; switch(Dummy) { case 0x43: // SST25LF020(A) (2 Mbit) 0xAF, 14us, AAI Byte case 0x48: // SST25VF512(A) (512 Kbit) 0xAF, 14us, AAI Byte case 0x49: // SST25VF010A (1 Mbit) 0xAF, 14us, AAI Byte deviceCaps.bits.bWriteByteStream = 1; break; case 0x4B: // SST25VF064C (64 Mbit) 0x02, 1.5ms/256 byte page, no AAI deviceCaps.bits.bPageProgram = 1; break; //case 0x01: // SST25WF512 (512 Kbit) 0xAD, 50us, AAI Word //case 0x02: // SST25WF010 (1 Mbit) 0xAD, 50us, AAI Word //case 0x03: // SST25WF020 (2 Mbit) 0xAD, 50us, AAI Word //case 0x04: // SST25WF040 (4 Mbit) 0xAD, 50us, AAI Word //case 0x05: // SST25WF080 (8 Mbit) 0xAD, 14us, AAI Word //case 0x41: // SST25VF016B (16 Mbit) 0xAD, 7us, AAI Word //case 0x4A: // SST25VF032B (32 Mbit) 0xAD, 7us, AAI Word //case 0x8C: // SST25VF020B (2 Mbit) 0xAD, 7us, AAI Word //case 0x8D: // SST25VF040B (4 Mbit) 0xAD, 7us, AAI Word //case 0x8E: // SST25VF080B (8 Mbit) 0xAD, 7us, AAI Word // Assume AAI Word programming is supported for the above commented // devices and unknown devices. default: deviceCaps.bits.bWriteWordStream = 1; } } // Clear any pre-existing AAI write mode // This may occur if the PIC is reset during a write, but the Flash is // not tied to the same hardware reset. _SendCmd(WRDI); // Execute Enable-Write-Status-Register (EWSR) instruction _SendCmd(EWSR); // Clear Write-Protect on all memory locations SPIFLASH_CS_IO = 0; SPIFLASH_SSPBUF = WRSR; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = 0x00; // Clear all block protect bits WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_CS_IO = 1; // Restore SPI state SPI_ON_BIT = 0; SPIFLASH_SPICON1 = SPICON1Save; SPI_ON_BIT = vSPIONSave; }