Subversion Repositories pentevo

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "_global.h"
  2. #include "_screen.h"
  3. #include "_ps2k.h"
  4. #include "_uart.h"
  5. #include "_sd.h"
  6.  
  7. //-----------------------------------------------------------------------------
  8. const WIND_DESC wind_tsd1 PROGMEM = { 10,10,32, 4,0x9f,0x01 };
  9. const WIND_DESC wind_tsd2 PROGMEM = {  0, 2,53,22,0xdf,0x00 };
  10. //-----------------------------------------------------------------------------
  11.  
  12. void t_sd_log_crlf(void)
  13. {
  14.  uart_crlf();
  15.  if (flags1&ENABLE_SD_LOG)  uart_putchar(0x3b);          // ';'
  16. }
  17.  
  18. //-----------------------------------------------------------------------------
  19.  
  20. void t_sd_scrY_log_crlf(u8 y)
  21. {
  22.  scr_set_cursor(1,y);
  23.  uart_crlf();
  24.  if (flags1&ENABLE_SD_LOG)  uart_putchar(0x3b);          // ';'
  25. }
  26.  
  27. //-----------------------------------------------------------------------------
  28.  
  29. void t_sd_log_acmd41(void)
  30. {
  31.  if (flags1&ENABLE_SD_LOG)
  32.  {
  33.   u8 stored_flags1;
  34.   stored_flags1=flags1;
  35.   flags1&=~(ENABLE_SCR|ENABLE_DIRECTUART);
  36.   flags1|=ENABLE_UART;
  37.   print_msg(msg_tsd_acmd41);
  38.   flags1=stored_flags1;
  39.  }
  40. }
  41.  
  42. //-----------------------------------------------------------------------------
  43.  
  44. void t_sd_log_csup(void)
  45. {
  46.  if (flags1&ENABLE_SD_LOG)
  47.  {
  48.   u8 stored_flags1;
  49.   stored_flags1=flags1;
  50.   flags1&=~(ENABLE_SCR|ENABLE_DIRECTUART);
  51.   flags1|=ENABLE_UART;
  52.   print_msg(msg_tsd_csup);
  53.   flags1=stored_flags1;
  54.  }
  55. }
  56.  
  57. //-----------------------------------------------------------------------------
  58.  
  59. void t_sd_log_csdown(void)
  60. {
  61.  if (flags1&ENABLE_SD_LOG)
  62.  {
  63.   u8 stored_flags1;
  64.   stored_flags1=flags1;
  65.   flags1&=~(ENABLE_SCR|ENABLE_DIRECTUART);
  66.   flags1|=ENABLE_UART;
  67.   print_msg(msg_tsd_csdown);
  68.   flags1=stored_flags1;
  69.  }
  70. }
  71.  
  72. //-----------------------------------------------------------------------------
  73.  
  74. void t_sd_log_cmdxx(u8 data)
  75. {
  76.  if (flags1&ENABLE_SD_LOG)
  77.  {
  78.   u8 stored_flags1;
  79.   stored_flags1=flags1;
  80.   flags1&=~(ENABLE_SCR|ENABLE_DIRECTUART);
  81.   flags1|=ENABLE_UART;
  82.   print_msg(msg_tsd_cmd);
  83.   print_dec99(data);
  84.   flags1=stored_flags1;
  85.  }
  86. }
  87.  
  88. //-----------------------------------------------------------------------------
  89.  
  90. void t_sd_setblklen(void)
  91. {
  92.  // CMD16
  93.  do
  94.  {
  95.   t_sd_log_cmdxx(16);
  96.  }while (sd_cmd_with_arg(0x40|16,512));
  97. }
  98.  
  99. //-----------------------------------------------------------------------------
  100.  
  101. void t_sd_crc_off(void)
  102. {
  103.  // CMD59
  104.  do
  105.  {
  106.   t_sd_log_cmdxx(59);
  107.  }while (sd_cmd_without_arg(0x40|59));
  108. }
  109.  
  110. //-----------------------------------------------------------------------------
  111.  
  112. void t_sd_sensors(void)
  113. {
  114.  scr_set_cursor(0,1);
  115.  scr_set_attr(0x7f);
  116.  print_mlmsg(mlmsg_sensors);
  117.  if (PINB&(1<<PB5))
  118.  {
  119.   scr_set_attr(0x7a);
  120.   print_mlmsg(mlmsg_s_nocard);
  121.  }
  122.  else
  123.  {
  124.   scr_set_attr(0x7c);
  125.   print_mlmsg(mlmsg_s_inserted);
  126.  }
  127.  if (PINB&(1<<PB4))
  128.  {
  129.   scr_set_attr(0x7a);
  130.   print_mlmsg(mlmsg_s_readonly);
  131.  }
  132.  else
  133.  {
  134.   scr_set_attr(0x7c);
  135.   print_mlmsg(mlmsg_s_writeen);
  136.  }
  137. }
  138.  
  139. //-----------------------------------------------------------------------------
  140.  
  141. void t_sd_putcursor(u8 y)
  142. {
  143.  u8 i;
  144.  for (i=0;i<2;i++)
  145.  {
  146.   scr_set_cursor(11,11+i);
  147.   u8 attr;
  148.   if (i==y) attr=0xf0; else attr=0x9f;
  149.   scr_fill_attr(attr,30);
  150.  }
  151.  scr_set_cursor(13,12);
  152.  u8 ch;
  153.  if (flags1&ENABLE_SD_LOG) ch=0xfb; else ch=0x20;        // '√'
  154.  scr_putchar(ch);
  155. }
  156.  
  157. //-----------------------------------------------------------------------------
  158.  
  159. void t_sd_complete_and_waitkey(u8 y)
  160. {
  161.  t_sd_scrY_log_crlf(y);
  162.  print_mlmsg(mlmsg_tsd_complete);
  163.  flags1&=~(ENABLE_UART|ENABLE_DIRECTUART);
  164.  flags1|=ENABLE_SCR;
  165.  u16 key;
  166.  while (!(inkey(&key))) t_sd_sensors();
  167. }
  168.  
  169. //-----------------------------------------------------------------------------
  170.  
  171. void testsd(void)
  172. {
  173.  u8 tsd_y, t_sd_attempt, tsd_arg_acmd41, tmp, tbuf[18];
  174.  flags1&=~(ENABLE_SCR|ENABLE_DIRECTUART);
  175.  flags1|=ENABLE_UART;
  176.  t_sd_log_crlf();
  177.  t_sd_log_crlf();
  178.  print_msg(msg_title1);
  179.  print_short_vers();
  180.  t_sd_log_crlf();
  181.  flags1|=ENABLE_SCR;
  182.  t_sd_sensors();
  183.  scr_window(&wind_tsd2);
  184.  scr_set_cursor(1,3); tsd_y=3;
  185.  t_sd_log_crlf();
  186.  print_mlmsg(mlmsg_tsd_init);
  187.  sd_cardtype=0;
  188.  
  189.  t_sd_log_csup();
  190.  fpga_sel_reg(SD_CS1);
  191.  sd_rd_dummy(32);
  192.  t_sd_log_csdown();
  193.  fpga_sel_reg(SD_CS0);
  194.  // CMD0
  195.  t_sd_attempt=255;
  196.  do
  197.  {
  198.   t_sd_attempt--;
  199.   t_sd_log_cmdxx(0);
  200.  }while ( (sd_cmd_without_arg(0x40)!=1) && (t_sd_attempt) );
  201.  
  202.  if (t_sd_attempt==0)
  203.  {
  204.   t_sd_scrY_log_crlf(++tsd_y);
  205.   print_mlmsg(mlmsg_tsd_nocard);
  206.   t_sd_complete_and_waitkey(++tsd_y);
  207.   return;
  208.  }
  209.  // CMD8
  210.  t_sd_log_cmdxx(8);
  211.  sd_rd_dummy(2);
  212.  sd_exchange(0x48);
  213.  sd_exchange(0x00);
  214.  sd_exchange(0x00);
  215.  sd_exchange(0x01);
  216.  sd_exchange(0xaa);
  217.  sd_exchange(0x87);
  218.  if (sd_wait_notff()&0x04) tsd_arg_acmd41=0x00; else tsd_arg_acmd41=0x40;
  219.  sd_rd_dummy(4);
  220.  // ACMD41
  221.  do
  222.  {
  223.   t_sd_log_acmd41();
  224.   sd_cmd_without_arg(0x40|55);
  225.   tmp=sd_cmd_with_1arg(0x40|41,tsd_arg_acmd41<<8);
  226.  }while ( (tmp) && ((tmp&0x04)==0) );
  227.  
  228.  if (tmp)
  229.  {
  230.   // CMD1
  231.   do
  232.   {
  233.    t_sd_log_cmdxx(1);
  234.   }while (sd_cmd_without_arg(0x40|1));
  235.   t_sd_crc_off();
  236.   t_sd_setblklen();
  237.   sd_cardtype=MMCFLAG;
  238.   t_sd_scrY_log_crlf(++tsd_y);
  239.   print_mlmsg(mlmsg_tsd_foundcard);
  240.   print_msg(msg_tsd_mmc);
  241.  }
  242.  else
  243.  {
  244.   t_sd_crc_off();
  245.   t_sd_setblklen();
  246.   if (tsd_arg_acmd41==0)
  247.   {
  248.    sd_cardtype=SDV1FLAG;
  249.    t_sd_scrY_log_crlf(++tsd_y);
  250.    print_mlmsg(mlmsg_tsd_foundcard);
  251.    print_msg(msg_tsd_sdv1);
  252.   }
  253.   else
  254.   {
  255.    // CMD58
  256.    t_sd_log_cmdxx(58);
  257.    sd_cmd_without_arg(0x40|58);
  258.    u8 i;
  259.    for (i=0;i<6;i++) tbuf[i]=sd_receive();
  260.    if (tbuf[0]&0x40)
  261.    {
  262.     sd_cardtype=SDV2FLAG|SDHCFLAG;
  263.     t_sd_scrY_log_crlf(++tsd_y);
  264.     print_mlmsg(mlmsg_tsd_foundcard);
  265.     print_msg(msg_tsd_sdhc);
  266.    }
  267.    else
  268.    {
  269.     sd_cardtype=SDV2FLAG;
  270.     t_sd_scrY_log_crlf(++tsd_y);
  271.     print_mlmsg(mlmsg_tsd_foundcard);
  272.     print_msg(msg_tsd_sdhc);
  273.    }
  274.    t_sd_scrY_log_crlf(++tsd_y);
  275.    print_msg(msg_tsd_ocr);
  276.    for (i=0;i<4;i++) print_hexbyte_for_dump(tbuf[i]);
  277.   }
  278.  }
  279.  
  280.  fpga_sel_reg(SD_CS0);
  281.  // CMD9
  282.  t_sd_log_cmdxx(9);
  283.  if (sd_cmd_without_arg(0x40|9)==0)
  284.  {
  285.   if (sd_wait_notff()!=0xff)
  286.   {
  287.    u8 i;
  288.    for (i=0;i<18;i++) tbuf[i]=sd_receive();
  289.    t_sd_scrY_log_crlf(++tsd_y);
  290.    print_msg(msg_tsd_csd);
  291.    for (i=0;i<15;i++) print_hexbyte_for_dump(tbuf[i]);
  292.   }
  293.  }
  294.  
  295.  fpga_sel_reg(SD_CS0);
  296.  // CMD10
  297.  t_sd_log_cmdxx(10);
  298.  if (sd_cmd_without_arg(0x40|10)==0)
  299.  {
  300.   if (sd_wait_notff()!=0xff)
  301.   {
  302.    u8 i;
  303.    for (i=0;i<18;i++) tbuf[i]=sd_receive();
  304.    t_sd_scrY_log_crlf(++tsd_y);
  305.    print_msg(msg_tsd_cid0);
  306.    for (i=0;i<15;i++) print_hexbyte_for_dump(tbuf[i]);
  307.  
  308.    t_sd_scrY_log_crlf(++tsd_y);
  309.    print_msg(msg_tsd_cid1);
  310.    print_hexbyte(tbuf[0]);
  311.  
  312.    t_sd_scrY_log_crlf(++tsd_y);
  313.    print_msg(msg_tsd_cid2);
  314.  
  315.    if (sd_cardtype&MMCFLAG)
  316.    {
  317.     print_hexbyte(tbuf[1]);
  318.     print_hexbyte(tbuf[2]);
  319.  
  320.     t_sd_scrY_log_crlf(++tsd_y);
  321.     print_msg(msg_tsd_cid3);
  322.     for (i=3;i<9;i++) put_char_for_dump(tbuf[i]);
  323.  
  324.     t_sd_scrY_log_crlf(++tsd_y);
  325.     print_msg(msg_tsd_cid4);
  326.     print_hexhalf(__builtin_avr_swap(tbuf[9]));
  327.     put_char('.');
  328.     print_hexhalf(tbuf[9]);
  329.  
  330.     t_sd_scrY_log_crlf(++tsd_y);
  331.     print_msg(msg_tsd_cid5);
  332.     for (i=10;i<14;i++) print_hexbyte(tbuf[i]);
  333.  
  334.     t_sd_scrY_log_crlf(++tsd_y);
  335.     print_msg(msg_tsd_cid6);
  336.     print_dec99((__builtin_avr_swap(tbuf[14]))&0x0f);
  337.     tmp=tbuf[14]&0x0f;
  338.     if (tmp<3)
  339.     {
  340.      print_msg(msg_tsd_cid6c);
  341.      tmp+=97;
  342.     }
  343.     else
  344.     {
  345.      print_msg(msg_tsd_cid6b);
  346.      tmp-=3;
  347.     }
  348.     print_dec99(tmp);
  349.    }
  350.    else
  351.    {
  352.     put_char_for_dump(tbuf[1]);
  353.     put_char_for_dump(tbuf[2]);
  354.  
  355.     t_sd_scrY_log_crlf(++tsd_y);
  356.     print_msg(msg_tsd_cid3);
  357.     for (i=3;i<8;i++) put_char_for_dump(tbuf[i]);
  358.  
  359.     t_sd_scrY_log_crlf(++tsd_y);
  360.     print_msg(msg_tsd_cid4);
  361.     print_hexhalf(__builtin_avr_swap(tbuf[8]));
  362.     put_char('.');
  363.     print_hexhalf(tbuf[8]);
  364.  
  365.     t_sd_scrY_log_crlf(++tsd_y);
  366.     print_msg(msg_tsd_cid5);
  367.     for (i=9;i<13;i++) print_hexbyte(tbuf[i]);
  368.  
  369.     t_sd_scrY_log_crlf(++tsd_y);
  370.     print_msg(msg_tsd_cid6);
  371.     print_dec99(tbuf[14]&0x0f);
  372.     print_msg(msg_tsd_cid6b);
  373.     tmp=(tbuf[14]&0xf0) | (tbuf[13]&0x0f);
  374.     tmp=__builtin_avr_swap(tmp);
  375.     print_dec99(tmp);
  376.    }
  377.   }
  378.  }
  379.  
  380.  
  381.  
  382.  t_sd_complete_and_waitkey(++tsd_y);
  383. }
  384.  
  385. //-----------------------------------------------------------------------------
  386.  
  387. void Test_SD_MMC(void)
  388. {
  389.  u8 tsd_y, go2;
  390.  do
  391.  {
  392.   flags1&=~(ENABLE_UART|ENABLE_DIRECTUART);
  393.   flags1|=ENABLE_SCR;
  394.   scr_fade();
  395.   scr_set_cursor(0,1);
  396.   scr_fill_char_attr(0x20,0x7f,53);
  397.   scr_window(&wind_tsd1);
  398.   scr_print_mlmsg(mlmsg_tsd_menu);
  399.   tsd_y=0;
  400.   t_sd_putcursor(tsd_y);
  401.   go2=GO_READKEY;
  402.   do
  403.   {
  404.    u16 key;
  405.    while (!(inkey(&key))) t_sd_sensors();
  406.    switch ((u8)(key>>8))
  407.    {
  408.     case KEY_UP:
  409.       tsd_y=0;
  410.       t_sd_putcursor(tsd_y);
  411.       break;
  412.     case KEY_DOWN:
  413.       tsd_y=1;
  414.       t_sd_putcursor(tsd_y);
  415.       break;
  416.     case KEY_ENTER:
  417.       if (tsd_y)
  418.       {
  419.        flags1^=ENABLE_SD_LOG;
  420.        t_sd_putcursor(tsd_y);
  421.       }
  422.       else
  423.       {
  424.        testsd();
  425.        go2=GO_RESTART;
  426.       }
  427.       break;
  428.     case KEY_ESC:
  429.       if (!((u8)key&(1<<PS2K_BIT_EXTKEY)))
  430.       go2=GO_EXIT;
  431.    }
  432.   }while (go2==GO_READKEY);
  433.  
  434.  }while (go2!=GO_EXIT);
  435.  
  436.  flags1&=~ENABLE_SD_LOG;
  437. }
  438.  
  439. //-----------------------------------------------------------------------------
  440.