{"id":2744,"date":"2024-08-17T15:11:51","date_gmt":"2024-08-17T15:11:51","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2744"},"modified":"2024-08-17T15:11:54","modified_gmt":"2024-08-17T15:11:54","slug":"getting-started-with-stm32g0-and-stm32cubeide-update-duty-cycle-using-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2744","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: Update Duty Cycle using DMA"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1.webp\" alt=\"\" class=\"wp-image-2745\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall see how to configure the DMA with timer to update the duty cycle of the PWM using DMA to generate certain waveform.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>STM32CubeMX Configuration.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. STM32CubeMX Configuration:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by creating new project with name of PWM_DMA.<\/p>\n\n\n\n<p>For how to create project, please refer to this <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" target=\"_blank\" rel=\"noreferrer noopener\">guide here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>One CubeMX is opened, from timers, select TIM1.<\/p>\n\n\n\n<p>From TIM1:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set clock  source to internal cock.<\/li><li>Channel1 and Channel2 PWM Generation CH1 and CH2 respectively.<\/li><li>Set counter period to 1600.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-1024x584.jpg\" alt=\"\" class=\"wp-image-2746\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-1024x584.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-300x171.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-768x438.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-1536x876.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-2048x1168.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-1150x656.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-750x428.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-400x228.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-59-04-250x143.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>From DMA tab:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Add TIM1_CH1 and TIM1_CH2.<\/li><li>Set direction to Memory to Peripheral for both.<\/li><li>Enable Circular mode for both.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-1024x584.jpg\" alt=\"\" class=\"wp-image-2747\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-1024x584.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-300x171.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-768x438.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-1536x877.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-2048x1169.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-1150x656.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-750x428.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-400x228.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-17_17-37-24-250x143.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Save the project and this will generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c source file:<\/p>\n\n\n\n<p>In User code begin PV (Private Variable):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define sample_size\t\t200\n\n\nuint16_t lookUp1[sample_size] = {0,50 ,100 ,151 ,201 ,250 ,300 ,349 ,398 ,446 ,494 ,542 ,589 ,635 ,681\n\t,726 ,771 ,814 ,857 ,899 ,940 ,981 ,1020 ,1058 ,1095 ,1131 ,1166 ,1200 ,1233 ,1264\n\t,1294 ,1323 ,1351 ,1377 ,1402 ,1426 ,1448 ,1468 ,1488 ,1505 ,1522 ,1536 ,1550 ,1561\n\t,1572 ,1580 ,1587 ,1593 ,1597 ,1599 ,1600 ,1599 ,1597 ,1593 ,1587 ,1580 ,1572 ,1561\n\t,1550 ,1536 ,1522 ,1505 ,1488 ,1468 ,1448 ,1426 ,1402 ,1377 ,1351 ,1323 ,1294 ,1264\n\t,1233 ,1200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 ,981 ,1020 ,1058 ,1095 ,1131 ,1166 ,1200 ,1233\n\t,1264 ,1294 ,1323 ,1351 ,1377 ,1402 ,1426 ,1448 ,1468 ,1488 ,1505 ,1522 ,1536 ,1550 ,1561 ,1572 ,1580\n\t,1587 ,1593 ,1597 ,1599 ,1600 ,1599 ,1597 ,1593 ,1587 ,1580 ,1572 ,1561 ,1550 ,1536 ,1522 ,1505 ,1488\n\t,1468 ,1448 ,1426 ,1402 ,1377 ,1351 ,1323 ,1294 ,1264 ,1233 ,1200 ,1166 ,1131 ,1095 ,1058 ,1020 ,981\n\t,940 ,899 ,857 ,814 ,771 ,726 ,681 ,635 ,589 ,542 ,494 ,446 ,398 ,349 ,300 ,250 ,201 ,151 ,100 ,50 ,0};<\/pre><\/div>\n\n\n\n<p>Here, we have 200 sample for duty cycle for sinewave.<\/p>\n\n\n\n<p>In user code begin 2:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">  HAL_TIM_PWM_Start_DMA(&amp;htim1, TIM_CHANNEL_1, lookUp1, 200);\n  HAL_TIM_PWM_Start_DMA(&amp;htim1, TIM_CHANNEL_2, lookUp2, 200);<\/pre><\/div>\n\n\n\n<p>The HAL_TIM_PWM_Start_DMA takes the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pointer to timer handler, htim1 in this case.<\/li><li>Timer channel, which is channel 1 and channel 2.<\/li><li>Data array that hold the duty cycles values.<\/li><li>Size of the data.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats it for the code.<\/p>\n\n\n\n<p>Save the project, build it and run it on your STM32G070.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>By using logic analyzer and probing PA8 and PA9, you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"211\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-1024x211.png\" alt=\"\" class=\"wp-image-2748\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-1024x211.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-300x62.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-768x158.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-1536x316.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-2048x421.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-1150x236.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-750x154.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-400x82.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-17-at-5.42.06\u202fPM-250x51.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We have successfully updated the duty cycle with DMA without using CPU resources at all.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to configure the DMA with timer to update the duty cycle of the PWM using DMA to generate certain waveform. In this guide, we shall cover the following: STM32CubeMX Configuration. Code. Results. 1. STM32CubeMX Configuration: We start off by creating new project with name of PWM_DMA. For how [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-2744","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2744"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2744"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2744\/revisions"}],"predecessor-version":[{"id":2749,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2744\/revisions\/2749"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}