Ignore:
Timestamp:
Dec 3, 2024, 8:26:26 AM (6 weeks ago)
Author:
Zed
Message:

Added SPI module for Display.

Location:
ctrl/firmware/Main/CubeMX/Core/Src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ctrl/firmware/Main/CubeMX/Core/Src/main.c

    r39 r45  
    4444RTC_HandleTypeDef hrtc;
    4545
     46SPI_HandleTypeDef hspi4;
     47DMA_HandleTypeDef hdma_spi4_tx;
     48
    4649/* USER CODE BEGIN PV */
    4750
     
    5255static void MPU_Config(void);
    5356static void MX_GPIO_Init(void);
     57static void MX_DMA_Init(void);
    5458static void MX_RTC_Init(void);
     59static void MX_SPI4_Init(void);
    5560/* USER CODE BEGIN PFP */
    5661
     
    102107  /* Initialize all configured peripherals */
    103108  MX_GPIO_Init();
     109  MX_DMA_Init();
    104110  MX_RTC_Init();
     111  MX_SPI4_Init();
    105112  /* USER CODE BEGIN 2 */
    106113
     
    150157  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    151158  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    152   RCC_OscInitStruct.PLL.PLLM = 25;
    153   RCC_OscInitStruct.PLL.PLLN = 200;
     159  RCC_OscInitStruct.PLL.PLLM = 5;
     160  RCC_OscInitStruct.PLL.PLLN = 40;
    154161  RCC_OscInitStruct.PLL.PLLP = 2;
    155162  RCC_OscInitStruct.PLL.PLLQ = 2;
    156163  RCC_OscInitStruct.PLL.PLLR = 2;
    157   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_0;
    158   RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOMEDIUM;
     164  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
     165  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
    159166  RCC_OscInitStruct.PLL.PLLFRACN = 0;
    160167  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
     
    219226
    220227/**
     228  * @brief SPI4 Initialization Function
     229  * @param None
     230  * @retval None
     231  */
     232static void MX_SPI4_Init(void)
     233{
     234
     235  /* USER CODE BEGIN SPI4_Init 0 */
     236
     237  /* USER CODE END SPI4_Init 0 */
     238
     239  /* USER CODE BEGIN SPI4_Init 1 */
     240
     241  /* USER CODE END SPI4_Init 1 */
     242  /* SPI4 parameter configuration*/
     243  hspi4.Instance = SPI4;
     244  hspi4.Init.Mode = SPI_MODE_MASTER;
     245  hspi4.Init.Direction = SPI_DIRECTION_1LINE;
     246  hspi4.Init.DataSize = SPI_DATASIZE_9BIT;
     247  hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;
     248  hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;
     249  hspi4.Init.NSS = SPI_NSS_HARD_OUTPUT;
     250  hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
     251  hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
     252  hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
     253  hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
     254  hspi4.Init.CRCPolynomial = 0x0;
     255  hspi4.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
     256  hspi4.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
     257  hspi4.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
     258  hspi4.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
     259  hspi4.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
     260  hspi4.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
     261  hspi4.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
     262  hspi4.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
     263  hspi4.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
     264  hspi4.Init.IOSwap = SPI_IO_SWAP_DISABLE;
     265  if (HAL_SPI_Init(&hspi4) != HAL_OK)
     266  {
     267    Error_Handler();
     268  }
     269  /* USER CODE BEGIN SPI4_Init 2 */
     270
     271  /* USER CODE END SPI4_Init 2 */
     272
     273}
     274
     275/**
     276  * Enable DMA controller clock
     277  */
     278static void MX_DMA_Init(void)
     279{
     280
     281  /* DMA controller clock enable */
     282  __HAL_RCC_DMA1_CLK_ENABLE();
     283
     284  /* DMA interrupt init */
     285  /* DMA1_Stream0_IRQn interrupt configuration */
     286  HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
     287  HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
     288
     289}
     290
     291/**
    221292  * @brief GPIO Initialization Function
    222293  * @param None
     
    231302  __HAL_RCC_GPIOC_CLK_ENABLE();
    232303  __HAL_RCC_GPIOH_CLK_ENABLE();
     304  __HAL_RCC_GPIOE_CLK_ENABLE();
    233305
    234306/* USER CODE BEGIN MX_GPIO_Init_2 */
  • ctrl/firmware/Main/CubeMX/Core/Src/stm32h7xx_hal_msp.c

    r39 r45  
    2424
    2525/* USER CODE END Includes */
     26extern DMA_HandleTypeDef hdma_spi4_tx;
    2627
    2728/* Private typedef -----------------------------------------------------------*/
     
    133134}
    134135
     136/**
     137* @brief SPI MSP Initialization
     138* This function configures the hardware resources used in this example
     139* @param hspi: SPI handle pointer
     140* @retval None
     141*/
     142void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
     143{
     144  GPIO_InitTypeDef GPIO_InitStruct = {0};
     145  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
     146  if(hspi->Instance==SPI4)
     147  {
     148  /* USER CODE BEGIN SPI4_MspInit 0 */
     149
     150  /* USER CODE END SPI4_MspInit 0 */
     151
     152  /** Initializes the peripherals clock
     153  */
     154    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI4;
     155    PeriphClkInitStruct.PLL2.PLL2M = 3;
     156    PeriphClkInitStruct.PLL2.PLL2N = 40;
     157    PeriphClkInitStruct.PLL2.PLL2P = 2;
     158    PeriphClkInitStruct.PLL2.PLL2Q = 11;
     159    PeriphClkInitStruct.PLL2.PLL2R = 2;
     160    PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3;
     161    PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE;
     162    PeriphClkInitStruct.PLL2.PLL2FRACN = 0.0;
     163    PeriphClkInitStruct.Spi45ClockSelection = RCC_SPI45CLKSOURCE_PLL2;
     164    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
     165    {
     166      Error_Handler();
     167    }
     168
     169    /* Peripheral clock enable */
     170    __HAL_RCC_SPI4_CLK_ENABLE();
     171
     172    __HAL_RCC_GPIOE_CLK_ENABLE();
     173    /**SPI4 GPIO Configuration
     174    PE11     ------> SPI4_NSS
     175    PE12     ------> SPI4_SCK
     176    PE14     ------> SPI4_MOSI
     177    */
     178    GPIO_InitStruct.Pin = SPI4_NSS_DISPLAY_Pin|SPI4_SCK_DISPLAY_Pin|SPI4_MOSI_DISPLAY_Pin;
     179    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     180    GPIO_InitStruct.Pull = GPIO_NOPULL;
     181    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     182    GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
     183    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
     184
     185    /* SPI4 DMA Init */
     186    /* SPI4_TX Init */
     187    hdma_spi4_tx.Instance = DMA1_Stream0;
     188    hdma_spi4_tx.Init.Request = DMA_REQUEST_SPI4_TX;
     189    hdma_spi4_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
     190    hdma_spi4_tx.Init.PeriphInc = DMA_PINC_DISABLE;
     191    hdma_spi4_tx.Init.MemInc = DMA_MINC_ENABLE;
     192    hdma_spi4_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
     193    hdma_spi4_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
     194    hdma_spi4_tx.Init.Mode = DMA_NORMAL;
     195    hdma_spi4_tx.Init.Priority = DMA_PRIORITY_LOW;
     196    hdma_spi4_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
     197    if (HAL_DMA_Init(&hdma_spi4_tx) != HAL_OK)
     198    {
     199      Error_Handler();
     200    }
     201
     202    __HAL_LINKDMA(hspi,hdmatx,hdma_spi4_tx);
     203
     204    /* SPI4 interrupt Init */
     205    HAL_NVIC_SetPriority(SPI4_IRQn, 0, 0);
     206    HAL_NVIC_EnableIRQ(SPI4_IRQn);
     207  /* USER CODE BEGIN SPI4_MspInit 1 */
     208
     209  /* USER CODE END SPI4_MspInit 1 */
     210
     211  }
     212
     213}
     214
     215/**
     216* @brief SPI MSP De-Initialization
     217* This function freeze the hardware resources used in this example
     218* @param hspi: SPI handle pointer
     219* @retval None
     220*/
     221void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
     222{
     223  if(hspi->Instance==SPI4)
     224  {
     225  /* USER CODE BEGIN SPI4_MspDeInit 0 */
     226
     227  /* USER CODE END SPI4_MspDeInit 0 */
     228    /* Peripheral clock disable */
     229    __HAL_RCC_SPI4_CLK_DISABLE();
     230
     231    /**SPI4 GPIO Configuration
     232    PE11     ------> SPI4_NSS
     233    PE12     ------> SPI4_SCK
     234    PE14     ------> SPI4_MOSI
     235    */
     236    HAL_GPIO_DeInit(GPIOE, SPI4_NSS_DISPLAY_Pin|SPI4_SCK_DISPLAY_Pin|SPI4_MOSI_DISPLAY_Pin);
     237
     238    /* SPI4 DMA DeInit */
     239    HAL_DMA_DeInit(hspi->hdmatx);
     240
     241    /* SPI4 interrupt DeInit */
     242    HAL_NVIC_DisableIRQ(SPI4_IRQn);
     243  /* USER CODE BEGIN SPI4_MspDeInit 1 */
     244
     245  /* USER CODE END SPI4_MspDeInit 1 */
     246  }
     247
     248}
     249
    135250/* USER CODE BEGIN 1 */
    136251
  • ctrl/firmware/Main/CubeMX/Core/Src/stm32h7xx_it.c

    r13 r45  
    5656
    5757/* External variables --------------------------------------------------------*/
    58 
     58extern DMA_HandleTypeDef hdma_spi4_tx;
     59extern SPI_HandleTypeDef hspi4;
    5960/* USER CODE BEGIN EV */
    6061
     
    199200/******************************************************************************/
    200201
     202/**
     203  * @brief This function handles DMA1 stream0 global interrupt.
     204  */
     205void DMA1_Stream0_IRQHandler(void)
     206{
     207  /* USER CODE BEGIN DMA1_Stream0_IRQn 0 */
     208
     209  /* USER CODE END DMA1_Stream0_IRQn 0 */
     210  HAL_DMA_IRQHandler(&hdma_spi4_tx);
     211  /* USER CODE BEGIN DMA1_Stream0_IRQn 1 */
     212
     213  /* USER CODE END DMA1_Stream0_IRQn 1 */
     214}
     215
     216/**
     217  * @brief This function handles SPI4 global interrupt.
     218  */
     219void SPI4_IRQHandler(void)
     220{
     221  /* USER CODE BEGIN SPI4_IRQn 0 */
     222
     223  /* USER CODE END SPI4_IRQn 0 */
     224  HAL_SPI_IRQHandler(&hspi4);
     225  /* USER CODE BEGIN SPI4_IRQn 1 */
     226
     227  /* USER CODE END SPI4_IRQn 1 */
     228}
     229
    201230/* USER CODE BEGIN 1 */
    202231
Note: See TracChangeset for help on using the changeset viewer.