This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:software:homelab:library:module:ethernet [2012/05/16 12:49] – created raivo.sell | en:software:homelab:library:module:ethernet [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Ethernet ====== | ====== Ethernet ====== | ||
| - | <note important> | ||
| - | //Seotud mooduliga: [HW] [[et: | ||
| - | See teegi osa sisaldab Kodulabori etherneti kasutamise funktsioone. | + | //Necessary knowledge: [HW] [[en: |
| - | ===== Konstandid ===== | + | The following part of library contains HomeLab Ethernet functions. |
| - | * **//static uint8_t mymac[6] | + | ===== Constants ===== |
| - | * **//static uint8_t myip[4] | + | |
| - | * **//static uint8_t buf[BUFFER_SIZE+1]// | + | |
| + | * **//static uint8_t mymac[6] = {0x54, | ||
| + | * **//static uint8_t myip[4] = {192, | ||
| + | * **//static uint8_t buf[BUFFER_SIZE+1]// | ||
| - | ===== Funktsioonid | + | |
| + | ===== Functions | ||
| * **//void ethernet_standby(void)// | * **//void ethernet_standby(void)// | ||
| - | | + | |
| * **//void ethernet_init(uint8_t *mac, uint8_t *ip)//** \\ | * **//void ethernet_init(uint8_t *mac, uint8_t *ip)//** \\ | ||
| - | | + | |
| - | * //*mac// - MAC aadressi massiiv. | + | Parameters: |
| - | * //*ip// - IP aadressi massiiv. | + | * //*mac// - MAC address array. |
| + | * //*ip// - IP address array. | ||
| * **// | * **// | ||
| - | | + | |
| - | * //maxlen// - maksimaalne lubatud andmepuhvri suurus. | + | Parameters: |
| - | * //*buffer// - massiiv, kuhu salvestab vastuvõetud andmed edasiseks töötluseks. | + | * //maxlen// - maximum allowed data buffer size. |
| - | * Kui pakett on saabunud, tagastab saabunud paketi pikkuse baitides, vastasel juhul tagastab | + | * //*buffer// - array where to save received data for further operations. |
| + | * If the packet has arrived, returns the length of the received packet in bytes and in opposite case returns | ||
| * **// | * **// | ||
| - | | + | |
| - | * //*buf// - andmete massiiv, mida analüüsida. | + | Parameters: |
| - | * //plen// - saabunud paketi pikkus baitides. | + | * //*buf// - data array to analyze. |
| + | * //plen// - length of received packet in bytes. | ||
| * Tagastab URL-i esimese tähemärgi aadressi. Kui URL on tühi, siis tagastab 0. | * Tagastab URL-i esimese tähemärgi aadressi. Kui URL on tühi, siis tagastab 0. | ||
| + | * Returns the address of the first char in URL. If URL is empty, it will return 0. | ||
| * **// | * **// | ||
| //** \\ | //** \\ | ||
| - | | + | |
| - | * //*buf// - andmemassiiv, | + | Parameters: |
| - | * //pos// - andmete lõpu aadress puhvris, mida tuleb jälgida, kui andmeid üksteise järele laadida sinna. Esimene aadress on 0. | + | * //*buf// - data arrya to send using TCP. |
| - | * // | + | * //pos// - data end address in buffer which must be maintained if data is loaded in queue. The first address is 0. |
| - | * Tagastab andmejada lõpu aadressi, mida saab sisestada parameetrina järgmise html-koodi laadimiseks. | + | * // |
| + | * Retruns end address of data array which can be input as a parameter for loading next HTML-code. | ||
| * **//void ethernet_print_webpage (uint8_t *buf, | * **//void ethernet_print_webpage (uint8_t *buf, | ||
| - | | + | |
| - | * //*buf// - andmemassiiv, | + | Parameters: |
| - | * //dplen// - andmete lõpu aadress puhvris, milleni kuvab koodi. | + | * //*buf// - Data array where HTLM is preloaded. |
| + | * //dplen// - End address of data in buffer to be displayed as HTML-code. | ||
| - | ~~PB~~ | + | < |
| - | ===== Näide | + | ===== Example |
| <code c> | <code c> | ||
| Line 57: | Line 63: | ||
| #include < | #include < | ||
| - | // Veebilehe koostamiseks eraldi funktsioon | + | // Function for compiling webpage |
| uint16_t ethernet_load_webpage(uint8_t on_off); | uint16_t ethernet_load_webpage(uint8_t on_off); | ||
| - | // MAC ja IP peavad olema unikaalsed oma kohtvõrgus, | + | // MAC and IP must be unique in network, |
| - | // kahte ühesugust aadressi ei saa olla seadmetel. | + | // same addresses in two devices are not allowed. |
| - | // See MAC on suvaliselt võetud | + | // Random |
| static uint8_t mymac[6] = {0x54, | static uint8_t mymac[6] = {0x54, | ||
| - | // IP vastavalt kohtvõrgule | + | // IP according to LAN (you need to choose the last number) |
| static uint8_t myip[4] = {192, | static uint8_t myip[4] = {192, | ||
| - | //andmejada pikkus | + | //length of data array |
| static uint16_t gPlen; | static uint16_t gPlen; | ||
| - | //Andmete puhvri suurus | + | //Data buffer size |
| #define BUFFER_SIZE 600 | #define BUFFER_SIZE 600 | ||
| static uint8_t buf[BUFFER_SIZE+1]; | static uint8_t buf[BUFFER_SIZE+1]; | ||
| - | //LED PB7 muutuja määramine | + | //LED PB7 variable |
| pin debug_led = PIN(B, 7); | pin debug_led = PIN(B, 7); | ||
| Line 85: | Line 91: | ||
| char *str; | char *str; | ||
| - | // LED PB7 on väljund | + | // LED PB7 is output |
| pin_setup_output(debug_led); | pin_setup_output(debug_led); | ||
| - | // Etherneti | + | // Etherneti |
| ethernet_init(mymac, | ethernet_init(mymac, | ||
| while(1) | while(1) | ||
| { | { | ||
| - | // Võtab vastu pakette, kuni saab endale määratud andmetega paketi | + | // Receive packets until it receives addressed to him with data |
| pktlen=ethernet_get_packet(BUFFER_SIZE, | pktlen=ethernet_get_packet(BUFFER_SIZE, | ||
| - | // Teeb paketi eeltöötluse ja vastab | + | // Performs packet preconditioning and answer to " |
| + | // Returns packet | ||
| gPlen=ethernet_analyse_packet(buf, | gPlen=ethernet_analyse_packet(buf, | ||
| - | // Kui on URL-is on infot, siis hakkab seda analüüsima | + | // If URL contains info, start analyzing it |
| if (gPlen!=0) | if (gPlen!=0) | ||
| { | { | ||
| - | // Laeb URL-i aadressiosa viita " | + | // Load URL address part into " |
| - | | + | // The first 4 digits are IP address numbers. |
| str = (char *)& | str = (char *)& | ||
| - | // Otsib URL-ist stringi | + | // Find string |
| if (strncmp("/ | if (strncmp("/ | ||
| { | { | ||
| - | // Laeb veebilehe | + | // Load webpage |
| gPlen = ethernet_load_webpage(0); | gPlen = ethernet_load_webpage(0); | ||
| - | // LED põlema | + | // LED on |
| pin_clear(debug_led); | pin_clear(debug_led); | ||
| | | ||
| } | } | ||
| - | // Järgmisena otsib URL-ist stringi | + | // At next, find string |
| else if (strncmp("/ | else if (strncmp("/ | ||
| { | { | ||
| - | // Laeb veebilehe | + | // Load webpage |
| gPlen = ethernet_load_webpage(1); | gPlen = ethernet_load_webpage(1); | ||
| - | // LED kustutada | + | // LED on |
| pin_set(debug_led); | pin_set(debug_led); | ||
| } | } | ||
| - | // Muul juhul lae veebileht vastavalt | + | // In other cases load webpage according to LED condition |
| else | else | ||
| { | { | ||
| Line 132: | Line 139: | ||
| } | } | ||
| - | // Kuva varem valmis laetud veebileht | + | // Display preloaded webpage |
| - | ethernet_print_webpage (buf, | + | ethernet_print_webpage (buf, |
| - | | + | |
| } | } | ||
| } | } | ||
| Line 141: | Line 147: | ||
| - | // Veebileht laetakse kuvamiseks kirjutades andmed | + | // Webpage will be loaded by writing data into TCP send buffer |
| uint16_t ethernet_load_webpage(uint8_t on_off) | uint16_t ethernet_load_webpage(uint8_t on_off) | ||
| { | { | ||
| uint16_t plen=0; | uint16_t plen=0; | ||
| - | // Laeb html koodi saatmiseks puhvrisse | + | // Load HTML-code into buffer to send it |
| - | // Need suured stringid on salvestatud | + | // Those large strings are saved into program memory using PSTR macro, |
| - | // et mitte raisata liigselt töömälu. | + | // to save SRAM |
| - | // Laeb veebilehe päise | + | // Load webpage header |
| - | | + | |
| + | plen=ethernet_load_data(buf, | ||
| + | (" | ||
| | | ||
| - | | + | plen=ethernet_load_data(buf, |
| - | // Kui LED ei põle, siis kuva nii. | + | // if LED is off, display it |
| if (on_off) | if (on_off) | ||
| { | { | ||
| - | plen=ethernet_load_data(buf, | + | |
| } | } | ||
| - | // Kui LED põleb. | + | // if LED is on |
| else | else | ||
| { | { | ||
| - | plen=ethernet_load_data(buf, | + | |
| } | } | ||
| // Lae " | // Lae " | ||
| - | plen=ethernet_load_data(buf, | + | plen=ethernet_load_data(buf, |
| + | | ||
| | | ||
| - | // Lae LED oleku muutmise nupp vastavalt selle olekule. | + | // Load the button for LED condition change. |
| if (on_off) | if (on_off) | ||
| { | { | ||
| Line 180: | Line 189: | ||
| } | } | ||
| - | // Väljasta andmete lõpu aadress puhvris | + | // Return the end address of data in buffer |
| return(plen); | return(plen); | ||
| } | } | ||
| </ | </ | ||
| - | |||