usb.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. # greaseweazle/usb.py
  2. #
  3. # Written & released by Keir Fraser <keir.xen@gmail.com>
  4. #
  5. # This is free and unencumbered software released into the public domain.
  6. # See the file COPYING for more details, or visit <http://unlicense.org>.
  7. import struct
  8. import itertools as it
  9. from greaseweazle import version
  10. from greaseweazle import error
  11. from greaseweazle.flux import Flux
  12. from greaseweazle import optimised
  13. EARLIEST_SUPPORTED_FIRMWARE = (0, 25)
  14. ## Control-Path command set
  15. class ControlCmd:
  16. ClearComms = 10000
  17. Normal = 9600
  18. ## Command set
  19. class Cmd:
  20. GetInfo = 0
  21. Update = 1
  22. Seek = 2
  23. Head = 3
  24. SetParams = 4
  25. GetParams = 5
  26. Motor = 6
  27. ReadFlux = 7
  28. WriteFlux = 8
  29. GetFluxStatus = 9
  30. GetIndexTimes = 10
  31. SwitchFwMode = 11
  32. Select = 12
  33. Deselect = 13
  34. SetBusType = 14
  35. SetPin = 15
  36. Reset = 16
  37. EraseFlux = 17
  38. SourceBytes = 18
  39. SinkBytes = 19
  40. str = {
  41. GetInfo: "GetInfo",
  42. Update: "Update",
  43. Seek: "Seek",
  44. Head: "Head",
  45. SetParams: "SetParams",
  46. GetParams: "GetParams",
  47. Motor: "Motor",
  48. ReadFlux: "ReadFlux",
  49. WriteFlux: "WriteFlux",
  50. GetFluxStatus: "GetFluxStatus",
  51. GetIndexTimes: "GetIndexTimes",
  52. SwitchFwMode: "SwitchFwMode",
  53. Select: "Select",
  54. Deselect: "Deselect",
  55. SetBusType: "SetBusType",
  56. SetPin: "SetPin",
  57. Reset: "Reset",
  58. EraseFlux: "EraseFlux",
  59. SourceBytes: "SourceBytes",
  60. SinkBytes: "SinkBytes"
  61. }
  62. ## Command responses/acknowledgements
  63. class Ack:
  64. Okay = 0
  65. BadCommand = 1
  66. NoIndex = 2
  67. NoTrk0 = 3
  68. FluxOverflow = 4
  69. FluxUnderflow = 5
  70. Wrprot = 6
  71. NoUnit = 7
  72. NoBus = 8
  73. BadUnit = 9
  74. BadPin = 10
  75. BadCylinder = 11
  76. str = {
  77. Okay: "Okay",
  78. BadCommand: "Bad Command",
  79. NoIndex: "No Index",
  80. NoTrk0: "Track 0 not found",
  81. FluxOverflow: "Flux Overflow",
  82. FluxUnderflow: "Flux Underflow",
  83. Wrprot: "Disk is Write Protected",
  84. NoUnit: "No drive unit selected",
  85. NoBus: "No bus type (eg. Shugart, IBM/PC) specified",
  86. BadUnit: "Invalid unit number",
  87. BadPin: "Not a modifiable pin",
  88. BadCylinder: "Invalid cylinder"
  89. }
  90. ## Cmd.GetInfo indexes
  91. class GetInfo:
  92. Firmware = 0
  93. BandwidthStats = 1
  94. ## Cmd.{Get,Set}Params indexes
  95. class Params:
  96. Delays = 0
  97. ## Cmd.SetBusType values
  98. class BusType:
  99. Invalid = 0
  100. IBMPC = 1
  101. Shugart = 2
  102. ## Flux read stream opcodes, preceded by 0xFF byte
  103. class FluxOp:
  104. Index = 1
  105. Space = 2
  106. Astable = 3
  107. ## CmdError: Encapsulates a command acknowledgement.
  108. class CmdError(Exception):
  109. def __init__(self, cmd, code):
  110. self.cmd = cmd
  111. self.code = code
  112. def cmd_str(self):
  113. return Cmd.str.get(self.cmd[0], "UnknownCmd")
  114. def errcode_str(self):
  115. if self.code == Ack.BadCylinder:
  116. s = Ack.str[Ack.BadCylinder]
  117. return s + " %d" % struct.unpack('2Bb', self.cmd)[2]
  118. return Ack.str.get(self.code, "Unknown Error (%u)" % self.code)
  119. def __str__(self):
  120. return "%s: %s" % (self.cmd_str(), self.errcode_str())
  121. class Unit:
  122. ## Unit information, instance variables:
  123. ## major, minor: Greaseweazle firmware version number
  124. ## max_cmd: Maximum Cmd number accepted by this unit
  125. ## sample_freq: Resolution of all time values passed to/from this unit
  126. ## update_mode: True iff the Greaseweazle unit is in update mode
  127. ## Unit(ser):
  128. ## Accepts a Pyserial instance for Greaseweazle communications.
  129. def __init__(self, ser):
  130. self.ser = ser
  131. self.reset()
  132. # Copy firmware info to instance variables (see above for definitions).
  133. self._send_cmd(struct.pack("3B", Cmd.GetInfo, 3, GetInfo.Firmware))
  134. x = struct.unpack("<4BI3B21x", self.ser.read(32))
  135. (self.major, self.minor, is_main_firmware,
  136. self.max_cmd, self.sample_freq, self.hw_model,
  137. self.hw_submodel, self.usb_speed) = x
  138. self.version = (self.major, self.minor)
  139. # Old firmware doesn't report HW type but runs on STM32F1 only.
  140. if self.hw_model == 0:
  141. self.hw_model = 1
  142. # Check whether firmware is in update mode: limited command set if so.
  143. self.update_mode = (is_main_firmware == 0)
  144. if self.update_mode:
  145. self.update_jumpered = (self.sample_freq & 1)
  146. del self.sample_freq
  147. return
  148. # We are running main firmware: Check whether an update is needed.
  149. # We can use only the GetInfo command if the firmware is out of date.
  150. self.update_needed = (self.version < EARLIEST_SUPPORTED_FIRMWARE or
  151. self.version > (version.major, version.minor))
  152. if self.update_needed:
  153. return
  154. # Initialise the delay properties with current firmware values.
  155. self._send_cmd(struct.pack("4B", Cmd.GetParams, 4, Params.Delays, 10))
  156. (self._select_delay, self._step_delay,
  157. self._seek_settle_delay, self._motor_delay,
  158. self._watchdog_delay) = struct.unpack("<5H", self.ser.read(10))
  159. ## reset:
  160. ## Resets communications with Greaseweazle.
  161. def reset(self):
  162. self.ser.reset_output_buffer()
  163. self.ser.baudrate = ControlCmd.ClearComms
  164. self.ser.baudrate = ControlCmd.Normal
  165. self.ser.reset_input_buffer()
  166. self.ser.close()
  167. self.ser.open()
  168. ## _send_cmd:
  169. ## Send given command byte sequence to Greaseweazle.
  170. ## Raise a CmdError if command fails.
  171. def _send_cmd(self, cmd):
  172. self.ser.write(cmd)
  173. (c,r) = struct.unpack("2B", self.ser.read(2))
  174. error.check(c == cmd[0], "Command returned garbage (%02x != %02x)"
  175. % (c, cmd[0]))
  176. if r != 0:
  177. raise CmdError(cmd, r)
  178. ## seek:
  179. ## Seek the selected drive's heads to the specified track (cyl, head).
  180. def seek(self, cyl, head):
  181. self._send_cmd(struct.pack("2Bb", Cmd.Seek, 3, cyl))
  182. self._send_cmd(struct.pack("3B", Cmd.Head, 3, head))
  183. ## set_bus_type:
  184. ## Set the floppy bus type.
  185. def set_bus_type(self, type):
  186. self._send_cmd(struct.pack("3B", Cmd.SetBusType, 3, type))
  187. ## set_pin:
  188. ## Set a pin level.
  189. def set_pin(self, pin, level):
  190. self._send_cmd(struct.pack("4B", Cmd.SetPin, 4, pin, int(level)))
  191. ## power_on_reset:
  192. ## Re-initialise to power-on defaults.
  193. def power_on_reset(self):
  194. self._send_cmd(struct.pack("2B", Cmd.Reset, 2))
  195. ## drive_select:
  196. ## Select the specified drive unit.
  197. def drive_select(self, unit):
  198. self._send_cmd(struct.pack("3B", Cmd.Select, 3, unit))
  199. ## drive_deselect:
  200. ## Deselect currently-selected drive unit (if any).
  201. def drive_deselect(self):
  202. self._send_cmd(struct.pack("2B", Cmd.Deselect, 2))
  203. ## drive_motor:
  204. ## Turn the specified drive's motor on/off.
  205. def drive_motor(self, unit, state):
  206. self._send_cmd(struct.pack("4B", Cmd.Motor, 4, unit, int(state)))
  207. ## switch_fw_mode:
  208. ## Switch between update bootloader and main firmware.
  209. def switch_fw_mode(self, mode):
  210. self._send_cmd(struct.pack("3B", Cmd.SwitchFwMode, 3, int(mode)))
  211. ## update_firmware:
  212. ## Update Greaseweazle to the given new firmware.
  213. def update_firmware(self, dat):
  214. self._send_cmd(struct.pack("<2BI", Cmd.Update, 6, len(dat)))
  215. self.ser.write(dat)
  216. (ack,) = struct.unpack("B", self.ser.read(1))
  217. return ack
  218. ## update_bootloader:
  219. ## Update Greaseweazle with the given new bootloader.
  220. def update_bootloader(self, dat):
  221. self._send_cmd(struct.pack("<2B2I", Cmd.Update, 10,
  222. len(dat), 0xdeafbee3))
  223. self.ser.write(dat)
  224. (ack,) = struct.unpack("B", self.ser.read(1))
  225. return ack
  226. ## _decode_flux:
  227. ## Decode the Greaseweazle data stream into a list of flux samples.
  228. def _decode_flux(self, dat):
  229. flux, index = [], []
  230. assert dat[-1] == 0
  231. dat_i = it.islice(dat, 0, len(dat)-1)
  232. ticks, ticks_since_index = 0, 0
  233. def _read_28bit():
  234. val = (next(dat_i) & 254) >> 1
  235. val += (next(dat_i) & 254) << 6
  236. val += (next(dat_i) & 254) << 13
  237. val += (next(dat_i) & 254) << 20
  238. return val
  239. try:
  240. while True:
  241. i = next(dat_i)
  242. if i == 255:
  243. opcode = next(dat_i)
  244. if opcode == FluxOp.Index:
  245. val = _read_28bit()
  246. index.append(ticks_since_index + ticks + val)
  247. ticks_since_index = -(ticks + val)
  248. elif opcode == FluxOp.Space:
  249. ticks += _read_28bit()
  250. else:
  251. raise error.Fatal("Bad opcode in flux stream (%d)"
  252. % opcode)
  253. else:
  254. if i < 250:
  255. val = i
  256. else:
  257. val = 250 + (i - 250) * 255
  258. val += next(dat_i) - 1
  259. ticks += val
  260. flux.append(ticks)
  261. ticks_since_index += ticks
  262. ticks = 0
  263. except StopIteration:
  264. pass
  265. return flux, index
  266. ## _encode_flux:
  267. ## Convert the given flux timings into an encoded data stream.
  268. def _encode_flux(self, flux):
  269. nfa_thresh = round(150e-6 * self.sample_freq) # 150us
  270. nfa_period = round(1.25e-6 * self.sample_freq) # 1.25us
  271. dat = bytearray()
  272. def _write_28bit(x):
  273. dat.append(1 | (x<<1) & 255)
  274. dat.append(1 | (x>>6) & 255)
  275. dat.append(1 | (x>>13) & 255)
  276. dat.append(1 | (x>>20) & 255)
  277. # Emit a dummy final flux value. This is never written to disk because
  278. # the write is aborted immediately the final flux is loaded into the
  279. # WDATA timer. The dummy flux is sacrificial, ensuring that the real
  280. # final flux gets written in full.
  281. dummy_flux = round(100e-6 * self.sample_freq)
  282. for val in it.chain(flux, [dummy_flux]):
  283. if val == 0:
  284. pass
  285. elif val < 250:
  286. dat.append(val)
  287. elif val > nfa_thresh:
  288. dat.append(255)
  289. dat.append(FluxOp.Space)
  290. _write_28bit(val)
  291. dat.append(255)
  292. dat.append(FluxOp.Astable)
  293. _write_28bit(nfa_period)
  294. else:
  295. high = (val-250) // 255
  296. if high < 5:
  297. dat.append(250 + high)
  298. dat.append(1 + (val-250) % 255)
  299. else:
  300. dat.append(255)
  301. dat.append(FluxOp.Space)
  302. _write_28bit(val - 249)
  303. dat.append(249)
  304. dat.append(0) # End of Stream
  305. return dat
  306. ## _read_track:
  307. ## Private helper which issues command requests to Greaseweazle.
  308. def _read_track(self, revs, ticks):
  309. # Request and read all flux timings for this track.
  310. dat = bytearray()
  311. self._send_cmd(struct.pack("<2BIH", Cmd.ReadFlux, 8,
  312. ticks, revs+1))
  313. while True:
  314. dat += self.ser.read(1)
  315. dat += self.ser.read(self.ser.in_waiting)
  316. if dat[-1] == 0:
  317. break
  318. # Check flux status. An exception is raised if there was an error.
  319. self._send_cmd(struct.pack("2B", Cmd.GetFluxStatus, 2))
  320. return dat
  321. ## read_track:
  322. ## Read and decode flux and index timings for the current track.
  323. def read_track(self, revs, ticks=0, nr_retries=5):
  324. retry = 0
  325. while True:
  326. try:
  327. dat = self._read_track(revs, ticks)
  328. except CmdError as error:
  329. # An error occurred. We may retry on transient overflows.
  330. if error.code == Ack.FluxOverflow and retry < nr_retries:
  331. retry += 1
  332. else:
  333. raise error
  334. else:
  335. # Success!
  336. break
  337. try:
  338. # Decode the flux list and read the index-times list.
  339. flux_list, index_list = optimised.decode_flux(dat)
  340. except AttributeError:
  341. flux_list, index_list = self._decode_flux(dat)
  342. # Success: Return the requested full index-to-index revolutions.
  343. return Flux(index_list, flux_list, self.sample_freq, index_cued=False)
  344. ## write_track:
  345. ## Write the given flux stream to the current track via Greaseweazle.
  346. def write_track(self, flux_list, terminate_at_index,
  347. cue_at_index=True, nr_retries=5):
  348. # Create encoded data stream.
  349. dat = self._encode_flux(flux_list)
  350. retry = 0
  351. while True:
  352. try:
  353. # Write the flux stream to the track via Greaseweazle.
  354. self._send_cmd(struct.pack("4B", Cmd.WriteFlux, 4,
  355. int(cue_at_index),
  356. int(terminate_at_index)))
  357. self.ser.write(dat)
  358. self.ser.read(1) # Sync with Greaseweazle
  359. self._send_cmd(struct.pack("2B", Cmd.GetFluxStatus, 2))
  360. except CmdError as error:
  361. # An error occurred. We may retry on transient underflows.
  362. if error.code == Ack.FluxUnderflow and retry < nr_retries:
  363. retry += 1
  364. else:
  365. raise error
  366. else:
  367. # Success!
  368. break
  369. ## erase_track:
  370. ## Erase the current track via Greaseweazle.
  371. def erase_track(self, ticks):
  372. self._send_cmd(struct.pack("<2BI", Cmd.EraseFlux, 6, int(ticks)))
  373. self.ser.read(1) # Sync with Greaseweazle
  374. self._send_cmd(struct.pack("2B", Cmd.GetFluxStatus, 2))
  375. ## source_bytes:
  376. ## Command Greaseweazle to source 'nr' garbage bytes.
  377. def source_bytes(self, nr):
  378. self._send_cmd(struct.pack("<2BI", Cmd.SourceBytes, 6, nr))
  379. while nr > 0:
  380. self.ser.read(1)
  381. waiting = self.ser.in_waiting
  382. self.ser.read(waiting)
  383. nr -= 1 + waiting
  384. ## sink_bytes:
  385. ## Command Greaseweazle to sink 'nr' garbage bytes.
  386. def sink_bytes(self, nr):
  387. self._send_cmd(struct.pack("<2BI", Cmd.SinkBytes, 6, nr))
  388. dat = bytes(1024*1024)
  389. while nr > len(dat):
  390. self.ser.write(dat)
  391. nr -= len(dat)
  392. self.ser.write(dat[:nr])
  393. self.ser.read(1) # Sync with Greaseweazle
  394. ## bw_stats:
  395. ## Get min/max bandwidth for previous source/sink command. Mbps (float).
  396. def bw_stats(self):
  397. self._send_cmd(struct.pack("3B", Cmd.GetInfo, 3,
  398. GetInfo.BandwidthStats))
  399. min_bytes, min_usecs, max_bytes, max_usecs = struct.unpack(
  400. "<4I16x", self.ser.read(32))
  401. min_bw = (8 * min_bytes) / min_usecs
  402. max_bw = (8 * max_bytes) / max_usecs
  403. return min_bw, max_bw
  404. ##
  405. ## Delay-property public getters and setters:
  406. ## select_delay: Delay (usec) after asserting drive select
  407. ## step_delay: Delay (usec) after issuing a head-step command
  408. ## seek_settle_delay: Delay (msec) after completing a head-seek operation
  409. ## motor_delay: Delay (msec) after turning on drive spindle motor
  410. ## watchdog_delay: Timeout (msec) since last command upon which all
  411. ## drives are deselected and spindle motors turned off
  412. ##
  413. def _set_delays(self):
  414. self._send_cmd(struct.pack("<3B5H", Cmd.SetParams,
  415. 3+5*2, Params.Delays,
  416. self._select_delay, self._step_delay,
  417. self._seek_settle_delay,
  418. self._motor_delay, self._watchdog_delay))
  419. @property
  420. def select_delay(self):
  421. return self._select_delay
  422. @select_delay.setter
  423. def select_delay(self, select_delay):
  424. self._select_delay = select_delay
  425. self._set_delays()
  426. @property
  427. def step_delay(self):
  428. return self._step_delay
  429. @step_delay.setter
  430. def step_delay(self, step_delay):
  431. self._step_delay = step_delay
  432. self._set_delays()
  433. @property
  434. def seek_settle_delay(self):
  435. return self._seek_settle_delay
  436. @seek_settle_delay.setter
  437. def seek_settle_delay(self, seek_settle_delay):
  438. self._seek_settle_delay = seek_settle_delay
  439. self._set_delays()
  440. @property
  441. def motor_delay(self):
  442. return self._motor_delay
  443. @motor_delay.setter
  444. def motor_delay(self, motor_delay):
  445. self._motor_delay = motor_delay
  446. self._set_delays()
  447. @property
  448. def watchdog_delay(self):
  449. return self._watchdog_delay
  450. @watchdog_delay.setter
  451. def watchdog_delay(self, watchdog_delay):
  452. self._watchdog_delay = watchdog_delay
  453. self._set_delays()
  454. # Local variables:
  455. # python-indent: 4
  456. # End: