Subversion Repositories pentevo

Rev

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