element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
At The Core Design Challenge
  • Challenges & Projects
  • Design Challenges
  • At The Core Design Challenge
  • More
  • Cancel
At The Core Design Challenge
Blog ATCDC: Digimorf, Day 15, The project SEGA SG-1000 Emulator, VGA on PSoC62S4 Pioneer kit - part 4
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join At The Core Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Digimorf
  • Date Created: 13 Mar 2023 4:50 PM Date Created
  • Views 1242 views
  • Likes 4 likes
  • Comments 0 comments
  • infineon
  • PSoCTm︎ 62 MCU
  • timer
  • pwm
  • modustoolbox
  • vga
Related
Recommended

ATCDC: Digimorf, Day 15, The project SEGA SG-1000 Emulator, VGA on PSoC62S4 Pioneer kit - part 4

Digimorf
Digimorf
13 Mar 2023
ATCDC: Digimorf, Day 15, The project SEGA SG-1000 Emulator, VGA on PSoC62S4 Pioneer kit - part 4

This is a good rainy day for examining code and parameters with a good cup of coffee and nice music.

The video frame is stable, even if the naive method that I used now for sending RGB data to the monitor is not what I have in mind. Anyway, I will explain it later. I want now to update you about the HSync question and about the parameters used with the new clock source.

Let's recap. The clock source used here is the ECO which is connected to the 24MHz external oscillator. The Peripheral clock generated is 75MHz +/- 0.001%. Now with this clock, the cycle of the TCPWM counter is (1/75MHz)13.33ns.

Looking back at the VGA timings for the 640x480x60Hz resolution:

Horizontal timing (line)

Polarity of horizontal sync pulse is negative (HIGH-LOW-HIGH).

Scanline part Pixels Time [µs]
Visible area 640 25.422045680238
Front porch 16 0.63555114200596
Sync pulse 96 3.8133068520357
Back porch 48 1.9066534260179
Whole line 800 31.777557100298

image

we can recalculate the period of the PWM cycle, but now we can go ahead and improve the PWM waveform using both CC0 and CC1 to better align the HSync pulse. 

CC0 = Active Area + Front Porch 

CC0 = 25422ns + 636ns = 26058ns 

CC1 = CC0 + Synch 

CC1 = 26058ns + 3813ns = 29871ns

TC = CC1 + Back Porch

TC = 29871ns + 1906ns = 31777ns

Now, thinking in terms of TCPWM counter cycles (13.33ns):

CC0 = 26058ns / 13.33ns = 1955

CC1 = 29871ns / 13.33ns = 2240

TC = 31777ns  / 13.33ns = 2383

image

In order to do this we need to configure the TCPWM as asymmetrical, as seen in the technical reference manual:

image 

Again the Device Configurator in ModusToolbox comes helpful, and we can set parameters easily:

image

This setting tab will generate the code of definitions and globals that are included automatically in the project. You can copy them for reference in the Code Preview tab. My suggestion is to copy the names/definitions/globals into a comment block and put it into your routine that initializes the TCPWMcounter.

/*
                          tcpwm_0_group_1_cnt_4_HW
                          tcpwm_0_group_1_cnt_4_NUM
                          tcpwm_0_group_1_cnt_4_IRQ
cy_stc_tcpwm_pwm_config_t tcpwm_0_group_1_cnt_4_config
cyhal_resource_inst_t     tcpwm_0_group_1_cnt_4_obj

                          init_cycfg_peripherals
                          reserve_cycfg_peripherals
*/

Here we come at the real code that I used to show color stripes on a VGA monitor.

void HSyncPWM_Init()
{
  /*----------------------------------------------------------------------*/
  cy_stc_sysint_t HSyncInt_cfg = {
      tcpwm_0_group_1_cnt_4_IRQ,
      0
  };

  /*----------------------------------------------------------------------*/
  /* Initialize the interrupt with vector at Interrupt_Handler_Port0() */
  Cy_SysInt_Init(&HSyncInt_cfg, (cy_israddress)HSyncPWM_IRQHandler);


  /* Enable the interrupt */
  NVIC_EnableIRQ(HSyncInt_cfg.intrSrc);

  /*----------------------------------------------------------------------
   *
      Horizontal :
             :_________________       :
             |                 |      :
    _________|  VIDEO          |______:___
             :                        :
    ____   __:___________________   __:___
        |_|  :                   |_|  :
             :               CCn-0 1  :TC

    CC0  1955
    CC1  2240  CC1-CC0 = HSync pulse
    TC   2383  TC-CC1 = Back porch
               TC = RGB data starts

   ----------------------------------------------------------------------*/
  if (CY_TCPWM_SUCCESS != Cy_TCPWM_PWM_Init(tcpwm_0_group_1_cnt_4_HW,
                                            tcpwm_0_group_1_cnt_4_NUM,
                                            &tcpwm_0_group_1_cnt_4_config)) {}

  /* Enable the initialized PWM */
  Cy_TCPWM_PWM_Enable(tcpwm_0_group_1_cnt_4_HW,
                      tcpwm_0_group_1_cnt_4_NUM);

  /* Then start the PWM */
  Cy_TCPWM_TriggerStart_Single(tcpwm_0_group_1_cnt_4_HW,
                               tcpwm_0_group_1_cnt_4_NUM);
  /*----------------------------------------------------------------------*/
}

Some globals to add:

/*******************************************************************************
* Global Variables
*******************************************************************************/
GPIO_PRT_Type* portAddr = GPIO_PRT10;

volatile uint16_t gScanline = 0;
volatile uint16_t x = 0;

And the ISR:

void HSyncPWM_IRQHandler(void)
{
  /* Get all the enabled pending interrupts */
  uint32_t interrupts =
      Cy_TCPWM_GetInterruptStatusMasked(tcpwm_0_group_1_cnt_4_HW,
                                        tcpwm_0_group_1_cnt_4_NUM);

  if (CY_TCPWM_INT_ON_CC0 & interrupts)
  {
    portAddr->OUT = 0;

    switch (gScanline) {

      case 0: cyhal_gpio_write(P9_1, false); break;
      case 2: cyhal_gpio_write(P9_1, true); break;
    }

    gScanline++;
    if (gScanline == 525) gScanline = 0;
  }

  if (CY_TCPWM_INT_ON_TC & interrupts)
  {
    if ((gScanline >= 35) && (gScanline < 525)) {
      x = 256;
      while (x--) {
        portAddr->OUT = x;
      }

      portAddr->OUT = 0;
    }
  }

  /* Clear the interrupt */
  Cy_TCPWM_ClearInterrupt(tcpwm_0_group_1_cnt_4_HW,
                          tcpwm_0_group_1_cnt_4_NUM,
                          interrupts);
}

And last but not least the call to the initialization of TCPWM from the main:

int main(void)
{
    cy_rslt_t result;

#if defined (CY_DEVICE_SECURE)
    cyhal_wdt_t wdt_obj;

    /* Clear watchdog timer so that it doesn't trigger a reset */
    result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
    CY_ASSERT(CY_RSLT_SUCCESS == result);
    cyhal_wdt_free(&wdt_obj);
#endif

    /* Initialize the device and board peripherals */
    result = cybsp_init();

    /* Board init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS) CY_ASSERT(0);

    /* Enable global interrupts */
    __enable_irq();

    HSyncPWM_Init();

    for (;;)
    {
    }
}

This time I am testing the VGA video signal on my 7" portable monitor comfortably sitting on my chair, and everything is nice and clean.

image

As seen also in this short closeup video.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 From now on things will get harder of course. Another counter will rule a DMA transfer from a line buffer or a frame buffer. Hard work to take advantage of the resources of the MCU, but this will free precious system cycles for emulation.

See you in the next part!

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube