aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/STM32F7xx/USB_RAW/main.c
blob: f088eeebe8cbe62e12fd22ebb78d6bff31084059 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include <stdio.h>
#include <string.h>

#include "ch.h"
#include "hal.h"

#include "usbcfg.h"

static const uint8_t txbuf[] =
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

static uint8_t rxbuf[1024 + 1];

/*
 * USB writer. This thread writes data to the USB at maximum rate.
 * Can be measured using:
 *   dd if=/dev/xxxx of=/dev/null bs=512 count=10000
 */
static THD_WORKING_AREA(waWriter, 128);
static THD_FUNCTION(Writer, arg) {

  (void)arg;
  chRegSetThreadName("writer");
  while (true) {
    msg_t msg = usbTransmit(&USBD2, USBD2_DATA_REQUEST_EP,
                            txbuf, sizeof (txbuf));
    if (msg == MSG_RESET)
      chThdSleepMilliseconds(500);
  }
}

/*
 * USB reader. This thread reads data from the USB at maximum rate.
 * Can be measured using:
 *   dd if=bigfile of=/dev/xxx bs=512 count=10000
 */
static THD_WORKING_AREA(waReader, 128);
static THD_FUNCTION(Reader, arg) {

  (void)arg;
  chRegSetThreadName("reader");
  while (true) {
    msg_t msg = usbReceive(&USBD2, USBD2_DATA_AVAILABLE_EP,
                           rxbuf, sizeof (rxbuf));
    if (msg == MSG_RESET)
      chThdSleepMilliseconds(500);
  }
}

/*
 * Red LED blinker thread, times are in milliseconds.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (true) {
    systime_t time;

    time = USBD2.state == USB_ACTIVE ? 250 : 500;
    palClearLine(LINE_ARD_D13);
    chThdSleepMilliseconds(time);
    palSetLine(LINE_ARD_D13);
    chThdSleepMilliseconds(time);
  }
}

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * GPIOI1 is programmed as output (board LED).
   */
  palClearLine(LINE_ARD_D13);
  palSetLineMode(LINE_ARD_D13, PAL_MODE_OUTPUT_PUSHPULL);

  /*
   * Activates the USB driver and then the USB bus pull-up on D+.
   * Note, a delay is inserted in order to not have to disconnect the cable
   * after a reset.
   */
  usbDisconnectBus(&USBD2);
  chThdSleepMilliseconds(1500);
  usbStart(&USBD2, &usbcfg);
  usbConnectBus(&USBD2);

  /*
   * Starting threads.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  chThdCreateStatic(waWriter, sizeof(waWriter), NORMALPRIO, Writer, NULL);
  chThdCreateStatic(waReader, sizeof(waReader), NORMALPRIO, Reader, NULL);

  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state.
   */
  while (true) {
    chThdSleepMilliseconds(1000);
  }
}
ass="si">}" # $1: exit code do_exit() { if [ ${1} -eq ${EXIT_FAILURE} ] ; then echo "Result: FAILED" else echo "Result: PASSED" fi echo "restoring original bios image using system's flashrom" flashrom ${FLASHROM_PARAM} -w "$BIOS" echo "test files remain in ${TMPDIR}" cd "$OLDDIR" exit "$1" } # # Actual tests are performed below. # NUM_REGIONS=16 # Make a layout - 4K regions on 4K boundaries. This will test basic # functionality of erasing and writing specific blocks. echo " 0x000000:0x000fff 00_0 0x001000:0x001fff ff_0 0x002000:0x002fff 00_1 0x003000:0x003fff ff_1 0x004000:0x004fff 00_2 0x005000:0x005fff ff_2 0x006000:0x006fff 00_3 0x007000:0x007fff ff_3 0x008000:0x008fff 00_4 0x009000:0x009fff ff_4 0x00a000:0x00afff 00_5 0x00b000:0x00bfff ff_5 0x00c000:0x00cfff 00_6 0x00d000:0x00dfff ff_6 0x00e000:0x00efff 00_7 0x00f000:0x00ffff ff_7 0x010000:0x010fff 00_8 0x011000:0x011fff ff_8 0x012000:0x012fff 00_9 0x013000:0x013fff ff_9 0x014000:0x014fff 00_10 0x015000:0x015fff ff_10 0x016000:0x016fff 00_11 0x017000:0x017fff ff_11 0x018000:0x018fff 00_12 0x019000:0x019fff ff_12 0x01a000:0x01afff 00_13 0x01b000:0x01bfff ff_13 0x01c000:0x01cfff 00_14 0x01d000:0x01dfff ff_14 0x01e000:0x01efff 00_15 0x01f000:0x01ffff ff_15 " > layout_4k_aligned.txt cp "$BIOS" "$TESTFILE" i=0 while [ $i -lt $NUM_REGIONS ] ; do echo -n "aligned region ${i} test: " offset=$((${i} * 8192)) dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) 2> /dev/null ./flashrom ${FLASHROM_PARAM} -l layout_4k_aligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null if [ "$?" != "0" ] ; then echo "failed to flash region" do_exit "$EXIT_FAILURE" fi # download the entire ROM image and use diff to compare to ensure # flashrom logic does not violate user-specified regions flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null diff -q difftest.bin "$TESTFILE" if [ "$?" != "0" ] ; then echo "failed diff test" do_exit "$EXIT_FAILURE" fi rm -f difftest.bin i=$((${i} + 1)) echo "passed" done # Make a layout - 4K regions on 4.5K boundaries. This will help find problems # with logic that only operates on part of a block. For example, if a user # wishes to re-write a fraction of a block, then: # 1. The whole block must be erased. # 2. The old content must be restored at unspecified offsets. # 3. The new content must be written at specified offsets. # # Note: The last chunk of 0xff bytes is only 2K as to avoid overrunning a 128KB # test image. # echo " 0x000800:0x0017ff 00_0 0x001800:0x0027ff ff_0 0x002800:0x0037ff 00_1 0x003800:0x0047ff ff_1 0x004800:0x0057ff 00_2 0x005800:0x0067ff ff_2 0x006800:0x0077ff 00_3 0x007800:0x0087ff ff_3 0x008800:0x0097ff 00_4 0x009800:0x00a7ff ff_4 0x00a800:0x00b7ff 00_5 0x00b800:0x00c7ff ff_5 0x00c800:0x00d7ff 00_6 0x00d800:0x00e7ff ff_6 0x00e800:0x00f7ff 00_7 0x00f800:0x0107ff ff_7 0x010800:0x0117ff 00_8 0x011800:0x0127ff ff_8 0x012800:0x0137ff 00_9 0x013800:0x0147ff ff_9 0x014800:0x0157ff 00_10 0x015800:0x0167ff ff_10 0x016800:0x0177ff 00_11 0x017800:0x0187ff ff_11 0x018800:0x0197ff 00_12 0x019800:0x01a7ff ff_12 0x01a800:0x01b7ff 00_13 0x01b800:0x01c7ff ff_13 0x01c800:0x01d7ff 00_14 0x01d800:0x01e7ff ff_14 0x01e800:0x01f7ff 00_15 0x01f800:0x01ffff ff_15 " > layout_unaligned.txt # reset the test file and ROM to the original state flashrom ${FLASHROM_PARAM} -w "$BIOS" > /dev/null cp "$BIOS" "$TESTFILE" i=0 while [ $i -lt $NUM_REGIONS ] ; do echo -n "unaligned region ${i} test: " offset=$(($((${i} * 8192)) + 2048)) # Protect against too long write writelen=4096 if [ $((${offset} + 4096 + 4096)) -ge 131072 ]; then writelen=$((131072 - $((${offset} + 4096)))) if [ ${writelen} -lt 0 ]; then writelen=0 fi fi dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) count=writelen 2> /dev/null ./flashrom ${FLASHROM_PARAM} -l layout_unaligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null if [ "$?" != "0" ] ; then echo "failed to flash region" do_exit "$EXIT_FAILURE" fi # download the entire ROM image and use diff to compare to ensure # flashrom logic does not violate user-specified regions flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null diff -q difftest.bin "$TESTFILE" if [ "$?" != "0" ] ; then echo "failed diff test" do_exit "$EXIT_FAILURE" fi rm -f difftest.bin i=$((${i} + 1)) echo "passed" done do_exit "$EXIT_SUCCESS"