发表于: 2017-2-16 15:14:56 | 显示全部楼层

飞思卡尔FRDM-K64F开发板非常适合用于以太网试验。但是在网上并没有在该开发板上快速搭建简易WEB服务器的教程。以下的内容将帮助大家在FRDM-K64F开发板上建立一个HTTP服务器,该服务器带有一个索引页以及支持打印一些随机数的简单CGI脚本的页面。这是使用该电路板开发任何基于网络的小工具的绝对最低的基础。

frdm-k64f120m-board-with-bluetooth-and-nrf24l01.png


在开发之前,建议仔细阅读一下这篇文章:HTTP server demo application。首先创建一个新项目,选择MK64FN1M0VLL12微控制器并保留默认设置,除了选项卡Utilities和Configure Image File Processing (FCARM)。 后面会详细介绍这些内容。接下来,选择Web服务器应用程序所有必需的包:CMSIS、以太网MAC和PHY的CMSIS驱动程序;

packs1.jpg


选择Kinetis SDK的框架包:

packs2.jpg


选择Kinetis SDK HAL驱动程序:

packs3.jpg


选择其他SDK包:

packs4.jpg


最后,选择网络包:

packs5.jpg


在项目浏览器中添加两个组:“SRC”和“Web Files”:

project1.jpg


在CMSIS组RTX_Conf_CM.C文件中配置RTOS:

rtxconf.jpg


在DEVICE组中编辑RTE_Device.h,启用MAC并选择RMII模式,并选择OSCERCLK作为时钟源:

eth1.jpg


编辑hardware_init.c文件并将以下代码放在该文件中:

  1. #include "board.h"
  2. #include "pin_mux.h"
  3. #include "fsl_clock_manager.h"
  4. #include "fsl_debug_console.h"

  5. void hardware_init(void) {
  6.   /* Disable Memory Protection Unit */
  7.   MPU->CESR &= ~MPU_CESR_VLD_MASK;

  8.   /* enable clock for PORTs */
  9.   CLOCK_SYS_EnablePortClock(PORTA_IDX);
  10.   CLOCK_SYS_EnablePortClock(PORTB_IDX);
  11.   CLOCK_SYS_EnablePortClock(PORTC_IDX);
  12.   CLOCK_SYS_EnablePortClock(PORTD_IDX);
  13.   CLOCK_SYS_EnablePortClock(PORTE_IDX);

  14.   configure_enet_pins (0);

  15.   /* Setup board clock source. */
  16.   g_xtal0ClkFreq = 50000000U;
  17.   g_xtalRtcClkFreq = 32768U;

  18.   /* Init board clock */
  19.   BOARD_ClockInit();
  20.   dbg_uart_init();
  21. }
复制代码

在NetWork组中编辑文件Net_Config.c中。输入本地主机名、内存池大小,启用系统服务和调整核心线程的堆栈大小:

netconfig.jpg


在文件Net_Config_Eth_0.h中编辑以太网配置,其地址适用于IP v4和v6的LAN:

ethconfig.jpg


在Net_Config_Legacy.h中启用所有传统回调。这是使能与旧版API的向后兼容性所必需的。在文件Net_Config_TCP.h中配置TCP层:

tcpconfig.jpg


在文件Net_Config_UDP.h中将UDP套接字调整为5

编辑文件Net_Config_HTTP_Server.h中的HTTP服务器配置:

httpconf.jpg

最后一步设置是在项目设置的选项卡Utilities和Configure Image File Processing (FCARM)中:输入web.c作为输出文件,并选择放置生成的源文件的文件组。定义图像文件的根文件夹。 “图像”文件是具有网页内容的所有文件。 FCARM将自动将这些文件转换为目标闪存中的阵列。


fcarm.jpg




现在是时候添加一些代码。

将main.c添加到SRC组中:

  1. #include "cmsis_os.h"                   /* CMSIS RTOS definitions             */
  2. #include "rl_net.h"                     /* Network definitions                */
  3. #include "board.h"                      /* BSP definitions                    */

  4. /*----------------------------------------------------------------------------
  5.   Main Thread 'main': Run Network
  6. *---------------------------------------------------------------------------*/
  7. int main (void) {

  8.   hardware_init();

  9.   net_initialize     ();

  10.   while(1) {
  11.     net_main ();
  12.     osThreadYield ();
  13.   }
  14. }
复制代码

在文件index.htm中添加主网页并将其添加到“Web Files”组中:

  1. <head>
  2. <title>S54MTB Testing Web server</title>
  3. </head>

  4. <body bgColor=#ffffff >
  5. <div align=center>
  6. The web server is working :)
  7. <br>
  8. </div></body>
  9. </html>
复制代码


现在可以编译和构建项目了。 首先,它需要一些时间。 如果一切正常,项目将编译为大约70k的代码,4.8k的RO数据的Web内容,需要大约38k的数据。


下一步是添加一些动态内容。

编辑索引文件并添加一个额外的链接到文件test.cgi:

  1. <head>
  2. <title>S54MTB Testing Web server</title>
  3. </head>

  4. <body bgColor=#ffffff >
  5. <div align=center>
  6. The web server is working :)
  7. <br>
  8. <hr>
  9. <a href="/test.cgi">Test - dynamic content</a>
  10. </div></body>
  11. </html>
复制代码

添加文件test.cgi到“Web Files”组:

  1. t <html><head><title>Test CGI</title>
  2. t <meta http-equiv="refresh" content="5"></head>
  3. t <h2 align=center><br>Test CGI --- print some RND numbers</h2>
  4. t <center>
  5. c r
  6. c a
  7. t </center>
  8. t </html>
  9. . End of script must be closed with period.
复制代码

关于通用网关接口(CGI)的脚本语言的一段话 - 摘自keil文档:

CGI is a standard method used to generate dynamic content on web pages. CGI stands for Common Gateway Interface and provides an interface between the HTTP server and programs generating web content. These programs are better known as CGI scripts. They are written in a scripting language. The Network Component provides such a scripting language. The HTTP server processes the script source file line by line and calls the CGI functions as needed. The output from a CGI function is sent to the web client (browser) as a part of the web page.

Each script line starts with a command character which specifies a command for the script interpreter. The scripting language itself is simple and works as follows:

i The script interpreter include a file from the file system and sends the content to the web browser.
t The line of text that follows will be sent to the browser.
c Calls the function netCGI_Script from the HTTP_Server_CGI.c file. It may be followed by a line of text which is passed to netCGI_Script as a pointer to an environment variable.
# This is a comment line and is ignored by the interpreter.
. Denotes the end of the script.
Note
The script files need to use the reserved filename extension of cgi, for the HTTP server’s script interpreter to recognize and process the files accordingly.
The script line length is limited to 120 characters.

上面的例子将调用函数uint32_t cgi_script(const char * env,char * buf,uint32_t buflen,uint32_t * pcgi);

我们应该通过重写这个函数为CGI调用添加解释器。 使用以下代码将文件http_cgi.c添加到SRC组:

  1. #include <stdio.h>
  2. #include "rl_net.h"
  3. #include "rl_net_lib.h"
  4. #include <stdlib.h> // rand()


  5. // Generate dynamic web data from a script line.
  6. uint32_t cgi_script (const char *env, char *buf, uint32_t buflen, uint32_t *pcgi) {
  7. uint32_t len = 0;

  8. switch (env[0]) {

  9. case 'r':
  10. // testing ... write random number
  11. len = sprintf (buf, "Tralala Random number between 0 and 50 is %d !!!<br>", rand() % 50);
  12. break;

  13. case 'a':
  14. // Write some text
  15. len = sprintf (buf, " Hopsasa Random number between 50 and 100 is %d !!!<br>", 50 + rand() % 50);
  16. break;
  17. }

  18. return (len);
  19. }
复制代码

现在我们新的小型网站服务器将响应以下两页:

web1.jpg

web2.jpg

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 47 | 回复: 68



手机版|

GMT+8, 2024-4-19 16:27 , Processed in 0.157789 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表