1 #include <esp_types.h>
2 #include <stdio.h>
3
4 #include "freertos/FreeRTOS.h"
5 #include "freertos/task.h"
6 #include "freertos/semphr.h"
7 #include "freertos/queue.h"
8 #include "esp_intr_alloc.h"
9 #include "unity.h"
10 #include "soc/cpu.h"
11 #include "test_utils.h"
12 #include "math.h"
13
14 #define SW_ISR_LEVEL_1 7
15 #ifdef CONFIG_FREERTOS_FPU_IN_ISR
16
17 struct fp_test_context {
18 SemaphoreHandle_t sync;
19 float expected;
20 };
21
software_isr(void * arg)22 static void software_isr(void *arg) {
23 (void)arg;
24 BaseType_t yield;
25 xt_set_intclear(1 << SW_ISR_LEVEL_1);
26
27 struct fp_test_context *ctx = (struct fp_test_context *)arg;
28
29 for(int i = 0; i < 16; i++) {
30 ctx->expected = ctx->expected * 2.0f * cosf(0.0f);
31 }
32
33 xSemaphoreGiveFromISR(ctx->sync, &yield);
34 if(yield) {
35 portYIELD_FROM_ISR();
36 }
37 }
38
39
40 TEST_CASE("Floating point usage in ISR test", "[freertos]" "[fp]")
41 {
42 struct fp_test_context ctx;
43 float fp_math_operation_result = 0.0f;
44
45 intr_handle_t handle;
46 esp_err_t err = esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1, &software_isr, &ctx, &handle);
47 TEST_ASSERT_EQUAL_HEX32(ESP_OK, err);
48
49 ctx.sync = xSemaphoreCreateBinary();
50 TEST_ASSERT(ctx.sync != NULL);
51 ctx.expected = 1.0f;
52
53 fp_math_operation_result = cosf(0.0f);
54
55 xt_set_intset(1 << SW_ISR_LEVEL_1);
56 xSemaphoreTake(ctx.sync, portMAX_DELAY);
57
58 esp_intr_free(handle);
59 vSemaphoreDelete(ctx.sync);
60
61 printf("FP math isr result: %f \n", ctx.expected);
62 TEST_ASSERT_FLOAT_WITHIN(0.1f, ctx.expected, fp_math_operation_result * 65536.0f);
63 }
64
65 #endif
66