c906a48b0ededd4b309f78a0e70d657e561dad0a
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) 
10) /**
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

21) }
22) 
23) /**
Stefan Schuermans implemented ethernet RX bus...

Stefan Schuermans authored 12 years ago

24)  * @brief get buffer position of ethernet receiver
25)  * @return current buffer position of ethernet receiver
26)  *
27)  * eveything before this position can be read
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) static void * eth_rx_get_pos(void)
30) {
31)   return (void *)eth_ptr[0];
32) }
33) 
34) /** initialize receiver */
35) void eth_rx_init(void)
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)   // give buffer 0 to HW
38)   eth_idx_hw = 0;
39)   eth_rx_new_buf(eth_rx_buf[0], sizeof(eth_rx_buf[0]));
40)   // buffer 1 owned by SW is empty
41)   eth_rx_buf[1][0] = 0;
42)   eth_rx_pos = eth_rx_buf[1];
43) }
44) 
45) /**
46)  * @brief get next received packet
47)  * @param[out] *pptr pointer to packet data
48)  * @param[out] *psz size of packet
49)  * @return if a packet was received
50)  */
51) int eth_rx(void **pptr, unsigned int *psz)
52) {
53)   // current SW buffer is empty and HW buffer contains a packet
54)   if (*eth_rx_pos == 0 && eth_rx_get_pos() != eth_rx_buf[eth_idx_hw]) {
55)     // swap buffers
56)     eth_idx_hw = 1 - eth_idx_hw;
57)     // give new HW buffer to HW
58)     eth_rx_new_buf(eth_rx_buf[eth_idx_hw], sizeof(eth_rx_buf[eth_idx_hw]));
59)     // start reading packet data at begin of new SW buffer
60)     eth_rx_pos = eth_rx_buf[1 - eth_idx_hw];
61)   }
62) 
63)   // SW buffer contains a packet
64)   if (*eth_rx_pos > 0) {
65)     // return size and pointer, advance position
66)     *psz = *eth_rx_pos;
67)     eth_rx_pos++;
68)     *pptr = eth_rx_pos;
69)     eth_rx_pos += *psz >> 2;
70)     return 1;
71)   }
72) 
73)   // no packet received
74)   return 0;