scieee AI-readable full text Open interactive document viewer

Design of a dedicated cape board for an embedded system lab course using beaglebone

Pérez López, Raúl

Abstract

[ANGLÈS] This memory describes the design of an embedded system programming lab course for electronics engineering using BeagleBone board and a dedicated expansion board.

Full text

PROJECTE FINAL DE CARRERA Design of a dedicated cape board for an embedded system lab course using BeagleBone Studies: Electronics engineering Author: Raúl Pérez López Director: Manuel M. Dominguez Pumar Year: 2014 Design a cape board for an embedded system lab course using BeagleBone 2 Index Resum del Projecte .......................................................................................................... 5 Resumen del Proyecto ..................................................................................................... 6 Abstract ............................................................................................................................ 7 1 Introduction ................................................................................................................ 8 1.1 Objectives ........................................................................................................... 8 1.2 Strategy ............................................................................................................... 8 1.3 Project Scope ...................................................................................................... 9 1.4 BeagleBone board ............................................................................................. 10 2 Hardware Cape board TT01 ..................................................................................... 13 2.1 BeagleBone board connection ........................................................................... 13 2.2 Power constraints .............................................................................................. 18 2.3 Cape-bone-TT01v1 peripherals ......................................................................... 18 2.3.1 Cape EEPROM .......................................................................................... 18 2.3.2 General Purpose LEDs .............................................................................. 21 2.3.3 Push button ................................................................................................ 22 2.3.4 Analog input ............................................................................................... 23 2.3.5 Rotary encoder ........................................................................................... 25 2.3.6 LED matrix ................................................................................................. 26 2.3.7 Accelerometer ............................................................................................ 29 2.3.8 Near Field Communication ......................................................................... 32 2.3.9 CAN bus ..................................................................................................... 39 2.3.10 GPMC ..................................................................................................... 41 2.4 Test Points ........................................................................................................ 45 2.5 Printed Circuit Board ......................................................................................... 45 2.5.1 Dimensions ................................................................................................ 45 Design a cape board for an embedded system lab course using BeagleBone 3 2.5.2 Layers ........................................................................................................ 46 2.6 Part List ............................................................................................................. 49 2.7 Cape EEPROM configuration and programming ............................................... 50 2.7.1 EEPROM content ....................................................................................... 51 2.7.2 Generate EEPROM content file .................................................................. 52 2.7.3 Program Cape EEPROM ............................................................................ 62 2.8 Device tree for cape-bone-TT01v1 .................................................................... 62 3 Embedded programming laboratory ......................................................................... 66 3.1 Tool chain configuration .................................................................................... 66 3.2 TT01 Cape Board .............................................................................................. 67 3.2.1 Cape TT01 P8 connector pin out ................................................................ 68 3.2.2 Cape TT01 P9 connector pin out. ............................................................... 69 3.3 Programming Applications ................................................................................. 71 3.3.1 General Purpose Input/Output (GPIO) ........................................................ 71 3.3.2 Analog input ............................................................................................... 89 3.3.3 LED Matrix ................................................................................................. 94 3.3.4 Encoder .................................................................................................... 101 3.3.5 Accelerometer .......................................................................................... 112 3.4 How to fix a static IP in the BeagleBone .......................................................... 124 3.5 Device Tree creation and compilation .............................................................. 126 3.6 Device tree cape-bone-TT01v1 ....................................................................... 130 4 Conclusions ............................................................................................................ 133 4.1 Cape design .................................................................................................... 133 4.2 Open Hardware ............................................................................................... 133 5 Annex I. IDE Eclipse configuration .......................................................................... 134 5.1 Installing a Tool Chain ..................................................................................... 134 5.2 Connecting the BeagleBone ............................................................................ 134 Design a cape board for an embedded system lab course using BeagleBone 4 5.2.1 Terminal connection ................................................................................. 134 5.2.2 Installing ssh ............................................................................................ 136 5.2.3 Defining a host with BeagleBone IP ......................................................... 136 5.3 Installing Eclipse .............................................................................................. 137 5.3.1 Installing Eclipse ...................................................................................... 137 5.3.2 Installing C/C++ Development Tools ........................................................ 139 5.3.3 Installing Additional Features ................................................................... 143 5.4 Creating and Configuring a Project .................................................................. 144 5.4.1 Creation of a New Project ......................................................................... 144 5.4.2 Creating a Build Configuration .................................................................. 146 5.5 Compiling and Running a Program .................................................................. 152 5.5.1 Compiling a Source File in the host PC .................................................... 152 5.5.2 Transferring the executable file to the BeagleBone using a command line 153 5.5.3 Transferring the executable file to the BeagleBone using Eclipse ............. 154 5.6 Remote Debugging ......................................................................................... 164 5.6.1 Installing gdbserver in the BeagleBone .................................................... 164 5.6.2 Setting up a Project for Remote Debugging.............................................. 164 6 Bibliography ........................................................................................................... 172 6.1 Hardware Cape board TT01 ............................................................................ 172 6.2 Embedded programming laboratory exercices ................................................ 172 6.3 IDE Eclipse configuration ................................................................................ 173 Design a cape board for an embedded system lab course using BeagleBone 5 Resum del Projecte Aquest projecte té com a objectiu l’elaboració d’unes pràctiques de programació de sistemes embedded per enginyeria electrònica. Per portar a terme aquestes pràctiques, es fa ús de la plataforma d’open hardware BeagleBone. Aquesta plataforma consisteix en un sistema embedded amb un microcontrolador Cortex-A8 de Texas Instrument Sitara AM3359. La plataforma BeagleBone disposa de connectivitat USB, Ethernet, memòria micro SD, i dos ports d’expansió on estan connectats gran part dels pins del microcontrolador. Amb aquests ports d’expansió, la plataforma BeagleBone, defineix un interface per plaques d’ampliació de funcionalitat. Aquestes plaques reben el nom genèric de Capes. En aquest projecte, es dissenya una placa Cape, que inclou alguns exemples de funcionalitat bàsics en un sistema embedded professional. Per la programació de la placa BeagleBone, i la seva iteració amb el Cape, s’utilitza una distribució adaptada de Linux. Amb aquestes pràctiques s’introdueix aquest sistema operatiu per programació de sistemes embedded. El projecte consta de:  Disseny hardware d’una placa Cape d’ampliació de la BeagleBone.  Disseny firmware d’uns exemples de programació dels perifèrics introduïts amb la placa Cape.  Descripció de la configuració d’un dels entorns de programació (IDE) més estesos per programació del BeagleBone, l’entorn Eclipse. Design a cape board for an embedded system lab course using BeagleBone 6 Resumen del Proyecto Este proyecto tiene como objetivo la elaboración de unas prácticas de programación de sistemas embebidos para ingeniería electrónica. Para llevar a cabo estas prácticas se hace uso de la plataforma open hardware BeagleBone. Esta plataforma consiste en un sistema embedded con el microcontrolador Cortex-A8 de Texas Instrument Sitara AM3359. La plataforma Beablebone dispone de conectividad USB, Ethernet, memoria micro SD, y dos puertos de expansión donde están conectados gran parte de los pines del microcontrolador. A través de esos puertos de expansión, la plataforma BeagleBone, define un interfaz para placas de ampliación de funcionalidad. Estas placas reciben el nombre genérico de Capes. En este proyecto, se diseña una placa Cape, que incluye algunos de los ejemplos de funcionalidad básicos en un sistema embedded profesional. Para la programación de la placa BeagleBone, y su interacción con la Cape, se utiliza una distribución Linux adaptada. Con las prácticas se introduce ese sistema operativo para programación de sistemas embedded. El proyecto consta de:  Diseño hardware de una placa Cape de ampliación de la BeagleBone.  Diseño firmware de unos ejemplos de programación de los periféricos introducidos con la placa Cape.  Descripción de la configuración de uno de los entornos de programación (IDE) más extendidos para la programación del BeagleBone, el entorno Eclipse. Design a cape board for an embedded system lab course using BeagleBone 7 Abstract This Project has as objective the design of a embedded system programming lab course for electronics engineering. The open hardware platform BeagleBone is used for that objective. This platform is an embedded system with Cortex-A8 Texas Instrument Sitara AM3359 microcontroller. This platform has connectivity with USB, Ethernet, micro SD memory, y two expansion ports where most of the pins of the Sitara microcontroller are connected. The expansion headers with the Sitara pins connected, define and interface for expansion boards in the BeagleBone. These expansion boards are called Capes. A Cape board is designed in this project, and it has some of the basics functionalities found in a professional embedded system. The BeagleBone is programmed with Linux. So, the lab introduced the Linux operative system for embedded systems. The project has:  A hardware design of a Cape Board for the BeagleBone.  A firmware design with some examples of Cape board peripherals.  Configuration description for BeagleBone for one of the most popular programming developing tools, the Eclipse IDE. Design a cape board for an embedded system lab course using BeagleBone 8 1 Introduction 1.1 Objectives The project has as objective design an embedded system programming labs using BeagleBone platform, with a Cape board designed in purpose, and Linux as operating system. 1.2 Strategy The open hardware platforms (i.e. BeagleBone, Arduino,...) have a intensive support in the web provided by the open hardware community. It allows to have a free, and easy access to documentation, tutorials, which helps to success in new developments. The huge progress in the open hardware world, the low cost and powerful boards available in the market, justify our decision to chose an open hardware platform, as BeagleBone, as the base platform to design the lab course. From our lab exercises, students can get engaged by the open hardware world, and continue with their own designs. That is the target. The first step in this project is to develop a BeagleBone expansion board which uses some of the most common functionalities in the embedded systems developments:  GPIOs  I2C bus  SPI bus  Memory access The peripherals for this hardware were selected considering the most usual electronics system designs, and the industry fields in the Barcelona area. Those peripherals selected were: Peripherals Justification LEDs Usually used in any user interface as light indicator Push Button The most common way to interface with an user Analog Inputs A generic functionality in most of the embedded systems LEDs matrix A usual user interface strategic to save pins Design a cape board for an embedded system lab course using BeagleBone 9 Peripherals Justification Encoder A way to navigate through menus by the user Accelerometer A common function in mechatronics systems CAN bus The most common field bus in the automotive industry Generic Memory access Generic access to external memories. It is common in complex embedded systems NFC Near Field Communication. A RFID protocol used in most of the last generation mobile phones to implement identification (i.e. for payment) EEPROM Generic memory used in most of the embedded systems to store configuration or manufacturing data This expansion board hardware is called cape-bone-TT01v1, where TT is the acronym of Teaching Tool. 1.3 Project Scope The following figure the Work Breakdown Structure (WBS) of the project: Design a cape board for an embedded system lab course using BeagleBone 16 The expansion header P9 schematic section is: The position of the expansion header P9 is: The P9 connector pin out is: Design a cape board for an embedded system lab course using BeagleBone 17 BEAGLEBLONE TT01 CAPE TT01 CAPE BEAGLEBONE GND GND 1 2 GND GND VDD_3V3EXP 3.3V 3 4 3.3V VDD_3V3EXP VDD_5V NC 5 6 NC VDD_5V SYS_5V 5.0V 7 8 5.0V SYS_5V PWR_BUT NC 9 10 NC SYS_RESETn UART4_RXD GPMC_WT0 11 12 GPMC_BE1N GPIO1_28 UART4_TXD NC 13 14 LED3 EHRPWM1A GPIO1_16 LED1 15 16 LED2 EHRPWM1B I2C1_SCL I2C1_SCL 17 18 I2C1_SDA I2C1_SDA I2C2_SCL I2C2_SCL 19 20 I2C2_SDA I2C2_SDA UART2_TXD UART2_TXD 21 22 UART2_RXD UART2_RXD GPIO1_17 RFWIP/BUSY 23 24 CAN_TXD UART_TXD GPIO3_21 AC_INT2 25 26 CAN_RXD UART_RXD GPIO3_19 AC_INT1 27 28 SPI1_CS0 SPI1_CS0 SPI1_D0 SPI1_MISO 29 30 SPI1_MOSI SPI1_D1 SPI1_SCLK SPI1_SCLK 31 32 VDD_ADC VDD_ADC AIN4 NC 33 34 GNDA_ADC GNDA_ADC AIN6 NC 35 36 NC AIN5 AIN2 NC 37 38 NC AIN3 AIN0 AN_SW 39 40 NC AIN1 CLKOUT2 PB1 41 42 SP1_CS1/RS GPIO1_7 GND GND 43 44 GND GND GND GND 45 46 GND GND Design a cape board for an embedded system lab course using BeagleBone 18 2.2 Power constraints Cape-bone-TT01v1 is powered by the BeagleBone via P9 connector. BeagleBone powers the following voltages:  3.3 V It is powered by the BeagleBone power VDD_3V3EXP. It is supplied by the LDO on the BeagleBone, it is the primary power rail for expansion boards. It can powers up to 250 mA.  5 V It is powered by the BeagleBone power SYS_5V. It is the main rail for the regulators in the BeagleBone board. It can powers up to 250 mA. Both voltages are referenced to the same ground: GND. 2.3 Cape-bone-TT01v1 peripherals 2.3.1 Cape EEPROM This cape EEPROM is used to identify the cape to the BeagleBone. Up to the kernel 3.8, this EEPROM was also used to configure the expansion headers pins. Its content is specified in the BeagleBone System Reference manual. In any case, this EEPROM is required to identify correctly the cape board when it is plugged into the BeagleBone. The Cape EEPROM has the ST Microelectronics reference M24256-BWMN6TP. It has the following features:  256 Kbit (32 Kbytes)  Page size: 64 bytes.  I2C serial access.  Random and sequential Read modes.  Write protect of the whole memory array. Design a cape board for an embedded system lab course using BeagleBone 19  Operation voltage from 2.5V to 5.5V  Package SO8 The schematic of the cape EEPROM is: The EEPROM is write protected by default. The pin 7, Write protection, is set to high level with a pull up. It shall be taken into consideration to program the EEPROM. This pin should be forced to low level during the programming. The following picture shows the position of the cape EEPROM in the cape board: The address of the EEPROM is set by the 2-position switch S1. The address line of the A2 is always high. We can configure the address lines A0, A1 with the switch S1. The Design a cape board for an embedded system lab course using BeagleBone 20 allowable address range for the EEPROM is 0x54 to 0x57. Considering only the EEPROM address factor, it could be possible to plug up to 4 capes into a single BeagleBone. The configuration of the S1 is: S2 S1 EEPROM Address OFF OFF 0x57 OFF ON 0x56 ON OFF 0x55 ON ON 0x54 Therefore the meaning of the switch position is:  ON: '0'  OFF: '1' The user shall ensure to set the proper EEPROM address. For the cape-bone-TT01v1 the EEPROM address is 0x57. The position of the switches are: From the kernel 3.8, the process during the boot to identify the cape board, and configure the pins according that cape, is: 1st. BeagleBone read the Cape EEPROM Part number field. 2nd. Configure the pins according to the overlay device tree file in BeagleBone /lib/firmware directory, which matches with the Part number name. In the annex is described a tool to define and create the EEPROM file, and the procedure to program it. Design a cape board for an embedded system lab course using BeagleBone 21 2.3.2 General Purpose LEDs This general purpose LEDs allows to practice with the general purpose Inputs Outputs (GPIO). These LEDs are controlled by the following signals: LED LED SIGNAL CONTROL P9 BEAGLEBONE PIN GPIO GPIO NUMBER LD1 LED1 15 GPIO1_16 48 LD2 LED2 16 GPIO1_19 51 LD3 LED3 14 GPIO1_18 50 The following figure shows the schematic of the general purpose LEDs: The position of the LEDs in the cape-bone-TT01v1: Design a cape board for an embedded system lab course using BeagleBone 22 The references of the LEDs are: LED REFERENCE MANUFACTURER COLOR DC FORWARD CURRENT VOLTAGE LD1 HSMG-C170 AVAGO GREEN 20 mA LD2 HSMD-C170 AVAGO ORANGE 20 mA LD3 HSMS-C170 AVAGO RED 20 mA The LEDs package are: 2.3.3 Push button There is an input push button (S3) connected to the PB1 signal: SIGNAL CONTROL P9 BEAGLEBONE PIN GPIO GPIO NUMBER PB1 41 GPIO0_20 20 Design a cape board for an embedded system lab course using BeagleBone 23 PB1 is set to low level voltage by default. When S3 is pushed, PB1 is set to high level voltage. The schematic for the push button is: The position of the push button in cape-bone-TT01v1: 2.3.4 Analog input Cape-bone-TT01v1 has an analog input (AN_SW) connected to the BeagleBone ADC channel 0. This signal is connected in the expansion header P9 pin 39. The BeagleBone has a successive-approximation 12 bits ADC. There are 8 ADC channels available. The reference voltage for the ADC is 1.8V, so the voltage equivalent to the ADC counts lecture is: voltage = (number_counts_ADC / (2^12)) * 1.8 The voltage range for the ADC is 0V to 1.8V. Design a cape board for an embedded system lab course using BeagleBone 24 There is a resistor divisor to change the voltage value in that ADC input. With the switch S1, this resistor divisor changes and therefore the voltage level of AN_SW: 1 2 3 AN_SW [V] OFF OFF OFF 1.65 ON OFF OFF 1.1 ON ON OFF 0.825 ON ON ON 0.66 The AN_SW voltage has a 1% tolerance. The schematic for this analog input is: The position of the S1 switch is: Design a cape board for an embedded system lab course using BeagleBone 25 2.3.5 Rotary encoder There is a rotary encoder in the cape-bone-TT01. This rotary encoder could also have a push button as an option. The schematic of the rotary encoder is: The rotary encoder has 2 signals to monitor. The changes in those signals values indicates the direction of the encoder: Measuring the interval of signal changes, it could be possible to know the speed of the turn. Design a cape board for an embedded system lab course using BeagleBone 32 2.3.8 Near Field Communication NFC (Near Field Communication) is a short-range radio technology that enables communication between devices which are held close together. NFC is an open-platform technology, standardized by the NFC Forum. There is a NFC tag implemented with the ST Microelectronics memory M24LR04E- RMN6T. This is a EEPROM with a NFC antenna control. The antenna used for the NFC is a 4.7 uH inductor following ST application note ANT3- M24LR04E (SMD-inductor based antenna reference board for M24LR04E-R). I2C bus is the bus interface to communicate with the EEPROM NFC memory. Some of the M24LR04E-RMN6T features:  Memory  4kbit-EEPROM organized into:  512 bytes in I2C mode  128 blocks of 32 bits in RF mode  I2C interface  Two-wire I2C serial interface, supports up to 400 kHz  random and sequential read modes  automatic address incrementing  single supply voltage 1.8V – 5.5V  Contactless interface  ISO 15693 and ISO 18000-3 mode 1 compatible Design a cape board for an embedded system lab course using BeagleBone 33  13.56 MHz +- 7kHz carrier frequency  Digital output pin  User configurable pin: RF write in progress or RF busy mode  Energy harvesting  Analog pin for energy harvesting  sink current configurable ranges The following figure illustrate the M24LR04E-RMN6T functional diagram: The following figure shows the NFC schematic implementation: The position of the NFC implementation: Design a cape board for an embedded system lab course using BeagleBone 34 The following topics are about NFC implementation: 2.3.8.1 Tag and reader NFC communication between two devices is possible when one devices acts as reader/writer and the other as tag. The tag is a simple, thin device which contains an antenna and a small amount of memory. It is a passive device, powered by a magnetic field. Depending on the tag type, the memory can be read only, rewritable, or writable once: The reader is an active device that generate radio signals and communicates with the tags. The reader powers the passive device when the two are engaged in the passive mode communication. An example of reader is a NFC enabled mobile phone. Design a cape board for an embedded system lab course using BeagleBone 35 2.3.8.2 Communication modes NFC devices support two communication modes: Active mode. In this mode the target and the initiator devices have power supplies and can communicate with other one by alternate signal transmission. Passive mode. In this mode, the initiator devices generates radio signals and the target device is powered by this electromagnetic field. The target device responds to the initiator by modulating the existing electromagnetic field. 2.3.8.2.1 Operating mode NFC can operate in three different modes:  Read / write. In this mode, the NFC-enabled phone can read or write data to any of the supported tag types in a standard NFC data format.  Peer to peer. In this mode, two NFC-enabled devices can exchange data.  Card emulation. An NFC-enabled phone acts as a reader when in contact with tags. In this mode, the phone can act as a tag or contactless card for existing readers. 2.3.8.2.2 NFC Data Exchange Format (NDEF) To enable interoperability when transferring data to and from tags and between NFC devices, the NFC Forum has specified a common data format known as the NFC Data Exchange Format (NDEF). The NFC Data Exchange Format (NDEF) specification defines a message encapsulation format to exchange information. NDEF is a lightweight, binary message format that can be used to encapsulate one or more application-defined payloads of arbitrary type and size into a single message. Design a cape board for an embedded system lab course using BeagleBone 36 An NDEF message is composed of one or more NDEF records: A NDEF record contains a specific payload, and have the following format that indicates the content and size of the record: 7 6 5 4 3 2 1 0 MB ME CF SR IL TNF TYPE LENGTH PAYLOAD LENGTH ID LENGTH RECORD TYPE ID PAYLOAD The NDEF Record Header, the byte 0 of the NDEF record, has following flags:  Type Name Format (TNF) 3 bits value that describes the NDEF record type, its content type. Possible TNF field possible values are: TNF value NFC Record type. 0x00 Empty record. Design a cape board for an embedded system lab course using BeagleBone 37 No type. It is useful to format empty cards. 0x01 Well-known record. The type field contains a value that follows the RTD type name format defined in the NFC Forum RTD specification, such as RTD Text, RTD URIs,... It is one of the most used record types. 0x02 MIME media record. The payload is an intermediate or final chunk or a chunk NDEF. 0x03 Absolute URI record. The type field contains an absolute Uniform Resource Identifier (URI). 0x04 External record. Indicates the type field contains a value that follows the RTD external name specification. 0x05 Unknown record. The payload is unknown. 0x06 Unchanged record. The payload is an intermediate or final chunk or a chunk NDEF.  IL: IDE Length field IL Function 0 ID Length field is omitted 1 ID Length field is not omitted  SR: Short Record bit SR Function 0 The payload length field is bigger than 1 byte. 1 The payload length field is 1 byte or less. It means payload length is 255 bytes as maximum.  CF: Chunk flag Design a cape board for an embedded system lab course using BeagleBone 38 CF Function 0 It is not the first record chunk or a middle record chunk. 1 It is the first record chunk or a middle record chunk.  ME: Message End ME Function 0 It is not the last record in the message. 1 It is the last record in the message.  MB: Message Begin MB Function 0 It is not the start record in the message. 1 It is the start record in the message. The NDEF record byte 1 indicates the length in bytes of the Record Type field. The NDEF record byte 2 indicates the payload length in bytes. If the SR flag in the header values 1, this value will be 1 byte long (0-255 bytes). If the SR flag is 0, this value will be a 32 bits (4 bytes). The NDEF field ID length indicates the length of the ID field. This field is present only if the flag IL is 1. The NDEF Record type field describes the type of record. The values in the type field must correspond with the value in the TNF bits in the record header. The NDEF Record ID field is a ID value. This is only present if the flag IL is active in the record header. The NDEF payload has the bytes described in the payload length field. The NFC message stored in the NFC EEPROM has this format. Design a cape board for an embedded system lab course using BeagleBone 39 2.3.9 CAN bus An isolated CAN bus is implemented with U2, Texas Instrument ISO1050DUB Isolated CAN transceiver. Some features of this transceiver:  ISO11898-2 compliant. This standard ISO 11898-2:2003 specifies the high-speed (transmission rates of up to 1 Mbit/s) for the CAN bus.  2500 Vrms isolation  Failsafe outputs  8-DUB package The functional diagram of the transceiver is: The pinout of the transceiver is: P9 HEADER PIN CAPE NET ISO1050 PIN 3,4 3.3V 1 NC +5V 8 1,2,43,44,45,46 GND 4 NC GND1 5 26 CAN_RXD 2 24 CAN_TXD 3 NC CANH 7 NC CANL 6 The isolated voltage is implemented with DC1, a DCDC converter NKE0305SC. Main features of this DCDC:  Nominal input voltage: min 2.97V, typ 3.3V, max 3.63V Design a cape board for an embedded system lab course using BeagleBone 40  Output voltage: 5 V  Output current: 200 mA  Rated power: 1W  Input current at rated load: 400 mA  Efficiency: min 72% - max 75 %  DIP package  Isolation: 3000VDC (1 second) The DCDC package: PIN FUNCTION 1 GND_3.3 2 3.3V 3 GND_5V 4 5V The schematic of the CAN bus implementation: The CAN implementation position in the cape board: Design a cape board for an embedded system lab course using BeagleBone 41 The pin-out for the CAN bus connector (CN3) is: PIN FUNCTION 1 CANL 2 GND1 3 CANH 4 +5V 2.3.10 GPMC The General Purpose Memory Controller (GPMC) is a memory controller dedicated to interfacing external memory devices:  Asynchronous SRAM-like memories and ASIC devices.  Asynchronous, synchronous, and page mode burst NOR flash devices.  NAND Flash.  Pseudo-SRAM devices. GPMC is connected to expansion headers, CN1 and CN2, to allow communication with external devices. GPMC signals implemented in the TT01v1 cape board are: Design a cape board for an embedded system lab course using BeagleBone 48 The following figure shows the midlayer 2, it is the GND plane: The gerbers files extension description: .drd Drill to Gerber .oln Outline .gtl Top Side .gbl Bottom Side .gto Silkscreen Top .gts Solder_Mask Top .gbs Solder_Mask Bot .g2 Midlayer1 .g3 Midlayer2 .gtp Top Stencil .gbp Bottom Stencil .gbo Silkscreen Bottom .pho Information Design a cape board for an embedded system lab course using BeagleBone 49 2.6 Part List The cape-bone-TT01v1 part list: Qty Parts Value Manufacturer Manufacturer Part Number 18 LD3, LD4, LD5, LD6, LD7, LD8, LD9, LD10, LD11, LD12, LD13, LD14, LD15, LD16, LD17, LD18, LD19,LD20 RED LED 0805 Avago HSMS-C170 1 LD1 GREEN LED 0805 Avago HSMG-C170 1 LD2 ORANGE LED 0805 Avago HSMD-C170 1 CN2 HEADER, 2.54MM, VERTICAL THT, 34WAY Samtec HTSS-117-02-T-D 1 CN1 Header 2.54mm THT R/A 40 Way Samtec HTSS-120-04-T-D-RA 2 R13, R14 RES 0R 0603 Panasonic ERJ-3GEY0R00V 10 C4, C5, C7, C8, C9, C10, C11, C12, C13, C17 CAP 100nF 0603 Yageo CC0603KRX7R7BeagleBone104 19 R9, R12, R20, R23, R27, R28, R30, R31, R32, R33, R34, R36, R37, R38, R40, R43, R46, R48, R49 RES 10K 0603 Panasonic ERJ-3EKF1002V 3 C14, C16, C18 CAP 10nF 0603 Yageo CC0603KRX7R9BeagleBone103 1 R11 RES 120R 0603 Panasonic ERJ-3EKF1200V 1 CN3 TERM BLOCK 4POS 3.5MM Phoenix Contact 1751264 5 R8, R10, R15, R35, R41 RES 1K 0603 Panasonic ERJ-3EKF1001V 1 R52 RES 20K 0603 Panasonic ERJ-3EKF2002V 1 S4 SWITCH DIP 2POS TOP ACT Ohmron A6S-2104-H Design a cape board for an embedded system lab course using BeagleBone 50 2 S1, S2 SWITCH DIP 3POS TOP ACT Ohmron A6S-3101-H 7 R2, R3, R4, R5, R6, R29, R39 RES 33R 0603 Panasonic ERJ-3EKF33R0V 9 R1, R7, R22, R24, R25, R26, R44, R45, R47 RES 4K7 0603 Panasonic ERJ-3EKF4701V 3 C3, C6, C19 CAP 4.7uF 1206 Kemet C1206C475K3RACTU 2 R42, R51 RES 5K6 0603 Panasonic ERJ-3EKF5601V 1 U1 8BIT SHIFT REG 3ST-OUT 16SOIC 4HC595 TI SN74HC595DR 4 Q1, Q2, Q3, Q4 TRT NPN 45V 500MA SOT- 23 BC817-25 OnSemi BC817-25LT3G 1 S3 SWITCH TACTILE SPSTNO 0.05A 12V TE CONNECTIVITY FSM6JSMA 1 EN1 ENCODER, VERTICAL, 12 DET, 12PPR ALPS EC12E1220401 1 U2 TXRX CAN 5V 8SOP ISO1050DUB TI ISO1050DUBR 1 U5 EEPROM M24256- BWMN6TP ST M24256-BWMN6TP 1 U4 NFC EEPROM M24LR04E- RMN6T/2 ST M24LR04E-RMN6T/2 1 U3 ACCELEROMETER 3AXIS 16QFN FREESCALE MMA8453QR1 1 DC1 DC/DC 1W 3.3VIN 5V SIP Murata NKE0305SC 1 L1 INDUCTOR POWER 4.7UH 20% SMD SDR0805- 4R7ML Bourns SDR0805-4R7ML 4 SPACER, 15.9/3.2PA Ettinger 06.81.176 4 SPACER, 6.4/3.2-PA Ettinger 06.81.116 2.7 Cape EEPROM configuration and programming In the BeagleBone capes shall be a EEPROM containing information that will allow the software to identify the board and to configure the expansion headers pins as needed. This Design a cape board for an embedded system lab course using BeagleBone 51 EEPROM is required for all capes to operate correctly when they are plugged into the BeagleBone. As it was explained in the document, the EEPROM address is configured by the switch S4. The EEPROM used in the cape is the M24256-BWM6P. In this chapter it will be explained:  how should be the content of this EEPROM,  how to generate the EEPROM content file,  how to program the EEPROM 2.7.1 EEPROM content The format of the EEPROM content is specified in the BeagleBone System Reference Manual: Design a cape board for an embedded system lab course using BeagleBone 52 2.7.2 Generate EEPROM content file In the Ångström distribution used for this document is the 2013.06.20 release, it is included the BoneScript library. It is a Node.js library optimized for the BeagleBone family. It Design a cape board for an embedded system lab course using BeagleBone 53 provides functions useful to interact with the hardware. It is a similar implementation as the Arduino approach. There is one script called eeprom.js, this script parses and generate the EEPROM data file that we will program in the EEPROM cape. The specification of the content shall be done in JSON format. JSON is the Java equivalent to the XML. This JSON description file shall have the following fields:  type: “cape” or “bone”  header: “aa5533ee” (Defined in this version of the EEPROM spec)  formatRev: “A0″ (Defined in this version of the EEPROM spec)  boardName: Your Cape name in ASCII and in quotes  version: Your version number in ASCII and in quotes  manufacturer: Manufacturer name in ASCII and in quotes  partNumber: Your part number in ASCII and in quotes  numPins: Number of I/O pins used in your design. Needs to match the number of mux: data entries.  serialNumber: Your serial number in ASCII and in quotes. Note: See the suggested format in the SRM and the table above.  currentVDD_3V3EXP: Maximum current drawn in milliamps (Range 0 to 250)  currentVDD_5V: Maximum current drawn in milliamps (Range 0 to 250)  currentSYS_5V: Maximum current drawn in milliamps (Range 0 to 1000)  DCSupplied: Amount of current in supplied milliamps on the VDD_5V rail by the Cape. (Range 0 to 65535)  used: “used” or “available”  direction: “in” or “out” or “bidir”  pullup: “disabled” or “pullup” or pulldown”  rx: “enabled” or “disabled”  slew: “slow” or “fast”  mode: Mode mux value (Range 0 to 7) The JSON file, cape-bone-TT01v1.json, which describes the expansion headers pin configuration is: Design a cape board for an embedded system lab course using BeagleBone 54 { "cape-bone-TT01v1": { "type": "cape", "header": "aa5533ee", "formatRev": "A0", "boardName": "cape-bone-TT01v1", "version": "00A0", "manufacturer": "", "partNumber": "cape-bone-TT01v1", "numPins": 47, "serialNumber": "040114000001", "currentVDD_3V3EXP": 200, "currentVDD_5V": 0, "currentSYS_5V": 200, "DCSupplied": 2000, "mux": { "P8_3": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad6" }, "P8_4": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad7" }, "P8_5": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad2" }, "P8_6": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad3" }, "P8_7": { "used": "used", "direction": "out", "rx": "disabled", Design a cape board for an embedded system lab course using BeagleBone 55 "pullup": "disabled", "mode": 0, "function": "gpmc_ale" }, "P8_8": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ren" }, "P8_9": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_cle" }, "P8_10": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_wen" }, "P8_11": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad13" }, "P8_12": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad12" }, "P8_13": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad9" }, Design a cape board for an embedded system lab course using BeagleBone 56 "P8_14": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad10" }, "P8_15": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad15" }, "P8_16": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad14" }, "P8_17": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad11" }, "P8_18": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_clk" }, "P8_19": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad8" }, "P8_20": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", Design a cape board for an embedded system lab course using BeagleBone 57 "mode": 0, "function": "gpmc_csn2" }, "P8_21": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_csn1" }, "P8_22": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad5" }, "P8_23": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad4" }, "P8_24": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad1" }, "P8_25": { "used": "used", "direction": "bidir", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_ad0" }, "P8_26": { "used": "used", "direction": "out", "rx": "disabled", "pullup": "disabled", "mode": 0, "function": "gpmc_csn0" }, "P9_11": { Design a cape board for an embedded system lab course using BeagleBone 64 0x1b4 0x07 /* p9_41 = pb1 = clkout2.gpio0_20, INPUT | MODE 7 */ >; }; bb_spi1_pins: pinmux_bb_spi1_pins { pinctrl-single,pins = < 0x190 0x13 /* p9_31 = spi1_sclk = mcasp0_aclkx.spi1_sclk, OUTPUT_PULLUP | MODE3 */ 0x194 0x33 /* p9_29 = spi1_miso = mcasp0_fsx.spi1_d0, INPUT_PULLUP | MODE3 */ 0x198 0x13 /* p9_30 = spi1_mosi = mcasp0_axr0.spi1_d1, OUTPUT_PULLUP | MODE3 */ 0x19c 0x13 /* p9_28 = spi1_cs0 = mcasp0_ahclkr.spi1_cs0, OUTPUT_PULLUP | MODE3 */ 0x164 0x12 /* P9_42 = GPIO0_7 = eCAP0_in_PWM0_out.gpio0[7] , OUTPUT_PULLUP | MODE2 */ >; }; cape_TT01_encoder: pinmux_cape_TT01_encoder { pinctrl-single,pins = < 0x090 0x07 /* p8_7 = enc_a = gpmc_advn_ale.gpio2[2], INPUT | MODE 7 */ 0x09C 0x07 /* p8_9 = enc_b = gpmc_ben0_cle.gpio2[5], INPUT | MODE 7 */ 0x034 0x07 /* p8_11 = enc_p = gpmc_ad13.gpio1[13], INPUT | MODE 7 */ >; }; }; }; fragment@1 { target = <&i2c1>; /* i2c1 is numbered correctly */ __overlay__ { status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&bb_i2c1_pins>; /* this is the configuration part */ clock-frequency = <100000>; #address-cells = <1>; #size-cells = <0>; }; }; fragment@2 { target = <&spi1>; __overlay__ { #address-cells = <1>; #size-cells = <0>; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&bb_spi1_pins>; cs-gpios = <&gpio4 17 0>, <&gpio1 7 0>; spi1_0{ #address-cells = <1>; #size-cells = <0>; compatible = "spidev"; reg = <0>; spi-max-frequency = <5000000>; spi-cs-high; }; }; }; fragment@3 { Design a cape board for an embedded system lab course using BeagleBone 65 target = <&ocp>; __overlay__ { pinmux_helper { compatible = "bone-pinmux-helper"; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&cape_TT01_leds_out>; pinctrl-1 = <&cape_TT01_pushbutton>; pinctrl-2 = <&cape_TT01_encoder>; }; }; }; fragment@4 { target = <&ocp>; __overlay__ { /* avoid stupid warning */ #address-cells = <1>; #size-cells = <1>; tscadc { compatible = "ti,ti-tscadc"; reg = <0x44e0d000 0x1000>; interrupt-parent = <&intc>; interrupts = <16>; ti,hwmods = "adc_tsc"; status = "okay"; adc { ti,adc-channels = <8>; }; }; }; }; }; Design a cape board for an embedded system lab course using BeagleBone 66 3 Embedded programming laboratory The next step is to program the Beaglebone, running with Linux, to implement a basic functionality with the Cape Board designed (TT01). In this point, I am going to introduce from low to medium complexity some firmware programs to work with some of the peripherals implemented in the cape-board-TT01. 3.1 Tool chain configuration To compile a BeagleBone application in a host computer, we need to install a proper tool chain. First of all, there shall be a correct communication between the host computer and the BeagleBone, and install the crosscompiler in the host computer. Next figure illustrates the steps to configure the tool chain. As an example, it is considered that the USB connection is in ttyUSB1 and the BeagleBone IP is 192.168.1.35. It could change according with your lab setup. Design a cape board for an embedded system lab course using BeagleBone 67 3.2 TT01 Cape Board A Cape board, called TT01, has been designed for this laboratory. The purpose of this cape board is to provide a tool to learn how to program some of the most common peripherals used in embedded systems. The peripherals implemented in the TT01 cape board are:  LEDs  Push Button  Analog inputs Design a cape board for an embedded system lab course using BeagleBone 68  LED matrix controlled by a shift register  Encoder  Accelerometer  Bus CAN  General Purpose Memory Controller (GPMC) connection  Near Field Communication (NFC) interface  EEPROM You can find some more details about these peripherals in next chapters. 3.2.1 Cape TT01 P8 connector pin out Design a cape board for an embedded system lab course using BeagleBone 69 BEAGLEBLONE TT01 CAPE TT01 CAPE BEAGLEBONE GND GND 1 2 GND GND GPIO1_6 GPMC_AD6 3 4 GPMC_AD7 GPIO1_7 GPIO1_2 GPMC_AD2 5 6 GPMC_AD3 GPIO1_3 TIMER4 GPMC_ALE/ENC_A 7 8 GPMC_REN TIMER7 TIMER5 GPMC_CLE/ENC_B 9 10 GPMC_WEN TIMER6 GPIO1_13 GPMC_AD13/ENC_P 11 12 GPMC_AD12 GPIO1_12 EHRPWM2B GPMC_AD9 13 14 GPMC_AD10 GPIO0_26 GPIO1_15 GPMC_AD15 15 16 GPMC_AD14 GPIO1_14 GPIO0_27 GPMC_AD11 17 18 GPMC_CLK GPIO2_1 EHRPWM2A GPMC_AD8 19 20 GPMC_CSN2 GPIO1_31 GPIO1_30 GPMC_CSN1 21 22 GPMC_AD5 GPIO1_5 GPIO1_4 GPMC_AD4 23 24 GPMC_AD1 GPIO1_1 GPIO1_0 GPMC_AD0 25 26 GPMC_CSN0 GPIO1_29 GPIO2_22 NC 27 28 NC GPIO2_24 GPIO2_23 NC 29 30 NC GPIO2_25 UART5_CTSN NC 31 32 NC UART5_RTSN UART4_RTSN NC 33 34 NC UART3_RTSN UART4_CTSN NC 35 36 NC UART3_CTSN UART5_TXD NC 37 38 NC UART5_RXD GPIO2_12 NC 39 40 NC GPIO2_13 GPIO2_10 NC 41 42 NC GPIO2_11 GPIO2_8 NC 43 44 NC GPIO2_9 GPIO2_6 NC 45 46 NC GPIO2_7 3.2.2 Cape TT01 P9 connector pin out. Design a cape board for an embedded system lab course using BeagleBone 70 BEAGLEBLONE TT01 CAPE TT01 CAPE BEAGLEBONE GND GND 1 2 GND GND VDD_3V3EXP 3.3V 3 4 3.3V VDD_3V3EXP VDD_5V NC 5 6 NC VDD_5V SYS_5V 5.0V 7 8 5.0V SYS_5V PWR_BUT NC 9 10 NC SYS_RESETn UART4_RXD GPMC_WT0 11 12 GPMC_BE1N GPIO1_28 UART4_TXD NC 13 14 LED3 EHRPWM1A GPIO1_16 LED1 15 16 LED2 EHRPWM1B I2C1_SCL I2C1_SCL 17 18 I2C1_SDA I2C1_SDA I2C2_SCL I2C2_SCL 19 20 I2C2_SDA I2C2_SDA UART2_TXD UART2_TXD 21 22 UART2_RXD UART2_RXD GPIO1_17 RFWIP/BUSY 23 24 CAN_TXD UART_TXD GPIO3_21 AC_INT2 25 26 CAN_RXD UART_RXD GPIO3_19 AC_INT1 27 28 SPI1_CS0 SPI1_CS0 SPI1_D0 SPI1_MISO 29 30 SPI1_MOSI SPI1_D1 SPI1_SCLK SPI1_SCLK 31 32 VDD_ADC VDD_ADC AIN4 NC 33 34 GNDA_ADC GNDA_ADC AIN6 NC 35 36 NC AIN5 AIN2 NC 37 38 NC AIN3 AIN0 AN_SW 39 40 NC AIN1 CLKOUT2 PB1 41 42 SP1_CS1/RS GPIO1_7 GND GND 43 44 GND GND GND GND 45 46 GND GND Design a cape board for an embedded system lab course using BeagleBone 71 3.3 Programming Applications 3.3.1 General Purpose Input/Output (GPIO) BeagleBone has 4 banks of 32 General Purpose Inputs/Outputs (GPIOs). Some of them are allocated for BeagleBone functions, i.e. BeagleBone LEDs, but there are 66 GPIOs that are accessible via the two expansion headers (P8, P9). Each pin of the BeagleBone processor (AM3359) can have up to eight different functions modes. Each function mode is selected with a multiplexer mode selection. You can find in the BeagleBone System Reference Manual the modes available for each processor pin, they are listed in the chapter 6.13 Expansion Headers. Each processor pin has a PAD Control Register to define the pin mode and its attributes. This PAD control register is a 32-bit register with the following fields: Slew Rate Selection SLEWCTRL 0 : fast 1 : slow Receiver input enabled RXACTIVE 0 : disabled 1 : enabled Pad pullup type selection PULLTYPESEL 0 : pulldown 1 : pullup Pullup / pulldown selection PULLEDEN 0 : disabled 1 : enabled Multiplexer mode selection MUXMODE 000 : Mode 0 … 111 : Mode 7 The following figure shows how is defined, as an example, the GPIO0_7: Design a cape board for an embedded system lab course using BeagleBone 72 A full description of the configuration pin register is in the Texas Instrument Technical Reference Manual for the AM335X Cortex-A8 Microprocessor, in the chapter 9.3.1.51. BeagleBone pins have 3.3V voltage level, and they can source low current, 5 mA on outputs, and sink 8 mA on inputs. The following documentation should be consulted to configure the GPIOs:  Reference Manual for the AM335X Cortex-A8 Microprocessor  BeagleBone schematic Design a cape board for an embedded system lab course using BeagleBone 73  BeagleBone reference manual We need that information to identify the used pins in the BeagleBone, which of those pins are available for the expansion boards, the modes of those pins, and theirs configuration registers. Ångström Linux Distribution uses a file system to access pins. Each pin control register is accessed as a file. it is a virtual file system called sysfs. To identify which is the file to control the GPIO, we must know its pin number. The general expression of a pin name is: GPIO <bank number>_<pin number within bank> The GPIOs are divided into three banks: 0,1,2 and 3. Each bank has an offset:  bank 0, offset 0  bank 1, offset 32  bank 2, offset 64  bank 3, offset 96 Pin number into the bank must be taken into account. The final pin number is: pin number = (bank number * 32) + pin number within bank For instance the GPIO1_16, its pin number will be: pin number = ( 1 * 32 ) + 16 = 48 Another example, the GPIO2_10, its pin number will be: pin number = ( 2 * 32 ) + 10 = 74 3.3.1.1 TT01 Cape GPIOs There are 3 LEDs GPIO outputs in the TT01 Cape: Design a cape board for an embedded system lab course using BeagleBone 80 //configure gpio48 as output high. Switch ON the LED write_pin = write (gpio48_value,logic_status,strlen(logic_status)); if (write_pin == -1) { perror("Unable to configure the GPIO48 as output high\n"); } //sleep during 3 seconds sleep(3); //switch off GPIO1_16 (LD1) //open the unexport file gpio48_unexport = open(unexport_file,O_WRONLY); if (gpio48_unexport == -1) { perror("Unable to open unexport\n"); } //write the pin number in the unexport file to disable the gpio48 pin write_pin = write(gpio48_unexport,pin_number,strlen(pin_number)); if (write_pin == -1) { perror("Unable to write unexport gpio48\n"); } //close all files close(gpio48_export); close(gpio48_unexport); close(gpio48_direction); close(gpio48_value); return 1; } The following example toggles the LED1, LED2 and LED3 every second. Program exits when push button (S3) is pressed more than 1 second: //************************************************************************** // GPIO2.c - Toggle LEDs every second. Break with push button ON > 1 sec //************************************************************************** Design a cape board for an embedded system lab course using BeagleBone 81 #include <stdio.h> #include <stddef.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #define output "out" #define input "in" #define pin_48 "48" //LED1 - LD1 - GPIO1_16 - GPIO48 #define pin_51 "51" //LED2 - LD2 - GPIO1_19 - GPIO51 #define pin_50 "50" //LED3 - LD3 - GPIO1_18 - GPIO50 #define pin_20 "20" //PUSH BUTTON - PB1 - GPIO0_20 #define export_file "/sys/class/gpio/export" #define unexport_file "/sys/class/gpio/unexport" #define gpio48_dir_file "/sys/class/gpio/gpio48/direction" #define gpio51_dir_file "/sys/class/gpio/gpio51/direction" #define gpio50_dir_file "/sys/class/gpio/gpio50/direction" #define gpio20_dir_file "/sys/class/gpio/gpio20/direction" #define gpio48_val_file "/sys/class/gpio/gpio48/value" #define gpio51_val_file "/sys/class/gpio/gpio51/value" #define gpio50_val_file "/sys/class/gpio/gpio50/value" #define gpio20_val_file "/sys/class/gpio/gpio20/value" int main(void) { //file handles int gpio48_exp; int gpio48_unexp; int gpio48_value; int gpio48_direction; int gpio51_exp; Design a cape board for an embedded system lab course using BeagleBone 82 int gpio51_unexp; int gpio51_value; int gpio51_direction; int gpio50_exp; int gpio50_unexp; int gpio50_value; int gpio50_direction; int gpio20_exp; int gpio20_unexp; int gpio20_value; int gpio20_direction; //read file size buffer int read_size; //write file size buffer int write_size; //variable to change LEDs value char LED[] = "0"; //status pushbutton: 0 - not pushed; 1 - pushed char value_pb[] = "0"; //open the export file gpio48 gpio48_exp = open(export_file,O_WRONLY); if (gpio48_exp == -1) { perror("Unable to open export 48\n"); } //export file to enable the gpio48 pin write_size = write(gpio48_exp,pin_48,strlen(pin_48)); if (write_size == -1) { perror("Unable to write export 48\n"); } //open gpio48 direction file gpio48_direction = open(gpio48_dir_file,O_WRONLY); Design a cape board for an embedded system lab course using BeagleBone 83 if(gpio48_direction == -1) { perror("Unable to open gpio48_direction\n"); } //configure gpio48 as output direction write_size = write(gpio48_direction, output, strlen(output)); if (write_size == -1) { perror("Unable to configure the GPIO48 as output\n"); } //open gpio48 value file gpio48_value = open(gpio48_val_file,O_WRONLY); if (gpio48_value == -1) { perror ("Unable to switch on the GPIO48\n"); } //open the export file gpio51 gpio51_exp = open(export_file,O_WRONLY); if (gpio51_exp == -1) { perror("Unable to open export\n"); } //export file to enable the gpio51 pin write_size = write(gpio51_exp,pin_51,strlen(pin_51)); if (write_size == -1) { perror("Unable to write export 51\n"); } //open gpio51 direction file gpio51_direction = open(gpio51_dir_file,O_WRONLY); if(gpio51_direction == -1) { perror("Unable to open gpio51_direction 51\n"); } //configure gpio51 as output direction write_size = write(gpio51_direction, output, strlen(output)); if (write_size == -1) { perror("Unable to configure the GPIO51 as output\n"); } //open gpio51 value file Design a cape board for an embedded system lab course using BeagleBone 84 gpio51_value = open(gpio51_val_file,O_WRONLY); if (gpio51_value == -1) { perror ("Unable to switch on the GPIO51\n"); } //open the export file gpio50 gpio50_exp = open(export_file,O_WRONLY); if (gpio50_exp == -1) { perror("Unable to open export 50\n"); } //export file to enable the gpio50 pin write_size = write(gpio50_exp,pin_50,strlen(pin_50)); if (write_size == -1) { perror("Unable to write export 50\n"); } //open gpio50 direction file gpio50_direction = open(gpio50_dir_file,O_WRONLY); if(gpio50_direction == -1) { perror("Unable to open gpio50_direction\n"); } //configure gpio50 as output direction write_size = write(gpio50_direction, output, strlen(output)); if (write_size == -1) { perror("Unable to configure the GPIO50 as output\n"); } //open gpio50 value file gpio50_value = open(gpio50_val_file,O_WRONLY); if (gpio50_value == -1) { perror ("Unable to switch on the GPIO50\n"); } //open the export file gpio20 gpio20_exp = open(export_file,O_WRONLY); if (gpio20_exp == -1) { perror("Unable to open export 20\n"); } Design a cape board for an embedded system lab course using BeagleBone 85 //export file to enable the gpio20 pin write_size = write(gpio20_exp,pin_20,strlen(pin_20)); if (write_size == -1) { perror("Unable to write export 20\n"); } //open gpio20 direction file gpio20_direction = open(gpio20_dir_file,O_WRONLY); if(gpio20_direction == -1) { perror("Unable to open gpio20_direction\n"); } //configure gpio20 as input direction write_size = write(gpio20_direction, input, strlen(input)); if (write_size == -1) { perror("Unable to configure the GPIO20 as input\n"); } //Loop to switch LED every second while(1) { //every second change the pin logic output if(strcmp(LED,"1") == 0) strcpy(LED,"0"); else strcpy(LED,"1"); //configure gpio48 as output high. Switch ON the LED write_size = write (gpio48_value,LED,strlen(LED)); if (write_size == -1) { perror("Unable conf GPIO48 out high\n"); } //configure gpio51 as output high. Switch ON the LED write_size = write (gpio51_value,LED,strlen(LED)); if (write_size == -1) { perror("Unable conf GPIO51 out high\n"); } Design a cape board for an embedded system lab course using BeagleBone 86 //configure gpio50 as output high. Switch ON the LED write_size = write (gpio50_value,LED,strlen(LED)); if (write_size == -1) { perror("Unable conf GPIO50 out high\n"); } //open gpio20 value file gpio20_value = open(gpio20_val_file,O_RDONLY); if (gpio20_value == -1) { perror ("Unable to switch on the GPIO20\n"); } //read gpio20 push button input read_size = read(gpio20_value,value_pb,1); if (read_size == -1) { perror("Unable to read input value\n"); } printf("logic status pushbutton %s\n", value_pb); //pushbutton pressed > 1 sec, break the while if( strcmp(value_pb,"1") == 0) break; //delay 1 second sleep(1); } //switch off LEDS //open the unexport file gpio48 gpio48_unexp = open(unexport_file,O_WRONLY); if (gpio48_unexp == -1) { perror("Unable to open unexport gpio48\n"); } //unexport file to disable the gpio48 pin write_size = write(gpio48_unexp,pin_48,strlen(pin_48)); Design a cape board for an embedded system lab course using BeagleBone 87 if (write_size == -1) { perror("Unable to write unexport gpio48\n"); } //open the unexport file gpio51 gpio51_unexp = open(unexport_file,O_WRONLY); if (gpio51_unexp == -1) { perror("Unable to open unexport gpio51\n"); } //unexport file to disable the gpio51 pin write_size = write(gpio51_unexp,pin_51,strlen(pin_51)); if (write_size == -1) { perror("Unable to write unexport gpio51\n"); } //open the unexport file gpio50 gpio50_unexp = open(unexport_file,O_WRONLY); if (gpio50_unexp == -1) { perror("Unable to open unexport gpio50\n"); } //unexport file to disable the gpio51 pin write_size = write(gpio50_unexp,pin_50,strlen(pin_50)); if (write_size == -1) { perror("Unable to write unexport gpio50\n"); } //disable Push Button //open the unexport file gpio20 gpio20_unexp = open(unexport_file,O_WRONLY); if (gpio20_unexp == -1) { perror("Unable to open unexport gpio20\n"); } Design a cape board for an embedded system lab course using BeagleBone 88 //unexport file to disable the gpio51 pin write_size = write(gpio20_unexp,pin_20,strlen(pin_20)); if (write_size == -1) { perror("Unable to write unexport gpio20\n"); } //close all files close(gpio48_exp); close(gpio48_direction); close(gpio48_value); close(gpio51_exp); close(gpio51_direction); close(gpio51_value); close(gpio50_exp); close(gpio50_direction); close(gpio50_value); close(gpio20_exp); close(gpio20_direction); close (gpio20_value); return 0; } 3.3.1.4 Proposed exercises Consider a digital number with 3 digits. LEDs shows the state of each of those digits: DIGIT 2 DIGIT 1 DIGIT 0 LD3 LD1 LD2 Show an incremental count with the LEDs. There is an incremental every second. Only exit program if the push button is pressed when all LEDs are switch off. Design a cape board for an embedded system lab course using BeagleBone 89 3.3.2 Analog input BeagleBone has a 12 bit ADC with 8 channels. It is a successive approximation conversion ACD type. The maximum voltage for each channel is 1.8V. 3.3.2.1 TT01 Cape ADC In the TT01 Cape there is one analog signal (AN_SW), which is the AIN0 in the BeagleBone: The analog value AN_SW is configurable with the switch S1: 1 2 3 AN_SW [V] OFF OFF OFF 1.6 ON OFF OFF 1.1 ON ON OFF 0.8 ON ON ON 0.7 3.3.2.2 Control ADC in C The BeagleBone has a successive-approximation 12 bits ADC. The reference voltage for the ADC is 1.8V. So the voltage equivalent to the ADC counts lecture is: voltage = (number_counts_ADC / (2^12)) * 1.8 The following program shows the ADC channel 0 in volts every 2 seconds. To exit the program keep push button pressed 1 second: //************************************************************************ // ANALOG.c - Read ADC0 every 2 seconds. Pushbutton > 1 sec to exit Design a cape board for an embedded system lab course using BeagleBone 96 { //SPI file int spi_file; //SPI write integer return int spi_wr; //register value to switch on all Matrix LEDs char wr_buf[2] = {0x0F,0x00}; //state push button char pb_state[] = "0"; //matrix status int matrix = 0; //export GPIO20 push button gpio_export(pin_20); //configure GPIO20 as input gpio_set_direction (pin_20, input); //open SPI file if ((spi_file = open(spi_file_out, O_RDWR)) < 0) { perror("Unable to open SPI file\n"); exit(1); } //loop to switch ON/OFF the LED Matrix while(1) { //write in the SPI file if ((spi_wr = write(spi_file, &wr_buf[matrix], sizeof(char))) < 0) { perror("Unable to write in SPI file\n"); exit(1); } //wait 2 seconds to change LED matrix status sleep(2); //change the buffer integer to change the LED Matrix status if (matrix == 0) { Design a cape board for an embedded system lab course using BeagleBone 97 matrix = 1; printf ("Switch ON LEDMATRIX\n\n"); } else { printf ("Switch OFF LEDMATRIX\n\n"); matrix = 0; } //function to check value push button gpio_read_value (pin_20, pb_state); //if push button is pressed finish the program if( strcmp(pb_state,"1") == 0) { printf ("Push button pressed\nExit\n"); break; } } //close SPI file close(spi_file); //unexport push button GPIO20 file gpio_unexport(pin_20); return 0; } // Function: gpio_export // *********************************************************************** // Export gpio // gpio_num: GPIO pin number // *********************************************************************** int gpio_export(char *gpio_num) { int exp_fd, write_size; //open the GPIO export file Design a cape board for an embedded system lab course using BeagleBone 98 exp_fd = open(SYS_GPIO_DIR"/export",O_WRONLY); if (exp_fd == -1) { perror("Unable to open export\n"); } //export file to enable GPIO write_size = write(exp_fd,gpio_num,strlen(gpio_num)); if (write_size == -1) { perror("Unable to write export\n"); } //close GPIO export file description close (exp_fd); return 0; } // Function: gpio_set_direction // *********************************************************************** // Set direction GPIO // gpio_num: GPIO pin number // dir: GPIO direction "in"/"out" // *********************************************************************** int gpio_set_direction (char *gpio_num, char *dir) { int dir_fd, write_size; char buffer[64]; snprintf(buffer,sizeof(buffer),SYS_GPIO_DIR"/gpio%s/direction",gpio_num); //open GPIO direction file dir_fd = open(buffer,O_WRONLY); if(dir_fd == -1) { perror("Unable to open direction file\n"); } //configure GPIO direction write_size = write(dir_fd, dir, strlen(dir)); if (write_size == -1) { Design a cape board for an embedded system lab course using BeagleBone 99 perror("Unable to configure the direction\n"); } close (dir_fd); return 0; } // Function: gpio_read_value // *********************************************************************** // Open read gpio file // gpio_num: number of GPIO // value: gpio value read // *********************************************************************** int gpio_read_value (char *gpio_num, char *value) { int value_fd, read_size; char buffer[64]; snprintf(buffer,sizeof(buffer),SYS_GPIO_DIR"/gpio%s/value",gpio_num); //open GPIO value file value_fd = open(buffer,O_RDONLY); if(value_fd == -1) { perror("Unable to open value file\n"); } //read GPIO value file read_size = read(value_fd,value,1); if (read_size == -1) { perror("Unable to read input value\n"); } close(value_fd); return 0; } // Function: gpio_unexport Design a cape board for an embedded system lab course using BeagleBone 100 // *********************************************************************** // Unexport gpio // gpio_num: number of GPIO // *********************************************************************** int gpio_unexport (char *gpio_num) { int unexp_fd , write_size; //open GPIO unexport file unexp_fd = open(SYS_GPIO_DIR"/unexport",O_WRONLY); if (unexp_fd == -1) { perror("Unable to open unexport\n"); } //unexport GPIO file to disable the pin write_size = write(unexp_fd,gpio_num,strlen(gpio_num)); if (write_size == -1) { perror("Unable to write unexport\n"); } //close unexport GPIO file close(unexp_fd); return 0; } 3.3.3.3 Proposed exercises  Switch-on every second, only one LED in the matrix, in an incremental way, from LD4 to LD19.  From the Analog values configured from the S1 switch-on the following LEDs: ANALOG INPUT LEDS ON 1.8 > V > 1.65 ALL LEDS 1.65 > V > 1.1 LD4 LD5 LD6 LD8 LD9 LD10 LD12 LD13 LD14 LD16 LD17 LD18 1.1 > V > 0.825 LD4 LD5 LD8 LD9 LD12 LD13 LD16 LD17 Design a cape board for an embedded system lab course using BeagleBone 101 0.825 > V > 0.66 LD4 LD8 LD12 LD16 0.66 > V NONE 3.3.4 Encoder A rotary encoder is a peripheral typically used in the user interfaces to navigate through the menus in a display. A rotary encoder has 2 outputs (ENC_A and ENC_B) with 90º out of phase with each other: Monitoring the two outputs of the encoder (ENC_A, ENC_B), it is possible to identify the direction of the turn, and therefore a relative position from the initial state. The state machine to identify if the direction of the turn is clockwise (CW) or counter clockwise (CCW) is: Design a cape board for an embedded system lab course using BeagleBone 102 3.3.4.1 TT01 Cape Encoder TT01 Cape has a rotary encoder, its signals are: ENCODER NET GPMC NET MODE 7 GPIOS GPIO ENC_A GPMC_ALE GPIO2_2 66 ENC_B GPMC_CLE GPIO2_5 69 ENC_P is the push button signal for those rotary encoders with that feature. When GPMC signals needs to be enable, configure all switches of the S2 as OFF state. It disables the encoder monitoring. Design a cape board for an embedded system lab course using BeagleBone 103 3.3.4.2 Control Encoder in C There are different ways to read a rotary encoder, we are going to monitor it with interrupt inputs by both edges (up and down edges). To do so, we configure the GPIOs with those features, and we use the poll() function provided by Linux to implement Multiplexed inputs / outputs (I/O). To understand the poll function, we mus consider that until now, we have used read() and write() functions to control files. These I/O systems calls blocks until the data is transferred. It is good enough in many applications, but not all applications. For instances, for the following applications:  Check whether a I/O function is possible on a file descriptor without blocking it if it is not possible to make a I/O function.  Monitor multiple file descriptors to see if I/O function is possible on any of them. Poll() function notifies without blocking when a file becomes ready to read or write. The poll() function is one for the Multiplexed I/O solution available with Linux. The poll() function description: #include <poll.h> int poll (struct pollfd *fds, nfds_t nfds, int timeout); The nfds parameter is the number of items in the fds array. Design a cape board for an embedded system lab course using BeagleBone 104 The timeout parameter specifies the time to wait in milliseconds, before returning regardless of any ready I/O. A negative value denotes an infinitive timeout. A value of 0 instructs the call to return immediately, listing any file descriptors with pending ready I/O, but not waiting for any further events. In this manner, poll() is true to its name, polling once, and immediately returning. The structure pollfd is defined as follows: #include <poll.h> struct pollfd { int fd; /* file descriptor */ short events; /* requested events to watch */ short revents; /* returned events witnessed */ }; The field fd contains a file descriptor for an open file. The field events is an input parameter, a bitmask specifying the events the application is interested in. The field revents is an output parameter, filled by the kernel with the events that actually occurred. The bits that may be set in events and returned in revents are defined in <poll.h>:  POLLIN : There is data to read.  POLLPRI : There is urgent data to read (e.g., out-of-band data on TCP socket; pseudo-terminal master in packet mode has seen state change in slave).  POLLOUT : Writing now will not block.  POLLRDHUP (since Linux 2.6.17) : Stream socket peer closed connection, or shut down writing half of connection. The _GNU_SOURCE feature test macro must be defined in order to obtain this definition.  POLLERR : Error condition (output only).  POLLHUP : Hang up (output only).  POLLNVAL : Invalid request: fd not open (output only). The Multiplexed I/O is explained as follows in the book “Linux system programming” (O'Reilly): Design a cape board for an embedded system lab course using BeagleBone 105 Multiplexed I/O allows an application to concurrently block on multiple file descriptors and receive notification when any one of them becomes ready to read or write without blocking. Multiplexed I/O thus becomes the pivot point for the application, designed similarly to the following activity: 1. Multiplexed I/O: Tell me when any of these file descriptors become ready for I/O. 2. Nothing ready? Sleep until one or more file descriptors are ready. 3. Woken up! What is ready? 4. Handle all file descriptors ready for I/O, without blocking. 5. Go back to step 1. The encoder pins ENC_A and ENC_B are configured as inputs with detection in both edges. The following application display the ENC_A and ENC_B status while you turn the encoder: //************************************************************************ // ENCODER.c - Display the value of the encoder outputs //************************************************************************ #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <poll.h> #include <stdio.h> #include <stddef.h> #include <unistd.h> #include <string.h> //function prototypes int get_value_lead(int value_fd); int gpio_export(char *gpio_num); int gpio_set_direction (char *gpio_num, char *dir ); int gpio_unexport (char *gpio_num); Design a cape board for an embedded system lab course using BeagleBone 112 // unexport gpio // gpio_num: number of GPIO // *********************************************************************** int gpio_unexport (char *gpio_num) { int unexp_fd , write_size; //open GPIO unexport file unexp_fd = open(SYS_GPIO_DIR"/unexport",O_WRONLY); if (unexp_fd == -1) { perror("Unable to open unexport\n"); } //unexport GPIO file to disable the pin write_size = write(unexp_fd,gpio_num,strlen(gpio_num)); if (write_size == -1) { perror("Unable to write unexport\n"); } //close unexport GPIO file close(unexp_fd); return 0; } 3.3.4.3 Proposed exercises Switch the LED Matrix following the encoder turn. Start with the LD4 switched-on. Increase or reduce the number of LED according with the rotary encoder movement. 3.3.5 Accelerometer An accelerometer measures the “proper acceleration”, it is the acceleration an object experience in free fall. An accelerometer can be used to detect orientation, shake, fall, tilt, motion, shock, vibration,... 3.3.5.1 TT01 Cape Accelerometer In the TT01 cape there is a 3-axis digital accelerometer, a Freescale MMA8453QR1 reference. It has 10 bits of resolution. The following figure illustrate the accelerometer schematic: Design a cape board for an embedded system lab course using BeagleBone 113 The I2C1 is the bus to communicate BeagleBone with this accelerometer. It has two programmable interrupt signals. The following figure illustrate an internal block diagram: 3.3.5.2 Monitor Accelerometer in C We are going to work with I2C tools provided by Linux. The I2C1 can be read with following command: root@BeagleBone:~# /lib/firmware# i2cdetect -l i2c-0 i2c OMAP I2C adapter I2C adapter i2c-1 i2c OMAP I2C adapter I2C adapter Design a cape board for an embedded system lab course using BeagleBone 114 i2c-2 i2c OMAP I2C adapter I2C adapter The i2c-2 is the BeagleBone I2C1 which is the one connected to the accelerometer and the NFC E2PROM. We can see the devices connected to the I2C1 with the i2cdetect command, specifying the I2C bus we want to check, in our case the i2c-2: root@BeagleBone:/lib/firmware# i2cdetect -y -r 2 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- 53 -- -- -- 57 -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- In the accelerometer MMA8453QR1 datasheet, you can find the I2C address is 0x1D for SA0 high. Once we know the address we can read all register of that address with the following command: root@BeagleBone:/lib/firmware# i2cdump -y 2 0x1d 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef 00: 00 7c 7c 01 fc ed 1c 00 00 00 00 00 00 3a 00 00 .||????......:.. 10: 00 80 00 44 84 00 00 00 00 00 00 00 00 00 00 00 .?.D?........... 20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 80: 00 7c 7c 01 fc ed 1c 00 00 00 00 00 00 3a 00 00 .||????......:.. 90: 00 80 00 44 84 00 00 00 00 00 00 00 00 00 00 00 .?.D?........... a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ Design a cape board for an embedded system lab course using BeagleBone 115 e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ We see the MMA8453QR1 accelerometer memory content, the register map. You can find the Register Address Map in the component datasheet (point 6. MMA8453Q datasheet Rev 5.1). The register map goes from 0x00 to 0x7F. The first registers (0x01 to 0x06) contains the X, Y, and Z acceleration data measure: By default the accelerometer is in Standby mode. We must change the mode to Active mode to read the acceleration values. In the datasheet we see that the LSB bit of the control register (0x2A) allows to change the mode to Active: To change that bit we execute the command i2cset which has the following format: i2cset <i2cbus> <chip_address> <register_address> <value> So, to change the LSB of the control register (0x2A), the command shall be: i2cset 2 0x1d 0x2a 0x01 If we execute again the dump command we can see that there are some changes in the X-Y-Z acceleration values (indicated in bold below): root@BeagleBone:~# i2cdump -y 2 0x1d Design a cape board for an embedded system lab course using BeagleBone 116 No size specified (using byte-data access) 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef 00: ff 01 40 01 00 3f 00 00 00 00 00 01 00 3a 00 00 .?@?.?.....?.:.. 10: 00 80 00 44 84 00 00 00 00 00 00 00 00 00 00 00 .?.D?........... 20: 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 ..........?..... 30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ We see then that the accelerometer is enabled. 3.3.5.3 Accelerometer control in C In the former chapter we saw how there are some i2c tool available to read and write in the I2C bus. If we want to program in C the peripheral through the I2C, we must work, as we have seen in several examples before, with the file system, with open, read and write command. The following C program illustrate how to work with the I2C peripheral: //************************************************************************ // ACCELEROMETER.c - Display X,Y,Z acceleration //************************************************************************ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/ioctl.h> #include <unistd.h> #include <string.h> #include <linux/i2c-dev.h> //function prototypes int i2c_open (); void i2c_init_slave (int i2c_fd); void enable_MMA8453QR1 (int i2c_fd); int read_MMA8453QR1 (int i2c_fd); void i2c_close (int i2c_fd); int gpio_export(char *gpio_num); Design a cape board for an embedded system lab course using BeagleBone 117 int gpio_set_direction (char *gpio_num, char *dir); int gpio_read_value (char *gpio_num, char *value); int gpio_unexport (char *gpio_num); //program constants #define MMA8453QR1_address 0x1D #define MMA8453QR1_buffer 0x80 #define MAX_BUF 80 #define ACC_X_MSB 0x01 #define ACC_X_LSB 0x02 #define ACC_Y_MSB 0x03 #define ACC_Y_LSB 0x04 #define ACC_Z_MSB 0x05 #define ACC_Z_LSB 0x06 #define REG_CTRL_REG1 0x2A #define I2C1_DIR "/dev/i2c-2" #define input "in" #define pin_20 "20" //PUSH BUTTON - PB1 - GPIO0_20 #define SYS_GPIO_DIR "/sys/class/gpio/" int main(void) { int i2c1_fd; //I2C1 file description //state push button char pb_state[] = "0"; //open I2C1 port i2c1_fd = i2c_open(); //init I2C1 accelerometer i2c_init_slave(i2c1_fd); Design a cape board for an embedded system lab course using BeagleBone 118 //enable accelerometer enable_MMA8453QR1(i2c1_fd); //prueba de configuración init exterior i2c_init_slave(i2c1_fd); //export GPIO20 push button gpio_export(pin_20); //configure GPIO20 as input gpio_set_direction (pin_20, input); //Loop to read the X,Y,Z accelerometer variables every 2 seconds while (1){ //init_i2c1_slave(); read_MMA8453QR1 (i2c1_fd); //function to check value push button gpio_read_value (pin_20, pb_state); //if push button is pressed finish the program if( strcmp(pb_state,"1") == 0) { printf ("Push button pressed\nExit\n"); break; } sleep(2); } //unexport push button GPIO20 file gpio_unexport(pin_20); //close i2c file i2c_close(i2c1_fd); return 0; } // Function: i2c_open // *********************************************************************** // Open I2C file Design a cape board for an embedded system lab course using BeagleBone 119 // i2c_fd: i2c file description // *********************************************************************** int i2c_open () { int i2c_fd; if((i2c_fd = open (I2C1_DIR,O_RDWR)) < 0) { perror("Open I2C bus failed"); exit (1); } return i2c_fd; } // Function: i2c_init_slave // *********************************************************************** // Init Accelerometer I2C // i2c_fd: i2c file description // *********************************************************************** void i2c_init_slave (int i2c_fd) { if (ioctl(i2c_fd,I2C_SLAVE,MMA8453QR1_address) < 0) { perror("Failure to take bus address and communicate with slave"); exit (1); } } // Function: enable_MMA8453QR1 // *********************************************************************** // Enable Accelerometer MMA8453QR1 // i2c_fd: i2c file description // *********************************************************************** void enable_MMA8453QR1 (int i2c_fd) { unsigned char I2C_WR_buffer[MAX_BUF]; Design a cape board for an embedded system lab course using BeagleBone 120 //MMA8453QR1 control register ACTIVE mode value 0x01 int activeMask = 0x01; I2C_WR_buffer[0] = REG_CTRL_REG1; I2C_WR_buffer[1] = activeMask; if (write(i2c_fd, I2C_WR_buffer, 2) != 2) { perror ("Enable Active mode Failure"); exit (1); } } // Function: read_MMA8453QR1 // *********************************************************************** // Read Accelerometer MMA8453QR1 // The register contain the x-axis, y-axis, z-axis 10 bits sample // data expressed as 2's complement numbers // The conversion consist to convert create a 10 bits number and // make the 2's complement number // i2c_fd: i2c file description // *********************************************************************** int read_MMA8453QR1 (int i2c_fd) { int acc_X; int acc_Y; int acc_Z; char RD_buf[15]; if (read(i2c_fd,RD_buf,15) != 15){ perror ("Read error"); } acc_X=(RD_buf[ACC_X_MSB] << 2)|((RD_buf[ACC_X_LSB] >> 6)& 3); acc_Y=(RD_buf[ACC_Y_MSB] << 2)|((RD_buf[ACC_Y_LSB] >> 6)& 3); acc_Z=(RD_buf[ACC_Z_MSB] << 2)|((RD_buf[ACC_Z_LSB] >> 6)& 3); Design a cape board for an embedded system lab course using BeagleBone 121 if (acc_X > 511) acc_X = acc_X - 1024; if (acc_Y > 511) acc_Y = acc_Y - 1024; if (acc_Z > 511) acc_Z = acc_Z - 1024; printf("ACC X: %d\nACC Y: %d\nACC Z: %d\n\n",acc_X,acc_Y,acc_Z); return 0; } // Function: i2c_close // *********************************************************************** // Close I2C file // i2c_fd: i2c file description // *********************************************************************** void i2c_close (int i2c_fd) { close(i2c_fd); } // Function: gpio_export // *********************************************************************** // Export gpio // gpio_num: GPIO pin number // *********************************************************************** int gpio_export(char *gpio_num) { int exp_fd, write_size; //open the GPIO export file exp_fd = open(SYS_GPIO_DIR"/export",O_WRONLY); if (exp_fd == -1) { perror("Unable to open export\n"); } Design a cape board for an embedded system lab course using BeagleBone 128 * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ /dts-v1/; /plugin/; / { compatible = "ti,BeagleBone", "ti,BeagleBone-black"; /* identification */ part-number = "iio-test"; /* state the resources this cape uses */ exclusive-use = /* the pin header uses */ "P9.39", /* AIN0 */ "P9.40", /* AIN1 */ "P9.37", /* AIN2 */ "P9.38", /* AIN3 */ "P9.33", /* AIN4 */ "P9.36", /* AIN5 */ "P9.35", /* AIN6 */ /* the hardware IP uses */ "tscadc"; fragment@0 { target = <&ocp>; __overlay__ { /* avoid stupid warning */ #address-cells = <1>; #size-cells = <1>; tscadc { compatible = "ti,ti-tscadc"; reg = <0x44e0d000 0x1000>; interrupt-parent = <&intc>; interrupts = <16>; ti,hwmods = "adc_tsc"; status = "okay"; adc { ti,adc-channels = <8>; }; }; test_helper: helper { compatible = "bone-iio-helper"; vsense-name = "AIN0", "AIN1", "AIN2", "AIN3", "AIN4", "AIN5", "AIN6", "AIN7"; vsense-scale = <100 100 100 100 100 100 100 100>; status = "okay"; }; }; }; }; The next step is to compile the dts device tree. We recommend that you create a script with the following commands: #!/bin/bash Design a cape board for an embedded system lab course using BeagleBone 129 echo “Compiling the overlay from dts to dto” dtc -O dtb -o cape-bone-iio-00A0.dtbo -b 0 -@ cape-bone-iio-00A0.dts Once executed this script, we get the cape-bone-iio-00A0.dtbo file. Copy cape-bone-iio-00A0.dtbo file to the directory /lib/firmware and go to that directory cp cape-bone-iio-00A0.dtbo /lib/firmware cd /lib/firmware Echo the compiled file to the SLOTS directory: echo cape-bone-iio > $SLOTS We have activated the ADC channels. To read ADC files we must go to the directory /sys/bus/platform/devices/tiadc/iio:device0: root@BeagleBone:/sys/bus/platform/devices/tiadc/iio:device0# ls -al total 0 drwxr-xr-x 3 root root 0 Jan 1 02:39 . drwxr-xr-x 4 root root 0 Jan 1 02:39 .. -r--r--r-- 1 root root 4096 Jan 1 03:29 dev -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage0_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage1_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage2_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage3_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage4_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage5_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage6_raw -rw-r--r-- 1 root root 4096 Jan 1 03:29 in_voltage7_raw -r--r--r-- 1 root root 4096 Jan 1 03:29 name drwxr-xr-x 2 root root 0 Jan 1 03:29 power lrwxrwxrwx 1 root root 0 Jan 1 03:29 subsystem -> ../../../../../bus/iio -rw-r--r-- 1 root root 4096 Jan 1 02:39 uevent We can know the raw measure of the ADC channels reading the in_voltage_raw files. For instance to read the ADC0 we can execute the following command: Design a cape board for an embedded system lab course using BeagleBone 130 cat in_voltage0_raw 3.6 Device tree cape-bone-TT01v1 The device tree cape-bone-TT01v1-00A0.dts needed to work with the TT01v1 cape is: /* * Copyright (C) 2013 Raul Perez Lopez <[email protected]> * <@raul_p_l> * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ /dts-v1/; /plugin/; / { compatible = "ti,BeagleBone", "ti,BeagleBone-black"; /* identification */ part-number = "cape-bone-TT01v1"; /* version */ version = "00A0"; /* state the resources this cape uses */ exclusive-use = /* the pin header uses */ "P9.18", /* i2c1: i2c1_sda */ "P9.17", /* i2c1: i2c1_scl */ "P9.39", /* analog: an_sw */ "P9.14", /* led: led3 */ "P9.15", /* led: led1 */ "P9.16", /* led: led2 */ "P9.41", /* pushbutton: pb1 */ "P8.7", /* encoder: enc_a */ "P8.9", /* encoder: enc_b */ "P8.11", /* encoder: enc_p */ "P9.28", /* spi1: spi1_cs0 */ "P9.29", /* spi1: spi1_miso */ "P9.30", /* spi1: spi1_mosi */ "P9.31", /* spi1: spi1_sclk */ /* the hardware IP uses */ "gpio1_16", "gpio1_18", "gpio1_19", "tscadc", "spi1", "i2c1"; fragment@0 { target = <&am33xx_pinmux>; __overlay__ { bb_i2c1_pins: pinmux_bb_i2c1_pins { pinctrl-single,pins = < 0x158 0x72 /* p9_18 = i2c1_sda = spi0_d1.i2c1_sda, SLEWCTRL_SLOW | INPUT_PULLUP | MODE2 */ 0x15c 0x72 /* p9_17 = i2c1_scl = spi0_cs0.i2c1_scl, SLEWCTRL_SLOW Design a cape board for an embedded system lab course using BeagleBone 131 | INPUT_PULLUP | MODE2 */ >; }; cape_TT01_leds_out: pinmux_cape_TT01_leds_out { pinctrl-single,pins = < 0x040 0x07 /* p9_15 = led1 = gpmc_a0.gpio1[16], OUTPUT | MODE 7 */ 0x04c 0x07 /* p9_16 = led2 = gpmc_a3.gpio1[19], OUTPUT | MODE 7 */ 0x048 0x07 /* p9_14 = led3 = gpmc_a2.gpio1[18], OUTPUT | MODE 7 */ >; }; cape_TT01_pushbutton: pinmux_cape_TT01_pushbutton { pinctrl-single,pins = < 0x1b4 0x07 /* p9_41 = pb1 = clkout2.gpio0_20, INPUT | MODE 7 */ >; }; bb_spi1_pins: pinmux_bb_spi1_pins { pinctrl-single,pins = < 0x190 0x13 /* p9_31 = spi1_sclk = mcasp0_aclkx.spi1_sclk, OUTPUT_PULLUP | MODE3 */ 0x194 0x33 /* p9_29 = spi1_miso = mcasp0_fsx.spi1_d0, INPUT_PULLUP | MODE3 */ 0x198 0x13 /* p9_30 = spi1_mosi = mcasp0_axr0.spi1_d1, OUTPUT_PULLUP | MODE3 */ 0x19c 0x13 /* p9_28 = spi1_cs0 = mcasp0_ahclkr.spi1_cs0, OUTPUT_PULLUP | MODE3 */ 0x164 0x12 /* P9_42 = GPIO0_7 = eCAP0_in_PWM0_out.gpio0[7] , OUTPUT_PULLUP | MODE2 */ >; }; cape_TT01_encoder: pinmux_cape_TT01_encoder { pinctrl-single,pins = < 0x090 0x07 /* p8_7 = enc_a = gpmc_advn_ale.gpio2[2], INPUT | MODE 7 */ 0x09C 0x07 /* p8_9 = enc_b = gpmc_ben0_cle.gpio2[5], INPUT | MODE 7 */ 0x034 0x07 /* p8_11 = enc_p = gpmc_ad13.gpio1[13], INPUT | MODE 7 */ >; }; }; }; fragment@1 { target = <&i2c1>; /* i2c1 is numbered correctly */ __overlay__ { status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&bb_i2c1_pins>; /* this is the configuration part */ clock-frequency = <100000>; #address-cells = <1>; #size-cells = <0>; }; }; fragment@2 { target = <&spi1>; __overlay__ { #address-cells = <1>; #size-cells = <0>; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&bb_spi1_pins>; cs-gpios = <&gpio4 17 0>, <&gpio1 7 0>; spi1_0{ Design a cape board for an embedded system lab course using BeagleBone 132 #address-cells = <1>; #size-cells = <0>; compatible = "spidev"; reg = <0>; spi-max-frequency = <5000000>; spi-cs-high; }; }; }; fragment@3 { target = <&ocp>; __overlay__ { pinmux_helper { compatible = "bone-pinmux-helper"; status = "okay"; pinctrl-names = "default"; pinctrl-0 = <&cape_TT01_leds_out>; pinctrl-1 = <&cape_TT01_pushbutton>; pinctrl-2 = <&cape_TT01_encoder>; }; }; }; fragment@4 { target = <&ocp>; __overlay__ { /* avoid stupid warning */ #address-cells = <1>; #size-cells = <1>; tscadc { compatible = "ti,ti-tscadc"; reg = <0x44e0d000 0x1000>; interrupt-parent = <&intc>; interrupts = <16>; ti,hwmods = "adc_tsc"; status = "okay"; adc { ti,adc-channels = <8>; }; }; }; }; }; Design a cape board for an embedded system lab course using BeagleBone 133 4 Conclusions 4.1 Cape design The hardware design was a tough due to the constraints in the board area. Finally, I was successful except for some small issues related with the GPMC pin out connector. From programming point of view, all peripherals were validated successfully. It would be pending to write an application for the CAN bus and the NFC. Those programs could be part of an advance project for the students. The IDE Eclipse was extremely useful to program. It is outstanding tool to write, debug, compile, programming and execute the program in the BeagleBone. I suggest a tutorial about this tool would be needed in an advance embedded programming course. 4.2 Open Hardware The open hardware has allowed to introduce tools and platforms to program embedded systems with a low price. Arduino (with low complexity), and BeagleBone are examples of substitutes in the electronics labs for the expensive evaluation boards designed by microcontroller manufacturers. The open hardware movement, as the open software, shares the designs in the web. The challenge to go further, contributing with the community, being part of it, are shaping what is already called the “third industrial revolution”, the Maker revolution. The Do-it-yourself 3D printers and companies as Adafruit and SparkFun, are the most popular examples about it. With this cape, and the programs, I have tried to contribute to introduce the students into the open hardware exciting challenge. Nowadays, more than ever, the embedded designs are limited only by our imagination. Design a cape board for an embedded system lab course using BeagleBone 134 5 Annex I. IDE Eclipse configuration The most popular Integrated Development Enviroment (IDE) used currently to program is Eclipse. In this Annex, I am going to explain how to configure this IDE to write the code, debug, and program the Beaglebone with Linux. The Eclipse IDE is installed in Ubuntu Linux in a PC. The Ubuntu release installed in a host PC is 12.04 LTS. The iso file installed is: Ubuntu_12.04.2_desktop_i386.iso The Linux Angstrom distribution in the BeagleBone is the 2013.06.20 release. The img file loaded in the SD card is: Angstrom-Cloud9-IDE-GNOME-eglibc-ipk-v2012.12-BeagleBone-2013.06.20 You can find the instructions to update BeagleBone with the latest software in the SD card in the following link: http://beagleboard.org/Getting%20Started 5.1 Installing a Tool Chain We need a cross-compiler to compile programs in the host PC for the target embedded system, the BeagleBone board. To install the cross-compiler, open a terminal in the host PC (Ctrl+Alt+T) and type: user@ubuntu:~$ wget http://www.angstrom-distribution.org/toolchains/angstrom-2011.03-i686- linux-armv7a-linux-gnueabi-toolchain.tar.bz2 user@ubuntu:~$ sudo tar -C / -xjf angstrom-2011.03-i686-linux-armv7a-linux-gnueabi- toolchain.tar.bz2 user@ubuntu:~$ export PATH=/usr/local/angstrom/arm/bin:$PATH 5.2 Connecting the BeagleBone 5.2.1 Terminal connection Connect the BeagleBone with an Ethernet cable to a router. Connect the BeagleBone USB Device Port to the host PC and give it a minute to boot. We must find out what device file /dev/ttyUSB* the BeagleBone is connected to. To do so, type in a terminal in the host PC: Design a cape board for an embedded system lab course using BeagleBone 135 user@ubuntu:~$ dmesg | grep FTDI | grep "now attached to" | tail -n 1 You should read the ttyUSB* connected to BeagleBone. For the rest of the tutorial, let us consider that the BeagleBone is connected to /dev/ttyUSB0. To open a serial console with BeagleBone, we use an application named screen. In order to install this application, type: user@ubuntu:~$ sudo apt-get install screen Now run screen as root with the ttyUSB* connected to the BeagleBone. user@ubuntu:$ sudo screen /dev/ttyUSB0 115200 Once screen is running, you will get a blank screen with a blinking cursor; just press enter to see the Angstrom login shown below. Otherwise, if a prompt U-boot# is shown instead of the Angstrom login, type “boot”, press enter, let the BeagleBone Linux some time to boot and the login should appear. Log in as root with no password. Now we can find out the BeagleBone IP address and use it to establish remote connections, i.e. ssh. To obtain such IP address, we can proceed as follows: For the rest of the tutorial, let us consider that the BeagleBone IP is 192.168.1.35. Design a cape board for an embedded system lab course using BeagleBone 136 As shown below, we can ping google.com to check if the BeagleBone internet connection works OK. 5.2.2 Installing ssh ssh deals for Secure Shell, a protocol designed to provide secure remote access to another computer. This protocol encrypts the session, provides better authentication facilities and secures file transfer. To install ssh, type in the BeagleBone terminal: root@BeagleBone:~# opkg install ssh Once ssh is installed, it is possible to transfer files from the host PC to the BeagleBone. Since we know the BeagleBone IP, using ssh we can now open a new terminal in the host PC and connect to the BeagleBone as root: user@ubuntu:~$ ssh roo[email protected] [email protected]’s password: root@BeagleBone:~# 5.2.3 Defining a host with BeagleBone IP Let us add the BeagleBone IP address to the /etc/hosts file. This is especially useful when such IP address is fixed. To do so, in a host PC terminal, type: user@ubuntu:~$ sudo gedit /etc/hosts In gedit, add the BeagleBone IP address to the file: 192.168.1.35 BeagleBone-IP Design a cape board for an embedded system lab course using BeagleBone 137 Click Save and close gedit. To test the connection, in a terminal, in the host PC, type: user@ubuntu:~$ ping BeagleBone-IP You should see the ping messages. Press Ctrl+C to quit the ping command. 5.3 Installing Eclipse Eclipse is a collection of software frameworks and tools for building Integrated Development Environments (IDEs). Eclipse has a base workspace and an extensible plug-in system for customizing the environment. The CDT Project 2 provides a fully functional C/C++ IDE for Eclipse. 5.3.1 Installing Eclipse In the host PC, open the Ubuntu Software Center and type eclipse in the search field. A window similar to the one below appears. There, select the Eclipse Integrated Development Environment, and click on Install. 2 http://www.eclipse.org/cdt/ Design a cape board for an embedded system lab course using BeagleBone 144 If not, the Install window should look as follows: Click on Next and follow the dialogs to accept the license and install the software. Once the installation has finished, click on Restart Now if prompted. 5.4 Creating and Configuring a Project 5.4.1 Creation of a New Project Run Eclipse and select File -> New -> Project Design a cape board for an embedded system lab course using BeagleBone 145 In the New Project window, expand C/C++, select C Project, and click on Next As shown in the window C Project below, enter the project name hello_world, select the executable type “Hello World ANSI C Project”, and click on Finish. If the Open Associated Perspective window appears, click on Yes and a window similar to the following opens: Design a cape board for an embedded system lab course using BeagleBone 146 5.4.2 Creating a Build Configuration The project requires defining a build configuration. It defines how Eclipse will crosscompile the project for the BeagleBone. In the Project Explorer tab, right-click on the hello_world project name and select Build Configurations -> Manage... Design a cape board for an embedded system lab course using BeagleBone 147 The Manage Configurations window, shown below, opens. Click on New to create a new configuration for cross-compiling. The Create New Configuration window opens. There, in the Name box, enter the name of the configuration: BeagleBone_debug. In the Copy settings from list, select Existing configuration and Debug. Design a cape board for an embedded system lab course using BeagleBone 148 Click OK twice to return to the project window. There, in the Project Explorer tab, rightclick hello_world and select Properties. The Properties window opens. At the left sub-window, expand C/C++ Build and select Settings. In the Configuration box, select BeagleBone_debug. Click on GCC C Compiler, and type the following text in the Command box: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-gcc Design a cape board for an embedded system lab course using BeagleBone 149 Under GCC C Compiler, click Includes. In the Include paths (-l) sub-window (the topright one), click on the Add… icon shown below to add a path. Add the following path: /usr/local/angstrom/arm/arm-angstrom-linux-gnueabi/usr/include Click OK and the Properties window should look like shown below. Design a cape board for an embedded system lab course using BeagleBone 150 Now click on GCC C Linker, and type the following text in the Command box: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-gcc Under GCC C Linker, click on Libraries. In the Library search path (-L) window (the bottom one), click on the Add… icon and add this directory path: Design a cape board for an embedded system lab course using BeagleBone 151 /usr/local/angstrom/arm/arm-angstrom-linux-gnueabi/lib Click OK and the Properties window should look like shown below. Now click on GCC Assembler, and type the following text in the Command box: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-as Click OK to close the Properties window and return to the project window. Design a cape board for an embedded system lab course using BeagleBone 152 There, in the Project Explorer tab, right-click the project name hello_world, select Build Configurations -> Set Active and select BeagleBone_debug. 5.5 Compiling and Running a Program Using Eclipse, we are now ready to create and compile a source file for your project and run the program on the target system, the BeagleBone. The next sections describe several options to do this: - Compile the program in the host PC, transfer it to the BeagleBone with a command line in a terminal and execute it in the BeagleBone. - Compile the program in the host PC, transfer it to the BeagleBone using only Eclipse and execute it in the BeagleBone. - Compile the program and execute it only in the BeagleBone. 5.5.1 Compiling a Source File in the host PC In the Eclipse project window, select Project -> Build Project. Design a cape board for an embedded system lab course using BeagleBone 153 A successful build creates the file hello_world in the configuration subdirectory of the project: /home/username/workspace/hello_world/BeagleBone_debug This is also the location of the project's makefile. Any build errors will show up in the Problems tab. On success, you're ready to transfer the file to the BeagleBone and test. 5.5.2 Transferring the executable file to the BeagleBone using a command line Let us create a directory in the BeagleBone where to transfer the executable file. Open a ssh session with the BeagleBone and type: root@BeagleBone:~# mkdir /home/root/workspace/hello_world Open a terminal in the host PC and go to the directory where the executable file is: user@ubuntu:~$ cd /home/user/workspace/hello_world/BeagleBone_debug As shown, use the scp command to transfer the file to the BeagleBone: user@ubuntu:~$ scp hello_world [email protected].35:/home/root/workspace/hello_world/ Comments: