diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/cc3000/inc/cc3000_common.h | 4 | ||||
-rw-r--r-- | drivers/cc3000/inc/socket.h | 2 | ||||
-rw-r--r-- | drivers/cc3000/src/socket.c | 2 | ||||
-rw-r--r-- | drivers/dht/dht.c | 89 | ||||
-rw-r--r-- | drivers/dht/dht.h | 3 | ||||
-rw-r--r-- | drivers/display/ssd1306.py | 31 |
6 files changed, 116 insertions, 15 deletions
diff --git a/drivers/cc3000/inc/cc3000_common.h b/drivers/cc3000/inc/cc3000_common.h index d263da6591..aa16242310 100644 --- a/drivers/cc3000/inc/cc3000_common.h +++ b/drivers/cc3000/inc/cc3000_common.h @@ -169,9 +169,9 @@ typedef INT32 time_t; typedef UINT32 clock_t; typedef INT32 suseconds_t; -typedef struct timeval timeval; +typedef struct cc3000_timeval cc3000_timeval; -struct timeval +struct cc3000_timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ diff --git a/drivers/cc3000/inc/socket.h b/drivers/cc3000/inc/socket.h index 8c2a0aa16b..96c814bf77 100644 --- a/drivers/cc3000/inc/socket.h +++ b/drivers/cc3000/inc/socket.h @@ -416,7 +416,7 @@ extern INT32 CC3000_EXPORT(connect)(INT32 sd, const sockaddr *addr, INT32 addrle // //***************************************************************************** extern INT16 CC3000_EXPORT(select)(INT32 nfds, fd_set *readsds, fd_set *writesds, - fd_set *exceptsds, struct timeval *timeout); + fd_set *exceptsds, struct cc3000_timeval *timeout); //***************************************************************************** // diff --git a/drivers/cc3000/src/socket.c b/drivers/cc3000/src/socket.c index 6261caeb0a..ddd7e56e80 100644 --- a/drivers/cc3000/src/socket.c +++ b/drivers/cc3000/src/socket.c @@ -587,7 +587,7 @@ INT32 CC3000_EXPORT(connect)(INT32 sd, const sockaddr *addr, INT32 addrlen) //***************************************************************************** INT16 CC3000_EXPORT(select)(INT32 nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, -struct timeval *timeout) +struct cc3000_timeval *timeout) { UINT8 *ptr, *args; tBsdSelectRecvParams tParams; diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c new file mode 100644 index 0000000000..95b0744510 --- /dev/null +++ b/drivers/dht/dht.c @@ -0,0 +1,89 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * + * 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 "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" +#include "extmod/machine_pulse.h" +#include "drivers/dht/dht.h" + +STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) { + mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in); + mp_hal_pin_open_drain(pin); + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); + + if (bufinfo.len < 5) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small")); + } + + // issue start command + mp_hal_pin_od_high(pin); + mp_hal_delay_ms(250); + mp_hal_pin_od_low(pin); + mp_hal_delay_ms(18); + + mp_uint_t irq_state = mp_hal_quiet_timing_enter(); + + // release the line so the device can respond + mp_hal_pin_od_high(pin); + mp_hal_delay_us_fast(10); + + // wait for device to respond + mp_uint_t ticks = mp_hal_ticks_us(); + while (mp_hal_pin_read(pin) != 0) { + if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) { + goto timeout; + } + } + + // time pulse, should be 80us + ticks = machine_time_pulse_us(pin, 1, 150); + if (ticks == (mp_uint_t)-1) { + goto timeout; + } + + // time 40 pulses for data (either 26us or 70us) + uint8_t *buf = bufinfo.buf; + for (int i = 0; i < 40; ++i) { + ticks = machine_time_pulse_us(pin, 1, 100); + if (ticks == (mp_uint_t)-1) { + goto timeout; + } + buf[i / 8] = (buf[i / 8] << 1) | (ticks > 48); + } + + mp_hal_quiet_timing_exit(irq_state); + return mp_const_none; + +timeout: + mp_hal_quiet_timing_exit(irq_state); + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT))); +} +MP_DEFINE_CONST_FUN_OBJ_2(dht_readinto_obj, dht_readinto); diff --git a/drivers/dht/dht.h b/drivers/dht/dht.h new file mode 100644 index 0000000000..8274228ac6 --- /dev/null +++ b/drivers/dht/dht.h @@ -0,0 +1,3 @@ +#include "py/obj.h" + +MP_DECLARE_CONST_FUN_OBJ(dht_readinto_obj); diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py index 852e015a5e..3bccf72dae 100644 --- a/drivers/display/ssd1306.py +++ b/drivers/display/ssd1306.py @@ -3,6 +3,7 @@ import time import framebuf + # register definitions SET_CONTRAST = const(0x81) SET_ENTIRE_ON = const(0xa4) @@ -22,6 +23,7 @@ SET_PRECHARGE = const(0xd9) SET_VCOM_DESEL = const(0xdb) SET_CHARGE_PUMP = const(0x8d) + class SSD1306: def __init__(self, height, external_vcc): self.width = 128 @@ -91,6 +93,7 @@ class SSD1306: def text(self, string, x, y, col=1): self.framebuf.text(string, x, y, col) + class SSD1306_I2C(SSD1306): def __init__(self, height, i2c, addr=0x3c, external_vcc=False): self.i2c = i2c @@ -114,27 +117,34 @@ class SSD1306_I2C(SSD1306): def poweron(self): pass -# TODO convert this class to use the new hardware API + class SSD1306_SPI(SSD1306): - def __init__(self, height, spi, dc, res, cs=None, external_vcc=False): - rate = 10 * 1024 * 1024 - spi.init(spi.MASTER, baudrate=rate, polarity=0, phase=0) - dc.init(dc.OUT, dc.PULL_NONE, value=0) - res.init(res.OUT, dc.PULL_NONE, value=0) - if cs is not None: - cs.init(cs.OUT, cs.PULL_NONE, value=0) + def __init__(self, height, spi, dc, res, cs, external_vcc=False): + self.rate = 10 * 1024 * 1024 + dc.init(dc.OUT, value=0) + res.init(res.OUT, value=0) + cs.init(cs.OUT, value=1) self.spi = spi self.dc = dc self.res = res + self.cs = cs super().__init__(height, external_vcc) def write_cmd(self, cmd): + self.spi.init(baudrate=self.rate, polarity=0, phase=0) + self.cs.high() self.dc.low() - self.spi.send(cmd) + self.cs.low() + self.spi.write(bytearray([cmd])) + self.cs.high() def write_data(self, buf): + self.spi.init(baudrate=self.rate, polarity=0, phase=0) + self.cs.high() self.dc.high() - self.spi.send(buf) + self.cs.low() + self.spi.write(buf) + self.cs.high() def poweron(self): self.res.high() @@ -142,4 +152,3 @@ class SSD1306_SPI(SSD1306): self.res.low() time.sleep_ms(10) self.res.high() - time.sleep_ms(10) |