PD Buddy Sink Firmware
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.

pd.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017-2018 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef PDB_PD_H
  19. #define PDB_PD_H
  20. #include <stdint.h>
  21. #include <ch.h>
  22. /*
  23. * Macros for working with USB Power Delivery messages.
  24. *
  25. * This file is mostly written from the PD Rev. 2.0 spec, but the header is
  26. * written from the Rev. 3.0 spec.
  27. */
  28. /*
  29. * PD Header
  30. */
  31. #define PD_HDR_MSGTYPE_SHIFT 0
  32. #define PD_HDR_MSGTYPE (0x1F << PD_HDR_MSGTYPE_SHIFT)
  33. #define PD_HDR_DATAROLE_SHIFT 5
  34. #define PD_HDR_DATAROLE (0x1 << PD_HDR_DATAROLE_SHIFT)
  35. #define PD_HDR_SPECREV_SHIFT 6
  36. #define PD_HDR_SPECREV (0x3 << PD_HDR_SPECREV_SHIFT)
  37. #define PD_HDR_POWERROLE_SHIFT 8
  38. #define PD_HDR_POWERROLE (1 << PD_HDR_POWERROLE_SHIFT)
  39. #define PD_HDR_MESSAGEID_SHIFT 9
  40. #define PD_HDR_MESSAGEID (0x7 << PD_HDR_MESSAGEID_SHIFT)
  41. #define PD_HDR_NUMOBJ_SHIFT 12
  42. #define PD_HDR_NUMOBJ (0x7 << PD_HDR_NUMOBJ_SHIFT)
  43. #define PD_HDR_EXT (1 << 15)
  44. /* Message types */
  45. #define PD_MSGTYPE_GET(msg) (((msg)->hdr & PD_HDR_MSGTYPE) >> PD_HDR_MSGTYPE_SHIFT)
  46. /* Control Message */
  47. #define PD_MSGTYPE_GOODCRC 0x01
  48. #define PD_MSGTYPE_GOTOMIN 0x02
  49. #define PD_MSGTYPE_ACCEPT 0x03
  50. #define PD_MSGTYPE_REJECT 0x04
  51. #define PD_MSGTYPE_PING 0x05
  52. #define PD_MSGTYPE_PS_RDY 0x06
  53. #define PD_MSGTYPE_GET_SOURCE_CAP 0x07
  54. #define PD_MSGTYPE_GET_SINK_CAP 0x08
  55. #define PD_MSGTYPE_DR_SWAP 0x09
  56. #define PD_MSGTYPE_PR_SWAP 0x0A
  57. #define PD_MSGTYPE_VCONN_SWAP 0x0B
  58. #define PD_MSGTYPE_WAIT 0x0C
  59. #define PD_MSGTYPE_SOFT_RESET 0x0D
  60. #define PD_MSGTYPE_NOT_SUPPORTED 0x10
  61. #define PD_MSGTYPE_GET_SOURCE_CAP_EXTENDED 0x11
  62. #define PD_MSGTYPE_GET_STATUS 0x12
  63. #define PD_MSGTYPE_FR_SWAP 0x13
  64. #define PD_MSGTYPE_GET_PPS_STATUS 0x14
  65. #define PD_MSGTYPE_GET_COUNTRY_CODES 0x15
  66. /* Data Message */
  67. #define PD_MSGTYPE_SOURCE_CAPABILITIES 0x01
  68. #define PD_MSGTYPE_REQUEST 0x02
  69. #define PD_MSGTYPE_BIST 0x03
  70. #define PD_MSGTYPE_SINK_CAPABILITIES 0x04
  71. #define PD_MSGTYPE_BATTERY_STATUS 0x05
  72. #define PD_MSGTYPE_ALERT 0x06
  73. #define PD_MSGTYPE_GET_COUNTRY_INFO 0x07
  74. #define PD_MSGTYPE_VENDOR_DEFINED 0x0F
  75. /* Extended Message */
  76. #define PD_MSGTYPE_SOURCE_CAPABILITIES_EXTENDED 0x01
  77. #define PD_MSGTYPE_STATUS 0x02
  78. #define PD_MSGTYPE_GET_BATTERY_CAP 0x03
  79. #define PD_MSGTYPE_GET_BATTERY_STATUS 0x04
  80. #define PD_MSGTYPE_BATTERY_CAPABILITIES 0x05
  81. #define PD_MSGTYPE_GET_MANUFACTURER_INFO 0x06
  82. #define PD_MSGTYPE_MANUFACTURER_INFO 0x07
  83. #define PD_MSGTYPE_SECURITY_REQUEST 0x08
  84. #define PD_MSGTYPE_SECURITY_RESPONSE 0x09
  85. #define PD_MSGTYPE_FIRMWARE_UPDATE_REQUEST 0x0A
  86. #define PD_MSGTYPE_FIRMWARE_UPDATE_RESPONSE 0x0B
  87. #define PD_MSGTYPE_PPS_STATUS 0x0C
  88. #define PD_MSGTYPE_COUNTRY_INFO 0x0D
  89. #define PD_MSGTYPE_COUNTRY_CODES 0x0E
  90. /* Data roles */
  91. #define PD_DATAROLE_UFP (0x0 << PD_HDR_DATAROLE_SHIFT)
  92. #define PD_DATAROLE_DFP (0x1 << PD_HDR_DATAROLE_SHIFT)
  93. /* Specification revisions */
  94. #define PD_SPECREV_1_0 (0x0 << PD_HDR_SPECREV_SHIFT)
  95. #define PD_SPECREV_2_0 (0x1 << PD_HDR_SPECREV_SHIFT)
  96. #define PD_SPECREV_3_0 (0x2 << PD_HDR_SPECREV_SHIFT)
  97. /* Port power roles */
  98. #define PD_POWERROLE_SINK (0x0 << PD_HDR_POWERROLE_SHIFT)
  99. #define PD_POWERROLE_SOURCE (0x1 << PD_HDR_POWERROLE_SHIFT)
  100. /* Message ID */
  101. #define PD_MESSAGEID_GET(msg) (((msg)->hdr & PD_HDR_MESSAGEID) >> PD_HDR_MESSAGEID_SHIFT)
  102. /* Number of data objects */
  103. #define PD_NUMOBJ(n) (((n) << PD_HDR_NUMOBJ_SHIFT) & PD_HDR_NUMOBJ)
  104. #define PD_NUMOBJ_GET(msg) (((msg)->hdr & PD_HDR_NUMOBJ) >> PD_HDR_NUMOBJ_SHIFT)
  105. /*
  106. * PD Extended Message Header
  107. */
  108. #define PD_EXTHDR_DATA_SIZE_SHIFT 0
  109. #define PD_EXTHDR_DATA_SIZE (0x1FF << PD_EXTHDR_DATA_SIZE_SHIFT)
  110. #define PD_EXTHDR_REQUEST_CHUNK_SHIFT 10
  111. #define PD_EXTHDR_REQUEST_CHUNK (1 << PD_EXTHDR_REQUEST_CHUNK_SHIFT)
  112. #define PD_EXTHDR_CHUNK_NUMBER_SHIFT 11
  113. #define PD_EXTHDR_CHUNK_NUMBER (0xF << PD_EXTHDR_CHUNK_NUMBER_SHIFT)
  114. #define PD_EXTHDR_CHUNKED_SHIFT 15
  115. #define PD_EXTHDR_CHUNKED (1 << PD_EXTHDR_CHUNKED_SHIFT)
  116. /* Data size */
  117. #define PD_DATA_SIZE(n) (((n) << PD_EXTHDR_DATA_SIZE_SHIFT) & PD_EXTHDR_DATA_SIZE)
  118. #define PD_DATA_SIZE_GET(msg) (((msg)->exthdr & PD_EXTHDR_DATA_SIZE) >> PD_EXTHDR_DATA_SIZE_SHIFT)
  119. /* Chunk number */
  120. #define PD_CHUNK_NUMBER(n) (((n) << PD_EXTHDR_CHUNK_NUMBER_SHIFT) & PD_EXTHDR_CHUNK_NUMBER)
  121. #define PD_CHUNK_NUMBER_GET(msg) (((msg)->exthdr & PD_EXTHDR_CHUNK_NUMBER) >> PD_EXTHDR_CHUNK_NUMBER_SHIFT)
  122. /*
  123. * PD Power Data Object
  124. */
  125. #define PD_PDO_TYPE_SHIFT 30
  126. #define PD_PDO_TYPE (0x3 << PD_PDO_TYPE_SHIFT)
  127. /* PDO types */
  128. #define PD_PDO_TYPE_FIXED ((unsigned) (0x0 << PD_PDO_TYPE_SHIFT))
  129. #define PD_PDO_TYPE_BATTERY ((unsigned) (0x1 << PD_PDO_TYPE_SHIFT))
  130. #define PD_PDO_TYPE_VARIABLE ((unsigned) (0x2 << PD_PDO_TYPE_SHIFT))
  131. #define PD_PDO_TYPE_AUGMENTED ((unsigned) (0x3 << PD_PDO_TYPE_SHIFT))
  132. #define PD_APDO_TYPE_SHIFT 28
  133. #define PD_APDO_TYPE (0x3 << PD_APDO_TYPE_SHIFT)
  134. /* APDO types */
  135. #define PD_APDO_TYPE_PPS (0x0 << PD_APDO_TYPE_SHIFT)
  136. /* PD Source Fixed PDO */
  137. #define PD_PDO_SRC_FIXED_DUAL_ROLE_PWR_SHIFT 29
  138. #define PD_PDO_SRC_FIXED_DUAL_ROLE_PWR (1 << PD_PDO_SRC_FIXED_DUAL_ROLE_PWR_SHIFT)
  139. #define PD_PDO_SRC_FIXED_USB_SUSPEND_SHIFT 28
  140. #define PD_PDO_SRC_FIXED_USB_SUSPEND (1 << PD_PDO_SRC_FIXED_USB_SUSPEND_SHIFT)
  141. #define PD_PDO_SRC_FIXED_UNCONSTRAINED_SHIFT 27
  142. #define PD_PDO_SRC_FIXED_UNCONSTRAINED (1 << PD_PDO_SRC_FIXED_UNCONSTRAINED_SHIFT)
  143. #define PD_PDO_SRC_FIXED_USB_COMMS_SHIFT 26
  144. #define PD_PDO_SRC_FIXED_USB_COMMS (1 << PD_PDO_SRC_FIXED_USB_COMMS_SHIFT)
  145. #define PD_PDO_SRC_FIXED_DUAL_ROLE_DATA_SHIFT 25
  146. #define PD_PDO_SRC_FIXED_DUAL_ROLE_DATA (1 << PD_PDO_SRC_FIXED_DUAL_ROLE_DATA_SHIFT)
  147. #define PD_PDO_SRC_FIXED_PEAK_CURRENT_SHIFT 20
  148. #define PD_PDO_SRC_FIXED_PEAK_CURRENT (0x3 << PD_PDO_SRC_FIXED_PEAK_CURRENT_SHIFT)
  149. #define PD_PDO_SRC_FIXED_VOLTAGE_SHIFT 10
  150. #define PD_PDO_SRC_FIXED_VOLTAGE (0x3FF << PD_PDO_SRC_FIXED_VOLTAGE_SHIFT)
  151. #define PD_PDO_SRC_FIXED_CURRENT_SHIFT 0
  152. #define PD_PDO_SRC_FIXED_CURRENT (0x3FF << PD_PDO_SRC_FIXED_CURRENT_SHIFT)
  153. /* PD Source Fixed PDO current */
  154. #define PD_PDO_SRC_FIXED_CURRENT_GET(pdo) (((pdo) & PD_PDO_SRC_FIXED_CURRENT) >> PD_PDO_SRC_FIXED_CURRENT_SHIFT)
  155. /* PD Source Fixed PDO voltage */
  156. #define PD_PDO_SRC_FIXED_VOLTAGE_GET(pdo) (((pdo) & PD_PDO_SRC_FIXED_VOLTAGE) >> PD_PDO_SRC_FIXED_VOLTAGE_SHIFT)
  157. /* PD Programmable Power Supply APDO */
  158. #define PD_APDO_PPS_MAX_VOLTAGE_SHIFT 17
  159. #define PD_APDO_PPS_MAX_VOLTAGE (0xFF << PD_APDO_PPS_MAX_VOLTAGE_SHIFT)
  160. #define PD_APDO_PPS_MIN_VOLTAGE_SHIFT 8
  161. #define PD_APDO_PPS_MIN_VOLTAGE (0xFF << PD_APDO_PPS_MIN_VOLTAGE_SHIFT)
  162. #define PD_APDO_PPS_CURRENT_SHIFT 0
  163. #define PD_APDO_PPS_CURRENT (0x7F << PD_APDO_PPS_CURRENT_SHIFT)
  164. /* PD Programmable Power Supply APDO voltages */
  165. #define PD_APDO_PPS_MAX_VOLTAGE_GET(pdo) (((pdo) & PD_APDO_PPS_MAX_VOLTAGE) >> PD_APDO_PPS_MAX_VOLTAGE_SHIFT)
  166. #define PD_APDO_PPS_MIN_VOLTAGE_GET(pdo) (((pdo) & PD_APDO_PPS_MIN_VOLTAGE) >> PD_APDO_PPS_MIN_VOLTAGE_SHIFT)
  167. #define PD_APDO_PPS_MAX_VOLTAGE_SET(v) (((v) << PD_APDO_PPS_MAX_VOLTAGE_SHIFT) & PD_APDO_PPS_MAX_VOLTAGE)
  168. #define PD_APDO_PPS_MIN_VOLTAGE_SET(v) (((v) << PD_APDO_PPS_MIN_VOLTAGE_SHIFT) & PD_APDO_PPS_MIN_VOLTAGE)
  169. /* PD Programmable Power Supply APDO current */
  170. #define PD_APDO_PPS_CURRENT_GET(pdo) ((uint8_t) (((pdo) & PD_APDO_PPS_CURRENT) >> PD_APDO_PPS_CURRENT_SHIFT))
  171. #define PD_APDO_PPS_CURRENT_SET(i) (((i) << PD_APDO_PPS_CURRENT_SHIFT) & PD_APDO_PPS_CURRENT)
  172. /* TODO: other types of source PDO */
  173. /* PD Sink Fixed PDO */
  174. #define PD_PDO_SNK_FIXED_DUAL_ROLE_PWR_SHIFT 29
  175. #define PD_PDO_SNK_FIXED_DUAL_ROLE_PWR (1 << PD_PDO_SNK_FIXED_DUAL_ROLE_PWR_SHIFT)
  176. #define PD_PDO_SNK_FIXED_HIGHER_CAP_SHIFT 28
  177. #define PD_PDO_SNK_FIXED_HIGHER_CAP (1 << PD_PDO_SNK_FIXED_HIGHER_CAP_SHIFT)
  178. #define PD_PDO_SNK_FIXED_UNCONSTRAINED_SHIFT 27
  179. #define PD_PDO_SNK_FIXED_UNCONSTRAINED (1 << PD_PDO_SNK_FIXED_UNCONSTRAINED_SHIFT)
  180. #define PD_PDO_SNK_FIXED_USB_COMMS_SHIFT 26
  181. #define PD_PDO_SNK_FIXED_USB_COMMS (1 << PD_PDO_SNK_FIXED_USB_COMMS_SHIFT)
  182. #define PD_PDO_SNK_FIXED_DUAL_ROLE_DATA_SHIFT 25
  183. #define PD_PDO_SNK_FIXED_DUAL_ROLE_DATA (1 << PD_PDO_SNK_FIXED_DUAL_ROLE_DATA_SHIFT)
  184. #define PD_PDO_SNK_FIXED_VOLTAGE_SHIFT 10
  185. #define PD_PDO_SNK_FIXED_VOLTAGE (0x3FF << PD_PDO_SNK_FIXED_VOLTAGE_SHIFT)
  186. #define PD_PDO_SNK_FIXED_CURRENT_SHIFT 0
  187. #define PD_PDO_SNK_FIXED_CURRENT (0x3FF << PD_PDO_SNK_FIXED_CURRENT_SHIFT)
  188. /* PD Sink Fixed PDO current */
  189. #define PD_PDO_SNK_FIXED_CURRENT_SET(i) (((i) << PD_PDO_SNK_FIXED_CURRENT_SHIFT) & PD_PDO_SNK_FIXED_CURRENT)
  190. /* PD Sink Fixed PDO voltage */
  191. #define PD_PDO_SNK_FIXED_VOLTAGE_SET(v) (((v) << PD_PDO_SNK_FIXED_VOLTAGE_SHIFT) & PD_PDO_SNK_FIXED_VOLTAGE)
  192. /* TODO: other types of sink PDO */
  193. /*
  194. * PD Request Data Object
  195. */
  196. #define PD_RDO_OBJPOS_SHIFT 28
  197. #define PD_RDO_OBJPOS (0x7 << PD_RDO_OBJPOS_SHIFT)
  198. #define PD_RDO_GIVEBACK_SHIFT 27
  199. #define PD_RDO_GIVEBACK (1 << PD_RDO_GIVEBACK_SHIFT)
  200. #define PD_RDO_CAP_MISMATCH_SHIFT 26
  201. #define PD_RDO_CAP_MISMATCH (1 << PD_RDO_CAP_MISMATCH_SHIFT)
  202. #define PD_RDO_USB_COMMS_SHIFT 25
  203. #define PD_RDO_USB_COMMS (1 << PD_RDO_USB_COMMS_SHIFT)
  204. #define PD_RDO_NO_USB_SUSPEND_SHIFT 24
  205. #define PD_RDO_NO_USB_SUSPEND (1 << PD_RDO_NO_USB_SUSPEND_SHIFT)
  206. #define PD_RDO_UNCHUNKED_EXT_MSG_SHIFT 23
  207. #define PD_RDO_UNCHUNKED_EXT_MSG (1 << PD_RDO_UNCHUNKED_EXT_MSG_SHIFT)
  208. #define PD_RDO_OBJPOS_SET(i) (((i) << PD_RDO_OBJPOS_SHIFT) & PD_RDO_OBJPOS)
  209. #define PD_RDO_OBJPOS_GET(msg) (((msg)->obj[0] & PD_RDO_OBJPOS) >> PD_RDO_OBJPOS_SHIFT)
  210. /* Fixed and Variable RDO, no GiveBack support */
  211. #define PD_RDO_FV_CURRENT_SHIFT 10
  212. #define PD_RDO_FV_CURRENT (0x3FF << PD_RDO_FV_CURRENT_SHIFT)
  213. #define PD_RDO_FV_MAX_CURRENT_SHIFT 0
  214. #define PD_RDO_FV_MAX_CURRENT (0x3FF << PD_RDO_FV_MAX_CURRENT_SHIFT)
  215. #define PD_RDO_FV_CURRENT_SET(i) (((i) << PD_RDO_FV_CURRENT_SHIFT) & PD_RDO_FV_CURRENT)
  216. #define PD_RDO_FV_MAX_CURRENT_SET(i) (((i) << PD_RDO_FV_MAX_CURRENT_SHIFT) & PD_RDO_FV_MAX_CURRENT)
  217. /* Fixed and Variable RDO with GiveBack support */
  218. #define PD_RDO_FV_MIN_CURRENT_SHIFT 0
  219. #define PD_RDO_FV_MIN_CURRENT (0x3FF << PD_RDO_FV_MIN_CURRENT_SHIFT)
  220. #define PD_RDO_FV_MIN_CURRENT_SET(i) (((i) << PD_RDO_FV_MIN_CURRENT_SHIFT) & PD_RDO_FV_MIN_CURRENT)
  221. /* TODO: Battery RDOs */
  222. /* Programmable RDO */
  223. #define PD_RDO_PROG_VOLTAGE_SHIFT 9
  224. #define PD_RDO_PROG_VOLTAGE (0x7FF << PD_RDO_PROG_VOLTAGE_SHIFT)
  225. #define PD_RDO_PROG_CURRENT_SHIFT 0
  226. #define PD_RDO_PROG_CURRENT (0x7F << PD_RDO_PROG_CURRENT_SHIFT)
  227. #define PD_RDO_PROG_VOLTAGE_SET(i) (((i) << PD_RDO_PROG_VOLTAGE_SHIFT) & PD_RDO_PROG_VOLTAGE)
  228. #define PD_RDO_PROG_CURRENT_SET(i) (((i) << PD_RDO_PROG_CURRENT_SHIFT) & PD_RDO_PROG_CURRENT)
  229. /*
  230. * Time values
  231. *
  232. * Where a range is specified, the middle of the range (rounded down to the
  233. * nearest millisecond) is used.
  234. */
  235. #define PD_T_CHUNKING_NOT_SUPPORTED MS2ST(45)
  236. #define PD_T_HARD_RESET_COMPLETE MS2ST(4)
  237. #define PD_T_PS_TRANSITION MS2ST(500)
  238. #define PD_T_SENDER_RESPONSE MS2ST(27)
  239. #define PD_T_SINK_REQUEST MS2ST(100)
  240. #define PD_T_TYPEC_SINK_WAIT_CAP MS2ST(465)
  241. #define PD_T_PPS_REQUEST S2ST(10)
  242. /* This is actually from Type-C, not Power Delivery, but who cares? */
  243. #define PD_T_PD_DEBOUNCE MS2ST(15)
  244. /*
  245. * Counter maximums
  246. */
  247. #define PD_N_HARD_RESET_COUNT 2
  248. /*
  249. * Value parameters
  250. */
  251. #define PD_MAX_EXT_MSG_LEN 260
  252. #define PD_MAX_EXT_MSG_CHUNK_LEN 26
  253. #define PD_MAX_EXT_MSG_LEGACY_LEN 26
  254. /*
  255. * Unit conversions
  256. *
  257. * V: volt
  258. * CV: centivolt
  259. * MV: millivolt
  260. * PRV: Programmable RDO voltage unit (20 mV)
  261. * PDV: Power Delivery voltage unit (50 mV)
  262. * PAV: PPS APDO voltage unit (100 mV)
  263. *
  264. * A: ampere
  265. * CA: centiampere
  266. * MA: milliampere
  267. * PDI: Power Delivery current unit (10 mA)
  268. * PAI: PPS APDO current unit (50 mA)
  269. *
  270. * W: watt
  271. * CW: centiwatt
  272. * MW: milliwatt
  273. *
  274. * O: ohm
  275. * CO: centiohm
  276. * MO: milliohm
  277. */
  278. #define PD_MV2PRV(mv) ((mv) / 20)
  279. #define PD_MV2PDV(mv) ((mv) / 50)
  280. #define PD_MV2PAV(mv) ((mv) / 100)
  281. #define PD_PRV2MV(prv) ((prv) * 20)
  282. #define PD_PDV2MV(pdv) ((pdv) * 50)
  283. #define PD_PAV2MV(pav) ((pav) * 100)
  284. #define PD_MA2CA(ma) (((ma) + 10 - 1) / 10)
  285. #define PD_MA2PDI(ma) (((ma) + 10 - 1) / 10)
  286. #define PD_MA2PAI(ma) (((ma) + 50 - 1) / 50)
  287. #define PD_CA2PAI(ca) (((ca) + 5 - 1) / 5)
  288. #define PD_PDI2MA(pdi) ((pdi) * 10)
  289. #define PD_PAI2MA(pai) ((pai) * 50)
  290. #define PD_PAI2CA(pai) ((pai) * 5)
  291. #define PD_MW2CW(mw) ((mw) / 10)
  292. #define PD_MO2CO(mo) ((mo) / 10)
  293. /* Get portions of a voltage in more normal units */
  294. #define PD_MV_V(mv) ((mv) / 1000)
  295. #define PD_MV_MV(mv) ((mv) % 1000)
  296. #define PD_PDV_V(pdv) ((pdv) / 20)
  297. #define PD_PDV_CV(pdv) (5 * ((pdv) % 20))
  298. #define PD_PAV_V(pav) ((pav) / 10)
  299. #define PD_PAV_CV(pav) (10 * ((pav) % 10))
  300. /* Get portions of a PD current in more normal units */
  301. #define PD_PDI_A(pdi) ((pdi) / 100)
  302. #define PD_PDI_CA(pdi) ((pdi) % 100)
  303. #define PD_PAI_A(pai) ((pai) / 20)
  304. #define PD_PAI_CA(pai) (5 * ((pai) % 20))
  305. /* Get portions of a power in more normal units */
  306. #define PD_CW_W(cw) ((cw) / 100)
  307. #define PD_CW_CW(cw) ((cw) % 100)
  308. /* Get portions of a resistance in more normal units */
  309. #define PD_CO_O(co) ((co) / 100)
  310. #define PD_CO_CO(co) ((co) % 100)
  311. /*
  312. * Unit constants
  313. */
  314. #define PD_MV_MIN 0
  315. #define PD_MV_MAX 21000
  316. #define PD_PDV_MIN PD_MV2PDV(PD_MV_MIN)
  317. #define PD_PDV_MAX PD_MV2PDV(PD_MV_MAX)
  318. #define PD_MA_MIN 0
  319. #define PD_MA_MAX 5000
  320. #define PD_CA_MIN PD_MA2CA(PD_MA_MIN)
  321. #define PD_CA_MAX PD_MA2CA(PD_MA_MAX)
  322. #define PD_PDI_MIN PD_MA2PDI(PD_MA_MIN)
  323. #define PD_PDI_MAX PD_MA2PDI(PD_MA_MAX)
  324. #define PD_MW_MIN 0
  325. #define PD_MW_MAX 100000
  326. #define PD_MO_MIN 500
  327. #define PD_MO_MAX 655350
  328. #endif /* PDB_PD_H */