{"action":"created","issue":{"url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32/issues/371","repository_url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32","labels_url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32/issues/371/labels{/name}","comments_url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32/issues/371/comments","events_url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32/issues/371/events","html_url":"https://github.com/stm32duino/Arduino_Core_STM32/issues/371","id":379927536,"node_id":"MDU6SXNzdWUzNzk5Mjc1MzY=","number":371,"title":"Gaining access to CYCCNT from inside the setup() methode","user":{"login":"hasenbanck","id":12084860,"node_id":"MDQ6VXNlcjEyMDg0ODYw","avatar_url":"https://avatars2.githubusercontent.com/u/12084860?v=4","gravatar_id":"","url":"https://api.github.com/users/hasenbanck","html_url":"https://github.com/hasenbanck","followers_url":"https://api.github.com/users/hasenbanck/followers","following_url":"https://api.github.com/users/hasenbanck/following{/other_user}","gists_url":"https://api.github.com/users/hasenbanck/gists{/gist_id}","starred_url":"https://api.github.com/users/hasenbanck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hasenbanck/subscriptions","organizations_url":"https://api.github.com/users/hasenbanck/orgs","repos_url":"https://api.github.com/users/hasenbanck/repos","events_url":"https://api.github.com/users/hasenbanck/events{/privacy}","received_events_url":"https://api.github.com/users/hasenbanck/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-11-12T19:51:03Z","updated_at":"2018-11-13T17:24:05Z","closed_at":"2018-11-13T17:24:05Z","author_association":"CONTRIBUTOR","body":"In Marlin we need to have access to the DWT CYCCNT register for short delays. On a STM32F7 you need to execute following code to gain access to these register:\r\n\r\n```c\r\nCoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;\r\nDWT->LAR = 0xC5ACCE55;\r\nDWT->CYCCNT = 0;\r\nDWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;\r\n```\r\n\r\nThis code is essential to not let the MCU halt when disconnecting the debugger probe (in my case Segger JLINK). After executing the above code, the program is then able to get the current cycle counter from the CYCCNT.\r\n\r\nRight now we are calling the above code from the setup() methode as the first code. But sadly, this seems too late, since the MCU will halt when detaching the debugger probe and starting normally.\r\n\r\nAfter some debugging I found out that I had to put the above code right after the SystemClock_Config() right after all clocks are configured. With that code change, I could now detach my debugger probe, the MCU didn't halt and the program got access to the CYCCNT register.\r\n\r\nI need to look deeper into the issue and need to identify what between the SystemClock_Config() and the Setup() method is called, that results in this problem.\r\n\r\nI would like to avoid putting the above code into the variant definition and I feel the setup() method is the right place to do so.\r\n"},"comment":{"url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32/issues/comments/438358534","html_url":"https://github.com/stm32duino/Arduino_Core_STM32/issues/371#issuecomment-438358534","issue_url":"https://api.github.com/repos/stm32duino/Arduino_Core_STM32/issues/371","id":438358534,"node_id":"MDEyOklzc3VlQ29tbWVudDQzODM1ODUzNA==","user":{"login":"hasenbanck","id":12084860,"node_id":"MDQ6VXNlcjEyMDg0ODYw","avatar_url":"https://avatars2.githubusercontent.com/u/12084860?v=4","gravatar_id":"","url":"https://api.github.com/users/hasenbanck","html_url":"https://github.com/hasenbanck","followers_url":"https://api.github.com/users/hasenbanck/followers","following_url":"https://api.github.com/users/hasenbanck/following{/other_user}","gists_url":"https://api.github.com/users/hasenbanck/gists{/gist_id}","starred_url":"https://api.github.com/users/hasenbanck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hasenbanck/subscriptions","organizations_url":"https://api.github.com/users/hasenbanck/orgs","repos_url":"https://api.github.com/users/hasenbanck/repos","events_url":"https://api.github.com/users/hasenbanck/events{/privacy}","received_events_url":"https://api.github.com/users/hasenbanck/received_events","type":"User","site_admin":false},"created_at":"2018-11-13T17:24:05Z","updated_at":"2018-11-13T17:24:05Z","author_association":"CONTRIBUTOR","body":"Since the current premain() also uses the \"constructor\" attribute, we can indeed the suggested method by @RickKimball .\r\n\r\nThis is how we can do the initialization between the premain() and main() function of the core:\r\n```\r\n__attribute__(( constructor (102)))\r\nvoid HAL_preinit() {\r\n  CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;\r\n  DWT->LAR = 0xC5ACCE55;\r\n  DWT->CYCCNT = 0;\r\n  DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;\r\n}\r\n```"}}
{"actor":{"display_login":"hasenbanck"}}