luat_lib_mcu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. @module mcu
  3. @summary 封装mcu一些特殊操作
  4. @version core V0007
  5. @date 2021.08.18
  6. */
  7. #include "luat_base.h"
  8. #include "luat_mcu.h"
  9. #define LUAT_LOG_TAG "mcu"
  10. #include "luat_log.h"
  11. /*
  12. 设置主频,单位MHZ. 请注意,主频与外设主频有关联性, 例如主频2M时SPI的最高只能1M
  13. @api mcu.setClk(mhz)
  14. @int 主频,根据设备的不同有不同的有效值,请查阅手册
  15. @return bool 成功返回true,否则返回false
  16. @usage
  17. -- 设置到80MHZ
  18. mcu.setClk(80)
  19. sys.wait(1000)
  20. -- 设置到240MHZ
  21. mcu.setClk(240)
  22. sys.wait(1000)
  23. -- 设置到2MHZ
  24. mcu.setClk(2)
  25. sys.wait(1000)
  26. */
  27. static int l_mcu_set_clk(lua_State* L) {
  28. int ret = luat_mcu_set_clk((size_t)luaL_checkinteger(L, 1));
  29. lua_pushboolean(L, ret == 0 ? 1 : 0);
  30. return 1;
  31. }
  32. /*
  33. 获取主频,单位MHZ.
  34. @api mcu.getClk()
  35. @return int 若失败返回-1,否则返回主频数值,若等于0,可能处于32k晶振的省电模式
  36. @usage
  37. local mhz = mcu.getClk()
  38. print("Boom", mhz)
  39. */
  40. static int l_mcu_get_clk(lua_State* L) {
  41. int mhz = luat_mcu_get_clk();
  42. lua_pushinteger(L, mhz);
  43. return 1;
  44. }
  45. /*
  46. 获取设备唯一id. 注意,可能包含不可见字符,如需查看建议toHex()后打印
  47. @api mcu.unique_id()
  48. @return string 设备唯一id.若不支持, 会返回空字符串.
  49. @usage
  50. local unique_id = mcu.unique_id()
  51. print("unique_id", unique_id)
  52. */
  53. static int l_mcu_unique_id(lua_State* L) {
  54. size_t len = 0;
  55. const char* id = luat_mcu_unique_id(&len);
  56. lua_pushlstring(L, id, len);
  57. return 1;
  58. }
  59. /*
  60. 获取启动后的tick数,注意会出现溢出会出现负数
  61. @api mcu.ticks()
  62. @return int 当前tick值
  63. @usage
  64. local tick = mcu.ticks()
  65. print("ticks", tick)
  66. */
  67. static int l_mcu_ticks(lua_State* L) {
  68. long tick = luat_mcu_ticks();
  69. lua_pushinteger(L, tick);
  70. return 1;
  71. }
  72. /*
  73. 获取每秒的tick数量
  74. @api mcu.hz()
  75. @return int 每秒的tick数量
  76. @usage
  77. local tick = mcu.hz()
  78. print("mcu.hz", hz)
  79. */
  80. static int l_mcu_hz(lua_State* L) {
  81. uint32_t hz = luat_mcu_hz();
  82. lua_pushinteger(L, hz);
  83. return 1;
  84. }
  85. /*
  86. 读写mcu的32bit寄存器或者ram,谨慎使用写功能,请熟悉mcu的寄存器使用方法后再使用
  87. @api mcu.reg(address, value, mask)
  88. @int 寄存器或者ram地址
  89. @int 写入的值,如果没有,则直接返回当前值
  90. @int 位掩码,可以对特定几个位置的bit做修改, 默认0xffffffff,修改全部32bit
  91. @return int 返回当前寄存的值
  92. @usage
  93. local value = mcu.reg(0x2009FFFC, 0x01, 0x01) --对0x2009FFFC地址上的值,修改bit0为1
  94. */
  95. static int l_mcu_reg32(lua_State* L) {
  96. volatile uint32_t *address = (uint32_t *)(luaL_checkinteger(L, 1) & 0xfffffffc);
  97. if (lua_isinteger(L, 2)) {
  98. volatile uint32_t value = lua_tointeger(L, 2);
  99. volatile uint32_t mask = luaL_optinteger(L, 3, 0xffffffff);
  100. volatile uint32_t org = *address;
  101. *address = (org & ~mask)| (value & mask);
  102. lua_pushinteger(L, *address);
  103. } else {
  104. lua_pushinteger(L, *address);
  105. }
  106. return 1;
  107. }
  108. #ifdef __LUATOS_TICK_64BIT__
  109. /*
  110. 获取启动后的高精度tick,目前只有105能用
  111. @api mcu.tick64()
  112. @return string 当前tick值,8个字节的uint64
  113. @return int 1us有几个tick,0表示未知
  114. @usage
  115. local tick_str, tick_per = mcu.tick64()
  116. print("ticks", tick_str, tick_per)
  117. */
  118. static int l_mcu_hw_tick64(lua_State* L) {
  119. uint64_t tick = luat_mcu_tick64();
  120. uint32_t us_period = luat_mcu_us_period();
  121. lua_pushlstring(L, &tick, 8);
  122. lua_pushinteger(L, us_period);
  123. return 2;
  124. }
  125. /*
  126. 计算2个64bit tick的差值,目前只有105能用
  127. @api mcu.dtick(tick1, tick2, check_value)
  128. @string tick1, 64bit的string
  129. @string tick2, 64bit的string
  130. @int 参考值,可选项,如果为0,则返回结果中第一个项目为true
  131. @return
  132. boolean 与参考值比较,如果大于等于为true,反之为false
  133. int 差值tick1 - tick2,如果超过了0x7fffffff,结果可能是错的
  134. @usage
  135. local result, diff_tick = mcu.dtick64(tick1, tick2)
  136. print("ticks", result, diff_tick)
  137. */
  138. static int l_mcu_hw_diff_tick64(lua_State* L) {
  139. uint64_t tick1, tick2;
  140. int64_t diff;
  141. int check_value = 0;
  142. size_t len1;
  143. const char *data1 = luaL_checklstring(L, 1, &len1);
  144. size_t len2;
  145. const char *data2 = luaL_checklstring(L, 2, &len2);
  146. check_value = luaL_optinteger(L, 3, 0);
  147. memcpy(&tick1, data1, len1);
  148. memcpy(&tick2, data2, len2);
  149. diff = tick1 - tick2;
  150. lua_pushboolean(L, (diff >= (int64_t)check_value)?1:0);
  151. lua_pushinteger(L, diff);
  152. return 2;
  153. }
  154. /*
  155. 选择时钟源
  156. @api mcu.setXTAL(source_main, source_32k),目前只有105能用
  157. @boolean 高速时钟是否使用外部时钟源,如果为空则不改变
  158. @boolean 低速32K是否使用外部时钟源,如果为空则不改变
  159. @int PLL稳定时间,在切换高速时钟的时候,根据硬件环境,需要delay一段时间等待PLL稳定,默认是1200,建议不小于1024
  160. @return 无
  161. @usage
  162. mcu.setXTAL(true, true, 1248) --高速时钟使用外部时钟,低速32K使用外部晶振, delay1248
  163. */
  164. static int l_mcu_set_xtal(lua_State* L) {
  165. int source_main = 255;
  166. int source_32k = 255;
  167. int delay = luaL_optinteger(L, 3, 1200);
  168. if (lua_isboolean(L, 1)) {
  169. source_main = lua_toboolean(L, 1);
  170. }
  171. if (lua_isboolean(L, 2)) {
  172. source_32k = lua_toboolean(L, 2);
  173. }
  174. luat_mcu_set_clk_source(source_main, source_32k, delay);
  175. return 0;
  176. }
  177. #endif
  178. #include "rotable2.h"
  179. static const rotable_Reg_t reg_mcu[] =
  180. {
  181. { "setClk" , ROREG_FUNC(l_mcu_set_clk)},
  182. { "getClk", ROREG_FUNC(l_mcu_get_clk)},
  183. { "unique_id", ROREG_FUNC(l_mcu_unique_id)},
  184. { "ticks", ROREG_FUNC(l_mcu_ticks)},
  185. { "hz", ROREG_FUNC(l_mcu_hz)},
  186. { "reg32", ROREG_FUNC(l_mcu_reg32)},
  187. #ifdef __LUATOS_TICK_64BIT__
  188. { "tick64", ROREG_FUNC(l_mcu_hw_tick64)},
  189. { "dtick64", ROREG_FUNC(l_mcu_hw_diff_tick64)},
  190. { "setXTAL", ROREG_FUNC(l_mcu_set_xtal)},
  191. #endif
  192. { NULL, ROREG_INT(0) }
  193. };
  194. LUAMOD_API int luaopen_mcu( lua_State *L ) {
  195. luat_newlib2(L, reg_mcu);
  196. return 1;
  197. }