text: //--------------------------------------------------------------
  // File     : stm32_ub_vga_320x240.c
  // CPU      : STM32F4
  // IDE      : CooCox CoIDE 1.7.0
  // Module   : GPIO, TIM, MISC, DMA
  // Function : VGA out by GPIO (320x240 Pixel, 8bit color)
  //
  // signals  : PB11      = HSync-Signal
  //            PB12      = VSync-Signal
  //            PE8+PE9   = color Blue
  //            PE10-PE12 = color Green
  //            PE13-PE15 = color red
  //
  // uses     : TIM1, TIM2
  //            DMA2, Channel6, Stream5
  //--------------------------------------------------------------
  
  
  //--------------------------------------------------------------
  // Includes
  //--------------------------------------------------------------
  #include "stm32_ub_vga_screen.h"
  
  extern TIM_HandleTypeDef htim1;
  extern TIM_HandleTypeDef htim2;
  extern DMA_HandleTypeDef hdma_tim1_up;
  
  //--------------------------------------------------------------
  // Init VGA-Module
  //--------------------------------------------------------------
  void UB_VGA_Screen_Init(void)
  {
    VGA.hsync_cnt = 0;
    VGA.start_adr = 0;
    VGA.dma2_cr_reg = 0;
  
    GPIOB->BSRR = VGA_VSYNC_Pin;
  
    // TIM2
    HAL_TIM_Base_Start(&htim2);
    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
    HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_3);
  
    // TIM1
    __HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_UPDATE);
    __HAL_TIM_ENABLE(&htim1);
    HAL_DMA_Start_IT(&hdma_tim1_up, (uint32_t)&VGA_RAM1[0], VGA_GPIOE_ODR_ADDRESS, VGA_DISPLAY_X + 1);
  
    HAL_DMA_Init(&hdma_tim1_up);
    __HAL_DMA_ENABLE_IT(&hdma_tim1_up, DMA_IT_TC);
  
    //-----------------------
    // Register swap and safe
    //-----------------------
    // content of CR-Register read and save
    VGA.dma2_cr_reg = DMA2_Stream5->CR;
  }
  
  
  //--------------------------------------------------------------
  // fill the DMA RAM buffer with one color
  //--------------------------------------------------------------
  void UB_VGA_FillScreen(uint8_t color)
  {
    uint16_t xp,yp;
  
    for(yp = 0; yp < VGA_DISPLAY_Y; yp++) {
      for(xp = 0; xp < VGA_DISPLAY_X; xp++) {
        UB_VGA_SetPixel(xp, yp, color);
      }
    }
  }
  
  
  //--------------------------------------------------------------
  // put one Pixel on the screen with one color
  // Important : the last Pixel+1 from every line must be black (don't know why??)
  //--------------------------------------------------------------
  void UB_VGA_SetPixel(uint16_t xp, uint16_t yp, uint8_t color)
  {
    if(xp >= VGA_DISPLAY_X)
      xp = 0;
    if(yp >= VGA_DISPLAY_Y)
      yp = 0;
  
    // Write pixel to ram
    VGA_RAM1[(yp * (VGA_DISPLAY_X + 1)) + xp] = color;
  }
  
  uint8_t UB_VGA_GetPixel(uint16_t xp, uint16_t yp)
  {
    if(xp >= VGA_DISPLAY_X)
      xp = 0;
    if(yp >= VGA_DISPLAY_Y)
      yp = 0;
  
    // Read pixel to ram
    return VGA_RAM1[(yp * (VGA_DISPLAY_X + 1)) + xp];
  }