SdInspect.Mod 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. MODULE SdInspect; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT
  3. BootConfig, Commands, KernelLog, Options,
  4. Sd;
  5. VAR
  6. hc: Sd.HostController;
  7. card: Sd.Card;
  8. result: LONGINT;
  9. (** SdInspect.Command [-a|--acmd] [-d|--data] [-r|--read] [--auto12] [--auto23] [-m|-multi] [-c|--count] [--dma] cmd arg resp rca [datalen] ~ *)
  10. PROCEDURE Command * (context: Commands.Context);
  11. VAR
  12. rsp: ARRAY 32 OF CHAR;
  13. opt: Options.Options;
  14. command: Sd.Command;
  15. datalen, rca: LONGINT;
  16. data: POINTER TO ARRAY OF CHAR;
  17. BEGIN
  18. NEW(opt);
  19. opt.Add('d', "data", Options.Flag);
  20. opt.Add('r', "read", Options.Flag);
  21. opt.Add(0X, "auto12", Options.Flag);
  22. opt.Add(0X, "auto23", Options.Flag);
  23. opt.Add('m', "multi", Options.Flag);
  24. opt.Add('c', "count", Options.Flag);
  25. opt.Add(0X, "dma", Options.Flag);
  26. opt.Add('a', "acmd", Options.Flag);
  27. IF ~opt.Parse(context.arg, context.error) THEN RETURN END;
  28. command.hc := hc;
  29. IF context.arg.GetInteger(command.command, FALSE) THEN
  30. IF context.arg.GetInteger(command.argument, TRUE) THEN
  31. IF context.arg.GetString(rsp) THEN
  32. IF rsp = "R1" THEN
  33. command.responseType := Sd.ResponseR1
  34. ELSIF rsp = "R1b" THEN
  35. command.responseType := Sd.ResponseR1b
  36. ELSIF rsp = "R2" THEN
  37. command.responseType := Sd.ResponseR2
  38. ELSIF rsp = "R3" THEN
  39. command.responseType := Sd.ResponseR3
  40. ELSIF rsp = "R4" THEN
  41. command.responseType := Sd.ResponseR4
  42. ELSIF rsp = "R5" THEN
  43. command.responseType := Sd.ResponseR5
  44. ELSIF rsp = "R6" THEN
  45. command.responseType := Sd.ResponseR6
  46. ELSIF rsp = "R7" THEN
  47. command.responseType := Sd.ResponseR7
  48. ELSE
  49. context.error.String("Unknown response type ");
  50. context.error.String(rsp);
  51. context.error.Ln;
  52. RETURN
  53. END;
  54. IF context.arg.GetInteger(command.rca, TRUE) THEN
  55. (* flags *)
  56. IF opt.GetFlag("read") THEN INCL(command.flags, Sd.FlagRead) END;
  57. IF opt.GetFlag("auto12") THEN INCL(command.flags, Sd.FlagAutoCmd12) END;
  58. IF opt.GetFlag("auto23") THEN INCL(command.flags, Sd.FlagAutoCmd23) END;
  59. IF opt.GetFlag("multi") THEN INCL(command.flags, Sd.FlagMultipleBlockTx) END;
  60. IF opt.GetFlag("count") THEN INCL(command.flags, Sd.FlagCountBlocks) END;
  61. IF opt.GetFlag("dma") THEN INCL(command.flags, Sd.FlagUseDma) END;
  62. IF opt.GetFlag("acmd") THEN INCL(command.flags, Sd.FlagApplicationCmd) END;
  63. IF opt.GetFlag("data") THEN
  64. INCL(command.flags, Sd.FlagData);
  65. IF context.arg.GetInteger(datalen, TRUE) THEN
  66. NEW(data, datalen);
  67. IF hc.execute(command, data^, 0, datalen, result) THEN
  68. context.out.String("Command successful");
  69. context.out.Ln;
  70. context.out.Update;
  71. IF Sd.FlagRead IN command.flags THEN
  72. KernelLog.String("Data:");
  73. KernelLog.Ln;
  74. KernelLog.Buffer(data^, 0, datalen)
  75. END
  76. ELSE
  77. context.out.String("Command failed: ");
  78. context.out.Int(result, 0);
  79. context.out.Ln
  80. END
  81. ELSE
  82. context.error.String("Expected data length");
  83. context.error.Ln
  84. END
  85. ELSE
  86. IF Sd.ExecuteCommand(command, result) THEN
  87. context.out.String("Command successful");
  88. context.out.Ln
  89. ELSE
  90. context.out.String("Command failed: ");
  91. context.out.Int(result, 0);
  92. context.out.Ln
  93. END
  94. END;
  95. context.out.String("Response: ");
  96. CASE command.responseType OF
  97. Sd.ResponseR1, Sd.ResponseR1b:
  98. context.out.Update;
  99. Sd.PrintCardStatus(Sd.GetR1(command))
  100. |Sd.ResponseR2:
  101. context.out.Hex(command.response[0], -8);
  102. context.out.Char(' ');
  103. context.out.Hex(command.response[1], -8);
  104. context.out.Char(' ');
  105. context.out.Hex(command.response[2], -8);
  106. context.out.Char(' ');
  107. context.out.Hex(command.response[3], -8);
  108. context.out.Char(' ')
  109. |Sd.ResponseR3: context.out.Hex(Sd.GetR3(command), -8)
  110. |Sd.ResponseR4: context.out.Hex(Sd.GetR4(command), -8)
  111. |Sd.ResponseR5: context.out.Hex(Sd.GetR5(command), -8)
  112. |Sd.ResponseR6: context.out.Hex(Sd.GetR6(command), -8)
  113. |Sd.ResponseR7: context.out.Hex(Sd.GetR7(command), -8)
  114. END;
  115. context.out.Ln
  116. ELSE
  117. context.error.String("Expected RCA");
  118. context.error.Ln
  119. END
  120. ELSE
  121. context.error.String("Expected response type");
  122. context.error.Ln
  123. END
  124. ELSE
  125. context.error.String("Expected argument number");
  126. context.error.Ln
  127. END
  128. ELSE
  129. context.error.String("Expected cmd number");
  130. context.error.Ln
  131. END
  132. END Command;
  133. PROCEDURE Read *;
  134. VAR
  135. data: ARRAY 512 OF CHAR;
  136. res: LONGINT;
  137. BEGIN
  138. IF ~Sd.Read(card, 0, 512, data, 0, res) THEN
  139. KernelLog.String("Read Failed: ");
  140. KernelLog.Int(res, 0);
  141. KernelLog.Ln
  142. ELSE
  143. KernelLog.String("Read succeeded");
  144. KernelLog.Ln;
  145. KernelLog.Buffer(data, 0, 512)
  146. END
  147. END Read;
  148. PROCEDURE Reads *;
  149. CONST
  150. Size = 1024 * 1024;
  151. VAR
  152. data: ARRAY Size OF CHAR;
  153. res: LONGINT;
  154. BEGIN
  155. IF ~Sd.Read(card, 0, Size, data, 0, res) THEN
  156. KernelLog.String("Read Failed: ");
  157. KernelLog.Int(res, 0);
  158. KernelLog.Ln
  159. ELSE
  160. KernelLog.String("Read succeeded");
  161. KernelLog.Ln;
  162. KernelLog.Buffer(data, 0, 512)
  163. END
  164. END Reads;
  165. PROCEDURE Write *;
  166. VAR
  167. data: ARRAY 512 OF CHAR;
  168. res: LONGINT;
  169. BEGIN
  170. IF ~Sd.Write(card, 0, 512, data, 0, res) THEN
  171. KernelLog.String("Write Failed: ");
  172. KernelLog.Int(res, 0);
  173. KernelLog.Ln
  174. ELSE
  175. KernelLog.String("Write succeeded");
  176. KernelLog.Ln;
  177. KernelLog.Buffer(data, 0, 512)
  178. END
  179. END Write;
  180. PROCEDURE Writes *;
  181. VAR
  182. data: ARRAY 8 * 512 OF CHAR;
  183. res: LONGINT;
  184. BEGIN
  185. IF ~Sd.Write(card, 0, 8 * 512, data, 0, res) THEN
  186. KernelLog.String("Write Failed: ");
  187. KernelLog.Int(res, 0);
  188. KernelLog.Ln
  189. ELSE
  190. KernelLog.String("Write succeeded");
  191. KernelLog.Ln;
  192. KernelLog.Buffer(data, 0, 512)
  193. END
  194. END Writes;
  195. PROCEDURE Test *;
  196. VAR
  197. command: Sd.Command;
  198. res: LONGINT;
  199. BEGIN
  200. command.hc := hc;
  201. command.rca := 7;
  202. command.command := Sd.CMD_SELECT_DESELECT_CARD;
  203. command.argument := 70000H;
  204. command.responseType := Sd.ResponseR1b;
  205. TRACE(Sd.ExecuteCommand(command, res));
  206. TRACE(res)
  207. END Test;
  208. BEGIN
  209. hc := Sd.New(ADDRESS(0E0100000H), BootConfig.GetIntValue("PsRefClockHz"), NIL, NIL);
  210. NEW(card);
  211. END SdInspect.