Skip to content

Commit af21b16

Browse files
committed
Added Lab3 example which is built for STM32F4 Discovery Kit and run on Renode Simulator and displays results of unit tests on UART terminal
1 parent 384db0b commit af21b16

19 files changed

Lines changed: 4351 additions & 0 deletions

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ jobs:
2121
run: |
2222
make -C Lab2
2323
./Lab2/Lab2
24+
- name: Install Renode
25+
run: |
26+
.Lab3/install_renode.sh
27+
- name: Build and Run Lab3
28+
run: |
29+
make run -C Lab3

Lab2/checksum.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "checksum.h"
2+
#include <stddef.h>
23

34
uint8_t calculate_checksum(uint8_t *buffer, uint8_t buffer_length)
45
{
6+
if (buffer == NULL || buffer_length == 0) {
7+
return 0; // Return 0 for null buffer or zero length
8+
}
9+
510
uint16_t result_checksum = 0;
611
uint8_t loop_index = 0;
712

Lab2/test_checksum.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,36 @@ void test_calculate_checksum_with_max_values(void) {
3434
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
3535
}
3636

37+
void test_calculate_checksum_with_null_buffer(void) {
38+
uint8_t *data = NULL;
39+
uint8_t expected_checksum = 0; // Null buffer should return 0
40+
uint8_t actual_checksum = calculate_checksum(data, 10); // Length is arbitrary since buffer is NULL
41+
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
42+
}
43+
44+
void test_calculate_checksum_with_zero_length(void) {
45+
uint8_t data[] = {0x01, 0x02, 0x03};
46+
uint8_t expected_checksum = 0; // Zero length should return 0
47+
uint8_t actual_checksum = calculate_checksum(data, 0);
48+
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
49+
}
50+
51+
void test_calculate_zero_checksum(void) {
52+
uint8_t data[] = {0x00, 0x00, 0x00, 0x00};
53+
uint8_t expected_checksum = 0; // All zeros should yield a checksum of 0
54+
uint8_t actual_checksum = calculate_checksum(data, sizeof(data));
55+
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
56+
}
57+
3758
int main(void) {
3859
UNITY_BEGIN();
3960
RUN_TEST(test_calculate_checksum_with_valid_data);
4061
RUN_TEST(test_calculate_checksum_with_empty_data);
4162
RUN_TEST(test_calculate_checksum_with_max_values);
63+
RUN_TEST(test_calculate_checksum_with_null_buffer);
64+
RUN_TEST(test_calculate_checksum_with_zero_length);
65+
RUN_TEST(test_calculate_zero_checksum);
66+
4267
return UNITY_END();
4368
}
4469

Lab3/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
TARGET = Lab3
2+
CC = arm-none-eabi-gcc
3+
4+
CFLAGS = -mcpu=cortex-m4 -mthumb \
5+
-O0 -g \
6+
-ffreestanding -fno-builtin\
7+
-Isrc -Iunity -Istm32f4 \
8+
-Wall -Wextra
9+
10+
LDFLAGS = -T stm32f4/linker.ld \
11+
-nostartfiles \
12+
-Wl,--gc-sections
13+
14+
SRC = \
15+
stm32f4/startup.c \
16+
stm32f4/uart.c \
17+
src/checksum.c \
18+
src/test_checksum.c \
19+
src/syscalls.c \
20+
unity/unity.c
21+
22+
all:
23+
$(CC) $(CFLAGS) $(SRC) $(LDFLAGS) -o $(TARGET).elf
24+
25+
clean:
26+
rm -f *.elf
27+
28+
run: all
29+
renode --disable-xwt renode/stm32.resc

Lab3/README.MD

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Running STM32 Binary on Renode
2+
* This Lab example uses STM32F4 Discovery kit based example.
3+
* You can run any firmware built for STM32F407 MCU.
4+
* Once you build your project, update the below instruction under renode/stm32.resc to load new binary file:
5+
```
6+
sysbus LoadELF @Lab3.elf
7+
```
8+
9+
# Build and run
10+
* You can use Makefile in this folder to build and run
11+
* To Build:
12+
```sh
13+
make all
14+
```
15+
16+
* To run Renode simulator:
17+
```sh
18+
make run
19+
```
20+
21+
* To see Renode GUI terminal for logs and output:
22+
```
23+
renode renode/stm32.resc
24+
```

Lab3/install_tools.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
sudo apt-get update
3+
sudo apt-get install -y gcc-arm-none-eabi gdb-multiarch binutils-arm-none-eabi
4+
5+
wget https://github.com/renode/renode/releases/download/v1.16.1/renode_1.16.1_amd64.deb
6+
sudo dpkg -i renode_1.16.1_amd64.deb
7+
rm renode_1.16.1_amd64.deb

Lab3/renode/stm32.resc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mach create
2+
machine LoadPlatformDescription @platforms/boards/stm32f4_discovery-kit.repl
3+
4+
sysbus LoadELF @Lab3.elf
5+
6+
showAnalyzer sysbus.usart2
7+
8+
sysbus.usart2 AddLineHook "TEST_RESULT:" "#print 'completed'";quit
9+
10+
start

Lab3/src/checksum.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "checksum.h"
2+
3+
uint8_t calculate_checksum(uint8_t *buffer, uint8_t buffer_length)
4+
{
5+
uint16_t result_checksum = 0;
6+
uint8_t loop_index = 0;
7+
8+
for (loop_index = 0; loop_index < buffer_length; loop_index ++)
9+
{
10+
result_checksum += buffer[loop_index ];
11+
}
12+
return (uint8_t)(result_checksum % 256);
13+
}

Lab3/src/checksum.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @file checksum.h
3+
* @brief Header file contains the declarations for checksum module
4+
*
5+
*/
6+
7+
#ifndef CHECKSUM_H
8+
#define CHECKSUM_H
9+
10+
#include <stdint.h>
11+
12+
/**
13+
* @brief Function to calculate the checksum of a given buffer of data.
14+
* The checksum is computed by summing all the bytes in the buffer and taking the result modulo 256.
15+
*
16+
* @param buffer Buffer of data for which the checksum is to be calculated.
17+
* @param length Length of the buffer.
18+
* @return uint8_t The calculated checksum result.
19+
*/
20+
uint8_t calculate_checksum(uint8_t *buffer, uint8_t length);
21+
22+
#endif

Lab3/src/syscalls.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <sys/stat.h>
2+
3+
/* Stub implementations of system calls for a bare-metal environment.
4+
These functions are required by the C library but are not used in this context.
5+
*/
6+
int _close(int file)
7+
{
8+
(void)file;
9+
return -1;
10+
}
11+
12+
int _fstat(int file, struct stat *st)
13+
{
14+
(void)file;
15+
st->st_mode = S_IFCHR;
16+
return 0;
17+
}
18+
19+
int _isatty(int file)
20+
{
21+
(void)file;
22+
return 1;
23+
}
24+
25+
int _lseek(int file, int ptr, int dir)
26+
{
27+
(void)file;
28+
(void)ptr;
29+
(void)dir;
30+
return 0;
31+
}
32+
33+
int _read(int file, char *ptr, int len)
34+
{
35+
(void)file;
36+
(void)ptr;
37+
(void)len;
38+
return 0;
39+
}
40+
41+
int _write(int file, char *ptr, int len)
42+
{
43+
(void)file;
44+
(void)ptr;
45+
return len;
46+
}
47+
48+
int _kill(int pid, int sig)
49+
{
50+
(void)pid;
51+
(void)sig;
52+
return -1;
53+
}
54+
55+
int _getpid(void)
56+
{
57+
return 1;
58+
}

0 commit comments

Comments
 (0)