ethernet_enc28j60.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /****************************************************************************
  2. * csky/hardware/bsp/common/ethernet_enc28j60/ethernet_enc28j60.h
  3. *
  4. * Copyright (C) 2016 The YunOS Open Source Project
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ****************************************************************************/
  19. #ifndef _ETHERNET_ENC28J60_H__
  20. #define _ETHERNET_ENC28J60_H__
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include <stdint.h>
  25. /* ****** ETH ****** */
  26. #define ETH_HEADER_LEN 14
  27. /* values of certain bytes: */
  28. #define ETHTYPE_ARP_H_V 0x08
  29. #define ETHTYPE_ARP_L_V 0x06
  30. #define ETHTYPE_IP_H_V 0x08
  31. #define ETHTYPE_IP_L_V 0x00
  32. /* byte positions in the ethernet frame:
  33. Ethernet type field (2bytes): */
  34. #define ETH_TYPE_H_P 12
  35. #define ETH_TYPE_L_P 13
  36. #define ETH_DST_MAC 0
  37. #define ETH_SRC_MAC 6
  38. /* ******* ARP ******* */
  39. #define ETH_ARP_OPCODE_REPLY_H_V 0x0
  40. #define ETH_ARP_OPCODE_REPLY_L_V 0x02
  41. #define ETHTYPE_ARP_L_V 0x06
  42. /* arp.dst.ip */
  43. #define ETH_ARP_DST_IP_P 0x26
  44. /* arp.opcode */
  45. #define ETH_ARP_OPCODE_H_P 0x14
  46. #define ETH_ARP_OPCODE_L_P 0x15
  47. /* arp.src.mac */
  48. #define ETH_ARP_SRC_MAC_P 0x16
  49. #define ETH_ARP_SRC_IP_P 0x1c
  50. #define ETH_ARP_DST_MAC_P 0x20
  51. #define ETH_ARP_DST_IP_P 0x26
  52. /* ******* IP ******* */
  53. #define IP_HEADER_LEN 20
  54. /* ip.src */
  55. #define IP_SRC_P 0x1a
  56. #define IP_DST_P 0x1e
  57. #define IP_HEADER_LEN_VER_P 0xe
  58. #define IP_CHECKSUM_P 0x18
  59. #define IP_TTL_P 0x16
  60. #define IP_FLAGS_P 0x14
  61. #define IP_P 0xe
  62. #define IP_TOTLEN_H_P 0x10
  63. #define IP_TOTLEN_L_P 0x11
  64. #define IP_PROTO_P 0x17
  65. #define IP_PROTO_ICMP_V 1
  66. #define IP_PROTO_TCP_V 6
  67. /* 17=0x11 */
  68. #define IP_PROTO_UDP_V 17
  69. /* ******* ICMP ******* */
  70. #define ICMP_TYPE_ECHOREPLY_V 0
  71. #define ICMP_TYPE_ECHOREQUEST_V 8
  72. #define ICMP_TYPE_P 0x22
  73. #define ICMP_CHECKSUM_P 0x24
  74. /* ******* UDP ******* */
  75. #define UDP_HEADER_LEN 8
  76. #define UDP_SRC_PORT_H_P 0x22
  77. #define UDP_SRC_PORT_L_P 0x23
  78. #define UDP_DST_PORT_H_P 0x24
  79. #define UDP_DST_PORT_L_P 0x25
  80. #define UDP_LEN_H_P 0x26
  81. #define UDP_LEN_L_P 0x27
  82. #define UDP_CHECKSUM_H_P 0x28
  83. #define UDP_CHECKSUM_L_P 0x29
  84. #define UDP_DATA_P 0x2a
  85. /* ******* TCP ******* */
  86. #define TCP_SRC_PORT_H_P 0x22
  87. #define TCP_SRC_PORT_L_P 0x23
  88. #define TCP_DST_PORT_H_P 0x24
  89. #define TCP_DST_PORT_L_P 0x25
  90. /* the tcp seq number is 4 bytes 0x26-0x29 */
  91. #define TCP_SEQ_H_P 0x26
  92. #define TCP_SEQACK_H_P 0x2a
  93. /* flags: SYN=2 */
  94. #define TCP_FLAGS_P 0x2f
  95. #define TCP_FLAGS_SYN_V 2
  96. #define TCP_FLAGS_FIN_V 1
  97. #define TCP_FLAGS_PUSH_V 8
  98. #define TCP_FLAGS_SYNACK_V 0x12
  99. #define TCP_FLAGS_ACK_V 0x10
  100. #define TCP_FLAGS_PSHACK_V 0x18
  101. /* plain len without the options: */
  102. #define TCP_HEADER_LEN_PLAIN 20
  103. #define TCP_HEADER_LEN_P 0x2e
  104. #define TCP_CHECKSUM_H_P 0x32
  105. #define TCP_CHECKSUM_L_P 0x33
  106. #define TCP_OPTIONS_P 0x36
  107. /* ENC28J60 Control Registers
  108. Control register definitions are a combination of address,
  109. bank number, and Ethernet/MAC/PHY indicator bits.
  110. - Register address (bits 0-4)
  111. - Bank number (bits 5-6)
  112. - MAC/PHY indicator (bit 7) */
  113. #define ADDR_MASK 0x1F
  114. #define BANK_MASK 0x60
  115. #define SPRD_MASK 0x80
  116. /* All-bank registers */
  117. #define EIE 0x1B
  118. #define EIR 0x1C
  119. #define ESTAT 0x1D
  120. #define ECON2 0x1E
  121. #define ECON1 0x1F
  122. /* Bank 0 registers */
  123. #define ERDPTL (0x00|0x00)
  124. #define ERDPTH (0x01|0x00)
  125. #define EWRPTL (0x02|0x00)
  126. #define EWRPTH (0x03|0x00)
  127. #define ETXSTL (0x04|0x00)
  128. #define ETXSTH (0x05|0x00)
  129. #define ETXNDL (0x06|0x00)
  130. #define ETXNDH (0x07|0x00)
  131. #define ERXSTL (0x08|0x00)
  132. #define ERXSTH (0x09|0x00)
  133. #define ERXNDL (0x0A|0x00)
  134. #define ERXNDH (0x0B|0x00)
  135. #define ERXRDPTL (0x0C|0x00)
  136. #define ERXRDPTH (0x0D|0x00)
  137. #define ERXWRPTL (0x0E|0x00)
  138. #define ERXWRPTH (0x0F|0x00)
  139. #define EDMASTL (0x10|0x00)
  140. #define EDMASTH (0x11|0x00)
  141. #define EDMANDL (0x12|0x00)
  142. #define EDMANDH (0x13|0x00)
  143. #define EDMADSTL (0x14|0x00)
  144. #define EDMADSTH (0x15|0x00)
  145. #define EDMACSL (0x16|0x00)
  146. #define EDMACSH (0x17|0x00)
  147. /* Bank 1 registers */
  148. #define EHT0 (0x00|0x20)
  149. #define EHT1 (0x01|0x20)
  150. #define EHT2 (0x02|0x20)
  151. #define EHT3 (0x03|0x20)
  152. #define EHT4 (0x04|0x20)
  153. #define EHT5 (0x05|0x20)
  154. #define EHT6 (0x06|0x20)
  155. #define EHT7 (0x07|0x20)
  156. #define EPMM0 (0x08|0x20)
  157. #define EPMM1 (0x09|0x20)
  158. #define EPMM2 (0x0A|0x20)
  159. #define EPMM3 (0x0B|0x20)
  160. #define EPMM4 (0x0C|0x20)
  161. #define EPMM5 (0x0D|0x20)
  162. #define EPMM6 (0x0E|0x20)
  163. #define EPMM7 (0x0F|0x20)
  164. #define EPMCSL (0x10|0x20)
  165. #define EPMCSH (0x11|0x20)
  166. #define EPMOL (0x14|0x20)
  167. #define EPMOH (0x15|0x20)
  168. #define EWOLIE (0x16|0x20)
  169. #define EWOLIR (0x17|0x20)
  170. #define ERXFCON (0x18|0x20)
  171. #define EPKTCNT (0x19|0x20)
  172. /* Bank 2 registers */
  173. #define MACON1 (0x00|0x40|0x80)
  174. #define MACON2 (0x01|0x40|0x80)
  175. #define MACON3 (0x02|0x40|0x80)
  176. #define MACON4 (0x03|0x40|0x80)
  177. #define MABBIPG (0x04|0x40|0x80)
  178. #define MAIPGL (0x06|0x40|0x80)
  179. #define MAIPGH (0x07|0x40|0x80)
  180. #define MACLCON1 (0x08|0x40|0x80)
  181. #define MACLCON2 (0x09|0x40|0x80)
  182. #define MAMXFLL (0x0A|0x40|0x80)
  183. #define MAMXFLH (0x0B|0x40|0x80)
  184. #define MAPHSUP (0x0D|0x40|0x80)
  185. #define MICON (0x11|0x40|0x80)
  186. #define MICMD (0x12|0x40|0x80)
  187. #define MIREGADR (0x14|0x40|0x80)
  188. #define MIWRL (0x16|0x40|0x80)
  189. #define MIWRH (0x17|0x40|0x80)
  190. #define MIRDL (0x18|0x40|0x80)
  191. #define MIRDH (0x19|0x40|0x80)
  192. /* Bank 3 registers */
  193. #define MAADR1 (0x00|0x60|0x80)
  194. #define MAADR0 (0x01|0x60|0x80)
  195. #define MAADR3 (0x02|0x60|0x80)
  196. #define MAADR2 (0x03|0x60|0x80)
  197. #define MAADR5 (0x04|0x60|0x80)
  198. #define MAADR4 (0x05|0x60|0x80)
  199. #define EBSTSD (0x06|0x60)
  200. #define EBSTCON (0x07|0x60)
  201. #define EBSTCSL (0x08|0x60)
  202. #define EBSTCSH (0x09|0x60)
  203. #define MISTAT (0x0A|0x60|0x80)
  204. #define EREVID (0x12|0x60)
  205. #define ECOCON (0x15|0x60)
  206. #define EFLOCON (0x17|0x60)
  207. #define EPAUSL (0x18|0x60)
  208. #define EPAUSH (0x19|0x60)
  209. /* PHY registers */
  210. #define PHCON1 0x00
  211. #define PHSTAT1 0x01
  212. #define PHHID1 0x02
  213. #define PHHID2 0x03
  214. #define PHCON2 0x10
  215. #define PHSTAT2 0x11
  216. #define PHIE 0x12
  217. #define PHIR 0x13
  218. #define PHLCON 0x14
  219. /* ENC28J60 ERXFCON Register Bit Definitions */
  220. #define ERXFCON_UCEN 0x80
  221. #define ERXFCON_ANDOR 0x40
  222. #define ERXFCON_CRCEN 0x20
  223. #define ERXFCON_PMEN 0x10
  224. #define ERXFCON_MPEN 0x08
  225. #define ERXFCON_HTEN 0x04
  226. #define ERXFCON_MCEN 0x02
  227. #define ERXFCON_BCEN 0x01
  228. /* ENC28J60 EIE Register Bit Definitions */
  229. #define EIE_INTIE 0x80
  230. #define EIE_PKTIE 0x40
  231. #define EIE_DMAIE 0x20
  232. #define EIE_LINKIE 0x10
  233. #define EIE_TXIE 0x08
  234. #define EIE_WOLIE 0x04
  235. #define EIE_TXERIE 0x02
  236. #define EIE_RXERIE 0x01
  237. #define EIE_ALLCLOSE 0xff
  238. /* ENC28J60 EIR Register Bit Definitions */
  239. #define EIR_PKTIF 0x40
  240. #define EIR_DMAIF 0x20
  241. #define EIR_LINKIF 0x10
  242. #define EIR_TXIF 0x08
  243. #define EIR_WOLIF 0x04
  244. #define EIR_TXERIF 0x02
  245. #define EIR_RXERIF 0x01
  246. #define EIR_ALLINTS 0x7b /* All interrupts */
  247. /* ENC28J60 ESTAT Register Bit Definitions */
  248. #define ESTAT_INT 0x80
  249. #define ESTAT_LATECOL 0x10
  250. #define ESTAT_RXBUSY 0x04
  251. #define ESTAT_TXABRT 0x02
  252. #define ESTAT_CLKRDY 0x01
  253. /* ENC28J60 ECON2 Register Bit Definitions */
  254. #define ECON2_AUTOINC 0x80
  255. #define ECON2_PKTDEC 0x40
  256. #define ECON2_PWRSV 0x20
  257. #define ECON2_VRPS 0x08
  258. /* ENC28J60 ECON1 Register Bit Definitions */
  259. #define ECON1_TXRST 0x80
  260. #define ECON1_RXRST 0x40
  261. #define ECON1_DMAST 0x20
  262. #define ECON1_CSUMEN 0x10
  263. #define ECON1_TXRTS 0x08
  264. #define ECON1_RXEN 0x04
  265. #define ECON1_BSEL1 0x02
  266. #define ECON1_BSEL0 0x01
  267. /* ENC28J60 MACON1 Register Bit Definitions */
  268. #define MACON1_LOOPBK 0x10
  269. #define MACON1_TXPAUS 0x08
  270. #define MACON1_RXPAUS 0x04
  271. #define MACON1_PASSALL 0x02
  272. #define MACON1_MARXEN 0x01
  273. /* ENC28J60 MACON2 Register Bit Definitions */
  274. #define MACON2_MARST 0x80
  275. #define MACON2_RNDRST 0x40
  276. #define MACON2_MARXRST 0x08
  277. #define MACON2_RFUNRST 0x04
  278. #define MACON2_MATXRST 0x02
  279. #define MACON2_TFUNRST 0x01
  280. /* ENC28J60 MACON3 Register Bit Definitions */
  281. #define MACON3_PADCFG2 0x80
  282. #define MACON3_PADCFG1 0x40
  283. #define MACON3_PADCFG0 0x20
  284. #define MACON3_TXCRCEN 0x10
  285. #define MACON3_PHDRLEN 0x08
  286. #define MACON3_HFRMLEN 0x04
  287. #define MACON3_FRMLNEN 0x02
  288. #define MACON3_FULDPX 0x01
  289. /* ENC28J60 MICMD Register Bit Definitions */
  290. #define MICMD_MIISCAN 0x02
  291. #define MICMD_MIIRD 0x01
  292. /* ENC28J60 MISTAT Register Bit Definitions */
  293. #define MISTAT_NVALID 0x04
  294. #define MISTAT_SCAN 0x02
  295. #define MISTAT_BUSY 0x01
  296. /* ENC28J60 PHY PHCON1 Register Bit Definitions */
  297. #define PHCON1_PRST 0x8000
  298. #define PHCON1_PLOOPBK 0x4000
  299. #define PHCON1_PPWRSV 0x0800
  300. #define PHCON1_PDPXMD 0x0100
  301. /* ENC28J60 PHY PHSTAT1 Register Bit Definitions */
  302. #define PHSTAT1_PFDPX 0x1000
  303. #define PHSTAT1_PHDPX 0x0800
  304. #define PHSTAT1_LLSTAT 0x0004
  305. #define PHSTAT1_JBSTAT 0x0002
  306. /* ENC28J60 PHY PHCON2 Register Bit Definitions */
  307. #define PHCON2_FRCLINK 0x4000
  308. #define PHCON2_TXDIS 0x2000
  309. #define PHCON2_JABBER 0x0400
  310. #define PHCON2_HDLDIS 0x0100
  311. /* ENC28J60 PHY PHIE Register Bit Definitions */
  312. #define PHIE_PLNKIE 0x0010
  313. #define PHIE_PGEIE 0x0002
  314. /* ENC28J60 Packet Control Byte Bit Definitions */
  315. #define PKTCTRL_PHUGEEN 0x08
  316. #define PKTCTRL_PPADEN 0x04
  317. #define PKTCTRL_PCRCEN 0x02
  318. #define PKTCTRL_POVERRIDE 0x01
  319. /* SPI operation codes */
  320. #define ENC28J60_READ_CTRL_REG 0x00
  321. #define ENC28J60_READ_BUF_MEM 0x3A
  322. #define ENC28J60_WRITE_CTRL_REG 0x40
  323. #define ENC28J60_WRITE_BUF_MEM 0x7A
  324. #define ENC28J60_BIT_FIELD_SET 0x80
  325. #define ENC28J60_BIT_FIELD_CLR 0xA0
  326. #define ENC28J60_SOFT_RESET 0xFF
  327. /* The RXSTART_INIT should be zero. See Rev. B4 Silicon Errata
  328. buffer boundaries applied to internal 8K ram
  329. the entire available packet buffer space is allocated
  330. start with recbuf at 0/ */
  331. #define RXSTART_INIT 0x0
  332. /* receive buffer end */
  333. #define RXSTOP_INIT (0x1FFF-0x0600-1)
  334. /* start TX buffer at 0x1FFF-0x0600, pace for one full ethernet frame (~1500 bytes) */
  335. #define TXSTART_INIT (0x1FFF-0x0600)
  336. /* stp TX buffer at end of mem */
  337. #define TXSTOP_INIT 0x1FFF
  338. /* max frame length which the conroller will accept: */
  339. #define MAX_FRAMELEN 1518 /* (note: maximum ethernet frame length would be 1518) */
  340. void enc28j60_spi_cs_status_change(int status);
  341. #if 1//defined CONFIG_PHOBOS_GENERAL
  342. #define PA5_A8 15
  343. #define PA1 12
  344. #define ENC28J60_CSL() enc28j60_spi_cs_status_change(0); //yunos_bsp_gpio_set_value(20, GPIO_VALUE_LOW); /* SPI_CS_LOW */
  345. #define ENC28J60_CSH() enc28j60_spi_cs_status_change(1); //yunos_bsp_gpio_set_value(20, GPIO_VALUE_HIGH); /* SPI_CS_HIGH */
  346. #else
  347. #define PA5_A8 47
  348. #define ENC28J60_CSL() enc28j60_spi_cs_status_change(0); //yunos_bsp_gpio_set_value(44, GPIO_VALUE_LOW); /* SPI_CS_LOW */
  349. #define ENC28J60_CSH() enc28j60_spi_cs_status_change(1); //yunos_bsp_gpio_set_value(44, GPIO_VALUE_HIGH); /* SPI_CS_HIGH */
  350. #endif
  351. typedef struct _spi_net_ops_t {
  352. int (*init)(const uint8_t *macaddr);
  353. int (*recv)(uint8_t *, uint16_t);
  354. int (*send)(uint8_t *, uint16_t);
  355. int (*reset)(void);
  356. int (*irq_enable)(int);
  357. int (*set_macaddr)(const uint8_t *macaddr);
  358. int (*get_link_status)(void);
  359. } net_ops_t;
  360. enum enc28j60_reset {
  361. RST_ENC28J60_ALL,
  362. RST_ENC28J60_TX,
  363. RST_ENC28J60_RX
  364. };
  365. int yunos_bsp_enc28j60_init(const uint8_t *macaddr);
  366. int yunos_bsp_enc28j60_reset(void);
  367. int yunos_bsp_enc28j60_get_link_status(void);
  368. int yunos_bsp_enc28j60_set_irq_enable(int enable);
  369. int yunos_bsp_enc28j60_get_interrupt_status(void);
  370. int yunos_bsp_enc28j60_set_interrupt_status(int status);
  371. //int yunos_bsp_enc28j60_set_interrupt(gpio_interrupt_t interrupt_cb);
  372. int yunos_bsp_enc28j60_get_pkt_cnt(void);
  373. int yunos_bsp_enc28j60_net_init(void);
  374. int yunos_bsp_enc28j60_set_macaddr(const uint8_t *macaddr);
  375. net_ops_t *yunos_bsp_spi_net_get_ctrl_ops(void);
  376. int yunos_bsp_enc28j60_handle_int_error(int status);
  377. int yunos_bsp_enc28j60_send_start(uint16_t len);
  378. void yunos_bsp_enc28j60_send_data(uint8_t *packet, uint16_t len);
  379. void yunos_bsp_enc28j60_send_end(void);
  380. int yunos_bsp_enc28j60_recv_start(uint16_t maxlen);
  381. int yunos_bsp_enc28j60_recv_data(uint8_t *packet, uint16_t len);
  382. void yunos_bsp_enc28j60_recv_end(void);
  383. void yunos_bsp_enc28j60_hard_reset(void);
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif /* _ETHERNET_ENC28J60_H__ */