ECEC 201 · Week 8

Modern Computer: Von Neumann

A register is a byte whose bits mean different things.

Week 8 of 10 · Das Frite

ECEC 201 · Week 8 · In class

A register is a byte whose bits mean different things.

ECEC 201 · Week 8 · In class

The station sensor is on the I2C bus the image already enables; the lecture names its device file.

ECEC 201 · Week 8 · Lecture

A register byte is several fields

Bits76543210
FieldMODEunusedREADYCOUNT

ECEC 201 · Week 8 · Lecture

Masks and shifts in C

#define MODE_MASK   0xC0u
#define MODE_SHIFT  6
#define READY_MASK  0x08u
#define COUNT_MASK  0x07u

unsigned mode  = (reg & MODE_MASK) >> MODE_SHIFT;
int      ready = (reg & READY_MASK) != 0;
unsigned count =  reg & COUNT_MASK;

reg |= READY_MASK;    /* set a bit   */
reg &= ~READY_MASK;   /* clear a bit */

ECEC 201 · Week 8 · Lecture

volatile and byte order

ECEC 201 · Week 8 · Lecture

One register through the I2C device file

/* needs fcntl.h, unistd.h, sys/ioctl.h, linux/i2c-dev.h */
int read_reg(const char *dev, int addr, unsigned char reg)
{
	unsigned char val;
	int ok = 0, fd = open(dev, O_RDWR);

	if (fd < 0)
		return -1;
	if (ioctl(fd, I2C_SLAVE, addr) == 0 &&
	    write(fd, &reg, 1) == 1)
		ok = read(fd, &val, 1) == 1;
	close(fd);
	return ok ? val : -1;
}

ECEC 201 · Week 8 · Lab

Lab

  1. On the I2C bus the image already enables, through the device file the lecture names, read one register of the station sensor with `ioctl` and `read`. Do not edit a device tree.
  2. Decode its fields with masks and shifts, using the sensor's datasheet. Second pass: print the raw byte in binary beside the decoded fields.

ECEC 201 · Week 8 · Lab

Lab (continued)

  1. Assemble the lab hand-in: The program, the register address, the raw byte, the decoded fields, and the datasheet page you used.

ECEC 201 · Week 8 · Lab

Hand in

The program, the register address, the raw byte, the decoded fields, and the datasheet page you used.

ECEC 201 · Week 8 · Homework

Homework

ECEC 201 · Week 8 · Homework

Turn in

The mask table.

ECEC 201 · Week 8 · Read

Read

On the lab site: Module 10, I/O, decodes a bus on an analyser. This week grades a C read of one register and its decoded bits. Stop before the analyser capture.

ECEC 201 · Week 8 · Next

Week 9

A GPIO line is requested from the Linux character device, then set or read.

Arrow keys or space to move, F for full screen, N for speaker notes, Home and End for the first and last slide. Print gives one slide per page.