Просмотр исходного кода

Merge branch 'master' of https://github.com/RT-Thread-packages/micropython.git

armink 8 лет назад
Родитель
Сommit
622b7cff53
4 измененных файлов с 186 добавлено и 18 удалено
  1. 39 17
      README.md
  2. 1 1
      port/machine_hw_i2c.c
  3. 145 0
      port/machine_hw_spi.c
  4. 1 0
      port/mpconfigport.h

+ 39 - 17
README.md

@@ -58,14 +58,14 @@ Use the [`time`](http://docs.micropython.org/en/latest/pyboard/library/utime.htm
 >>> delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
 ```
 
-### pyb - functions related to the board
+### machine - functions related to the hardware
 
-See [pyb](http://docs.micropython.org/en/latest/pyboard/library/pyb.html).
+See [machine](http://docs.micropython.org/en/latest/pyboard/library/machine.html).
 
 ```
->>> import pyb
+>>> import machine
 >>>
->>> pyb.info()              # show information about the board
+>>> machine.info()              # show information about the board
 ---------------------------------------------
 RT-Thread
 ---------------------------------------------
@@ -90,22 +90,14 @@ GC:
   16064 total
   464 : 15600
   1=14 2=6 m=3
->>> pyb.enable_irq()        # enable interrupt
->>> pyb.disable_irq()       # disable interrupt, WARNING: this operation is dangerous
->>> time_start = pyb.millis()          # return the number of milliseconds
->>> pyb.elapsed_millis(time_start)     # calculate the elapsed time of milliseconds
-2449
->>> time_start = pyb.micros()          # return the number of microseconds
->>> pyb.elapsed_micros(time_start)     # calculate the elapsed time of microseconds
-1769000
->>> pyb.delay(1000)         # delay milliseconds
->>> pyb.udelay(1000*1000)   # delay microseconds
->>> pyb.hard_reset()        # hard reset, like push RESET button
+>>> machine.enable_irq()        # enable interrupt
+>>> machine.disable_irq()       # disable interrupt, WARNING: this operation is dangerous
+>>> machine.reset()        # hard reset, like push RESET button
 ```
 
 #### Pins and GPIO
 
-See [pyb.Pin](http://docs.micropython.org/en/latest/pyboard/library/pyb.Pin.html#pyb-pin).
+See [machine.Pin](http://docs.micropython.org/en/latest/pyboard/library/machine.Pin.html).
 
 ```
 >>> from pyb import Pin
@@ -174,6 +166,9 @@ b'rt-thread\r'
 
 #####  I2C
 
+See [machine.I2C](http://docs.micropython.org/en/latest/pyboard/library/machine.I2C.html).
+
+`software I2C` :
 ```
 >>> from machine import Pin, I2C
 >>> clk = Pin(("clk", 43), Pin.OUT_OD)   # Select the 43 pin device as the clock
@@ -191,8 +186,19 @@ b'\x12'                               # starting at memory-address 8 in the slav
                                       # starting at address 2 in the slave
 ```
 
+`hardware I2C` :
+```
+>>> from machine import Pin, I2C
+>>> i2c = I2C(0)           # create I2C peripheral at frequency of 100kHz
+>>> i2c.scan()                        # scan for slaves, returning a list of 7-bit addresses
+[81]                                  # Decimal representation
+```
+
 #####  SPI
 
+See [machine.SPI](http://docs.micropython.org/en/latest/pyboard/library/machine.SPI.html).
+
+`software SPI` :
 ```
 >>> from machine import Pin, SPI
 >>> clk = Pin(("clk", 43), Pin.OUT_PP)
@@ -206,4 +212,20 @@ SoftSPI(baudrate=500000, polarity=0, phase=0, sck=clk, mosi=mosi, miso=miso)
 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
 ```
 
-### Coming soon
+`hardware SPI` :
+```
+>>> from machine import SPI
+>>> spi = SPI(50)
+>>> print(spi)
+SPI(device port : spi50)
+>>> spi.write(b'\x9f')
+>>> spi.read(5)
+b'\xff\xff\xff\xff\xff'
+>>> buf = bytearray(1)
+>>> spi.write_readinto(b"\x9f",buf)
+>>> buf
+bytearray(b'\xef')
+>>> spi.init(100000,0,0,8,1)     # Resetting SPI parameter
+```
+
+### Coming soon 

+ 1 - 1
port/machine_hw_i2c.c

@@ -67,7 +67,7 @@ int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_
 /* MicroPython bindings for machine API                                       */
 
 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) {
-    char iic_device[10];
+    char iic_device[RT_NAME_MAX];
 
     snprintf(iic_device, sizeof(iic_device), "i2c%d", mp_obj_get_int(all_args[0]));
     struct rt_i2c_bus_device *i2c_bus = rt_i2c_bus_device_find(iic_device);

+ 145 - 0
port/machine_hw_spi.c

@@ -0,0 +1,145 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 SummerGift <zhangyuan@rt-thread.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "extmod/machine_spi.h"
+
+#ifdef MICROPYTHON_USING_MACHINE_SPI
+
+STATIC const mp_obj_type_t machine_hard_spi_type;
+
+typedef struct _machine_hard_spi_obj_t {
+    mp_obj_base_t base;
+    struct rt_spi_device *spi_device;
+} machine_hard_spi_obj_t;
+
+STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+    machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t*)self_in;
+    mp_printf(print,"SPI(device port : %s)",self->spi_device->parent.parent.name);
+}
+
+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) {
+    char spi_dev_name[RT_NAME_MAX];
+
+    snprintf(spi_dev_name, sizeof(spi_dev_name), "spi%d", mp_obj_get_int(all_args[0]));
+
+    struct rt_spi_device *rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
+    if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
+        rt_kprintf("ERROR: SPI device %s not found!\n", spi_dev_name);
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI(%s) doesn't exist", spi_dev_name));
+    }
+
+    // create new hard SPI object
+    machine_hard_spi_obj_t *self = m_new_obj(machine_hard_spi_obj_t);
+    self->base.type = &machine_hard_spi_type;
+    self->spi_device = rt_spi_device;
+    return (mp_obj_t) self;
+}
+
+//SPI.init( baudrate=100000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB/LSB )
+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) {
+    machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t*)self_in;
+    rt_uint8_t mode = 0;
+    int baudrate = mp_obj_get_int(pos_args[0]);
+    int polarity = mp_obj_get_int(pos_args[1]);
+    int phase = mp_obj_get_int(pos_args[2]);
+    int bits = mp_obj_get_int(pos_args[3]);
+    int firstbit = mp_obj_get_int(pos_args[4]);
+
+    if(!polarity && !phase)
+    {
+        mode = RT_SPI_MODE_0;
+    }
+
+    if(!polarity && phase)
+    {
+        mode = RT_SPI_MODE_1;
+    }
+
+    if(polarity && !phase)
+    {
+        mode = RT_SPI_MODE_2;
+    }
+
+    if(polarity && phase)
+    {
+        mode = RT_SPI_MODE_3;
+    }
+
+    if(firstbit)
+    {
+        mode |= RT_SPI_MSB;
+    } else {
+        mode |= RT_SPI_LSB;
+    }
+
+    /* config spi */
+    {
+        struct rt_spi_configuration cfg;
+        cfg.data_width = bits;
+        cfg.mode = mode;
+        cfg.max_hz = baudrate;
+        rt_spi_configure(self->spi_device, &cfg);
+    }
+}
+
+STATIC void machine_hard_spi_deinit(mp_obj_base_t *self_in) {
+    return;
+}
+
+STATIC void machine_hard_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
+    machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t*)self_in;
+
+    if (src && dest) {
+        rt_spi_send_then_recv(self->spi_device, src, len, dest, len);
+    } else if (src) {
+        rt_spi_send(self->spi_device, src, len);
+    } else {
+        rt_spi_recv(self->spi_device, dest, len);
+    }
+}
+
+STATIC const mp_machine_spi_p_t machine_hard_spi_p = {
+    .init = machine_hard_spi_init,
+    .deinit = NULL,
+    .transfer = machine_hard_spi_transfer,
+};
+
+STATIC const mp_obj_type_t machine_hard_spi_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_SPI,
+    .print = machine_hard_spi_print,
+    .make_new = machine_hard_spi_make_new,
+    .protocol = &machine_hard_spi_p,
+    .locals_dict = (mp_obj_t)&mp_machine_spi_locals_dict,
+};
+
+#endif // MICROPYTHON_USING_MACHINE_SPI
+

+ 1 - 0
port/mpconfigport.h

@@ -219,6 +219,7 @@
 
 #ifdef MICROPYTHON_USING_MACHINE_SPI
 #define MICROPY_PY_MACHINE_SPI      (1)
+#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hard_spi_make_new
 #endif
 
 #define MICROPY_EVENT_POLL_HOOK rt_thread_delay(1);