text: /* USER CODE BEGIN Header */
  /**
    ******************************************************************************
    * @file           : main.c
    * @brief          : Main program body
    ******************************************************************************
    * @attention
    *
    * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
    * All rights reserved.</center></h2>
    *
    * This software component is licensed by ST under BSD 3-Clause license,
    * the "License"; You may not use this file except in compliance with the
    * License. You may obtain a copy of the License at:
    *                        opensource.org/licenses/BSD-3-Clause
    *
    ******************************************************************************
    */
  /* USER CODE END Header */
  
  /* Includes ------------------------------------------------------------------*/
  #include "main.h"
  
  /* Private includes ----------------------------------------------------------*/
  /* USER CODE BEGIN Includes */
  #include "ssd1306_tests.h"
  #include "stdio.h"
  #include "menu.h"
  #include "w25qxx.h"
  /* USER CODE END Includes */
  
  /* Private typedef -----------------------------------------------------------*/
  /* USER CODE BEGIN PTD */
  
  /* USER CODE END PTD */
  
  /* Private define ------------------------------------------------------------*/
  /* USER CODE BEGIN PD */
  #define RXBUFFERSIZE 10
  /* USER CODE END PD */
  
  /* Private macro -------------------------------------------------------------*/
  /* USER CODE BEGIN PM */
  
  /* USER CODE END PM */
  
  /* Private variables ---------------------------------------------------------*/
  I2C_HandleTypeDef hi2c2;
  
  RTC_HandleTypeDef hrtc;
  
  SPI_HandleTypeDef hspi2;
  
  UART_HandleTypeDef huart1;
  
  /* USER CODE BEGIN PV */
  extern int menuType;
  RTC_DateTypeDef GetData;
  
  RTC_TimeTypeDef GetTime;
  int beep = 0;
  volatile int needRead = 0;
  uint16_t pageAddr = 10;
  volatile int isOperation = 0;
  
  volatile uint8_t aRxBuffer;
  uint8_t buffer[256] = {0};
  volatile uint8_t bufferCount = 0;
  volatile uint8_t startCount = 0;
  volatile uint8_t stopCount = 0;
  volatile uint32_t pageIndex = 20;
  volatile uint32_t startPage = 20;
  volatile uint32_t endPage = 20;
  volatile uint32_t currentPage = 0;
  volatile int inTransfer = 0;
  volatile int stop = 0;
  volatile int xxxx = 0;
  uint8_t totalPage = 0;
  /* USER CODE END PV */
  
  /* Private function prototypes -----------------------------------------------*/
  void SystemClock_Config(void);
  static void MX_GPIO_Init(void);
  static void MX_I2C2_Init(void);
  static void MX_USART1_UART_Init(void);
  static void MX_RTC_Init(void);
  static void MX_SPI2_Init(void);
  /* USER CODE BEGIN PFP */
  
  /* USER CODE END PFP */
  
  /* Private user code ---------------------------------------------------------*/
  /* USER CODE BEGIN 0 */
  int fputc(int ch, FILE *f)
  {
    uint8_t temp[1] = {ch};
    HAL_UART_Transmit(&huart1, temp, 1, 2);
    return ch;
  }
  void write(){
  	isOperation = 1;
  	 W25qxx_ErasePage(pageIndex);
     W25qxx_WritePage(buffer, pageIndex, 10, 0);
  	 pageIndex++;
  	 W25qxx_ErasePage(5);
  	 W25qxx_WritePage((uint8_t*)&pageIndex, 5, 10, 1);
  	 isOperation = 0;
  };
  void read(){
  		W25qxx_ReadPage(buffer,10,10,0);
  }
  /* 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();
    MX_I2C2_Init();
    MX_USART1_UART_Init();
    MX_RTC_Init();
    MX_SPI2_Init();
    /* USER CODE BEGIN 2 */
  	printf("xxx");
    uint8_t ms[] = {"Hello World!"};
  	W25qxx_ErasePage(0);
  	W25qxx_WritePage(ms,10,10,0);
  	W25qxx_ReadPage(buffer,10,10,0);
  	
    /* USER CODE END 2 */
  
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
  
  		if(bufferCount==245)
  			{
  			if(stop){
  				bufferCount = 0;
  			}
  			printf(buffer);
  			write();
  			HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
  			}else{
  				HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
  			}
      // HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_0);
      // HAL_Delay(500);
      /* 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};
    RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  
    /** Initializes the CPU, AHB and APB busses clocks 
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;
    RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
      Error_Handler();
    }
    /** Initializes the CPU, AHB and APB busses clocks 
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  
    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    {
      Error_Handler();
    }
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
    PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }
  }
  
  /**
    * @brief I2C2 Initialization Function
    * @param None
    * @retval None
    */
  static void MX_I2C2_Init(void)
  {
  
    /* USER CODE BEGIN I2C2_Init 0 */
  
    /* USER CODE END I2C2_Init 0 */
  
    /* USER CODE BEGIN I2C2_Init 1 */
  
    /* USER CODE END I2C2_Init 1 */
    hi2c2.Instance = I2C2;
    hi2c2.Init.ClockSpeed = 100000;
    hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c2.Init.OwnAddress1 = 0;
    hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c2.Init.OwnAddress2 = 0;
    hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    if (HAL_I2C_Init(&hi2c2) != HAL_OK)
    {
      Error_Handler();
    }
    /* USER CODE BEGIN I2C2_Init 2 */
    ssd1306_Init();
    fillBlack();
    init();
    showMenu();
    /* USER CODE END I2C2_Init 2 */
  }
  
  /**
    * @brief RTC Initialization Function
    * @param None
    * @retval None
    */
  static void MX_RTC_Init(void)
  {
  
    /* USER CODE BEGIN RTC_Init 0 */
  
    /* USER CODE END RTC_Init 0 */
  
    RTC_TimeTypeDef sTime = {0};
    RTC_DateTypeDef DateToUpdate = {0};
  
    /* USER CODE BEGIN RTC_Init 1 */
  
    /* USER CODE END RTC_Init 1 */
    /** Initialize RTC Only 
    */
    hrtc.Instance = RTC;
    hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
    hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
    if (HAL_RTC_Init(&hrtc) != HAL_OK)
    {
      Error_Handler();
    }
  
    /* USER CODE BEGIN Check_RTC_BKUP */
  
    /* USER CODE END Check_RTC_BKUP */
  
    /** Initialize RTC and set the Time and Date 
    */
    sTime.Hours = 0x0;
    sTime.Minutes = 0x30;
    sTime.Seconds = 0x0;
  
    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
    {
      Error_Handler();
    }
    DateToUpdate.WeekDay = RTC_WEEKDAY_THURSDAY;
    DateToUpdate.Month = RTC_MONTH_AUGUST;
    DateToUpdate.Date = 0x19;
    DateToUpdate.Year = 0x20;
  
    if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
    {
      Error_Handler();
    }
    /* USER CODE BEGIN RTC_Init 2 */
    HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);
    printf("%02d/%02d/%02d\r\n", 2000 + GetData.Year, GetData.Month, GetData.Date);
    printf("%02d:%02d:%02d\r\n", GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
    printf("\r\n");
    char str[20];
    sprintf(str, "%02d/%02d/%02d  %02d:%02d:%02d  ", GetData.Year, GetData.Month, GetData.Date, GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
    updateDateTime(str);
    HAL_RTCEx_SetSecond_IT(&hrtc);
    RTC_AlarmTypeDef test;
    /* USER CODE END RTC_Init 2 */
  }
  
  /**
    * @brief SPI2 Initialization Function
    * @param None
    * @retval None
    */
  static void MX_SPI2_Init(void)
  {
  
    /* USER CODE BEGIN SPI2_Init 0 */
  
    /* USER CODE END SPI2_Init 0 */
  
    /* USER CODE BEGIN SPI2_Init 1 */
  
    /* USER CODE END SPI2_Init 1 */
    /* SPI2 parameter configuration*/
    hspi2.Instance = SPI2;
    hspi2.Init.Mode = SPI_MODE_MASTER;
    hspi2.Init.Direction = SPI_DIRECTION_2LINES;
    hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
    hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
    hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
    hspi2.Init.NSS = SPI_NSS_SOFT;
    hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
    hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
    hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLE;
    hspi2.Init.CRCPolynomial = 10;
    if (HAL_SPI_Init(&hspi2) != HAL_OK)
    {
      Error_Handler();
    }
    /* USER CODE BEGIN SPI2_Init 2 */
    W25qxx_Init();
    /* USER CODE END SPI2_Init 2 */
  }
  
  /**
    * @brief USART1 Initialization Function
    * @param None
    * @retval None
    */
  static void MX_USART1_UART_Init(void)
  {
  
    /* USER CODE BEGIN USART1_Init 0 */
  
    /* USER CODE END USART1_Init 0 */
  
    /* USER CODE BEGIN USART1_Init 1 */
  
    /* USER CODE END USART1_Init 1 */
    huart1.Instance = USART1;
    huart1.Init.BaudRate = 115200;
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
    huart1.Init.StopBits = UART_STOPBITS_1;
    huart1.Init.Parity = UART_PARITY_NONE;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart1) != HAL_OK)
    {
      Error_Handler();
    }
    /* USER CODE BEGIN USART1_Init 2 */
    HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE);
    /* USER CODE END USART1_Init 2 */
  }
  
  /**
    * @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_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(GPIOB, GPIO_PIN_12 | GPIO_PIN_8, GPIO_PIN_SET);
  
    /*Configure GPIO pin : PA0 */
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  
    /*Configure GPIO pin : PB12 */
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  
    /*Configure GPIO pins : PC8 PC9 */
    GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  
    /*Configure GPIO pin : PD2 */
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  
    /*Configure GPIO pin : PB8 */
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  
    /* EXTI interrupt init*/
    HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 2);
    HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  
    HAL_NVIC_SetPriority(EXTI2_IRQn, 2, 1);
    HAL_NVIC_EnableIRQ(EXTI2_IRQn);
  
    HAL_NVIC_SetPriority(EXTI9_5_IRQn, 2, 0);
    HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
  }
  
  /* USER CODE BEGIN 4 */
  void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  {
   if (inTransfer)
    {
      if(bufferCount>=246){
  			bufferCount=0;
  		}else{
  			buffer[bufferCount]=aRxBuffer;
  			bufferCount++;
  		}
    }
  
  
  	if(stopCount ==3){
  		stopCount = 0;
  
  		for (int i = bufferCount-4; i < 246; i++)
      {
        buffer[i] = 0x00;
  			bufferCount++;
      }
  		inTransfer=0;
  		bufferCount = 245;
  		stop = 1;
  	}
    if (aRxBuffer == 'x')
    {
      stopCount++;
    }else{
  		stopCount=0;
  	}
  
  }
  void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
  {
    HAL_RTC_GetTime(hrtc, &GetTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(hrtc, &GetData, RTC_FORMAT_BIN);
    // printf("%02d/%02d/%02d\r\n", 2000 + GetData.Year, GetData.Month, GetData.Date);
    // printf("%02d:%02d:%02d\r\n", GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
    // printf("\r\n");
    char str[20];
    sprintf(str, "%02d/%02d/%02d  %02d:%02d:%02d  ", GetData.Year, GetData.Month, GetData.Date, GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
    updateDateTime(str);
  }
  void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  {
  
    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
    beep = 1;
    printf("\r\nalarm\r\n");
  }
  void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  {
    if (beep)
    {
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
      beep = 0;
      return;
    }
    HAL_Delay(200);
    uint8_t *mes;
    extern int inHandler;
    switch (GPIO_Pin)
    {
    case GPIO_PIN_0:
      mes = "up";
      if (inHandler > 0)
      {
        handleCLick(3);
      }
      else
        up();
      break;
    case GPIO_PIN_8:
      mes = "left";
      if (inHandler > 0)
      {
        handleCLick(2);
      }
      else
        back();
      break;
    case GPIO_PIN_9:
      mes = "down";
      if (inHandler > 0)
      {
        handleCLick(4);
      }
      else
        down();
      break;
    case GPIO_PIN_2:
      mes = "right";
      if (inHandler > 0)
      {
        handleCLick(1);
      }
      else
        go();
      break;
    default:
      break;
    }
    HAL_UART_Transmit_IT(&huart1, mes, sizeof(mes));
  }
  
  /* USER CODE END 4 */
  
  /**
    * @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****/
  