fusb302.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*********************************************************************
  2. * FileName: FUSB300.h
  3. * Dependencies: See INCLUDES section below
  4. * Processor: PIC32
  5. * Compiler: XC32
  6. * Company: Fairchild Semiconductor
  7. *
  8. * Author Date Comment
  9. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. * M. Smith 12/04/2014 Initial Version
  11. *
  12. *
  13. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. *
  15. * Software License Agreement:
  16. *
  17. * The software supplied herewith by Fairchild Semiconductor (the “Company?
  18. * is supplied to you, the Company's customer, for exclusive use with its
  19. * USB Type C / USB PD products. The software is owned by the Company and/or
  20. * its supplier, and is protected under applicable copyright laws.
  21. * All rights are reserved. Any use in violation of the foregoing restrictions
  22. * may subject the user to criminal sanctions under applicable laws, as well
  23. * as to civil liability for the breach of the terms and conditions of this
  24. * license.
  25. *
  26. * THIS SOFTWARE IS PROVIDED IN AN “AS IS?CONDITION. NO WARRANTIES,
  27. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
  28. * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  29. * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
  30. * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
  31. * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  32. *
  33. ********************************************************************/
  34. #ifndef FUSB300_H
  35. #define FUSB300_H
  36. /* /////////////////////////////////////////////////////////////////////////// */
  37. /* Required headers */
  38. /* /////////////////////////////////////////////////////////////////////////// */
  39. #define TRUE true
  40. #define FALSE false
  41. #define BOOL bool
  42. #define UINT8 u8
  43. #define UINT16 u16
  44. #define UINT32 u32
  45. #define SDAC_DEFAULT 0x20
  46. #define MDAC_1P6V 0x26
  47. #define MDAC_2P05V 0x31
  48. #define MDAC_2P6V 0x3E
  49. /* FUSB300 Register Addresses */
  50. #define regDeviceID 0x01
  51. #define regSwitches0 0x02
  52. #define regSwitches1 0x03
  53. #define regMeasure 0x04
  54. #define regSlice 0x05
  55. #define regControl0 0x06
  56. #define regControl1 0x07
  57. #define regControl2 0x08
  58. #define regControl3 0x09
  59. #define regMask 0x0A
  60. #define regPower 0x0B
  61. #define regReset 0x0C
  62. #define regOCPreg 0x0D
  63. #define regMaska 0x0E
  64. #define regMaskb 0x0F
  65. #define regControl4 0x10
  66. #define regStatus0a 0x3C
  67. #define regStatus1a 0x3D
  68. #define regInterrupta 0x3E
  69. #define regInterruptb 0x3F
  70. #define regStatus0 0x40
  71. #define regStatus1 0x41
  72. #define regInterrupt 0x42
  73. #define regFIFO 0x43
  74. /* Type C Timing Parameters */
  75. #define tAMETimeout 1000
  76. #define tCCDebounceMin 100
  77. #define tCCDebounceNom 120
  78. #define tCCDebounceMax 200
  79. #define tPDDebounceMin 10
  80. #define tPDDebounceMax 20
  81. #define tAccDetect 100
  82. #define tDRP 80
  83. #define tDRPAdvert 30
  84. #define tDRPTransition 1
  85. #define tDRPTry 125
  86. #define tDRPTryWait 600
  87. #define tErrorRecovery 25
  88. /* Max time from entry to Attached.SRC until VBUS reaches minimum vSafe5V */
  89. #define tVBUSOn 275
  90. /* Max time from when the sink is detached until the source removes VBUS and reaches vSafe0V */
  91. #define tVBUSOff 650
  92. /* VConn should be applied prior to VBUS */
  93. #define tVConnOn 2
  94. /* Max time from when Sink enters PoweredAccessory state until sourcing VCONN */
  95. #define tVConnOnPA 100
  96. /* Max time to remove VCONN supply */
  97. #define tVConnOff 35
  98. /* Nominal time for the sink to reduce its consumption due to a change in Type-C current advertisement */
  99. #define tSinkAdj 40
  100. /* Duration in ms to wait before checking other CC pin for the FUSB302 */
  101. #define tFUSB302Toggle 3
  102. typedef enum {
  103. USBTypeC_Sink = 0,
  104. USBTypeC_Source,
  105. USBTypeC_DRP
  106. } USBTypeCPort;
  107. typedef enum {
  108. Disabled = 0,
  109. ErrorRecovery,
  110. Unattached,
  111. AttachWaitSink,
  112. AttachedSink,
  113. AttachWaitSource,
  114. AttachedSource,
  115. TrySource,
  116. TryWaitSink,
  117. AudioAccessory,
  118. DebugAccessory,
  119. AttachWaitAccessory,
  120. PoweredAccessory,
  121. UnsupportedAccessory,
  122. DelayUnattached,
  123. } ConnectionState;
  124. typedef enum {
  125. CCTypeNone = 0,
  126. CCTypeRa,
  127. CCTypeRdUSB,
  128. CCTypeRd1p5,
  129. CCTypeRd3p0
  130. } CCTermType;
  131. typedef enum {
  132. TypeCPin_None = 0,
  133. TypeCPin_GND1,
  134. TypeCPin_TXp1,
  135. TypeCPin_TXn1,
  136. TypeCPin_VBUS1,
  137. TypeCPin_CC1,
  138. TypeCPin_Dp1,
  139. TypeCPin_Dn1,
  140. TypeCPin_SBU1,
  141. TypeCPin_VBUS2,
  142. TypeCPin_RXn2,
  143. TypeCPin_RXp2,
  144. TypeCPin_GND2,
  145. TypeCPin_GND3,
  146. TypeCPin_TXp2,
  147. TypeCPin_TXn2,
  148. TypeCPin_VBUS3,
  149. TypeCPin_CC2,
  150. TypeCPin_Dp2,
  151. TypeCPin_Dn2,
  152. TypeCPin_SBU2,
  153. TypeCPin_VBUS4,
  154. TypeCPin_RXn1,
  155. TypeCPin_RXp1,
  156. TypeCPin_GND4
  157. } TypeCPins_t;
  158. typedef enum {
  159. utccNone = 0,
  160. utccDefault,
  161. utcc1p5A,
  162. utcc3p0A
  163. } USBTypeCCurrent;
  164. typedef union {
  165. UINT8 byte;
  166. struct {
  167. unsigned REVISION:3;
  168. unsigned VERSION:5;
  169. };
  170. } regDeviceID_t;
  171. typedef union {
  172. UINT16 word;
  173. UINT8 byte[2] __aligned(8);
  174. struct {
  175. /* Switches0 */
  176. unsigned PDWN1:1;
  177. unsigned PDWN2:1;
  178. unsigned MEAS_CC1:1;
  179. unsigned MEAS_CC2:1;
  180. unsigned VCONN_CC1:1;
  181. unsigned VCONN_CC2:1;
  182. unsigned PU_EN1:1;
  183. unsigned PU_EN2:1;
  184. /* Switches1 */
  185. unsigned TXCC1:1;
  186. unsigned TXCC2:1;
  187. unsigned AUTO_CRC:1;
  188. unsigned:1;
  189. unsigned DATAROLE:1;
  190. unsigned SPECREV:2;
  191. unsigned POWERROLE:1;
  192. };
  193. } regSwitches_t;
  194. typedef union {
  195. UINT8 byte;
  196. struct {
  197. unsigned MDAC:6;
  198. unsigned MEAS_VBUS:1;
  199. unsigned:1;
  200. };
  201. } regMeasure_t;
  202. typedef union {
  203. UINT8 byte;
  204. struct {
  205. unsigned SDAC:6;
  206. unsigned:2;
  207. };
  208. } regSlice_t;
  209. typedef union {
  210. UINT32 dword;
  211. UINT8 byte[4] __aligned(8);
  212. struct {
  213. /* Control0 */
  214. unsigned TX_START:1;
  215. unsigned AUTO_PRE:1;
  216. unsigned HOST_CUR:2;
  217. unsigned LOOPBACK:1;
  218. unsigned INT_MASK:1;
  219. unsigned TX_FLUSH:1;
  220. unsigned:1;
  221. /* Control1 */
  222. unsigned ENSOP1:1;
  223. unsigned ENSOP2:1;
  224. unsigned RX_FLUSH:1;
  225. unsigned:1;
  226. unsigned BIST_MODE2:1;
  227. unsigned ENSOP1DP:1;
  228. unsigned ENSOP2DB:1;
  229. unsigned:1;
  230. /* Control2 */
  231. unsigned TOGGLE:1;
  232. unsigned MODE:2;
  233. unsigned WAKE_EN:1;
  234. unsigned WAKE_SELF:1;
  235. unsigned TOG_RD_ONLY:1;
  236. unsigned:2;
  237. /* Control3 */
  238. unsigned AUTO_RETRY:1;
  239. unsigned N_RETRIES:2;
  240. unsigned AUTO_SOFTRESET:1;
  241. unsigned AUTO_HARDRESET:1;
  242. unsigned:1;
  243. unsigned SEND_HARDRESET:1;
  244. unsigned:1;
  245. };
  246. } regControl_t;
  247. typedef union {
  248. UINT8 byte;
  249. struct {
  250. unsigned M_BC_LVL:1;
  251. unsigned M_COLLISION:1;
  252. unsigned M_WAKE:1;
  253. unsigned M_ALERT:1;
  254. unsigned M_CRC_CHK:1;
  255. unsigned M_COMP_CHNG:1;
  256. unsigned M_ACTIVITY:1;
  257. unsigned M_VBUSOK:1;
  258. };
  259. } regMask_t;
  260. typedef union {
  261. UINT8 byte;
  262. struct {
  263. unsigned PWR:4;
  264. unsigned:4;
  265. };
  266. } regPower_t;
  267. typedef union {
  268. UINT8 byte;
  269. struct {
  270. unsigned SW_RES:1;
  271. unsigned:7;
  272. };
  273. } regReset_t;
  274. typedef union {
  275. UINT8 byte;
  276. struct {
  277. unsigned OCP_CUR:3;
  278. unsigned OCP_RANGE:1;
  279. unsigned:4;
  280. };
  281. } regOCPreg_t;
  282. typedef union {
  283. UINT16 word;
  284. UINT8 byte[2] __aligned(8);
  285. struct {
  286. /* Maska */
  287. unsigned M_HARDRST:1;
  288. unsigned M_SOFTRST:1;
  289. unsigned M_TXCRCSENT:1;
  290. unsigned M_HARDSENT:1;
  291. unsigned M_RETRYFAIL:1;
  292. unsigned M_SOFTFAIL:1;
  293. unsigned M_TOGDONE:1;
  294. unsigned M_OCP_TEMP:1;
  295. /* Maskb */
  296. unsigned M_GCRCSENT:1;
  297. unsigned:7;
  298. };
  299. } regMaskAdv_t;
  300. typedef union {
  301. UINT8 byte;
  302. struct {
  303. unsigned TOG_USRC_EXIT:1;
  304. unsigned:7;
  305. };
  306. } regControl4_t;
  307. typedef union {
  308. UINT8 byte[7] __aligned(8);
  309. struct {
  310. UINT16 StatusAdv;
  311. UINT16 InterruptAdv;
  312. UINT16 Status;
  313. UINT8 Interrupt1;
  314. };
  315. struct {
  316. /* Status0a */
  317. unsigned HARDRST:1;
  318. unsigned SOFTRST:1;
  319. unsigned POWER23:2;
  320. unsigned RETRYFAIL:1;
  321. unsigned SOFTFAIL:1;
  322. unsigned TOGDONE:1;
  323. unsigned M_OCP_TEMP:1;
  324. /* Status1a */
  325. unsigned RXSOP:1;
  326. unsigned RXSOP1DB:1;
  327. unsigned RXSOP2DB:1;
  328. unsigned TOGSS:3;
  329. unsigned:2;
  330. /* Interrupta */
  331. unsigned I_HARDRST:1;
  332. unsigned I_SOFTRST:1;
  333. unsigned I_TXSENT:1;
  334. unsigned I_HARDSENT:1;
  335. unsigned I_RETRYFAIL:1;
  336. unsigned I_SOFTFAIL:1;
  337. unsigned I_TOGDONE:1;
  338. unsigned I_OCP_TEMP:1;
  339. /* Interruptb */
  340. unsigned I_GCRCSENT:1;
  341. unsigned:7;
  342. /* Status0 */
  343. unsigned BC_LVL:2;
  344. unsigned WAKE:1;
  345. unsigned ALERT:1;
  346. unsigned CRC_CHK:1;
  347. unsigned COMP:1;
  348. unsigned ACTIVITY:1;
  349. unsigned VBUSOK:1;
  350. /* Status1 */
  351. unsigned OCP:1;
  352. unsigned OVRTEMP:1;
  353. unsigned TX_FULL:1;
  354. unsigned TX_EMPTY:1;
  355. unsigned RX_FULL:1;
  356. unsigned RX_EMPTY:1;
  357. unsigned RXSOP1:1;
  358. unsigned RXSOP2:1;
  359. /* Interrupt */
  360. unsigned I_BC_LVL:1;
  361. unsigned I_COLLISION:1;
  362. unsigned I_WAKE:1;
  363. unsigned I_ALERT:1;
  364. unsigned I_CRC_CHK:1;
  365. unsigned I_COMP_CHNG:1;
  366. unsigned I_ACTIVITY:1;
  367. unsigned I_VBUSOK:1;
  368. };
  369. } regStatus_t;
  370. typedef struct {
  371. regDeviceID_t DeviceID;
  372. regSwitches_t Switches;
  373. regMeasure_t Measure;
  374. regSlice_t Slice;
  375. regControl_t Control;
  376. regMask_t Mask;
  377. regPower_t Power;
  378. regReset_t Reset;
  379. regOCPreg_t OCPreg;
  380. regMaskAdv_t MaskAdv;
  381. regControl4_t Control4;
  382. regStatus_t Status;
  383. } FUSB300reg_t;
  384. /* /////////////////////////////////////////////////////////////////////////// */
  385. /* LOCAL PROTOTYPES */
  386. /* /////////////////////////////////////////////////////////////////////////// */
  387. void InitializeFUSB300Interrupt(BOOL blnEnable);
  388. void InitializeFUSB300Timer(BOOL blnEnable);
  389. void InitializeFUSB300Variables(void);
  390. void InitializeFUSB300(void);
  391. void DisableFUSB300StateMachine(void);
  392. void EnableFUSB300StateMachine(void);
  393. void StateMachineFUSB300(struct usbtypc *typec);
  394. void StateMachineDisabled(void);
  395. void StateMachineErrorRecovery(void);
  396. void StateMachineDelayUnattached(void);
  397. void StateMachineUnattached(void);
  398. void StateMachineAttachWaitSnk(void);
  399. void StateMachineAttachWaitSrc(void);
  400. void StateMachineAttachWaitAcc(void);
  401. void StateMachineAttachedSink(void);
  402. void StateMachineAttachedSource(void);
  403. void StateMachineTryWaitSnk(void);
  404. void StateMachineTrySrc(void);
  405. void StateMachineDebugAccessory(void);
  406. void StateMachineAudioAccessory(void);
  407. void StateMachinePoweredAccessory(void);
  408. void StateMachineUnsupportedAccessory(void);
  409. void SetStateDisabled(void);
  410. void SetStateErrorRecovery(void);
  411. void SetStateDelayUnattached(void);
  412. void SetStateUnattached(void);
  413. void SetStateAttachWaitSnk(void);
  414. void SetStateAttachWaitSrc(void);
  415. void SetStateAttachWaitAcc(void);
  416. void SetStateAttachedSrc(void);
  417. void SetStateAttachedSink(void);
  418. void RoleSwapToAttachedSink(void);
  419. void RoleSwapToAttachedSource(void);
  420. void SetStateTryWaitSnk(void);
  421. void SetStateTrySrc(void);
  422. void SetStateDebugAccessory(void);
  423. void SetStateAudioAccessory(void);
  424. void SetStatePoweredAccessory(void);
  425. void SetStateUnsupportedAccessory(void);
  426. void UpdateSourcePowerMode(void);
  427. void ToggleMeasureCC1(void);
  428. void ToggleMeasureCC2(void);
  429. CCTermType DecodeCCTermination(void);
  430. void UpdateSinkCurrent(CCTermType Termination);
  431. void ConfigurePortType(unsigned char Control);
  432. void UpdateCurrentAdvert(unsigned char Current);
  433. void GetFUSB300TypeCStatus(unsigned char abytData[]);
  434. unsigned char GetTypeCSMControl(void);
  435. unsigned char GetCCTermination(void);
  436. BOOL FUSB300Write(unsigned char regAddr, unsigned char length, unsigned char *data);
  437. BOOL FUSB300Read(unsigned char regAddr, unsigned char length, unsigned char *data);
  438. void fusb300_i2c_w_reg8(struct i2c_client *client, u8 addr, u8 var);
  439. u8 fusb300_i2c_r_reg(struct i2c_client *client, u8 addr);
  440. #endif /* FUSB300_H */