USBMicroX.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #ifndef _USBMICROX_H_
  2. #define _USBMICROX_H_
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <libgen.h>
  8. #include <mach/mach.h>
  9. #include <sys/types.h>
  10. #include <CoreFoundation/CFNumber.h>
  11. #include <CoreFoundation/CoreFoundation.h>
  12. #include <IOKit/IOKitLib.h>
  13. #include <IOKit/IOCFPlugIn.h>
  14. #include <IOKit/usb/IOUSBLib.h>
  15. #include <IOKit/hid/IOHIDLib.h>
  16. #include <IOKit/hid/IOHIDKeys.h>
  17. #define USBMX_LOG(S, ...) \
  18. ({ fprintf(stderr, "/%s/%s/%d >>> " S "\n", basename(__FILE__) , \
  19. __FUNCTION__ , __LINE__ , ## __VA_ARGS__ ); })
  20. #define USBMX_CMD(...) (u_char[8]){ __VA_ARGS__ }
  21. #define USBMX_ALLOC(TYPE) (TYPE*)malloc(sizeof(TYPE))
  22. #define USBMX_DEBUG_LOG 0
  23. #define USBMX_RESPONSE_BUFFER_SIZE 8
  24. struct _USBmX_Context;
  25. typedef struct _USBmX_Device {
  26. struct _USBmX_Context *ctx;
  27. IOHIDDeviceInterface **interface;
  28. CFMutableDictionaryRef properties;
  29. const char *serialNumber;
  30. const char *deviceName;
  31. const char *manufacturer;
  32. int response_ready;
  33. u_char response_buffer[USBMX_RESPONSE_BUFFER_SIZE];
  34. struct _USBmX_Device *next;
  35. void *userData;
  36. } USBmX_Device;
  37. typedef USBmX_Device* USBmX_DeviceRef;
  38. typedef void (*USBmX_DeviceAddedProc)(USBmX_DeviceRef dev, void *cookie);
  39. typedef void (*USBmX_DeviceRemovedProc)(USBmX_DeviceRef dev, void *cookie);
  40. typedef struct _USBmX_Context {
  41. USBmX_DeviceRef deviceList;
  42. CFMutableDictionaryRef matchingDict;
  43. USBmX_DeviceAddedProc deviceAdded;
  44. USBmX_DeviceRemovedProc deviceRemoved;
  45. void *deviceAdded_cookie;
  46. void *deviceRemoved_cookie;
  47. } USBmX_Context;
  48. typedef USBmX_Context* USBmX_ContextRef;
  49. /*
  50. * USBmX_Create:
  51. * Creates a USBmX_ContextRef used in all other
  52. * routines. Should return a valid USBmX_ContextRef
  53. * even if no devices are conected.
  54. */
  55. extern USBmX_ContextRef USBmX_Create(void);
  56. extern int USBmX_DeviceAddedHandler(
  57. USBmX_ContextRef ctx,
  58. USBmX_DeviceAddedProc proc,
  59. void *cookie);
  60. extern int USBmX_DeviceRemovedHandler(
  61. USBmX_ContextRef ctx,
  62. USBmX_DeviceRemovedProc proc,
  63. void *cookie);
  64. /*
  65. * USBmX_Begin:
  66. * You must call USBmX_Begin to init the USB
  67. * subsystem. Call before sending any commands.
  68. * Returns < 0 on error.
  69. */
  70. extern int USBmX_Begin(USBmX_ContextRef ctx);
  71. /*
  72. * USBmX_ScanForDevices:
  73. * Call to manually re-scan the USB bus for newly
  74. * connected devices. You should never have to
  75. * call this yourself (there are internal
  76. * notifications that should call this automatically
  77. * when a device is plugged in.
  78. * Returns < 0 on error.
  79. */
  80. extern int USBmX_ScanForDevices(USBmX_ContextRef ctx);
  81. /*
  82. * USBmX_Destroy:
  83. * Call when you are done messing with all devices.
  84. */
  85. extern int USBmX_Destroy(USBmX_ContextRef ctx);
  86. /*
  87. * USBmX_DeviceCount:
  88. * Get the number of connected devices.
  89. */
  90. extern int USBmX_DeviceCount(USBmX_ContextRef ctx);
  91. /*
  92. * USBmX_DefaultDevice:
  93. * Get the default devico (generally the last device
  94. * that was plugged in. Useful if you only have 1
  95. * device plugged in and don't want to mess with the
  96. * serial number. Returns NULL if there are no
  97. * devices connected.
  98. */
  99. extern USBmX_DeviceRef USBmX_DefaultDevice(USBmX_ContextRef ctx);
  100. /*
  101. * USBmX_DeviceWithSerial:
  102. * Get the device matching the provided serial
  103. * number. Returns NULL if the device cannot be
  104. * found.
  105. */
  106. extern USBmX_DeviceRef USBmX_DeviceWithSerial(USBmX_ContextRef ctx, const char *serial);
  107. /*
  108. * USBmX_DumpCommand:
  109. * Small utility function that prints out an 8 byte
  110. * buffer in human readable form. Useful for
  111. * printing commands and responses.
  112. */
  113. extern void USBmX_DumpCommand(u_char buf[]);
  114. /*
  115. * USBmX_DeviceInfo:
  116. * Outputs as much infomation as possible on a
  117. * given device.
  118. */
  119. extern int USBmX_DeviceInfo(USBmX_DeviceRef dev);
  120. /*
  121. *****************************************************
  122. ***************** Commands ************************
  123. *****************************************************
  124. The following command functions return 0 on success
  125. and < 0 on error.
  126. Where the command demands that you chose a 'line'
  127. (A0..A7, or B0..B7), the table below may be helpful:
  128. Value Line
  129. ----- ----
  130. 0x00 A0
  131. 0x01 A1
  132. 0x02 A2
  133. 0x03 A3
  134. 0x04 A4
  135. 0x05 A5
  136. 0x06 A6
  137. 0x07 A7
  138. 0x08 B0
  139. 0x09 B1
  140. 0x0A B2
  141. 0x0B B3
  142. 0x0C B4
  143. 0x0D B5
  144. 0x0E B6
  145. 0x0F B7
  146. */
  147. /*
  148. * USBmX_RawCommand:
  149. * Performs a raw command on the specifie device.
  150. * More info can be found here:
  151. * http://usbmicro.com/odn/index.html
  152. * under "Raw Command Summary"
  153. */
  154. extern int USBmX_RawCommand(USBmX_DeviceRef dev,
  155. const u_char command[8],
  156. u_char response[8]);
  157. /*
  158. The initial state of the ports on power up is that all
  159. of the 16 lines are set to be inputs. This command
  160. duplicates the power up conditions of setting all lines
  161. to input.
  162. */
  163. extern int USBmX_InitPorts(USBmX_DeviceRef dev);
  164. /*
  165. Write a byte value to specified port. The possible values
  166. range from 0-255 (0x00..0xFF) (passed in value).
  167. */
  168. extern int USBmX_WriteA(USBmX_DeviceRef dev, u_char value);
  169. extern int USBmX_WriteB(USBmX_DeviceRef dev, u_char value);
  170. /*
  171. Write masked values to specified port. The net result of
  172. writing masked values is that only the specified bits will
  173. be written. The resulting port condition is the logic
  174. combination of the current port state ANDed with the
  175. first term (v_and) and then ORed with the second (v_or).
  176. This command can affect any number of lines on the port.
  177. */
  178. extern int USBmX_WriteABit(USBmX_DeviceRef dev, u_char v_and, u_char v_or);
  179. extern int USBmX_WriteBBit(USBmX_DeviceRef dev, u_char v_and, u_char v_or);
  180. /*
  181. Read a byte value from specified port. The returned value
  182. (returned in *value_out) is the state of the port lines that
  183. an external device has set, if the line is an input.
  184. The lines that might be configured as outputs return the
  185. output state.
  186. */
  187. extern int USBmX_ReadA(USBmX_DeviceRef dev, u_char *value_out);
  188. extern int USBmX_ReadB(USBmX_DeviceRef dev, u_char *value_out);
  189. /*
  190. Set a single bit/line high. (0..15)
  191. */
  192. extern int USBmX_SetBit(USBmX_DeviceRef dev, u_char line);
  193. /*
  194. Set a single bit/line low. (0..15)
  195. */
  196. extern int USBmX_ResetBit(USBmX_DeviceRef dev, u_char line);
  197. /*
  198. Set the i/o direction of specified port.
  199. dir0 is written to the direction0 control register, dir1
  200. is written to the direction1 control register appropriate
  201. for the port.
  202. The individual pins of the ports can be set to input, or
  203. output. Input is set when both bits associated with a
  204. particular line are set to 0. There are different types
  205. of outputs available for the board, but generally setting
  206. both of the direction bits to 1 will suffice.
  207. High order bits of dir0 and dir1 correspond to high order
  208. lines.
  209. */
  210. extern int USBmX_DirectionA(USBmX_DeviceRef dev, u_char dir0, u_char dir1);
  211. extern int USBmX_DirectionB(USBmX_DeviceRef dev, u_char dir0, u_char dir1);
  212. /*
  213. Strobe write of a byte value to a port. This command
  214. selects port A (port_select=0x00) or B (port_select=0x01)
  215. for the written byte, as well as a polarity (negative or
  216. positive) and a line (A.0 - B.7 (strobe_line=0..15)) to
  217. toggle. The byte is written and then the line toggled.
  218. pulse_length can be anywhere between 0x00..0xFF.
  219. Default is to have the strobe negative-going
  220. (high->low->high). Add 0x10 to strobe_line to have the
  221. pulse be positive-going (low->high->low).
  222. */
  223. extern int USBmX_StrobeWrite(USBmX_DeviceRef dev,
  224. u_char data,
  225. u_char port_select,
  226. u_char strobe_line,
  227. u_char pulse_length);
  228. /*
  229. Strobe read of a byte value from a port. This command
  230. selects port A (port_select=0x00) or B (port_select=0x01)
  231. for the read byte, as well as a polarity (negative or
  232. positive) and a line (A.0 - B.7 (strobe_line=0..15)) to
  233. toggle. The line is toggled and the byte is read.
  234. pulse_length can be anywhere between 0x00..0xFF.
  235. Default is to have the strobe negative-going
  236. (high->low->high). Add 0x10 to strobe_line to have the
  237. pulse be positive-going (low->high->low).
  238. *data_out is set to the byte read.
  239. */
  240. extern int USBmX_StrobeRead(USBmX_DeviceRef dev,
  241. u_char *data_out,
  242. u_char port_select,
  243. u_char strobe_line,
  244. u_char pulse_length);
  245. /*
  246. Strobe write of a 1 to 6 byte value to a port. This
  247. command uses port A or B for the written byte, as well
  248. as a polarity (negative or positive) and a line
  249. (A.0 - B.7) to toggle based on the previously executed
  250. USBmX_StrobeWrite or USBmX_StrobeRead.
  251. Control this command's port for the written byte,
  252. polarity and strobe line by using the USBmX_StrobeWrite
  253. or USBmX_StrobeRead. This command will then use the
  254. same selected port, polarity, and strobe line to write
  255. up to 6 bytes.
  256. n_bytes is the number of bytes to write.
  257. bytes[] is an array of bytes, the first n_bytes values
  258. will be written. It must be at least of size n_bytes.
  259. */
  260. extern int USBmX_StrobeWrites(USBmX_DeviceRef dev,
  261. u_char n_bytes,
  262. u_char bytes[]);
  263. /*
  264. Strobe read of a 1 to 6 byte value from a port. This
  265. command uses port A or B for the read byte, as well
  266. as a polarity (negative or positive) and a line
  267. (A.0 - B.7) to toggle based on the previously executed
  268. USBmX_StrobeWrite or USBmX_StrobeRead.
  269. Control this command's port for the written byte,
  270. polarity and strobe line by using the USBmX_StrobeWrite
  271. or USBmX_StrobeRead. This command will then use the
  272. same selected port, polarity, and strobe line to read
  273. up to 6 bytes.
  274. n_bytes is the number of bytes to read.
  275. bytes[] is an array of bytes that the read values will be
  276. copied into. It must be at least of size n_bytes.
  277. */
  278. extern int USBmX_StrobeReads(USBmX_DeviceRef dev,
  279. u_char n_bytes,
  280. u_char bytes[]);
  281. /*
  282. Initialize LCD variables. This includes the selection
  283. of the lines used for RW, RS, E and the port used for
  284. data. RW, RS, and E are reset. These commands are
  285. appropriate for HD44780 devices and devices that are
  286. compatible.
  287. port_select = 0 for port A, or 1 for port B.
  288. See table (way) above for line->number mappings.
  289. */
  290. extern int USBmX_InitLCD(USBmX_DeviceRef dev,
  291. u_char rw_select,
  292. u_char rs_select,
  293. u_char port_select,
  294. u_char e_select);
  295. /*
  296. Write a command to the LCD. The RS, RW, E lines and the
  297. data port are selected by the USBmX_InitLCD. The data
  298. byte is written to the selected port and the control
  299. lines are set appropriately. The E line is toggled for
  300. five to eight microseconds.
  301. */
  302. extern int USBmX_LCDCmd(USBmX_DeviceRef dev, u_char cmd);
  303. /*
  304. Write a character to the LCD. The RS, RW, E lines and
  305. the data port are selected by the USBmX_InitLCD. The data
  306. byte is written to the selected port and the control
  307. lines are set appropriately. The E line is toggled for
  308. five to eight microseconds.
  309. */
  310. extern int USBmX_LCDData(USBmX_DeviceRef dev, u_char data);
  311. // TODO: SPI + Stepper
  312. #endif /* _USBMICROX_H_ */