Wednesday 12 October 2016

Embedded Basics No.1 - Registers

A core concept in embedded-electronics is the register.

For embedded newbies, put simply,

a register is a ‘box’ that stores a number

‘Stores’ means a register is a memory device. Place (embedded programmers say ‘poke’) a number into the ‘box’ (register), and it’s held there until you decide to change it or you power-off the IC.

Other parts of an electronic system may be able to read a number you’ve poked into a register, and adjust their behavior based on its value. Figure 1 shows the general idea. Here a 3-bit register controls 3 LED's. Poking the value '101' turns two LED's on, and poking '010', one LED.



Modern IC’s may contain hundreds of registers, each controlling a different feature of the device. Manufacturers therefore need to give you a way to single-out an individual register. They do this by giving each register a unique name or number termed its “address”. You use the address to send the IC instructions along the lines:

Hi Mr. IC, poke the value 17 to the register with address 82”

Hi Mr. IC, poke 23 to the register TXCTRL”

“Hi Mr. IC, what's the value in register RXBUFF?”

Here “TXCTRL” and “RXBUIFF” are  example register names (its traditional to use uppercase lettering for registers). 

The first two instructions are write-instructions. The third is a read-instruction. Not all registers allow both reads and writes. For example, a manufacturer might include a read-only register in an IC to store a serial number, giving you the ability to read the number, but not over-write it.

Some registers contain both write’able and read-only regions. For example, a register might store an 8-digit (bit) number, but only allow you to write (change) seven of these bits, the eighth bit being permanently fixed (at ‘1’ say). 

It can also be important to know what value may be present in a register before you write anything to it (the default value present in the register immediately after you’ve powered-up the chip for example). 

All the various properties of an chip's registers are stated in the datasheet in a (fairly) standard format. Figure 2 shows a typical (fictitious) example.



In words, the table can be understood as saying:
  • CFG0 is the name of a register name with address "127" (= 7F in hex)

  • The register stores an 8-bit (binary) value. The individual bits are themselves named (LED0, LED1...CTRL).

  • Bits 0 - 4 can be both written (‘W’) and read (‘R’).  

  • Bit 5 - 6 can be read, but not written (changed).

  • Bit 7 can be written but not read.

  • Bits 1,2,3,5,6 have default value '0' 

  • Bits 0,4 have default value '1' 

  • Bit 7  has an undefined ('U') default value
In addition to the table, the datasheet will describe what each section of the register does. For example the datasheet might describe the function of the 'LED0' bit by saying

  • LED0: Writing '1'('0') turns the on-board LED#1 on (off)


(By the way, precisely how you send register-read/write instructions to a chip depends on the device. For some, you might send instructions via an (e.g. SPI or I2C) interface; A microprocessor might have the instruction written into its software. Written in C (you can skip this bit if you don’t know C, but you should be aware that a large proportion of embedded software uses it), the second example instruction above might look something like

*(TXCTRL) = 23

I should add there's a lot more could be said here about pointers, volatiles etc.  but these are software topics for another time).

Finally, at the hardware level, a register is typically just a row of (one or more) edge-triggered D-type flip flops. Unless you’re heavily into transistor-level integrated-circuit design, it’s rarely necessary to understand the inner workings of flip flops, so I won’t get into details here. Wikipedia is the place to start if you’re hungry for more. It’s enough to know that a ‘1’ (or a ‘0’) put on the ‘D’ input of a flip-flop, will be captured and stored if a rising-edge (a voltage step) is applied the ‘Clk’ input. The stored ‘1’ (or ‘0’) will show up on the flip-flop's ‘Q’ output. A row of D-type flip flops allows you store a set of '0’s and ‘1’s – in other words it forms a register.

The ability to arrange and manipulate registers is so important in digital system design (for example, when creating ASIC's or programming FPGA’s) that dedicated hardware design languages (HDL’s) such as VHDL and Verilog are available to help streamline the task. In a language like VHDL, you routinely see lines of code like

If rising_edge(clock) then A <= “10101010”; 

This translates as an instruction to your FPGA / ASIC design- tools that you want a (clocked) register with an output called “A”, storing the value “10101010”.

I’ll end my brief introduction to registers here.

Do drop me a comment on this, or any of the others topics on this blog.

No comments:

Post a Comment