aboutsummaryrefslogtreecommitdiff
path: root/stm32/unittest/src/tst_semihost.c
blob: a48b7e5b990515ecaf8e36f8ab877a0c8f3892cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <errno.h>

#include "semihosting.h"

#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "machdep.h"

#define min(a, b) ((a < b) ? (a) : (b))

int main(int argc, char *argv[]) {

    semihosting_init();

    printf("semihosting test - stdout\n");
    fprintf(stderr, "semihosting test - stderr\n");

    uint8_t buf[128];
    int count;
    int i;

    FILE *fin = fopen("stm_in.raw", "rb");
    if (!fin) {
        fprintf(stderr, "Error %d opening fin\n", errno);
    }
    setbuf(fin, NULL);

    FILE *fout = fopen("stm_out.raw", "wb");
    if (!fout) {
        fprintf(stderr, "Error %d opening fout\n", errno);
    }
    setbuf(fout, NULL);

    // Unrolled while loop for simpler debugging:
    // Pass 0: expect 16 bytes 00-0f
    printf("Pass 0: feof(fin) = %d\n", feof(fin));
    count = fread(&buf[0], 1, 16, fin);
    printf("read %d bytes: ", count);
    for (i=0; i<count; i++) printf(" %02x", buf[i]);
    printf("\nfeof(fin) = %d\n", feof(fin));
    for (i=0; i<min(count, 16); i++) buf[i] = ~buf[i];
    if (count) count = fwrite(&buf[0], 1, count, fout);
    printf("Wrote %d bytes\n\n", count);

    // Pass 1: expect 16 bytes 10-1f
    printf("Pass 1: feof(fin) = %d\n", feof(fin));
    count = fread(&buf[0], 1, 16, fin);
    printf("read %d bytes: ", count);
    for (i=0; i<count; i++) printf(" %02x", buf[i]);
    printf("\nfeof(fin) = %d\n", feof(fin));
    for (i=0; i<min(count, 16); i++) buf[i] = ~buf[i];
    if (count) count = fwrite(&buf[0], 1, count, fout);
    printf("Wrote %d bytes\n\n", count);

    // Pass 2: expect 3 bytes 20-22
    printf("Pass 2: feof(fin) = %d\n", feof(fin));
    count = fread(&buf[0], 1, 16, fin);
    printf("read %d bytes: ", count);
    for (i=0; i<count; i++) printf(" %02x", buf[i]);
    printf("\nfeof(fin) = %d\n", feof(fin));
    for (i=0; i<min(count, 16); i++) buf[i] = ~buf[i];
    if (count) count = fwrite(&buf[0], 1, count, fout);
    printf("Wrote %d bytes\n\n", count);
    
    // Pass 3: expect 0 result (EOF)
    printf("Pass 3: feof(fin) = %d\n", feof(fin));
    count = fread(&buf[0], 1, 16, fin);
    printf("read %d bytes: ", count);
    for (i=0; i<count; i++) printf(" %02x", buf[i]);
    printf("\nfeof(fin) = %d\n", feof(fin));
    for (i=0; i<min(count, 16); i++) buf[i] = ~buf[i];
    if (count) count = fwrite(&buf[0], 1, count, fout);
    printf("Wrote %d bytes\n\n", count);
    

    fclose(fin);
    fclose(fout);

    printf("End of test\n");
    fflush(stdout);
    fflush(stderr);
    
    return 0;
}

/* vi:set ts=4 et sts=4: */