These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
EMAC/Easy_Web/easyweb.c@0:bf7b9fba3924, 2011-03-20 (annotated)
- Committer:
- frank26080115
- Date:
- Sun Mar 20 05:38:56 2011 +0000
- Revision:
- 0:bf7b9fba3924
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frank26080115 | 0:bf7b9fba3924 | 1 | /****************************************************************** |
frank26080115 | 0:bf7b9fba3924 | 2 | ***** ***** |
frank26080115 | 0:bf7b9fba3924 | 3 | ***** Name: easyweb.c ***** |
frank26080115 | 0:bf7b9fba3924 | 4 | ***** Ver.: 1.0 ***** |
frank26080115 | 0:bf7b9fba3924 | 5 | ***** Date: 07/05/2001 ***** |
frank26080115 | 0:bf7b9fba3924 | 6 | ***** Auth: Andreas Dannenberg ***** |
frank26080115 | 0:bf7b9fba3924 | 7 | ***** HTWK Leipzig ***** |
frank26080115 | 0:bf7b9fba3924 | 8 | ***** university of applied sciences ***** |
frank26080115 | 0:bf7b9fba3924 | 9 | ***** Germany ***** |
frank26080115 | 0:bf7b9fba3924 | 10 | ***** Func: implements a dynamic HTTP-server by using ***** |
frank26080115 | 0:bf7b9fba3924 | 11 | ***** the easyWEB-API ***** |
frank26080115 | 0:bf7b9fba3924 | 12 | ***** ***** |
frank26080115 | 0:bf7b9fba3924 | 13 | ******************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 14 | |
frank26080115 | 0:bf7b9fba3924 | 15 | #include <stdlib.h> |
frank26080115 | 0:bf7b9fba3924 | 16 | #include <stdio.h> |
frank26080115 | 0:bf7b9fba3924 | 17 | #include <string.h> |
frank26080115 | 0:bf7b9fba3924 | 18 | #include "adc.h" |
frank26080115 | 0:bf7b9fba3924 | 19 | #include "lpc17xx_libcfg.h" |
frank26080115 | 0:bf7b9fba3924 | 20 | #include "EMAC.h" // Keil: *.c -> *.h // ethernet packet driver |
frank26080115 | 0:bf7b9fba3924 | 21 | #define extern // Keil: Line added for modular project management |
frank26080115 | 0:bf7b9fba3924 | 22 | |
frank26080115 | 0:bf7b9fba3924 | 23 | #include "easyweb.h" |
frank26080115 | 0:bf7b9fba3924 | 24 | #include "tcpip.h" // Keil: *.c -> *.h // easyWEB TCP/IP stack |
frank26080115 | 0:bf7b9fba3924 | 25 | #include "webpage.h" // webside for our HTTP server (HTML) |
frank26080115 | 0:bf7b9fba3924 | 26 | |
frank26080115 | 0:bf7b9fba3924 | 27 | /* Example group ----------------------------------------------------------- */ |
frank26080115 | 0:bf7b9fba3924 | 28 | /** @defgroup EMAC_Easy_Web Easy_Web |
frank26080115 | 0:bf7b9fba3924 | 29 | * @ingroup EMAC_Examples |
frank26080115 | 0:bf7b9fba3924 | 30 | * @{ |
frank26080115 | 0:bf7b9fba3924 | 31 | */ |
frank26080115 | 0:bf7b9fba3924 | 32 | |
frank26080115 | 0:bf7b9fba3924 | 33 | // NXP: Include some header files that differs from the origin |
frank26080115 | 0:bf7b9fba3924 | 34 | int main(void) |
frank26080115 | 0:bf7b9fba3924 | 35 | { |
frank26080115 | 0:bf7b9fba3924 | 36 | TCPLowLevelInit(); |
frank26080115 | 0:bf7b9fba3924 | 37 | HTTPStatus = 0; // clear HTTP-server's flag register |
frank26080115 | 0:bf7b9fba3924 | 38 | |
frank26080115 | 0:bf7b9fba3924 | 39 | TCPLocalPort = TCP_PORT_HTTP; // set port we want to listen to |
frank26080115 | 0:bf7b9fba3924 | 40 | |
frank26080115 | 0:bf7b9fba3924 | 41 | while (1) // repeat forever |
frank26080115 | 0:bf7b9fba3924 | 42 | { |
frank26080115 | 0:bf7b9fba3924 | 43 | if (!(SocketStatus & SOCK_ACTIVE)) TCPPassiveOpen(); // listen for incoming TCP-connection |
frank26080115 | 0:bf7b9fba3924 | 44 | DoNetworkStuff(); // handle network and easyWEB-stack |
frank26080115 | 0:bf7b9fba3924 | 45 | // events |
frank26080115 | 0:bf7b9fba3924 | 46 | HTTPServer(); |
frank26080115 | 0:bf7b9fba3924 | 47 | } |
frank26080115 | 0:bf7b9fba3924 | 48 | } |
frank26080115 | 0:bf7b9fba3924 | 49 | |
frank26080115 | 0:bf7b9fba3924 | 50 | // This function implements a very simple dynamic HTTP-server. |
frank26080115 | 0:bf7b9fba3924 | 51 | // It waits until connected, then sends a HTTP-header and the |
frank26080115 | 0:bf7b9fba3924 | 52 | // HTML-code stored in memory. Before sending, it replaces |
frank26080115 | 0:bf7b9fba3924 | 53 | // some special strings with dynamic values. |
frank26080115 | 0:bf7b9fba3924 | 54 | // NOTE: For strings crossing page boundaries, replacing will |
frank26080115 | 0:bf7b9fba3924 | 55 | // not work. In this case, simply add some extra lines |
frank26080115 | 0:bf7b9fba3924 | 56 | // (e.g. CR and LFs) to the HTML-code. |
frank26080115 | 0:bf7b9fba3924 | 57 | |
frank26080115 | 0:bf7b9fba3924 | 58 | void HTTPServer(void) |
frank26080115 | 0:bf7b9fba3924 | 59 | { |
frank26080115 | 0:bf7b9fba3924 | 60 | if (SocketStatus & SOCK_CONNECTED) // check if somebody has connected to our TCP |
frank26080115 | 0:bf7b9fba3924 | 61 | { |
frank26080115 | 0:bf7b9fba3924 | 62 | if (SocketStatus & SOCK_DATA_AVAILABLE) // check if remote TCP sent data |
frank26080115 | 0:bf7b9fba3924 | 63 | TCPReleaseRxBuffer(); // and throw it away |
frank26080115 | 0:bf7b9fba3924 | 64 | |
frank26080115 | 0:bf7b9fba3924 | 65 | if (SocketStatus & SOCK_TX_BUF_RELEASED) // check if buffer is free for TX |
frank26080115 | 0:bf7b9fba3924 | 66 | { |
frank26080115 | 0:bf7b9fba3924 | 67 | if (!(HTTPStatus & HTTP_SEND_PAGE)) // init byte-counter and pointer to webside |
frank26080115 | 0:bf7b9fba3924 | 68 | { // if called the 1st time |
frank26080115 | 0:bf7b9fba3924 | 69 | HTTPBytesToSend = sizeof(WebSide) - 1; // get HTML length, ignore trailing zero |
frank26080115 | 0:bf7b9fba3924 | 70 | PWebSide = (unsigned char *)WebSide; // pointer to HTML-code |
frank26080115 | 0:bf7b9fba3924 | 71 | } |
frank26080115 | 0:bf7b9fba3924 | 72 | |
frank26080115 | 0:bf7b9fba3924 | 73 | if (HTTPBytesToSend > MAX_TCP_TX_DATA_SIZE) // transmit a segment of MAX_SIZE |
frank26080115 | 0:bf7b9fba3924 | 74 | { |
frank26080115 | 0:bf7b9fba3924 | 75 | if (!(HTTPStatus & HTTP_SEND_PAGE)) // 1st time, include HTTP-header |
frank26080115 | 0:bf7b9fba3924 | 76 | { |
frank26080115 | 0:bf7b9fba3924 | 77 | memcpy(TCP_TX_BUF, GetResponse, sizeof(GetResponse) - 1); |
frank26080115 | 0:bf7b9fba3924 | 78 | memcpy(TCP_TX_BUF + sizeof(GetResponse) - 1, PWebSide, MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1); |
frank26080115 | 0:bf7b9fba3924 | 79 | HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1; |
frank26080115 | 0:bf7b9fba3924 | 80 | PWebSide += MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1; |
frank26080115 | 0:bf7b9fba3924 | 81 | } |
frank26080115 | 0:bf7b9fba3924 | 82 | else |
frank26080115 | 0:bf7b9fba3924 | 83 | { |
frank26080115 | 0:bf7b9fba3924 | 84 | memcpy(TCP_TX_BUF, PWebSide, MAX_TCP_TX_DATA_SIZE); |
frank26080115 | 0:bf7b9fba3924 | 85 | HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE; |
frank26080115 | 0:bf7b9fba3924 | 86 | PWebSide += MAX_TCP_TX_DATA_SIZE; |
frank26080115 | 0:bf7b9fba3924 | 87 | } |
frank26080115 | 0:bf7b9fba3924 | 88 | |
frank26080115 | 0:bf7b9fba3924 | 89 | TCPTxDataCount = MAX_TCP_TX_DATA_SIZE; // bytes to xfer |
frank26080115 | 0:bf7b9fba3924 | 90 | InsertDynamicValues(); // exchange some strings... |
frank26080115 | 0:bf7b9fba3924 | 91 | TCPTransmitTxBuffer(); // xfer buffer |
frank26080115 | 0:bf7b9fba3924 | 92 | } |
frank26080115 | 0:bf7b9fba3924 | 93 | else if (HTTPBytesToSend) // transmit leftover bytes |
frank26080115 | 0:bf7b9fba3924 | 94 | { |
frank26080115 | 0:bf7b9fba3924 | 95 | memcpy(TCP_TX_BUF, PWebSide, HTTPBytesToSend); |
frank26080115 | 0:bf7b9fba3924 | 96 | TCPTxDataCount = HTTPBytesToSend; // bytes to xfer |
frank26080115 | 0:bf7b9fba3924 | 97 | InsertDynamicValues(); // exchange some strings... |
frank26080115 | 0:bf7b9fba3924 | 98 | TCPTransmitTxBuffer(); // send last segment |
frank26080115 | 0:bf7b9fba3924 | 99 | TCPClose(); // and close connection |
frank26080115 | 0:bf7b9fba3924 | 100 | HTTPBytesToSend = 0; // all data sent |
frank26080115 | 0:bf7b9fba3924 | 101 | } |
frank26080115 | 0:bf7b9fba3924 | 102 | |
frank26080115 | 0:bf7b9fba3924 | 103 | HTTPStatus |= HTTP_SEND_PAGE; // ok, 1st loop executed |
frank26080115 | 0:bf7b9fba3924 | 104 | } |
frank26080115 | 0:bf7b9fba3924 | 105 | } |
frank26080115 | 0:bf7b9fba3924 | 106 | else |
frank26080115 | 0:bf7b9fba3924 | 107 | HTTPStatus &= ~HTTP_SEND_PAGE; // reset help-flag if not connected |
frank26080115 | 0:bf7b9fba3924 | 108 | } |
frank26080115 | 0:bf7b9fba3924 | 109 | |
frank26080115 | 0:bf7b9fba3924 | 110 | // samples and returns the AD-converter value of channel 2 (MCB1700 board) or channel 5 (IAR board) |
frank26080115 | 0:bf7b9fba3924 | 111 | |
frank26080115 | 0:bf7b9fba3924 | 112 | unsigned int GetAD7Val(void) |
frank26080115 | 0:bf7b9fba3924 | 113 | { |
frank26080115 | 0:bf7b9fba3924 | 114 | unsigned int val; |
frank26080115 | 0:bf7b9fba3924 | 115 | ADC_startCnv(); |
frank26080115 | 0:bf7b9fba3924 | 116 | val = ADC_getCnv(); |
frank26080115 | 0:bf7b9fba3924 | 117 | ADC_stopCnv(); |
frank26080115 | 0:bf7b9fba3924 | 118 | return (val/40); |
frank26080115 | 0:bf7b9fba3924 | 119 | } |
frank26080115 | 0:bf7b9fba3924 | 120 | |
frank26080115 | 0:bf7b9fba3924 | 121 | // samples and returns AD-converter value of channel 1 |
frank26080115 | 0:bf7b9fba3924 | 122 | |
frank26080115 | 0:bf7b9fba3924 | 123 | unsigned int GetTempVal(void) |
frank26080115 | 0:bf7b9fba3924 | 124 | { |
frank26080115 | 0:bf7b9fba3924 | 125 | // Always return (0) |
frank26080115 | 0:bf7b9fba3924 | 126 | return (0); |
frank26080115 | 0:bf7b9fba3924 | 127 | } |
frank26080115 | 0:bf7b9fba3924 | 128 | |
frank26080115 | 0:bf7b9fba3924 | 129 | |
frank26080115 | 0:bf7b9fba3924 | 130 | // searches the TX-buffer for special strings and replaces them |
frank26080115 | 0:bf7b9fba3924 | 131 | // with dynamic values (AD-converter results) |
frank26080115 | 0:bf7b9fba3924 | 132 | |
frank26080115 | 0:bf7b9fba3924 | 133 | void InsertDynamicValues(void) |
frank26080115 | 0:bf7b9fba3924 | 134 | { |
frank26080115 | 0:bf7b9fba3924 | 135 | unsigned char *Key; |
frank26080115 | 0:bf7b9fba3924 | 136 | char NewKey[5]; |
frank26080115 | 0:bf7b9fba3924 | 137 | unsigned int i; |
frank26080115 | 0:bf7b9fba3924 | 138 | |
frank26080115 | 0:bf7b9fba3924 | 139 | if (TCPTxDataCount < 4) return; // there can't be any special string |
frank26080115 | 0:bf7b9fba3924 | 140 | |
frank26080115 | 0:bf7b9fba3924 | 141 | Key = TCP_TX_BUF; |
frank26080115 | 0:bf7b9fba3924 | 142 | |
frank26080115 | 0:bf7b9fba3924 | 143 | for (i = 0; i < (TCPTxDataCount - 3); i++) |
frank26080115 | 0:bf7b9fba3924 | 144 | { |
frank26080115 | 0:bf7b9fba3924 | 145 | if (*Key == 'A') |
frank26080115 | 0:bf7b9fba3924 | 146 | if (*(Key + 1) == 'D') |
frank26080115 | 0:bf7b9fba3924 | 147 | if (*(Key + 3) == '%') |
frank26080115 | 0:bf7b9fba3924 | 148 | switch (*(Key + 2)) |
frank26080115 | 0:bf7b9fba3924 | 149 | { |
frank26080115 | 0:bf7b9fba3924 | 150 | case '7' : // "AD7%"? |
frank26080115 | 0:bf7b9fba3924 | 151 | { |
frank26080115 | 0:bf7b9fba3924 | 152 | sprintf(NewKey, "%3u", GetAD7Val()); // insert AD converter value |
frank26080115 | 0:bf7b9fba3924 | 153 | memcpy(Key, NewKey, 3); // channel 7 (P6.7) |
frank26080115 | 0:bf7b9fba3924 | 154 | break; |
frank26080115 | 0:bf7b9fba3924 | 155 | } |
frank26080115 | 0:bf7b9fba3924 | 156 | case 'A' : // "ADA%"? |
frank26080115 | 0:bf7b9fba3924 | 157 | { |
frank26080115 | 0:bf7b9fba3924 | 158 | sprintf(NewKey, "%3u", GetTempVal()); // insert AD converter value |
frank26080115 | 0:bf7b9fba3924 | 159 | memcpy(Key, NewKey, 3); // channel 10 (temp.-diode) |
frank26080115 | 0:bf7b9fba3924 | 160 | break; |
frank26080115 | 0:bf7b9fba3924 | 161 | } |
frank26080115 | 0:bf7b9fba3924 | 162 | } |
frank26080115 | 0:bf7b9fba3924 | 163 | Key++; |
frank26080115 | 0:bf7b9fba3924 | 164 | } |
frank26080115 | 0:bf7b9fba3924 | 165 | } |
frank26080115 | 0:bf7b9fba3924 | 166 | |
frank26080115 | 0:bf7b9fba3924 | 167 | #ifdef DEBUG |
frank26080115 | 0:bf7b9fba3924 | 168 | /******************************************************************************* |
frank26080115 | 0:bf7b9fba3924 | 169 | * @brief Reports the name of the source file and the source line number |
frank26080115 | 0:bf7b9fba3924 | 170 | * where the CHECK_PARAM error has occurred. |
frank26080115 | 0:bf7b9fba3924 | 171 | * @param[in] file Pointer to the source file name |
frank26080115 | 0:bf7b9fba3924 | 172 | * @param[in] line assert_param error line source number |
frank26080115 | 0:bf7b9fba3924 | 173 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 174 | *******************************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 175 | void check_failed(uint8_t *file, uint32_t line) |
frank26080115 | 0:bf7b9fba3924 | 176 | { |
frank26080115 | 0:bf7b9fba3924 | 177 | /* User can add his own implementation to report the file name and line number, |
frank26080115 | 0:bf7b9fba3924 | 178 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
frank26080115 | 0:bf7b9fba3924 | 179 | |
frank26080115 | 0:bf7b9fba3924 | 180 | /* Infinite loop */ |
frank26080115 | 0:bf7b9fba3924 | 181 | while(1); |
frank26080115 | 0:bf7b9fba3924 | 182 | } |
frank26080115 | 0:bf7b9fba3924 | 183 | #endif |
frank26080115 | 0:bf7b9fba3924 | 184 | |
frank26080115 | 0:bf7b9fba3924 | 185 | /* |
frank26080115 | 0:bf7b9fba3924 | 186 | * @} |
frank26080115 | 0:bf7b9fba3924 | 187 | */ |