1*** Variables ***
2${UART}                             sysbus.usart3
3
4${PROJECT_URL}                      https://dl.antmicro.com/projects/renode
5${ECHO_SERVER}                      ${PROJECT_URL}/zephyr-nucleo_h753zi_echo_server.elf-s_3820436-2a55e73d28b438666b588d87cc9822365ee46cf6
6${ECHO_CLIENT}                      ${PROJECT_URL}/zephyr-nucleo_h753zi_echo_client.elf-s_3773508-692f892b406f5a4a0aedb4afe120acd26f420d21
7${BLINKY}                           ${PROJECT_URL}/zephyr--nucleo-h753zi-blinky.elf-s_586204-d9aa33947652eb18930088c06704ad6a8cdc7fa4
8${BUTTON}                           ${PROJECT_URL}/zephyr--nucleo_h753zi_button_sample.elf-s_582696-3d5e6775a24c75e8fff6b812d5bc850b361e3d93
9${CRYPTO_GCM}                       ${PROJECT_URL}/stm32cubeh7--stm32h753zi-CRYP_AESGCM.elf-s_2136368-45a90683e4f954667a464fc8fa9ce57d0b74ac09
10${CRYPTO_GCM_IT}                    ${PROJECT_URL}/stm32cubeh7--stm32h753zi-CRYP_AESGCM_IT.elf-s_2137876-ee038aa93bf68cb91af9894e0be9584eec3057e5
11${QSPI_RW}                          ${PROJECT_URL}/stm32cubeh7--stm32h753zi-QSPI_ReadWrite_IT.elf-s_2146460-5c6870c2698fe33a9ef78ce791d9e83439328cc4
12${QSPI_MemMapped}                   ${PROJECT_URL}/stm32cubeh7--stm32h753zi-QSPI_MemoryMapped.elf-s_2152312-faec8bb984c61aabb52ae9eec1598999ea906a1c
13${QSPI_XIP}                         ${PROJECT_URL}/stm32cubeh7--stm32h753zi-QSPI_ExecuteInPlace.elf-s_2233412-67befe44572c483b242a95ce8e714f75f4e7dc69
14
15${PLATFORM}                         @platforms/boards/nucleo_h753zi.repl
16
17${EVAL_STUB}=    SEPARATOR=
18...  """                                                                  ${\n}
19...  led1: Miscellaneous.LED @ gpioPortF 10 { invert: true }              ${\n}
20...  led3: Miscellaneous.LED @ gpioPortA 4 { invert: true }               ${\n}
21...  gpioPortF:                                                           ${\n}
22...  ${SPACE*4}10 -> led1@0                                               ${\n}
23...                                                                       ${\n}
24...  gpioPortA:                                                           ${\n}
25...  ${SPACE*4}4 -> led3@0                                                ${\n}
26...  """
27
28# The address of the memory is mentioned in the MCU docs, when the QSPI is configured to operate in memory-mapped mode
29# So it's added along with the external flash
30${EXTERNAL_FLASH}=   SEPARATOR=
31...    """
32...    externalQspiFlash: SPI.Macronix_MX25R @ qspi {underlyingMemory: qspiMappedFlashMemory}   ${\n}
33...    qspiMappedFlashMemory: Memory.MappedMemory @ sysbus 0x90000000 { size: 0x10000000 }  ${\n}
34...    """
35
36*** Keywords ***
37Create Setup
38    Execute Command                 emulation CreateSwitch "switch"
39
40    Create Machine                  ${ECHO_SERVER}  server
41    Execute Command                 connector Connect sysbus.ethernet switch
42    Create Machine                  ${ECHO_CLIENT}  client
43    Execute Command                 connector Connect sysbus.ethernet switch
44
45Create Machine
46    [Arguments]                     ${elf}  ${name}
47
48    Execute Command                 mach add "${name}"
49    Execute Command                 mach set "${name}"
50    Execute Command                 machine LoadPlatformDescription ${PLATFORM}
51
52    Execute Command                 sysbus LoadELF @${elf}
53
54Assert PC Equals
55    [Arguments]            ${expected}
56    ${pc}=                 Execute Command  sysbus.cpu PC
57    Should Be Equal As Integers  ${pc}  ${expected}
58
59*** Test Cases ***
60Should Talk Over Ethernet
61    Create Setup
62    ${server}=  Create Terminal Tester          ${UART}  machine=server  defaultPauseEmulation=True
63    ${client}=  Create Terminal Tester          ${UART}  machine=client  defaultPauseEmulation=True
64
65    Wait For Line On Uart           Initializing network                                                 testerId=${server}
66    Wait For Line On Uart           Run echo server                                                      testerId=${server}
67    Wait For Line On Uart           Network connected                                                    testerId=${server}
68    Wait For Line On Uart           Waiting for TCP connection                                           testerId=${server}
69
70    Wait For Line On Uart           Initializing network                                                 testerId=${client}
71    Wait For Line On Uart           Run echo client                                                      testerId=${client}
72    Wait For Line On Uart           Network connected                                                    testerId=${client}
73
74    Wait For Line On Uart           Accepted connection                                                  testerId=${server}
75
76    Wait For Line On Uart           Sent                                                                 testerId=${client}
77    Wait For Line On Uart           Received and replied                                                 testerId=${server}
78    Wait For Line On Uart           Received and compared \\d+ bytes, all ok                             testerId=${client}   treatAsRegex=true
79
80    Wait For Line On Uart           Sent                                                                 testerId=${client}
81    Wait For Line On Uart           Received and replied                                                 testerId=${server}
82    Wait For Line On Uart           Received and compared \\d+ bytes, all ok                             testerId=${client}   treatAsRegex=true
83
84    Wait For Line On Uart           Sent                                                                 testerId=${client}
85    Wait For Line On Uart           Received and replied                                                 testerId=${server}
86    Wait For Line On Uart           Received and compared \\d+ bytes, all ok                             testerId=${client}   treatAsRegex=true
87
88    Wait For Line On Uart           Sent                                                                 testerId=${client}
89    Wait For Line On Uart           Received and replied                                                 testerId=${server}
90    Wait For Line On Uart           Received and compared \\d+ bytes, all ok                             testerId=${client}   treatAsRegex=true
91
92Should Blink Led
93    Create Machine                  ${BLINKY}  blinky
94
95    Create Terminal Tester          ${UART}                                       defaultPauseEmulation=True
96    Create LED Tester               sysbus.gpioPortB.GreenLED                     defaultTimeout=1
97
98    Wait For Line On Uart           *** Booting Zephyr OS                         includeUnfinishedLine=true
99    Wait For Line On Uart           LED state: (ON|OFF)                           treatAsRegex=true
100
101    Assert LED Is Blinking          testDuration=8  onDuration=1  offDuration=1  pauseEmulation=true
102
103Should See Button Press
104    Create Machine                  ${BUTTON}  button
105
106    Create Terminal Tester          ${UART}                                       defaultPauseEmulation=True
107    Create LED Tester               sysbus.gpioPortB.GreenLED                     defaultTimeout=1
108
109    Wait For Line On Uart           *** Booting Zephyr OS                         includeUnfinishedLine=true
110    Wait For Line On Uart           Press the button
111    Assert LED State                false
112
113    Execute Command                 sysbus.gpioPortC.UserButton1 Press
114    Wait For Line On Uart           Button pressed at                             includeUnfinishedLine=true
115    Assert LED State                true
116    Execute Command                 sysbus.gpioPortC.UserButton1 Release
117    Assert LED State                false
118
119Should Encrypt And Decrypt Data in AES GCM Mode
120    Create Machine                  ${CRYPTO_GCM}  crypt-gcm
121    # This sample is built for STM32 Evaluation Kit, which uses the same SoC but has a bit different HW - we only care about LEDs to signal test status
122    Execute Command                 machine LoadPlatformDescriptionFromString ${EVAL_STUB}
123
124    ${led3_tester}=                 Create LED Tester   sysbus.gpioPortA.led3     defaultTimeout=1
125    ${led1_tester}=                 Create LED Tester   sysbus.gpioPortF.led1     defaultTimeout=1
126
127    # LED3 would be set if at any point of the test a failure occurred (e.g. on invalid MAC or ciphertext not matching the expected value)
128    # LED1 is set at the very end of the test, when the entire procedure is complete with no failures
129    Assert LED State                false    testerId=${led3_tester}
130    Assert LED State                true     testerId=${led1_tester}
131
132Should Encrypt And Decrypt Data in AES GCM Mode With Interrupts
133    Create Machine                  ${CRYPTO_GCM_IT}  crypt-gcm
134    Execute Command                 machine LoadPlatformDescriptionFromString ${EVAL_STUB}
135
136    ${led3_tester}=                 Create LED Tester   sysbus.gpioPortA.led3     defaultTimeout=1
137    ${led1_tester}=                 Create LED Tester   sysbus.gpioPortF.led1     defaultTimeout=1
138
139    # See `Should Encrypt And Decrypt Data in AES GCM Mode` for explanation
140    Assert LED State                false    testerId=${led3_tester}
141    Assert LED State                true     testerId=${led1_tester}
142
143Should Program Flash With QSPI
144    Create Machine                  ${QSPI_RW}  qspi
145    # This sample is built for STM32 Evaluation Kit, which uses the same SoC but has a bit different HW - we only care about LEDs to signal test status
146    Execute Command                 machine LoadPlatformDescriptionFromString ${EVAL_STUB}
147    Execute Command                 machine LoadPlatformDescriptionFromString ${EXTERNAL_FLASH}
148
149    ${led3_tester}=                 Create LED Tester   sysbus.gpioPortA.led3     defaultTimeout=1
150    ${led1_tester}=                 Create LED Tester   sysbus.gpioPortF.led1     defaultTimeout=1
151
152    # Wait for drivers to configure GPIO
153    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
154    Assert LED State                false    testerId=${led1_tester}     pauseEmulation=true
155
156    # LED1 means that the data was uploaded to flash, and the comparison with the base was successful
157    # LED2 should inform about comparison success, but we lack the necessary peripherals to configure it
158    # the sample has been modified instead to halt on first comparison error and turn LED3 on
159    Assert LED State                true     testerId=${led1_tester}     pauseEmulation=true     timeout=10
160    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
161
162    # And again - LED 1 toggles each time a transfer round completes
163    Assert LED State                false    testerId=${led1_tester}     pauseEmulation=true
164    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
165
166    Assert LED State                true     testerId=${led1_tester}     pauseEmulation=true
167    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
168
169Should Program Flash With QSPI Memory Mapped
170    Create Machine                  ${QSPI_MemMapped}  qspi
171    # This sample is built for STM32 Evaluation Kit, which uses the same SoC but has a bit different HW - we only care about LEDs to signal test status
172    Execute Command                 machine LoadPlatformDescriptionFromString ${EVAL_STUB}
173    Execute Command                 machine LoadPlatformDescriptionFromString ${EXTERNAL_FLASH}
174
175    ${led3_tester}=                 Create LED Tester   sysbus.gpioPortA.led3     defaultTimeout=1
176    ${led1_tester}=                 Create LED Tester   sysbus.gpioPortF.led1     defaultTimeout=1
177
178    # Wait for drivers to configure GPIO
179    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
180    Assert LED State                false    testerId=${led1_tester}     pauseEmulation=true
181
182    # LED1 means that the data was uploaded to flash, and the comparison with the base was successful
183    Assert LED State                true     testerId=${led1_tester}     pauseEmulation=true    timeout=10
184    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
185
186    # And again - LED 1 toggles each time a transfer round completes
187    Assert LED State                false    testerId=${led1_tester}     pauseEmulation=true
188    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
189
190    Assert LED State                true     testerId=${led1_tester}     pauseEmulation=true
191    Assert LED State                false    testerId=${led3_tester}     pauseEmulation=true
192
193# This sample normally would use MDMA to transfer data, but has been switched to interrupt mode instead
194# Additionally, unsupported LEDs are disabled
195# Instead of blinking LEDs periodically, it will spin forever after turning them on, on test success
196Should Program Flash With QSPI and use XIP
197    Create Machine                  ${QSPI_XIP}  qspi
198    # This sample is built for STM32 Evaluation Kit, which uses the same SoC but has a bit different HW - we only care about LEDs to signal test status
199    Execute Command                 machine LoadPlatformDescriptionFromString ${EVAL_STUB}
200    Execute Command                 machine LoadPlatformDescriptionFromString ${EXTERNAL_FLASH}
201
202    ${led3_tester}=                 Create LED Tester   sysbus.gpioPortA.led3     defaultTimeout=1
203    ${led1_tester}=                 Create LED Tester   sysbus.gpioPortF.led1     defaultTimeout=1
204
205    # Wait for drivers to configure GPIO
206    Assert LED State                false    testerId=${led3_tester}     timeout=20  pauseEmulation=true
207    Assert LED State                false    testerId=${led1_tester}                 pauseEmulation=true
208
209    # If the LEDs turned on, it means that the code relocated to QSPI memory is being executed
210    Assert LED State                true     testerId=${led1_tester}     timeout=10  pauseEmulation=true
211    Assert LED State                true     testerId=${led3_tester}                 pauseEmulation=true
212
213    # Get out of GPIO init functions, which are located in regular memory
214    # and step into infinite spin-loop in the QSPI memory
215    Execute Command                 emulation RunFor "00:00:00.01"
216    # QSPI memory starts at:        0x90000000
217    Assert PC Equals                0x90000010
218