Advertisement
alexander_seredinov

9LLAtRHf.c

Dec 28th, 2021
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.76 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * Copyright (c) 2021 STMicroelectronics International N.V.
  14.   * All rights reserved.
  15.   *
  16.   * Redistribution and use in source and binary forms, with or without
  17.   * modification, are permitted, provided that the following conditions are met:
  18.   *
  19.   * 1. Redistribution of source code must retain the above copyright notice,
  20.   *    this list of conditions and the following disclaimer.
  21.   * 2. Redistributions in binary form must reproduce the above copyright notice,
  22.   *    this list of conditions and the following disclaimer in the documentation
  23.   *    and/or other materials provided with the distribution.
  24.   * 3. Neither the name of STMicroelectronics nor the names of other
  25.   *    contributors to this software may be used to endorse or promote products
  26.   *    derived from this software without specific written permission.
  27.   * 4. This software, including modifications and/or derivative works of this
  28.   *    software, must execute solely and exclusively on microcontroller or
  29.   *    microprocessor devices manufactured by or for STMicroelectronics.
  30.   * 5. Redistribution and use of this software other than as permitted under
  31.   *    this license is void and will automatically terminate your rights under
  32.   *    this license.
  33.   *
  34.   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35.   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36.   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37.   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38.   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39.   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40.   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42.   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43.   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44.   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45.   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46.   *
  47.   ******************************************************************************
  48.   */
  49. /* USER CODE END Header */
  50. /* Includes ------------------------------------------------------------------*/
  51. #include "main.h"
  52. #include "cmsis_os.h"
  53. #include "lwip.h"
  54.  
  55. /* Private includes ----------------------------------------------------------*/
  56. /* USER CODE BEGIN Includes */
  57. #include "stdint.h"
  58. #include "string.h"
  59.  
  60. #include "lwip/opt.h"
  61. #include "lwip/arch.h"
  62. #include "lwip/api.h"
  63. #include "fs.h"
  64. #include <stdbool.h>
  65. #include "button.h"
  66.  
  67.  
  68. /* USER CODE END Includes */
  69.  
  70. /* Private typedef -----------------------------------------------------------*/
  71. /* USER CODE BEGIN PTD */
  72.  
  73. /* USER CODE END PTD */
  74.  
  75. /* Private define ------------------------------------------------------------*/
  76. /* USER CODE BEGIN PD */
  77.  
  78. /* USER CODE END PD */
  79.  
  80. /* Private macro -------------------------------------------------------------*/
  81. /* USER CODE BEGIN PM */
  82.  
  83. /* USER CODE END PM */
  84.  
  85. /* Private variables ---------------------------------------------------------*/
  86. osThreadId defaultTaskHandle;
  87. /* USER CODE BEGIN PV */
  88.  
  89. /* USER CODE END PV */
  90.  
  91. /* Private function prototypes -----------------------------------------------*/
  92. void SystemClock_Config(void);
  93. static void MX_GPIO_Init(void);
  94. void StartDefaultTask(void const * argument);
  95.  
  96. /* USER CODE BEGIN PFP */
  97.  
  98. void TaskStringOut(void const * argument);
  99. void TaskButtonSwitch (void const * argument);
  100.  
  101.  
  102.  
  103. /* USER CODE END PFP */
  104.  
  105. /* Private user code ---------------------------------------------------------*/
  106. /* USER CODE BEGIN 0 */
  107.  
  108. osThreadId TaskStringOutHandle, TaskButtonSwitchHandle ;
  109.  
  110. osMailQId strout_Queue;
  111.  
  112. typedef struct struct_sock_t {
  113.     uint16_t y_pos;
  114.     struct netconn *conn;
  115. } struct_sock;
  116.  
  117. struct_sock sock01;
  118.  
  119.  
  120. typedef struct struct_out_t {
  121.     uint32_t tick_count;
  122.     char str[60];
  123.     uint16_t y_pos;
  124. } struct_out;
  125.  
  126. #define MAIL_SIZE (uint32_t) 5
  127.  
  128.  
  129. static const unsigned char PAGE_HEADER_200_OK[] = {
  130.     //"HTTP/1.1 200 OK"
  131.     0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d,
  132.     0x0a,
  133.     //zero
  134.     0x00
  135. };
  136. static const unsigned char PAGE_HEADER_SERVER[] = {
  137.     //"Server: lwIP/1.3.1 (http://savannah.nongnu.org/projects/lwip)"
  138.     0x53,0x65,0x72,0x76,0x65,0x72,0x3a,0x20,0x6c,0x77,0x49,0x50,0x2f,0x31,0x2e,0x33,
  139.     0x2e,0x31,0x20,0x28,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x73,0x61,0x76,0x61,0x6e,
  140.     0x6e,0x61,0x68,0x2e,0x6e,0x6f,0x6e,0x67,0x6e,0x75,0x2e,0x6f,0x72,0x67,0x2f,0x70,
  141.     0x72,0x6f,0x6a,0x65,0x63,0x74,0x73,0x2f,0x6c,0x77,0x69,0x70,0x29,0x0d,0x0a,
  142.     //zero
  143.     0x00
  144. };
  145. static const unsigned char PAGE_HEADER_CONTENT_TEXT[] = {
  146.     //"Content-type: text/html"
  147.     0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x74,0x79,0x70,0x65,0x3a,0x20,0x74,0x65,
  148.     0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x0d,0x0a,
  149.     //zero
  150.     0x00
  151. };
  152. //*
  153. static const unsigned char PAGE_HEADER_CONTENT_STREAM[] = {
  154.     //"Content-Type: application/octet-stream"
  155.     0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x3a,0x20,0x61,0x70,
  156.     0x70,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,0x6f,0x63,0x74,0x65,0x74,0x2d,
  157.     0x73,0x74,0x72,0x65,0x61,0x6d,0x0d,0x0a,
  158.     //zero
  159.     0x00
  160. };
  161. static const unsigned char PAGE_HEADER_LEN[] = {
  162.     //"Content-Length: "
  163.     0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20,
  164.     //zero
  165.     0x00
  166. };
  167. static const unsigned char PAGE_HEADER_BYTES[] = {
  168.     //"Accept-Ranges: bytes"
  169.     0x41,0x63,0x63,0x65,0x70,0x74,0x2d,0x52,0x61,0x6e,0x67,0x65,0x73,0x3a,0x20,0x62,
  170.     0x79,0x74,0x65,0x73,0x0d,0x0a,0x0d,0x0a,
  171.     //zero
  172.     0x00
  173. };
  174.  
  175. //------------------
  176. /* USER CODE END 0 */
  177.  
  178. /**
  179.   * @brief  The application entry point.
  180.   * @retval int
  181.   */
  182. int main(void)
  183. {
  184.     /* USER CODE BEGIN 1 */
  185.  
  186.     /* USER CODE END 1 */
  187.  
  188.     /* MCU Configuration--------------------------------------------------------*/
  189.  
  190.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  191.     HAL_Init();
  192.  
  193.     /* USER CODE BEGIN Init */
  194.  
  195.     /* USER CODE END Init */
  196.  
  197.     /* Configure the system clock */
  198.     SystemClock_Config();
  199.  
  200.     /* USER CODE BEGIN SysInit */
  201.  
  202.     /* USER CODE END SysInit */
  203.  
  204.     /* Initialize all configured peripherals */
  205.     MX_GPIO_Init();
  206.     /* USER CODE BEGIN 2 */
  207.  
  208.     /* USER CODE END 2 */
  209.  
  210.     /* USER CODE BEGIN RTOS_MUTEX */
  211.     /* add mutexes, ... */
  212.     /* USER CODE END RTOS_MUTEX */
  213.  
  214.     /* USER CODE BEGIN RTOS_SEMAPHORES */
  215.     /* add semaphores, ... */
  216.     /* USER CODE END RTOS_SEMAPHORES */
  217.  
  218.     /* USER CODE BEGIN RTOS_TIMERS */
  219.     /* start timers, add new ones, ... */
  220.     /* USER CODE END RTOS_TIMERS */
  221.  
  222.     /* USER CODE BEGIN RTOS_QUEUES */
  223.  
  224.     /* add queues, ... */
  225.  
  226.  
  227.     osMailQDef(stroutqueue, MAIL_SIZE, struct_out);
  228.     strout_Queue = osMailCreate(osMailQ(stroutqueue), NULL);
  229.     /* USER CODE END RTOS_QUEUES */
  230.  
  231.     /* Create the thread(s) */
  232.     /* definition and creation of defaultTask */
  233.     osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 1280);
  234.     defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  235.  
  236.     /* USER CODE BEGIN RTOS_THREADS */
  237.     /* add threads, ... */
  238.  
  239.  
  240.     osThreadDef(tskstrout, TaskStringOut, osPriorityNormal, 0, 1280);
  241.     TaskStringOutHandle = osThreadCreate(osThread(tskstrout), NULL);
  242.  
  243.  
  244.     osThreadDef(tskstbutton, TaskButtonSwitch ,osPriorityIdle, 0, 280);
  245.     TaskButtonSwitchHandle = osThreadCreate(osThread(tskstbutton), NULL);
  246.     /* USER CODE END RTOS_THREADS */
  247.  
  248.     /* Start scheduler */
  249.     osKernelStart();
  250.  
  251.     /* We should never get here as control is now taken by the scheduler */
  252.     /* Infinite loop */
  253.     /* USER CODE BEGIN WHILE */
  254.     while (1)
  255.     {
  256.         /* USER CODE END WHILE */
  257.  
  258.         /* USER CODE BEGIN 3 */
  259.     }
  260.     /* USER CODE END 3 */
  261. }
  262.  
  263. /**
  264.   * @brief System Clock Configuration
  265.   * @retval None
  266.   */
  267. void SystemClock_Config(void)
  268. {
  269.     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  270.     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  271.  
  272.     /** Configure the main internal regulator output voltage
  273.   */
  274.     __HAL_RCC_PWR_CLK_ENABLE();
  275.     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  276.     /** Initializes the RCC Oscillators according to the specified parameters
  277.   * in the RCC_OscInitTypeDef structure.
  278.   */
  279.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  280.     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  281.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  282.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  283.     RCC_OscInitStruct.PLL.PLLM = 8;
  284.     RCC_OscInitStruct.PLL.PLLN = 336;
  285.     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  286.     RCC_OscInitStruct.PLL.PLLQ = 4;
  287.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  288.     {
  289.         Error_Handler();
  290.     }
  291.     /** Initializes the CPU, AHB and APB buses clocks
  292.   */
  293.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  294.             |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  295.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  296.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  297.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  298.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  299.  
  300.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  301.     {
  302.         Error_Handler();
  303.     }
  304. }
  305.  
  306. /**
  307.   * @brief GPIO Initialization Function
  308.   * @param None
  309.   * @retval None
  310.   */
  311. static void MX_GPIO_Init(void)
  312. {
  313.     GPIO_InitTypeDef GPIO_InitStruct = {0};
  314.  
  315.     /* GPIO Ports Clock Enable */
  316.     __HAL_RCC_GPIOH_CLK_ENABLE();
  317.     __HAL_RCC_GPIOC_CLK_ENABLE();
  318.     __HAL_RCC_GPIOA_CLK_ENABLE();
  319.     __HAL_RCC_GPIOB_CLK_ENABLE();
  320.     __HAL_RCC_GPIOD_CLK_ENABLE();
  321.  
  322.     /*Configure GPIO pin Output Level */
  323.     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
  324.  
  325.     /*Configure GPIO pin : PA0 */
  326.     GPIO_InitStruct.Pin = GPIO_PIN_0;
  327.     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  328.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  329.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  330.  
  331.     /*Configure GPIO pins : PD14 PD15 */
  332.     GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  333.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  334.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  335.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  336.     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  337.  
  338. }
  339.  
  340. /* USER CODE BEGIN 4 */
  341. //---------------------------------------------------------------
  342.  
  343.  
  344. //     task
  345. void TaskStringOut(void const * argument){
  346.  
  347.  
  348.     osEvent event;
  349.     struct_out *qstruct;
  350.     for(;;){
  351.         event = osMailGet(strout_Queue, osWaitForever);
  352.         if (event.status == osEventMail){
  353.             qstruct = event.value.p;
  354.         }
  355.     }
  356.  
  357. }
  358.  
  359.  
  360. //---------------------------------------------------------------
  361. void DynWebPageStr(struct netconn *conn )
  362. {
  363.     portCHAR PAGE_BODY [100];
  364.     char *prt= "Button status WEB SITE\r\n";
  365.     char *prtbutton="Button status DISCOVERY \r\n";
  366.     sprintf ( (char*)PAGE_BODY ,"%s%s%s%s", prt, prtled_status, prtbutton, prtled_buttonstatus);
  367.     netconn_write(conn, PAGE_BODY, strlen((char*)PAGE_BODY), NETCONN_COPY);
  368. }
  369.  
  370.  
  371. //-----------------------------------------
  372.  
  373. static void tcp_thread(void *arg)
  374. {
  375.     struct_out *qstruct;
  376.     err_t err, recv_err;
  377.     struct netconn *conn;
  378.     struct netbuf *inbuf;
  379.     struct netconn *newconn;
  380.     struct_sock *arg_sock;
  381.     arg_sock = (struct_sock*) arg;
  382.     conn = arg_sock->conn;
  383.     u16_t buflen;
  384.     char* buf;
  385.     struct fs_file file;
  386.  
  387.     for(;;){
  388.         err = netconn_accept(conn, &newconn);
  389.         if (err == ERR_OK){
  390.             recv_err = netconn_recv(newconn, &inbuf);
  391.             if (recv_err == ERR_OK){
  392.                 if (netconn_err(newconn) == ERR_OK){
  393.                     netbuf_data(inbuf, (void**)&buf, &buflen);
  394.                     if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0)){
  395.                         qstruct = osMailAlloc(strout_Queue, osWaitForever);
  396.                         osDelay(1);
  397.                         if ((strncmp((char const *)buf,"GET / ",6)==0)||(strncmp((char const *)buf,"GET /index.html",15)==0)) {
  398.                             fs_open(&file, "/index.html");
  399.                             netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
  400.                             fs_close(&file);
  401.                         } else if (strncmp((char const *)buf,"GET /style.css",14)==0) {
  402.                             fs_open(&file, "/style.css");
  403.                             netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
  404.                             fs_close(&file);
  405.                         } else if (strncmp((char const *)buf,"GET /color.html?c=",18)==0) {
  406.  
  407.                             switch (buf[18]){
  408.                             case '1':
  409.                                 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
  410.                                 ledStatus(led_on);
  411.                                 break;
  412.                             case '2':
  413.                                 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
  414.                                 ledStatus(led_off);
  415.                                 break;
  416.                             }
  417.  
  418.                         } else if (strncmp((char const *)buf,"GET /content.html",17)==0){
  419.                             DynWebPageStr(newconn);
  420.                         }
  421.                     }
  422.                 }
  423.             }
  424.             netconn_close(newconn);
  425.             netbuf_delete(inbuf);
  426.             netconn_delete(newconn);
  427.         }
  428.         osDelay(1);
  429.     }
  430. }
  431.  
  432. //---------------------------------------------------------------
  433.  
  434.  
  435. /* USER CODE END 4 */
  436.  
  437. /* USER CODE BEGIN Header_StartDefaultTask */
  438. /**
  439.   * @brief  Function implementing the defaultTask thread.
  440.   * @param  argument: Not used
  441.   * @retval None
  442.   */
  443. /* USER CODE END Header_StartDefaultTask */
  444. void StartDefaultTask(void const * argument)
  445. {
  446.     /* init code for LWIP */
  447.     MX_LWIP_Init();
  448.     /* USER CODE BEGIN 5 */
  449.     struct netconn *conn;
  450.     err_t err;
  451.     conn = netconn_new(NETCONN_TCP);
  452.  
  453.     if(conn!=NULL) {
  454.         sock01.conn = conn;
  455.         err = netconn_bind(conn, NULL, 80);
  456.         if (err == ERR_OK) {
  457.             netconn_listen(conn);
  458.             sys_thread_new("tcp_thread1", tcp_thread, (void*)&sock01, DEFAULT_THREAD_STACKSIZE, osPriorityNormal);
  459.         } else {
  460.             netconn_delete(conn);
  461.         }
  462.     }
  463.  
  464.     /* Infinite loop */
  465.     for(;;) {
  466.         osDelay(1);
  467.     }
  468.     /* USER CODE END 5 */
  469. }
  470.  
  471. /**
  472.   * @brief  Period elapsed callback in non blocking mode
  473.   * @note   This function is called  when TIM1 interrupt took place, inside
  474.   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  475.   * a global variable "uwTick" used as application time base.
  476.   * @param  htim : TIM handle
  477.   * @retval None
  478.   */
  479. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  480. {
  481.     /* USER CODE BEGIN Callback 0 */
  482.  
  483.     /* USER CODE END Callback 0 */
  484.     if (htim->Instance == TIM1) {
  485.         HAL_IncTick();
  486.     }
  487.     /* USER CODE BEGIN Callback 1 */
  488.  
  489.  
  490.     /* USER CODE END Callback 1 */
  491. }
  492.  
  493. /**
  494.   * @brief  This function is executed in case of error occurrence.
  495.   * @retval None
  496.   */
  497. void Error_Handler(void)
  498. {
  499.     /* USER CODE BEGIN Error_Handler_Debug */
  500.     /* User can add his own implementation to report the HAL error return state */
  501.  
  502.     /* USER CODE END Error_Handler_Debug */
  503. }
  504.  
  505. #ifdef  USE_FULL_ASSERT
  506. /**
  507.   * @brief  Reports the name of the source file and the source line number
  508.   *         where the assert_param error has occurred.
  509.   * @param  file: pointer to the source file name
  510.   * @param  line: assert_param error line source number
  511.   * @retval None
  512.   */
  513. void assert_failed(uint8_t *file, uint32_t line)
  514. {
  515.     /* USER CODE BEGIN 6 */
  516.     /* User can add his own implementation to report the file name and line number,
  517.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  518.     /* USER CODE END 6 */
  519. }
  520. #endif /* USE_FULL_ASSERT */
  521.  
  522. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  523.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement