Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usbotg.h 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014 <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // ----- Local Includes -----
  18. #include "mchck.h"
  19. #include "usb-common.h"
  20. // ----- Structs -----
  21. /**
  22. * Hardware structures
  23. */
  24. struct USB_ADDINFO_t {
  25. UNION_STRUCT_START(8);
  26. uint8_t iehost : 1;
  27. uint8_t _rsvd0 : 2;
  28. uint8_t irqnum : 5;
  29. UNION_STRUCT_END;
  30. };
  31. CTASSERT_SIZE_BIT(struct USB_ADDINFO_t, 8);
  32. struct USB_OTGSTAT_t {
  33. UNION_STRUCT_START(8);
  34. uint8_t avbus : 1;
  35. uint8_t _rsvd0 : 1;
  36. uint8_t b_sess : 1;
  37. uint8_t sessvld : 1;
  38. uint8_t _rsvd1 : 1;
  39. uint8_t line_state : 1;
  40. uint8_t onemsec : 1;
  41. uint8_t idchg : 1;
  42. UNION_STRUCT_END;
  43. };
  44. CTASSERT_SIZE_BIT(struct USB_OTGSTAT_t, 8);
  45. struct USB_OTGCTL_t {
  46. UNION_STRUCT_START(8);
  47. uint8_t _rsvd0 : 2;
  48. uint8_t otgen : 1;
  49. uint8_t _rsvd1 : 1;
  50. uint8_t dmlow : 1;
  51. uint8_t dplow : 1;
  52. uint8_t _rsvd2 : 1;
  53. uint8_t dphigh : 1;
  54. UNION_STRUCT_END;
  55. };
  56. CTASSERT_SIZE_BIT(struct USB_OTGCTL_t, 8);
  57. struct USB_ISTAT_t {
  58. UNION_STRUCT_START(8);
  59. uint8_t usbrst : 1;
  60. uint8_t error : 1;
  61. uint8_t softok : 1;
  62. uint8_t tokdne : 1;
  63. uint8_t sleep : 1;
  64. uint8_t resume : 1;
  65. uint8_t attach : 1;
  66. uint8_t stall : 1;
  67. UNION_STRUCT_END;
  68. };
  69. CTASSERT_SIZE_BIT(struct USB_ISTAT_t, 8);
  70. struct USB_ERRSTAT_t {
  71. UNION_STRUCT_START(8);
  72. uint8_t piderr : 1;
  73. uint8_t crc5eof : 1;
  74. uint8_t crc16 : 1;
  75. uint8_t dfn8 : 1;
  76. uint8_t btoerr : 1;
  77. uint8_t dmaerr : 1;
  78. uint8_t _rsvd0 : 1;
  79. uint8_t btserr : 1;
  80. UNION_STRUCT_END;
  81. };
  82. CTASSERT_SIZE_BIT(struct USB_ERRSTAT_t, 8);
  83. struct USB_STAT_t {
  84. UNION_STRUCT_START(8);
  85. uint8_t _rsvd0 : 2;
  86. enum usb_ep_pingpong pingpong : 1;
  87. enum usb_ep_dir dir : 1;
  88. uint8_t ep : 4;
  89. UNION_STRUCT_END;
  90. };
  91. CTASSERT_SIZE_BIT(struct USB_STAT_t, 8);
  92. struct USB_CTL_t {
  93. union {
  94. struct /* common */ {
  95. uint8_t _rsvd1 : 1;
  96. uint8_t oddrst : 1;
  97. uint8_t resume : 1;
  98. uint8_t _rsvd2 : 3;
  99. uint8_t se0 : 1;
  100. uint8_t jstate : 1;
  101. };
  102. struct /* host */ {
  103. uint8_t sofen : 1;
  104. uint8_t _rsvd3 : 2;
  105. uint8_t hostmodeen : 1;
  106. uint8_t reset : 1;
  107. uint8_t token_busy : 1;
  108. uint8_t _rsvd4 : 2;
  109. };
  110. struct /* device */ {
  111. uint8_t usben : 1;
  112. uint8_t _rsvd5 : 4;
  113. uint8_t txd_suspend : 1;
  114. uint8_t _rsvd6 : 2;
  115. };
  116. uint8_t raw;
  117. };
  118. };
  119. CTASSERT_SIZE_BIT(struct USB_CTL_t, 8);
  120. struct USB_ADDR_t {
  121. UNION_STRUCT_START(8);
  122. uint8_t addr : 7;
  123. uint8_t lsen : 1;
  124. UNION_STRUCT_END;
  125. };
  126. CTASSERT_SIZE_BIT(struct USB_ADDR_t, 8);
  127. struct USB_TOKEN_t {
  128. UNION_STRUCT_START(8);
  129. uint8_t endpt : 4;
  130. enum usb_tok_pid pid : 4;
  131. UNION_STRUCT_END;
  132. };
  133. CTASSERT_SIZE_BIT(struct USB_TOKEN_t, 8);
  134. struct USB_ENDPT_t {
  135. UNION_STRUCT_START(8);
  136. uint8_t ephshk : 1;
  137. uint8_t epstall : 1;
  138. uint8_t eptxen : 1;
  139. uint8_t eprxen : 1;
  140. uint8_t epctldis : 1;
  141. uint8_t _rsvd0 : 1;
  142. uint8_t retrydis : 1;
  143. uint8_t hostwohub : 1;
  144. UNION_STRUCT_END;
  145. };
  146. CTASSERT_SIZE_BIT(struct USB_ENDPT_t, 8);
  147. struct USB_USBCTRL_t {
  148. UNION_STRUCT_START(8);
  149. uint8_t _rsvd0 : 6;
  150. uint8_t pde : 1;
  151. uint8_t susp : 1;
  152. UNION_STRUCT_END;
  153. };
  154. CTASSERT_SIZE_BIT(struct USB_USBCTRL_t, 8);
  155. struct USB_OBSERVE_t {
  156. UNION_STRUCT_START(8);
  157. uint8_t _rsvd0 : 4;
  158. uint8_t dmpd : 1;
  159. uint8_t _rsvd1 : 1;
  160. uint8_t dppd : 1;
  161. uint8_t dppu : 1;
  162. UNION_STRUCT_END;
  163. };
  164. CTASSERT_SIZE_BIT(struct USB_OBSERVE_t, 8);
  165. struct USB_CONTROL_t {
  166. UNION_STRUCT_START(8);
  167. uint8_t _rsvd0 : 4;
  168. uint8_t dppullupnonotg : 1;
  169. uint8_t _rsvd1 : 3;
  170. UNION_STRUCT_END;
  171. };
  172. CTASSERT_SIZE_BIT(struct USB_CONTROL_t, 8);
  173. struct USB_USBTRC0_t {
  174. UNION_STRUCT_START(8);
  175. uint8_t usb_resume_int : 1;
  176. uint8_t sync_det : 1;
  177. uint8_t _rsvd0 : 3;
  178. uint8_t usbresmen : 1;
  179. uint8_t _rsvd1 : 1;
  180. uint8_t usbreset : 1;
  181. UNION_STRUCT_END;
  182. };
  183. CTASSERT_SIZE_BIT(struct USB_USBTRC0_t, 8);
  184. struct USB_t {
  185. uint8_t perid;
  186. uint8_t _pad0[3];
  187. uint8_t idcomp;
  188. uint8_t _pad1[3];
  189. uint8_t rev;
  190. uint8_t _pad2[3];
  191. struct USB_ADDINFO_t addinfo;
  192. uint8_t _pad3[3];
  193. struct USB_OTGSTAT_t otgistat;
  194. uint8_t _pad4[3];
  195. struct USB_OTGSTAT_t otgicr;
  196. uint8_t _pad5[3];
  197. struct USB_OTGSTAT_t otgstat;
  198. uint8_t _pad6[3];
  199. struct USB_OTGCTL_t otgctl;
  200. uint8_t _pad7[3];
  201. uint8_t _pad8[0x80 - 0x20];
  202. struct USB_ISTAT_t istat;
  203. uint8_t _pad9[3];
  204. struct USB_ISTAT_t inten;
  205. uint8_t _pad10[3];
  206. struct USB_ERRSTAT_t errstat;
  207. uint8_t _pad11[3];
  208. struct USB_ERRSTAT_t erren;
  209. uint8_t _pad12[3];
  210. struct USB_STAT_t stat;
  211. uint8_t _pad13[3];
  212. struct USB_CTL_t ctl;
  213. uint8_t _pad14[3];
  214. struct USB_ADDR_t addr;
  215. uint8_t _pad15[3];
  216. uint8_t bdtpage1;
  217. uint8_t _pad16[3];
  218. uint8_t frmnuml;
  219. uint8_t _pad17[3];
  220. struct {
  221. uint8_t frmnumh : 3;
  222. uint8_t _rsvd0 : 5;
  223. };
  224. uint8_t _pad18[3];
  225. struct USB_TOKEN_t token;
  226. uint8_t _pad19[3];
  227. uint8_t softhld;
  228. uint8_t _pad20[3];
  229. uint8_t bdtpage2;
  230. uint8_t _pad21[3];
  231. uint8_t bdtpage3;
  232. uint8_t _pad22[3];
  233. uint8_t _pad23[0xc0 - 0xb8];
  234. struct {
  235. struct USB_ENDPT_t;
  236. uint8_t _pad24[3];
  237. } endpt[16];
  238. struct USB_USBCTRL_t usbctrl;
  239. uint8_t _pad25[3];
  240. struct USB_OBSERVE_t observe;
  241. uint8_t _pad26[3];
  242. struct USB_CONTROL_t control;
  243. uint8_t _pad27[3];
  244. struct USB_USBTRC0_t usbtrc0;
  245. uint8_t _pad28[3];
  246. uint8_t _pad29[4];
  247. uint8_t usbfrmadjust;
  248. uint8_t _pad30[3];
  249. };
  250. CTASSERT_SIZE_BYTE(struct USB_t, 0x118);
  251. struct USB_BD_t {
  252. struct USB_BD_BITS_t {
  253. union {
  254. struct {
  255. uint32_t _rsvd0 : 2;
  256. uint32_t stall : 1;
  257. uint32_t dts : 1;
  258. uint32_t ninc : 1;
  259. uint32_t keep : 1;
  260. enum usb_data01 data01 : 1;
  261. uint32_t own : 1;
  262. uint32_t _rsvd1 : 8;
  263. uint32_t bc : 10;
  264. uint32_t _rsvd2 : 6;
  265. };
  266. struct /* processor */ {
  267. uint32_t _rsvd5 : 2;
  268. enum usb_tok_pid tok_pid : 4;
  269. uint32_t _rsvd6 : 26;
  270. };
  271. uint32_t raw;
  272. };
  273. };
  274. void *addr;
  275. };
  276. CTASSERT_SIZE_BYTE(struct USB_BD_t, 8);
  277. extern volatile struct USB_t USB0;