Skip to content

Commit 91db71e

Browse files
authored
Merge pull request adafruit#841 from matt-land/feature-i2c-gemma
Feature: shortcut I2C support for atmel-samd boards
2 parents 6a8db03 + 0511bec commit 91db71e

File tree

39 files changed

+191
-0
lines changed

39 files changed

+191
-0
lines changed

ports/atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ SRC_ASF := $(addprefix asf4/$(CHIP_FAMILY)/, $(SRC_ASF))
227227

228228
SRC_C = \
229229
audio_dma.c \
230+
board_busses.c \
230231
background.c \
231232
clocks.c \
232233
$(CHIP_FAMILY)_clocks.c \

ports/atmel-samd/board_busses.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
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 "shared-bindings/busio/I2C.h"
28+
#include "shared-bindings/microcontroller/Pin.h"
29+
#include "mpconfigboard.h"
30+
#include "pins.h"
31+
#include "py/runtime.h"
32+
33+
34+
35+
#if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL)
36+
37+
STATIC mp_obj_t board_i2c(void) {
38+
//board_i2c_obj = NULL;
39+
mp_raise_NotImplementedError("No default I2C bus");
40+
return NULL;
41+
}
42+
43+
#else
44+
STATIC mp_obj_t i2c_singleton = NULL;
45+
46+
STATIC mp_obj_t board_i2c(void) {
47+
48+
if (i2c_singleton == NULL) {
49+
busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t);
50+
self->base.type = &busio_i2c_type;
51+
52+
assert_pin_free(DEFAULT_I2C_BUS_SDA);
53+
assert_pin_free(DEFAULT_I2C_BUS_SCL);
54+
common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000, 0);
55+
i2c_singleton = (mp_obj_t)self;
56+
}
57+
return i2c_singleton;
58+
59+
}
60+
#endif
61+
62+
MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);

ports/atmel-samd/board_busses.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
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+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H
28+
#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H
29+
30+
void board_i2c(void);
31+
extern mp_obj_fun_builtin_fixed_t board_i2c_obj;
32+
33+
#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_BUSSES_H

ports/atmel-samd/boards/arduino_zero/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
1414
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
1515

1616
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)
17+
18+
#define DEFAULT_I2C_BUS_SCL (&pin_PA23)
19+
#define DEFAULT_I2C_BUS_SDA (&pin_PA22)

ports/atmel-samd/boards/arduino_zero/pins.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "shared-bindings/board/__init__.h"
22

33
#include "samd21_pins.h"
4+
#include "board_busses.h"
5+
46

57
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
68
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) },
@@ -30,5 +32,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
3032
{ MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PB11) },
3133
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PB10) },
3234
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA12) },
35+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
3336
};
3437
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@
5858

5959
// Explanation of how a user got into safe mode.
6060
#define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up"
61+
62+
#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
63+
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)

ports/atmel-samd/boards/circuitplayground_express/pins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "samd21_pins.h"
2+
#include "board_busses.h"
23

34
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
45
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) },
@@ -56,5 +57,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
5657
{ MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PA05) },
5758
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA07) },
5859
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA06) },
60+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
5961
};
6062
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
1313

1414
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)
15+
16+
#define DEFAULT_I2C_BUS_SCL (&pin_PA22)
17+
#define DEFAULT_I2C_BUS_SDA (&pin_PA23)

ports/atmel-samd/boards/feather_m0_adalogger/pins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "samd21_pins.h"
2+
#include "board_busses.h"
23

34
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
45
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) },
@@ -30,5 +31,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
3031
{ MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA19) },
3132
{ MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_PA17) },
3233
{ MP_ROM_QSTR(MP_QSTR_RED_LED), MP_ROM_PTR(&pin_PA17) },
34+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
3335
};
3436
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
1414

1515
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)
16+
17+
#define DEFAULT_I2C_BUS_SCL (&pin_PA23)
18+
#define DEFAULT_I2C_BUS_SDA (&pin_PA22)

0 commit comments

Comments
 (0)