text: //Handles configuration for Raspberry PI (SPI, DMA etc.), 08/02/2021, Chandler Samuel Keep
  #include "rpi_flow.h"
  
  void setupDMA(){
      /* Init with LL driver */
      /* DMA controller clock enable */
      LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
      /* DMA interrupt init */
      /* Enable DMA transfer complete/error interrupts  */
      LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_5);
      LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_4);
      LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_4);
      LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_4);
      /* DMA1_Channel4_5_IRQn interrupt configuration */
      NVIC_SetPriority(DMA1_Channel4_5_IRQn, 1);
      NVIC_EnableIRQ(DMA1_Channel4_5_IRQn);
  }
  
  void setupSPI(){
  
      /* Peripheral clock enable */
      LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
      LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  
      LL_SPI_InitTypeDef SPI_InitStruct = {0};
      LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
      /**SPI2 GPIO Configuration
      PB12   ------> SPI2_NSS
      PB13   ------> SPI2_SCK
      PB14   ------> SPI2_MISO
      PB15   ------> SPI2_MOSI
      */
      GPIO_InitStruct.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13 | LL_GPIO_PIN_14 | LL_GPIO_PIN_15;
      GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
      GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
      GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
      GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
      GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
      LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  
      /* SPI2 DMA Init */
  
      /* SPI2_RX Init */
      LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_4, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
      LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PRIORITY_HIGH);
      LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MODE_NORMAL);
      LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PERIPH_NOINCREMENT);
      LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MEMORY_INCREMENT);
      LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PDATAALIGN_BYTE);
      LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MDATAALIGN_BYTE);
  
      /* SPI2_TX Init */
      LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_5, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
      LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PRIORITY_MEDIUM);
      LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MODE_NORMAL);
      LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PERIPH_NOINCREMENT);
      LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MEMORY_INCREMENT);
      LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_5, LL_DMA_PDATAALIGN_BYTE);
      LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_5, LL_DMA_MDATAALIGN_BYTE);
  
      /* USER CODE BEGIN SPI2_Init 1 */
  
      /* USER CODE END SPI2_Init 1 */
      /* SPI2 parameter configuration*/
      SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
      SPI_InitStruct.Mode = LL_SPI_MODE_SLAVE;
      SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
      SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
      SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
      SPI_InitStruct.NSS = LL_SPI_NSS_HARD_INPUT;
      SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
      SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
      SPI_InitStruct.CRCPoly = 7;
      SPI2->CR1 |= LL_SPI_BAUDRATEPRESCALER_DIV2;
      SPI2->CR2 |= LL_SPI_RX_FIFO_TH_QUARTER;
      LL_SPI_Init(SPI2, &SPI_InitStruct);
      LL_SPI_DisableNSSPulseMgt(SPI2);
  
  }