{"id":5787,"date":"2023-07-01T16:12:05","date_gmt":"2023-07-01T14:12:05","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=5787"},"modified":"2023-07-26T23:07:37","modified_gmt":"2023-07-26T21:07:37","slug":"under-the-hood-arduino-uno-r4-timers","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/","title":{"rendered":"Under the Hood: Arduino UNO R4 &#8211; Timers"},"content":{"rendered":"<p>I was wondering how to use the <strong>timers<\/strong> in the new <strong>Arduino UNO R4<\/strong>. Unfortunately I did not find any documentation, so I decided to document my findings here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg\" alt=\"\" width=\"300\" height=\"300\" class=\"alignnone size-full wp-image-5728\" srcset=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg 300w, https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4-150x150.jpg 150w, https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4-120x120.jpg 120w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>The <strong>UNO R4<\/strong> has two timer peripherals: the <strong>General PWM Timer (GPT)<\/strong> and the <strong>Asynchronous General Purpose Timer (AGT)<\/strong>.<\/p>\n<p>There are only 2 16 bit AGT timers (but one is already used to provide the Arduino millis() and microseconds() methods) and there are 7 GPT timers which are also used by PWM. To increase the flexibility you can explicitly request a timer which has been reserved for PWM.<\/p>\n<p>The Arduino API implements the <a href=\"https:\/\/github.com\/arduino\/ArduinoCore-renesas\/blob\/149f78b6490ccbafeb420f68919c381a5bdb6e21\/cores\/arduino\/FspTimer.h#L87\">FspTimer class<\/a> which provides all the necessary functionality.<\/p>\n<p>Here is a test sketch that is setting up a GPT timer with a corresponding interrupt which executes a callback method at a defined interval rate e.g. 8000 hz.<\/p>\n<pre><code>#include \"FspTimer.h\"\n\nFspTimer audio_timer;\nuint64_t count=0;\nuint64_t start_time=0;\n\n\/\/ callback method used by timer\nvoid timer_callback(timer_callback_args_t __attribute((unused)) *p_args) {\n  count++;\n}\n\nbool beginTimer(float rate) {\n  uint8_t timer_type = GPT_TIMER;\n  int8_t tindex = FspTimer::get_available_timer(timer_type);\n  if (tindex &lt; 0){\n    tindex = FspTimer::get_available_timer(timer_type, true);\n  }\n  if (tindex &lt; 0){\n    return false;\n  }\n\n  FspTimer::force_use_of_pwm_reserved_timer();\n\n  if(!audio_timer.begin(TIMER_MODE_PERIODIC, timer_type, tindex, rate, 0.0f, timer_callback)){\n    return false;\n  }\n\n  if (!audio_timer.setup_overflow_irq()){\n    return false;\n  }\n\n  if (!audio_timer.open()){\n    return false;\n  }\n\n  if (!audio_timer.start()){\n    return false;\n  }\n  return true;\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  beginTimer(8000);\n  start_time = millis();\n}\n\nvoid loop() {\n  \/\/ calculate the effective frequency\n  int freq = 1000 * count \/ (millis()-start_time);\n  Serial.println(freq);\n  count = 0;\n  start_time = millis();\n  delay(1000);\n}\n<\/code><\/pre>\n<p>This is pretty straight forward!<\/p>\n<p>The cool thing, which is no shown here, is that you can add a final parameter to the audio_timer.begin() method which will end up as parameter value which is passed to the timer_callback!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was wondering how to use the timers in the new Arduino UNO R4. Unfortunately I did not find any documentation, so I decided to document my findings here. The UNO R4 has two timer peripherals: the General PWM Timer (GPT) and the Asynchronous General Purpose Timer (AGT). There are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5728,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20],"tags":[44],"class_list":["post-5787","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","tag-uno"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Under the Hood: Arduino UNO R4 - Timers - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Under the Hood: Arduino UNO R4 - Timers - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"I was wondering how to use the timers in the new Arduino UNO R4. Unfortunately I did not find any documentation, so I decided to document my findings here. The UNO R4 has two timer peripherals: the General PWM Timer (GPT) and the Asynchronous General Purpose Timer (AGT). There are [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-01T14:12:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-26T21:07:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"Under the Hood: Arduino UNO R4 &#8211; Timers\",\"datePublished\":\"2023-07-01T14:12:05+00:00\",\"dateModified\":\"2023-07-26T21:07:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/\"},\"wordCount\":185,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/UnoR4.jpg\",\"keywords\":[\"UNO\"],\"articleSection\":[\"Arduino\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/\",\"name\":\"Under the Hood: Arduino UNO R4 - Timers - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/UnoR4.jpg\",\"datePublished\":\"2023-07-01T14:12:05+00:00\",\"dateModified\":\"2023-07-26T21:07:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/UnoR4.jpg\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/UnoR4.jpg\",\"width\":300,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/07\\\/01\\\/under-the-hood-arduino-uno-r4-timers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Under the Hood: Arduino UNO R4 &#8211; Timers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Under the Hood: Arduino UNO R4 - Timers - Phil Schatzmann","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/","og_locale":"en_US","og_type":"article","og_title":"Under the Hood: Arduino UNO R4 - Timers - Phil Schatzmann","og_description":"I was wondering how to use the timers in the new Arduino UNO R4. Unfortunately I did not find any documentation, so I decided to document my findings here. The UNO R4 has two timer peripherals: the General PWM Timer (GPT) and the Asynchronous General Purpose Timer (AGT). There are [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/","og_site_name":"Phil Schatzmann","article_published_time":"2023-07-01T14:12:05+00:00","article_modified_time":"2023-07-26T21:07:37+00:00","og_image":[{"width":300,"height":300,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg","type":"image\/jpeg"}],"author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"Under the Hood: Arduino UNO R4 &#8211; Timers","datePublished":"2023-07-01T14:12:05+00:00","dateModified":"2023-07-26T21:07:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/"},"wordCount":185,"commentCount":9,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg","keywords":["UNO"],"articleSection":["Arduino"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/","url":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/","name":"Under the Hood: Arduino UNO R4 - Timers - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg","datePublished":"2023-07-01T14:12:05+00:00","dateModified":"2023-07-26T21:07:37+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2023\/06\/UnoR4.jpg","width":300,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/07\/01\/under-the-hood-arduino-uno-r4-timers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"Under the Hood: Arduino UNO R4 &#8211; Timers"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=5787"}],"version-history":[{"count":11,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5787\/revisions"}],"predecessor-version":[{"id":5885,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5787\/revisions\/5885"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/5728"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=5787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=5787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=5787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}