Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * Copyright (c) 2021 STMicroelectronics International N.V.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted, provided that the following conditions are met:
- *
- * 1. Redistribution of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of other
- * contributors to this software may be used to endorse or promote products
- * derived from this software without specific written permission.
- * 4. This software, including modifications and/or derivative works of this
- * software, must execute solely and exclusively on microcontroller or
- * microprocessor devices manufactured by or for STMicroelectronics.
- * 5. Redistribution and use of this software other than as permitted under
- * this license is void and will automatically terminate your rights under
- * this license.
- *
- * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
- * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
- * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "cmsis_os.h"
- #include "lwip.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdint.h"
- #include "string.h"
- #include "lwip/opt.h"
- #include "lwip/arch.h"
- #include "lwip/api.h"
- #include "fs.h"
- #include <stdbool.h>
- #include "button.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- osThreadId defaultTaskHandle;
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- void StartDefaultTask(void const * argument);
- /* USER CODE BEGIN PFP */
- void TaskStringOut(void const * argument);
- void TaskButtonSwitch (void const * argument);
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- osThreadId TaskStringOutHandle, TaskButtonSwitchHandle ;
- osMailQId strout_Queue;
- typedef struct struct_sock_t {
- uint16_t y_pos;
- struct netconn *conn;
- } struct_sock;
- struct_sock sock01;
- typedef struct struct_out_t {
- uint32_t tick_count;
- char str[60];
- uint16_t y_pos;
- } struct_out;
- #define MAIL_SIZE (uint32_t) 5
- static const unsigned char PAGE_HEADER_200_OK[] = {
- //"HTTP/1.1 200 OK"
- 0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d,
- 0x0a,
- //zero
- 0x00
- };
- static const unsigned char PAGE_HEADER_SERVER[] = {
- //"Server: lwIP/1.3.1 (http://savannah.nongnu.org/projects/lwip)"
- 0x53,0x65,0x72,0x76,0x65,0x72,0x3a,0x20,0x6c,0x77,0x49,0x50,0x2f,0x31,0x2e,0x33,
- 0x2e,0x31,0x20,0x28,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x73,0x61,0x76,0x61,0x6e,
- 0x6e,0x61,0x68,0x2e,0x6e,0x6f,0x6e,0x67,0x6e,0x75,0x2e,0x6f,0x72,0x67,0x2f,0x70,
- 0x72,0x6f,0x6a,0x65,0x63,0x74,0x73,0x2f,0x6c,0x77,0x69,0x70,0x29,0x0d,0x0a,
- //zero
- 0x00
- };
- static const unsigned char PAGE_HEADER_CONTENT_TEXT[] = {
- //"Content-type: text/html"
- 0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x74,0x79,0x70,0x65,0x3a,0x20,0x74,0x65,
- 0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x0d,0x0a,
- //zero
- 0x00
- };
- //*
- static const unsigned char PAGE_HEADER_CONTENT_STREAM[] = {
- //"Content-Type: application/octet-stream"
- 0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x3a,0x20,0x61,0x70,
- 0x70,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,0x6f,0x63,0x74,0x65,0x74,0x2d,
- 0x73,0x74,0x72,0x65,0x61,0x6d,0x0d,0x0a,
- //zero
- 0x00
- };
- static const unsigned char PAGE_HEADER_LEN[] = {
- //"Content-Length: "
- 0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20,
- //zero
- 0x00
- };
- static const unsigned char PAGE_HEADER_BYTES[] = {
- //"Accept-Ranges: bytes"
- 0x41,0x63,0x63,0x65,0x70,0x74,0x2d,0x52,0x61,0x6e,0x67,0x65,0x73,0x3a,0x20,0x62,
- 0x79,0x74,0x65,0x73,0x0d,0x0a,0x0d,0x0a,
- //zero
- 0x00
- };
- //------------------
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* USER CODE BEGIN RTOS_MUTEX */
- /* add mutexes, ... */
- /* USER CODE END RTOS_MUTEX */
- /* USER CODE BEGIN RTOS_SEMAPHORES */
- /* add semaphores, ... */
- /* USER CODE END RTOS_SEMAPHORES */
- /* USER CODE BEGIN RTOS_TIMERS */
- /* start timers, add new ones, ... */
- /* USER CODE END RTOS_TIMERS */
- /* USER CODE BEGIN RTOS_QUEUES */
- /* add queues, ... */
- osMailQDef(stroutqueue, MAIL_SIZE, struct_out);
- strout_Queue = osMailCreate(osMailQ(stroutqueue), NULL);
- /* USER CODE END RTOS_QUEUES */
- /* Create the thread(s) */
- /* definition and creation of defaultTask */
- osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 1280);
- defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
- /* USER CODE BEGIN RTOS_THREADS */
- /* add threads, ... */
- osThreadDef(tskstrout, TaskStringOut, osPriorityNormal, 0, 1280);
- TaskStringOutHandle = osThreadCreate(osThread(tskstrout), NULL);
- osThreadDef(tskstbutton, TaskButtonSwitch ,osPriorityIdle, 0, 280);
- TaskButtonSwitchHandle = osThreadCreate(osThread(tskstbutton), NULL);
- /* USER CODE END RTOS_THREADS */
- /* Start scheduler */
- osKernelStart();
- /* We should never get here as control is now taken by the scheduler */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
- /*Configure GPIO pin : PA0 */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : PD14 PD15 */
- GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- //---------------------------------------------------------------
- // task
- void TaskStringOut(void const * argument){
- osEvent event;
- struct_out *qstruct;
- for(;;){
- event = osMailGet(strout_Queue, osWaitForever);
- if (event.status == osEventMail){
- qstruct = event.value.p;
- }
- }
- }
- //---------------------------------------------------------------
- void DynWebPageStr(struct netconn *conn )
- {
- portCHAR PAGE_BODY [100];
- char *prt= "Button status WEB SITE\r\n";
- char *prtbutton="Button status DISCOVERY \r\n";
- sprintf ( (char*)PAGE_BODY ,"%s%s%s%s", prt, prtled_status, prtbutton, prtled_buttonstatus);
- netconn_write(conn, PAGE_BODY, strlen((char*)PAGE_BODY), NETCONN_COPY);
- }
- //-----------------------------------------
- static void tcp_thread(void *arg)
- {
- struct_out *qstruct;
- err_t err, recv_err;
- struct netconn *conn;
- struct netbuf *inbuf;
- struct netconn *newconn;
- struct_sock *arg_sock;
- arg_sock = (struct_sock*) arg;
- conn = arg_sock->conn;
- u16_t buflen;
- char* buf;
- struct fs_file file;
- for(;;){
- err = netconn_accept(conn, &newconn);
- if (err == ERR_OK){
- recv_err = netconn_recv(newconn, &inbuf);
- if (recv_err == ERR_OK){
- if (netconn_err(newconn) == ERR_OK){
- netbuf_data(inbuf, (void**)&buf, &buflen);
- if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0)){
- qstruct = osMailAlloc(strout_Queue, osWaitForever);
- osDelay(1);
- if ((strncmp((char const *)buf,"GET / ",6)==0)||(strncmp((char const *)buf,"GET /index.html",15)==0)) {
- fs_open(&file, "/index.html");
- netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
- fs_close(&file);
- } else if (strncmp((char const *)buf,"GET /style.css",14)==0) {
- fs_open(&file, "/style.css");
- netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
- fs_close(&file);
- } else if (strncmp((char const *)buf,"GET /color.html?c=",18)==0) {
- switch (buf[18]){
- case '1':
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
- ledStatus(led_on);
- break;
- case '2':
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
- ledStatus(led_off);
- break;
- }
- } else if (strncmp((char const *)buf,"GET /content.html",17)==0){
- DynWebPageStr(newconn);
- }
- }
- }
- }
- netconn_close(newconn);
- netbuf_delete(inbuf);
- netconn_delete(newconn);
- }
- osDelay(1);
- }
- }
- //---------------------------------------------------------------
- /* USER CODE END 4 */
- /* USER CODE BEGIN Header_StartDefaultTask */
- /**
- * @brief Function implementing the defaultTask thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_StartDefaultTask */
- void StartDefaultTask(void const * argument)
- {
- /* init code for LWIP */
- MX_LWIP_Init();
- /* USER CODE BEGIN 5 */
- struct netconn *conn;
- err_t err;
- conn = netconn_new(NETCONN_TCP);
- if(conn!=NULL) {
- sock01.conn = conn;
- err = netconn_bind(conn, NULL, 80);
- if (err == ERR_OK) {
- netconn_listen(conn);
- sys_thread_new("tcp_thread1", tcp_thread, (void*)&sock01, DEFAULT_THREAD_STACKSIZE, osPriorityNormal);
- } else {
- netconn_delete(conn);
- }
- }
- /* Infinite loop */
- for(;;) {
- osDelay(1);
- }
- /* USER CODE END 5 */
- }
- /**
- * @brief Period elapsed callback in non blocking mode
- * @note This function is called when TIM1 interrupt took place, inside
- * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
- * a global variable "uwTick" used as application time base.
- * @param htim : TIM handle
- * @retval None
- */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- /* USER CODE BEGIN Callback 0 */
- /* USER CODE END Callback 0 */
- if (htim->Instance == TIM1) {
- HAL_IncTick();
- }
- /* USER CODE BEGIN Callback 1 */
- /* USER CODE END Callback 1 */
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement