Tuesday 20 June 2017

Embedded

uses of the "volatile" 
- One is to turn off compiler optimaztions that remove loops with global variables.
- It's to inform compiler that the variable may be altered runtime by another process. This is actually pretty useful in debugging using ICD, you can modify volatile variables and the new value will be used in execution in the instruction that's following your modification.

What kind of data structure would you use to store data from a serial receive line?
- A queue (First in, First out [FIFO] )
- Another one would be a ring-buffer, which is sort like a queue

When spinlock is used ?

  1. The thread that holds the lock is not allowed to sleep.
  2. The thread that is waiting for a lock does not sleep, but spins in a tight loop

Rule - 1: Any code that holds the spinlock, can not relinquish the processor for any reason except to service interrupts ( sometimes not even then). So code holding spinlock can not sleep.

Reason: suppose your driver holding spinlock goes to sleep.  If some other thread tries to obtain the same lock, it would spin for very long time. In the worst case it would result in deedlock.
Kernel preemption case is handled by the spinlock code itself. Anytime kernel code holds a spinlock, preemption is disabled on the relevant processor. Even uniprocessor system must disable the preemption in this way.

Rule - 2: Disable interrupts on the local CPU, while the spinlock is held.

Reason: Suppse your driver take a spinlock that control access to the device and then issues an interrupt. If the interrupt handler runs on the same processor, it will start spinning. The driver code also can not run to release the lock. SO the processor will spin for ever.

Rule - 3: Spinlocks must be held for the minimum time possible.

Reason: Long lock hold times also keeps the current processor from scheduling, meaning a higher priority process may have to wait to get the CPU.
So it impacts kernel latency (time a process may have to wait to be scheduled). Typically spinlocks should be held for the time duration, less than that CPU takes to do a contex switch between threads.

Device Tree (DT) :-

 

The Device Tree is a data structure for describing hardware. Rather than hard coding every detail of a device into an operating system, many aspect of the hardware can be described in a data structure that is passed to the operating system at boot time.

every board has to be described in a DTS (foo.dts) file that is compiled via the DTC compiler to a binary format DTB (flattened device tree format) which gets parsed on boot by the kernel and the devices are created. 

   

 Device Drivers


Device controllers are typically connected to the CPU through their respectively named buses (collection of physical lines) — for example, the PCI bus, the IDE bus, etc. In today’s embedded world, we encounter more micro-controllers than CPUs; these are the CPU plus various device controllers built onto a single chip .

The device-specific portion of a device driver remains the same across all operating systems, and is more about understanding and decoding the device data sheets than software programming.  

To dynamically load or unload a driver,
  • lsmod — lists currently loaded modules
  • insmod — inserts/loads the specified module file
  • modprobe — inserts/loads the module, along with any dependencies
  • rmmod — removes/unloads the module
     


SPIN LOCK
---------------

The most basic primitive for locking is spinlock.

static DEFINE_SPINLOCK(xxx_lock);

 unsigned long flags;

 spin_lock_irqsave(&xxx_lock, flags);
 ... critical section here ..
 spin_unlock_irqrestore(&xxx_lock, flags);

The above is always safe. It will disable interrupts _locally_, but the
spinlock itself will guarantee the global lock, so it will guarantee that
there is only one thread-of-control within the region(s) protected by that
lock.

spin_lock_irqsave is basically used to save the interrupt state before taking the spin lock, this is because spin lock disables the interrupt, when the lock is taken in interrupt context, and re-enables it when while unlocking. The interrupt state is saved so that it should reinstate the interrupts again










 

 
 

No comments:

Post a Comment