Advertisement
ZodiacMentor

SD-to-TFT ProMicro

Feb 12th, 2022
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. uint8_t readSDToScreen_320x240i(uint32_t fromBlock)
  2. {
  3.   if (!sdCardInitialized) return;
  4.    
  5.   uint8_t res1 = 0xff;
  6.   uint8_t tries = 0;
  7.    
  8.   while (res1 == 0xff && tries < 32) {
  9.     SPI_transfer(0xff);
  10.     SDCS_LOW();
  11.     SD_command(18, fromBlock, 0);
  12.     res1 = SD_readRes1();
  13.     if (res1 == 0xff) {
  14.       SPI_transfer(0xff);
  15.       SDCS_HIGH();
  16.       tries++;
  17.     }  
  18.   }
  19.  
  20.   cli();
  21.  
  22.   if (tries == 32)
  23.   {
  24.     return SD_ERR_CMD18_FAILED;
  25.   }
  26.  
  27.   asm volatile(
  28.     "ldi    r16,1\n\t"
  29.     "mov    __tmp_reg__,r16\n\t"
  30.     "clr    __zero_reg__\n\t"
  31.     "clr    r22\n\t"
  32.     "clr    r23\n\t"
  33.     "clr    r24\n\t"
  34.     "clr    r25\n\t"
  35.     "clr    r18\n\t"
  36.     "clr    r26\n\t"
  37.     "clr    r27\n\t"
  38.     "ldi    r16, 0xff\n\t"
  39.  
  40.     // wait for datablock start (0xfe)
  41.   "waitForDBStart:\n\t"
  42.     "in     r17, 0x2d\n\t"
  43.     "in     r17, 0x2e\n\t"
  44.     "out    0x2e, r16\n\t"
  45.   "waitForDBStartResponse:\n\t"
  46.     "in     r17, 0x2d\n\t"
  47.     "sbrs   r17, 7\n\t"
  48.     "rjmp   waitForDBStartResponse\n\t"
  49.     "in     r17, 0x2e\n\t"
  50.     "cpi    r17, 0xfe\n\t"
  51.     "brne   waitForDBStart\n\t"
  52.    
  53.     // initiate first SPI transfer of data block
  54.     "out    0x2e, r16\n\t"
  55.     "clr    r20\n\t"
  56.     "clr    r21\n\t"
  57.    
  58.     "nop\n\t"  // wait for the first byte to come through    
  59.     "nop\n\t"  // wait for the first byte to come through
  60.     "nop\n\t"  // wait for the first byte to come through
  61.     "nop\n\t"  // wait for the first byte to come through
  62.     "nop\n\t"  // wait for the first byte to come through
  63.     "nop\n\t"  // wait for the first byte to come through
  64.     "nop\n\t"  // wait for the first byte to come through
  65.     "nop\n\t"  // wait for the first byte to come through
  66.     "nop\n\t"  // wait for the first byte to come through
  67.     "nop\n\t"  // wait for the first byte to come through
  68.     "nop\n\t"  // wait for the first byte to come through
  69.     "nop\n\t"  // wait for the first byte to come through
  70.     "nop\n\t"  // wait for the first byte to come through
  71.     "nop\n\t"  // wait for the first byte to come through
  72.    
  73.   "SPItoScreenLoop:\n\t"
  74.     // Read last byte from SPI
  75.     "in     r17, 0x2e\n\t"
  76.     "push   r17\n\t"
  77.     // initiate next SPI transfer while we output to screen
  78.     "out    0x2e, r16\n\t"
  79.    
  80.     // PORTF
  81.     "out    0x11, r17\n\t"
  82.     // PORTD
  83.     "out    0x0b, r17\n\t"
  84.     // Toggle ILI WR
  85.     "cbi    0x08, 6\n\t"
  86.     "sbi    0x08, 6\n\t"
  87.  
  88.     "rjmp   checkDone\n\t"
  89.    
  90.     "sbrs   r20,0\n\t"
  91.     "rjmp   checkDone\n\t"
  92.    
  93.     "rjmp   checkDone\n\t"
  94.    
  95.     "add    r18, __tmp_reg__\n\t"
  96.     "adc    r27, __zero_reg__\n\t"
  97.    
  98.     "cpi    r18, 0x40\n\t"
  99.     "brne   checkDone\n\t"
  100.     "cpi    r27, 0x01\n\t"
  101.     "brne   checkDone\n\t"
  102.    
  103.     // next line
  104.     "clr    r18\n\t"
  105.     "clr    r27\n\t"
  106.    
  107.     "subi   r26, 0xfe\n\t"  // Add 2
  108.     "sbrc   r26, 0\n\t"
  109.     "rjmp   handleField1\n\t"
  110.     "cpi    r26, 0xf0\n\t"  // 240 ? reset to 1
  111.     "brne   sendPASET\n\t"
  112.     "ldi    r26, 1\n\t"
  113.     "rjmp   sendPASET\n\t"
  114.   "handleField1:\n\t"
  115.     "cpi    r26, 0xf1\n\t"
  116.     "brne   sendPASET\n\t"  // 241 ? reset to 0
  117.     "clr    r26\n\t"
  118.   "sendPASET:\n\t"
  119.     // Send PASET command 0x2b
  120.     "ldi    r17, 0x2b\n\t"
  121.     "out    0x11, r17\n\t"
  122.     "out    0x0b, r17\n\t"
  123.     "cbi    0x0e, 6\n\t"
  124.     "cbi    0x08, 6\n\t"
  125.     "sbi    0x08, 6\n\t"
  126.     "sbi    0x0e, 6\n\t"
  127.     // Send zero
  128.     "out    0x11, __zero_reg__\n\t"
  129.     "out    0x0b, __zero_reg__\n\t"
  130.     "cbi    0x08, 6\n\t"
  131.     "sbi    0x08, 6\n\t"
  132.     // Send current row as start address
  133.     "out    0x11, r26\n\t"
  134.     "out    0x0b, r26\n\t"
  135.     "cbi    0x08, 6\n\t"
  136.     "sbi    0x08, 6\n\t"
  137.     // Send zero
  138.     "out    0x11, __zero_reg__\n\t"
  139.     "out    0x0b, __zero_reg__\n\t"
  140.     "cbi    0x08, 6\n\t"
  141.     "sbi    0x08, 6\n\t"
  142.     // Send 0xef as end address
  143.     "out    0x11, r26\n\t"
  144.     "out    0x0b, r26\n\t"
  145.     "cbi    0x08, 6\n\t"
  146.     "sbi    0x08, 6\n\t"
  147.    
  148.     // Send RAMWR command 0x2c
  149.     "ldi    r17, 0x2c\n\t"
  150.     "out    0x11, r17\n\t"
  151.     "out    0x0b, r17\n\t"
  152.     "cbi    0x0e, 6\n\t"
  153.     "cbi    0x08, 6\n\t"
  154.     "sbi    0x08, 6\n\t"
  155.     "sbi    0x0e, 6\n\t"
  156.  
  157.   "checkDone:\n\t"
  158.     // Check if we are done
  159.     "add    r20, __tmp_reg__\n\t"
  160.     "adc    r21, __zero_reg__\n\t"
  161.     "cpi    r21,2\n\t" // test against 512
  162.     "brne   jmpToStart\n\t"
  163.     "cpi    r20,0\n\t" // test against 513 as we skip the first write to let us write to screen while waiting for SPI
  164.     "brne   jmpToStart\n\t"
  165.    
  166.     "rjmp   readCRC\n\t"
  167.    
  168.   "jmpToStart:\n\t"
  169.     "rjmp   SPItoScreenLoop\n\t"
  170.  
  171.   "readCRC:\n\t"
  172.     "in     r17, 0x2d\n\t"
  173.     "in     r17, 0x2e\n\t"
  174.     "out    0x2e, r16\n\t"
  175.   "readCRCResponse:\n\t"
  176.     "in     r17, 0x2d\n\t"
  177.     "sbrs   r17, 7\n\t"
  178.     "rjmp   readCRCResponse\n\t"
  179.  
  180.   "checkLoop:\n\t"
  181.     "add    r22, __tmp_reg__\n\t"
  182.     "adc    r23, __zero_reg__\n\t"
  183.     "adc    r24, __zero_reg__\n\t"
  184.     "cpi    r22, 0xbe\n\t"
  185.     "brne   jumpToDBStart\n\t"
  186.     "cpi    r23, 0xd3\n\t"
  187.     "brne   jumpToDBStart\n\t"
  188.     "cpi    r24, 0x04\n\t"
  189.     "brne   jumpToDBStart\n\t"
  190.     "rjmp   end\n\t"
  191.    
  192.   "jumpToDBStart:\n\t"
  193.     "rjmp   waitForDBStart\n\t"
  194.  
  195.   "end:\n\t"
  196.     :
  197.     :
  198.     : "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", "r24", "r25", "r26", "r27"
  199.   );
  200.   sei();
  201.  
  202.   SDCS_HIGH();
  203.  
  204.   SD_CMD12();
  205.  
  206.   return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement