2021-12-23 01:12:47 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-04-17 15:34:56 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "esp_sleep.h"
|
|
|
|
#include "soc/rtc_cntl_reg.h"
|
|
|
|
#include "soc/sens_reg.h"
|
|
|
|
#include "soc/rtc_periph.h"
|
2022-01-21 04:13:48 -05:00
|
|
|
#include "ulp_riscv.h"
|
2020-04-17 15:34:56 -04:00
|
|
|
#include "ulp_test_app.h"
|
|
|
|
#include "unity.h"
|
2021-12-23 01:12:47 -05:00
|
|
|
#include <sys/time.h>
|
2022-01-18 21:57:31 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2020-04-17 15:34:56 -04:00
|
|
|
|
|
|
|
typedef enum{
|
|
|
|
RISCV_READ_WRITE_TEST = 1,
|
|
|
|
RISCV_DEEP_SLEEP_WAKEUP_TEST,
|
|
|
|
RISCV_LIGHT_SLEEP_WAKEUP_TEST,
|
2022-01-18 21:57:31 -05:00
|
|
|
RISCV_STOP_TEST,
|
2020-04-17 15:34:56 -04:00
|
|
|
RISCV_NO_COMMAND,
|
|
|
|
} riscv_test_commands_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
RISCV_COMMAND_OK = 1,
|
|
|
|
RISCV_COMMAND_NOK,
|
2021-12-23 01:12:47 -05:00
|
|
|
RISCV_COMMAND_INVALID,
|
2020-04-17 15:34:56 -04:00
|
|
|
} riscv_test_command_reply_t;
|
|
|
|
|
|
|
|
#define XOR_MASK 0xDEADBEEF
|
2021-12-23 01:12:47 -05:00
|
|
|
#define ULP_WAKEUP_PERIOD 1000000 // 1 second
|
2020-04-17 15:34:56 -04:00
|
|
|
|
|
|
|
extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_test_app_bin_start");
|
|
|
|
extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_test_app_bin_end");
|
|
|
|
static bool firmware_loaded = false;
|
|
|
|
|
|
|
|
static void load_and_start_ulp_firmware(void)
|
|
|
|
{
|
|
|
|
if (!firmware_loaded) {
|
2020-11-10 02:40:01 -05:00
|
|
|
TEST_ASSERT(ulp_riscv_load_binary(ulp_main_bin_start,
|
2020-04-17 15:34:56 -04:00
|
|
|
(ulp_main_bin_end - ulp_main_bin_start)) == ESP_OK);
|
|
|
|
|
2021-12-23 01:12:47 -05:00
|
|
|
TEST_ASSERT(ulp_set_wakeup_period(0, ULP_WAKEUP_PERIOD) == ESP_OK);
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(ulp_riscv_run() == ESP_OK);
|
|
|
|
|
|
|
|
firmware_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 21:57:31 -05:00
|
|
|
TEST_CASE("ULP-RISC-V and main CPU are able to exchange data", "[ulp]")
|
2020-11-10 02:40:01 -05:00
|
|
|
{
|
2020-04-17 15:34:56 -04:00
|
|
|
const uint32_t test_data = 0x12345678;
|
2021-12-23 01:12:47 -05:00
|
|
|
struct timeval start, end;
|
2020-04-17 15:34:56 -04:00
|
|
|
|
2021-12-23 01:12:47 -05:00
|
|
|
/* Load ULP RISC-V firmware and start the ULP RISC-V Coprocessor */
|
2020-04-17 15:34:56 -04:00
|
|
|
load_and_start_ulp_firmware();
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Setup wakeup triggers */
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
|
|
|
|
|
2021-12-23 01:12:47 -05:00
|
|
|
/* Setup test data */
|
2020-04-17 15:34:56 -04:00
|
|
|
ulp_riscv_test_data_in = test_data ^ XOR_MASK;
|
|
|
|
ulp_main_cpu_command = RISCV_READ_WRITE_TEST;
|
|
|
|
|
2021-12-23 01:12:47 -05:00
|
|
|
/* Enter Light Sleep */
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(esp_light_sleep_start() == ESP_OK);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Wait for wakeup from ULP RISC-V Coprocessor */
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_ULP);
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2021-12-23 01:12:47 -05:00
|
|
|
/* Wait till we receive the correct command response */
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
while (ulp_command_resp != RISCV_READ_WRITE_TEST)
|
|
|
|
;
|
|
|
|
gettimeofday(&end, NULL);
|
2022-02-22 00:42:56 -05:00
|
|
|
printf("Response time %jd ms\n", ((intmax_t)end.tv_sec - (intmax_t)start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec) / 1000);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Verify test data */
|
|
|
|
TEST_ASSERT(ulp_command_resp == RISCV_READ_WRITE_TEST);
|
|
|
|
TEST_ASSERT(ulp_main_cpu_reply == RISCV_COMMAND_OK);
|
2020-04-17 15:34:56 -04:00
|
|
|
printf("data out: 0x%X, expected: 0x%X \n", ulp_riscv_test_data_out, test_data);
|
|
|
|
TEST_ASSERT(test_data == ulp_riscv_test_data_out);
|
|
|
|
|
2021-12-23 01:12:47 -05:00
|
|
|
/* Clear test data */
|
|
|
|
ulp_main_cpu_command = RISCV_NO_COMMAND;
|
2020-04-17 15:34:56 -04:00
|
|
|
}
|
|
|
|
|
2022-01-18 21:57:31 -05:00
|
|
|
TEST_CASE("ULP-RISC-V is able to wakeup main CPU from light sleep", "[ulp]")
|
2020-04-17 15:34:56 -04:00
|
|
|
{
|
2021-12-23 01:12:47 -05:00
|
|
|
struct timeval start, end;
|
|
|
|
|
|
|
|
/* Load ULP RISC-V firmware and start the ULP RISC-V Coprocessor */
|
2020-04-17 15:34:56 -04:00
|
|
|
load_and_start_ulp_firmware();
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Setup wakeup triggers */
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Setup test data */
|
2020-04-17 15:34:56 -04:00
|
|
|
ulp_main_cpu_command = RISCV_LIGHT_SLEEP_WAKEUP_TEST;
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Enter Light Sleep */
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(esp_light_sleep_start() == ESP_OK);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Wait for wakeup from ULP RISC-V Coprocessor */
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_ULP);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Wait till we receive the correct command response */
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
while (ulp_command_resp != RISCV_LIGHT_SLEEP_WAKEUP_TEST)
|
|
|
|
;
|
|
|
|
gettimeofday(&end, NULL);
|
2022-02-22 00:42:56 -05:00
|
|
|
printf("Response time %jd ms\n", ((intmax_t)end.tv_sec - (intmax_t)start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec) / 1000);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Verify test data */
|
|
|
|
TEST_ASSERT(ulp_command_resp == RISCV_LIGHT_SLEEP_WAKEUP_TEST);
|
2020-04-17 15:34:56 -04:00
|
|
|
TEST_ASSERT(ulp_main_cpu_reply == RISCV_COMMAND_OK);
|
2021-12-23 01:12:47 -05:00
|
|
|
|
|
|
|
/* Clear test data */
|
|
|
|
ulp_main_cpu_command = RISCV_NO_COMMAND;
|
2020-04-17 15:34:56 -04:00
|
|
|
}
|
|
|
|
|
2022-01-18 21:57:31 -05:00
|
|
|
static bool ulp_riscv_is_running(void)
|
|
|
|
{
|
|
|
|
uint32_t start_cnt = ulp_riscv_counter;
|
|
|
|
|
|
|
|
/* Wait a few ULP wakeup cycles to ensure ULP has run */
|
|
|
|
vTaskDelay((5 * ULP_WAKEUP_PERIOD / 1000) / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
uint32_t end_cnt = ulp_riscv_counter;
|
|
|
|
printf("start run count: %d, end run count %d\n", start_cnt, end_cnt);
|
|
|
|
|
|
|
|
/* If the ulp is running the counter should have been incremented */
|
|
|
|
return (start_cnt != end_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("ULP-RISC-V can be stopped and resumed from main CPU", "[ulp]")
|
|
|
|
{
|
|
|
|
/* Load ULP RISC-V firmware and start the ULP RISC-V Coprocessor */
|
|
|
|
load_and_start_ulp_firmware();
|
|
|
|
|
|
|
|
TEST_ASSERT(ulp_riscv_is_running());
|
|
|
|
|
|
|
|
printf("Stopping the ULP\n");
|
|
|
|
ulp_riscv_timer_stop();
|
|
|
|
ulp_riscv_halt();
|
|
|
|
|
|
|
|
TEST_ASSERT(!ulp_riscv_is_running());
|
|
|
|
|
|
|
|
printf("Resuming the ULP\n");
|
|
|
|
ulp_riscv_timer_resume();
|
|
|
|
|
|
|
|
TEST_ASSERT(ulp_riscv_is_running());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("ULP-RISC-V can stop itself and be resumed from the main CPU", "[ulp]")
|
|
|
|
{
|
|
|
|
volatile riscv_test_commands_t *command_resp = &ulp_command_resp;
|
|
|
|
|
|
|
|
/* Load ULP RISC-V firmware and start the ULP RISC-V Coprocessor */
|
|
|
|
load_and_start_ulp_firmware();
|
|
|
|
|
|
|
|
TEST_ASSERT(ulp_riscv_is_running());
|
|
|
|
|
|
|
|
printf("Stopping the ULP\n");
|
|
|
|
/* Setup test data */
|
|
|
|
ulp_main_cpu_command = RISCV_STOP_TEST;
|
|
|
|
|
|
|
|
while (*command_resp != RISCV_STOP_TEST) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a bit to ensure ULP finished shutting down */
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
TEST_ASSERT(!ulp_riscv_is_running());
|
|
|
|
|
|
|
|
printf("Resuming the ULP\n");
|
|
|
|
ulp_main_cpu_command = RISCV_NO_COMMAND;
|
|
|
|
ulp_riscv_timer_resume();
|
|
|
|
|
|
|
|
TEST_ASSERT(ulp_riscv_is_running());
|
|
|
|
}
|
2022-01-21 04:13:48 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep this test case as the last test case in this suite as a CPU reset occurs.
|
|
|
|
* Add new test cases above in order to ensure they run when all test cases are run together.
|
|
|
|
*/
|
|
|
|
TEST_CASE("ULP-RISC-V is able to wakeup main CPU from deep sleep", "[ulp][reset=SW_CPU_RESET][ignore]")
|
|
|
|
{
|
|
|
|
/* Load ULP RISC-V firmware and start the ULP RISC-V Coprocessor */
|
|
|
|
load_and_start_ulp_firmware();
|
|
|
|
|
|
|
|
/* Setup wakeup triggers */
|
|
|
|
TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
|
|
|
|
|
|
|
|
/* Setup test data */
|
|
|
|
ulp_main_cpu_command = RISCV_DEEP_SLEEP_WAKEUP_TEST;
|
|
|
|
|
|
|
|
/* Enter Deep Sleep */
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
UNITY_TEST_FAIL(__LINE__, "Should not get here!");
|
|
|
|
}
|