1MB version

Dependencies:   mbed EEPROM USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bbt_eeprom.cpp Source File

bbt_eeprom.cpp

00001 #include "bbt_eeprom.h"
00002 
00003 bbt_eeprom::bbt_eeprom(uint16_t offsetReservedBlockAddress=1004){
00004     m_offsetReservedBlockAddress = offsetReservedBlockAddress;
00005     m_translateTableMaxIndex=0;
00006     m_tempSlot=0;
00007 }
00008 
00009 uint8_t bbt_eeprom::getStatus_EEP(){
00010     return m_eeprom.get(BBT_EEPROM_STATUS_ADDR);
00011 }
00012 
00013 void bbt_eeprom::setStatus_EEP(uint8_t status){
00014     m_eeprom.put(BBT_EEPROM_STATUS_ADDR,status);
00015 }
00016 
00017 void bbt_eeprom::clearAllSlot_EEP(){
00018     for (int l=0;l<BBT_EEPROM_SLOT_LENGTH;l++){
00019         setSlot_EEP(l,0,0,0);
00020     }
00021 }
00022 
00023 void bbt_eeprom::setSlot_EEP(uint8_t slot, uint8_t broken, uint8_t used,uint16_t blockAddress){
00024     m_eeprom.put(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE,   (blockAddress&0xFF) );
00025     m_eeprom.put(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE+1, ((broken&1)<<7) | ((used&1)<<6) | ((blockAddress>>8)&0x3F) );   
00026 }
00027 
00028 void bbt_eeprom::getSlot_EEP(uint8_t slot, uint8_t * broken, uint8_t * used, uint16_t * blockAddress){
00029     uint8_t  high_byte;
00030     uint8_t  low_byte;
00031     low_byte  = m_eeprom.get(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE);
00032     high_byte = m_eeprom.get(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE+1);
00033     *broken = high_byte >> 7;
00034     *used   = ( high_byte >> 6 ) & 1;
00035     *blockAddress = ( ( high_byte & 0x3F ) << 8 ) | low_byte;
00036 }
00037 
00038 uint16_t bbt_eeprom::slotToReservedBlockAddress(uint8_t slot){
00039     return m_offsetReservedBlockAddress + slot;
00040 }
00041 
00042 uint8_t  bbt_eeprom::reservedBlockAddressToSlot(uint16_t reservedBlockAddress){
00043     return reservedBlockAddress - m_offsetReservedBlockAddress;
00044 }
00045 
00046 // 0-19: free slot number
00047 // 255: fail
00048 uint8_t bbt_eeprom::getFreeSlot_EEP(){
00049     uint8_t  broken,used;
00050     uint16_t block_addr;
00051     for (int s=0;s<BBT_EEPROM_SLOT_LENGTH;s++){
00052         getSlot_EEP(s,&broken,&used,&block_addr);
00053         if( broken==0 && used==0 ) {
00054             return s;
00055         }
00056     }
00057     return 255;
00058 }
00059 
00060 void bbt_eeprom::updateTranslateTable(){
00061     m_translateTableMaxIndex = 0;
00062     uint8_t  broken,used;
00063     uint16_t block_addr;    
00064     for (int s=0;s<BBT_EEPROM_SLOT_LENGTH;s++){
00065         getSlot_EEP(s,&broken,&used,&block_addr);
00066         if (broken==0 && used==1) {
00067             m_translateTableFrom[m_translateTableMaxIndex]=block_addr;
00068             m_translateTableTo[m_translateTableMaxIndex]=slotToReservedBlockAddress(s);
00069             m_translateTableMaxIndex++;
00070         }
00071     }    
00072 }
00073 
00074 uint16_t bbt_eeprom::getTranslatedBlockAddress(uint16_t blockAddress){
00075     for (int i=0;i<m_translateTableMaxIndex;i++){
00076         if(m_translateTableFrom[i]==blockAddress){
00077             return m_translateTableTo[i];
00078         }
00079     }
00080     return blockAddress;
00081 }
00082 
00083 void     bbt_eeprom::setTempSlot_EEP(uint8_t tempSlot){
00084     m_eeprom.put(BBT_EEPROM_TEMPSLOT_ADDR,tempSlot);
00085 }
00086 
00087 uint8_t  bbt_eeprom::getTempSlot_EEP(){
00088     return m_eeprom.get(BBT_EEPROM_TEMPSLOT_ADDR);
00089 }
00090 
00091 void     bbt_eeprom::incrTempSlot_EEP(){
00092     setTempSlot_EEP(getTempSlot_EEP()+1);
00093 }
00094 
00095 void     bbt_eeprom::updateTempSlot(){
00096     m_tempSlot = getTempSlot_EEP();
00097 }
00098 
00099 void     bbt_eeprom::setTempSlot(uint8_t tempSlot){
00100         m_tempSlot = tempSlot;
00101 }
00102 
00103 uint8_t  bbt_eeprom::getTempSlot(){
00104         return m_tempSlot;
00105 }
00106 
00107 bool     bbt_eeprom::isReservedBlockAddress(uint16_t blockAddress){
00108     if(m_offsetReservedBlockAddress>=blockAddress){
00109         return true;
00110     }
00111     return false;
00112 }
00113 
00114 uint8_t  bbt_eeprom::get(uint16_t address){
00115     return m_eeprom.get(address);
00116 }
00117 
00118 void     bbt_eeprom::put(uint16_t address,uint8_t data){
00119     m_eeprom.put(address,data);
00120 }