test_float_in_isr.c 1.5 KB

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