Webserver+3d print
Embed:
(wiki syntax)
Show/hide line numbers
main.c
00001 /** 00002 * @file main.c 00003 * @brief Main routine 00004 * 00005 * @section License 00006 * 00007 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software Foundation, 00021 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 * 00023 * @author Oryx Embedded SARL (www.oryx-embedded.com) 00024 * @version 1.7.6 00025 **/ 00026 00027 //Dependencies 00028 #include <stdlib.h> 00029 #include "stm32f7xx.h" 00030 #include "stm32f7xx_hal.h" 00031 #include "stm32f7xx_nucleo_144.h" 00032 #include "os_port.h" 00033 #include "core/net.h" 00034 #include "drivers/stm32f7xx_eth.h" 00035 #include "drivers/lan8742.h" 00036 #include "dhcp/dhcp_client.h" 00037 #include "ipv6/slaac.h" 00038 #include "ftp/ftp_client.h" 00039 #include "error.h" 00040 #include "debug.h" 00041 00042 //Application configuration 00043 #define APP_MAC_ADDR "00-AB-CD-EF-07-46" 00044 00045 #define APP_USE_DHCP ENABLED 00046 #define APP_IPV4_HOST_ADDR "192.168.0.20" 00047 #define APP_IPV4_SUBNET_MASK "255.255.255.0" 00048 #define APP_IPV4_DEFAULT_GATEWAY "192.168.0.254" 00049 #define APP_IPV4_PRIMARY_DNS "8.8.8.8" 00050 #define APP_IPV4_SECONDARY_DNS "8.8.4.4" 00051 00052 #define APP_USE_SLAAC ENABLED 00053 #define APP_IPV6_LINK_LOCAL_ADDR "fe80::746" 00054 #define APP_IPV6_PREFIX "2001:db8::" 00055 #define APP_IPV6_PREFIX_LENGTH 64 00056 #define APP_IPV6_GLOBAL_ADDR "2001:db8::746" 00057 #define APP_IPV6_ROUTER "fe80::1" 00058 #define APP_IPV6_PRIMARY_DNS "2001:4860:4860::8888" 00059 #define APP_IPV6_SECONDARY_DNS "2001:4860:4860::8844" 00060 00061 //Global variables 00062 DhcpClientSettings dhcpClientSettings; 00063 DhcpClientContext dhcpClientContext; 00064 SlaacSettings slaacSettings; 00065 SlaacContext slaacContext; 00066 00067 00068 /** 00069 * @brief System clock configuration 00070 **/ 00071 00072 void SystemClock_Config(void) 00073 { 00074 RCC_OscInitTypeDef RCC_OscInitStruct; 00075 RCC_ClkInitTypeDef RCC_ClkInitStruct; 00076 00077 //Enable Power Control clock 00078 __HAL_RCC_PWR_CLK_ENABLE(); 00079 00080 //Enable HSE Oscillator and activate PLL with HSE as source 00081 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 00082 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; 00083 RCC_OscInitStruct.HSIState = RCC_HSI_OFF; 00084 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 00085 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 00086 RCC_OscInitStruct.PLL.PLLM = 8; 00087 RCC_OscInitStruct.PLL.PLLN = 432; 00088 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; 00089 RCC_OscInitStruct.PLL.PLLQ = 9; 00090 HAL_RCC_OscConfig(&RCC_OscInitStruct); 00091 00092 //Enable overdrive mode 00093 HAL_PWREx_EnableOverDrive(); 00094 00095 //Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 00096 //clocks dividers 00097 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; 00098 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 00099 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 00100 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 00101 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 00102 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); 00103 } 00104 00105 00106 /** 00107 * @brief MPU configuration 00108 **/ 00109 00110 void MPU_Config(void) 00111 { 00112 MPU_Region_InitTypeDef MPU_InitStruct; 00113 00114 //Disable MPU 00115 HAL_MPU_Disable(); 00116 00117 //SRAM 00118 MPU_InitStruct.Enable = MPU_REGION_ENABLE; 00119 MPU_InitStruct.Number = MPU_REGION_NUMBER0; 00120 MPU_InitStruct.BaseAddress = 0x20000000; 00121 MPU_InitStruct.Size = MPU_REGION_SIZE_512KB; 00122 MPU_InitStruct.SubRegionDisable = 0; 00123 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0; 00124 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; 00125 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; 00126 MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE; 00127 MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE; 00128 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; 00129 HAL_MPU_ConfigRegion(&MPU_InitStruct); 00130 00131 //SRAM2 00132 MPU_InitStruct.Enable = MPU_REGION_ENABLE; 00133 MPU_InitStruct.Number = MPU_REGION_NUMBER1; 00134 MPU_InitStruct.BaseAddress = 0x2004C000; 00135 MPU_InitStruct.Size = MPU_REGION_SIZE_16KB; 00136 MPU_InitStruct.SubRegionDisable = 0; 00137 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0; 00138 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; 00139 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; 00140 MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE; 00141 MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; 00142 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; 00143 HAL_MPU_ConfigRegion(&MPU_InitStruct); 00144 00145 //Enable MPU 00146 HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT); 00147 } 00148 00149 00150 /** 00151 * @brief FTP client test routine 00152 * @return Error code 00153 **/ 00154 00155 error_t ftpClientTest(void) 00156 { 00157 error_t error; 00158 size_t length; 00159 IpAddr ipAddr; 00160 FtpClientContext ftpContext; 00161 static char_t buffer[256]; 00162 00163 //Debug message 00164 TRACE_INFO("\r\n\r\nResolving server name...\r\n"); 00165 //Resolve FTP server name 00166 error = getHostByName(NULL, "ftp.gnu.org", &ipAddr, 0); 00167 00168 //Any error to report? 00169 if(error) 00170 { 00171 //Debug message 00172 TRACE_INFO("Failed to resolve server name!\r\n"); 00173 //Exit immediately 00174 return error; 00175 } 00176 00177 //Debug message 00178 TRACE_INFO("Connecting to FTP server %s\r\n", ipAddrToString(&ipAddr, NULL)); 00179 //Connect to the FTP server 00180 error = ftpConnect(&ftpContext, NULL, &ipAddr, 21, FTP_NO_SECURITY | FTP_PASSIVE_MODE); 00181 00182 //Any error to report? 00183 if(error) 00184 { 00185 //Debug message 00186 TRACE_INFO("Failed to connect to FTP server!\r\n"); 00187 //Exit immediately 00188 return error; 00189 } 00190 00191 //Debug message 00192 TRACE_INFO("Successful connection\r\n"); 00193 00194 //Start of exception handling block 00195 do 00196 { 00197 //Login to the FTP server using the provided username and password 00198 error = ftpLogin(&ftpContext, "anonymous", "password", ""); 00199 //Any error to report? 00200 if(error) break; 00201 00202 //Open the specified file for reading 00203 error = ftpOpenFile(&ftpContext, "welcome.msg", FTP_FOR_READING | FTP_BINARY_TYPE); 00204 //Any error to report? 00205 if(error) break; 00206 00207 //Dump the contents of the file 00208 while(1) 00209 { 00210 //Read data 00211 error = ftpReadFile(&ftpContext, buffer, sizeof(buffer) - 1, &length, 0); 00212 //End of file? 00213 if(error) break; 00214 00215 //Properly terminate the string with a NULL character 00216 buffer[length] = '\0'; 00217 //Dump current data 00218 TRACE_INFO("%s", buffer); 00219 } 00220 00221 //End the string with a line feed 00222 TRACE_INFO("\r\n"); 00223 //Close the file 00224 error = ftpCloseFile(&ftpContext); 00225 00226 //End of exception handling block 00227 } while(0); 00228 00229 //Close the connection 00230 ftpClose(&ftpContext); 00231 //Debug message 00232 TRACE_INFO("Connection closed...\r\n"); 00233 00234 //Return status code 00235 return error; 00236 } 00237 00238 00239 /** 00240 * @brief User task 00241 **/ 00242 00243 void userTask(void *param) 00244 { 00245 //Endless loop 00246 while(1) 00247 { 00248 //User button pressed? 00249 if(BSP_PB_GetState(BUTTON_KEY)) 00250 { 00251 //FTP client test routine 00252 ftpClientTest(); 00253 00254 //Wait for the user button to be released 00255 while(BSP_PB_GetState(BUTTON_KEY)); 00256 } 00257 00258 //Loop delay 00259 osDelayTask(100); 00260 } 00261 } 00262 00263 00264 /** 00265 * @brief LED blinking task 00266 **/ 00267 00268 void blinkTask(void *param) 00269 { 00270 //Endless loop 00271 while(1) 00272 { 00273 BSP_LED_On(LED1); 00274 osDelayTask(100); 00275 BSP_LED_Off(LED1); 00276 osDelayTask(900); 00277 } 00278 } 00279 00280 00281 /** 00282 * @brief Main entry point 00283 * @return Unused value 00284 **/ 00285 00286 int_t main(void) 00287 { 00288 error_t error; 00289 NetInterface *interface; 00290 OsTask *task; 00291 MacAddr macAddr; 00292 #if (APP_USE_DHCP == DISABLED) 00293 Ipv4Addr ipv4Addr; 00294 #endif 00295 #if (APP_USE_SLAAC == DISABLED) 00296 Ipv6Addr ipv6Addr; 00297 #endif 00298 00299 //MPU configuration 00300 MPU_Config(); 00301 //HAL library initialization 00302 HAL_Init(); 00303 //Configure the system clock 00304 SystemClock_Config(); 00305 00306 //Enable I-cache and D-cache 00307 SCB_EnableICache(); 00308 SCB_EnableDCache(); 00309 00310 //Initialize kernel 00311 osInitKernel(); 00312 //Configure debug UART 00313 debugInit(115200); 00314 00315 //Start-up message 00316 TRACE_INFO("\r\n"); 00317 TRACE_INFO("**********************************\r\n"); 00318 TRACE_INFO("*** CycloneTCP FTP Client Demo ***\r\n"); 00319 TRACE_INFO("**********************************\r\n"); 00320 TRACE_INFO("Copyright: 2010-2017 Oryx Embedded SARL\r\n"); 00321 TRACE_INFO("Compiled: %s %s\r\n", __DATE__, __TIME__); 00322 TRACE_INFO("Target: STM32F746\r\n"); 00323 TRACE_INFO("\r\n"); 00324 00325 //LED configuration 00326 BSP_LED_Init(LED1); 00327 BSP_LED_Init(LED2); 00328 BSP_LED_Init(LED3); 00329 00330 //Clear LEDs 00331 BSP_LED_Off(LED1); 00332 BSP_LED_Off(LED2); 00333 BSP_LED_Off(LED3); 00334 00335 //Initialize user button 00336 BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); 00337 00338 //TCP/IP stack initialization 00339 error = netInit(); 00340 //Any error to report? 00341 if(error) 00342 { 00343 //Debug message 00344 TRACE_ERROR("Failed to initialize TCP/IP stack!\r\n"); 00345 } 00346 00347 //Configure the first Ethernet interface 00348 interface = &netInterface[0]; 00349 00350 //Set interface name 00351 netSetInterfaceName(interface, "eth0"); 00352 //Set host name 00353 netSetHostname(interface, "FTPClientDemo"); 00354 //Select the relevant network adapter 00355 netSetDriver(interface, &stm32f7xxEthDriver); 00356 netSetPhyDriver(interface, &lan8742PhyDriver); 00357 //Set host MAC address 00358 macStringToAddr(APP_MAC_ADDR, &macAddr); 00359 netSetMacAddr(interface, &macAddr); 00360 00361 //Initialize network interface 00362 error = netConfigInterface(interface); 00363 //Any error to report? 00364 if(error) 00365 { 00366 //Debug message 00367 TRACE_ERROR("Failed to configure interface %s!\r\n", interface->name); 00368 } 00369 00370 #if (IPV4_SUPPORT == ENABLED) 00371 #if (APP_USE_DHCP == ENABLED) 00372 //Get default settings 00373 dhcpClientGetDefaultSettings(&dhcpClientSettings); 00374 //Set the network interface to be configured by DHCP 00375 dhcpClientSettings.interface = interface; 00376 //Disable rapid commit option 00377 dhcpClientSettings.rapidCommit = FALSE; 00378 00379 //DHCP client initialization 00380 error = dhcpClientInit(&dhcpClientContext, &dhcpClientSettings); 00381 //Failed to initialize DHCP client? 00382 if(error) 00383 { 00384 //Debug message 00385 TRACE_ERROR("Failed to initialize DHCP client!\r\n"); 00386 } 00387 00388 //Start DHCP client 00389 error = dhcpClientStart(&dhcpClientContext); 00390 //Failed to start DHCP client? 00391 if(error) 00392 { 00393 //Debug message 00394 TRACE_ERROR("Failed to start DHCP client!\r\n"); 00395 } 00396 #else 00397 //Set IPv4 host address 00398 ipv4StringToAddr(APP_IPV4_HOST_ADDR, &ipv4Addr); 00399 ipv4SetHostAddr(interface, ipv4Addr); 00400 00401 //Set subnet mask 00402 ipv4StringToAddr(APP_IPV4_SUBNET_MASK, &ipv4Addr); 00403 ipv4SetSubnetMask(interface, ipv4Addr); 00404 00405 //Set default gateway 00406 ipv4StringToAddr(APP_IPV4_DEFAULT_GATEWAY, &ipv4Addr); 00407 ipv4SetDefaultGateway(interface, ipv4Addr); 00408 00409 //Set primary and secondary DNS servers 00410 ipv4StringToAddr(APP_IPV4_PRIMARY_DNS, &ipv4Addr); 00411 ipv4SetDnsServer(interface, 0, ipv4Addr); 00412 ipv4StringToAddr(APP_IPV4_SECONDARY_DNS, &ipv4Addr); 00413 ipv4SetDnsServer(interface, 1, ipv4Addr); 00414 #endif 00415 #endif 00416 00417 #if (IPV6_SUPPORT == ENABLED) 00418 #if (APP_USE_SLAAC == ENABLED) 00419 //Get default settings 00420 slaacGetDefaultSettings(&slaacSettings); 00421 //Set the network interface to be configured 00422 slaacSettings.interface = interface; 00423 00424 //SLAAC initialization 00425 error = slaacInit(&slaacContext, &slaacSettings); 00426 //Failed to initialize SLAAC? 00427 if(error) 00428 { 00429 //Debug message 00430 TRACE_ERROR("Failed to initialize SLAAC!\r\n"); 00431 } 00432 00433 //Start IPv6 address autoconfiguration process 00434 error = slaacStart(&slaacContext); 00435 //Failed to start SLAAC process? 00436 if(error) 00437 { 00438 //Debug message 00439 TRACE_ERROR("Failed to start SLAAC!\r\n"); 00440 } 00441 #else 00442 //Set link-local address 00443 ipv6StringToAddr(APP_IPV6_LINK_LOCAL_ADDR, &ipv6Addr); 00444 ipv6SetLinkLocalAddr(interface, &ipv6Addr); 00445 00446 //Set IPv6 prefix 00447 ipv6StringToAddr(APP_IPV6_PREFIX, &ipv6Addr); 00448 ipv6SetPrefix(interface, 0, &ipv6Addr, APP_IPV6_PREFIX_LENGTH); 00449 00450 //Set global address 00451 ipv6StringToAddr(APP_IPV6_GLOBAL_ADDR, &ipv6Addr); 00452 ipv6SetGlobalAddr(interface, 0, &ipv6Addr); 00453 00454 //Set default router 00455 ipv6StringToAddr(APP_IPV6_ROUTER, &ipv6Addr); 00456 ipv6SetDefaultRouter(interface, 0, &ipv6Addr); 00457 00458 //Set primary and secondary DNS servers 00459 ipv6StringToAddr(APP_IPV6_PRIMARY_DNS, &ipv6Addr); 00460 ipv6SetDnsServer(interface, 0, &ipv6Addr); 00461 ipv6StringToAddr(APP_IPV6_SECONDARY_DNS, &ipv6Addr); 00462 ipv6SetDnsServer(interface, 1, &ipv6Addr); 00463 #endif 00464 #endif 00465 00466 //Create user task 00467 task = osCreateTask("User Task", userTask, NULL, 500, OS_TASK_PRIORITY_NORMAL); 00468 //Failed to create the task? 00469 if(task == OS_INVALID_HANDLE) 00470 { 00471 //Debug message 00472 TRACE_ERROR("Failed to create task!\r\n"); 00473 } 00474 00475 //Create a task to blink the LED 00476 task = osCreateTask("Blink", blinkTask, NULL, 500, OS_TASK_PRIORITY_NORMAL); 00477 //Failed to create the task? 00478 if(task == OS_INVALID_HANDLE) 00479 { 00480 //Debug message 00481 TRACE_ERROR("Failed to create task!\r\n"); 00482 } 00483 00484 //Start the execution of tasks 00485 osStartKernel(); 00486 00487 //This function should never return 00488 return 0; 00489 } 00490
Generated on Tue Jul 12 2022 17:10:14 by
