Permalink
Browse files

Added I2C and EepromI2C drivers.

  • Loading branch information...
eerimoq committed Jul 3, 2017
1 parent 8f04e8e commit 669d95c5d430d511f8e5334f1c4ba8395c6cf0e2
@@ -14,6 +14,7 @@ The following classes are defined:
- :class:`class Dac<.Dac>` -- Digital to analog convertion
- :class:`class Spi<.Spi>` -- Serial peripheral interface
- :class:`class Can<.Can>` -- Controller Area Network
- :class:`class I2C<.I2C>` -- I2C
- :class:`class I2CSoft<.I2CSoft>` -- Software I2C
- :class:`class Owi<.Owi>` -- Onewire
- :class:`class Ds18b20<.Ds18b20>` -- DS18B20 temperature
@@ -22,6 +23,7 @@ The following classes are defined:
- :class:`class Uart<.Uart>` -- Universal Asynchronous Receiver/Transmitter
- :class:`class Flash<.Flash>` -- Flash memory
- :class:`class Ws2812<.Ws2812>` -- WS2812 Neo Pixels
- :class:`class EepromI2C<.EepromI2C>` -- I2C EEPROM
Simba documentation: `drivers`_
@@ -399,6 +401,56 @@ Simba documentation: `drivers`_
Extended frame flag. A 29 bits frame id will be sent/received.
.. class:: drivers.I2C(device, baudrate=100000, address=-1)
Create a I2C object.
Here is an example of how to create a I2C obeject and scan the bus
to find conected devices.
.. code-block:: python
>>> i2c = I2C(0)
>>> i2c.start()
>>> i2c.scan()
[87, 104]
>>> i2c.stop()
Simba documentation: `drivers/i2c`_
.. method:: start()
Start the i2c driver. Configures the hardware.
.. method:: stop()
Stop the i2c driver. Resets the hardware.
.. method:: read(address, size)
Read `size` bytes from slave with address `address`.
.. method:: read_into(address, buffer[, size])
Read ``len(buffer)`` bytes from slave with address `address`
into `buffer`. Give the argument `size` to read fewer bytes than
``len(buffer)``.
.. method:: write(address, buffer[, size])
Write the buffer `buffer` to slave with address `address`.
.. method:: scan()
Scan the bus and return a list of all found slave addresses.
.. class:: drivers.I2CSoft(scl, sda, baudrate=50000, max_clock_stretching_sleep_us=1000000, clock_stretching_sleep_us=10000)
Create a I2CSoft object.
@@ -858,7 +910,7 @@ Simba documentation: `drivers`_
to the flash start address or an absolute physical address in the
CPU memory map. This is board dependent and it is not documented
anywhere.
Here is an example of how to create a flash driver and use the
erase, read and write methods.
@@ -924,6 +976,42 @@ Simba documentation: `drivers`_
data in `buffer` if `size` is not given.
.. class:: drivers.EepromI2C(i2c, address, size)
Create a I2C EEPROM object.
Here is an example of how to create an I2C EEPROM obeject and
transfer data to and from the EEPROM using it.
.. code-block:: python
>>> i2c = I2C(0)
>>> i2c.start()
>>> eeprom = EepromI2C(i2c, 0x57, 32768)
>>> eeprom.write(0, b'Hello World!')
>>> eeprom.read(0, 12)
b'Hello World!'
Simba documentation: `drivers/eeprom_i2c`_
.. method:: read(address, size)
Read `size` bytes from EEPROM address `address`.
.. method:: read_into(address, buffer[, size])
Read ``len(buffer)`` bytes from EEPROM address `address` into
`buffer`. Give the argument `size` to read fewer bytes than
``len(buffer)``.
.. method:: write(address, buffer[, size])
Write the buffer `buffer` to EEPROM address `address`.
.. _drivers: http://simba-os.readthedocs.io/en/latest/library-reference/drivers.html
.. _drivers/pin: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/pin.html
.. _drivers/exti: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/exti.html
@@ -932,10 +1020,12 @@ Simba documentation: `drivers`_
.. _drivers/spi: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/spi.html
.. _drivers/can: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/can.html
.. _drivers/sd: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/sd.html
.. _drivers/i2c: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/i2c.html
.. _drivers/i2c_soft: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/i2c_soft.html
.. _drivers/owi: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/owi.html
.. _drivers/ds18b20: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/ds18b20.html
.. _drivers/esp_wifi: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/esp_wifi.html
.. _drivers/uart: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/uart.html
.. _drivers/flash: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/flash.html
.. _drivers/ws2812: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/ws2812.html
.. _drivers/eeprom_i2c: http://simba-os.readthedocs.io/en/latest/library-reference/drivers/eeprom_i2c.html
@@ -0,0 +1,36 @@
#
# @section License
#
# The MIT License (MIT)
#
# Copyright (c) 2016-2017, Erik Moqvist
#
# 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.
#
# This file is part of the Pumbaa project.
#
NAME = eeprom_i2c
BOARD ?= linux
PUMBAA_ROOT ?= ../..
include $(PUMBAA_ROOT)/make/app.mk
@@ -0,0 +1,49 @@
#
# @section License
#
# The MIT License (MIT)
#
# Copyright (c) 2017, Erik Moqvist
#
# 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.
#
# This file is part of the Pumbaa project.
#
from drivers import I2C, EepromI2C
# Change those to match your EEPROM.
EEPROM_I2C_ADDRESS = 0x57
EEPROM_SIZE = 32768
i2c = I2C(0)
i2c.start()
eeprom = EepromI2C(i2c, EEPROM_I2C_ADDRESS, EEPROM_SIZE)
address = 0
data = b'Hello World!'
print('Writing {} to EEPROM address {}:'.format(data, address))
eeprom.write(address, data)
read_data = eeprom.read(address, 12)
print('Read {} from EEPROM address {}.'.format(read_data, address))
2 simba
Submodule simba updated 187 files
View
@@ -53,6 +53,15 @@ static mp_obj_t module_init(void)
#if CONFIG_PUMBAA_CLASS_FLASH == 1
flash_module_init();
#endif
#if CONFIG_PUMBAA_CLASS_I2C == 1
i2c_module_init();
#endif
#if CONFIG_PUMBAA_CLASS_I2C_SOFT == 1
i2c_soft_module_init();
#endif
#if CONFIG_PUMBAA_CLASS_EEPROM_I2C == 1
eeprom_i2c_module_init();
#endif
return (mp_const_none);
}
@@ -88,9 +97,15 @@ static const mp_rom_map_elem_t module_drivers_globals_table[] = {
#if CONFIG_PUMBAA_CLASS_SPI == 1
{ MP_ROM_QSTR(MP_QSTR_Spi), MP_ROM_PTR(&module_drivers_class_spi) },
#endif
#if CONFIG_PUMBAA_CLASS_I2C == 1
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&module_drivers_class_i2c) },
#endif
#if CONFIG_PUMBAA_CLASS_I2C_SOFT == 1
{ MP_ROM_QSTR(MP_QSTR_I2CSoft), MP_ROM_PTR(&module_drivers_class_i2c_soft) },
#endif
#if CONFIG_PUMBAA_CLASS_EEPROM_I2C == 1
{ MP_ROM_QSTR(MP_QSTR_EepromI2C), MP_ROM_PTR(&module_drivers_class_eeprom_i2c) },
#endif
#if CONFIG_PUMBAA_CLASS_ESP_WIFI == 1
{ MP_ROM_QSTR(MP_QSTR_esp_wifi), MP_ROM_PTR(&module_drivers_esp_wifi_obj) },
#endif
Oops, something went wrong.

0 comments on commit 669d95c

Please sign in to comment.