luat_hwtimer_air101.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "luat_base.h"
  2. #include "luat_hwtimer.h"
  3. #include "luat_msgbus.h"
  4. #include "wm_timer.h"
  5. static int l_hwtimer_handler(lua_State *L, void* ptr) {
  6. lua_getglobal(L, "sys_pub");
  7. if (lua_isfunction(L, -1)) {
  8. lua_pushstring(L, "HWTIMER_IRQ");
  9. lua_call(L, 1, 0);
  10. }
  11. return 0;
  12. }
  13. static void luat_hwtimer_cb(void *arg) {
  14. rtos_msg_t msg;
  15. msg.handler = l_hwtimer_handler;
  16. luat_msgbus_put(&msg, 0);
  17. }
  18. int luat_hwtimer_create(luat_hwtimer_conf_t *conf) {
  19. struct tls_timer_cfg cfg = {0};
  20. cfg.unit = conf->unit == 0 ? TLS_TIMER_UNIT_US : TLS_TIMER_UNIT_MS;
  21. cfg.timeout = conf->timeout;
  22. cfg.is_repeat = conf->is_repeat;
  23. cfg.callback = luat_hwtimer_cb;
  24. cfg.arg = NULL;
  25. u8 id = tls_timer_create(&cfg);
  26. if (id <= 5)
  27. return id;
  28. return -1;
  29. }
  30. int luat_hwtimer_start(int id) {
  31. if (id < 0 || id > 5) return -1;
  32. tls_timer_start(id);
  33. return 0;
  34. }
  35. int luat_hwtimer_stop(int id) {
  36. if (id < 0 || id > 5) return -1;
  37. tls_timer_stop(id);
  38. return 0;
  39. }
  40. int luat_hwtimer_read(int id) {
  41. if (id < 0 || id > 5) return -1;
  42. return tls_timer_read(id);
  43. }
  44. int luat_hwtimer_change(int id, uint32_t newtimeout) {
  45. if (id < 0 || id > 5) return -1;
  46. tls_timer_change(id, newtimeout);
  47. return 0;
  48. }
  49. int luat_hwtimer_destroy(int id) {
  50. if (id < 0 || id > 5) return -1;
  51. tls_timer_destroy(id);
  52. return 0;
  53. }