Skip to content

Commit de0666b

Browse files
committed
Add support for I2C communication in the Thymio3 MP Api
1 parent 1cadbf1 commit de0666b

3 files changed

Lines changed: 231 additions & 0 deletions

File tree

ports/esp32/modthymio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "thymio_leds_buttons.h"
5959
#include "thymio_led_receiver.h"
6060
#include "thymio_led_mic.h"
61+
#include "thymio_i2c.h"
6162
#include "../../../../../main/mode.h"
6263
#include "../../../../../main/utility.h"
6364
#include "../../../../../main/stm32_spi.h"
@@ -115,6 +116,7 @@ STATIC const mp_rom_map_elem_t thymio_module_globals_table[] = {
115116
{ MP_ROM_QSTR(MP_QSTR_LEDS_BUTTONS), MP_ROM_PTR(&thymio_leds_buttons_type) },
116117
{ MP_ROM_QSTR(MP_QSTR_LED_RECEIVER), MP_ROM_PTR(&thymio_led_receiver_type) },
117118
{ MP_ROM_QSTR(MP_QSTR_LED_MICROPHONE), MP_ROM_PTR(&thymio_led_microphone_type) },
119+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&thymio_i2c_type) },
118120
};
119121

120122
STATIC MP_DEFINE_CONST_DICT(tyhmio_module_globals, thymio_module_globals_table);

ports/esp32/thymio_i2c.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
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+
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "py/runtime.h"
31+
#include "py/mphal.h"
32+
#include "py/obj.h"
33+
#include "py/objstr.h"
34+
#include "thymio_i2c.h"
35+
#include "../../../../../main/i2c.h"
36+
37+
38+
/// \moduleref thymio
39+
/// \class I2C - I2C object
40+
///
41+
/// The I2C object read data from and write data to I2C bus.
42+
43+
typedef struct _thymio_i2c_obj_t {
44+
mp_obj_base_t base;
45+
} thymio_i2c_obj_t;
46+
47+
void i2c_init(void) {
48+
}
49+
50+
void i2c_write_reg(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint16_t size)
51+
{
52+
I2C_WriteToAddress(slaveAddress, registerAddress, data, size);
53+
}
54+
55+
void i2c_read_reg(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint16_t size)
56+
{
57+
I2C_ReadFromAddress(slaveAddress, registerAddress, data, size);
58+
}
59+
60+
void i2c_write_read(uint8_t slaveAddress, uint8_t* txData, uint16_t txSize, uint8_t* rxData, uint16_t rxSize)
61+
{
62+
I2C_WriteAndRead(slaveAddress, txData, txSize, rxData, rxSize);
63+
}
64+
65+
void i2c_read(uint8_t slaveAddress, uint8_t* data, uint16_t size)
66+
{
67+
I2C_Read(slaveAddress, data, size);
68+
}
69+
70+
void i2c_write(uint8_t slaveAddress, uint8_t* data, uint16_t size)
71+
{
72+
I2C_Write(slaveAddress, data, size);
73+
}
74+
75+
/******************************************************************************/
76+
/* MicroPython bindings */
77+
78+
void i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
79+
mp_printf(print, "I2C0");
80+
}
81+
82+
/// \classmethod \constructor()
83+
/// Create an I2C object:
84+
STATIC mp_obj_t i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
85+
thymio_i2c_obj_t *i2c = m_new_obj(thymio_i2c_obj_t);
86+
i2c->base.type = &thymio_i2c_type;
87+
return MP_OBJ_FROM_PTR(i2c);
88+
}
89+
90+
/// \method write_reg()
91+
/// Write to I2C slave register.
92+
/// Params:
93+
/// - `device address` 7 bit slave address.
94+
/// - `register address` this is the start address if more than one byte is written.
95+
/// - `tx data` data to be transmitted to the slave.
96+
/// - `tx data size` number of bytes to be transmitted
97+
STATIC mp_obj_t thymio_i2c_write_reg(size_t n_args, const mp_obj_t *args) {
98+
uint8_t dev_addr = mp_obj_get_int(args[1]);
99+
uint8_t reg_addr = mp_obj_get_int(args[2]);
100+
uint16_t size = mp_obj_get_int(args[4]);
101+
uint8_t *data = mp_obj_str_get_data(args[3], (size_t*)&size);
102+
i2c_write_reg(dev_addr, reg_addr, data, size);
103+
return mp_const_none;
104+
}
105+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thymio_i2c_write_reg_obj, 5, 5, thymio_i2c_write_reg);
106+
107+
/// \method read_reg()
108+
/// Read from I2C slave register.
109+
/// Params:
110+
/// - `device address` 7 bit slave address.
111+
/// - `register address` this is the start address if more than one byte is read.
112+
/// - `rx data size` number of bytes to be read from slave
113+
STATIC mp_obj_t thymio_i2c_read_reg(size_t n_args, const mp_obj_t *args) {
114+
uint8_t dev_addr = mp_obj_get_int(args[1]);
115+
uint8_t reg_addr = mp_obj_get_int(args[2]);
116+
uint16_t size = mp_obj_get_int(args[3]);
117+
byte *buf;
118+
buf = m_new(byte, size);
119+
i2c_read_reg(dev_addr, reg_addr, buf, size);
120+
return mp_obj_new_bytearray_by_ref(size, buf);
121+
}
122+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thymio_i2c_read_reg_obj, 4, 4, thymio_i2c_read_reg);
123+
124+
/// \method write_and_read()
125+
/// Raw write and read: the transmit buffer is sent to the slave and the following received data are returned.
126+
/// Params:
127+
/// - `device address` 7 bit slave address.
128+
/// - `tx data` data to be transmitted to the slave.
129+
/// - `tx data size` number of bytes to be transmitted
130+
/// - `rx data size` number of bytes ro be read from slave.
131+
STATIC mp_obj_t thymio_i2c_wr(size_t n_args, const mp_obj_t *args) {
132+
byte *rx_data;
133+
uint8_t dev_addr = mp_obj_get_int(args[1]);
134+
uint16_t tx_size = mp_obj_get_int(args[3]);
135+
uint8_t *tx_data = mp_obj_str_get_data(args[2], (size_t*)&tx_size);
136+
uint16_t rx_size = mp_obj_get_int(args[4]);
137+
rx_data = m_new(byte, rx_size);
138+
i2c_write_read(dev_addr, tx_data, tx_size, rx_data, rx_size);
139+
return mp_obj_new_bytearray_by_ref(rx_size, rx_data);
140+
}
141+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thymio_i2c_wr_obj, 5, 5, thymio_i2c_wr);
142+
143+
/// \method write()
144+
/// Raw write to I2C slave: the transmit buffer is sent to the slave.
145+
/// Params:
146+
/// - `device address` 7 bit slave address.
147+
/// - `tx data` data to be transmitted to the slave.
148+
/// - `tx data size` number of bytes to be transmitted
149+
STATIC mp_obj_t thymio_i2c_write(size_t n_args, const mp_obj_t *args) {
150+
uint8_t dev_addr = mp_obj_get_int(args[1]);
151+
uint16_t size = mp_obj_get_int(args[3]);
152+
uint8_t *data = mp_obj_str_get_data(args[2], (size_t*)&size);
153+
i2c_write(dev_addr, data, size);
154+
return mp_const_none;
155+
}
156+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thymio_i2c_write_obj, 4, 4, thymio_i2c_write);
157+
158+
/// \method read()
159+
/// Raw read from I2C slave.
160+
/// Params:
161+
/// - `device address` 7 bit slave address.
162+
/// - `rx data size` number of bytes to be read from slave
163+
STATIC mp_obj_t thymio_i2c_read(size_t n_args, const mp_obj_t *args) {
164+
uint8_t dev_addr = mp_obj_get_int(args[1]);
165+
uint16_t size = mp_obj_get_int(args[2]);
166+
byte *buf;
167+
buf = m_new(byte, size);
168+
i2c_read(dev_addr, buf, size);
169+
return mp_obj_new_bytearray_by_ref(size, buf);
170+
}
171+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thymio_i2c_read_obj, 3, 3, thymio_i2c_read);
172+
173+
STATIC const mp_rom_map_elem_t i2c_locals_dict_table[] = {
174+
{ MP_ROM_QSTR(MP_QSTR_write_reg), MP_ROM_PTR(&thymio_i2c_write_reg_obj) },
175+
{ MP_ROM_QSTR(MP_QSTR_read_reg), MP_ROM_PTR(&thymio_i2c_read_reg_obj) },
176+
{ MP_ROM_QSTR(MP_QSTR_wr), MP_ROM_PTR(&thymio_i2c_wr_obj) },
177+
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&thymio_i2c_write_obj) },
178+
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&thymio_i2c_read_obj) },
179+
};
180+
181+
STATIC MP_DEFINE_CONST_DICT(i2c_locals_dict, i2c_locals_dict_table);
182+
183+
MP_DEFINE_CONST_OBJ_TYPE(
184+
thymio_i2c_type,
185+
MP_QSTR_I2C,
186+
MP_TYPE_FLAG_NONE,
187+
make_new, i2c_make_new,
188+
print, i2c_print,
189+
locals_dict, &i2c_locals_dict
190+
);
191+

ports/esp32/thymio_i2c.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
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+
#ifndef MICROPY_INCLUDED_THYMIO_I2C_H
27+
#define MICROPY_INCLUDED_THYMIO_I2C_H
28+
#include <stdint.h>
29+
30+
void i2c_init(void);
31+
void i2c_write_reg(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint16_t size);
32+
void i2c_read_reg(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint16_t size);
33+
void i2c_write_read(uint8_t slaveAddress, uint8_t* txData, uint16_t txSize, uint8_t* rxData, uint16_t rxSize);
34+
void i2c_read(uint8_t slaveAddress, uint8_t* data, uint16_t size);
35+
void i2c_write(uint8_t slaveAddress, uint8_t* data, uint16_t size);
36+
extern const mp_obj_type_t thymio_i2c_type;
37+
38+
#endif // MICROPY_INCLUDED_THYMIO_I2C_H

0 commit comments

Comments
 (0)