modesp.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2015 Paul Sokolovsky
  9. * Copyright (c) 2016 Damien P. George
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. #include <stdio.h>
  30. #include "rom/gpio.h"
  31. #include "esp_log.h"
  32. #include "esp_spi_flash.h"
  33. #include "py/runtime.h"
  34. #include "py/mperrno.h"
  35. #include "py/mphal.h"
  36. #include "drivers/dht/dht.h"
  37. #include "modesp.h"
  38. STATIC mp_obj_t esp_osdebug(size_t n_args, const mp_obj_t *args) {
  39. esp_log_level_t level = LOG_LOCAL_LEVEL;
  40. if (n_args == 2) {
  41. level = mp_obj_get_int(args[1]);
  42. }
  43. if (args[0] == mp_const_none) {
  44. // Disable logging
  45. esp_log_level_set("*", ESP_LOG_ERROR);
  46. } else {
  47. // Enable logging at the given level
  48. // TODO args[0] should set the UART to which debug is sent
  49. esp_log_level_set("*", level);
  50. }
  51. return mp_const_none;
  52. }
  53. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_osdebug_obj, 1, 2, esp_osdebug);
  54. STATIC mp_obj_t esp_flash_read(mp_obj_t offset_in, mp_obj_t buf_in) {
  55. mp_int_t offset = mp_obj_get_int(offset_in);
  56. mp_buffer_info_t bufinfo;
  57. mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
  58. esp_err_t res = spi_flash_read(offset, bufinfo.buf, bufinfo.len);
  59. if (res != ESP_OK) {
  60. mp_raise_OSError(MP_EIO);
  61. }
  62. return mp_const_none;
  63. }
  64. STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_flash_read_obj, esp_flash_read);
  65. STATIC mp_obj_t esp_flash_write(mp_obj_t offset_in, mp_obj_t buf_in) {
  66. mp_int_t offset = mp_obj_get_int(offset_in);
  67. mp_buffer_info_t bufinfo;
  68. mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
  69. esp_err_t res = spi_flash_write(offset, bufinfo.buf, bufinfo.len);
  70. if (res != ESP_OK) {
  71. mp_raise_OSError(MP_EIO);
  72. }
  73. return mp_const_none;
  74. }
  75. STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_flash_write_obj, esp_flash_write);
  76. STATIC mp_obj_t esp_flash_erase(mp_obj_t sector_in) {
  77. mp_int_t sector = mp_obj_get_int(sector_in);
  78. esp_err_t res = spi_flash_erase_sector(sector);
  79. if (res != ESP_OK) {
  80. mp_raise_OSError(MP_EIO);
  81. }
  82. return mp_const_none;
  83. }
  84. STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_flash_erase_obj, esp_flash_erase);
  85. STATIC mp_obj_t esp_flash_size(void) {
  86. return mp_obj_new_int_from_uint(spi_flash_get_chip_size());
  87. }
  88. STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size);
  89. STATIC mp_obj_t esp_flash_user_start(void) {
  90. return MP_OBJ_NEW_SMALL_INT(0x200000);
  91. }
  92. STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_user_start_obj, esp_flash_user_start);
  93. STATIC mp_obj_t esp_gpio_matrix_in(mp_obj_t pin, mp_obj_t sig, mp_obj_t inv) {
  94. gpio_matrix_in(mp_obj_get_int(pin), mp_obj_get_int(sig), mp_obj_get_int(inv));
  95. return mp_const_none;
  96. }
  97. STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_gpio_matrix_in_obj, esp_gpio_matrix_in);
  98. STATIC mp_obj_t esp_gpio_matrix_out(size_t n_args, const mp_obj_t *args) {
  99. (void)n_args;
  100. gpio_matrix_out(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3]));
  101. return mp_const_none;
  102. }
  103. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_gpio_matrix_out_obj, 4, 4, esp_gpio_matrix_out);
  104. STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t timing) {
  105. mp_buffer_info_t bufinfo;
  106. mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
  107. esp_neopixel_write(mp_hal_get_pin_obj(pin),
  108. (uint8_t*)bufinfo.buf, bufinfo.len, mp_obj_get_int(timing));
  109. return mp_const_none;
  110. }
  111. STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_);
  112. STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
  113. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
  114. { MP_ROM_QSTR(MP_QSTR_osdebug), MP_ROM_PTR(&esp_osdebug_obj) },
  115. { MP_ROM_QSTR(MP_QSTR_flash_read), MP_ROM_PTR(&esp_flash_read_obj) },
  116. { MP_ROM_QSTR(MP_QSTR_flash_write), MP_ROM_PTR(&esp_flash_write_obj) },
  117. { MP_ROM_QSTR(MP_QSTR_flash_erase), MP_ROM_PTR(&esp_flash_erase_obj) },
  118. { MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&esp_flash_size_obj) },
  119. { MP_ROM_QSTR(MP_QSTR_flash_user_start), MP_ROM_PTR(&esp_flash_user_start_obj) },
  120. { MP_ROM_QSTR(MP_QSTR_gpio_matrix_in), MP_ROM_PTR(&esp_gpio_matrix_in_obj) },
  121. { MP_ROM_QSTR(MP_QSTR_gpio_matrix_out), MP_ROM_PTR(&esp_gpio_matrix_out_obj) },
  122. { MP_ROM_QSTR(MP_QSTR_neopixel_write), MP_ROM_PTR(&esp_neopixel_write_obj) },
  123. { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
  124. // Constants for second arg of osdebug()
  125. { MP_ROM_QSTR(MP_QSTR_LOG_NONE), MP_ROM_INT((mp_uint_t)ESP_LOG_NONE)},
  126. { MP_ROM_QSTR(MP_QSTR_LOG_ERROR), MP_ROM_INT((mp_uint_t)ESP_LOG_ERROR)},
  127. { MP_ROM_QSTR(MP_QSTR_LOG_WARNING), MP_ROM_INT((mp_uint_t)ESP_LOG_WARN)},
  128. { MP_ROM_QSTR(MP_QSTR_LOG_INFO), MP_ROM_INT((mp_uint_t)ESP_LOG_INFO)},
  129. { MP_ROM_QSTR(MP_QSTR_LOG_DEBUG), MP_ROM_INT((mp_uint_t)ESP_LOG_DEBUG)},
  130. { MP_ROM_QSTR(MP_QSTR_LOG_VERBOSE), MP_ROM_INT((mp_uint_t)ESP_LOG_VERBOSE)},
  131. };
  132. STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table);
  133. const mp_obj_module_t esp_module = {
  134. .base = { &mp_type_module },
  135. .globals = (mp_obj_dict_t*)&esp_module_globals,
  136. };