2 * Ethernet Communication RPP API header file.
6 * @copyright Copyright (C) 2013 Czech Technical University in Prague
8 * @author Carlos Jenkins <carlos@jenkins.co.cr>
9 * @author Rostislav Lisový
10 * @author Jan Doležal <pm.jenik@gmail.com>
19 #include "lwip/netif.h"
21 /* this MAC address is used when user put NULL on the right place when calling postInit function */
23 * Default MAC address for interface.
25 #define RPP_MAC_ADDR { 0x12 /* Unicast, Locally administered */, 0x34, 0x56, 0x78, 0x9A, 0xBC }
28 * When static IP is configured in lwipopts.h, this IP address is used for interface.
30 #define RPP_IP_ADDR 0xC0A8F701 /* 192.168.247.1 IP4_ADDR(rppip, 192,168,0,10); */
32 * When static IP is configured in lwipopts.h, this NETMASK address is used for interface.
34 #define RPP_NETMASK 0xFFFFFF00 /* 255.255.255.0 IP4_ADDR(rppnetmask, 255,255,255,0); */
36 * When static IP is configured in lwipopts.h, this Gateway address is used for interface.
38 #define RPP_GW 0xC0A8F7FE /* 192.168.247.254 IP4_ADDR(rppgw, 192,168,0,254); */
42 * While scanning phy addresses no alive phy was found.
43 * Return value of rpp_eth_hw_init() function.
45 #define NO_PHY_ALIVE -1
47 * Scanning default phy address, it was found it's not alive.
48 * Return value of rpp_eth_hw_init() function.
50 #define DFLT_PHY_NOT_ALIVE -1
52 * When setting autonegotiation parameters to EMAC module, there was found impossible mode (usually on timeout of autonegotiation).
53 * Return value of rpp_eth_hw_init_postInit() function.
55 #define UNKN_DUPLEX_MODE -2 /* this could mean that autonegotiation was not completed yet */
58 * Return value of rpp_eth_init_postInit() function.
60 #define PHY_LINK_DOWN -3
63 * LwIP netif couldn't be added, it is likely that there was an error during initialization of the hardware.
65 #define NETIF_ADD_ERR -10 /* could be one of previous, except PHY_LINK_DOWN - currently */
67 * Memory requirements couldn't be satisfied.
69 #define DHCP_MEM_ERR -11
72 * Configure which function will be used for debugging prints
75 #define rpp_debug_printf rpp_sci_printf
77 #define rpp_debug_printf(...)
81 * configures whether rpp_eth_get_macAddrStr() creates string with big or small latin letters
83 #define MAC_BIG_LETTERS 1
86 * This function allows application to check whether eth was already initialized
87 * (it's post OS starup part)
89 * @return TRUE if initialized
90 * FALSE if not initialized
92 boolean_t isPostInitialized();
95 * ETH module system startup initialization.
97 * Call this method before using this module.
98 * This method starts autonegotiation and doesn't check for end of autoneg.
99 * When eth module is about to be used, you have to run rpp_eth_init_postInit()
100 * first and you should check whether link is up.
102 * @return SUCCESS if initialization successful.\n
103 * FAILURE if module already initialized.
105 int8_t rpp_eth_init();
108 * ETH module application initialization.
110 * Call this method before using eth module.
111 * This method waits for a while on autonegotiation till it's complete, if not completed
112 * in predefined time it continues with creating tasks for lwIP (including lwIP init),
113 * for receiving and for handling transmission buffer descriptors. Sets IP address using
114 * predefined static IP or DHCP. Before calling this method you must call rpp_eth_init() first.
116 * @param instNum number of EMAC instance
117 * @param macArray address assigned to link layer - when NULL RPP_MAC_ADDR is used
119 * @return SUCCESS if init successful.
120 * FAILURE if module was already initialized
121 * NETIF_ADD_ERR if lwip netif was not succesfully initialized or on low hw init error.
122 * DHCP_MEM_ERR if DHCP unsuccesfull.
124 int8_t rpp_eth_init_postInit(uint32_t instNum, uint8_t *macArray);
126 // Helper routines/functions for command processor and other applications
129 * Uses MDIO module to read link status of PHY attached to instNum network interface
131 * @param instNum Number of network interface
133 * @return TRUE if link is UP
134 * FALSE if link is DOWN
136 uint32_t rpp_eth_phylinkstat(uint32_t instNum);
139 * Fills macStr field with MAC address of given network interface instance as 'string'
141 * @note 18 bytes will be filled {2 chars per 6 mac bytes + 5 semicolons + 1 null termination}
143 * @param instNum Number of network interface
144 * @param *macStr field where null terminated string will be inserted
146 void rpp_eth_get_macAddrStr(uint32_t instNum, uint8_t *macStr);
149 * Fills ipStr field with IP address in ip_addr_t as 'string';
150 * prepares an IP address to ipStr in form of null terminated string;
151 * each byte of ip is converted to one of 4 field in IPv4 IP address format 0xAABBCCDD -> "aaa.bbb.ccc.ddd"
153 * @note up to 16 bytes will be filled {1-3 * 4 IP fields + 3 dots + 1 null termination}
156 * @param ip ip address to be converted
157 * @param ipStr pointer to string where ip will be filled as text
159 inline void rpp_eth_getIPDecimalStr(ip_addr_t ip, uint8_t *ipStr)
161 snprintf((char *)ipStr, 16, "%d.%d.%d.%d",(ip.addr >> 24),((ip.addr >> 16) & 0xff),((ip.addr >> 8) & 0xff),(ip.addr & 0xff));
165 * Fills ip.addr with converted ipstr. Checks for correct format of IP string
166 * x.x.x.x where x = [0..9]. When FAILURE is returned struct ip was not modified.
168 * @param ip struct to be filled
169 * @param ipstr string to be converted
171 * @return SUCCESS when succesfully converted
172 * FAILURE when wrong format of IP string
174 err_t rpp_eth_stringToIP(ip_addr_t *ip, uint8_t *ipstr);
177 * Converts given null terminated string containing number to number in
180 * @param string to be converted
182 * port number otherwise
184 uint16_t rpp_eth_portStrToInt(uint8_t *string);
187 * Returns struct describing network interface
189 * @param instNum instance number of network interface
191 * @return pointer to struct netif
193 struct netif *rpp_eth_get_netif(uint32_t instNum);
196 * Adjusts packet (pbuf) length and send it within critical section
197 * using rpp_eth_send_raw()
199 * @param netif lwIP network interface describing struct
200 * @param p buffer for data to be sent
202 err_t rpp_eth_send(struct netif *netif, struct pbuf *p);
205 * Handles transmission of data buffer p through EMAC to network.
207 * @param netif lwIP network interface describing struct
208 * @param p buffer for data to be sent
210 static err_t rpp_eth_send_raw(struct netif *netif, struct pbuf *p);
213 * Receives raw data and sends them upward to lwIP stack.
214 * Handles rx emac descriptors.
216 * @note when using OS tasks (!NO_SYS) this function is used as
217 * task, otherwise (NO_SYS) this is called from RX ISR.
221 void rpp_eth_recv_raw_thr(void *arg);
223 void rpp_eth_send_raw_thr(void *arg);
226 * Handles freeing of buffer descriptors and other structures in memory
227 * after transmission of bd was completed.
231 void rpp_eth_send_bd_handler(void *arg);
234 * Handles receiving by running rpp_eth_recv_raw_thr()
236 * @param instNum number of EMAC instance
238 void RxIntHandler(u32_t instNum);
241 * Handles tx emac descriptors.
243 * @param instNum number of EMAC instance
245 void TxIntHandler(u32_t instNum);
247 #endif /* __RPP_ETH_H */