/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>

#include "lvgl.h"

#include "lcdio.h"
#include "ILI9341.h"
#include "ADS7843.h"

#include "home_screen.h"
#include "PSE_unit.h"
#include "pse_stepper_planer.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define BUFF_SIZE (320 * 5)
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
SD_HandleTypeDef hsd;

TIM_HandleTypeDef htim4;

UART_HandleTypeDef huart1;

DMA_HandleTypeDef hdma_memtomem_dma2_channel1;
SRAM_HandleTypeDef hsram1;

/* USER CODE BEGIN PV */
void XferCpltCallback(DMA_HandleTypeDef *hdma);
static lv_disp_draw_buf_t disp_buf;

static lv_color_t buf_1[BUFF_SIZE];
static lv_color_t buf_2[BUFF_SIZE];

static int32_t drawY, drawXmin, drawYmax, drawLineLen;
static lv_color_t* drawBuf;
static lv_disp_drv_t* drawDisp;

ADS7843_Def Touch_Def = {
  .CS_GPIO_Port = ADS7843_CS_GPIO_Port,
  .CS_GPIO_Pin = ADS7843_CS_Pin,
  .SCK_GPIO_Port = ADS7843_SCK_GPIO_Port,
  .SCK_GPIO_Pin = ADS7843_SCK_Pin,
  .MOSI_GPIO_Port = ADS7843_MOSI_GPIO_Port,
  .MOSI_GPIO_Pin = ADS7843_MOSI_Pin,
  .MISO_GPIO_Port = ADS7843_MISO_GPIO_Port,
  .MISO_GPIO_Pin = ADS7843_MISO_Pin,
  .Int_GPIO_Port = ADS7843_Int_GPIO_Port,
  .Int_GPIO_Pin = ADS7843_Int_Pin,
};

pse_unit pse_units[PSE_UNITS_NUM];
pse_syringe pse_syringes[PSE_UNITS_NUM];

enum steppers_axis{PSE_X_STEPPER, PSE_Y_STEPPER, PSE_Z_STEPPER, PSE_E_STEPPER, PSE_STEPPER_NUM};
pse_stepper_conf pse_stepper_confs[PSE_STEPPER_NUM] = {
  [PSE_X_STEPPER] = {
    .DIR_GPIO_Port = X_STEPPER_DIR_GPIO_Port,
	.DIR_GPIO_Pin = X_STEPPER_DIR_Pin,
	.EN_GPIO_Port = X_STEPPER_EN_GPIO_Port,
	.EN_GPIO_Pin = X_STEPPER_EN_Pin,
	.STEP_GPIO_Port = X_STEPPER_STEP_GPIO_Port,
	.STEP_GPIO_Pin = X_STEPPER_STEP_Pin,
	.step_max = 69,
  },
  [PSE_Y_STEPPER] = {
    .DIR_GPIO_Port = Y_STEPPER_DIR_GPIO_Port,
	.DIR_GPIO_Pin = Y_STEPPER_DIR_Pin,
	.EN_GPIO_Port = Y_STEPPER_EN_GPIO_Port,
	.EN_GPIO_Pin = Y_STEPPER_EN_Pin,
	.STEP_GPIO_Port = Y_STEPPER_STEP_GPIO_Port,
	.STEP_GPIO_Pin = Y_STEPPER_STEP_Pin,
  },
  [PSE_Z_STEPPER] = {
    .DIR_GPIO_Port = Z_STEPPER_DIR_GPIO_Port,
	.DIR_GPIO_Pin = Z_STEPPER_DIR_Pin,
	.EN_GPIO_Port = Z_STEPPER_EN_GPIO_Port,
	.EN_GPIO_Pin = Z_STEPPER_EN_Pin,
	.STEP_GPIO_Port = Z_STEPPER_STEP_GPIO_Port,
	.STEP_GPIO_Pin = Z_STEPPER_STEP_Pin,
  },
  [PSE_E_STEPPER] = {
    .DIR_GPIO_Port = E_STEPPER_DIR_GPIO_Port,
	.DIR_GPIO_Pin = E_STEPPER_DIR_Pin,
	.EN_GPIO_Port = E_STEPPER_EN_GPIO_Port,
	.EN_GPIO_Pin = E_STEPPER_EN_Pin,
	.STEP_GPIO_Port = E_STEPPER_STEP_GPIO_Port,
	.STEP_GPIO_Pin = E_STEPPER_STEP_Pin,
  },
};
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_FSMC_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM4_Init(void);
static void MX_SDIO_SD_Init(void);
/* USER CODE BEGIN PFP */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

// LVGL screen update function
void my_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * buf){
	drawY = area->y1;
	drawXmin = area->x1;
	drawYmax = area->y2;
  	ILI9341_SetCursor(area->x1,drawY);
  	ILI9341_WriteRam();
  	drawLineLen = (area->x2 - area->x1)+1;
  	drawY++;
  	drawBuf = buf;
  	drawDisp = disp;
  	hdma_memtomem_dma2_channel1.XferCpltCallback=&XferCpltCallback;
  	HAL_DMA_Start_IT(&hdma_memtomem_dma2_channel1, (uint32_t)buf, (uint32_t)LCD_IO_getDataPt(), drawLineLen);
}

// LVGL input update function
void touchscreen_read_callback(lv_indev_drv_t * drv, lv_indev_data_t*data){
  if(ADS7843_Touched(&Touch_Def)) {
    data->state = LV_INDEV_STATE_PRESSED;
    data->point.x = ADS7843_GetX(&Touch_Def);
    data->point.y = ADS7843_GetY(&Touch_Def);
  } else {
    data->state = LV_INDEV_STATE_RELEASED;
    data->point.x = -1;
    data->point.y = -1;
  }
}
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 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_DMA_Init();
  MX_FSMC_Init();
  MX_USART1_UART_Init();
  MX_TIM4_Init();
  MX_FATFS_Init();
  MX_SDIO_SD_Init();
  /* USER CODE BEGIN 2 */
  // Reset the LCD to start clean
  HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_RESET);
  HAL_Delay(5);
  HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_SET);
  HAL_Delay(5);

  // Initialise the LCD driver
  ILI9341_Init();

  // Initialise LVGL UI library
  lv_init();

  // Add the display buffer to LVGL
  lv_disp_draw_buf_init(&disp_buf, buf_1, buf_2, BUFF_SIZE);

  // initilize LVGL display
  lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  disp_drv.draw_buf = &disp_buf;
  disp_drv.hor_res = LCD_WIDTH;
  disp_drv.ver_res = LCD_HEIGHT;
  disp_drv.flush_cb = &my_flush_cb;
  lv_disp_drv_register(&disp_drv);

  lv_disp_drv_register(&disp_drv);

  // load the default theme
  lv_theme_t * th = lv_theme_default_init(lv_disp_get_default(), lv_color_hex(0x2BAEE1), lv_color_hex(0x001361), true, LV_FONT_DEFAULT);
  lv_disp_set_theme(lv_disp_get_default(), th);

  // initialize LVGL touchscreen input
  lv_indev_drv_t indev_drv;
  lv_indev_drv_init(&indev_drv);
  indev_drv.type = LV_INDEV_TYPE_POINTER;
  indev_drv.read_cb = touchscreen_read_callback;
  lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv);

  // import the MINT logo as a picture
  LV_IMG_DECLARE(logo_mint_resize);
  lv_obj_t * logo_mint = lv_img_create(lv_scr_act());
  lv_img_set_src(logo_mint, &logo_mint_resize);

  // Draw the MINT logo
  lv_obj_align(logo_mint, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_size(logo_mint, 320, 240);

  // Create the cursor
  lv_obj_t* cursor = lv_label_create(lv_scr_act());
  lv_label_set_text(cursor, "A");
  lv_indev_set_cursor(my_indev, cursor);

  // initialize the Touchscreen Driver
  ADS7843_Init(&Touch_Def);

  // mount the SD card
  char SD_disk_path[4];
  FATFS FatFs;
  f_mount(&FatFs, SD_disk_path, 0);

  // Load saved units
  load_units(pse_units, pse_syringes, pse_stepper_confs, PSE_UNITS_NUM);

  // Set timer used for step interrupts
  pse_sp_set_timer(&htim4);

  // Load the home screen
  Home_Screen_Gen(pse_units, PSE_UNITS_NUM);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	// Run LVGL update loop
	lv_timer_handler();
	HAL_Delay(5);
    /* 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};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief SDIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_SDIO_SD_Init(void)
{

  /* USER CODE BEGIN SDIO_Init 0 */

  /* USER CODE END SDIO_Init 0 */

  /* USER CODE BEGIN SDIO_Init 1 */

  /* USER CODE END SDIO_Init 1 */
  hsd.Instance = SDIO;
  hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 16;
  /* USER CODE BEGIN SDIO_Init 2 */

  /* USER CODE END SDIO_Init 2 */

}

/**
  * @brief TIM4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM4_Init(void)
{

  /* USER CODE BEGIN TIM4_Init 0 */

  /* USER CODE END TIM4_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM4_Init 1 */

  /* USER CODE END TIM4_Init 1 */
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 64;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 50;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM4_Init 2 */

  /* USER CODE END TIM4_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 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * Enable DMA controller clock
  * Configure DMA for memory to memory transfers
  *   hdma_memtomem_dma2_channel1
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA2_CLK_ENABLE();

  /* Configure DMA request hdma_memtomem_dma2_channel1 on DMA2_Channel1 */
  hdma_memtomem_dma2_channel1.Instance = DMA2_Channel1;
  hdma_memtomem_dma2_channel1.Init.Direction = DMA_MEMORY_TO_MEMORY;
  hdma_memtomem_dma2_channel1.Init.PeriphInc = DMA_PINC_ENABLE;
  hdma_memtomem_dma2_channel1.Init.MemInc = DMA_MINC_DISABLE;
  hdma_memtomem_dma2_channel1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  hdma_memtomem_dma2_channel1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  hdma_memtomem_dma2_channel1.Init.Mode = DMA_NORMAL;
  hdma_memtomem_dma2_channel1.Init.Priority = DMA_PRIORITY_LOW;
  if (HAL_DMA_Init(&hdma_memtomem_dma2_channel1) != HAL_OK)
  {
    Error_Handler( );
  }

  /* DMA interrupt init */
  /* DMA2_Channel1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA2_Channel1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA2_Channel1_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOE, E_STEPPER_DIR_Pin|E_STEPPER_STEP_Pin|Z_STEPPER_STEP_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOE, E_STEPPER_EN_Pin|Z_STEPPER_EN_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, ADS7843_CS_Pin|X_STEPPER_DIR_Pin|X_STEPPER_EN_Pin|Y_STEPPER_EN_Pin
                          |Z_STEPPER_DIR_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, ADS7843_SCK_Pin|ADS7843_MOSI_Pin|X_STEPPER_STEP_Pin|Y_STEPPER_DIR_Pin
                          |Y_STEPPER_STEP_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET);

  /*Configure GPIO pins : E_STEPPER_DIR_Pin E_STEPPER_STEP_Pin E_STEPPER_EN_Pin Z_STEPPER_STEP_Pin
                           Z_STEPPER_EN_Pin */
  GPIO_InitStruct.Pin = E_STEPPER_DIR_Pin|E_STEPPER_STEP_Pin|E_STEPPER_EN_Pin|Z_STEPPER_STEP_Pin
                          |Z_STEPPER_EN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  /*Configure GPIO pins : LED_Pin LCD_RST_Pin */
  GPIO_InitStruct.Pin = LED_Pin|LCD_RST_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pins : ADS7843_CS_Pin ADS7843_SCK_Pin ADS7843_MOSI_Pin */
  GPIO_InitStruct.Pin = ADS7843_CS_Pin|ADS7843_SCK_Pin|ADS7843_MOSI_Pin;
  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);

  /*Configure GPIO pin : ADS7843_MISO_Pin */
  GPIO_InitStruct.Pin = ADS7843_MISO_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADS7843_MISO_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : LCD_CS_Pin */
  GPIO_InitStruct.Pin = LCD_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LCD_CS_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : ADS7843_Int_Pin */
  GPIO_InitStruct.Pin = ADS7843_Int_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(ADS7843_Int_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : X_STEPPER_DIR_Pin X_STEPPER_STEP_Pin X_STEPPER_EN_Pin Y_STEPPER_DIR_Pin
                           Y_STEPPER_STEP_Pin Y_STEPPER_EN_Pin Z_STEPPER_DIR_Pin */
  GPIO_InitStruct.Pin = X_STEPPER_DIR_Pin|X_STEPPER_STEP_Pin|X_STEPPER_EN_Pin|Y_STEPPER_DIR_Pin
                          |Y_STEPPER_STEP_Pin|Y_STEPPER_EN_Pin|Z_STEPPER_DIR_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* FSMC initialization function */
static void MX_FSMC_Init(void)
{

  /* USER CODE BEGIN FSMC_Init 0 */

  /* USER CODE END FSMC_Init 0 */

  FSMC_NORSRAM_TimingTypeDef Timing = {0};

  /* USER CODE BEGIN FSMC_Init 1 */

  /* USER CODE END FSMC_Init 1 */

  /** Perform the SRAM1 memory initialization sequence
  */
  hsram1.Instance = FSMC_NORSRAM_DEVICE;
  hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  /* hsram1.Init */
  hsram1.Init.NSBank = FSMC_NORSRAM_BANK1;
  hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
  hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
  hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
  hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
  hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
  hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
  /* Timing */
  Timing.AddressSetupTime = 15;
  Timing.AddressHoldTime = 15;
  Timing.DataSetupTime = 255;
  Timing.BusTurnAroundDuration = 15;
  Timing.CLKDivision = 16;
  Timing.DataLatency = 17;
  Timing.AccessMode = FSMC_ACCESS_MODE_A;
  /* ExtTiming */

  if (HAL_SRAM_Init(&hsram1, &Timing, NULL) != HAL_OK)
  {
    Error_Handler( );
  }

  /** Disconnect NADV
  */

  __HAL_AFIO_FSMCNADV_DISCONNECTED();

  /* USER CODE BEGIN FSMC_Init 2 */

  /* USER CODE END FSMC_Init 2 */
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim){
  pse_stepper_planer_tick(pse_units, PSE_UNITS_NUM);
}

void XferCpltCallback(DMA_HandleTypeDef *hdma){
	if(drawY > drawYmax){
	    lv_disp_flush_ready(drawDisp);
	    return;
	}

  	ILI9341_SetCursor(drawXmin,drawY);
  	ILI9341_WriteRam();
  	drawY++;
  	drawBuf+=drawLineLen;
  	hdma_memtomem_dma2_channel1.XferCpltCallback=&XferCpltCallback;
  	HAL_DMA_Start_IT(&hdma_memtomem_dma2_channel1, (uint32_t)drawBuf, (uint32_t)LCD_IO_getDataPt(), drawLineLen);
}
/* 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 */
  __disable_irq();
  while (1)
  {
  }
  /* 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,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
