BYTE PHYGetLongRAMAddr(WORD address) { BYTE toReturn; #if defined(__C30__) IntStatus.CCP2IntF = RFIE; RFIE = 0; PHY_CS = 0; SPIPut(((address>>3)&0b01111111)|0x80); SPIPut(((address<<5)&0b11100000)); toReturn = SPIGet(); PHY_CS = 1; RFIE = IntStatus.CCP2IntF; #else IntStatus.CCP2IntF = RFIE; RFIE = 0; PHY_CSn = 0; NOP(); SPIPut(((address>>3)&0b01111111)|0x80); SPIPut(((address<<5)&0b11100000)); toReturn = SPIGet(); NOP(); PHY_CSn = 1; RFIE = IntStatus.CCP2IntF; #endif return toReturn; }
/************************************************************************ * Function: BYTE SST25ReadByte(DWORD address) * * Overview: this function reads a byte from the address specified * * Input: address * * Output: data read * ************************************************************************/ BYTE SST25ReadByte(DWORD address) { BYTE temp; while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_READ); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[2]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[1]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[0]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, 0); temp = SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); return (temp); }
/************************************************************************ * Function: void SST25WriteByte(BYTE data, DWORD address) * * Overview: this function writes a byte to the address specified * * Input: data to be written and address * * Output: none * ************************************************************************/ void SST25WriteByte(BYTE data, DWORD address) { // SST25SectorErase(address); //DelayMs(100); SST25WriteEnable(); SST25SSLow(); SPIPut(SST25_CMD_WRITE); SPIGet(); SPIPut(((DWORD_VAL) address).v[2]); SPIGet(); SPIPut(((DWORD_VAL) address).v[1]); SPIGet(); SPIPut(((DWORD_VAL) address).v[0]); SPIGet(); SPIPut(data); SPIGet(); SST25SSHigh(); // Wait for write end while(SST25IsWriteBusy()); }
BYTE PHYGetShortRAMAddr(BYTE address) { BYTE toReturn; #if defined(__C30__) IntStatus.CCP2IntF = RFIE; RFIE = 0; PHY_CS = 0; SPIPut((address<<1)&0b01111110); toReturn = SPIGet(); PHY_CS = 1; RFIE = IntStatus.CCP2IntF; #else IntStatus.CCP2IntF = RFIE; RFIE = 0; PHY_CSn = 0; NOP(); SPIPut((address<<1)&0b01111110); toReturn = SPIGet(); NOP(); PHY_CSn = 1; RFIE = IntStatus.CCP2IntF; #endif return toReturn; }
/********************************************************************* * Function: void ReadMacAddress() * * PreCondition: none * * Input: none * * Output: Reads MAC Address from MAC Address EEPROM * * Side Effects: none * * Overview: Uses the MAC Address from the EEPROM for addressing * * Note: **********************************************************************/ void ReadMacAddress(void) { RF_EEnCS_TRIS = 0; { uint8_t Address0, Address1, Address2; RF_EEnCS = 0; SPIPut(0x03); //Read Sequence to EEPROM SPIPut(0xFA); //MAC address Start byte Address0 = SPIGet(); Address1 = SPIGet(); Address2 = SPIGet(); RF_EEnCS = 1; if((Address0 == 0x00) && (Address1 == 0x04) && (Address2 == 0xA3)) //Compare the value against Microchip's OUI { RF_EEnCS = 0; SPIPut(0x03); SPIPut(0xFD); switch(MY_ADDRESS_LENGTH) { case 8: myLongAddress[7] = 0x00; case 7: myLongAddress[6] = 0x04; case 6: myLongAddress[5] = 0xA3; case 5: myLongAddress[4] = 0xFF; case 4: myLongAddress[3] = 0xFE; case 3: myLongAddress[2] = SPIGet(); case 2: myLongAddress[1] = SPIGet(); case 1: myLongAddress[0] = SPIGet(); break; default: break; } RF_EEnCS = 1; } } }
/************************************************************************ * Function: void SST25SectorErase(DWORD address) * * Overview: this function erases a 4Kb sector * * Input: address within sector to be erased * * Output: none * ************************************************************************/ void SST25SectorErase(DWORD address) { SST25WriteEnable(); while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_SER); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[2]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[1]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[0]); SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); // Wait for write end while(SST25IsWriteBusy()); }
/************************************************************************ * Function: void SST25ReadArray(DWORD address, BYTE* pData, nCount) * * Overview: this function reads data into buffer specified * * Input: flash memory address, pointer to the data buffer, data number * ************************************************************************/ void SST25ReadArray(DWORD address, BYTE *pData, WORD nCount) { while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_READ); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[2]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[1]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[0]); SPIGet(spiInitData.channel); while(nCount--) { SPIPut(spiInitData.channel, 0); *pData++ = SPIGet(spiInitData.channel); } SST25CSHigh(); SPIUnLock(spiInitData.channel); }
/************************************************************************ * Function: void SST25SectorErase(DWORD address) * * Overview: this function erases a 4Kb sector * * Input: address within sector to be erased * * Output: none * ************************************************************************/ void SST25SectorErase(uint32_t address) { SST25WriteEnable(); // while(!SPILock(spiInitData.spiId)); SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_SER); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>16); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>8); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address); PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); // SPIUnLock(spiInitData.spiId); // Wait for write end while(SST25IsWriteBusy()); }
/************************************************************************ * Function: BYTE SST25ReadByte(DWORD address) * * Overview: this function reads a byte from the address specified * * Input: address * * Output: data read * ************************************************************************/ uint8_t SST25ReadByte(uint32_t address) { uint8_t temp; SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_READ); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>16); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>8); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, 0); temp = PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); return (temp); }
/************************************************************************ * Function: void SST25ReadArray(DWORD address, BYTE* pData, nCount) * * Overview: this function reads data into buffer specified * * Input: flash memory address, pointer to the data buffer, data number * ************************************************************************/ void SST25ReadArray(uint32_t address, uint8_t *pData, uint16_t nCount) { SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_READ); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>16); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>8); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address); PLIB_SPI_BufferRead(spiInitData.spiId); while(nCount--) { SPIPut(spiInitData.spiId, 0); *pData++ = PLIB_SPI_BufferRead(spiInitData.spiId); } SST25CSHigh(); }
/************************************************************************ * Function: void SST25WriteByte(BYTE data, DWORD address) * * Overview: this function writes a byte to the address specified * * Input: data to be written and address * * Output: none * ************************************************************************/ void SST25WriteByte(uint8_t data, uint32_t address) { SST25WriteEnable(); SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_WRITE); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>16); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address>>8); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, address); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, data); PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); // Wait for write end while(SST25IsWriteBusy()); }
uint8 CC2420_WriteRegister(uint8 addr, uint16 data) { uint8 status; MAKE_CC_CS_LOW(); status = SPIPut(addr); status = SPIPut(data>>8); status = SPIPut(data); MAKE_CC_CS_HIGH(); return status; }
/************************************************************************ * Function: void SST25ResetWriteProtection() * * Overview: this function reset write protection bits * * Input: none * * Output: none * ************************************************************************/ void SST25ResetWriteProtection(void) { #if defined(USE_M25P80) BYTE status; #endif while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); // send write enable command SPIPut(spiInitData.channel, SST25_CMD_EWSR); SPIGet(spiInitData.channel); SST25CSHigh(); #if defined(USE_M25P80) SST25CSLow(); // verify if the WEL bit is set high while(1) { SPIPut(spiInitData.channel, SST25_CMD_RDSR); SPIGet(spiInitData.channel); status = 0xFF; while((status & SST25_STATUS_MASK) > 0) { SPIPut(spiInitData.channel, 0); status = SPIGet(spiInitData.channel); } if ((status & SST25_WEL_STATUS) == SST25_WEL_STATUS) break; } SST25CSHigh(); #endif SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_WRSR); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, 0); SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); // Wait for write end while(SST25IsWriteBusy()); }
void LCDWritePortB(BYTE v) { BYTE preRFIE = RFIE; RFIE = 0; LCD_CS = 0; SPIPut(0x40); SPIPut(0x13); SPIPut(v); LCD_CS = 1; RFIE = preRFIE; }
void LCDInitPortB(void) { BYTE preRFIE = RFIE; RFIE = 0; LCD_CS = 0; SPIPut(0x40); SPIPut(0x01); SPIPut(0x00); LCD_CS = 1; RFIE = preRFIE; }
void CC2420_FlushRxFIFO() { MAKE_CC_CS_LOW(); SPIPut(CC2420_RXFIFO | 0x40); SPIGet(); MAKE_CC_CS_HIGH(); MAKE_CC_CS_LOW(); SPIPut(CC2420_SFLUSHRX); SPIPut(CC2420_SFLUSHRX); MAKE_CC_CS_HIGH(); }
void LCDWritePortB(uint8_t v) { uint8_t preRFIE = RFIE; RFIE = 0; LCD_CS = 0; SPIPut(0x40); SPIPut(0x13); SPIPut(v); LCD_CS = 1; RFIE = preRFIE; }
void LCDInitPortA(void) { uint8_t preRFIE = RFIE; RFIE = 0; LCD_CS = 0; SPIPut(0x40); SPIPut(0x00); SPIPut(0x00); LCD_CS = 1; RFIE = preRFIE; }
void EEPROMRead(uint8_t *dest, uint8_t addr, uint8_t count) { EE_nCS = 0; SPIPut(SPI_READ); SPIPut(addr); while( count ) { *dest++ = SPIGet(); count--; } EE_nCS = 1; }
/************************************************************************ * Function: void SST25ResetWriteProtection() * * Overview: this function reset write protection bits * * Input: none * * Output: none * ************************************************************************/ void SST25ResetWriteProtection(void) { #if defined(USE_M25P80) uint8_t status; #endif // while(!SPILock(spiInitData.spiId)); SST25CSLow(); // send write enable command SPIPut(spiInitData.spiId, SST25_CMD_EWSR); PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); #if defined(USE_M25P80) SST25CSLow(); // verify if the WEL bit is set high while(1) { SPIPut(spiInitData.spiId, SST25_CMD_RDSR); SPIGet(spiInitData.spiId); status = 0xFF; while((status & SST25_STATUS_MASK) > 0) { SPIPut(spiInitData.spiId, 0); status = SPIGet(spiInitData.spiId); } if ((status & SST25_WEL_STATUS) == SST25_WEL_STATUS) break; } SST25CSHigh(); #endif SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_WRSR); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, 0); PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); // SPIUnLock(spiInitData.spiId); // Wait for write end while(SST25IsWriteBusy()); }
/************************************************************************ * Function: BYTE SST25IsWriteBusy(void) * * Overview: this function reads status register and chek BUSY bit for write operation * * Input: none * * Output: non zero if busy * ************************************************************************/ uint8_t SST25IsWriteBusy(void) { uint8_t temp; SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_RDSR); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, 0); temp = PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); return (temp & 0x01); }
/************************************************************************ * Function: BYTE SST25IsWriteBusy(void) * * Overview: this function reads status register and chek BUSY bit for write operation * * Input: none * * Output: non zero if busy * ************************************************************************/ BYTE SST25IsWriteBusy(void) { BYTE temp; SST25SSLow(); SPIPut(SST25_CMD_RDSR); SPIGet(); SPIPut(0); temp = SPIGet(); SST25SSHigh(); return (temp & 0x01); }
/************************************************************************ * Function: MCHP25LC256ReadStatus() * * Overview: this function reads status register * * Input: none * * Output: status register value * ************************************************************************/ union _MCHP25LC256Status_ MCHP25LC256ReadStatus(void) { BYTE temp; MCHP25LC256CSLow(); SPIPut(MCHP25LC256_spi_channel, EEPROM_CMD_RDSR); SPIGet(MCHP25LC256_spi_channel); SPIPut(MCHP25LC256_spi_channel, 0); temp = SPIGet(MCHP25LC256_spi_channel); MCHP25LC256CSHigh(); return (union _MCHP25LC256Status_)temp; }
void CC2420_Restart() { uint8 status; status = CC2420_StrobCmd(CC2420_SXOSCON); while (!(status & 0x40)) { //CC2420_StrobCmd(CC2420_SXOSCOFF); status = CC2420_StrobCmd(CC2420_SXOSCON); } status = CC2420_StrobCmd(CC2420_SRXON); MAKE_CC_CS_LOW(); SPIPut(CC2420_SFLUSHRX); SPIPut(CC2420_SFLUSHRX); MAKE_CC_CS_HIGH(); }
/************************************************************************ * Function: BYTE SST25WriteArray(DWORD address, BYTE* pData, nCount) * * Overview: this function writes a data array at the address specified * * Input: flash memory address, pointer to the data array, data number * * Output: return 1 if the operation was successfull * ************************************************************************/ uint8_t SST25WriteArray(uint32_t address, uint8_t *pData, uint16_t nCount) { uint32_t addr; uint8_t *pD; uint16_t counter; #if defined(USE_M25P80) uint8_t status; #endif addr = address; pD = pData; // WRITE for(counter = 0; counter < nCount; counter++) { SST25WriteByte(*pD++, addr++); } #if defined(USE_M25P80) // check status of Write in Progress // wait for BULK ERASE to be done SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_RDSR); SPIGet(spiInitData.spiId); while(1) { SPIPut(spiInitData.spiId, 0); status = (SPIGet(spiInitData.spiId)& SST25_WIP_STATUS); if ((status & SST25_WIP_STATUS) == 0) break; } SST25CSHigh(); #endif // VERIFY for(counter = 0; counter < nCount; counter++) { if(*pData != SST25ReadByte(address)) return (0); pData++; address++; } return (1); }
/************************************************************************ * Function: BYTE SST25WriteArray(DWORD address, BYTE* pData, nCount) * * Overview: this function writes a data array at the address specified * * Input: flash memory address, pointer to the data array, data number * * Output: return 1 if the operation was successfull * ************************************************************************/ BYTE SST25WriteArray(DWORD address, BYTE *pData, WORD nCount) { DWORD addr; BYTE *pD; WORD counter; #if defined(USE_M25P80) BYTE status; #endif addr = address; pD = pData; // WRITE for(counter = 0; counter < nCount; counter++) { SST25WriteByte(*pD++, addr++); } #if defined(USE_M25P80) // check status of Write in Progress // wait for BULK ERASE to be done SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_RDSR); SPIGet(spiInitData.channel); while(1) { SPIPut(spiInitData.channel, 0); status = (SPIGet(spiInitData.channel)& SST25_WIP_STATUS); if ((status & SST25_WIP_STATUS) == 0) break; } SST25CSHigh(); #endif // VERIFY for(counter = 0; counter < nCount; counter++) { if(*pData != SST25ReadByte(address)) return (0); pData++; address++; } return (1); }
/************************************************************************ * Function: MCHP25LC256WriteEnable() * * Overview: this function allows write/erase MCHP25LC256. Must be called * before every write/erase command. * * Input: none * * Output: none * ************************************************************************/ void MCHP25LC256WriteEnable(void) { MCHP25LC256CSLow(); SPIPut(MCHP25LC256_spi_channel, EEPROM_CMD_WREN); SPIGet(MCHP25LC256_spi_channel); MCHP25LC256CSHigh(); }
/************************************************************************ * Function: SST25WriteEnable() * * Overview: this function allows write/erase SST25. Must be called * before every write/erase command. * * Input: none * * Output: none * ************************************************************************/ void SST25WriteEnable(void) { SST25SSLow(); SPIPut(SST25_CMD_WREN); SPIGet(); SST25SSHigh(); }
void NVMRead (BYTE *dest, WORD src, BYTE count) { #if !defined(__C30__) BYTE oldGIEH; #endif #ifdef ENABLE_DEBUG ConsolePut('r'); PrintChar( (BYTE)(((WORD)src>>8)&0xFF) ); PrintChar( (BYTE)((WORD)src&0xFF) ); ConsolePut('-'); PrintChar( count ); #endif #if !defined(__C30__) oldGIEH = 0; if ( INTCONbits.GIEH ) { oldGIEH = 1; } INTCONbits.GIEH = 0; #endif SPISelectEEPROM(); SPIPut( SPIREAD ); SPIPut( (BYTE)(((WORD)src>>8) & 0xFF) ); SPIPut( (BYTE)((WORD)src & 0xFF) ); while( count ) { *dest = SPIGet(); #ifdef ENABLE_DEBUG #endif dest++; count--; } SPIUnselectEEPROM(); #if !defined(__C30__) if (oldGIEH) { INTCONbits.GIEH = 1; } #endif #ifdef ENABLE_DEBUG ConsolePutROMString((ROM char * const)"\r\n"); #endif }
uint8 CC2420_WriteTxFIFO(uint8 length, uint8* data) { uint8 status; uint8 i; MAKE_CC_CS_LOW(); status = SPIPut(CC2420_TXFIFO); // add another 2 bytes on the data length status = SPIPut(length + 2); for(i=0; i<length; i++) { SPIPut(data); } MAKE_CC_CS_HIGH(); return status; }