test_ulp.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <freertos/FreeRTOS.h>
  17. #include <freertos/task.h>
  18. #include <freertos/semphr.h>
  19. #include <unity.h>
  20. #include "esp_attr.h"
  21. #include "esp_err.h"
  22. #include "esp_log.h"
  23. #include "esp32/ulp.h"
  24. #include "soc/soc.h"
  25. #include "soc/rtc_cntl_reg.h"
  26. #include "soc/saradc_reg.h"
  27. #include "sdkconfig.h"
  28. static void hexdump(const uint32_t* src, size_t count) {
  29. for (size_t i = 0; i < count; ++i) {
  30. printf("%08x ", *src);
  31. ++src;
  32. if ((i + 1) % 4 == 0) {
  33. printf("\n");
  34. }
  35. }
  36. }
  37. TEST_CASE("ulp add test", "[ulp]")
  38. {
  39. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  40. const ulp_insn_t program[] = {
  41. I_MOVI(R3, 16),
  42. I_LD(R0, R3, 0),
  43. I_LD(R1, R3, 1),
  44. I_ADDR(R2, R0, R1),
  45. I_ST(R2, R3, 2),
  46. I_HALT()
  47. };
  48. RTC_SLOW_MEM[16] = 10;
  49. RTC_SLOW_MEM[17] = 11;
  50. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  51. TEST_ASSERT_EQUAL(ESP_OK, ulp_process_macros_and_load(0, program, &size));
  52. TEST_ASSERT_EQUAL(ESP_OK, ulp_run(0));
  53. ets_delay_us(1000);
  54. hexdump(RTC_SLOW_MEM, CONFIG_ULP_COPROC_RESERVE_MEM / 4);
  55. TEST_ASSERT_EQUAL(10 + 11, RTC_SLOW_MEM[18] & 0xffff);
  56. }
  57. TEST_CASE("ulp branch test", "[ulp]")
  58. {
  59. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  60. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  61. const ulp_insn_t program[] = {
  62. I_MOVI(R0, 34), // r0 = dst
  63. M_LABEL(1),
  64. I_MOVI(R1, 32),
  65. I_LD(R1, R1, 0), // r1 = mem[33]
  66. I_MOVI(R2, 33),
  67. I_LD(R2, R2, 0), // r2 = mem[34]
  68. I_SUBR(R3, R1, R2), // r3 = r1 - r2
  69. I_ST(R3, R0, 0), // dst[0] = r3
  70. I_ADDI(R0, R0, 1),
  71. M_BL(1, 64),
  72. I_HALT(),
  73. };
  74. RTC_SLOW_MEM[32] = 42;
  75. RTC_SLOW_MEM[33] = 18;
  76. hexdump(RTC_SLOW_MEM, CONFIG_ULP_COPROC_RESERVE_MEM / 4);
  77. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  78. ulp_process_macros_and_load(0, program, &size);
  79. ulp_run(0);
  80. printf("\n\n");
  81. hexdump(RTC_SLOW_MEM, CONFIG_ULP_COPROC_RESERVE_MEM / 4);
  82. for (int i = 34; i < 64; ++i) {
  83. TEST_ASSERT_EQUAL(42 - 18, RTC_SLOW_MEM[i] & 0xffff);
  84. }
  85. TEST_ASSERT_EQUAL(0, RTC_SLOW_MEM[64]);
  86. }