1MB version
Dependencies: mbed EEPROM USBDevice
bbt_eeprom.cpp@3:28ceea26c918, 2021-02-10 (annotated)
- Committer:
- muraguchi
- Date:
- Wed Feb 10 03:45:12 2021 +0000
- Revision:
- 3:28ceea26c918
- Parent:
- 0:1472308ded03
Comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
muraguchi | 0:1472308ded03 | 1 | #include "bbt_eeprom.h" |
muraguchi | 0:1472308ded03 | 2 | |
muraguchi | 0:1472308ded03 | 3 | bbt_eeprom::bbt_eeprom(uint16_t offsetReservedBlockAddress=1004){ |
muraguchi | 0:1472308ded03 | 4 | m_offsetReservedBlockAddress = offsetReservedBlockAddress; |
muraguchi | 0:1472308ded03 | 5 | m_translateTableMaxIndex=0; |
muraguchi | 0:1472308ded03 | 6 | m_tempSlot=0; |
muraguchi | 0:1472308ded03 | 7 | } |
muraguchi | 0:1472308ded03 | 8 | |
muraguchi | 0:1472308ded03 | 9 | uint8_t bbt_eeprom::getStatus_EEP(){ |
muraguchi | 0:1472308ded03 | 10 | return m_eeprom.get(BBT_EEPROM_STATUS_ADDR); |
muraguchi | 0:1472308ded03 | 11 | } |
muraguchi | 0:1472308ded03 | 12 | |
muraguchi | 0:1472308ded03 | 13 | void bbt_eeprom::setStatus_EEP(uint8_t status){ |
muraguchi | 0:1472308ded03 | 14 | m_eeprom.put(BBT_EEPROM_STATUS_ADDR,status); |
muraguchi | 0:1472308ded03 | 15 | } |
muraguchi | 0:1472308ded03 | 16 | |
muraguchi | 0:1472308ded03 | 17 | void bbt_eeprom::clearAllSlot_EEP(){ |
muraguchi | 0:1472308ded03 | 18 | for (int l=0;l<BBT_EEPROM_SLOT_LENGTH;l++){ |
muraguchi | 0:1472308ded03 | 19 | setSlot_EEP(l,0,0,0); |
muraguchi | 0:1472308ded03 | 20 | } |
muraguchi | 0:1472308ded03 | 21 | } |
muraguchi | 0:1472308ded03 | 22 | |
muraguchi | 0:1472308ded03 | 23 | void bbt_eeprom::setSlot_EEP(uint8_t slot, uint8_t broken, uint8_t used,uint16_t blockAddress){ |
muraguchi | 0:1472308ded03 | 24 | m_eeprom.put(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE, (blockAddress&0xFF) ); |
muraguchi | 0:1472308ded03 | 25 | m_eeprom.put(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE+1, ((broken&1)<<7) | ((used&1)<<6) | ((blockAddress>>8)&0x3F) ); |
muraguchi | 0:1472308ded03 | 26 | } |
muraguchi | 0:1472308ded03 | 27 | |
muraguchi | 0:1472308ded03 | 28 | void bbt_eeprom::getSlot_EEP(uint8_t slot, uint8_t * broken, uint8_t * used, uint16_t * blockAddress){ |
muraguchi | 0:1472308ded03 | 29 | uint8_t high_byte; |
muraguchi | 0:1472308ded03 | 30 | uint8_t low_byte; |
muraguchi | 0:1472308ded03 | 31 | low_byte = m_eeprom.get(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE); |
muraguchi | 0:1472308ded03 | 32 | high_byte = m_eeprom.get(BBT_EEPROM_SLOT_BASE_ADDR+slot*BBT_EEPROM_SLOT_SIZE+1); |
muraguchi | 0:1472308ded03 | 33 | *broken = high_byte >> 7; |
muraguchi | 0:1472308ded03 | 34 | *used = ( high_byte >> 6 ) & 1; |
muraguchi | 0:1472308ded03 | 35 | *blockAddress = ( ( high_byte & 0x3F ) << 8 ) | low_byte; |
muraguchi | 0:1472308ded03 | 36 | } |
muraguchi | 0:1472308ded03 | 37 | |
muraguchi | 0:1472308ded03 | 38 | uint16_t bbt_eeprom::slotToReservedBlockAddress(uint8_t slot){ |
muraguchi | 0:1472308ded03 | 39 | return m_offsetReservedBlockAddress + slot; |
muraguchi | 0:1472308ded03 | 40 | } |
muraguchi | 0:1472308ded03 | 41 | |
muraguchi | 0:1472308ded03 | 42 | uint8_t bbt_eeprom::reservedBlockAddressToSlot(uint16_t reservedBlockAddress){ |
muraguchi | 0:1472308ded03 | 43 | return reservedBlockAddress - m_offsetReservedBlockAddress; |
muraguchi | 0:1472308ded03 | 44 | } |
muraguchi | 0:1472308ded03 | 45 | |
muraguchi | 0:1472308ded03 | 46 | // 0-19: free slot number |
muraguchi | 0:1472308ded03 | 47 | // 255: fail |
muraguchi | 0:1472308ded03 | 48 | uint8_t bbt_eeprom::getFreeSlot_EEP(){ |
muraguchi | 0:1472308ded03 | 49 | uint8_t broken,used; |
muraguchi | 0:1472308ded03 | 50 | uint16_t block_addr; |
muraguchi | 0:1472308ded03 | 51 | for (int s=0;s<BBT_EEPROM_SLOT_LENGTH;s++){ |
muraguchi | 0:1472308ded03 | 52 | getSlot_EEP(s,&broken,&used,&block_addr); |
muraguchi | 0:1472308ded03 | 53 | if( broken==0 && used==0 ) { |
muraguchi | 0:1472308ded03 | 54 | return s; |
muraguchi | 0:1472308ded03 | 55 | } |
muraguchi | 0:1472308ded03 | 56 | } |
muraguchi | 0:1472308ded03 | 57 | return 255; |
muraguchi | 0:1472308ded03 | 58 | } |
muraguchi | 0:1472308ded03 | 59 | |
muraguchi | 0:1472308ded03 | 60 | void bbt_eeprom::updateTranslateTable(){ |
muraguchi | 0:1472308ded03 | 61 | m_translateTableMaxIndex = 0; |
muraguchi | 0:1472308ded03 | 62 | uint8_t broken,used; |
muraguchi | 0:1472308ded03 | 63 | uint16_t block_addr; |
muraguchi | 0:1472308ded03 | 64 | for (int s=0;s<BBT_EEPROM_SLOT_LENGTH;s++){ |
muraguchi | 0:1472308ded03 | 65 | getSlot_EEP(s,&broken,&used,&block_addr); |
muraguchi | 0:1472308ded03 | 66 | if (broken==0 && used==1) { |
muraguchi | 0:1472308ded03 | 67 | m_translateTableFrom[m_translateTableMaxIndex]=block_addr; |
muraguchi | 0:1472308ded03 | 68 | m_translateTableTo[m_translateTableMaxIndex]=slotToReservedBlockAddress(s); |
muraguchi | 0:1472308ded03 | 69 | m_translateTableMaxIndex++; |
muraguchi | 0:1472308ded03 | 70 | } |
muraguchi | 0:1472308ded03 | 71 | } |
muraguchi | 0:1472308ded03 | 72 | } |
muraguchi | 0:1472308ded03 | 73 | |
muraguchi | 0:1472308ded03 | 74 | uint16_t bbt_eeprom::getTranslatedBlockAddress(uint16_t blockAddress){ |
muraguchi | 0:1472308ded03 | 75 | for (int i=0;i<m_translateTableMaxIndex;i++){ |
muraguchi | 0:1472308ded03 | 76 | if(m_translateTableFrom[i]==blockAddress){ |
muraguchi | 0:1472308ded03 | 77 | return m_translateTableTo[i]; |
muraguchi | 0:1472308ded03 | 78 | } |
muraguchi | 0:1472308ded03 | 79 | } |
muraguchi | 0:1472308ded03 | 80 | return blockAddress; |
muraguchi | 0:1472308ded03 | 81 | } |
muraguchi | 0:1472308ded03 | 82 | |
muraguchi | 0:1472308ded03 | 83 | void bbt_eeprom::setTempSlot_EEP(uint8_t tempSlot){ |
muraguchi | 0:1472308ded03 | 84 | m_eeprom.put(BBT_EEPROM_TEMPSLOT_ADDR,tempSlot); |
muraguchi | 0:1472308ded03 | 85 | } |
muraguchi | 0:1472308ded03 | 86 | |
muraguchi | 0:1472308ded03 | 87 | uint8_t bbt_eeprom::getTempSlot_EEP(){ |
muraguchi | 0:1472308ded03 | 88 | return m_eeprom.get(BBT_EEPROM_TEMPSLOT_ADDR); |
muraguchi | 0:1472308ded03 | 89 | } |
muraguchi | 0:1472308ded03 | 90 | |
muraguchi | 0:1472308ded03 | 91 | void bbt_eeprom::incrTempSlot_EEP(){ |
muraguchi | 0:1472308ded03 | 92 | setTempSlot_EEP(getTempSlot_EEP()+1); |
muraguchi | 0:1472308ded03 | 93 | } |
muraguchi | 0:1472308ded03 | 94 | |
muraguchi | 0:1472308ded03 | 95 | void bbt_eeprom::updateTempSlot(){ |
muraguchi | 0:1472308ded03 | 96 | m_tempSlot = getTempSlot_EEP(); |
muraguchi | 0:1472308ded03 | 97 | } |
muraguchi | 0:1472308ded03 | 98 | |
muraguchi | 0:1472308ded03 | 99 | void bbt_eeprom::setTempSlot(uint8_t tempSlot){ |
muraguchi | 0:1472308ded03 | 100 | m_tempSlot = tempSlot; |
muraguchi | 0:1472308ded03 | 101 | } |
muraguchi | 0:1472308ded03 | 102 | |
muraguchi | 0:1472308ded03 | 103 | uint8_t bbt_eeprom::getTempSlot(){ |
muraguchi | 0:1472308ded03 | 104 | return m_tempSlot; |
muraguchi | 0:1472308ded03 | 105 | } |
muraguchi | 0:1472308ded03 | 106 | |
muraguchi | 0:1472308ded03 | 107 | bool bbt_eeprom::isReservedBlockAddress(uint16_t blockAddress){ |
muraguchi | 0:1472308ded03 | 108 | if(m_offsetReservedBlockAddress>=blockAddress){ |
muraguchi | 0:1472308ded03 | 109 | return true; |
muraguchi | 0:1472308ded03 | 110 | } |
muraguchi | 0:1472308ded03 | 111 | return false; |
muraguchi | 0:1472308ded03 | 112 | } |
muraguchi | 0:1472308ded03 | 113 | |
muraguchi | 0:1472308ded03 | 114 | uint8_t bbt_eeprom::get(uint16_t address){ |
muraguchi | 0:1472308ded03 | 115 | return m_eeprom.get(address); |
muraguchi | 0:1472308ded03 | 116 | } |
muraguchi | 0:1472308ded03 | 117 | |
muraguchi | 0:1472308ded03 | 118 | void bbt_eeprom::put(uint16_t address,uint8_t data){ |
muraguchi | 0:1472308ded03 | 119 | m_eeprom.put(address,data); |
muraguchi | 0:1472308ded03 | 120 | } |