Facebook
From Rude Camel, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 221
  1.  
  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) 2018 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. /* Includes ------------------------------------------------------------------*/
  50. #include "main.h"
  51. #include "stm32f7xx_hal.h"
  52. #include "cmsis_os.h"
  53. #include "lwip.h"
  54.  
  55. /* USER CODE BEGIN Includes */
  56. #include  <errno.h>
  57. #include  <sys/unistd.h>
  58.  
  59. #include "stm32746g_discovery_lcd.h"
  60. #include "Utilities/Fonts/fonts.h"
  61. #include "stm32746g_discovery_ts.h"
  62. #include "term_io.h"
  63. #include "dbgu.h"
  64. #include "ansi.h"
  65.  
  66. #include "FreeRTOS.h"
  67. #include "task.h"
  68. #include "queue.h"
  69.  
  70. #include "lwip/opt.h"
  71. #include "lwip/api.h"
  72. #include "lwip/apps/fs.h"
  73. #include "lwip/dhcp.h"
  74. #include "lwip/tcpip.h"
  75. #include "lwip/netdb.h"
  76. #include "lwip/sockets.h"
  77.  
  78. #include "lwip.h"
  79.  
  80. #include "wm8994/wm8994.h"
  81.  
  82. //
  83. #include "lwip/apps/mqtt.h"
  84.  
  85.  
  86. /* USER CODE END Includes */
  87.  
  88. /* Private variables ---------------------------------------------------------*/
  89. ADC_HandleTypeDef hadc3;
  90.  
  91. CRC_HandleTypeDef hcrc;
  92.  
  93. DCMI_HandleTypeDef hdcmi;
  94.  
  95. DMA2D_HandleTypeDef hdma2d;
  96.  
  97. I2C_HandleTypeDef hi2c1;
  98. I2C_HandleTypeDef hi2c3;
  99.  
  100. LTDC_HandleTypeDef hltdc;
  101.  
  102. QSPI_HandleTypeDef hqspi;
  103.  
  104. RTC_HandleTypeDef hrtc;
  105.  
  106. SAI_HandleTypeDef hsai_BlockA2;
  107. SAI_HandleTypeDef hsai_BlockB2;
  108.  
  109. SD_HandleTypeDef hsd1;
  110.  
  111. SPDIFRX_HandleTypeDef hspdif;
  112.  
  113. SPI_HandleTypeDef hspi2;
  114.  
  115. TIM_HandleTypeDef htim1;
  116. TIM_HandleTypeDef htim2;
  117. TIM_HandleTypeDef htim3;
  118. TIM_HandleTypeDef htim5;
  119. TIM_HandleTypeDef htim7;
  120. TIM_HandleTypeDef htim8;
  121. TIM_HandleTypeDef htim12;
  122.  
  123. UART_HandleTypeDef huart1;
  124. UART_HandleTypeDef huart6;
  125.  
  126. SDRAM_HandleTypeDef hsdram1;
  127.  
  128. osThreadId defaultTaskHandle;
  129.  
  130. /* USER CODE BEGIN PV */
  131. /* Private variables ---------------------------------------------------------*/
  132.  
  133. /* USER CODE END PV */
  134.  
  135. /* Private function prototypes -----------------------------------------------*/
  136. void SystemClock_Config(void);
  137. static void MX_GPIO_Init(void);
  138. static void MX_I2C1_Init(void);
  139. static void MX_USART1_UART_Init(void);
  140. static void MX_USART6_UART_Init(void);
  141. void StartDefaultTask(void const * argument);
  142.  
  143. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
  144.  
  145.  
  146.  
  147.  
  148.  
  149. /* USER CODE BEGIN PFP */
  150. /* Private function prototypes -----------------------------------------------*/
  151.  
  152. /* USER CODE END PFP */
  153.  
  154. /* USER CODE BEGIN 0 */
  155. void mainTask(void* p);
  156.  
  157. osThreadId netconn_thread_handle;
  158.  
  159. #define LCD_X_SIZE                      RK043FN48H_WIDTH
  160. #define LCD_Y_SIZE                      RK043FN48H_HEIGHT
  161.  
  162. #define PRINTF_USES_HAL_TX              0
  163.  
  164. int __io_putchar(int ch)
  165. {
  166.         uint8_t data = ch;
  167.         #if PRINTF_USES_HAL_TX
  168.                 HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, (uint8_t*)&data, len, 100);
  169.         #else
  170.                 while(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE) == RESET) { ; }
  171.                 huart1.Instance->TDR = (uint16_t)data;
  172.         #endif
  173.         return 0;
  174. }
  175.  
  176. char inkey(void)
  177. {
  178.         uint32_t flags = huart1.Instance->ISR;
  179.  
  180.         if((flags & UART_FLAG_RXNE) || (flags & UART_FLAG_ORE))
  181.         {
  182.                 __HAL_UART_CLEAR_OREFLAG(&huart1);
  183.                 return (huart1.Instance->RDR);
  184.         }
  185.         else
  186.                 return 0;
  187. }
  188.  
  189. //partially based on available code examples
  190. static void lcd_start(void)
  191. {
  192.   /* LCD Initialization */
  193.   BSP_LCD_Init();
  194.  
  195.   /* LCD Initialization */
  196.   BSP_LCD_LayerDefaultInit(0, (unsigned int)0xC0000000);
  197.   //BSP_LCD_LayerDefaultInit(1, (unsigned int)lcd_image_bg+(LCD_X_SIZE*LCD_Y_SIZE*4));
  198.   BSP_LCD_LayerDefaultInit(1, (unsigned int)0xC0000000+(LCD_X_SIZE*LCD_Y_SIZE*4));
  199.  
  200.   /* Enable the LCD */
  201.   BSP_LCD_DisplayOn();
  202.  
  203.   /* Select the LCD Background Layer  */
  204.   BSP_LCD_SelectLayer(0);
  205.  
  206.   /* Clear the Background Layer */
  207.   BSP_LCD_Clear(LCD_COLOR_WHITE);
  208.   BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  209.  
  210.   BSP_LCD_SetColorKeying(1,LCD_COLOR_WHITE);
  211.  
  212.   /* Select the LCD Foreground Layer  */
  213.   BSP_LCD_SelectLayer(1);
  214.  
  215.   /* Clear the Foreground Layer */
  216.   BSP_LCD_Clear(LCD_COLOR_WHITE);
  217.   BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  218.  
  219.   /* Configure the transparency for foreground and background :
  220.      Increase the transparency */
  221.   BSP_LCD_SetTransparency(0, 255);
  222.   BSP_LCD_SetTransparency(1, 255);
  223. }
  224.  
  225. //[rmv]
  226. void draw_button(int xPos, int yPos, int width, int height, uint32_t color, uint8_t* text);
  227. void draw_background(void)
  228. {
  229.         /* Select the LCD Background Layer  */
  230.         BSP_LCD_SelectLayer(0);
  231.         BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
  232.         BSP_LCD_FillRect(0,0,LCD_X_SIZE,LCD_Y_SIZE);
  233.  
  234.         //select Foreground Layer
  235.         BSP_LCD_SelectLayer(1);
  236.         char greeting[] = "Hello";
  237.         draw_button(50,50,100,100,LCD_COLOR_MAGENTA,&greeting);
  238. }
  239.  
  240. void draw_button(int xPos, int yPos, int width, int height, uint32_t color, uint8_t* text){
  241.  
  242.         BSP_LCD_SelectLayer(0);
  243.         BSP_LCD_SetTextColor(color);
  244.         BSP_LCD_FillRect(xPos,yPos,width,height);
  245.         BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
  246.         BSP_LCD_DisplayStringAt(xPos+5, yPos+5, text, 0);
  247.  
  248. }
  249.  
  250. void draw_chart(float* data){
  251.  
  252.     int chart_height = 220;
  253.     int chart_length = 450;
  254.     int chart_y_pos = 20;
  255.     int chart_x_pos = 20;
  256.  
  257. //drawing coordinates system
  258.     BSP_LCD_SelectLayer(0);
  259.     BSP_LCD_Clear(LCD_COLOR_WHITE);
  260.     BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
  261.     BSP_LCD_DrawHLine(chart_x_pos,chart_y_pos,chart_length);
  262.     BSP_LCD_DrawVLine(chart_x_pos,chart_y_pos,chart_height);
  263.    
  264. //    BSP_LCD_FillTriangle(chart_length-10,chart_length-10,chart_length,chart_y_pos+5,chart_y_pos-5,chart_y_pos);
  265.     int n = 30; //sizeof(data)/sizeof(data[0]);
  266.     float max = data[0];
  267.     float min = 0;//data[0];
  268.  
  269.     for(int i=1; i<n; i++){
  270.       if(data[i] < min)
  271.           min = data[i];
  272.       if(data[i] > max)
  273.           max = data[i];
  274.     }
  275.     int min_i = 0;//min;
  276.     int max_i = max;
  277.     float coordinates_scale = chart_height/(max_i - min_i);
  278.     float amplitude = max - min; // ?
  279.     float scale = chart_height/amplitude; // ?
  280.  
  281. // draw scale
  282.     int y = chart_y_pos;
  283.     BSP_LCD_SetTextColor(LCD_COLOR_LIGHTGRAY);
  284.     for(int i=1;i<amplitude;i++){
  285.       BSP_LCD_DrawHLine(chart_x_pos,chart_y_pos+i*coordinates_scale,chart_length);
  286.     }
  287.  
  288. // draw data
  289.   BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
  290.  
  291.   int sample_len = chart_length/n;
  292.   for(int i=0; i<n-1; i++){
  293.     int x1,y1,x2,y2;
  294.     x1 = chart_x_pos + i * sample_len;
  295.     x2 = chart_x_pos + (i+1) * sample_len;
  296.     y1 = chart_y_pos + (data[i] - min_i)*coordinates_scale;
  297.     y2 = chart_y_pos + (data[i+1] - min_i)*coordinates_scale;
  298.  
  299.     BSP_LCD_DrawLine(x1,y1,x2,y2);
  300.  
  301.   }
  302.         BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
  303.         Point points[3];
  304.         points[0].X=chart_x_pos+5;
  305.         points[1].X=chart_x_pos-5;
  306.         points[2].X=chart_x_pos;
  307.        
  308.         points[0].Y=chart_y_pos+chart_height-10;
  309.         points[1].Y=chart_y_pos+chart_height-10;
  310.         points[2].Y=chart_y_pos+chart_height;
  311.         BSP_LCD_FillPolygon(points,3);
  312.  
  313.         points[0].X=chart_x_pos+chart_length-10;
  314.         points[1].X=chart_x_pos+chart_length;
  315.         points[2].X=chart_x_pos+chart_length-10;
  316.  
  317.         points[0].Y=chart_y_pos-5;
  318.         points[1].Y=chart_y_pos;
  319.         points[2].Y=chart_y_pos+5;
  320.         BSP_LCD_FillPolygon(points,3);
  321.         //BSP_LCD_FillPolygon(chart_x_pos-5,chart_x_pos-5,chart_x_pos+5,chart_height-10,chart_height,chart_height-10);
  322.  
  323.  
  324.         BSP_LCD_SetFont(&Font12);
  325.         BSP_LCD_DisplayStringAt(chart_x_pos-10,chart_y_pos-5, (uint8_t*)"0", LEFT_MODE);
  326.         BSP_LCD_DisplayStringAt(chart_x_pos-10,chart_y_pos-5+5*coordinates_scale, (uint8_t*)"5", LEFT_MODE);
  327.         BSP_LCD_DisplayStringAt(chart_x_pos-10,chart_y_pos-5+10*coordinates_scale, (uint8_t*)"10", LEFT_MODE);
  328.         BSP_LCD_DisplayStringAt(chart_x_pos-10,chart_y_pos-5+15*coordinates_scale, (uint8_t*)"15", LEFT_MODE);
  329.         }
  330.  
  331. static TS_StateTypeDef  TS_State;
  332.  
  333.  
  334. int initialize_touchscreen(void)
  335. {
  336.         uint8_t  status = 0;
  337.         status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
  338.         if(status != TS_OK) return -1;
  339.         return 0;
  340. }
  341.  
  342.  int print_data(char* data, int client_id, int len){
  343.         BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  344.         BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  345.         BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
  346.         if(client_id == 1){
  347.                 printf("%s\n",data);
  348.                 int i;
  349.                 for(i = 10; i < 70;i++)
  350.                         BSP_LCD_ClearStringLine(i);
  351.                 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  352.                 BSP_LCD_FillRect(0.3*LCD_X_SIZE,0.1*LCD_Y_SIZE,10,50);
  353.                 BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
  354.                 BSP_LCD_ClearStringLine(10);
  355.                 BSP_LCD_ClearStringLine(50);
  356.                 char target[100];
  357.  
  358.                 strncpy(target, data, len);
  359.                 target[len] = '\0';
  360.                 BSP_LCD_DisplayStringAt(0, 10, (uint8_t*)"Temperature1 is:", CENTER_MODE);
  361.                 BSP_LCD_DisplayStringAt(0, 50, (uint8_t*)target, CENTER_MODE);
  362.         }
  363.         else{
  364.         printf("%s\n",data);
  365.         char target[100];
  366.         BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  367.                 BSP_LCD_FillRect(0.3*LCD_X_SIZE,0.1*LCD_Y_SIZE,10,50);
  368.                 BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
  369.         strncpy(target, data, len);
  370.         target[len] = '\0';
  371.         BSP_LCD_DisplayStringAt(0, 70, (uint8_t*)"Temperature2 is:", CENTER_MODE);
  372.         BSP_LCD_DisplayStringAt(0, 110, (uint8_t*)target, CENTER_MODE);
  373.         }
  374. }
  375.  
  376.  
  377. /* USER CODE END 0 */
  378.  
  379. /**
  380.   * @brief  The application entry point.
  381.   *
  382.   * @retval None
  383.   */
  384. int main(void)
  385. {
  386.  
  387.  
  388.   float data[30] = {20.34,21.39,22.05,25.07,26.97,24.27,26.34,26.39,26.32,22.01,20.34,21.39,22.05,25.07,26.97,24.27,26.34,26.39,26.32,22.01,20.34,21.39,22.05,25.07,26.97,24.27,26.34,26.39,26.32,22.01};
  389.  
  390.   /* USER CODE BEGIN 1 */
  391.  
  392.   /* USER CODE END 1 */
  393.  
  394.   /* MCU Configuration----------------------------------------------------------*/
  395.  
  396.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  397.   HAL_Init();
  398.  
  399.   /* USER CODE BEGIN Init */
  400.  
  401.   /* USER CODE END Init */
  402.  
  403.   /* Configure the system clock */
  404.   SystemClock_Config();
  405.  
  406.   /* USER CODE BEGIN SysInit */
  407.  
  408.   /* USER CODE END SysInit */
  409.  
  410.   /* Initialize all configured peripherals */
  411.   MX_GPIO_Init();
  412.   MX_USART1_UART_Init();
  413.   MX_USART6_UART_Init();
  414.   /* USER CODE BEGIN 2 */
  415.   debug_init(&huart1);
  416.  
  417.   xprintf(ANSI_FG_GREEN "STM32F746 Discovery Project" ANSI_FG_DEFAULT "\n");
  418.  
  419.   printf("Regular printf\n");
  420.  
  421. lcd_start();
  422. draw_background();
  423. initialize_touchscreen();
  424. draw_chart(data);
  425.  
  426.   /* Create the thread(s) */
  427.   /* definition and creation of defaultTask */
  428.   osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 4096);
  429.   defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  430.  
  431.  
  432.   /* Start scheduler */
  433.   osKernelStart();
  434.  
  435.   /* We should never get here as control is now taken by the scheduler */
  436.  
  437.   /* Infinite loop */
  438.   /* USER CODE BEGIN WHILE */
  439.   while (1)
  440.   {
  441.  
  442.   }
  443.  
  444. }
  445.  
  446. /**
  447.   * @brief System Clock Configuration
  448.   * @retval None
  449.   */
  450. void SystemClock_Config(void)
  451. {
  452.  
  453.   RCC_OscInitTypeDef RCC_OscInitStruct;
  454.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  455.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  456.  
  457.     /**Configure the main internal regulator output voltage
  458.     */
  459.   __HAL_RCC_PWR_CLK_ENABLE();
  460.  
  461.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  462.  
  463.     /**Initializes the CPU, AHB and APB busses clocks
  464.     */
  465.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  466.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  467.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  468.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  469.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  470.   RCC_OscInitStruct.PLL.PLLM = 25;
  471.   RCC_OscInitStruct.PLL.PLLN = 400;
  472.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  473.   RCC_OscInitStruct.PLL.PLLQ = 8;
  474.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  475.   {
  476.     _Error_Handler(__FILE__, __LINE__);
  477.   }
  478.  
  479.     /**Activate the Over-Drive mode
  480.     */
  481.   if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  482.   {
  483.     _Error_Handler(__FILE__, __LINE__);
  484.   }
  485.  
  486.     /**Initializes the CPU, AHB and APB busses clocks
  487.     */
  488.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  489.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  490.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  491.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  492.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  493.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  494.  
  495.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6) != HAL_OK)
  496.   {
  497.     _Error_Handler(__FILE__, __LINE__);
  498.   }
  499.  
  500.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPDIFRX|RCC_PERIPHCLK_LTDC
  501.                               |RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
  502.                               |RCC_PERIPHCLK_USART6|RCC_PERIPHCLK_SAI2
  503.                               |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_I2C3
  504.                               |RCC_PERIPHCLK_SDMMC1|RCC_PERIPHCLK_CLK48;
  505.   PeriphClkInitStruct.PLLI2S.PLLI2SN = 100;
  506.   PeriphClkInitStruct.PLLI2S.PLLI2SP = RCC_PLLP_DIV2;
  507.   PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  508.   PeriphClkInitStruct.PLLI2S.PLLI2SQ = 2;
  509.   PeriphClkInitStruct.PLLSAI.PLLSAIN = 192;
  510.   PeriphClkInitStruct.PLLSAI.PLLSAIR = 4;
  511.   PeriphClkInitStruct.PLLSAI.PLLSAIQ = 4;
  512.   PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV4;
  513.   PeriphClkInitStruct.PLLI2SDivQ = 1;
  514.   PeriphClkInitStruct.PLLSAIDivQ = 1;
  515.   PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_8;
  516.   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  517.   PeriphClkInitStruct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLSAI;
  518.   PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  519.   PeriphClkInitStruct.Usart6ClockSelection = RCC_USART6CLKSOURCE_PCLK2;
  520.   PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  521.   PeriphClkInitStruct.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  522.   PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLLSAIP;
  523.   PeriphClkInitStruct.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_CLK48;
  524.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  525.   {
  526.     _Error_Handler(__FILE__, __LINE__);
  527.   }
  528.  
  529.     /**Configure the Systick interrupt time
  530.     */
  531.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  532.  
  533.     /**Configure the Systick
  534.     */
  535.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  536.  
  537.   /* SysTick_IRQn interrupt configuration */
  538.   HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  539. }
  540.  
  541.  
  542.  
  543.  
  544. /* USART1 init function */
  545. static void MX_USART1_UART_Init(void)
  546. {
  547.  
  548.   huart1.Instance = USART1;
  549.   huart1.Init.BaudRate = 115200;
  550.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  551.   huart1.Init.StopBits = UART_STOPBITS_1;
  552.   huart1.Init.Parity = UART_PARITY_NONE;
  553.   huart1.Init.Mode = UART_MODE_TX_RX;
  554.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  555.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  556.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  557.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  558.   if (HAL_UART_Init(&huart1) != HAL_OK)
  559.   {
  560.     _Error_Handler(__FILE__, __LINE__);
  561.   }
  562.  
  563. }
  564.  
  565. /* USART6 init function */
  566. static void MX_USART6_UART_Init(void)
  567. {
  568.  
  569.   huart6.Instance = USART6;
  570.   huart6.Init.BaudRate = 115200;
  571.   huart6.Init.WordLength = UART_WORDLENGTH_8B;
  572.   huart6.Init.StopBits = UART_STOPBITS_1;
  573.   huart6.Init.Parity = UART_PARITY_NONE;
  574.   huart6.Init.Mode = UART_MODE_TX_RX;
  575.   huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  576.   huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  577.   huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  578.   huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  579.   if (HAL_UART_Init(&huart6) != HAL_OK)
  580.   {
  581.     _Error_Handler(__FILE__, __LINE__);
  582.   }
  583.  
  584. }
  585.  
  586.  
  587.  
  588. /** Configure pins as
  589.         * Analog
  590.         * Input
  591.         * Output
  592.         * EVENT_OUT
  593.         * EXTI
  594.      PB5   ------> USB_OTG_HS_ULPI_D7
  595.      PH4   ------> USB_OTG_HS_ULPI_NXT
  596.      PB13   ------> USB_OTG_HS_ULPI_D6
  597.      PB12   ------> USB_OTG_HS_ULPI_D5
  598.      PC0   ------> USB_OTG_HS_ULPI_STP
  599.      PC2   ------> USB_OTG_HS_ULPI_DIR
  600.      PA5   ------> USB_OTG_HS_ULPI_CK
  601.      PB10   ------> USB_OTG_HS_ULPI_D3
  602.      PA3   ------> USB_OTG_HS_ULPI_D0
  603.      PB1   ------> USB_OTG_HS_ULPI_D2
  604.      PB0   ------> USB_OTG_HS_ULPI_D1
  605.      PB11   ------> USB_OTG_HS_ULPI_D4
  606. */
  607. static void MX_GPIO_Init(void)
  608. {
  609.  
  610.   GPIO_InitTypeDef GPIO_InitStruct;
  611.  
  612.   /* GPIO Ports Clock Enable */
  613.   __HAL_RCC_GPIOE_CLK_ENABLE();
  614.   __HAL_RCC_GPIOG_CLK_ENABLE();
  615.   __HAL_RCC_GPIOB_CLK_ENABLE();
  616.   __HAL_RCC_GPIOD_CLK_ENABLE();
  617.   __HAL_RCC_GPIOC_CLK_ENABLE();
  618.   __HAL_RCC_GPIOA_CLK_ENABLE();
  619.   __HAL_RCC_GPIOJ_CLK_ENABLE();
  620.   __HAL_RCC_GPIOI_CLK_ENABLE();
  621.   __HAL_RCC_GPIOK_CLK_ENABLE();
  622.   __HAL_RCC_GPIOF_CLK_ENABLE();
  623.   __HAL_RCC_GPIOH_CLK_ENABLE();
  624.  
  625.   /*Configure GPIO pin Output Level */
  626.   HAL_GPIO_WritePin(OTG_FS_PowerSwitchOn_GPIO_Port, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);
  627.  
  628.   /*Configure GPIO pin Output Level */
  629.   HAL_GPIO_WritePin(GPIOI, ARDUINO_D7_Pin|ARDUINO_D8_Pin, GPIO_PIN_RESET);
  630.  
  631.   /*Configure GPIO pin Output Level */
  632.   HAL_GPIO_WritePin(LCD_BL_CTRL_GPIO_Port, LCD_BL_CTRL_Pin, GPIO_PIN_SET);
  633.  
  634.   /*Configure GPIO pin Output Level */
  635.   HAL_GPIO_WritePin(LCD_DISP_GPIO_Port, LCD_DISP_Pin, GPIO_PIN_SET);
  636.  
  637.   /*Configure GPIO pin Output Level */
  638.   HAL_GPIO_WritePin(DCMI_PWR_EN_GPIO_Port, DCMI_PWR_EN_Pin, GPIO_PIN_RESET);
  639.  
  640.   /*Configure GPIO pin Output Level */
  641.   HAL_GPIO_WritePin(GPIOG, ARDUINO_D4_Pin|ARDUINO_D2_Pin|EXT_RST_Pin, GPIO_PIN_RESET);
  642.  
  643.   /*Configure GPIO pin : OTG_HS_OverCurrent_Pin */
  644.   GPIO_InitStruct.Pin = OTG_HS_OverCurrent_Pin;
  645.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  646.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  647.   HAL_GPIO_Init(OTG_HS_OverCurrent_GPIO_Port, &GPIO_InitStruct);
  648.  
  649.   /*Configure GPIO pins : ULPI_D7_Pin ULPI_D6_Pin ULPI_D5_Pin ULPI_D3_Pin
  650.                            ULPI_D2_Pin ULPI_D1_Pin ULPI_D4_Pin */
  651.   GPIO_InitStruct.Pin = ULPI_D7_Pin|ULPI_D6_Pin|ULPI_D5_Pin|ULPI_D3_Pin
  652.                           |ULPI_D2_Pin|ULPI_D1_Pin|ULPI_D4_Pin;
  653.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  654.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  655.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  656.   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  657.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  658.  
  659.   /*Configure GPIO pin : OTG_FS_VBUS_Pin */
  660.   GPIO_InitStruct.Pin = OTG_FS_VBUS_Pin;
  661.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  662.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  663.   HAL_GPIO_Init(OTG_FS_VBUS_GPIO_Port, &GPIO_InitStruct);
  664.  
  665.   /*Configure GPIO pin : Audio_INT_Pin */
  666.   /*Configure GPIO pin : OTG_FS_PowerSwitchOn_Pin */
  667.   GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin;
  668.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  669.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  670.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  671.   HAL_GPIO_Init(OTG_FS_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
  672.  
  673.   /*Configure GPIO pins : ARDUINO_D7_Pin ARDUINO_D8_Pin LCD_DISP_Pin */
  674.   GPIO_InitStruct.Pin = ARDUINO_D7_Pin|ARDUINO_D8_Pin|LCD_DISP_Pin;
  675.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  676.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  677.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  678.   HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
  679.  
  680.   /*Configure GPIO pin : uSD_Detect_Pin */
  681.   GPIO_InitStruct.Pin = uSD_Detect_Pin;
  682.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  683.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  684.   HAL_GPIO_Init(uSD_Detect_GPIO_Port, &GPIO_InitStruct);
  685.  
  686.   /*Configure GPIO pin : LCD_BL_CTRL_Pin */
  687.   GPIO_InitStruct.Pin = LCD_BL_CTRL_Pin;
  688.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  689.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  690.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  691.   HAL_GPIO_Init(LCD_BL_CTRL_GPIO_Port, &GPIO_InitStruct);
  692.  
  693.   /*Configure GPIO pin : OTG_FS_OverCurrent_Pin */
  694.   GPIO_InitStruct.Pin = OTG_FS_OverCurrent_Pin;
  695.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  696.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  697.   HAL_GPIO_Init(OTG_FS_OverCurrent_GPIO_Port, &GPIO_InitStruct);
  698.  
  699.   /*Configure GPIO pins : TP3_Pin NC2_Pin */
  700.   GPIO_InitStruct.Pin = TP3_Pin|NC2_Pin;
  701.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  702.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  703.   HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  704.  
  705.   /*Configure GPIO pin : DCMI_PWR_EN_Pin */
  706.   GPIO_InitStruct.Pin = DCMI_PWR_EN_Pin;
  707.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  708.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  709.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  710.   HAL_GPIO_Init(DCMI_PWR_EN_GPIO_Port, &GPIO_InitStruct);
  711.  
  712.   /*Configure GPIO pin : LCD_INT_Pin */
  713.   GPIO_InitStruct.Pin = LCD_INT_Pin;
  714.   GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  715.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  716.   HAL_GPIO_Init(LCD_INT_GPIO_Port, &GPIO_InitStruct);
  717.  
  718.   /*Configure GPIO pin : ULPI_NXT_Pin */
  719.   GPIO_InitStruct.Pin = ULPI_NXT_Pin;
  720.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  721.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  722.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  723.   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  724.   HAL_GPIO_Init(ULPI_NXT_GPIO_Port, &GPIO_InitStruct);
  725.  
  726.   /*Configure GPIO pins : ARDUINO_D4_Pin ARDUINO_D2_Pin EXT_RST_Pin */
  727.   GPIO_InitStruct.Pin = ARDUINO_D4_Pin|ARDUINO_D2_Pin|EXT_RST_Pin;
  728.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  729.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  730.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  731.   HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  732.  
  733.   /*Configure GPIO pins : ULPI_STP_Pin ULPI_DIR_Pin */
  734.   GPIO_InitStruct.Pin = ULPI_STP_Pin|ULPI_DIR_Pin;
  735.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  736.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  737.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  738.   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  739.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  740.  
  741.   /*Configure GPIO pin : RMII_RXER_Pin */
  742.   GPIO_InitStruct.Pin = RMII_RXER_Pin;
  743.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  744.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  745.   HAL_GPIO_Init(RMII_RXER_GPIO_Port, &GPIO_InitStruct);
  746.  
  747.   /*Configure GPIO pins : ULPI_CLK_Pin ULPI_D0_Pin */
  748.   GPIO_InitStruct.Pin = ULPI_CLK_Pin|ULPI_D0_Pin;
  749.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  750.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  751.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  752.   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  753.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  754.  
  755. }
  756.  
  757.  
  758.  
  759. /* USER CODE END 4 */
  760.  
  761. /* USER CODE BEGIN Header_StartDefaultTask */
  762. /**
  763.   * @brief  Function implementing the defaultTask thread.
  764.   * @param  argument: Not used
  765.   * @retval None
  766.   */
  767. /* USER CODE END Header_StartDefaultTask */
  768. static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status);
  769. static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len);
  770. static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags);
  771. static void mqtt_sub_request_cb(void *arg, err_t result);
  772. static void mqtt_pub_request_cb(void *arg, err_t result);
  773. int example_do_connect(mqtt_client_t *client)
  774. {
  775.   struct mqtt_connect_client_info_t ci;
  776.   err_t err;
  777.  
  778.   /* Setup an empty client info structure */
  779.   memset(&ci, 0, sizeof(ci));
  780.  
  781.   /* Minimal amount of information required is client identifier, so set it here */
  782.   ci.client_id = "lwip_test";
  783.  
  784.   /* Initiate client and connect to server, if this fails immediately an error code is returned
  785.      otherwise mqtt_connection_cb will be called with connection result after attempting
  786.      to establish a connection with the server.
  787.      For now MQTT version 3.1.1 is always used */
  788.   ip_addr_t ip_addr;
  789.   if(1 == ipaddr_aton( "192.168.0.101",&ip_addr)){
  790.         printf("Created ip");
  791.   }
  792.   err = mqtt_client_connect(client, &ip_addr, 1883, mqtt_connection_cb, 0, &ci);
  793.  
  794.   /* For now just print the result code if something goes wrong */
  795.   if(err != ERR_OK) {
  796.     printf("mqtt_connect return tutaj %d\n", err);
  797.         return 1;
  798.  
  799.   }
  800.   else{
  801.         printf("mqtt_connectbvgfdbgfdgfd\n");
  802.         return 0;
  803.   }
  804. }
  805.  
  806. static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
  807. {
  808.  
  809.   err_t err;
  810.   err_t err2;
  811.   if(status == MQTT_CONNECT_ACCEPTED) {
  812.     printf("mqtt_connection_cb: Successfully connected\n");
  813.  
  814.     /* Setup callback for incoming publish requests */
  815.     mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg);
  816.  
  817.     /* Subscribe to a topic named "subtopic" with QoS level 1, call mqtt_sub_request_cb with result */
  818.     err = mqtt_subscribe(client, "esp/temperature/client2", 1, mqtt_sub_request_cb, arg);
  819.         err2 = mqtt_subscribe(client, "esp/temperature/client1", 1, mqtt_sub_request_cb, arg);
  820.  
  821.     if(err != ERR_OK || err2 != ERR_OK) {
  822.       printf("mqtt_subscribe return: %d\n", err);
  823.     }
  824.   } else {
  825.     printf("mqtt_connection_cb: Disconnected, reason: %d\n", status);
  826.     /* Its more nice to be connected, so try to reconnect */
  827.     example_do_connect(client);
  828.   }
  829.  
  830. }
  831.  
  832. static void mqtt_sub_request_cb(void *arg, err_t result)
  833. {
  834.   /* Just print the result code here for simplicity,
  835.      normal behaviour would be to take some action if subscribe fails like
  836.      notifying user, retry subscribe or disconnect from server */
  837.   printf("Subscribe result: %d\n", result);
  838.   if(result == ERR_OK){
  839.   printf("Subscribe result is OK!\n");
  840.   }
  841. }
  842.  
  843. //-----------------------------------------------------------------
  844. //3. Implementing callbacks for incoming publish and data
  845.  
  846. /* The idea is to demultiplex topic and create some reference to be used in data callbacks
  847.    Example here uses a global variable, better would be to use a member in arg
  848.    If RAM and CPU budget allows it, the easiest implementation might be to just take a copy of
  849.    the topic string and use it in mqtt_incoming_data_cb
  850. */
  851. static int inpub_id;
  852. static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) // callback invoked when publish starts, contain topic and total length of payload
  853. {
  854.   printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len);
  855.  
  856.   /* Decode topic string into a user defined reference */
  857.   if(strcmp(topic, "print_payload") == 0) {
  858.     inpub_id = 0;
  859.   } else if(topic[0] == 'A') {
  860.     /* All topics starting with 'A' might be handled at the same way */
  861.     inpub_id = 1;
  862.   } else if(strcmp(topic,"esp/temperature/client1") == 0){
  863.         inpub_id = 2;
  864.   }
  865.   else {
  866.     /* For all other topics */
  867.     inpub_id = 3;
  868.   }
  869. }
  870.  
  871. static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) // callback for each fragment of payload that arrives
  872. {
  873.   printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags);
  874.  
  875.   if(flags & MQTT_DATA_FLAG_LAST) {
  876.     /* Last fragment of payload received (or whole part if payload fits receive buffer
  877.        See MQTT_VAR_HEADER_BUFFER_LEN)  */
  878.  
  879.     /* Call function or do action depending on reference, in this case inpub_id */
  880.     if(inpub_id == 0) {
  881.       /* Don't trust the publisher, check zero termination */
  882.       if(data[len-1] == 0) {
  883.         printf("mqtt_incoming_data_cb: %s\n", (const char *)data);
  884.       }
  885.     } else if(inpub_id == 1) {
  886.       /* Call an 'A' function... */
  887.  
  888.           xprintf("Call an A function ...\n");
  889.  
  890.     } else if(inpub_id == 2){
  891.                 print_data((const char *)data, 1,len);
  892.         }
  893.         else {
  894.       //printf("mqtt_incoming_data_cb: Ignoring payload...\n");
  895.           printf("mqtt_incoming_data_cb: %s\n", (const char *)data);
  896.           print_data((const char *)data,2,len);
  897.     }
  898.   } else {
  899.     /* Handle fragmented payload, store in buffer, write to file or whatever */
  900.   }
  901.   //data = NULL;
  902. }
  903.  
  904. //-----------------------------------------------------------------
  905. //4. Using outgoing publish
  906.  static void mqtt_pub_request_cb(void *arg, err_t result)
  907. {
  908.   if(result != ERR_OK) {
  909.     printf("Publish result: %d\n", result);
  910.   }
  911. }
  912.  
  913. void example_publish(mqtt_client_t *client, void *arg)
  914. {
  915.   const char *pub_payload= "give_temperature";
  916.   err_t err;
  917.   u8_t qos = 2; /* 0 1 or 2, see MQTT specification */
  918.   u8_t retain = 0; /* No don't retain such crappy payload... */
  919.   err = mqtt_publish(client, "esp/temperature", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
  920.   if(err != ERR_OK) {
  921.     printf("Publish err: %d\n", err);
  922.   }
  923. }
  924.  
  925. void create_menu(){
  926.  
  927.  
  928. }
  929. void StartDefaultTask(void const * argument)
  930. {
  931.   MX_LWIP_Init();
  932.  
  933.   /*MQTT */
  934.  
  935.   int tmp = 1;
  936.   mqtt_client_t static_client;
  937.   while(tmp == 1){
  938.         xprintf("Trying to connect\n");
  939.         vTaskDelay(1000);
  940.         tmp = example_do_connect(&static_client);
  941.         vTaskDelay(1000);
  942.   //xprintf("tutaj koniecx");
  943.   }
  944.  
  945.   for(;;)
  946.   {
  947.  
  948.         BSP_TS_GetState(&TS_State);
  949.         if(TS_State.touchDetected)
  950.         {
  951.                 BSP_LCD_Clear(LCD_COLOR_WHITE);
  952.                 BSP_LCD_SelectLayer(0);
  953.                 BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
  954.                 BSP_LCD_FillRect(0,0,LCD_X_SIZE,LCD_Y_SIZE);
  955.  
  956.                 //select Foreground Layer
  957.                 BSP_LCD_SelectLayer(1);
  958.                 char greeting[] = "Hello";
  959.                 draw_button(50,50,100,100,LCD_COLOR_MAGENTA,&greeting);
  960.  
  961.         }
  962.  
  963.         char key = inkey();
  964.         if(key == 'p'){
  965.                 xprintf("Send message \n");
  966.                 example_publish(&static_client,2);
  967.         }
  968.  
  969.  
  970.         vTaskDelay(2);
  971.  
  972.   }
  973.   /* USER CODE END 5 */
  974. }
  975.  
  976. /**
  977.   * @brief  Period elapsed callback in non blocking mode
  978.   * @note   This function is called  when TIM6 interrupt took place, inside
  979.   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  980.   * a global variable "uwTick" used as application time base.
  981.   * @param  htim : TIM handle
  982.   * @retval None
  983.   */
  984. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  985. {
  986.   /* USER CODE BEGIN Callback 0 */
  987.  
  988.   /* USER CODE END Callback 0 */
  989.   if (htim->Instance == TIM6) {
  990.     HAL_IncTick();
  991.   }
  992.   /* USER CODE BEGIN Callback 1 */
  993.  
  994.   /* USER CODE END Callback 1 */
  995. }
  996.  
  997. /**
  998.   * @brief  This function is executed in case of error occurrence.
  999.   * @param  file: The file name as string.
  1000.   * @param  line: The line in file as a number.
  1001.   * @retval None
  1002.   */
  1003. void _Error_Handler(char *file, int line)
  1004. {
  1005.   /* USER CODE BEGIN Error_Handler_Debug */
  1006.   /* User can add his own implementation to report the HAL error return state */
  1007.   while(1)
  1008.   {
  1009.   }
  1010.   /* USER CODE END Error_Handler_Debug */
  1011. }
  1012.  
  1013. #ifdef  USE_FULL_ASSERT
  1014. /**
  1015.   * @brief  Reports the name of the source file and the source line number
  1016.   *         where the assert_param error has occurred.
  1017.   * @param  file: pointer to the source file name
  1018.   * @param  line: assert_param error line source number
  1019.   * @retval None
  1020.   */
  1021. void assert_failed(uint8_t* file, uint32_t line)
  1022. {
  1023.   /* USER CODE BEGIN 6 */
  1024.   /* User can add his own implementation to report the file name and line number,
  1025.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1026.   /* USER CODE END 6 */
  1027. }
  1028. #endif /* USE_FULL_ASSERT */
  1029.  
  1030. /**
  1031.   * @}
  1032.   */
  1033.  
  1034. /**
  1035.   * @}
  1036.   */
  1037.  
  1038. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  1039.