imc_extension.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdio.h>
  2. #include "ImC/imc_kernel.h"
  3. #include "ImC/imc_extension.h"
  4. #include "ImC/imc_api.h"
  5. extern ADC_HandleTypeDef ADC_HANDLER_SBC;
  6. float loop_energy_estimation[imcMAX_LOOP_IDS] IMC_GLOBAL;
  7. int imc_is_passing_loops IMC_GLOBAL;
  8. int imc_energy_estimation_initialized IMC_GLOBAL;
  9. void imc_init_energy_estimation() {
  10. if(!imc_energy_estimation_initialized) {
  11. for(int i=0; i<imcMAX_LOOP_IDS; i++) {
  12. loop_energy_estimation[i] = 0.1;
  13. }
  14. imc_energy_estimation_initialized = 1;
  15. }
  16. if(imc_is_passing_loops != -1) {
  17. float current_estimation = loop_energy_estimation[imc_is_passing_loops];
  18. current_estimation *= 2;
  19. if(current_estimation > 1) current_estimation = 1;
  20. loop_energy_estimation[imc_is_passing_loops] = current_estimation;
  21. // printf("estimation for loop%d is updated to %f\r\n", imc_is_passing_loops, current_estimation);
  22. }
  23. imc_is_passing_loops = -1;
  24. }
  25. void __imc_finish_loop_skipping() {
  26. if(imc_is_passing_loops >= 0) {
  27. int volt = measure_voltage(ADC_HANDLER_SBC, EPS_CAP_ID_SBC);
  28. float energy_available = (float)(volt * volt - imcCAP_VOL_LOW * imcCAP_VOL_LOW) / imcENERGY_TOTAL;
  29. if(energy_available > 0.2) {
  30. float current_estimation = loop_energy_estimation[imc_is_passing_loops];
  31. current_estimation /= 2;
  32. loop_energy_estimation[imc_is_passing_loops] = current_estimation;
  33. // printf("estimation for loop%d is updated to %f\r\n", imc_is_passing_loops, current_estimation);
  34. }
  35. }
  36. imc_is_passing_loops = -1;
  37. }
  38. int __imc_get_loop_pass_count(int loop_id) {
  39. // printf("__imc_get_loop_pass_count() called\r\n");
  40. #if(imcENABLE_STATIC_LOOP_PASS_COUNT)
  41. return imcLOOP_PASS_COUNT;
  42. #elif(imcENABLE_ADAPTIVE_LOOP_PASS_COUNT)
  43. int volt = measure_voltage(ADC_HANDLER_SBC, EPS_CAP_ID_SBC);
  44. float energy_available = (float)(volt*volt - imcCAP_VOL_LOW*imcCAP_VOL_LOW) / imcENERGY_TOTAL;
  45. imc_is_passing_loops = loop_id;
  46. // printf("energy available: %f\r\n", energy_available);
  47. if(energy_available > 1) return (int)(energy_available / loop_energy_estimation[loop_id]);
  48. else if(energy_available < 0) return 0;
  49. else return (int)(energy_available / loop_energy_estimation[loop_id]);
  50. #else
  51. return 0;
  52. #endif
  53. }