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

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