Friday 8 April 2016

On Android development and porting

I have decided to work on parallel to learn to understand the android system and porting to a new SoC. My baremetal adventures will continue as is but this a slight distraction/relaxation from the intense work of the baremetal OS.

I will post articles and examples on what I have learnt in android along with some mini projects to demonstrate the concepts. I will referring to "Embedded Android" by Karim Yaghmour to learn my concepts. The development board which I will be using is a Wandboard dual with Bluetooth and WiFi. It has a NXP/Freescale i.MX6 dual core processor in it.

Sunday 3 April 2016

Code cleanup and refactoring in MDK-OS Clock setup

Clocks form an important part in any embedded system and configuration of this clock tree is very error prone. In the present code clean up I have parametrized the existing configurations which were hard coded. This is very error prone as any change in the base clock speed would cause a mismatch with the configuration.

The first step I took in refactoring my code was to have helper functions to get FCLK, HCLK and PCLK. In the same vein I created a conditional compilation set up which compiles based on the board i.e. it being a MINI2440 or a MINI2410 etc. From the clock point of view the crystal used in these boards varies. The MINI2440 uses a clock source derived from a crystal based on OM3 and OM2 pins. The crystal as per the schematic is a 12Mhz crystal.

With this as the base and using the helper functions the whole clock tree settings i.e the pre-scaler can be automated for different peripherals such as UART, SD MMC using formulae rather than hard coding values.

First we will try to understand the different clocks signals generated by the control logic. There are 3 clock signals generated FCLK for the CPU, HCLK for the AHB bus peripherals, and PCLK for the APB bus peripherals.

The S3C2440 has  2 PLL's one for FCLK, HCLK and PCLK and the other dedicated for USB block (48 MHz). The control logic can make slow clocks without the PLL's.

What is a PLL and what does it do? PLL or a Phase Locked Loop is a control system which generates an output signal whose phase is related to the phase of the input signal.  Generally we use a PLL to generate a multiple of the input frequency. So the input to the PLL is a oscillator and output is a multiple of the input frequency. So in our case we have a 12MHz crystal oscillator which is given to the PLL's to generate multiples and keeping the phase locked with the input and output frequencies.
PLL's take time to stabilize. Hence there should be a way for the chip to work based on the oscillator frequency or an external signal. Once the PLL stabilizes and is able to generate a clean signal the chip can switch to the PLL frequency.

The following is the clock architecture of the S3C2440.


The main clock comes from the external crystal (XTlpll) or an external clock (EXTCLK). The clock generator includes an oscillator (Oscillation Amplifier), which is connected to an external crystal, and also has two PLL's (Phase locked loop) which generate the high frequency clock required in the S3C2440A.


 The OM[3:2] status is latched internally by referring the OM3 and OM2 pins at the rising edge of nRESET as shown below

According to the data sheet the clock selection during boot-up is Crystal for the main clock source and USB.

The MPLL  starts just after a reset but the MPLL output is not used as the system clock until the software writes valid settings to the MPLLCON register. Before these settings, the clock from the external crystal or EXTCLK source will be used as the system clock directly. Even if the user does not want to change the default value of the MPLLCON register, the user should write the same value into the MPLLCON register.

After the power on reset the crystal oscillator begins oscillation within several milliseconds. When nRESET is released after the OSC (XTIpll) clock the PLL starts to operate according the default PLL configuration. However the PLL is commonly known to be unstable after power-on reset so Fin is fed directly to the FCLK instead of the Mpll (PLL output) before the software newly configures PLLCON.

The PLL restarts the lockup sequence toward the new frequency only after the software configures the PLL with a new frequency. FCLK can be configured as PLL output (Mpll) immediately after lock time.

The wave form diagram below gives a clearer picture


For USB clock control and USB device interface needs 48MHz clock hence a dedicated USB PLL (UPLL) generates the clock.

Now we come to coding the clock setup. The clock setup has to be parameterized.

We come to the init_clock(..) function. Here we set the clock lock time to maximum. Next we do a set_clock_divn(..). Here we set the dividers. We divide the FCLK by 1, HDIVN by 4 and PDIVN by 8. Note that MPLL is fed into the CLKCNTL logic which generates the FCLK.



1
2
3
4
 set_clock_divn(CLK_BASE_ADDR,
     DIVN_UPLL_BY_1,
     HDIVN_FCLK_BY_4,
     PDIVN_HCLK_BY_2);


So the final clock setup would be:

 FCLK = 405 MHz.
 HCLK = 405/4 = 101MHz
 PCLK = 405/8 = 50 MHz

 We need to setup the CPU to asynchronous mode. We can see the P 2-11 of ARM920T (Chapter 5). Also S3C2440 does not support synchronous mode.

The code is as follows:


1
2
3
4
5
6
7
8
__asm__ __volatile__(
  "mrc p15,0,r1,c1,c0,0\n\t"
  "orr r1,r1,#0xC0000000\n\t"
  "mcr p15,0,r1,c1,c0,0\n\t"
  : /* No output */
  : /* No input */
  : "r1" /* r1 clobbered */
  );

Next we have to set the 2 PLL's. MPLL and UPLL.

First we set the UPLL to generate 48 MHz as follows:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 set_clk_upll(CLK_BASE_ADDR,0x38,0x2,0x2); //48 MHz.
    
 __asm__ __volatile__(
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
  );

Note the mov r0,r0. This is used generate at least 7 NOPs.

Next we set the MPLL to 405 MHz  as follows:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 set_clk_mpll(CLK_BASE_ADDR,0x7f,0x2,0x1); //405 MHz

 __asm__ __volatile__(
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
   "mov r0,r0\n\t"
  );

Finally we clear the slow clock register bits as follows.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#define clear_slow_clock(BA) do { \
 clear_reg_bits(CLKSLOW_REG(BA),UCLK_ON); \
 clear_reg_bits(CLKSLOW_REG(BA),MPLL_OFF); \
 __asm__ __volatile__ ( \
  "mov r0,r0 \n\t"  \
  "mov r0,r0 \n\t"  \
  "mov r0,r0 \n\t"  \
  "mov r0,r0 \n\t"  \
  "mov r0,r0 \n\t"  \
  "mov r0,r0 \n\t"  \
 ); \
 clear_reg_bits(CLKSLOW_REG(BA),SLOW_BIT); \
 } while(0)

We clear the UCLK bit to turn on UPL. Next we turn on the MPLL and allow it settle which takes 300us. Hence we have NOPs for the delay. After this we turn OFF the slow clock to set the MPLL to the FCLK.


To get the current clocks I have written utility functions so that I can derive the different clocks just by reading the registers itself.

First I have helper functions to get the dividers as follows:


1
2
3
4
5
6
7
8
#define get_clk_pll_mdiv(PLL_REG) \
 (((readreg32(PLL_REG)) & MDIV_MASK) >> MDIV_SHIFT)

#define get_clk_pll_pdiv(PLL_REG) \
 (((readreg32(PLL_REG)) & PDIV_MASK) >> PDIV_SHIFT)

#define get_clk_pll_sdiv(PLL_REG) \
 (((readreg32(PLL_REG)) & SDIV_MASK) >> SDIV_SHIFT)

Next I get the 2 PLL clock i.e. UPLL and MPLL based on the following helper functions:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
uint32_t get_mpll_clk(uint32_t BA)
{
 uint32_t m,p,s;
 
 m = get_clk_pll_mdiv(MPLLCON_REG(BA)) + 8;
 p = get_clk_pll_pdiv(MPLLCON_REG(BA)) + 2;
 s = get_clk_pll_sdiv(MPLLCON_REG(BA));

 return ((m * S3C_CLOCK_REFERENCE) * 2)/(p * (1<<s));
}


uint32_t get_upll_clk(uint32_t BA)
{
 
 uint32_t m,p,s;
 
 m = get_clk_pll_mdiv(UPLLCON_REG(BA)) + 8;
 p = get_clk_pll_pdiv(UPLLCON_REG(BA)) + 2;
 s = get_clk_pll_sdiv(UPLLCON_REG(BA));

 return (m * S3C_CLOCK_REFERENCE)/(p * (1<<s));
}

To explain the above code I have to bring out the formulae. According the datasheet:

MPLL Control Register

Mpll = (2 * m * Fin) / (p * 2 S)
m = (MDIV + 8), p = (PDIV + 2), s = SDIV

UPLL Control Register

Upll = (m * Fin) / (p * 2 S)
m = (MDIV + 8), p = (PDIV + 2), s = SDIV



Hence we get the m, p and s.

Next we have the formula:

PLL Value Selection Guide (MPLLCON)
  1.  Fout = 2 * m * Fin / (p*(2^s) ), Fvco = 2 * m * Fin / p where: m=MDIV+8, p=PDIV+2, s=SDIV
  2.  600MHz ≤ FVCO ≤ 1.2GHz
  3.  200MHz ≤ FCLK OUT ≤ 600MHz
In the above code we return the Fout formula.

So to finally get the FCLK, HCLK,PCLK and the UCLK we have to check the dividers. We do divide the FCLK so we use Fout as is. Next for HCLK,PCLK and UCLK we have the following code:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
uint32_t get_hclk(uint32_t BA)
{
 uint32_t fclk = get_fclk(BA);

 switch(get_clock_hdivn(BA)) {
  case HDIVN_FCLK_BY_1:
   return fclk;
  case HDIVN_FCLK_BY_2:
   return (fclk >> 1);
  case HDIVN_FCLK_BY_4:
   //TODO: CAMDIVN conditon check pending.
   return (fclk >> 2);
  case HDIVN_FCLK_BY_3:
   //TODO: CAMDIVN conditon check pending.
   return (fclk / 3);
 }

 return fclk;
}

uint32_t get_pclk(uint32_t BA)
{
 uint32_t hclk = get_hclk(BA);

 switch(get_clock_pdivn(BA)) {
  case PDIVN_HCLK_BY_1:
   return hclk;
  case PDIVN_HCLK_BY_2:
   return hclk >> 1;
 }

 return hclk;
}

uint32_t get_uclk(uint32_t BA)
{
 uint32_t uclk = get_upll_clk(BA);

 switch(get_clock_upll_divn(BA)) {
  case DIVN_UPLL_BY_1:
   return uclk;
  case DIVN_UPLL_BY_2:
   return uclk >> 2;
 }

 return uclk;
}


Note that the FCLK forms the base for the HCLK i.e. we divide the FCLK to get the HCLK. Next for the PCLK, HCLK forms the base i.e. we divide the HCLK to get the PCLK.

For the UCLK we have the UPLL and hence we check the UDIVN and divide to get the final clock.

So there you have it. The clock setup for the S3C2440.

Time is an illusion
--Albert Einstein

Procuring and sourcing electronic components in India

The one thing that an embedded engineer needs is a lot of electronic components and boards and a decent low cost lab at home. I will write about my sources in procuring and sourcing components in India so that it helps others looking for answers to this.

The first thing that comes to mind for electronics enthusiasts in Bangalore is SP Road. Here you can get a lot of electronic components. I generally go to Vishal electronics but you should explore different shops and find what is needed and select what best fits you. Also do not forget to bargain. Before going there prepare a list and shop fast. The parking situation is horrible and your vehicle can get towed for no reason.

Some of the things you can buy in cheap for a small home lab setup are:
  1. Discrete components. (Resistors, capacitors, transistors, LED's etc). You would generally get through hole types but sometimes SMD components as well. There might be defective parts hence beware. Also since the handling of different components is really questionable be prepared for ESD issues in components.
  2. Soldering station and different solder tips.
  3. Magnifying glass. (Really need this to inspect your soldering and also seeing tiny parts in your PCB's).
  4. Cleaning alcohol solutions
  5. Cables, solder, solder paste  and solder braids (for de-soldering).
  6. Tweezers for holding the components in place while soldering so that you don't burn your fingers.
  7. Bread boards.
  8. Jumper cables. 
  9. Various interface cables such as RS232, USB to serial cables, Straight and cross cables etc. You get the choice of crimping done there.
I generally buy the above stuff in SP road. I generally tend to stay away from buying micro-controller’s or development boards from them because of poor handling but I have started looking at cheaper micro-controllers there.

Apart from this we have some pretty good online stores in India. Some of them are mentioned below:

  1. Tenet Technetronics ( http://tenettech.com  ) -- Slow delivery. I got my two MINI2440's from here. Apart from that I got myself a Nooelec SDR.
  2. Protocentral ( https://www.protocentral.com ) --  Fast delivery. Quite costly for simple items. You can find a lot of sparkfun and adafruit designs here.
  3. Kits 'n' spares ( http://kitsnspares.com ) -- You get element14 parts as element14 requires you to have a TIN number. Delivery is slow and also they are not organized. You have to follow up them with your orders or it will take a really long time for them to deliver. I got my MSP430 boards from here.
  4. Crazypi ( https://www.crazypi.com/ ) -- Misleading name as you would think they deal with only Raspberry Pi solutions. They deal with ARM SBC's. I got my  TI ARM Sitara based beaglebone from here. You can either go directly to their store or you can order from their website. Good service.
  5. Innovate solutions ( https://www.innovatesolutions.net/ ) -- Deals with ARM SBC's and microcontrollers, debuggers etc.  I got my i.MX6 based wandboard from here. You can also walk into their store to buy your items.
  6. Rhydo labz ( http://www.rhydolabz.com/ ) -- Personally I have not bought anything from here but heard they are good. You have a good selection of components and boards.
  7. Amazon -- Good passive kits such as resistors, capacitors and transistors
  8. Quad Store ( https://quadstore.in/ )  -- Very good collection for different sensors
For some of the boards I bought it directly from the vendor store or their partners. Some of them are:

  1. TI eStore ( https://store.ti.com/ ) -- Expensive and they only accept credit cards. Good board packaging so no worry about ESD's. Surprisingly fast delivery. I bought my MSP430 launchpads as well as MSP430 through hole package sample chips from them.
  2. Coreel Technologies ( http://www.coreel.com/ ) -- I bought a FPGA Digilent Basys 2 board from them. Expensive but they offer guarantees and warranties. Good packaging.

It is a good idea to invest some money on an oscilloscope. I have a Rigol  DS1102E two channel 100MHz oscilloscope. It is a really nice scope for a low price compared to a Tektronix or an Agilent. It costed me Rs 31500. You can buy it when they offer discounts. I bought it from Salicon Tech ( http://www.salicontech.com ). The prices are slightly expensive but the service is good. They provide after sales service provided you bear the cost. (Shipping + repair).

Apart from this you can always order free samples of different chips from different manufacturers and vendors to try it out in your designs. I got some free samples from FriendlyARM which I have detailed in my previous posts.

Apart from these have good relations with the vendor FAE's and always ask/bug them for development boards/parts whenever you meet them. They generally have some boards/chips lying around or they can offer you some refurbished boards or they will have connections which will allow you to get some boards/chips/parts at massive discounts.

Hope I have covered most of the things. I will edit this post from time to time if I find good vendors.

“Artists work best alone. Work alone.”
― Steve Wozniak,
iWoz