test_float_in_isr.c 1.6 KB

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