CES 520 - WEEK 4 September 12, 2006 -- Preparing for Hardware
Some useful links:
Software implementation issues
- main()
- Accepts no arguments, returns nothing
- Loops forever
- Reset process
- Set-up stack, heap (done by Dynamic C)
- Initialize variables
- Initialize hardware
- Bit widths of data types are platform-dependent.
- May be signed or unsigned.
- It's best to create explicit types like uint8, int32, etc.
- Manipulation of individual bits is very common in embedded systems.
- Bit manipulation is a read/modify/write operation
- The Rabbit uses "shadow registers" since many internal I/O devices are write-only
- Always write to the associated shadow register when writing to a register
- Shadow register address is passed as an argument to functions that access registers
- Shadow register may be read to determine the port's contents
- Some shadow registers are defined in the BIOS
- e.g. "GCSRShadow" is the shadow for the Global Control Status Register
- Use "&", "|", or "^" to clear, set, or invert individual bits
- var & 0xFD; // "11111101" clears bit 1, leaving the others unchanged
- var | 0x02; // "00000010" sets bit 1, leaving the others unchanged
- var ^ 0x80; // "10000000" inverts the most-significant bit, leaving the others unchanged
- Let the compiler do the math
- Use "(1 << bit_number)" to make the bit mask
- Use "~" operator to invert the bit mask
- Some "key" keywords in C
- "Static" variables
- Example: static char c;
- Limits scope of external variables or functions to the source file
- Makes variables within functions permanent between function calls
- "Auto" variables are stored on the stack
- Example: auto char c;
- Variable disappears at the end of the function
- In Dynamic C, all variables are static by default
- If you don't want to allocate permanent storage, you must declare them as auto
- The default can be changed to auto with compiler directive "#class auto"
- "Const" makes a variable non-modifiable
- Example: const char c;
- Dynamic C automatically makes all initialized variables constant (stored in flash)
- "Extern" variables
- Example: extern char c;
- Used to explicitly declare variables to be external
- Must be used if variable is defined in the BIOS or another source file
- "Register" variables
- Example: register char c;
- Tells the compiler that this variable is used frequently - store in a register if possible.
- Pointers to register variables are illegal
- Note: The term baud means "bit per second" only if there is one bit per symbol (the usual case)
- RS-232
- Originally developed in 1962 to interface between a Teletype machine and a modem
- DTE = Data Terminal Equipment (the Teletype)
- DCE = Data Communications Equipment (the modem)
- For modern devices it is not always clear which should be used
- A computer or terminal is usually DTE
- If a camera is connected to a printer, which is DTE?
- The DB-25, 25-pin D-subminiature connector is the original standard
- Male connector with DTE pinout for terminals
- Female connector with DCE pinout for modems
- Pin names are SUPPOSED to be the same on both ends.
- Names based on direction of signal flow with respect to the DTE device
- Other devices may have other combinations of connector and pinout
- Many devices use undefined pins for other functions (e.g. original IBM PC)
- Separate data and handshake signals for transmit and receive: Full duplex is possible.
- Note separate shield (pin 1) and signal ground (pin 7). Should not be connected together.
- The DE-9 nine-pin connector used on PCs is now a TIA standard (TIA-574)
- 8-pin RJ-45 connectors and a number of other connectors are also used
- Two DTE devices may be connected with a "Null modem" cable
- Swaps the corresponding transmit and receive pins
- "Breakout box" connects in series with a cable to help troubleshoot connection problems.
- Includes lights and switches to experiment with cross-connecting wires.
- Voltage levels
- +3V to +15V = logic 0 = "space" = "on"
- -3V to -15V = logic 1 = "mark" = "off"
- Some non-standard devices have TTL-compatible inputs:
- +2 to +15V = logic 0
- +0.8V to -15V = logic 1
- Teletype machines used current loops to power their internal selector solenoids
- 20 mA and 60 mA were standard values for TTY
- 4-20 mA still used in some legacy devices
- Parameters that must match at both ends of the circuit (DTE and DCE):
- Baud rate
- From 50 to 19,200 bps are standard
- Higher rates may be used with short cables
- Number of bits per character. 5 to 8 are typical.
- Parity bit is optional to detect transmission errors.
- Must specify positive, negative, mark, space, or none..
- Start/stop bits
- Used to time asynchronous communications
- Use "space" (logic 0) polarity for start, "mark" (logic 1) for stop
- Start is always one data bit wide
- Minimum width of stop signal can be 1, 1.5 or 2 bits
- Flow control
- Normally asynchronous, although a synchronous mode is defined
- RTS/CTS or DSR/DTR (hardware)
- Xon/Xoff (software)
- Allows 3-wire interface (Ground, TXD and RXD only)
- None
- (On PCs) COM port number
- Limitations of RS-232
- The "standard" is not very standard
- Definition of DTE vs DCE
- Two standard connectors and many non-standard alternates
- Many parameters are not specified in the standard and must match for the interface to work
- Connector, pinout, synchronous vs non-sync, and all parameters listed in previous section
- Flow control is not always consistently applied.
- Unipolar signalling voltage - some devices are compatible, some are not
- Large-amplitude, bipolar voltage swings complicate power supply and driver design
- Non-differential signalling limits noise immunity
- Cable length typically limited to 15m, more with low-capacitance cables.
- Slower than modern serial interfaces, such as USB
- No standard way to connect more than two devices on one bus
- The standard connector is quite bulky
- RS-422
- Like RS-232 but with differential signalling
- Improves noise immunity
- Allows longer cables and higher data rates.
- 10 Mbaud up to 12 meters, 100 kbaud up to 1200 meters
- 0 to +5V nominal signal swing
- Allows multiple receivers but not multiple transmitters on the same bus (multi-drop)
- Often used as an RS-232 extender
- RS-485
- Modern version of RS-422 that allows multi-point applications (i.e. more than two RX/TX on same bus)
- Used for inexpensive local networks
- Linear, point-to-point bus topology
- 35 Mbaud up to 10 meters and 100 kbaud up to 1200 meters
- I2C
- Developed by Philips Semiconductors for on-board communications.
- Multiple boards possible within a closed system
- Philips Semiconductors' I2C Bus specification
- 2-wire (plus ground) interface
- SCL = Serial Clock
- Generated by the processor
- SDA = Serial Data
- Both input and output data
- Open-collector drivers - bus lines have pull-up resistors
- A problem with long bus lines - drivers are difficult to implement
- Three data rate modes
- Standard 100 kbps
- "Bit banging" - I/O lines controlled directly by software
- Fast 400 kbps
- Most current devices support this rate
- High Speed 3.4 Mbps
- Generally requires hardware support
- Multiple devices can share the same bus
- Bus state transitions
- Data on SDA changes only while SCL is low
- If SCL is high:
- START condition signalled by high-to-low transition of SDA
- STOP or END condition signalled by low-to-high transition of SDA
- Data sequence
- Start signal
- 7-bit device address (newer high-speed and fast-mode devices support 10-bit address)
- Often lower bits are configurable to allow more than one identical device
- Read/Write bit
- Acknowledge bit (from I/O device to processor)
- N-bit internal address, specific to the peripheral
- Acknowledge bit (from I/O device to processor)
- 8 bits of data (either input or output)
- Acknowledge bit (generated by I/O device for writes, by processor for reads)
- Some devices permit muliple data/acknowledge cycles in one transfer
- Stop signal
- Clock stretching
- Slow slave device holds clock line low to slow down the processor
- Multi-master with I2C
- Both masters may send at the same time
- When one tries to send a high and the other a low, the one sending low wins
- Microwire and SPI
- Microwire developed by National Semiconductor for their COPS processor family,
- SPI developed my Motorola for their 68HC11 processor family.
- Like I2C, both are intended for short-distance, on-board communications.
- 3-wire interface
- SI = Serial Input
- SO = Serial Output
- SK = Serial Clock
- Data capured on rising clock edge. (SPI has option for falling edge)
- Compatible with almost any serial peripheral (with the proper software)
- No device addressing - each device has a chip select input
- CS is negative for Microwire, may be either polarity for SPI
- Speeds up data transfer at cost of extra I/O pins
- Max around 3 Mbps for Microwire and 10 Mbps for SPI
- Some peripheral devices clock input and output data at the same timie
- You must read output data after each input bit is clocked in or the data will be lost
- Number of bits and the meaning of each bit depend on the specific peripheral
- USB
- Originally intended for PCs, but now used for many other devices
- Uses a tiered-star topology
- Host controller connects to multiple daisy-chained devices or hubs
- The root hub connects to the controller
- Each hub can daisy-chain to additional hubs
- Maximum 5-level daisy chain
- Maximum 127 devices per host controller
- Each device can have up to 32 logical pipes to 32 endpoints
- Each endpoint is unidirectional - 16 in each direction
- Unlike RS-232, the USB standard specifies several layers of software protocols
- Plug and play
- 1. Host detects a pull-up resistor on a data line in the device
- 2. Host enumerates the device (gives it a unique 7-bit address)
- 3. Host loads the device driver automatically
- Standard device drivers for up to 253 device classes.
- Examples: HID (keyboard/mouse), printer, mass storage, wireless controller, etc.
- Or custom driver for devices that do not support any standard device class
- Host polls devices in round-robin fashion
- So-called interrupt transfers allow device to signal host for service on next poll
- Isochronous transfer mode available for high-speed data transfer with bounded latency
- Error detection but no guaranteed delivery of data
- Bulk transfer has lower bus priority than isochronous but guarantees correct data delivery
- 4-pin connectors
- Type A receptacle on host, type B on device or hub
- Mini connectors also available
- "Mini-AB" connector allows a device to be either a host or device (USB On-The-Go)
- Connectors are cheap and rugged
- Order of connection upon insertion: Ground shield, power/signal ground, signal.
- Maximum cable length 5 meters (extendable with a hub)
- 5V at up to 0.5A provided on connector
- Devices request 0.1 to 0.5A in 0.1A increments
- Bus-powered hubs have only 0.4A for devices -- maximum 4 devices per hub
- Differential signalling
- Half duplex on single twisted pair
- LOW = 0-0.3V, HIGH = 2.8-3.6V
- NOTE: Certain bus states are indicated by non-differential signals on D+, D-, or both
- Speeds
- Low speed 1.5 Mbps (HID devices)
- Full speed 12 Mbps
- Hi-speed 480 Mbps (only available with USB 2.0)
- USB 2.0 is not necessarily Hi-speed
- For hosts without an operting system libusb is an open-source common library interface
- CAN
- Introduction to the Controller Area Network
- Originally developed by Bosch in 1986 for automotive applications
- Well-suited to harsh mechanical/thermal/electrical environments where high data integrity is necessary
- Now used in many demanding environments - manufacturing, aerospace, etc.
- Multi-drop with differential signalling over twisted-pair for noise immunity
- Up to 1 Mbps
- Hot-plugging allowed
- Multi-master system.
- Carrier-Sense, Multiple Access protocol with Collision Detection (CSMA/CD)
- Arbitration on message priority (AMP)
- Each message has a priority in its identifier field - lowest priority number has highest priority
- If two nodes transmit at same time, the first one with a "1" in the address loses and stops transmitting
- Standard CAN has 11-bit identifier, Extended CAN has 29-bit identifier
- Up to 8 bytes (64 bits) of data per message
- Five levels of error detection, 2 at the bit level, 3 at the message level
- Faulty nodes are automatically removed from the bus.
- Much of the protocol is embedded in the CAN chip, for ease of implementation.
- CAN to RS-232 converters are readily available
- Time-Triggered CAN (TTCAN)
- Uses periodic time slots, each slot reserved for one node
- An add-on to the standard CAN protocol. Uses standard CAN ICs with additional timing hardware.
- Well suited to real-time systems using periodic static scheduling
- Automatic retransmission of corrupted messages must be disabled
- Microchip's I2C EEPROM
- Note that EEPROM is not suitable for general-purpose RAM
- Speed typically 400 kbps
- Limited number (1 million) of write cycles
- On power-up load EEPROM data into RAM, only write back if something changes
- Look at data sheet and I2C library
- Different size devices have same pinout and I/O
- How would you implement the EEPROM interface using the I/O ports?
- Dallas Semiconductor 1-wire interface
- How do they transfer data without an accurate clock?
- All commands preceded by a Reset
- Master sends a long (480 us) low pulse
- Any device(s) on bus respond with a 60-240 us pulse
- For each bit to be transmitted, master first sends a short (6 us) low pulse
- For writes:
- To write 0: Holds low for 60 us
- To write 1: Returns high for 60 us
- For reads:
- Master allows line to float high
- Slave outputs 0 or 1 for 60 us
- Look at temperature sensor data sheet
- Each sensor is manufactured with a unique 64-bit ID code.
- Complicated flow charts (pp 12-13) that must be followed exactly
- Simplified if only one sensor on bus (use "Skip ROM" command)
- Library is on the class web page
Assignments:
- Read Embedded Systems Design, Chapter 7
- Read the data sheets for the EEPROM and temperature sensor.
- Lab 3: Add the EEPROM and temperature sensor hardware and software. (Due in 2 weeks)