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

1、【增加】:I2C 相关功能函数,添加 Hardware 分类。

SummerGift 8 лет назад
Родитель
Сommit
8d56347b92
5 измененных файлов с 41 добавлено и 12 удалено
  1. 8 8
      extmod/machine_i2c.c
  2. 9 1
      port/machine_pin.c
  3. 3 1
      port/modmachine.c
  4. 12 2
      port/mpconfigport.h
  5. 9 0
      port/mphalport.h

+ 8 - 8
extmod/machine_i2c.c

@@ -40,21 +40,21 @@ typedef mp_machine_soft_i2c_obj_t machine_i2c_obj_t;
 STATIC void mp_hal_i2c_delay(machine_i2c_obj_t *self) {
     // We need to use an accurate delay to get acceptable I2C
     // speeds (eg 1us should be not much more than 1us).
-    // mp_hal_delay_us_fast(self->us_delay);
+    mp_hal_delay_us_fast(self->us_delay);
 }
 
 STATIC void mp_hal_i2c_scl_low(machine_i2c_obj_t *self) {
-    //mp_hal_pin_od_low(self->scl);
+    mp_hal_pin_od_low(self->scl);
 }
 
 STATIC int mp_hal_i2c_scl_release(machine_i2c_obj_t *self) {
     uint32_t count = self->us_timeout;
 
-    //mp_hal_pin_od_high(self->scl);
+    mp_hal_pin_od_high(self->scl);
     mp_hal_i2c_delay(self);
     // For clock stretching, wait for the SCL pin to be released, with timeout.
     for (; mp_hal_pin_read(self->scl) == 0 && count; --count) {
-        // mp_hal_delay_us_fast(1);
+         mp_hal_delay_us_fast(1);
     }
     if (count == 0) {
         return -MP_ETIMEDOUT;
@@ -63,11 +63,11 @@ STATIC int mp_hal_i2c_scl_release(machine_i2c_obj_t *self) {
 }
 
 STATIC void mp_hal_i2c_sda_low(machine_i2c_obj_t *self) {
-    //mp_hal_pin_od_low(self->sda);
+    mp_hal_pin_od_low(self->sda);
 }
 
 STATIC void mp_hal_i2c_sda_release(machine_i2c_obj_t *self) {
-    //mp_hal_pin_od_high(self->sda);
+    mp_hal_pin_od_high(self->sda);
 }
 
 STATIC int mp_hal_i2c_sda_read(machine_i2c_obj_t *self) {
@@ -101,8 +101,8 @@ STATIC void mp_hal_i2c_init(machine_i2c_obj_t *self, uint32_t freq) {
     if (self->us_delay == 0) {
         self->us_delay = 1;
     }
-    //mp_hal_pin_open_drain(self->scl);
-    //mp_hal_pin_open_drain(self->sda);
+    mp_hal_pin_open_drain(self->scl);
+    mp_hal_pin_open_drain(self->sda);
     mp_hal_i2c_stop(self); // ignore error
 }
 

+ 9 - 1
port/machine_pin.c

@@ -53,6 +53,14 @@
 
 const mp_obj_base_t machine_pin_obj_template = {&machine_pin_type};
 
+void mp_pin_od_write(void *machine_pin, int stat) {
+    rt_pin_write(((machine_pin_obj_t *)machine_pin)->pin, stat);
+}
+
+void mp_hal_pin_open_set(void *machine_pin, int mode) {
+    rt_pin_mode(((machine_pin_obj_t *)machine_pin)->pin, mode);
+}
+
 STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
 
 STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -243,4 +251,4 @@ const mp_obj_type_t machine_pin_type = {
     .locals_dict = (mp_obj_t)&machine_pin_locals_dict,
 };
 
-#endif
+#endif

+ 3 - 1
port/modmachine.c

@@ -190,7 +190,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
 #if MICROPY_PY_MACHINE_I2C
     { MP_ROM_QSTR(MP_QSTR_I2C),                 MP_ROM_PTR(&machine_i2c_type) },
 #endif
-//    { MP_ROM_QSTR(MP_QSTR_SPI),                 MP_ROM_PTR(&machine_hard_spi_type) },
+#if MICROPY_PY_MACHINE_SPI
+    { MP_ROM_QSTR(MP_QSTR_SPI),                 MP_ROM_PTR(&machine_hard_spi_type) },
+#endif
 //    { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&pyb_uart_type) },
 //    { MP_ROM_QSTR(MP_QSTR_WDT),                 MP_ROM_PTR(&pyb_wdt_type) },
     { MP_ROM_QSTR(MP_QSTR_PWRON_RESET),         MP_ROM_INT(PYB_RESET_POWER_ON) },

+ 12 - 2
port/mpconfigport.h

@@ -124,8 +124,6 @@
 #define MICROPY_PY_UTIME_MP_HAL     (1)
 #define MICROPY_PY_UTIMEQ           (1)
 #define MICROPY_PY_RTTHREAD         (1)
-#define MICROPY_PY_MACHINE_I2C      (1)
-
 
 /*****************************************************************************/
 /* System Module                                                             */
@@ -201,9 +199,21 @@
 
 /*****************************************************************************/
 /* Third-party Module                                                        */
+
 #define MICROPY_PY_USSL             (0)
 #define MICROPY_SSL_MBEDTLS         (0)
 
+/*****************************************************************************/
+/* Hardware Module                                                              */
+
+#ifdef MICROPYTHON_USING_MACHINE_I2C
+#define MICROPY_PY_MACHINE_I2C      (1)
+#endif
+
+#if MICROPYTHON_USING_MACHINE_SPI
+#define MICROPY_PY_MACHINE_SPI      (1)
+#endif
+
 #if MICROPY_PY_THREAD
 #define MICROPY_EVENT_POLL_HOOK \
     do { \

+ 9 - 0
port/mphalport.h

@@ -47,3 +47,12 @@ static inline void mp_hal_delay_ms(mp_uint_t delay) {
 }
 
 extern void mp_hal_set_interrupt_char (int c);
+extern void mp_pin_od_write(void *machine_pin, int stat);
+extern void mp_hal_pin_open_set(void *machine_pin, int mode);
+
+// needed for machine.I2C
+#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
+#define mp_hal_pin_od_low(pin)   mp_pin_od_write(pin, PIN_LOW)
+#define mp_hal_pin_od_high(pin)  mp_pin_od_write(pin, PIN_HIGH)
+#define mp_hal_pin_open_drain(p) mp_hal_pin_open_set(p, PIN_MODE_OUTPUT_OD)
+