machine_hw_spi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2018 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 "extmod/machine_spi.h"
  31. #ifdef MICROPYTHON_USING_MACHINE_SPI
  32. #ifndef RT_USING_SPI
  33. #error "Please define the RT_USING_SPI on 'rtconfig.h'"
  34. #endif
  35. STATIC const mp_obj_type_t machine_hard_spi_type;
  36. typedef struct _machine_hard_spi_obj_t {
  37. mp_obj_base_t base;
  38. struct rt_spi_device *spi_device;
  39. } machine_hard_spi_obj_t;
  40. STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  41. machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t*)self_in;
  42. mp_printf(print,"SPI(device port : %s)",self->spi_device->parent.parent.name);
  43. }
  44. mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  45. char spi_dev_name[RT_NAME_MAX];
  46. snprintf(spi_dev_name, sizeof(spi_dev_name), "spi%d", mp_obj_get_int(all_args[0]));
  47. struct rt_spi_device *rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
  48. if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
  49. mp_printf(&mp_plat_print, "ERROR: SPI device %s not found!\n", spi_dev_name);
  50. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI(%s) doesn't exist", spi_dev_name));
  51. }
  52. // create new hard SPI object
  53. machine_hard_spi_obj_t *self = m_new_obj(machine_hard_spi_obj_t);
  54. self->base.type = &machine_hard_spi_type;
  55. self->spi_device = rt_spi_device;
  56. return (mp_obj_t) self;
  57. }
  58. //SPI.init( baudrate=100000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB/LSB )
  59. STATIC void machine_hard_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  60. machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t*)self_in;
  61. rt_uint8_t mode = 0;
  62. int baudrate = mp_obj_get_int(pos_args[0]);
  63. int polarity = mp_obj_get_int(pos_args[1]);
  64. int phase = mp_obj_get_int(pos_args[2]);
  65. int bits = mp_obj_get_int(pos_args[3]);
  66. int firstbit = mp_obj_get_int(pos_args[4]);
  67. if(!polarity && !phase)
  68. {
  69. mode = RT_SPI_MODE_0;
  70. }
  71. if(!polarity && phase)
  72. {
  73. mode = RT_SPI_MODE_1;
  74. }
  75. if(polarity && !phase)
  76. {
  77. mode = RT_SPI_MODE_2;
  78. }
  79. if(polarity && phase)
  80. {
  81. mode = RT_SPI_MODE_3;
  82. }
  83. if(firstbit)
  84. {
  85. mode |= RT_SPI_MSB;
  86. } else {
  87. mode |= RT_SPI_LSB;
  88. }
  89. /* config spi */
  90. {
  91. struct rt_spi_configuration cfg;
  92. cfg.data_width = bits;
  93. cfg.mode = mode;
  94. cfg.max_hz = baudrate;
  95. rt_spi_configure(self->spi_device, &cfg);
  96. }
  97. }
  98. STATIC void machine_hard_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
  99. machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t*)self_in;
  100. if (src && dest) {
  101. rt_spi_send_then_recv(self->spi_device, src, len, dest, len);
  102. } else if (src) {
  103. rt_spi_send(self->spi_device, src, len);
  104. } else {
  105. rt_spi_recv(self->spi_device, dest, len);
  106. }
  107. }
  108. STATIC const mp_machine_spi_p_t machine_hard_spi_p = {
  109. .init = machine_hard_spi_init,
  110. .deinit = NULL,
  111. .transfer = machine_hard_spi_transfer,
  112. };
  113. STATIC const mp_obj_type_t machine_hard_spi_type = {
  114. { &mp_type_type },
  115. .name = MP_QSTR_SPI,
  116. .print = machine_hard_spi_print,
  117. .make_new = machine_hard_spi_make_new,
  118. .protocol = &machine_hard_spi_p,
  119. .locals_dict = (mp_obj_t)&mp_machine_spi_locals_dict,
  120. };
  121. #endif // MICROPYTHON_USING_MACHINE_SPI