luat_mcu_idf5.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "luat_base.h"
  2. #include "luat_mcu.h"
  3. #include "esp_system.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_flash.h"
  7. #include "driver/gptimer.h"
  8. #include "spi_flash/spi_flash_defs.h"
  9. #include "esp_pm.h"
  10. enum TYPE_FLASH_ID{
  11. SPIFLASH_MID_GD = 0xC8,
  12. SPIFLASH_MID_ESMT = 0x1C,
  13. SPIFLASH_MID_PUYA = 0x85,
  14. SPIFLASH_MID_WINBOND = 0xEF,
  15. SPIFLASH_MID_FUDANMICRO = 0xA1,
  16. SPIFLASH_MID_BOYA = 0x68,
  17. SPIFLASH_MID_XMC = 0x20,
  18. SPIFLASH_MID_XTX = 0x0B,
  19. SPIFLASH_MID_TSINGTENG = 0xEB, /*UNIGROUP TSINGTENG*/
  20. SPIFLASH_MID_TSINGTENG_1MB_4MB = 0xCD, /*UNIGROUP TSINGTENG*/
  21. };
  22. #define LUAT_LOG_TAG "mcu"
  23. #include "luat_log.h"
  24. int luat_mcu_set_clk(size_t mhz) {
  25. esp_pm_config_t config = {0};
  26. esp_pm_get_configuration(&config);
  27. if (config.max_freq_mhz == mhz) {
  28. return 0;
  29. }
  30. config.max_freq_mhz = mhz;
  31. if (config.min_freq_mhz > config.max_freq_mhz) {
  32. config.min_freq_mhz = config.max_freq_mhz;
  33. }
  34. int ret = esp_pm_configure(&config);
  35. if (ret) {
  36. LLOGD("set clk %d ret %d", mhz, ret);
  37. }
  38. return ret;
  39. }
  40. int luat_mcu_get_clk(void) {
  41. esp_pm_config_t config = {0};
  42. esp_pm_get_configuration(&config);
  43. return config.max_freq_mhz;
  44. }
  45. extern esp_flash_t *esp_flash_default_chip;
  46. static uint8_t uid[18];
  47. int uni_bytes = 8;
  48. const char* luat_mcu_unique_id(size_t* t) {
  49. esp_flash_t *chip = esp_flash_default_chip;
  50. if (uid[0] == 0) {
  51. // 这里使用新的UNIQUE_ID实现, 原因是部分FLASH的ID是16字节的
  52. // 而esp_flash_read_unique_chip_id只能读取前8字节,就可能出现重复的情况
  53. // 添加这个宏是考虑到有客户可能仍旧需要使用老的unique_id
  54. // 新的unique_id均添加2个字节, 标识芯片id和容量, 与air101系列保持一致
  55. #ifndef LUAT_MCU_UNIQUE_ID_OLDSTYLE
  56. uint32_t tmpvalue = 0;
  57. esp_flash_read_id(chip, &tmpvalue);
  58. int rid = (tmpvalue >> 16) & 0xFF;
  59. // LLOGD("chip id %04X %02X", tmpvalue, rid);
  60. uid[0] = (uint8_t)rid;
  61. esp_flash_get_physical_size(chip, &tmpvalue);
  62. // LLOGD("physical_size %04X", tmpvalue);
  63. uid[1] = (uint8_t)(tmpvalue >> 20);
  64. int dumy_bytes = 4;
  65. switch(rid)
  66. {
  67. case SPIFLASH_MID_GD:
  68. case SPIFLASH_MID_PUYA:
  69. case SPIFLASH_MID_TSINGTENG:
  70. case SPIFLASH_MID_TSINGTENG_1MB_4MB:
  71. dumy_bytes = 4;
  72. uni_bytes = 16;
  73. break;
  74. case SPIFLASH_MID_WINBOND:
  75. case SPIFLASH_MID_FUDANMICRO:
  76. case SPIFLASH_MID_BOYA:
  77. case SPIFLASH_MID_XMC:
  78. dumy_bytes = 4;
  79. uni_bytes = 8;
  80. break;
  81. case SPIFLASH_MID_ESMT:
  82. case SPIFLASH_MID_XTX:
  83. default:
  84. break;
  85. }
  86. // LLOGD("query RUID %d %d", uni_bytes, dumy_bytes);
  87. spi_flash_trans_t transfer = {
  88. .command = CMD_RDUID,
  89. .miso_len = uni_bytes,
  90. .miso_data = ((uint8_t *)uid + 2),
  91. .dummy_bitlen = dumy_bytes * 8, //RDUID command followed by 4 bytes (32 bits) of dummy clocks.
  92. };
  93. esp_err_t err = chip->host->driver->common_command(chip->host, &transfer);
  94. // LLOGD("ret %d", err);
  95. if (err == 0) {
  96. size_t max = uni_bytes;
  97. for (size_t i = 0; i < max; i++)
  98. {
  99. if (uid[max - i + 2] == 0xFF)
  100. uni_bytes -= 1;
  101. else
  102. break;
  103. }
  104. if (uni_bytes == 0) {
  105. LLOGE("flash chip NO support unique id");
  106. }
  107. }
  108. #else
  109. uint64_t chip_uid = 0;
  110. uint8_t uuid2[8];
  111. esp_flash_read_unique_chip_id(chip, &chip_uid);
  112. memcpy(uuid2, &chip_uid, 8);
  113. // LLOGD("ORG %02X%02X%02X%02X%02X%02X%02X%02X", uuid2[0], uuid2[1], uuid2[2], uuid2[3],
  114. // uuid2[4], uuid2[5], uuid2[6], uuid2[7]);
  115. memcpy(uid, uuid2, 8);
  116. uni_bytes = 6;
  117. #endif
  118. }
  119. *t = uni_bytes + 2;
  120. return (const char*)uid;
  121. }
  122. long luat_mcu_ticks(void) {
  123. return xTaskGetTickCount();
  124. }
  125. uint32_t luat_mcu_hz(void) {
  126. return configTICK_RATE_HZ;
  127. }
  128. static gptimer_handle_t us_timer;
  129. uint64_t luat_mcu_tick64(void) {
  130. uint64_t ret = 0;
  131. gptimer_get_raw_count(us_timer, &ret);
  132. // LLOGD("tick64 %lld", ret);
  133. return ret;
  134. }
  135. int luat_mcu_us_period(void) {
  136. return 1;
  137. }
  138. uint64_t luat_mcu_tick64_ms(void) {
  139. return luat_mcu_tick64() / 1000;
  140. }
  141. void luat_mcu_set_clk_source(uint8_t source_main, uint8_t source_32k, uint32_t delay) {
  142. // nop
  143. }
  144. #define TIMER_DIVIDER (16) // Hardware timer clock divider
  145. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) // convert counter value to seconds
  146. void luat_mcu_us_timer_init() {
  147. /* Select and initialize basic parameters of the timer */
  148. const gptimer_config_t config = {
  149. #if defined(CONFIG_IDF_TARGET_ESP32)
  150. .clk_src = GPTIMER_CLK_SRC_APB,
  151. #else
  152. .clk_src = GPTIMER_CLK_SRC_XTAL,
  153. #endif
  154. .direction = GPTIMER_COUNT_UP,
  155. .resolution_hz = 1000000, // us
  156. }; // default clock source is APB
  157. int ret = gptimer_new_timer(&config, &us_timer);
  158. // LLOGD("gptimer_new_timer %d", ret);
  159. if (ret == 0) {
  160. if (0 == gptimer_enable(us_timer))
  161. gptimer_start(us_timer);
  162. }
  163. }
  164. void luat_mcu_set_hardfault_mode(int mode) {;}
  165. void luat_mcu_xtal_ref_output(uint8_t main_enable, uint8_t slow_32k_enable) {;}
  166. int luat_uart_pre_setup(int uart_id, uint8_t use_alt_type){return -1;}
  167. int luat_i2c_set_iomux(int id, uint8_t value){return -1;}