Skip to content

Commit 56d2b47

Browse files
committed
rp2/machine_i2c: Factor default pin macros to header file.
So they can be reused by the I2CTarget implementation. Signed-off-by: Damien George <[email protected]>
1 parent 01e570a commit 56d2b47

File tree

2 files changed

+69
-39
lines changed

2 files changed

+69
-39
lines changed

ports/rp2/machine_i2c.c

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,13 @@
2828
#include "py/mphal.h"
2929
#include "py/mperrno.h"
3030
#include "extmod/modmachine.h"
31+
#include "machine_i2c.h"
3132

3233
#include "hardware/i2c.h"
3334

3435
#define DEFAULT_I2C_FREQ (400000)
3536
#define DEFAULT_I2C_TIMEOUT (50000)
3637

37-
#ifdef MICROPY_HW_I2C_NO_DEFAULT_PINS
38-
39-
// With no default I2C, need to require the pin args.
40-
#define MICROPY_HW_I2C0_SCL (0)
41-
#define MICROPY_HW_I2C0_SDA (0)
42-
#define MICROPY_HW_I2C1_SCL (0)
43-
#define MICROPY_HW_I2C1_SDA (0)
44-
#define MICROPY_I2C_PINS_ARG_OPTS MP_ARG_REQUIRED
45-
46-
#else
47-
48-
// Most boards do not require pin args.
49-
#define MICROPY_I2C_PINS_ARG_OPTS 0
50-
51-
#ifndef MICROPY_HW_I2C0_SCL
52-
#if PICO_DEFAULT_I2C == 0
53-
#define MICROPY_HW_I2C0_SCL (PICO_DEFAULT_I2C_SCL_PIN)
54-
#define MICROPY_HW_I2C0_SDA (PICO_DEFAULT_I2C_SDA_PIN)
55-
#else
56-
#define MICROPY_HW_I2C0_SCL (9)
57-
#define MICROPY_HW_I2C0_SDA (8)
58-
#endif
59-
#endif
60-
61-
#ifndef MICROPY_HW_I2C1_SCL
62-
#if PICO_DEFAULT_I2C == 1
63-
#define MICROPY_HW_I2C1_SCL (PICO_DEFAULT_I2C_SCL_PIN)
64-
#define MICROPY_HW_I2C1_SDA (PICO_DEFAULT_I2C_SDA_PIN)
65-
#else
66-
#define MICROPY_HW_I2C1_SCL (7)
67-
#define MICROPY_HW_I2C1_SDA (6)
68-
#endif
69-
#endif
70-
#endif
71-
72-
// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins.
73-
#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c))
74-
#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c))
75-
7638
typedef struct _machine_i2c_obj_t {
7739
mp_obj_base_t base;
7840
i2c_inst_t *const i2c_inst;

ports/rp2/machine_i2c.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020-2025 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_RP2_MACHINE_I2C_H
27+
#define MICROPY_INCLUDED_RP2_MACHINE_I2C_H
28+
29+
#ifdef MICROPY_HW_I2C_NO_DEFAULT_PINS
30+
31+
// With no default I2C, need to require the pin args.
32+
#define MICROPY_HW_I2C0_SCL (0)
33+
#define MICROPY_HW_I2C0_SDA (0)
34+
#define MICROPY_HW_I2C1_SCL (0)
35+
#define MICROPY_HW_I2C1_SDA (0)
36+
#define MICROPY_I2C_PINS_ARG_OPTS MP_ARG_REQUIRED
37+
38+
#else
39+
40+
// Most boards do not require pin args.
41+
#define MICROPY_I2C_PINS_ARG_OPTS 0
42+
43+
#ifndef MICROPY_HW_I2C0_SCL
44+
#if PICO_DEFAULT_I2C == 0
45+
#define MICROPY_HW_I2C0_SCL (PICO_DEFAULT_I2C_SCL_PIN)
46+
#define MICROPY_HW_I2C0_SDA (PICO_DEFAULT_I2C_SDA_PIN)
47+
#else
48+
#define MICROPY_HW_I2C0_SCL (9)
49+
#define MICROPY_HW_I2C0_SDA (8)
50+
#endif
51+
#endif
52+
53+
#ifndef MICROPY_HW_I2C1_SCL
54+
#if PICO_DEFAULT_I2C == 1
55+
#define MICROPY_HW_I2C1_SCL (PICO_DEFAULT_I2C_SCL_PIN)
56+
#define MICROPY_HW_I2C1_SDA (PICO_DEFAULT_I2C_SDA_PIN)
57+
#else
58+
#define MICROPY_HW_I2C1_SCL (7)
59+
#define MICROPY_HW_I2C1_SDA (6)
60+
#endif
61+
#endif
62+
#endif
63+
64+
// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins.
65+
#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c))
66+
#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c))
67+
68+
#endif // MICROPY_INCLUDED_RP2_MACHINE_I2C_H

0 commit comments

Comments
 (0)