Professional Documents
Culture Documents
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.
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.
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
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
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
SLEWCTRL
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
Pulldown selected
Pullup selected
PULLUDEN
Pullup/pulldown enabled.
Pullup/pulldown disabled.
MUXMODE
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
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
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