[go: up one dir, main page]

0% found this document useful (0 votes)
34 views14 pages

Embedded Tutorial Solutions-1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

ESD Solutions By AG

Tutorial -01 Unit 3

Q. A watchdog timer that uses two cascaded 16 bit up counters as shown in figure below
is connected to an 11.981 MHz oscillator. A timeout should occur if the function
watchdog_reset is not called in 5 minutes. What is the value that should be loaded into
the counter pair when the function is called?

Sol:

The question asks for the value that should be loaded into the counter pair of a watchdog timer
when the function watchdog_reset is called. The watchdog timer uses two cascaded 16-bit up
counters and is connected to an 11.981 MHz oscillator. A timeout should occur if the function
watchdog_reset is not called within 5 minutes.
AG
Steps to solve:

1. Calculate the timer resolution:


○ The resolution of the timer is the reciprocal of the clock frequency.
○ Clock frequency = 11.981 MHz = 11.981 * 10^6 Hz
○ Resolution = 1 / Clock frequency = 1 / (11.981 * 10^6 Hz) ≈ 83.47 nanoseconds
○ Total Bits of counter pair ‘n’ = 2 * 16 = 32 bits
○ The range of counter pair = T(Resolution) * 2^n = 0.08347 * 2^32 = 358.5 sec
○ Timeout time = t =5 min = 300 sec
○ Loaded time = range - timeout = 358.5 - 300 = 58.5 sec
○ Loaded value into the counter pair = loaded time/T(Resolution) = 58.5 sec / 83.47
nanoseconds = 700850605

Tutorial -03 Unit 3

Write driver to support LM75A digital temperature sensor through I2C. Assume APIs for
I2C interface as listed below are available.

uint32_t I2C_Init (void); uint32_t I2C_Start (void);

uint32_t I2C_Stop (void);

uint32_t I2C_Addr (uint8_t adr, uint8_t dir); //adr: Device address dir: direction bit
ESD Solutions By AG

uint32_t I2C_Write (uint8_t byte); // byte: byte to be written

uint32_t I2C_Read (uint32_t ack, uint8_t *byte);//ack=1 if ACK is to be generated


otherwise 0, byte: pointer to buffer to store data read

//Return type of the these functions is 1 used to indicate error condition otherwise 0. //
I2C_Addr(..), I2C_Write(…) waits for acknowledgement. If no acknowledgement error is
condition is generated.

Sol:

LM75_Read (int8_t slaveadd, int8_t pointer, int8_t size, int8_t *Pdata){


int32_t error = 0;
int8_t cnt = size;
int8_t buffer = Pdata;
do {
error |= I2C_Init();
error |= I2C_Start();
error |= I2C_Addr(slaveadd, 0);
error |= I2C_Write(Pointer);
error |= I2C_Start();
AG error |= I2C_Addr(slaveadd, 1);
while (error != 1 && cnt--) {
error = I2C_Read(cnt, buffer++);
}
error |= I2C_Stop();
} while (error == 1);
}
LM75_Write (int8_t slaveadd, int8_t pointer, int8_t size, int8_t *Pdata){
int32_t error = 0;
int8_t cnt = size;
int8_t buffer = Pdata;
do {
error |= I2C_Init();
error |= I2C_Start();
error |= I2C_Addr(slaveadd, 0);
error |= I2C_Write(Pointer);
while (error != 1 && cnt--) {
error = I2C_Write(*buffer--);
}
error |= I2C_Stop();
} while (error == 1);
}

void LM75_Read (int8_t slaveadd, int8_t pointer, int8_t size, int8_t *pointerD);
void LM75_Write(int8_t slaveadd, int8_t pointer, int8_t size, int8_t *pointerD);
void LM75_Configuration(int8_t slaveadd, int8_t config, int16_t tempos, int 16_t temphys)
{
int8_t Pdata = Config;
int8_t array[2];
LM75_Write(slaveadd, 0x01, 1, &Pdata);
ESD Solutions By AG

temphys *= 2;
array[0] = ((temphys<< 7) & 0xFF00)>>8
array[1] = (temphys << 7)& 0xFF
LM75_Write(slaveadd, 0x02, 2, array);

uint16_t Read_Temp(int8_t slaveadd) {


int8_t pdata[2], int16_t temp;
LM75_Read(slaveadd,0x00,2,pdata);
temp = pdata[0] | pdata[1] >> 8
temp >>= 7;
temp |= 2;
return temp;
}
}

Tutorial -02 Unit 3


Q.The pin diagram shown in figure below is TLV2541 IC, a high performance, 12-bit, low
power, miniature, CMOS analog-to-digital Converter. The pin functions are listed as
below.

The conversion procedure is as follows:


AG
The falling edge of CS is the start of the cycle. The converter sample time is 12 SCLKs in
duration, beginning on the 5 th SCLK received after the converter has received a high-to-low CS
transition. The conversion is started after the 16th SCLK falling edge and takes 3.5 µs to
complete. The converted digital data is presented on SDO. The SDO data presented during a
cycle, is the result of the conversion of the sample taken during the previous cycle. TheTiming
diagrams are as shown below.Write driver firmware to support TLV2541. Use SPI type of serial
interface to connect TLV2541. Target MCU is STM32F407VG. Show interfacing diagram.

Solution:
(SPI can be used but the design will be complex so , we will be giving manual data inputs ,
using GPIOs)
Connections:
PA1 - GPIO_Input -> SDO
PA2 - GPIO_Output -> SCLK
PA0 - GPIO_Output -> CS bar
VDD and Vref both to High (VDD)
ESD Solutions By AG

GND - GND
AIN - Analog Input to a Analog sensor
NC - No connection

#include "main.h"
void SystemClock_Config();
void MX_GPIO_Init();

int16_t adcOut; // any name can be taken here


// referred as adcOut

void main() {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while(1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
for(int i=0; i<16; i++){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET);
HAL Delay (1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
AG
HAL-Delay (1);
if (i<12) {
adcOut = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1);
adcout<<= 1;
}
}
HAL-Delay(1);
}

Tutorial -04 Unit 3


Q. Write application code to execute on STM32F407VG MCU for setting RTC to current
time(12 Hour format). And also write program statements to read the time and date. Use
blocking functions shown in slide 55-57.

Solution:
#include "main.h"

I2C_HandleTypeDef hi2c1;

// Function to initialize I2C1


void MX_I2C1_Init(void);

// Function to set RTC time


void setRTCTime(uint8_t hours, uint8_t minutes, uint8_t seconds) {
uint8_t timeData[] = {seconds, minutes, hours}; // RTC register order
if (HAL_I2C_IsDeviceReady(&hi2c1, RTC_I2C_ADDRESS, 10, 100) == HAL_OK) {
HAL_I2C_Master_Transmit(&hi2c1, RTC_I2C_ADDRESS, timeData, sizeof(timeData), 100);
}
}
ESD Solutions By AG

// Function to read RTC time


void readRTCTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds) {
uint8_t timeData[3];
if (HAL_I2C_IsDeviceReady(&hi2c1, RTC_I2C_ADDRESS, 10, 100) == HAL_OK) {
HAL_I2C_Master_Receive(&hi2c1, RTC_I2C_ADDRESS, timeData, sizeof(timeData), 100);
*seconds = timeData[0];
*minutes = timeData[1];
*hours = timeData[2] & 0x1F; // Mask for 12-hour format
}
}

// Function to read RTC date


void readRTCDate(uint8_t *day, uint8_t *month, uint8_t *year) {
// Implement additional I2C communication to read date registers
// based on your specific RTC model and date format
// ...
}

int main(void) {
// Initialize HAL, system clock, I2C1, etc.
AGHAL_Init();
SystemClock_Config();
MX_I2C1_Init();

// Get current time (replace with your time-getting logic)


uint8_t currentHours = 10; // Example
uint8_t currentMinutes = 35;
uint8_t currentSeconds = 20;

// Set RTC time


setRTCTime(currentHours, currentMinutes, currentSeconds);

// Read RTC time and date after a delay


HAL_Delay(1000); // Wait for RTC to update
uint8_t readHours, readMinutes, readSeconds;
readRTCTime(&readHours, &readMinutes, &readSeconds);

uint8_t readDay, readMonth, readYear;


readRTCDate(&readDay, &readMonth, &readYear);

// Print time in 12-hour format


printf("Current time: %02d:%02d:%02d\n", readHours % 12, readMinutes, readSeconds);
printf("Current date: %02d/%02d/%02d\n", readDay, readMonth, readYear);
}
ESD Solutions By AG

Tutorial-01 Unit 3 (IWDT)

Q. The following functions are generated in STM32CubeMx for IWDT configuration:


// IWDT Handler
IWDG_HandleTypeDef hiwdg; //Initialize the IWDG according to the specified parameters in
the IWDG_InitTypeDef and start watchdog.

HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg) //IWDG Initialization


Function

void MX_IWDG_Init(void) //Refresh the IWDG

HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)

Design software code to use ADC and WDT. The system is expected to complete analog
to dgital conversion in 50 msec, otherwise reset may be issued to recover from probable
transient error. HAL functions of ADC can be found in next slide.

HAL generated:
ADC_HandleTypeDef hadc1; //ADC handler
AG
void MX_ADC1_Init();

HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc);

HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t


Timeout);

init16_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)

Solution:

#include “main.h”
ADC_HandleTypeDef hadc1;
IWDG_HandleTypeDef hiwdg;

void SystemClock_Config();
static void MX_GPIO_Init();
void MX_ADC1_intit();
void MX_ADC1_Init();
uint32_t analogValue;

int main(){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_IWDG_Init();
while(1){
HAL_ADC_Start(&hadc1);
HAL_IWDG_Init(&hiwdg);
ESD Solutions By AG

if(HAL_ADC_PollForConversion(&hadc1, 50) == HAL_OK){


analogValue = HAL_ADC_GetValue(&hadc1);
HAL_IWDG_Refresh(&hiwdg);
}
}
}

AG
ESD Solutions By AG

Unit 2
Tutorial -03 Unit 2
Q.A cache has 512KB capacity,4B word,64B block size and 8 way set associative. The
system is using 32 bit address. Given the address 0xABC89984, which set of cache will
be searched and specify which word of the selected cache block will be forwarded if it is
a hit in cache?
AG
Detailed Solution:

Cache Size = E * B * S
Block Size = 64Bytes
E = no. of lines per set = 8 (since 8 way set associative)
Cache size = 512KB

S = 512K/8*64 = 1024
WKT, S = 2^s
s= no. of sets = 10 byte
b= no. of bytes per line
B = 2^b => b= 6 byte
Since 4Byte is one word in cache, so for offset it will be 6 - 4 =2

For 32 bit address,

16bits - tag
10bits - set
Remaining 6bits - offset + word index
ESD Solutions By AG

Unit 2 tutorial 1b

Q. Consider a three level memory hierarchy with following specifications:

Design the memory hierarchy to achieve an effective memory access time t=10.04us with
cache hit ratios h1=0.98 and a hit ratio h2=0.9 in the main memory . Also limit the total
cost of the memory hierarchy is upper bounded by $15,000.

Solution:

t = t1 * h1 + (1-h1) * h2 * t2 + (1-h1) * (1-h2) * (t3)

t2 = t - (t1 * h1 + (1-h1) * (1-h2) * (t3)) / ((1-h1) * h2)


AG t2 = 111.97 us

Upper bound >= c1*s1 + c2*s2 + c3*s3

15000 = 1.25*512 + 32*10^3 * 0.2 + 0.0002 * s3

s3 = 39800000 = 39.8 Gbs

Tutorial 1a Unit 2
Q.Perform capacity planning for a two level memory hierarchy system. The first level, M1
is a cache with three capacity choices 64 Kbytes, 128 Kbytes and 256 Kbytes. The
second level, M2 is a main memory with a 4 Mbyte capacity. Let C1 and C2 be the cost
per byte and t1 and t2 the access times for M1 and M2 respectively. Assume C1 =20C2
and t2 =10t1. The cache hit ratios for the three capacities are assumed to be 0.7, 0.9 and
0.98 respectively.

i) What is the average access time ta in terms of t1=20ns in the three cache designs?

ii) Express the average byte cost of the entire memory hierarchy if C2=$0.2/Kbyte.

iii) Compare the three memory designs and indicate the order of merit in terms of
average costs and average access times respectively. Choose the optimal design based
on the product of average cost & average access times.

Solution:

i) The average access time ta in terms of t1=20ns in the three cache designs can be calculated
as follows:
ESD Solutions By AG

For M1 with 64 Kbytes capacity:


Access time = t1 + (1 - hit ratio) * t2
Access time = 20ns + (1 - 0.7) * 200ns
Access time = 80ns
For M1 with 128 Kbytes capacity:
Access time = t1 + (1 - hit ratio) * t2
Access time = 20ns + (1 - 0.9) * 200ns
Access time = 38ns
For M1 with 256 Kbytes capacity:
Access time = t1 + (1 - hit ratio) * t2
Access time = 20ns + (1 - 0.98) * 200ns
Access time = 24ns
Therefore, the average access times for the three cache designs are 80ns, 38ns, and 24ns
respectively.
ii) The average byte cost of the entire memory hierarchy can be calculated as follows:
Cost of M1 in 64K= C1 * (64K)
= 20C2 * (64K)= 20 ($0.2/Kbyte)*64 K= $256 / Kbyte
Cost of M1 in 128K= C1 * (128K)
= 20C2 * (128K)= $512 /Kbyte
Cost of M1 in 256K= C1 * (256K)
AG
= 20C2 * (256K)= $1024/Kbyte
Cost of M2 = C2 * 4M = $0.2/Kbyte * 4M = $800/Kbyte
Total cost in 64K= Cost of M1 + Cost of M2 = $256 + $800= $1056/Kbyte
Total cost in 128K= Cost of M1 + Cost of M2 = $512 +$800= $1312/Kbyte
Total cost in 256K= Cost of M1 + Cost of M2 = $1024+$800= $1824/Kbyte
The average byte cost of the entire memory hierarchy is $/byte.
iii) The order of merit in terms of average costs and average access times respectively is as
follows:
M1 with 256 Kbytes capacity: Average access time = 24ns, Average cost =
M1 with 128 Kbytes capacity: Average access time = 38ns, Average cost =
M1 with 64 Kbytes capacity: Average access time = 80ns, Average cost =
Therefore, the optimal design is M1 with 256 Kbytes capacity as it has the lowest product of
average cost and average access time.

(Unit 2 tutorial 2)
Q. Show Hamming code parity bits calculation for data bits 111011100011101. Show
decoding of data by considering single bit errors and multi bit errors.

Data : 111011100011101

Step 1 : leave blanks at 2 powers (for parity bits)


(2^0 2^1 2^2 2^3 ….)

123456789012345678901
?_1_110_1110001_11101
Position -1 check 1 leave 1
No. of 1 = 8 even parity
ESD Solutions By AG

0?1_110_1110001_11101
Position -2 check 2 leave 2
No. of 1 = 7 odd parity

011?110_1110001_11101
Position -4 check 4 leave 4
No. of 1 = 4 even

0110110?1110001_11101
Position -8 check 8 leave 8
No. of 1 = 4 even

011011001110001?11101
Position -16 check 16 leave 16
No. of 1 = 4 even

Final code word : 0 1 1 0 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 1

(Unit 2 tutorial -3)


Q.Write address range to connect STM m48z08 8 K x 8 bit RAM Chip following memory
AG
map shown in slide number 20 .
ESD Solutions By AG

8k * 8 bit RAM Chip

8k - Address lines each of 8 bits

= 8Kilobyte (1 Kilo Byte = 1024(2^10) Bytes)

=8*2^10 - 1 = 8191 bytes

Ans. Mapped from 0x8FF - 0x28FE

AG
ESD Solutions By AG

Unit 1

Tutorial 2 Unit 1
A new multimedia unit (MU) is added to a processor, which accelerates the
completion of multimedia instructions given to the processor by four times.
Assuming a program consists of 40% multimedia instructions, what is the
overall speedup gained while running the program when it is executed on
the processor with the new MU compared to when it is run on the
processor without this MU? Use Amdahl’s Law to calculate.

Solution:

Amdahl’s Law :

Overall Speed Up = 1/ ( (1-P) + (P/ S enhanced) )

Where
● Soverall is the overall speedup.
● P is the fraction of the workload that benefits from the enhancement.
● S enhanced is the speedup factor for the enhanced portion.
AG
4 times accelerated => S enhanced = 4
P = 40% = 0.4

S overall = 1.428

Tutorial 4 Unit 1:
The execution times (in seconds) of three programs on three MCUs are given below:

Consider three different computers, each executing 100,000,000 instructions. Calculate


the MIPS (Million Instructions Per Second) rating of each program on each of the three
machines. Based on these ratings, draw a clear conclusion regarding the relative
performance of the three computers.

Solution

𝑀𝐼𝑃𝑆 = 𝐶𝑙𝑜𝑐𝑘𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦/𝐶𝑃𝐼∗1,000,000
ESD Solutions By AG

CPI - Cycles per Instructions

(Assume)CPI = 1cycle/ 10^8 Instructions

Clock freq = 1/Execution time

For MCU A:

Prog 1 : MIPS = 10^8/(10*10^6) = 10 MIPS


Prog 2 : MIPS = 10^8/(1000*10^6) = 0.1 MIPS
Prog 3 : MIPS = 10^8/(500*10^6) = 0.2 MIPS

For MCU B:

Prog 1 : MIPS = 10^8/(1*10^6) = 100 MIPS


Prog 2 : MIPS = 10^8/(200*10^6) = 0.5 MIPS
Prog 3 : MIPS = 10^8/(200*10^6) = 0.5 MIPS
AG
For MCU C:

Prog 1 : MIPS = 10^8/(20*10^6) = 5 MIPS


Prog 2 : MIPS = 10^8/(20*10^6) = 5 MIPS
Prog 3 : MIPS = 10^8/(20*10^6) = 5 MIPS

MCU is C is reliable due to its consistent Execution time

You might also like