machine_hw_i2c.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 SummerGift <zhangyuan@rt-thread.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/mphal.h"
  30. #include "py/mperrno.h"
  31. #include "extmod/machine_i2c.h"
  32. #ifdef MICROPYTHON_USING_MACHINE_I2C
  33. STATIC const mp_obj_type_t machine_hard_i2c_type;
  34. typedef struct _machine_hard_i2c_obj_t {
  35. mp_obj_base_t base;
  36. struct rt_i2c_bus_device *i2c_bus;
  37. } machine_hard_i2c_obj_t;
  38. #ifndef RT_USING_I2C
  39. #error "Please define the RT_USING_I2C on 'rtconfig.h'"
  40. #endif
  41. STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  42. machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
  43. mp_printf(print,"I2C(%s, timeout=%u)",
  44. self->i2c_bus->parent.parent.name,
  45. self->i2c_bus->timeout);
  46. return;
  47. }
  48. int machine_hard_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
  49. machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
  50. return rt_i2c_master_recv(self->i2c_bus, addr, 0, dest, len);
  51. }
  52. int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
  53. uint8_t buf[1] = {0};
  54. machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
  55. if (len == 0){
  56. len = 1;
  57. if (src == NULL){
  58. src = buf;
  59. }
  60. return !rt_i2c_master_send(self->i2c_bus, addr, 0, src, len);
  61. } else if (src == NULL){
  62. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "buf must not NULL"));
  63. }
  64. return rt_i2c_master_send(self->i2c_bus, addr, 0, src, len);
  65. }
  66. /******************************************************************************/
  67. /* MicroPython bindings for machine API */
  68. mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  69. char iic_device[RT_NAME_MAX];
  70. snprintf(iic_device, sizeof(iic_device), "i2c%d", mp_obj_get_int(all_args[0]));
  71. struct rt_i2c_bus_device *i2c_bus = rt_i2c_bus_device_find(iic_device);
  72. if (i2c_bus == RT_NULL) {
  73. rt_kprintf("can't find %s device\r\n", iic_device);
  74. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C(%s) doesn't exist", iic_device));
  75. }
  76. // create new hard I2C object
  77. machine_hard_i2c_obj_t *self = m_new_obj(machine_hard_i2c_obj_t);
  78. self->base.type = &machine_hard_i2c_type;
  79. self->i2c_bus = i2c_bus;
  80. return (mp_obj_t) self;
  81. }
  82. STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
  83. .start = NULL,
  84. .stop = NULL,
  85. .read = NULL,
  86. .write = NULL,
  87. .readfrom = machine_hard_i2c_readfrom,
  88. .writeto = machine_hard_i2c_writeto,
  89. };
  90. STATIC const mp_obj_type_t machine_hard_i2c_type = {
  91. { &mp_type_type },
  92. .name = MP_QSTR_I2C,
  93. .print = machine_hard_i2c_print,
  94. .make_new = machine_hard_i2c_make_new,
  95. .protocol = &machine_hard_i2c_p,
  96. .locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict,
  97. };
  98. #endif // MICROPYTHON_USING_MACHINE_I2C