2ef1d95cf5b52936b651e95a370f0485bb60d998
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

3) static volatile unsigned int *const eth_ptr =
4)   (volatile unsigned int *)0x80000400;
5) 
6) static unsigned int eth_idx_hw;
7) static unsigned int eth_rx_buf[2][256];
8) static unsigned int *eth_rx_pos;
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

9) 
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

32) }
33) 
34) /**
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

35)  * @brief get buffer position of ethernet receiver
36)  * @return current buffer position of ethernet receiver
37)  *
38)  * eveything before this position can be read
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

40) static void * eth_rx_get_pos(void)
41) {
42)   return (void *)eth_ptr[0];
43) }
44) 
Stefan Schuermans made MAC configurable in et...

Stefan Schuermans authored 12 years ago

45) /** initialize MAC address */
46) void eth_mac_init(void)
47) {
48)   static const unsigned char mac[6] = {0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
49)   eth_mac_set(mac);
50) }
51) 
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

55)   // give buffer 0 to HW
56)   eth_idx_hw = 0;
57)   eth_rx_new_buf(eth_rx_buf[0], sizeof(eth_rx_buf[0]));
58)   // buffer 1 owned by SW is empty
59)   eth_rx_buf[1][0] = 0;
60)   eth_rx_pos = eth_rx_buf[1];
61) }
62) 
63) /**
64)  * @brief get next received packet
65)  * @param[out] *pptr pointer to packet data
66)  * @param[out] *psz size of packet
67)  * @return if a packet was received
68)  */
69) int eth_rx(void **pptr, unsigned int *psz)
70) {
71)   // current SW buffer is empty and HW buffer contains a packet
72)   if (*eth_rx_pos == 0 && eth_rx_get_pos() != eth_rx_buf[eth_idx_hw]) {
73)     // swap buffers
74)     eth_idx_hw = 1 - eth_idx_hw;
75)     // give new HW buffer to HW
76)     eth_rx_new_buf(eth_rx_buf[eth_idx_hw], sizeof(eth_rx_buf[eth_idx_hw]));
77)     // start reading packet data at begin of new SW buffer
78)     eth_rx_pos = eth_rx_buf[1 - eth_idx_hw];
79)   }
80) 
81)   // SW buffer contains a packet
82)   if (*eth_rx_pos > 0) {
83)     // return size and pointer, advance position
84)     *psz = *eth_rx_pos;
85)     eth_rx_pos++;
86)     *pptr = eth_rx_pos;
87)     eth_rx_pos += *psz >> 2;
88)     return 1;
89)   }
90) 
91)   // no packet received
92)   return 0;
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

93) }
94) 
Stefan Schuermans implemented ethernet TX fir...

Stefan Schuermans authored 12 years ago

95) /**
96)  * @brief transmit packet
97)  * @param[out] ptr pointer to packet data
98)  * @param[out] sz size of packet
99)  */
Stefan Schuermans ethernet TX function takes...

Stefan Schuermans authored 12 years ago

100) void eth_tx(const void *ptr, unsigned int sz)