ECE 320 · Week 8
Modern Computer: Embedded Systems
A mocked bus returns a sample the library asked for.
Week 8 of 10 · Das Frite
ECE 320 · Week 8 · In class
A mocked bus returns a sample the library asked for.
- A mismatched baud scrambles a byte.
- A kernel module is not accepted.
- The mock lets the test run with no chip on the wires.
- The mismatched baud is a capture that is not the byte you sent.
ECE 320 · Week 8 · In class
A mocked bus returns a sample the library asked for. (continued)
- The library stays in userspace.
ECE 320 · Week 8 · In class
Loop a UART to the bench PC at two baud rates.
- Write the userspace library for the MCP3008 and the mock test.
- The homework is those two sentences: what the mock returns, and what the mismatched baud does to a byte.
ECE 320 · Week 8 · Lecture
A driver in userspace
- The kernel's generic
spidevdriver gives a device file,/dev/spidevB.Cfor bus B, chip select C. - Your library turns "read channel n" into bytes on the wire, from the part's datasheet. A kernel module is not accepted.
- Reach the bus only through one function pointer, so a test can swap in a fake bus:
struct bus {
int fd; /* open("/dev/spidevB.C", O_RDWR) */
int (*xfer)(struct bus *b, const unsigned char *tx,
unsigned char *rx, unsigned len);
};
ECE 320 · Week 8 · Lecture
The real bus: one spidev transfer
/* needs string.h, sys/ioctl.h, linux/spi/spidev.h */
int spidev_xfer(struct bus *b, const unsigned char *tx,
unsigned char *rx, unsigned len)
{
struct spi_ioc_transfer t;
memset(&t, 0, sizeof t);
t.tx_buf = (unsigned long)tx;
t.rx_buf = (unsigned long)rx;
t.len = len;
if (ioctl(b->fd, SPI_IOC_MESSAGE(1), &t) < 0)
return -1;
return 0;
}
ECE 320 · Week 8 · Lecture
The mocked bus
struct mock {
struct bus bus; /* first, so the casts work */
unsigned char reply[3]; /* what the "chip" sends back */
unsigned char sent[3]; /* what the library sent */
};
int mock_xfer(struct bus *b, const unsigned char *tx,
unsigned char *rx, unsigned len)
{
struct mock *m = (struct mock *)b;
if (len > sizeof m->reply) return -1;
memcpy(m->sent, tx, len);
memcpy(rx, m->reply, len);
return 0;
}
ECE 320 · Week 8 · Lecture
A UART byte, and a baud mismatch
| Idle | Start | Data | Stop |
|---|---|---|---|
| high | one bit, low | 8 bits, least significant first | one bit, high |
- There is no clock wire. The receiver times every bit from the start edge at its own baud rate.
- When the two ends disagree, the receiver samples in the wrong places.
stty -F /dev/ttyUSB0 9600 raw -echosets the bench PC's side.
ECE 320 · Week 8 · Lab
Lab
- Loop a UART to the bench PC at two baud rates.
- Write a userspace driver library for the MCP3008, with a test on a mocked bus. A kernel module is not accepted. Second pass: run the mock test once more and keep the second result in your notes.
- Assemble the lab hand-in: The userspace driver library, its test on a mocked bus, and the UART capture at the mismatched baud.
ECE 320 · Week 8 · Lab
Hand in
The userspace driver library, its test on a mocked bus, and the UART capture at the mismatched baud.
ECE 320 · Week 8 · Homework
Homework
- Write what a mocked bus returns when the library asks for one sample, and what a mismatched baud does to a byte.
ECE 320 · Week 8 · Homework
Turn in
Those two sentences.
ECE 320 · Week 8 · Read
Read
On the lab site: Module 11, Drivers, introduces the driver model. This week grades the userspace library, the mock test, and the bad-baud capture. Stop before a kernel module.
ECE 320 · Week 8 · Next
Week 9
Which of the two buses comes up first, and why.