 Hello. Within this part we will focus on software timers and its implementation within 3R2S and CMC2S version to API. I'm starting from STM32QPyDE by starting a new project. So I go to File, New, STM32 project and I will use STM32L476RG microcontroller and it is soldered on the NucleoL476RG port which I'm using during this session. You can use instead QBMX and your preferred toolchain and any other STM32 microcontroller. Name of my exercise is 11 underscore SW underscore TIM and concerning pinout selection and the peripheral selection I will enable only the debug interface with additional serial wire output pin. So system core says this debug trace asynchronous SW. So to have those three lines and I will change the timebase source for HAL library from SysTick to Timer 6. I need to enable 3R2S value CMC's version to API. And within the configuration I need to check whether the software timers are enabled. In this current version the software timers are enabled by default. Additionally we've got 3 other parameters timer task priority, timer queue, length and timer task stack depth. So those three parameters I already described within the theoretical part. In this field the most important one is this one task priority. It is important to select the priority of this task on the level which is allowing us to execute let's say scheduler execute this task between other tasks. If it would be too low the priority would be too low and other tasks will never go to the blocked state it might happen that the software timers will be let's say serviced to not proper way. This default tool is really too low versus the standard priorities which we are using within this CMC's OS V2 but let me continue with this too low value and show you the negative effect of it which could be quite positive in terms of an education. So let's go to the task so we'll define only one so I would let's say change the name of it of this default task into the task 1. Priority normal stack size 256 words like in other exercises and the default task start task 1. That's it and we need as well to specify one software timer. For this I'm going to timers and semaphors tab and within timers section I would add my timer. I would keep the default settings and that's it. That's all components which we would like to select within the configurator so I will generate the code and we need to open the main.c file. If it's not open automatically I can find it within the core source main. Let's continue with code processing. This is our main.c file. If I scroll down I can see two components already created so our task 1 and timer 1. Then within main after hardware installation I can see the creation of timer 1 and its assignment to the callback which would be called once the timer will overflow and type of it so the periodic in our case. Then there is our let's say task creation and if we go below we can see our standard task entry function and below we can see as well the callback function which would be called once the timer will overflow. It is created as well automatically. Please remember that the callback function of the timer should not be a blocking function so we should avoid here to put any delays and let's say too long coding the information within this callback should be very short and precise. So I would use here only sign of life so task action and within task action we will send C over selected interface. I would implement task action in a while after basic operations. Then how to start the software timer because it is created but not started yet. I can use the initialization part of any of the tasks. What is important is that it is not allowed to stop nor start timer software timer from the interrupt. This is why we are not using them in this exercise. As a result if you try to do this you will exit from the function with the negative value os status underscore t type which is an information that there is something wrong. We will start our software timer within the initialization part of our task 1 and to start the software timer we can use function os timer start and to do this we need let's say two arguments first is the handler and the second one is number of ticks so in our case milliseconds because our tick within this this series of exercises this training is one millisecond so I would use 1000 so in fact one second and the timer is started okay and then within our let's say task function body I would first wait two seconds sending the task to the block state and I would send some let's say sign of life so this is task one so I would just send one that's it this is the complete code and what is an expected result an expected result is the following as our task one would be in fact executing something once per two seconds so I am expecting that I would see on my communication interface on my let's say terminal one displayed once per two seconds and in the meantime I should see to see letters on the same terminal sent by the callback from the software timers as let's say the software timer is periodic and its period is 1000 milliseconds so this is the next unexpected result so let's see whether it will work so I would compile the code okay as expected there is an error because we have not implemented yet task action so let's do it so in my case I would use itm interface itm itm send char and then message and after this I would as well use the same function to send as sign of new line to have each sign of life within separate line okay and we need to put the prototype above this part is quite okay for this purpose because it's private function prototypes okay let's try to build it once again now it's much better zero errors zero warnings so let's start a debug session my board is already connected so I preset back icon within debugger settings I'm enabling serial wire viewer swv configure the clock to the used one so the four megahertz apply and okay okay being within debug perspective I'm selecting the swv itm data console if you cannot see this please find it using this quick access on top swv and you select this line with the monitor icon on the left side once you select it please configure it using this button and please select the stimulus port to zero then please start trace and start your application okay let's pause it and let's analyze what we've got display my main.c file both task and callback let's analyze it what we can see we can see at the beginning see coming from the software timer then we've got one from task you can observe that this one is coming once per two seconds in the meantime we can see two occurrences of the callback which has the period of 1000 milliseconds so it is working like expected but analyzing this do we cover all of the let's say potential risks the answer is unfortunately no because please have a look we've got a protection here which is this OS delay OS delay in this place causes this task one sent to blocked state if this task is sent to blocked state there is a space to other tasks with lower priorities among those let's say tasks with lower priorities is this task responsible to pass information to the let's say from task to the software timer this is let me come back to this point and task and the config parameters as you remember from the theoretical part the software timers are using let's say its own task which is managing the communication between the tasks and the software timers all the comments related to the software timers are sent via separate queue which is defined within this line the most important is in fact this timer task if this priority is too low it may happen that it will be never executed so let's do a short experiment I would replace this OS delay with Hall delay so in fact my function will be not sent to the blocked state but it will remain either in run or in ready state in this case scheduler will always select only task one as the only one with the highest possible priority so let's have a look what would be the difference now now I can let's say start a debug session ops data console start tracing and start an application you see there is no software callback once per two seconds I can see the signals from our task one and that's it the callback cannot be executed as the software timer is in fact not started it is not started as a software timer task cannot be executed as it has too low priority so let's have a look how we can correct it if we scroll up and we will check what is in fact the numeric numerical value of this OS priority normal it is 24 so let's come back to our ios let's say configuration file and the last change this to you know 20 into that 24 so it will let's say that the priority of the software timer task will be the same like the priority of the task we are using in our application so after this I would regenerate the code and I would compile it please have a look I still have this hardy like here but in this case even if this task one will never go to the blocked state it will be either in run or in ready state but in this case my software timer task will have the same priority like task one so scheduler will select task one or a software timer task and there would be a chance to execute this callback because software timer will be started okay so let's now start a debug session itm data console start trace and run okay this is not a result I expected so let's let's exit so let's modify the priority of the timer task to 24 to be equal to other other tasks which are present in the code I regenerate the code so at the moment we will have in fact three tasks task one software timer task and idle task idle task has lowest possible priority but the other two tasks task one and software timer task will have the same priority and even if within our task one we will keep it always in a run already state there would be a space for a software timer task which would let's say start and communicate with the software timer so let's start a debug session before I would compile it start team banger then itm data console start trace and start the application and now we can see the situation as expected so two occurrences of a callback from software timer once per second and then once per two seconds I can see the action from task one so as you can see it is really important once you are designing an application of usage of software timers to properly configure those parameters especially this timer task priority to have it in a range of other tasks which are present in the system that's all for this part thank you for watching this video