c2b040193a777c09bdc595c95492b57a2521db87
Stefan Schuermans added file headers

Stefan Schuermans authored 12 years ago

1) /* MIPS I system
2)  * Copyright 2011-2012 Stefan Schuermans <stefan@schuermans.info>
3)  * Copyleft GNU public license V2 or later
4)  *          http://www.gnu.org/copyleft/gpl.html
5)  */
6) 
Stefan Schuermans implementation of ethernet...

Stefan Schuermans authored 12 years ago

7) #include "config.h"
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

8) #include "eth.h"
9) 
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

10) static volatile unsigned int *const eth_ptr =
11)   (volatile unsigned int *)0x80000400;
12) 
13) static unsigned int eth_idx_hw;
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

14) static unsigned int eth_rx_buf[2][381]; /* max frame: 1522 byte = 380.5 int */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

15) static unsigned int *eth_rx_pos;
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

16) 
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

17) static unsigned int eth_rx_cnt;
18) 
19) static unsigned int eth_tx_cnt;
20) 
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

21) /**
22)  * @brief set MAC address
23)  * @param[in] mac MAC address
24)  */
25) static void eth_mac_set(const unsigned char mac[6])
26) {
27)   unsigned int i;
28)   for (i = 0; i < 6; ++i)
29)     ((volatile unsigned char *)(eth_ptr + 12))[i] = mac[i];
30) }
31) 
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

32) /**
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

33)  * @brief provide new receive buffer
34)  * @param[in] ptr pointer to new receive buffer
35)  * @param[in] sz size of new receive buffer
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

36)  */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

37) static void eth_rx_new_buf(void *ptr, unsigned int sz)
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

38) {
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

39)   eth_ptr[4] = (unsigned int)ptr; /* new start */
40)   eth_ptr[5] = (unsigned int)ptr + sz; /* new end */
41)   eth_ptr[6] = 1; /* set flag */
42)   while (eth_ptr[6] == 1); /* wait until processed */
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

43) }
44) 
45) /**
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

46)  * @brief get buffer position of ethernet receiver
47)  * @return current buffer position of ethernet receiver
48)  *
49)  * eveything before this position can be read
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

50)  */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

51) static void * eth_rx_get_pos(void)
52) {
53)   return (void *)eth_ptr[0];
54) }
55) 
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

56) /** initialize MAC address */
57) void eth_mac_init(void)
58) {
Stefan Schuermans implementation of ethernet...

Stefan Schuermans authored 12 years ago

59)   eth_mac_set(config_mac.mac);
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

60) }
61) 
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

62) /** initialize receiver */
63) void eth_rx_init(void)
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

64) {
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

65)   /* give buffer 0 to HW */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

66)   eth_idx_hw = 0;
67)   eth_rx_new_buf(eth_rx_buf[0], sizeof(eth_rx_buf[0]));
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

68)   /* buffer 1 owned by SW is empty */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

69)   eth_rx_buf[1][0] = 0;
70)   eth_rx_pos = eth_rx_buf[1];
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

71)   /* no packets received yet */
72)   eth_rx_cnt = 0;
73) }
74) 
75) /** initialize transmitter */
76) void eth_tx_init(void)
77) {
78)   /* no packets transmitted yet */
79)   eth_tx_cnt = 0;
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

80) }
81) 
82) /**
83)  * @brief get next received packet
84)  * @param[out] *pptr pointer to packet data
85)  * @param[out] *psz size of packet
86)  * @return if a packet was received
87)  */
88) int eth_rx(void **pptr, unsigned int *psz)
89) {
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

90)   /* current SW buffer is empty and HW buffer contains a packet */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

91)   if (*eth_rx_pos == 0 && eth_rx_get_pos() != eth_rx_buf[eth_idx_hw]) {
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

92)     /* swap buffers */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

93)     eth_idx_hw = 1 - eth_idx_hw;
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

94)     /* give new HW buffer to HW */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

95)     eth_rx_new_buf(eth_rx_buf[eth_idx_hw], sizeof(eth_rx_buf[eth_idx_hw]));
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

96)     /* start reading packet data at begin of new SW buffer */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

97)     eth_rx_pos = eth_rx_buf[1 - eth_idx_hw];
98)   }
99) 
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

100)   /* SW buffer contains a packet */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

101)   if (*eth_rx_pos > 0) {
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

102)     /* return size and pointer, advance position */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

103)     *psz = *eth_rx_pos;
104)     eth_rx_pos++;
105)     *pptr = eth_rx_pos;
106)     eth_rx_pos += *psz >> 2;
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

107)     ++eth_rx_cnt; /* count received packets */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

108)     return 1;
109)   }
110) 
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

111)   /* no packet received */
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

112)   return 0;
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

113) }
114) 
Stefan Schuermans implemented ethernet TX fir...

Stefan Schuermans authored 12 years ago

115) /**
116)  * @brief transmit packet
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

117)  * @param[in] ptr pointer to packet data
118)  * @param[in] sz size of packet
119)  *
120)  * Padding will be appended if sz is not a multiple of 4 or smaller 60 bytes.
121)  * The buffer pointed to by ptr must be sufficiently large.
Stefan Schuermans implemented ethernet TX fir...

Stefan Schuermans authored 12 years ago

122)  */
Stefan Schuermans ethernet TX function takes...

Stefan Schuermans authored 12 years ago

123) void eth_tx(const void *ptr, unsigned int sz)
Stefan Schuermans implemented ethernet TX fir...

Stefan Schuermans authored 12 years ago

124) {
125)   eth_ptr[8] = (unsigned int)ptr; /* start */
126)   eth_ptr[9] = (unsigned int)ptr + sz; /* end */
127)   eth_ptr[10] = 1; /* set flag */
128)   while (eth_ptr[10] == 1); /* wait until processed */
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

129)   ++eth_tx_cnt; /* count transmitted packets */
130) }
131) 
132) /** get number of received packets */
133) unsigned int eth_rx_get_cnt(void)
134) {
135)   return eth_rx_cnt;
136) }
137) 
138) /** get number of transmitted packets */
139) unsigned int eth_tx_get_cnt(void)
140) {
141)   return eth_tx_cnt;