Sunday, 7 April 2019

RC Oscillator Vs Crystal Oscillator

RC Oscillators and Crystal Oscillators are so common in Embedded world. Nowadays most of the controllers are coming with inbuilt RC Oscillator. But still as most of the embedded geeks knows critical applications are going with crystal oscillator. Here it is a practical experiment which helps to understand the performance of both oscillators in detail.

This experiment is done by ST32F4 Discovery board. STM32F407VG is embedded in this board. It comes with an internal RC Oscillator of 16MHz. I have used this clock to create a system clock of frequency 8MHz. This clock is output at RCC_MCO2(Pin PC9). In order to do this experimen I enabled external crystal oscillator of 8MHz. This is output at RCC_MCO1(Pin PA8). The results is as follows.

In general both works fine, as shown abovr from both source 8MHz is output as expected.In this Channel 4 is RC oscillator and Channel 7 is Crystal Oscillator.

Now if we had a closure observation of RC Oscillator output  continuously, the fluctuation can be noted in its output while Crystal Oscillator output is stable.

In the above diagram in place of 8MHz , the output is disturbed. This fluctuation can be seen here in RC Oscillator output.
This shows the accuracy of crystal oscillator for normal operation.
Now let's see which is stable in faster.  In the below image, MCU is reset at the marked area.
It can be seen that the crystal oscillator takes really large time , more than 2 microsecond , to become stable. But in case of RC Oscillator(Channel 4) it can be seen that the output is become stable faster than crystal oscillator output.

This shows why for faster stabilization RC oscillator is preferable and continuous stability Crystal Oscillator is preferable.

Thursday, 27 December 2018

Remotely Controllable Botloader

         Here, this is a bootloader designed from scratch for STM32F407 out of hobby. Though it is designed for ST, it can be used with any ARM Cortex M4 based controllers with minimal changes. 
        How are we getting updates for embedded software remotely and how are we able to flash or configure any controller from anywhere!!! This is studied here and ended up with this bootloader that can be controlled remotely over piconet.  
        This custom bootloader is placed at sector 0, i .e at starting address of flash. If the predefined input is not received within a specific time, bootloader will call user application which resides at sector 2 of flash. This is the normal mode of operation . Once the system receives user input to latch on bootloader mode it will wait for further command.  Since my bluetooth module is compatible with UART communication, I preferred UART communication for my bootloader. As per the user input, bootloader is featured with the following opertion,

  • Programming the flash
  • Erasing the flash
  • Jumping to Memory locations
  • CRC Check
  • Enabling sector protection
  • Disabling protection
  • Memory Read etc.
Lot of new things came to my attention during this project including ST's native bootloader, option bits of ST etc.. It was an interesting learning time! Next phase I may update this to communicate over internet. 
The complete work is available in the following Github repository:
https://github.com/nidhinboom/Bootloader.git



How can we write a function with Variable Arguments ???

I am sure there won't be any C-programmer who never use 'printf()' function. But have you noted its specialty ??  I can take n-number of arguments. The type and number of arguments are also part of the signature of a function. Still how is it possible ?

I guess there are enough questions . Now time to answer. 

The first time when I thought about this is while I am doing an embedded program for a LCD display. I was thinking about a printf kind of function which can take any number of arguments and display.

This can be done by va (Virtual Argument)api which are defined in the header file <stdarg.h> .

va_list, va_start , va_arg and va_end are the most useful apis available from this. Let me explain these with an example.

This is my function prototype which takes any number of arguments and will print the sum.

void sum_numbers(int num,...);


How can it be implemented ?

int sum_numbers(int num,...)
{


va_list args; // Here we created a pointer that will point to the first element of the argument list.
va_start(args,num); //Here we are initializing the pointer with the first element in the list; The
                  // second argument should be the last argument of the function that comes before ellipses.
int sum=0;
fot(int i=0; i<num; i++)
{
int element=va_args(num,int);
/* Here each arguments are being retrieved. Here I have assumed that all arguments are integer type.
    But if you are not sure you can always pass a format string to know type.

sum +=element;
}

va_end(args); // Before function returns we have to close it by this api.

return sum;
}

I guess embedded engineers may meet many use cases of this api. :-)




RC Oscillator Vs Crystal Oscillator

RC Oscillators and Crystal Oscillators are so common in Embedded world. Nowadays most of the controllers are coming with inbuilt RC Oscilla...