cc0f06d3dc633d98f2ffc8014112cf7e8949c84e
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 implementation of ethernet...

Stefan Schuermans authored 12 years ago

8) static unsigned int eth_rx_buf[2][381]; // max frame len: 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 made MAC configurable in et...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

23)  * @brief provide new receive buffer
24)  * @param[in] ptr pointer to new receive buffer
25)  * @param[in] sz size of new receive buffer
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) static void eth_rx_new_buf(void *ptr, unsigned int sz)
Stefan Schuermans begin of ethernet RX implem...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

46) /** initialize MAC address */
47) void eth_mac_init(void)
48) {
Stefan Schuermans implementation of ethernet...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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)