55a4ef917532453cc39576073284cfb60d9752af
Stefan Schuermans implementation of ethernet...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

2) #include "eth.h"
3) 
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

4) static volatile unsigned int *const eth_ptr =
5)   (volatile unsigned int *)0x80000400;
6) 
7) static unsigned int eth_idx_hw;
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

8) 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

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

11) static unsigned int eth_rx_cnt;
12) 
13) static unsigned int eth_tx_cnt;
14) 
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

15) /**
16)  * @brief set MAC address
17)  * @param[in] mac MAC address
18)  */
19) static void eth_mac_set(const unsigned char mac[6])
20) {
21)   unsigned int i;
22)   for (i = 0; i < 6; ++i)
23)     ((volatile unsigned char *)(eth_ptr + 12))[i] = mac[i];
24) }
25) 
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

31) static void eth_rx_new_buf(void *ptr, unsigned int sz)
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)   eth_ptr[4] = (unsigned int)ptr; /* new start */
34)   eth_ptr[5] = (unsigned int)ptr + sz; /* new end */
35)   eth_ptr[6] = 1; /* set flag */
36)   while (eth_ptr[6] == 1); /* wait until processed */
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

37) }
38) 
39) /**
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

40)  * @brief get buffer position of ethernet receiver
41)  * @return current buffer position of ethernet receiver
42)  *
43)  * eveything before this position can be read
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

45) static void * eth_rx_get_pos(void)
46) {
47)   return (void *)eth_ptr[0];
48) }
49) 
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

50) /** initialize MAC address */
51) void eth_mac_init(void)
52) {
Stefan Schuermans implementation of ethernet...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

54) }
55) 
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

60)   eth_idx_hw = 0;
61)   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

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

65)   /* no packets received yet */
66)   eth_rx_cnt = 0;
67) }
68) 
69) /** initialize transmitter */
70) void eth_tx_init(void)
71) {
72)   /* no packets transmitted yet */
73)   eth_tx_cnt = 0;
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

74) }
75) 
76) /**
77)  * @brief get next received packet
78)  * @param[out] *pptr pointer to packet data
79)  * @param[out] *psz size of packet
80)  * @return if a packet was received
81)  */
82) int eth_rx(void **pptr, unsigned int *psz)
83) {
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

85)   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

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

89)     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

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

Stefan Schuermans authored 12 years ago

91)     eth_rx_pos = eth_rx_buf[1 - eth_idx_hw];
92)   }
93) 
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

97)     *psz = *eth_rx_pos;
98)     eth_rx_pos++;
99)     *pptr = eth_rx_pos;
100)     eth_rx_pos += *psz >> 2;
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

102)     return 1;
103)   }
104) 
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

107) }
108) 
Stefan Schuermans implemented ethernet TX fir...

Stefan Schuermans authored 12 years ago

109) /**
110)  * @brief transmit packet
Stefan Schuermans added padding of ethernet p...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

119)   while(sz < 60 || sz & 3) /* pad with zeros */
Stefan Schuermans fix ethernet TX firmware: p...

Stefan Schuermans authored 12 years ago

120)     ((unsigned char *)ptr)[sz++] = 0;
Stefan Schuermans implemented ethernet TX fir...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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