Friday, April 7, 2017

WCE7 : Stamp license key


From Visual Studio

After you building a Release version of NK.Bin, the license key can be "stamped" using Platform Builder :

Here are the steps:
  1. Make sure that you have selected a Release configuration of your OS design to build a run-time image from. (WINCESHIP option shall be set to 1 in project properties. It defines the compile time flag -DSHIP_BUILD)
  2. From the Build menu, verify that Make Run-Time Image After Build and Copy Files to Release Directory After Build are selected.
  3. From the Build menu, select Build and Sysgen to build your run-time image.
  4. From the Tools menu, select Platform Builder for CE 7.0, and then select License Run-Time Image to stamp your .bin file with the license key.
  5. In the License Run-Time Image dialog box, in the Run-time key field, enter the run-time key.
  6. Browse to the location of the run-time image file that you want to license, and then click License.

Automatic

Not tested
Create a text file license.txt with the following content :
<license key> 00000000 00000000 00000000 00000000 00000000
here is an example:
OKJB4-098HN-HFR8F-HBVC8-JF789 00000000 00000000 00000000 00000000 00000000
The trailing 5 DWORDs are OEM-specific.
Run the following command in the release directory:
stampbin -1 -i NK.bin license.txt
Verification:
viewbin -t nk.bin >binaryInfo.txt
Look for ROMHDR and check that PID [0-4] contains the license key:
ROMHDR Extensions -----------------------------
    PID[0] = 0x11111111
    PID[1] = 0x22222222
    PID[2] = 0x33333333
    PID[3] = 0x44444444
    PID[4] = 0x00000000
    PID[5] = 0x00000000
    PID[6] = 0x00000000
    PID[7] = 0x00000000
    PID[8] = 0x00000000
    PID[9] = 0x00000000

WCE7 : Connection loss with device on breakpoint

Connection with device lost on Breakpoint

After a successful deployment of the application to the remote device, the application starts (no breakpoint set so far).
However, if the user sets his first breakpoint in code, the debugging session immediately ends due to a "Connection lost" issue.
There is a fix from Microsoft to resolve this issue with WinCE7:

http://www.microsoft.com/en-us/download/details.aspx?id=11935

Install the package and it will resolve the problem.

TPS65217 : The power management chip on BBB

TPS65217 is mounted in its C revision on BeagleBlone Black rev C and BeagleCore. This chip is in charge of power up/ power down and handles the voltages needed by the board.
For this note, we will ignore the push button action on the chip as we are not using it.

 

Features

TPS65217 provides:
  • 3 step down converters (up to 1,2A output each)
  • 2 LDOs (+ 2 load switches configurable as LDOs): 1&2: 100mA, 3&4: 400 mA (rev C)
  • 1 linear battery charger
  • White LEDs driver
  • Power path
It can be configured through its i²C interface:
  • all rails, power swtches, LDOs can be enabled/disabled
  • power up/down sequences can be programmed (non-persistent, must be programmed from chip startup)
  • over temperature limits

Power up/down sequence

Power up

The default power up sequence can work with any type of application. It is defined by strobes and delay times. On each strobe (from 1 to 7) the rail that is attached to the strobe is powered up. After a delay (from 1 to 10 ms), the next strobe occurs and so on. Several rails can be attached to the same strobe but a rail can be attached to 1 strobe only.
The power up sequence occurs when any power source is ON (USB/AC adaptor) and PWR_EN pin is asserted.

Power down

It follows the reverse power up sequence but with STROBE7 occuring first and continuing down to STROBE1.
If the application requires all strobes to occur at the same time (no delay), INSTDWN bit in SEQ6 shall be asserted.
The power down sequence occurs when any of the following events occurs:
  • SEQDWN bit is set
  • PWR_EN is pulled low
  • nRESET is pulled low
  • A fault occurs in the IC

Strobe 14/15

The strobes are not handled by the sequencer. They are used to control rails that are always ON and shall be powered up as soon as the device exits OFF state. The strobe can only be assigned for LDO1 and LDO2 rails.
For power down, the strobes occurs only if the OFF bit is set which means that they are still active in SLEEP mode.

 

Power good

These signals are here to indicate if an output is in regulation or at fault. They are monitored at all time and if a fault occurs, all output rails are powered down and the device enters OFF state.

Power path

The power path allows simultaneous and independend charging of the battery and powering of the system. The battery can be charged from USB or AC.
The power path prioritizes the AC over USB and both over battery input to power up the system.

 

Device functional modes

Mode Description
OFF PMIC is completely shut down with the exception of AC/USB monitoring inputs. To enter OFF state, OFF bit in STATUS register shall be asserted and then, PWR_EN pin whall go low. OFF state can only be enetered from ACTIVE state
ACTIVE All power rails are operational, and i²C is active.
SLEEP Low power mode. All power rails are turned OFF with the exception of LDO1 (but with a current of 1mA). To enter SLEEP state, OFF bit in STATUS register shall be de-asserted (0) and then, PWR_EN pin whall go low.
RESET All power rails are shut down and registers are reset. Stays in this state for 1 sec before returning to ACTIVE.


Block diagram


Power signals assignments on BBB (non-exhaustive)

Output Mapping Function
L1_VDCDC1 VDDS_DDR SDRAM
L2_VDCDC2 VDD_MPU
L3_VDCDC3 VDD_CORE
LS1_OUT VDD_1V8 SDRAM / MPU / ADC / USB / HDMI
LS2_OUT VDD_3V3A eMMC / EEPROM / USB / HDMI / LCD / nRESET / (BCM1 : Power Led) / HDMI / I2C0 (pull-up)
VLDO1 VIO / VRTC / VDD_S nRESET / Ext. Wakeup
VLDO2 VDD_3V3AUX Power Led
SYS1 & SYS2 SYS_5V User Leds / HDMI / USB
Moreover, the signal VDD_3V3B is created from SYS_5V and VDD_3V3A (Enable). It is used for : JTAG / UART0 / µSD / LAN / HDMI CEC.

WCE7 : Disable the Input panel (SIP)

SIP=System Input Panel.
It is the official virtual keyboard that pops up in windows CE when we are in edition mode.


To disable it, the registry need to be edited. The following key needs to be changed:
[HKEY_CURRENT_USER\ControlPanel\SIP]
"TurnOffAutoDeploy"=dword:0
to
[HKEY_CURRENT_USER\ControlPanel\SIP]
"TurnOffAutoDeploy"=dword:1
When the modification has been saved, the input panel won't show up anymore.

Hard drive layout and boot requirements

How does software boot from hard drive ?
What are the requirements to boot any operating system from a hard drive ?
What means MBR, FAT, cluster, sector ... ?

 

MBR, the cornerstone

The Master Boot Record is a memory zone located at:
  • Logical address 0 (if the hard drive uses LBA)
  • Cylinder 0, Head 0, Sector 1 (if the hard drive uses CHS addressing. Used by old hard drives)
which contains the OS boot code (i.e. bootstrap) and a partition table.
This memory area has a size of 512 bytes.
mbr.gif
Basically, if anything happens to this memory zone, the hard drive is brain dead.
Usually, the machine will execute the bootstrap which is the 1st level bootloader that scans the partition table, finds the active partition and jumps to it where the 2nd level bootloader shall be located at address 0. Others, like embedded systems where the CPU has its own 1st level bootloader, will read directly the partition table and achieve the same task.
partitionentry.jpg
All partition have a beginning address, ending address, a size given in sectors.
By reading this table, a machine locates the active partition, and jumps to its starting address where another boot record is located. Basically, a boot record is present at the first sector of each partition.

 

Wait, a sector ? What is this ?

When speaking about hard drives, the same vocabulary is always used:
  • sector: amount of bytes (almost always 512 byte for compatibily purpose)
  • cluster: amount of sectors. This number depends on the disk format settings and can be read from boot record.
Note: When working with non-volatile memories like eMMC, another unit is sometimes uses : block. If the memory is formatted as a bootable hard drive, a block equals a sector.
The cluster is the unit used by file systems.

 

FAT

File Allocation table is one of the disk formats used by computers (old Windows mashines, boot disks for some linux machines, ...).
There are 3 types of FAT that are commonly used : FAT12, FAT16 and FAT32.
Here is an overview of the layout of a FAT disk:
fatlayout.jpg
One of the biggest difference between FAT32 and older FAT layouts is that the root directory section is now part of the data area.

 

OK about FAT layout, but where is the MBR ?

In the reserved area, the first sector of a FAT disk is ... the MBR !
Here are the settings that are registered in the boot code for a FAT12 disk:
Bytes Description
0-2 Jump to bootstrap Legacy bytes almost always equals to 0xeb 0x3C 0x90 where 0xEB is the jump instruction for x86 machine (sometimes 0xEA is used instead)
3-10 OEM Name / Version (string)
11-12 Number of bytes per sector (512,1024,2048,4096)
13 Number of sectors per cluster (1,2,4,8,16,32,64,128)
14-15 Number of reserved sectors (FAT32 -> 32, others -> 1)
16 Number of FAT copies
17-18 Number of root directory entries (0 for FAT32, 512 recommended for FAT16)
19-20 Total number of sectors in the filesystem (N/A for FAT32)
21 Media descriptor type (0xF0: Floppy disk, 0xF8: Hard disk)
22-23 Number of sectors per FAT (0 for FAT32)
24-25 Number of sectors per track
26-27 Number of heads (2 for a double sided floppy disk)
28-29 Number of hidden sectors
30-509 Bootstrap
510-511 Signature, always equal to 55h AAh (signature of the MBR)
Here are the FAT16 extensions:
Bytes Description
11-27 Identical
28-31 Number of hidden sectors
32-35 Total number of sectors in the filesystem (replaces byte 19-20 of FAT12)
36 Logical drive number
37 Reserved
38 Extended signature
39-42 Serial Number of partition
43-53 Volume label or no name (string)
54-61 File system type ("FAT32 ", "FAT16 ", "FAT ")
62-509 Bootstrap
510-511 Signature 55h AAh
Here are the FAT32 extensions:
Bytes Description
11-35 Identical
36-39 Sectors per FAT
40-41 Mirror flags (Bits 0-3: number of active FAT, Bits 4-6: reserved, Bit7; 1=single active FAT, 0=all FATs are updated at runtime)
42-43 Filesystem version
44-47 First cluster of root directory (usually 2). This is the important improvement of FAT32. Now the root directory area is not at a fixed place with a fixed size and grow.
48-49 Filesystem information sector (FSINFO) in reserved area (usually 1)
50-51 Backup boot sector location (can be 0)
52-63 Reserved
64 Logical drive number
65 Reserved
66 Extended signature
67-70 Serial number of partition
71-81 Volume label
82-89 Filesystem type ("FAT32 ")

The FAT area

A FAT is table with one entry per cluster. Each entry has a size of:
  • 12 bits for FAT12
  • 16 bits for FAT16
  • 32 bits for FAT32
Each entry specified if the cluster has data in it or if there is another cluster following it.
The first 2 entries of a FAT table are unused. The first one contains 0xFFFFFFF8 with 0xF8 the media descriptor while the other one contains the end of file marker (0xFFFFFFFF).
The FAT area is split in two :
FAT #1 FAT #2
where FAT #2 is a mirror of FAT #1.

 

FSINFO

File system information sector is located at logical sector 1 (in reserved area).
The purpose of this sector is not clear.
Offset Description Size
00h First Signature (52h 52h 61h41h) 1 Double Word
04h Unknown, Currently (Mightjust be Null) 480 Bytes
1E4h Signature of FSInfo Sector(72h 72h 41h 61h) 1 Double Word
1E8h Number of Free Clusters (Setto -1 if Unknown) 1 Double Word
1ECh Cluster Number of Clusterthat was Most Recently Allocated. 1 Double Word
1F0h Reserved 12 Bytes
1FCh Unknown or Null 2 Bytes
1FEh Boot Record Signature (55hAAh) 2 Bytes

Root directory

This area contains an entry for each file located at the root of the filesystem. Every entry has the following properties:
Offset Description Size
00h Filename 8 bytes
08h Filename extension 3 bytes
0Bh File attributes 1 byte
0Ch Reserved 10 bytes
16h Time created or last updated 2 bytes
18h Date created or last updated 2 bytes
1Ah Starting cluster for this file 2 bytes
1Ch File size in bytes 4 bytes
A subdirectory is a file with specific attributes (byte 0Bh).

WCE7 : Read CPU registers on BeagleBone

Sometimes, it may be useful to watch the CPUs registers to confirm that a peripheral is configured properly or to monitor a signal change from the CPU point of view.
Here are some hints to achieve that easily.

Monitor MPU registers

MPU is the central processing unit in charge of interrupt handling. To read its registers, use the following code:
#include "am33x_irq.h" 
#include <am33x_interrupt_struct.h>

// Interrupt context
static AM33X_INTR_CONTEXT  s_intr;
AM33X_INTR_CONTEXT const *g_pIntr = &s_intr;

void somefunction(void)
{
   UINT32                        irqSIR, irqINTC_ITR_3, irqPENDING_IRQ3;
   PHYSICAL_ADDRESS pa;

   // Get interrupt controller and GPIO registers' virtual uncached addresses
    pa.QuadPart = GetAddressByDevice(AM_DEVICE_MPU);
    s_intr.pICLRegs = (AM33X_INTC_MPU_REGS*)MmMapIoSpace(pa, sizeof(AM33X_INTC_MPU_REGS), FALSE);
    if (s_intr.pICLRegs == NULL)
    {
        DEBUGMSG(1, (L"ERROR: Am3xxGpioInit: Failed map MPU controller registers\r\n"));
    }

    irqSIR = INREG32(&g_pIntr->pICLRegs->INTC_SIR_IRQ);
    irqINTC_ITR_3 = INREG32(&g_pIntr->pICLRegs->INTC_ITR3);
    irqPENDING_IRQ3 = INREG32(&g_pIntr->pICLRegs->INTC_PENDING_IRQ3);

    // Activate IRQs for GPIO0A
    //OUTREG32(&g_pIntr->pICLRegs->INTC_ILR[96], 0U); // Route to IRQ and set max priority
    //OUTREG32(&g_pIntr->pICLRegs->INTC_MIR3, irqMIR3 | 0x1);
}

 

Monitor GPIO registers

#include "am33x_irq.h" 
#include <am33x_interrupt_struct.h>

// Interrupt context
static AM33X_INTR_CONTEXT  s_intr;
AM33X_INTR_CONTEXT const *g_pIntr = &s_intr;

void  somefuncion(void)
{
    PHYSICAL_ADDRESS pa;

    // Bank 0
    pa.QuadPart = GetAddressByDevice(AM_DEVICE_GPIO0);
    s_intr.pGPIORegs[0] = (AM3XX_GPIO_REGS*)MmMapIoSpace(pa, sizeof(AM3XX_GPIO_REGS), FALSE);
    if (s_intr.pGPIORegs[0] == NULL)
    {
        DEBUGMSG(1, (L"ERROR: Am3xxGpioInit: Failed map GPIO0 controller registers\r\n"));
    }
    // Bank 1
    pa.QuadPart = GetAddressByDevice(AM_DEVICE_GPIO1);
    s_intr.pGPIORegs[1] = (AM3XX_GPIO_REGS*)MmMapIoSpace(pa, sizeof(AM3XX_GPIO_REGS), FALSE);
    if (s_intr.pGPIORegs[1] == NULL)
    {
        DEBUGMSG(1, (L"ERROR: Am3xxGpioInit: Failed map GPIO1 controller registers\r\n"));
    }
    // Bank 2
    pa.QuadPart = GetAddressByDevice(AM_DEVICE_GPIO2);
    s_intr.pGPIORegs[2] = (AM3XX_GPIO_REGS*)MmMapIoSpace(pa, sizeof(AM3XX_GPIO_REGS), FALSE);
    if (s_intr.pGPIORegs[2] == NULL)
    {
        DEBUGMSG(1, (L"ERROR: Am3xxGpioInit: Failed map GPIO2 controller registers\r\n"));
    }
    // Bank 3
    pa.QuadPart = GetAddressByDevice(AM_DEVICE_GPIO3);
    s_intr.pGPIORegs[3] = (AM3XX_GPIO_REGS*)MmMapIoSpace(pa, sizeof(AM3XX_GPIO_REGS), FALSE);
    if (s_intr.pGPIORegs[3] == NULL)
    {
        DEBUGMSG(1, (L"ERROR: Am3xxGpioInit: Failed map GPIO3 controller registers\r\n"));
    }
}

OALPAtoUA vs MmMapIoSpace

Both functions achieve the same goal so why use one instead of the other ?
OALxAtoxA functions are reserved for OAL use and can not be called from drivers (if you try, you will end with a link error saying that OALPAtoVA symbol has not been found). This is also why some parts of code use the following code:
#ifdef OAL
   // OALxAtoxA business
#endif
#ifdef DEVICE
  // MmMapIoSpace business
#endif
MmMapIoSpace is the function to use in drivers.

Thursday, April 6, 2017

WCE7 : Setup tools to tweak the BSP

The following tools need to be installed (in this order):
  • Visual Studio 2008 Professional ENU
  • Visual Studio 2008 Professional SP1
  • Windows Compact 7 : Be sure to check Platform Builder option ! Don't install the binaries for all hardware architecture. Instead, pick ARMv7 only.

Note: You need to a valid license ! If this is the first use, you can register on Microsoft Windows Embedded Compact 7 web page and get a 180 days demo license.

 

OS Customization tools

AutoLaunch4ce

Install Autolaunch v310 (also available in the network directory or downloadable here).
Add to your OS Design from Catalog View, in Third Party->Embedded101->Check all checkboxes but CoreCon. It seems that when started from the AutoLaunch, the deployment speed slows down significantly.
AutoLaunch is a an executable that will start automatically at boot. It will look in registry for executables to run at OS startup.
The following lines will be automatically added to your OS image to start CoreCon and remote display (cerdisp) automatically at startup:
[HKEY_LOCAL_MACHINE\Startup]
    "Process0"="ConmanClient2.exe"     
    "Process0Delay"=dword:1388
    "Process1"="cerdisp -c" 
    "Process1Delay"=dword:2710

The startup delay can be modified to any value (0 included).
If other applications need to be started at boot, simply add them like that:
[HKEY_LOCAL_MACHINE\Startup]
    "Process2"="App_01.exe" 
    "Process2Delay"=dword:3A98
    "Process3"="App_02.exe" 
    "Process3Delay"=dword:4E20

Note: Process0 and Process1 are reserved to CoreCon and remote display. Do not use them for other applications.
Known Issue: If you are running under a french operating system, the postlink.bat script located in autolaunch project will not complete due to a path issue. You will end with a missing file (DeviceAgentTransport.dll in my case) in the global output result.

To resolve this issue, replace
"%Program Files%/Common files"
string with
%COMMONPROGRAMFILES%
in postlink.bat. If you program files directory is named
"Program Files (X86)"
, use this replacement string instead:
%COMMONPROGRAMFILES(x86)%
. Re-launch the build.

 

Credits

http://www.embedded101.com/Blogs/SamuelPhung/entryid/207/Compact-7-Getting-Started-Part-3-Develop-an-OS-Design

 
biz.