]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/sdcard_sd/pd.py
sdcard_sd: Add basic support for CSD register fields.
[libsigrokdecode.git] / decoders / sdcard_sd / pd.py
index b7f6a85d33355ff8e5fa8cb66e4bb992849c3803..e0f73e7116965478ffa0147bb2df9c1af469a8ef 100644 (file)
@@ -22,11 +22,20 @@ from common.srdhelper import SrdIntEnum, SrdStrEnum
 from common.sdcard import (cmd_names, acmd_names, accepted_voltages, card_status, sd_status)
 
 responses = '1 1b 2 3 6 7'.split()
+reg_cid = 'MID OID PNM PRV PSN RSVD MDT CRC ONE'.split()
+reg_csd = 'CSD_STRUCTURE RSVD TAAC NSAC TRAN_SPEED CCC READ_BL_LEN \
+    READ_BL_PARTIAL WRITE_BLK_MISALIGN READ_BLK_MISALIGN DSR_IMP C_SIZE \
+    VDD_R_CURR_MIN VDD_R_CURR_MAX VDD_W_CURR_MIN VDD_W_CURR_MAX C_SIZE_MULT \
+    ERASE_BLK_EN SECTOR_SIZE WP_GRP_SIZE WP_GRP_ENABLE R2W_FACTOR \
+    WRITE_BL_LEN WRITE_BL_PARTIAL FILE_FORMAT_GRP COPY PERM_WRITE_PROTECT \
+    TMP_WRITE_PROTECT FILE_FORMAT CRC ONE'.split()
 
 Pin = SrdIntEnum.from_str('Pin', 'CMD CLK DAT0 DAT1 DAT2 DAT3')
 
 a = ['CMD%d' % i for i in range(64)] + ['ACMD%d' % i for i in range(64)] + \
-    ['R' + r.upper() for r in responses] + \
+    ['RESPONSE_R' + r.upper() for r in responses] + \
+    ['R_CID_' + r for r in reg_cid] + \
+    ['R_CSD_' + r for r in reg_csd] + \
     ['F_' + f for f in 'START TRANSM CMD ARG CRC END'.split()] + \
     ['BIT', 'DECODED_BIT', 'DECODED_F']
 Ann = SrdIntEnum.from_list('Ann', a)
@@ -64,7 +73,10 @@ class Decoder(srd.Decoder):
     annotations = \
         tuple(('cmd%d' % i, 'CMD%d' % i) for i in range(64)) + \
         tuple(('acmd%d' % i, 'ACMD%d' % i) for i in range(64)) + \
-        tuple(('r%s' % r, 'R%s response' % r) for r in responses) + ( \
+        tuple(('response_r%s' % r, 'R%s' % r) for r in responses) + \
+        tuple(('reg_cid_' + r.lower(), 'CID: ' + r) for r in reg_cid) + \
+        tuple(('reg_csd_' + r.lower(), 'CSD: ' + r) for r in reg_csd) + \
+    ( \
         ('field-start', 'Start bit'),
         ('field-transmission', 'Transmission bit'),
         ('field-cmd', 'Command'),
@@ -77,10 +89,10 @@ class Decoder(srd.Decoder):
     )
     annotation_rows = (
         ('raw-bits', 'Raw bits', (Ann.BIT,)),
-        ('decoded-bits', 'Decoded bits', (Ann.DECODED_BIT,)),
+        ('decoded-bits', 'Decoded bits', (Ann.DECODED_BIT,) + Ann.prefixes('R_')),
         ('decoded-fields', 'Decoded fields', (Ann.DECODED_F,)),
         ('fields', 'Fields', Ann.prefixes('F_')),
-        ('commands', 'Commands', Ann.prefixes('CMD ACMD R')),
+        ('commands', 'Commands', Ann.prefixes('CMD ACMD RESPONSE_')),
     )
 
     def __init__(self):
@@ -118,7 +130,7 @@ class Decoder(srd.Decoder):
                          self.cmd_str.split(' ')[0]]])
 
     def putr(self, r):
-        self.putt([r, ['Response: %s' % r.name]])
+        self.putt([r, ['Response: %s' % r.name.split('_')[1]]])
 
     def cmd_name(self, cmd):
         c = acmd_names if self.is_acmd else cmd_names
@@ -300,6 +312,54 @@ class Decoder(srd.Decoder):
     def handle_acmd999(self):
         self.token, self.state = [], St.GET_RESPONSE_R1
 
+    def handle_reg_cid(self):
+        self.putf(8, 15, [Ann.R_CID_MID, ['Manufacturer ID', 'MID']])
+        self.putf(16, 31, [Ann.R_CID_OID, ['OEM/application ID', 'OID']])
+        self.putf(32, 71, [Ann.R_CID_PNM, ['Product name', 'PNM']])
+        self.putf(72, 79, [Ann.R_CID_PRV, ['Product revision', 'PRV']])
+        self.putf(80, 111, [Ann.R_CID_PSN, ['Product serial number', 'PSN']])
+        self.putf(112, 115, [Ann.R_CID_RSVD, ['Reserved', 'RSVD', 'R']])
+        self.putf(116, 127, [Ann.R_CID_MDT, ['Manufacturing date', 'MDT']])
+        self.putf(128, 134, [Ann.R_CID_CRC, ['CRC7 checksum', 'CRC']])
+        self.putf(135, 135, [Ann.R_CID_ONE, ['Always 1', '1']])
+
+    def handle_reg_csd(self):
+        self.putf(8, 9, [Ann.R_CSD_CSD_STRUCTURE, ['CSD structure', 'CSD_STRUCTURE']])
+        self.putf(10, 15, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
+        self.putf(16, 23, [Ann.R_CSD_TAAC, ['Data read access-time - 1', 'TAAC']])
+        self.putf(24, 31, [Ann.R_CSD_NSAC, ['Data read access-time - 2 in CLK cycles (NSAC * 100)', 'NSAC']])
+        self.putf(32, 39, [Ann.R_CSD_TRAN_SPEED, ['Max. data transfer rate', 'TRAN_SPEED']])
+        self.putf(40, 51, [Ann.R_CSD_CCC, ['Card command classes', 'CCC']])
+        self.putf(52, 55, [Ann.R_CSD_READ_BL_LEN, ['Max. read data block length', 'READ_BL_LEN']])
+        self.putf(56, 56, [Ann.R_CSD_READ_BL_PARTIAL, ['Partial blocks for read allowed', 'READ_BL_PARTIAL']])
+        self.putf(57, 57, [Ann.R_CSD_WRITE_BLK_MISALIGN, ['Write block misalignment', 'WRITE_BLK_MISALIGN']])
+        self.putf(58, 58, [Ann.R_CSD_READ_BLK_MISALIGN, ['Read block misalignment', 'READ_BLK_MISALIGN']])
+        self.putf(59, 59, [Ann.R_CSD_DSR_IMP, ['DSR implemented', 'DSR_IMP']])
+        self.putf(60, 61, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
+        self.putf(62, 73, [Ann.R_CSD_C_SIZE, ['Device size', 'C_SIZE']])
+        self.putf(74, 76, [Ann.R_CSD_VDD_R_CURR_MIN, ['Max. read current @VDD min', 'VDD_R_CURR_MIN']])
+        self.putf(77, 79, [Ann.R_CSD_VDD_R_CURR_MAX, ['Max. read current @VDD max', 'VDD_R_CURR_MAX']])
+        self.putf(80, 82, [Ann.R_CSD_VDD_W_CURR_MIN, ['Max. write current @VDD min', 'VDD_W_CURR_MIN']])
+        self.putf(83, 85, [Ann.R_CSD_VDD_W_CURR_MAX, ['Max. write current @VDD max', 'VDD_W_CURR_MAX']])
+        self.putf(86, 88, [Ann.R_CSD_C_SIZE_MULT, ['Device size multiplier', 'C_SIZE_MULT']])
+        self.putf(89, 89, [Ann.R_CSD_ERASE_BLK_EN, ['Erase single block enable', 'ERASE_BLK_EN']])
+        self.putf(90, 96, [Ann.R_CSD_SECTOR_SIZE, ['Erase sector size', 'SECTOR_SIZE']])
+        self.putf(97, 103, [Ann.R_CSD_WP_GRP_SIZE, ['Write protect group size', 'WP_GRP_SIZE']])
+        self.putf(104, 104, [Ann.R_CSD_WP_GRP_ENABLE, ['Write protect group enable', 'WP_GRP_ENABLE']])
+        self.putf(105, 106, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
+        self.putf(107, 109, [Ann.R_CSD_R2W_FACTOR, ['Write speed factor', 'R2W_FACTOR']])
+        self.putf(110, 113, [Ann.R_CSD_WRITE_BL_LEN, ['Max. write data block length', 'WRITE_BL_LEN']])
+        self.putf(114, 114, [Ann.R_CSD_WRITE_BL_PARTIAL, ['Partial blocks for write allowed', 'WRITE_BL_PARTIAL']])
+        self.putf(115, 119, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD']])
+        self.putf(120, 120, [Ann.R_CSD_FILE_FORMAT_GRP, ['File format group', 'FILE_FORMAT_GRP']])
+        self.putf(121, 121, [Ann.R_CSD_COPY, ['Copy flag', 'COPY']])
+        self.putf(122, 122, [Ann.R_CSD_PERM_WRITE_PROTECT, ['Permanent write protection', 'PERM_WRITE_PROTECT']])
+        self.putf(123, 123, [Ann.R_CSD_TMP_WRITE_PROTECT, ['Temporary write protection', 'TMP_WRITE_PROTECT']])
+        self.putf(124, 125, [Ann.R_CSD_FILE_FORMAT, ['File format', 'FILE_FORMAT']])
+        self.putf(126, 127, [Ann.R_CSD_RSVD, ['Reserved', 'RSVD', 'R']])
+        self.putf(128, 134, [Ann.R_CSD_CRC, ['CRC', 'CRC', 'C']])
+        self.putf(135, 135, [Ann.R_CSD_ONE, ['Always 1', '1']])
+
     # Response tokens can have one of four formats (depends on content).
     # They can have a total length of 48 or 136 bits.
     # They're sent serially (MSB-first) by the card that the host
@@ -316,7 +376,7 @@ class Decoder(srd.Decoder):
         if not self.get_token_bits(cmd_pin, 48):
             return
         self.handle_common_token_fields()
-        self.putr(Ann.R1)
+        self.putr(Ann.RESPONSE_R1)
         self.puta(0, 31, [Ann.DECODED_F, ['Card status', 'Status', 'S']])
         for i in range(32):
             self.putbit(8 + i, [card_status[31 - i]])
@@ -328,7 +388,7 @@ class Decoder(srd.Decoder):
             return
         self.handle_common_token_fields()
         self.puta(0, 31, [Ann.DECODED_F, ['Card status', 'Status', 'S']])
-        self.putr(Ann.R1B)
+        self.putr(Ann.RESPONSE_R1B)
         self.token, self.state = [], St.GET_COMMAND_TOKEN
 
     def handle_response_r2(self, cmd_pin):
@@ -350,7 +410,14 @@ class Decoder(srd.Decoder):
         self.putf(8, 134, [Ann.F_ARG, ['Argument', 'Arg', 'A']])
         self.putf(135, 135, [Ann.F_END, ['End bit', 'End', 'E']])
         self.putf(8, 134, [Ann.DECODED_F, ['CID/CSD register', 'CID/CSD', 'C']])
-        self.putf(0, 135, [Ann.R2, ['R2']])
+        self.putf(0, 135, [Ann.RESPONSE_R2, ['Response: R2']])
+
+        if self.last_cmd in (Ann.CMD2, Ann.CMD10):
+            self.handle_reg_cid()
+
+        if self.last_cmd == Ann.CMD9:
+            self.handle_reg_csd()
+
         self.token, self.state = [], St.GET_COMMAND_TOKEN
 
     def handle_response_r3(self, cmd_pin):
@@ -363,7 +430,7 @@ class Decoder(srd.Decoder):
         #  - Bits[00:00]: End bit (always 1)
         if not self.get_token_bits(cmd_pin, 48):
             return
-        self.putr(Ann.R3)
+        self.putr(Ann.RESPONSE_R3)
         # Annotations for each individual bit.
         for bit in range(len(self.token)):
             self.putf(bit, bit, [Ann.BIT, ['%d' % self.token[bit].bit]])
@@ -391,7 +458,7 @@ class Decoder(srd.Decoder):
         self.handle_common_token_fields()
         self.puta(0, 15, [Ann.DECODED_F, ['Card status bits', 'Status', 'S']])
         self.puta(16, 31, [Ann.DECODED_F, ['Relative card address', 'RCA', 'R']])
-        self.putr(Ann.R6)
+        self.putr(Ann.RESPONSE_R6)
         self.token, self.state = [], St.GET_COMMAND_TOKEN
 
     def handle_response_r7(self, cmd_pin):
@@ -408,7 +475,7 @@ class Decoder(srd.Decoder):
             return
         self.handle_common_token_fields()
 
-        self.putr(Ann.R7)
+        self.putr(Ann.RESPONSE_R7)
 
         # Arg[31:12]: Reserved bits (all-zero)
         self.puta(12, 31, [Ann.DECODED_F, ['Reserved', 'Res', 'R']])