]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/include/rpp/eth.h
Change license to MIT
[pes-rpp/rpp-lib.git] / rpp / include / rpp / eth.h
1 /**
2  * Ethernet Communication RPP API header file.
3  *
4  * @file eth.h
5  *
6  * @copyright Copyright (C) 2013 Czech Technical University in Prague
7  *
8  * @author Carlos Jenkins <carlos@jenkins.co.cr>
9  * @author Rostislav Lisový
10  * @author Jan Doležal <pm.jenik@gmail.com>
11  */
12
13 #ifndef __RPP_ETH_H
14 #define __RPP_ETH_H
15
16 #include <stdio.h>
17
18 #include "os/os.h"
19 #include "lwip/netif.h"
20
21 /* this MAC address is used when user put NULL on the right place when calling postInit function */
22 /**
23  * Default MAC address for interface.
24  */
25 #define RPP_MAC_ADDR             { 0x12 /* Unicast, Locally administered */, 0x34, 0x56, 0x78, 0x9A, 0xBC }
26 #if STATIC_IP_ADDRESS
27 /**
28  * When static IP is configured in lwipopts.h, this IP address is used for interface.
29  * Static IP address is working only at Ciirc. Also mask and GW havo to be changet to work in different network. 
30  */
31 #define RPP_IP_ADDR               0x0A235F19 /* 10.35.95.25   IP4_ADDR(rppip, 10,35,95,25);       */
32 /**
33  * When static IP is configured in lwipopts.h, this NETMASK address is used for interface.
34  */
35 #define RPP_NETMASK               0xFFFFFF00 /* 255.255.255.0   IP4_ADDR(rppnetmask, 255,255,255,0); */
36 /**
37  * When static IP is configured in lwipopts.h, this Gateway address is used for interface.
38  */
39 #define RPP_GW                    0x0A235F01 /* 10.35.95.1      IP4_ADDR(rppgw, 10,35,95,1);      */
40 #endif
41
42 /**
43  * While scanning phy addresses no alive phy was found.
44  * Return value of rpp_eth_hw_init() function.
45  */
46 #define NO_PHY_ALIVE             -1
47 /**
48  * Scanning default phy address, it was found it's not alive.
49  * Return value of rpp_eth_hw_init() function.
50  */
51 #define DFLT_PHY_NOT_ALIVE       -1
52 /**
53  * When setting autonegotiation parameters to EMAC module, there was found impossible mode (usually on timeout of autonegotiation).
54  * Return value of rpp_eth_hw_init_postInit() function.
55  */
56 #define UNKN_DUPLEX_MODE         -2 /* this could mean that autonegotiation was not completed yet */
57 /**
58  * Phy is down error.
59  * Return value of rpp_eth_init_postInit() function.
60  */
61 #define PHY_LINK_DOWN            -3
62
63 /**
64  * LwIP netif couldn't be added, it is likely that there was an error during initialization of the hardware.
65  */
66 #define NETIF_ADD_ERR            -10 /* could be one of previous, except PHY_LINK_DOWN - currently */
67 /**
68  * Memory requirements couldn't be satisfied.
69  */
70 #define DHCP_MEM_ERR             -11
71
72 /**
73  * Configure which function will be used for debugging prints
74  */
75 #ifdef DEBUG
76 #define rpp_debug_printf rpp_sci_printf
77 #else
78 #define rpp_debug_printf(...)
79 #endif
80
81 /**
82  * configures whether rpp_eth_get_macAddrStr() creates string with big or small latin letters
83  */
84 #define MAC_BIG_LETTERS           1
85
86 /**
87  * This function allows application to check whether eth was already initialized
88  * (it's post OS starup part)
89  *
90  * @return TRUE if initialized
91  *         FALSE if not initialized
92  */
93 boolean_t isPostInitialized();
94
95 /**
96  * ETH module system startup initialization.
97  *
98  * Call this method before using this module.
99  * This method starts autonegotiation and doesn't check for end of autoneg.
100  * When eth module is about to be used, you have to run rpp_eth_init_postInit()
101  * first and you should check whether link is up.
102  *
103  * @return SUCCESS if initialization successful.\n
104  *         FAILURE if module already initialized.
105  */
106 int8_t rpp_eth_init();
107
108 /**
109  * ETH module application initialization.
110  *
111  * Call this method before using eth module.
112  * This method waits for a while on autonegotiation till it's complete, if not completed
113  * in predefined time it continues with creating tasks for lwIP (including lwIP init),
114  * for receiving and for handling transmission buffer descriptors. Sets IP address using
115  * predefined static IP or DHCP. Before calling this method you must call rpp_eth_init() first.
116  *
117  * @param instNum number of EMAC instance
118  * @param macArray address assigned to link layer - when NULL RPP_MAC_ADDR is used
119  *
120  * @return SUCCESS if init successful.
121  *         FAILURE if module was already initialized
122  *         NETIF_ADD_ERR if lwip netif was not succesfully initialized or on low hw init error.
123  *         DHCP_MEM_ERR if DHCP unsuccesfull.
124  */
125 int8_t rpp_eth_init_postInit(uint32_t instNum, uint8_t *macArray);
126
127 // Helper routines/functions for command processor and other applications
128
129 /**
130  * Uses MDIO module to read link status of PHY attached to instNum network interface
131  *
132  * @param instNum Number of network interface
133  *
134  * @return TRUE if link is UP
135  *         FALSE if link is DOWN
136  */
137 uint32_t rpp_eth_phylinkstat(uint32_t instNum);
138
139 /**
140  * Fills macStr field with MAC address of given network interface instance as 'string'
141  *
142  * @note 18 bytes will be filled {2 chars per 6 mac bytes + 5 semicolons + 1 null termination}
143  *
144  * @param instNum Number of network interface
145  * @param *macStr field where null terminated string will be inserted
146  */
147 void rpp_eth_get_macAddrStr(uint32_t instNum, uint8_t *macStr);
148
149 /**
150  * Fills ipStr field with IP address in ip_addr_t as 'string';
151  * prepares an IP address to ipStr in form of null terminated string;
152  * each byte of ip is converted to one of 4 field in IPv4 IP address format 0xAABBCCDD -> "aaa.bbb.ccc.ddd"
153  *
154  * @note up to 16 bytes will be filled {1-3 * 4 IP fields + 3 dots + 1 null termination}
155  *
156  *
157  * @param ip ip address to be converted
158  * @param ipStr pointer to string where ip will be filled as text
159  */
160 inline void rpp_eth_getIPDecimalStr(ip_addr_t ip, uint8_t *ipStr)
161 {
162         snprintf((char *)ipStr, 16, "%d.%d.%d.%d",(ip.addr >> 24),((ip.addr >> 16) & 0xff),((ip.addr >> 8) & 0xff),(ip.addr & 0xff));
163 }
164
165 /**
166  * Fills ip.addr with converted ipstr. Checks for correct format of IP string
167  * x.x.x.x where x = [0..9]. When FAILURE is returned struct ip was not modified.
168  *
169  * @param ip struct to be filled
170  * @param ipstr string to be converted
171  *
172  * @return SUCCESS when succesfully converted
173  *         FAILURE when wrong format of IP string
174  */
175 err_t rpp_eth_stringToIP(ip_addr_t *ip, uint8_t *ipstr);
176
177 /**
178  * Converts given null terminated string containing number to number in
179  * range <1-65535>
180  *
181  * @param string to be converted
182  * @return 0 on error
183  *         port number otherwise
184  */
185 uint16_t rpp_eth_portStrToInt(uint8_t *string);
186
187 /**
188  * Returns struct describing network interface
189  *
190  * @param instNum instance number of network interface
191  *
192  * @return pointer to struct netif
193  */
194 struct netif *rpp_eth_get_netif(uint32_t instNum);
195
196 /**
197  * Adjusts packet (pbuf) length and send it within critical section
198  * using rpp_eth_send_raw()
199  *
200  * @param netif lwIP network interface describing struct
201  * @param p buffer for data to be sent
202  */
203 err_t rpp_eth_send(struct netif *netif, struct pbuf *p);
204
205 /**
206  * Handles transmission of data buffer p through EMAC to network.
207  *
208  * @param netif lwIP network interface describing struct
209  * @param p buffer for data to be sent
210  */
211 static err_t rpp_eth_send_raw(struct netif *netif, struct pbuf *p);
212
213 /**
214  * Receives raw data and sends them upward to lwIP stack.
215  * Handles rx emac descriptors.
216  *
217  * @note when using OS tasks (!NO_SYS) this function is used as
218  *       task, otherwise (NO_SYS) this is called from RX ISR.
219  *
220  * @param arg netif
221  */
222 void rpp_eth_recv_raw_thr(void *arg);
223
224 void rpp_eth_send_raw_thr(void *arg);
225
226 /**
227  * Handles freeing of buffer descriptors and other structures in memory
228  * after transmission of bd was completed.
229  *
230  * @param arg hdkif
231  */
232 void rpp_eth_send_bd_handler(void *arg);
233
234 /**
235  * Handles receiving by running rpp_eth_recv_raw_thr()
236  *
237  * @param instNum number of EMAC instance
238  */
239 void RxIntHandler(u32_t instNum);
240
241 /**
242  * Handles tx emac descriptors.
243  *
244  * @param instNum number of EMAC instance
245  */
246 void TxIntHandler(u32_t instNum);
247
248 #endif /* __RPP_ETH_H */