You are on page 1of 10

Quiz 1

1. What is a microcontroller?
A microcontroller is a small computer on a single integrated circuit containing
a processor core, memory, and programmable input/output peripherals.
Program memory is also often included on chip, as well as a typically small
amount of RAM.
2. The .profile file sets a global shell variable PATH. What is the purpose of this variable?
PATH is an environment variable which specifies a set of directories where executable programs are located.
3. Write a script that takes one command line parameter and prints an appropriate message if the parameter is Yah
or Nah, or prints another message otherwise.
#!/bin/bash
if [ "$1" == "Yah" ] || [ "$1" == "Nah" ]
then
echo "Good job!"
else
echo "Bad job!!"
fi
exit 0
4. What is the syntax of the case statement is the Bash shell? Give an example.
case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN) COMMAND-LIST;; esac
echo -n "Do you agree with me? [yes or no]: "
read yno
case $yno in
yes )
echo "Agreed"
;;
no )
echo "Not agreed, you can't pass go";
exit 1
;;
*) echo "Invalid input"
;;
esac
5. How is usage added to a script? Give an example.
If [$# != 2]
Then
Echo usage: %0 arg1 arge2
Fi
6. How can a user check to see if a file can be run?
after creating the file (vi filename),
Save your file by the command (:wq)
then you can run your file using (bash file name)
another option is bash -x filename.sh to attempt the debugging.

Quiz 2
1. How do you debug code using the gdb debugger?
You may first compile the program using gcc -g filename.c -o filename then test the file by run the command ./filename.
you can start debugging the program using the command gdb filename. You can also run the program inside the debugger
by using the command gdb run. Inside gdb, the user also allows to backtrack, step, next and print command to step into,
over, backtrack and review the code.
2. The "*" symbol has different meanings when used in the following three contexts: declaration, (*s), and (a * b).
Explain.
Used in a declaration context, * declares an address variable
Used in a unary context, * means value stored at that address
Used in a binary context, * means multiply rhs and lhs
3. a) What is a pointer in the C programming language?
b) How do you use a pointer?
A pointer is a programming language object, whose value refers to
(or "points to") another value stored elsewhere in the computer memory
using its address. A pointer references a location in memory, and obtaining
the value stored at that location is known as dereferencing the pointer.
int a = 5;
int *ptr = &a;
(*ptr), a[0]
4. a) Give an example of a unary operator.
b) Give an example of a binary operator.
c) Give an example of a ternary operator.
unary operator: ! + binary operator: % |
ternary operator: * ?
5. The "&" symbol has different meanings when used in the expressions (&a) and (a & b). Explain.
Used in a unary context, & means address of variable
Used in a binary context, & means bit-wise and of rhs and lhs
6. What is the syntax for the "if" construct in the C programming language? Give an example of use.
if( cond ) stmt1; [else stmt2;]
cond: boolean expression - execute stmt1 if TRUE [or stmt2 if FALSE (optional)]
if( a < b ) a = b; else b = a;
7. In writing a c program, the perror function can be very useful. Explain how it is used.
The routine perror() produces a message on the standard error output,
describing the last error encountered during a call to a system or
library function. The prototype is
void perror(const char *s);
To be of most use, the argument string should include the name of the
function that incurred the error.
8. Write a complete C program that will take an integer input by the user and print "ECE251 is a fun class" on that
number of lines using a for loop, a while loop, and a do while loop.
#include<stdio.h>
int main(void);
int main()
{
int i;
while( ++i <= 5 )
{
printf( "ECE251\n" );
}

return 0;
}
9. What is the syntax of the "for" construct in the c programming language? Give an example of use.
for( init ; cond ; incr ) stmt;
init: initialization phrase
cond: boolean expression - loop while TRUE
incr: increment phrase
stmt: statement to be executed once each time throiugh loop
ex: for( i = 0 ; i < 10 ; ++i ) printf( "%d\n", i );
Quiz 3
1. What is the difference between fnmatch and wordexp?

fnmatch check whether the string argument matches with the pattern argument. fnmatch returns zero if string
matches pattern and return FNM_NOMATCH if there is no match or an error. synopsis for fnmatch is
#include<fnmatch.h)
wordexp perform as a word expansion (shell) of the string and return the result in the structure pointed.
synopsis for wordexp is #include<wordexp.h>
2. What information does the hexdump utility provide?
The hexdump utility is a filter which displays the specified files,
or the standard input, if no files are specified, in a user specified format.
3. #include <stdio.h>
int main(void);
int main()
{
int i;
float d, sum;
FILE *fp = fopen( "input.dat", "r" );
if( !fp )
{
printf( "cannot open input.dat for reading\n" );
return 1;
}
for( i = 0, sum = 0.0 ; i != 5 ; ++i )
{
if( fscanf( fp, "%f", &d ) != 1 )
{
printf( "Tried to read a data item from input.dat, but failed.\n" );
return 2;
}
sum += d;
}
fclose( fp );
if( (fp = fopen( "results.dat", "w" )) == NULL )
{
printf( "Cannot open file results.dat for output\n" );
return 3;
}
fprintf( fp, "%f", sum/5.0 );
fclose( fp );

return 0;
}
4. What is a glob? Give an example of use with a brief explanation.
Glob is a term used to describe the expansion or the match of values returned
when using wildcards, regular expressions, or other pattern matches.
ex: ls *.c
5. How does a c programmer access formatted and binary (unformatted) data for
I/O? Use snippets of code to illustrate your answer.

binary: open, read, write, close


formatted: fopen, fscanf, fprintf, fclose
int i, fd = open( "filename", O_RDONLY );
read( fd, &i, sizeof(i));
close( fd );
fd = open( "filename", O_WR_ONLY );
write( fd, &i, sizeof(i) );
fclose( fd );
int i;
FILE *fp = fopen( "filename", "r" );
fscanf( fp, "%d", &i );
fclose( fp );
fp = fopen( "filename", "w" );
fprintf( fp, "%d", i );
fclose( fp );
Quiz 3
What are the nominal values for the GPIO pull-up and pull-down resistors that
were determined in class?

PU: ~13k
PD: ~7.3k
What is the program devmem2 used for?
Devmem2 can be use to accessing the gpio registers as well as to map the memory at a
certain address. It also allow us to return the values at the address. Devmem2 also allows
us to read all the active events and write IRQ for debugging.

What do the following terms mean when using config-pin?


high

in
inin+
low
out
high configure gpio as output with logic 1 as initial value
in configure gpio as input
in- configure gpio as input with pull down resistor enabled
in+ configure gpio as input with pull up resistor enabled
low configure gpio as output with logic 0 as initial value
out configure gpio as output
1.
How many banks of gpio pins are there on the AM335x microcontroller?
2.
How many pins per gpio bank?
3.
How many SPI devices are available on the BBB?
4.
How many I2C devices are available on the BBB?
4 banks,32 pins per bank

Using the GPIO registers below,


a) How is a value of 0 written into gpio44?

b) How is a value of 1 written into gpio44?

b) How is the value in gpio44 determined?

Table 25-5. GPIO Registers


Offset Acronym Register Name Section
0h GPIO_REVISION
10h GPIO_SYSCONFIG
20h GPIO_EOI
24h GPIO_IRQSTATUS_RAW_0
28h GPIO_IRQSTATUS_RAW_1
2Ch GPIO_IRQSTATUS_0
30h GPIO_IRQSTATUS_1
34h GPIO_IRQSTATUS_SET_0
38h GPIO_IRQSTATUS_SET_1

3Ch GPIO_IRQSTATUS_CLR_0
40h GPIO_IRQSTATUS_CLR_1
44h GPIO_IRQWAKEN_0
48h GPIO_IRQWAKEN_1
114h GPIO_SYSSTATUS
130h GPIO_CTRL
134h GPIO_OE
138h GPIO_DATAIN
13Ch GPIO_DATAOUT
140h GPIO_LEVELDETECT0
144h GPIO_LEVELDETECT1
148h GPIO_RISINGDETECT
14Ch GPIO_FALLINGDETECT
150h GPIO_DEBOUNCENABLE
154h GPIO_DEBOUNCINGTIME
190h GPIO_CLEARDATAOUT
194h GPIO_SETDATAOUT

a) How is a value of 0 written into gpio44?


gpio44 => 44 = 32+12 => bank 1, (1<<12)
Write (1<<12) into Bank1, GPIO_CLEARDATAOUT
b) How is a value of 1 written into gpio44?
Write (1<<12) into Bank 1, GPIO_SETDATAOUT
c) How is the value in gpio44 determined?
Read Bank 1, GPIO_DATAIN, bitwise and with (1<<12), if 0, value was 0, if not zero, value
was 1.

Using the pad control registers below, what do pad control values of 0x27, 0x37, and 0x2F
mean?
Table 9-1. Pad Control Register Field Descriptions
Bit

Field

317

Reserved

Reserved. Read returns 0.

SLEWCTRL

Select between faster or slower slew rate.

Value

Description

Fast

1
5

2-0

Slow
Input enable value for the Pad. Set to 0 for output only. Set
to 1 for input or output.

RXACTIVE
0

Receiver disabled

Receiver enabled

PULLTYPESEL

Pad pullup/pulldown type selection


0

Pulldown selected

Pullup selected

PULLUDEN

Pad Pullup/pulldown enable


0

Pullup/pulldown enabled.

Pullup/pulldown disabled.

MUXMODE

Pad functional signal mux select

0x27 => 00100111 => |0|1|0|0|111| => Fast slew, Receiver enabled, Pulldown
selected, PU/PD enabled, mode 7
0x37 => 00110111 => |0|1|1|0|111| => Fast slew, Receiver enabled, Pullup
selected, PU/PD enabled, mode 7
0x2F => 00101111 => |0|1|0|1|111| => Fast slew, Receiver enabled, Pulldown
selected, PU/PD disabled, mode 7
Quiz 5
What is looked for in data sheets to check for BBB compatibility?
V 0 <-> 3.3 for gpio, 1.8 for ADC
A 0 <-> 4ma

What are the symbols for the bitwise and, or, and exclusive-or
operators?
For "and" operator : &
for "or" operator: |
for excluse-or operator: ^

The KM2520EC01 LED has a forward current of 5mA and forward voltage of 1.9V. The NPN
transistor 2N2222 has a current gain of 80. Resistors are connected to the base, collector,
and emitter of the transistor and are labeled RB, RC, and RE respectively. The resistor RB is
connected to a gpio pin on your BBB, the resistor RC is connected to the LED and the LED is
connected to 3.3VDC, and the resistor RE is connected to ground. The spec sheets for the
transistor say that VBE(sat) = 0.7V and VCE(sat) = 0.2V.
Determine the resistance values and verify that the transistor is in saturation.

RB 3.3v = RB * iB + VBE => 3.3v = RB * 2ma + 0.7v => RB = (3.3 0.7) / .002 = 1300
RC 5v = VF + RC * IF + VCE => 5v = 1.9v + RC * 5ma + .3v => RC
= (5 - 1.9 - .3)/.005 = 560
RE 0

The 74LVC138A is a 3-to-8 line decoder/demultiplexer. It accepts three binary


weighted address inputs (A0, A1 and A2) and, when enabled, provides eight
mutually exclusive outputs (Y0 to Y7) that are LOW when selected.
There are three enable inputs: two active LOW (E1 and E2) and one active HIGH (E3).
Every output will be HIGH unless E1 and E2 are LOW and E3 is HIGH.
The eight outputs are connected to an led and 220 ohm series resistor to 5VDC.
The three inputs, A0, A1 and A2, are connected to three gpio pins, bits 15, 17,
and 20, in bank 3.
a) How should the enable inputs, E1, E2, and E3 be connected to enable the device?

b) Write a snippet of c code to turn the leds on sequentially from Y0 to Y7.

a) How should the enable inputs, E1, E2, and E3 be connected to enable the device?
E1 and E2 are low active, so ground
E3 is high active so connect to VCC

b) Write a snippet of c code to turn the leds on sequentially from Y0 to Y7.


bank 3 bits 15, 17, 20 => 96 + 15, 17, 20 = 111, 113, 116
configure three gpio's for output
for i = 0 ; i != 8 ; ++i
write 0 to the three leds
if i & 1 then wrtie 1 to gpio111
if i & 2 then write 1 to gpio113
if i & 4 then write 1 to gpio116
wait for a bit

You have a device connected to two output gpio pins on your BBB. One output, DOUT, is a
data bit and the other output, DAV, is a data available bit. When you place a data bit on the
gpio, you signal that the data is available by issuing a 1->0 transition on the data available
bit. On your BBB, DOUT corresponds to P9_23, or gpio49, and DAV corresponds to P9_14, or
gpio50.
Write a c function that takes an unsigned char as an input parameter and output that
unsigned char, most significant bit first.
You have the following functions available for your use.
#ifndef _GPIO_UTILS_H_
#define _GPIO_UTILS_H_
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
int gpio_export(unsigned int gpio);
int gpio_unexport(unsigned int gpio);
int gpio_set_dir(unsigned int gpio, const char* dir);
int gpio_set_value(unsigned int gpio, unsigned int value);
int gpio_get_value(unsigned int gpio, unsigned int *value);
int gpio_set_edge(unsigned int gpio, const char *edge);
int gpio_fd_open(unsigned int gpio, unsigned int dir);
int gpio_fd_close(int fd);
#endif
void writeByte(unsigned char uc)
{
int i, val;
/*
** These might be run external to this function
** gpio_export( 49 );

** gpio_export( 50 );
*/
for( i = 0 ; i != 8 ; ++i, uc <<= 1 )
{
gpio_set_value( 39, (uc & 128) ? 1 ; 0 );
gpio_set_value( 40, 0 );
gpio_set_value( 40, 1 );
}
/*
** These might be run external to this function
** gpio_unexport( 49 );
** gpio_unexport( 50 );
*/
}

Explain the difference between the bitwise and and logical and
operators.
ogical && binary operator, boolean operation, true/false - in integer datatype zero is false,
one or more bits is true
true && true is true
true && false is false
false && false is false
bitwise & binary operator, works on pairs of bits in corresponding positions in integer
datatype
1 & 1 is 1
0 & 1 is 0
0 & 0 is 0

You might also like