2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-25 19:26:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2015-06-10 08:53:00 -04:00
|
|
|
#include "irq.h"
|
2014-04-19 19:30:09 -04:00
|
|
|
#include "pin.h"
|
2014-04-20 19:10:04 -04:00
|
|
|
#include "bufhelper.h"
|
2015-06-10 08:53:00 -04:00
|
|
|
#include "dma.h"
|
2014-03-25 19:26:14 -04:00
|
|
|
#include "i2c.h"
|
|
|
|
|
2018-05-13 23:53:46 -04:00
|
|
|
#if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C
|
2017-12-21 23:20:42 -05:00
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class I2C - a two-wire serial protocol
|
|
|
|
///
|
|
|
|
/// I2C is a two-wire protocol for communicating between devices. At the physical
|
|
|
|
/// level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
|
|
|
|
///
|
|
|
|
/// I2C objects are created attached to a specific bus. They can be initialised
|
|
|
|
/// when created, or initialised later on:
|
|
|
|
///
|
|
|
|
/// from pyb import I2C
|
|
|
|
///
|
|
|
|
/// i2c = I2C(1) # create on bus 1
|
2021-06-12 00:53:44 -04:00
|
|
|
/// i2c = I2C(1, I2C.CONTROLLER) # create and init as a controller
|
|
|
|
/// i2c.init(I2C.CONTROLLER, baudrate=20000) # init as a controller
|
|
|
|
/// i2c.init(I2C.PERIPHERAL, addr=0x42) # init as a peripheral with given address
|
|
|
|
/// i2c.deinit() # turn off the I2C unit
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// Printing the i2c object gives you information about its configuration.
|
|
|
|
///
|
2021-06-12 00:53:44 -04:00
|
|
|
/// Basic methods for peripheral are send and recv:
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// i2c.send('abc') # send 3 bytes
|
|
|
|
/// i2c.send(0x42) # send a single byte, given by the number
|
|
|
|
/// data = i2c.recv(3) # receive 3 bytes
|
|
|
|
///
|
|
|
|
/// To receive inplace, first create a bytearray:
|
|
|
|
///
|
|
|
|
/// data = bytearray(3) # create a buffer
|
|
|
|
/// i2c.recv(data) # receive 3 bytes, writing them into data
|
|
|
|
///
|
|
|
|
/// You can specify a timeout (in ms):
|
|
|
|
///
|
|
|
|
/// i2c.send(b'123', timeout=2000) # timout after 2 seconds
|
|
|
|
///
|
2021-06-12 00:53:44 -04:00
|
|
|
/// A controller must specify the recipient's address:
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
2021-06-12 00:53:44 -04:00
|
|
|
/// i2c.init(I2C.CONTROLLER)
|
|
|
|
/// i2c.send('123', 0x42) # send 3 bytes to peripheral with address 0x42
|
2014-04-29 17:55:34 -04:00
|
|
|
/// i2c.send(b'456', addr=0x42) # keyword for address
|
|
|
|
///
|
|
|
|
/// Master also has other methods:
|
|
|
|
///
|
2021-06-12 00:53:44 -04:00
|
|
|
/// i2c.is_ready(0x42) # check if peripheral 0x42 is ready
|
|
|
|
/// i2c.scan() # scan for peripherals on the bus, returning
|
2014-04-29 17:55:34 -04:00
|
|
|
/// # a list of valid addresses
|
2021-06-12 00:53:44 -04:00
|
|
|
/// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of peripheral 0x42,
|
|
|
|
/// # starting at address 2 in the peripheral
|
2014-04-29 17:55:34 -04:00
|
|
|
/// i2c.mem_write('abc', 0x42, 2, timeout=1000)
|
2014-04-20 19:10:04 -04:00
|
|
|
#define PYB_I2C_MASTER (0)
|
|
|
|
#define PYB_I2C_SLAVE (1)
|
|
|
|
|
2018-01-07 06:40:29 -05:00
|
|
|
#define PYB_I2C_SPEED_STANDARD (100000L)
|
|
|
|
#define PYB_I2C_SPEED_FULL (400000L)
|
|
|
|
#define PYB_I2C_SPEED_FAST (1000000L)
|
|
|
|
|
2015-04-18 11:12:57 -04:00
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
2014-04-19 19:30:09 -04:00
|
|
|
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
|
2014-04-20 14:06:15 -04:00
|
|
|
#endif
|
2015-04-18 11:12:57 -04:00
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
2014-04-19 19:30:09 -04:00
|
|
|
I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
I2C_HandleTypeDef I2CHandle3 = {.Instance = NULL};
|
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
I2C_HandleTypeDef I2CHandle4 = {.Instance = NULL};
|
|
|
|
#endif
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2016-11-24 19:21:18 -05:00
|
|
|
STATIC bool pyb_i2c_use_dma[4];
|
2016-11-11 01:36:19 -05:00
|
|
|
|
2016-11-25 00:30:51 -05:00
|
|
|
const pyb_i2c_obj_t pyb_i2c_obj[] = {
|
2015-06-10 08:53:00 -04:00
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
2016-11-11 01:36:19 -05:00
|
|
|
{{&pyb_i2c_type}, &I2CHandle1, &dma_I2C_1_TX, &dma_I2C_1_RX, &pyb_i2c_use_dma[0]},
|
2015-06-10 08:53:00 -04:00
|
|
|
#else
|
2016-11-11 01:36:19 -05:00
|
|
|
{{&pyb_i2c_type}, NULL, NULL, NULL, NULL},
|
2015-06-10 08:53:00 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
2016-11-11 01:36:19 -05:00
|
|
|
{{&pyb_i2c_type}, &I2CHandle2, &dma_I2C_2_TX, &dma_I2C_2_RX, &pyb_i2c_use_dma[1]},
|
2015-06-10 08:53:00 -04:00
|
|
|
#else
|
2016-11-11 01:36:19 -05:00
|
|
|
{{&pyb_i2c_type}, NULL, NULL, NULL, NULL},
|
2015-06-10 08:53:00 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
2016-11-11 01:36:19 -05:00
|
|
|
{{&pyb_i2c_type}, &I2CHandle3, &dma_I2C_3_TX, &dma_I2C_3_RX, &pyb_i2c_use_dma[2]},
|
2015-06-10 08:53:00 -04:00
|
|
|
#else
|
2016-11-11 01:36:19 -05:00
|
|
|
{{&pyb_i2c_type}, NULL, NULL, NULL, NULL},
|
2015-06-10 08:53:00 -04:00
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
{{&pyb_i2c_type}, &I2CHandle4, &dma_I2C_4_TX, &dma_I2C_4_RX, &pyb_i2c_use_dma[3]},
|
|
|
|
#else
|
|
|
|
{{&pyb_i2c_type}, NULL, NULL, NULL, NULL},
|
|
|
|
#endif
|
2015-06-10 08:53:00 -04:00
|
|
|
};
|
|
|
|
|
2021-01-26 08:49:56 -05:00
|
|
|
#if defined(STM32F7) || defined(STM32G4) || defined(STM32L4) || defined(STM32H7)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
2018-03-19 09:09:00 -04:00
|
|
|
// The STM32F0, F3, F7, H7 and L4 use a TIMINGR register rather than ClockSpeed and
|
2015-08-04 02:04:57 -04:00
|
|
|
// DutyCycle.
|
|
|
|
|
2019-05-20 23:45:17 -04:00
|
|
|
#define PYB_I2C_TIMINGR (1)
|
|
|
|
|
2022-03-22 01:01:09 -04:00
|
|
|
#if defined(STM32F745xx) || defined(STM32F746xx)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
|
|
|
// The value 0x40912732 was obtained from the DISCOVERY_I2Cx_TIMING constant
|
|
|
|
// defined in the STM32F7Cube file Drivers/BSP/STM32F746G-Discovery/stm32f7456g_discovery.h
|
2017-12-23 03:23:35 -05:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
|
2018-01-07 06:40:29 -05:00
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x40912732}, \
|
|
|
|
{PYB_I2C_SPEED_FULL, 0x10911823}, \
|
|
|
|
{PYB_I2C_SPEED_FAST, 0x00611116}, \
|
2020-02-26 23:36:53 -05:00
|
|
|
}
|
2018-01-07 06:40:29 -05:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_FULL)
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_FAST)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
2017-12-23 03:23:35 -05:00
|
|
|
#elif defined(STM32F722xx) || defined(STM32F723xx) \
|
|
|
|
|| defined(STM32F732xx) || defined(STM32F733xx) \
|
2018-09-05 02:38:25 -04:00
|
|
|
|| defined(STM32F765xx) || defined(STM32F767xx) \
|
|
|
|
|| defined(STM32F769xx)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
|
|
|
// These timing values are for f_I2CCLK=54MHz and are only approximate
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
|
2018-01-07 06:40:29 -05:00
|
|
|
{PYB_I2C_SPEED_STANDARD, 0xb0420f13}, \
|
|
|
|
{PYB_I2C_SPEED_FULL, 0x70330309}, \
|
|
|
|
{PYB_I2C_SPEED_FAST, 0x50100103}, \
|
2020-02-26 23:36:53 -05:00
|
|
|
}
|
2018-01-07 06:40:29 -05:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_FULL)
|
2018-03-19 09:09:00 -04:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_FAST)
|
|
|
|
|
2021-01-26 08:49:56 -05:00
|
|
|
#elif defined(STM32G4)
|
|
|
|
// timing input depends on PLL
|
|
|
|
// for now: 170MHz sysclock, PCLK 10.625 MHz
|
|
|
|
// using PCLOCK
|
|
|
|
// generated using CubeMX
|
|
|
|
#if defined(STM32G431xx) || defined(STM32G441xx)
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x30A0A7FB}, \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_STANDARD)
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_STANDARD)
|
|
|
|
|
2018-03-19 09:09:00 -04:00
|
|
|
#elif defined(STM32H7)
|
|
|
|
|
|
|
|
// I2C TIMINGs obtained from the STHAL examples.
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
|
|
|
|
{PYB_I2C_SPEED_STANDARD, 0x40604E73}, \
|
|
|
|
{PYB_I2C_SPEED_FULL, 0x00901954}, \
|
|
|
|
{PYB_I2C_SPEED_FAST, 0x10810915}, \
|
2020-02-26 23:36:53 -05:00
|
|
|
}
|
2018-03-19 09:09:00 -04:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_FULL)
|
2018-01-07 06:40:29 -05:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_FAST)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
2018-03-16 19:42:50 -04:00
|
|
|
#elif defined(STM32L4)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
|
|
|
// The value 0x90112626 was obtained from the DISCOVERY_I2C1_TIMING constant
|
|
|
|
// defined in the STM32L4Cube file Drivers/BSP/STM32L476G-Discovery/stm32l476g_discovery.h
|
2018-01-07 06:40:29 -05:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_TIMING {{PYB_I2C_SPEED_STANDARD, 0x90112626}}
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_STANDARD)
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_STANDARD)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
#error "no I2C timings for this MCU"
|
|
|
|
#endif
|
|
|
|
|
2015-08-04 02:04:57 -04:00
|
|
|
STATIC const struct {
|
2020-02-26 23:36:53 -05:00
|
|
|
uint32_t baudrate;
|
|
|
|
uint32_t timing;
|
2015-08-04 02:04:57 -04:00
|
|
|
} pyb_i2c_baudrate_timing[] = MICROPY_HW_I2C_BAUDRATE_TIMING;
|
|
|
|
|
|
|
|
#define NUM_BAUDRATE_TIMINGS MP_ARRAY_SIZE(pyb_i2c_baudrate_timing)
|
|
|
|
|
|
|
|
STATIC void i2c_set_baudrate(I2C_InitTypeDef *init, uint32_t baudrate) {
|
|
|
|
for (int i = 0; i < NUM_BAUDRATE_TIMINGS; i++) {
|
|
|
|
if (pyb_i2c_baudrate_timing[i].baudrate == baudrate) {
|
|
|
|
init->Timing = pyb_i2c_baudrate_timing[i].timing;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("unsupported I2C baudrate: %u"), baudrate);
|
2015-08-04 02:04:57 -04:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:32:16 -04:00
|
|
|
uint32_t pyb_i2c_get_baudrate(I2C_HandleTypeDef *i2c) {
|
2015-08-04 02:04:57 -04:00
|
|
|
for (int i = 0; i < NUM_BAUDRATE_TIMINGS; i++) {
|
2018-03-06 22:59:03 -05:00
|
|
|
if (pyb_i2c_baudrate_timing[i].timing == i2c->Init.Timing) {
|
2015-08-04 02:04:57 -04:00
|
|
|
return pyb_i2c_baudrate_timing[i].baudrate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-05-20 23:45:17 -04:00
|
|
|
#define PYB_I2C_TIMINGR (0)
|
|
|
|
|
2018-01-07 06:40:29 -05:00
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_FULL)
|
|
|
|
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_FULL)
|
2017-03-30 21:53:56 -04:00
|
|
|
|
2015-08-04 02:04:57 -04:00
|
|
|
STATIC void i2c_set_baudrate(I2C_InitTypeDef *init, uint32_t baudrate) {
|
|
|
|
init->ClockSpeed = baudrate;
|
|
|
|
init->DutyCycle = I2C_DUTYCYCLE_16_9;
|
|
|
|
}
|
|
|
|
|
2018-04-24 03:32:16 -04:00
|
|
|
uint32_t pyb_i2c_get_baudrate(I2C_HandleTypeDef *i2c) {
|
2018-03-06 22:59:03 -05:00
|
|
|
uint32_t pfreq = i2c->Instance->CR2 & 0x3f;
|
|
|
|
uint32_t ccr = i2c->Instance->CCR & 0xfff;
|
|
|
|
if (i2c->Instance->CCR & 0x8000) {
|
|
|
|
// Fast mode, assume duty cycle of 16/9
|
|
|
|
return pfreq * 40000 / ccr;
|
|
|
|
} else {
|
|
|
|
// Standard mode
|
|
|
|
return pfreq * 500000 / ccr;
|
|
|
|
}
|
2015-08-04 02:04:57 -04:00
|
|
|
}
|
|
|
|
|
2017-03-30 21:53:56 -04:00
|
|
|
#endif
|
2015-08-04 02:04:57 -04:00
|
|
|
|
2014-04-19 19:30:09 -04:00
|
|
|
void i2c_init0(void) {
|
2018-02-02 03:04:36 -05:00
|
|
|
// Initialise the I2C handles.
|
|
|
|
// The structs live on the BSS so all other fields will be zero after a reset.
|
2015-04-18 11:12:57 -04:00
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
2014-04-19 19:30:09 -04:00
|
|
|
I2CHandle1.Instance = I2C1;
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
2014-04-19 19:30:09 -04:00
|
|
|
I2CHandle2.Instance = I2C2;
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
I2CHandle3.Instance = I2C3;
|
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
2017-12-23 03:22:52 -05:00
|
|
|
I2CHandle4.Instance = I2C4;
|
2016-11-24 19:21:18 -05:00
|
|
|
#endif
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:32:16 -04:00
|
|
|
void pyb_i2c_init(I2C_HandleTypeDef *i2c) {
|
2016-02-11 00:20:14 -05:00
|
|
|
int i2c_unit;
|
|
|
|
const pin_obj_t *scl_pin;
|
|
|
|
const pin_obj_t *sda_pin;
|
2015-06-10 08:53:00 -04:00
|
|
|
|
2014-04-20 14:06:15 -04:00
|
|
|
if (0) {
|
2015-04-18 11:12:57 -04:00
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
2014-04-20 14:06:15 -04:00
|
|
|
} else if (i2c == &I2CHandle1) {
|
2016-02-11 00:20:14 -05:00
|
|
|
i2c_unit = 1;
|
2018-03-28 01:13:21 -04:00
|
|
|
scl_pin = MICROPY_HW_I2C1_SCL;
|
|
|
|
sda_pin = MICROPY_HW_I2C1_SDA;
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C1_CLK_ENABLE();
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
2014-04-20 14:06:15 -04:00
|
|
|
} else if (i2c == &I2CHandle2) {
|
2016-02-11 00:20:14 -05:00
|
|
|
i2c_unit = 2;
|
2018-03-28 01:13:21 -04:00
|
|
|
scl_pin = MICROPY_HW_I2C2_SCL;
|
|
|
|
sda_pin = MICROPY_HW_I2C2_SDA;
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C2_CLK_ENABLE();
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
} else if (i2c == &I2CHandle3) {
|
2016-02-11 00:20:14 -05:00
|
|
|
i2c_unit = 3;
|
2018-03-28 01:13:21 -04:00
|
|
|
scl_pin = MICROPY_HW_I2C3_SCL;
|
|
|
|
sda_pin = MICROPY_HW_I2C3_SDA;
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C3_CLK_ENABLE();
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
} else if (i2c == &I2CHandle4) {
|
|
|
|
i2c_unit = 4;
|
2018-03-28 01:13:21 -04:00
|
|
|
scl_pin = MICROPY_HW_I2C4_SCL;
|
|
|
|
sda_pin = MICROPY_HW_I2C4_SDA;
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C4_CLK_ENABLE();
|
2016-11-24 19:21:18 -05:00
|
|
|
#endif
|
2014-04-20 14:06:15 -04:00
|
|
|
} else {
|
|
|
|
// I2C does not exist for this board (shouldn't get here, should be checked by caller)
|
|
|
|
return;
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-19 19:30:09 -04:00
|
|
|
// init the GPIO lines
|
2016-11-11 01:53:45 -05:00
|
|
|
uint32_t mode = MP_HAL_PIN_MODE_ALT_OPEN_DRAIN;
|
|
|
|
uint32_t pull = MP_HAL_PIN_PULL_NONE; // have external pull-up resistors on both lines
|
|
|
|
mp_hal_pin_config_alt(scl_pin, mode, pull, AF_FN_I2C, i2c_unit);
|
|
|
|
mp_hal_pin_config_alt(sda_pin, mode, pull, AF_FN_I2C, i2c_unit);
|
2014-04-19 19:30:09 -04:00
|
|
|
|
2014-03-25 19:26:14 -04:00
|
|
|
// init the I2C device
|
2014-04-19 19:30:09 -04:00
|
|
|
if (HAL_I2C_Init(i2c) != HAL_OK) {
|
2014-03-25 19:26:14 -04:00
|
|
|
// init error
|
2014-04-20 14:06:15 -04:00
|
|
|
// TODO should raise an exception, but this function is not necessarily going to be
|
|
|
|
// called via Python, so may not be properly wrapped in an NLR handler
|
2014-10-23 09:25:32 -04:00
|
|
|
printf("OSError: HAL_I2C_Init failed\n");
|
2014-03-25 19:26:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-06-10 08:53:00 -04:00
|
|
|
|
|
|
|
// invalidate the DMA channels so they are initialised on first use
|
2016-02-11 00:20:14 -05:00
|
|
|
const pyb_i2c_obj_t *self = &pyb_i2c_obj[i2c_unit - 1];
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_invalidate_channel(self->tx_dma_descr);
|
|
|
|
dma_invalidate_channel(self->rx_dma_descr);
|
2017-07-18 23:12:10 -04:00
|
|
|
|
2016-09-07 07:00:17 -04:00
|
|
|
if (0) {
|
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
|
|
|
} else if (i2c->Instance == I2C1) {
|
|
|
|
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
|
|
|
|
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
|
|
|
} else if (i2c->Instance == I2C2) {
|
|
|
|
HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
|
|
|
|
HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
} else if (i2c->Instance == I2C3) {
|
|
|
|
HAL_NVIC_EnableIRQ(I2C3_EV_IRQn);
|
|
|
|
HAL_NVIC_EnableIRQ(I2C3_ER_IRQn);
|
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
} else if (i2c->Instance == I2C4) {
|
|
|
|
HAL_NVIC_EnableIRQ(I2C4_EV_IRQn);
|
|
|
|
HAL_NVIC_EnableIRQ(I2C4_ER_IRQn);
|
|
|
|
#endif
|
2016-09-07 07:00:17 -04:00
|
|
|
}
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
void i2c_deinit(I2C_HandleTypeDef *i2c) {
|
|
|
|
HAL_I2C_DeInit(i2c);
|
|
|
|
if (0) {
|
2015-04-18 11:12:57 -04:00
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
2014-04-20 19:10:04 -04:00
|
|
|
} else if (i2c->Instance == I2C1) {
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C1_FORCE_RESET();
|
|
|
|
__HAL_RCC_I2C1_RELEASE_RESET();
|
|
|
|
__HAL_RCC_I2C1_CLK_DISABLE();
|
2016-09-07 07:00:17 -04:00
|
|
|
HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
|
|
|
|
HAL_NVIC_DisableIRQ(I2C1_ER_IRQn);
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
2014-04-20 19:10:04 -04:00
|
|
|
} else if (i2c->Instance == I2C2) {
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C2_FORCE_RESET();
|
|
|
|
__HAL_RCC_I2C2_RELEASE_RESET();
|
|
|
|
__HAL_RCC_I2C2_CLK_DISABLE();
|
2016-09-07 07:00:17 -04:00
|
|
|
HAL_NVIC_DisableIRQ(I2C2_EV_IRQn);
|
|
|
|
HAL_NVIC_DisableIRQ(I2C2_ER_IRQn);
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
} else if (i2c->Instance == I2C3) {
|
2018-04-10 00:28:39 -04:00
|
|
|
__HAL_RCC_I2C3_FORCE_RESET();
|
|
|
|
__HAL_RCC_I2C3_RELEASE_RESET();
|
|
|
|
__HAL_RCC_I2C3_CLK_DISABLE();
|
2016-09-07 07:00:17 -04:00
|
|
|
HAL_NVIC_DisableIRQ(I2C3_EV_IRQn);
|
|
|
|
HAL_NVIC_DisableIRQ(I2C3_ER_IRQn);
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
} else if (i2c->Instance == I2C4) {
|
|
|
|
__HAL_RCC_I2C4_FORCE_RESET();
|
|
|
|
__HAL_RCC_I2C4_RELEASE_RESET();
|
|
|
|
__HAL_RCC_I2C4_CLK_DISABLE();
|
|
|
|
HAL_NVIC_DisableIRQ(I2C4_EV_IRQn);
|
|
|
|
HAL_NVIC_DisableIRQ(I2C4_ER_IRQn);
|
|
|
|
#endif
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 03:32:16 -04:00
|
|
|
void pyb_i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq) {
|
2016-11-25 00:30:51 -05:00
|
|
|
I2C_InitTypeDef *init = &self->i2c->Init;
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
init->AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
|
|
init->DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
|
|
|
init->GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
|
|
|
init->NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
|
|
init->OwnAddress1 = PYB_I2C_MASTER_ADDRESS;
|
|
|
|
init->OwnAddress2 = 0; // unused
|
2016-11-25 00:30:51 -05:00
|
|
|
if (freq != -1) {
|
|
|
|
i2c_set_baudrate(init, MIN(freq, MICROPY_HW_I2C_BAUDRATE_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
*self->use_dma = false;
|
|
|
|
|
|
|
|
// init the I2C bus
|
|
|
|
i2c_deinit(self->i2c);
|
2018-04-24 03:32:16 -04:00
|
|
|
pyb_i2c_init(self->i2c);
|
2016-11-25 00:30:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-11 01:38:52 -05:00
|
|
|
STATIC void i2c_reset_after_error(I2C_HandleTypeDef *i2c) {
|
|
|
|
// wait for bus-busy flag to be cleared, with a timeout
|
|
|
|
for (int timeout = 50; timeout > 0; --timeout) {
|
|
|
|
if (!__HAL_I2C_GET_FLAG(i2c, I2C_FLAG_BUSY)) {
|
|
|
|
// stop bit was generated and bus is back to normal
|
|
|
|
return;
|
|
|
|
}
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(1);
|
2016-11-11 01:38:52 -05:00
|
|
|
}
|
|
|
|
// bus was/is busy, need to reset the peripheral to get it to work again
|
|
|
|
i2c_deinit(i2c);
|
2018-04-24 03:32:16 -04:00
|
|
|
pyb_i2c_init(i2c);
|
2016-11-11 01:38:52 -05:00
|
|
|
}
|
|
|
|
|
2016-09-09 09:27:46 -04:00
|
|
|
void i2c_ev_irq_handler(mp_uint_t i2c_id) {
|
|
|
|
I2C_HandleTypeDef *hi2c;
|
|
|
|
|
|
|
|
switch (i2c_id) {
|
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
|
|
|
case 1:
|
|
|
|
hi2c = &I2CHandle1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
|
|
|
case 2:
|
|
|
|
hi2c = &I2CHandle2;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
case 3:
|
|
|
|
hi2c = &I2CHandle3;
|
|
|
|
break;
|
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
case 4:
|
|
|
|
hi2c = &I2CHandle4;
|
|
|
|
break;
|
|
|
|
#endif
|
2016-09-09 09:27:46 -04:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:42:50 -04:00
|
|
|
#if defined(STM32F4)
|
2016-11-21 00:12:09 -05:00
|
|
|
|
2016-09-09 09:27:46 -04:00
|
|
|
if (hi2c->Instance->SR1 & I2C_FLAG_BTF && hi2c->State == HAL_I2C_STATE_BUSY_TX) {
|
|
|
|
if (hi2c->XferCount != 0U) {
|
|
|
|
hi2c->Instance->DR = *hi2c->pBuffPtr++;
|
|
|
|
hi2c->XferCount--;
|
|
|
|
} else {
|
|
|
|
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
|
|
|
|
if (hi2c->XferOptions != I2C_FIRST_FRAME) {
|
|
|
|
hi2c->Instance->CR1 |= I2C_CR1_STOP;
|
|
|
|
}
|
|
|
|
hi2c->Mode = HAL_I2C_MODE_NONE;
|
|
|
|
hi2c->State = HAL_I2C_STATE_READY;
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 00:12:09 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// if not an F4 MCU, use the HAL's IRQ handler
|
|
|
|
HAL_I2C_EV_IRQHandler(hi2c);
|
|
|
|
|
|
|
|
#endif
|
2016-09-09 09:27:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_er_irq_handler(mp_uint_t i2c_id) {
|
|
|
|
I2C_HandleTypeDef *hi2c;
|
|
|
|
|
|
|
|
switch (i2c_id) {
|
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
|
|
|
case 1:
|
|
|
|
hi2c = &I2CHandle1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
|
|
|
case 2:
|
|
|
|
hi2c = &I2CHandle2;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
|
|
|
case 3:
|
|
|
|
hi2c = &I2CHandle3;
|
|
|
|
break;
|
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
|
|
|
case 4:
|
|
|
|
hi2c = &I2CHandle4;
|
|
|
|
break;
|
|
|
|
#endif
|
2016-09-09 09:27:46 -04:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:42:50 -04:00
|
|
|
#if defined(STM32F4)
|
2016-11-21 00:12:09 -05:00
|
|
|
|
2016-09-09 09:27:46 -04:00
|
|
|
uint32_t sr1 = hi2c->Instance->SR1;
|
|
|
|
|
|
|
|
// I2C Bus error
|
|
|
|
if (sr1 & I2C_FLAG_BERR) {
|
|
|
|
hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
|
|
|
|
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2C Arbitration Loss error
|
|
|
|
if (sr1 & I2C_FLAG_ARLO) {
|
|
|
|
hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
|
|
|
|
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2C Acknowledge failure
|
|
|
|
if (sr1 & I2C_FLAG_AF) {
|
|
|
|
hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
|
|
|
|
SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
|
|
|
|
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2C Over-Run/Under-Run
|
|
|
|
if (sr1 & I2C_FLAG_OVR) {
|
|
|
|
hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
|
|
|
|
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
|
|
|
|
}
|
2016-11-21 00:12:09 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// if not an F4 MCU, use the HAL's IRQ handler
|
|
|
|
HAL_I2C_ER_IRQHandler(hi2c);
|
|
|
|
|
|
|
|
#endif
|
2016-09-09 09:27:46 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 08:53:00 -04:00
|
|
|
STATIC HAL_StatusTypeDef i2c_wait_dma_finished(I2C_HandleTypeDef *i2c, uint32_t timeout) {
|
|
|
|
// Note: we can't use WFI to idle in this loop because the DMA completion
|
|
|
|
// interrupt may occur before the WFI. Hence we miss it and have to wait
|
|
|
|
// until the next sys-tick (up to 1ms).
|
|
|
|
uint32_t start = HAL_GetTick();
|
|
|
|
while (HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) {
|
|
|
|
if (HAL_GetTick() - start >= timeout) {
|
|
|
|
return HAL_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-25 19:26:14 -04:00
|
|
|
/******************************************************************************/
|
2017-06-30 03:22:17 -04:00
|
|
|
/* MicroPython bindings */
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
static inline bool in_master_mode(pyb_i2c_obj_t *self) {
|
|
|
|
return self->i2c->Init.OwnAddress1 == PYB_I2C_MASTER_ADDRESS;
|
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2015-04-18 11:12:57 -04:00
|
|
|
uint i2c_num = 0;
|
2020-02-26 23:36:53 -05:00
|
|
|
if (0) {
|
|
|
|
}
|
2015-04-18 11:12:57 -04:00
|
|
|
#if defined(MICROPY_HW_I2C1_SCL)
|
2020-02-26 23:36:53 -05:00
|
|
|
else if (self->i2c->Instance == I2C1) {
|
|
|
|
i2c_num = 1;
|
|
|
|
}
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C2_SCL)
|
2020-02-26 23:36:53 -05:00
|
|
|
else if (self->i2c->Instance == I2C2) {
|
|
|
|
i2c_num = 2;
|
|
|
|
}
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_I2C3_SCL)
|
2020-02-26 23:36:53 -05:00
|
|
|
else if (self->i2c->Instance == I2C3) {
|
|
|
|
i2c_num = 3;
|
|
|
|
}
|
2015-04-18 11:12:57 -04:00
|
|
|
#endif
|
2016-11-24 19:21:18 -05:00
|
|
|
#if defined(MICROPY_HW_I2C4_SCL)
|
2020-02-26 23:36:53 -05:00
|
|
|
else if (self->i2c->Instance == I2C4) {
|
|
|
|
i2c_num = 4;
|
|
|
|
}
|
2016-11-24 19:21:18 -05:00
|
|
|
#endif
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
if (self->i2c->State == HAL_I2C_STATE_RESET) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "I2C(%u)", i2c_num);
|
2014-04-20 19:10:04 -04:00
|
|
|
} else {
|
|
|
|
if (in_master_mode(self)) {
|
2021-06-12 00:53:44 -04:00
|
|
|
mp_printf(print, "I2C(%u, I2C.CONTROLLER, baudrate=%u"
|
2019-05-20 23:45:17 -04:00
|
|
|
#if PYB_I2C_TIMINGR
|
|
|
|
", timingr=0x%08x"
|
|
|
|
#endif
|
|
|
|
")", i2c_num, pyb_i2c_get_baudrate(self->i2c)
|
|
|
|
#if PYB_I2C_TIMINGR
|
|
|
|
, self->i2c->Init.Timing
|
|
|
|
#endif
|
2020-02-26 23:36:53 -05:00
|
|
|
);
|
2014-04-20 19:10:04 -04:00
|
|
|
} else {
|
2021-06-12 00:53:44 -04:00
|
|
|
mp_printf(print, "I2C(%u, I2C.PERIPHERAL, addr=0x%02x)", i2c_num, (self->i2c->Instance->OAR1 >> 1) & 0x7f);
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method init(mode, *, addr=0x12, baudrate=400000, gencall=False)
|
|
|
|
///
|
|
|
|
/// Initialise the I2C bus with the given parameters:
|
|
|
|
///
|
2021-06-12 00:53:44 -04:00
|
|
|
/// - `mode` must be either `I2C.CONTROLLER` or `I2C.PERIPHERAL`
|
|
|
|
/// - `addr` is the 7-bit address (only sensible for a peripheral)
|
|
|
|
/// - `baudrate` is the SCL clock rate (only sensible for a controller)
|
2014-04-29 17:55:34 -04:00
|
|
|
/// - `gencall` is whether to support general call mode
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_init_helper(const pyb_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-06-22 18:46:22 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
stmhal/modmachine: Initial attempt to add I2C & SPI classes.
In new hardware API, these classes implement master modes of interfaces,
and "mode" parameter is not accepted. Trying to implement new HW API
in terms of older pyb module leaves variuos corner cases:
In new HW API, I2C(1) means "I2C #1 in master mode" (? depends on
interpretation), while in old API, it means "I2C #1, with no settings
changes".
For I2C class, it's easy to make mode optional, because that's last
positional param, but for SPI, there's "baudrate" after it (which
is inconsistent with I2C, which requires "baudrate" to be kwonly-arg).
2015-11-14 09:13:58 -05:00
|
|
|
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYB_I2C_MASTER} },
|
2015-06-22 18:46:22 -04:00
|
|
|
{ MP_QSTR_addr, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x12} },
|
2015-08-04 02:04:57 -04:00
|
|
|
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_HW_I2C_BAUDRATE_DEFAULT} },
|
2015-06-22 18:46:22 -04:00
|
|
|
{ MP_QSTR_gencall, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2016-11-11 01:36:19 -05:00
|
|
|
{ MP_QSTR_dma, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2019-05-20 23:45:17 -04:00
|
|
|
#if PYB_I2C_TIMINGR
|
2019-12-27 06:46:43 -05:00
|
|
|
{ MP_QSTR_timingr, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2019-05-20 23:45:17 -04:00
|
|
|
#endif
|
2015-06-22 18:46:22 -04:00
|
|
|
};
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// parse args
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// set the I2C configuration values
|
|
|
|
I2C_InitTypeDef *init = &self->i2c->Init;
|
|
|
|
|
2015-06-22 18:46:22 -04:00
|
|
|
if (args[0].u_int == PYB_I2C_MASTER) {
|
2021-06-12 00:53:44 -04:00
|
|
|
// use a special address to indicate we are a controller
|
2014-04-20 19:10:04 -04:00
|
|
|
init->OwnAddress1 = PYB_I2C_MASTER_ADDRESS;
|
|
|
|
} else {
|
2015-06-22 18:46:22 -04:00
|
|
|
init->OwnAddress1 = (args[1].u_int << 1) & 0xfe;
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:45:17 -04:00
|
|
|
// Set baudrate or timing value (if supported)
|
|
|
|
#if PYB_I2C_TIMINGR
|
|
|
|
if (args[5].u_obj != mp_const_none) {
|
|
|
|
init->Timing = mp_obj_get_int_truncated(args[5].u_obj);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
i2c_set_baudrate(init, MIN(args[2].u_int, MICROPY_HW_I2C_BAUDRATE_MAX));
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
init->AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
2014-04-20 19:10:04 -04:00
|
|
|
init->DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
2015-06-22 18:46:22 -04:00
|
|
|
init->GeneralCallMode = args[3].u_bool ? I2C_GENERALCALL_ENABLED : I2C_GENERALCALL_DISABLED;
|
2020-02-26 23:36:53 -05:00
|
|
|
init->OwnAddress2 = 0; // unused
|
|
|
|
init->NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2016-11-11 01:36:19 -05:00
|
|
|
*self->use_dma = args[4].u_bool;
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// init the I2C bus
|
2016-11-11 01:38:52 -05:00
|
|
|
i2c_deinit(self->i2c);
|
2018-04-24 03:32:16 -04:00
|
|
|
pyb_i2c_init(self->i2c);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \classmethod \constructor(bus, ...)
|
|
|
|
///
|
|
|
|
/// Construct an I2C object on the given bus. `bus` can be 1 or 2.
|
|
|
|
/// With no additional parameters, the I2C object is created but not
|
|
|
|
/// initialised (it has the settings from the last initialisation of
|
|
|
|
/// the bus, if any). If extra arguments are given, the bus is initialised.
|
|
|
|
/// See `init` for parameters of initialisation.
|
2014-05-04 09:28:11 -04:00
|
|
|
///
|
2021-05-20 02:21:47 -04:00
|
|
|
/// The physical pins of the I2C buses are:
|
2014-05-04 09:28:11 -04:00
|
|
|
///
|
|
|
|
/// - `I2C(1)` is on the X position: `(SCL, SDA) = (X9, X10) = (PB6, PB7)`
|
|
|
|
/// - `I2C(2)` is on the Y position: `(SCL, SDA) = (Y9, Y10) = (PB10, PB11)`
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t pyb_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-03-25 19:26:14 -04:00
|
|
|
// check arguments
|
2014-04-20 19:10:04 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// get I2C object
|
2020-12-02 20:58:48 -05:00
|
|
|
int i2c_id = i2c_find_peripheral(args[0]);
|
2015-05-27 12:16:26 -04:00
|
|
|
const pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id - 1];
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
if (n_args > 1 || n_kw > 0) {
|
|
|
|
// start the peripheral
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
|
|
|
pyb_i2c_init_helper(i2c_obj, n_args - 1, args + 1, &kw_args);
|
|
|
|
}
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2018-07-08 09:25:11 -04:00
|
|
|
return MP_OBJ_FROM_PTR(i2c_obj);
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:32:16 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_init_(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2018-07-08 09:25:11 -04:00
|
|
|
return pyb_i2c_init_helper(MP_OBJ_TO_PTR(args[0]), n_args - 1, args + 1, kw_args);
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2018-04-24 03:32:16 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init_);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method deinit()
|
|
|
|
/// Turn off the I2C bus.
|
2014-04-20 19:10:04 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-20 19:10:04 -04:00
|
|
|
i2c_deinit(self->i2c);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_deinit_obj, pyb_i2c_deinit);
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method is_ready(addr)
|
2021-06-12 00:53:44 -04:00
|
|
|
/// Check if an I2C device responds to the given address. Only valid when in controller mode.
|
2014-03-25 19:26:14 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
if (!in_master_mode(self)) {
|
2021-06-12 00:53:44 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("I2C must be a controller"));
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o) << 1;
|
2014-03-25 19:26:14 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2014-04-19 19:30:09 -04:00
|
|
|
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, i2c_addr, 10, 200);
|
2014-03-25 19:26:14 -04:00
|
|
|
if (status == HAL_OK) {
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method scan()
|
2016-05-02 06:15:36 -04:00
|
|
|
/// Scan all I2C addresses from 0x08 to 0x77 and return a list of those that respond.
|
2021-06-12 00:53:44 -04:00
|
|
|
/// Only valid when in controller mode.
|
2014-04-15 14:24:13 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-15 14:24:13 -04:00
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
if (!in_master_mode(self)) {
|
2021-06-12 00:53:44 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("I2C must be a controller"));
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
|
|
|
|
2014-04-15 14:24:13 -04:00
|
|
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
|
|
|
|
2016-05-02 06:15:36 -04:00
|
|
|
for (uint addr = 0x08; addr <= 0x77; addr++) {
|
2017-09-07 21:19:40 -04:00
|
|
|
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 1, 200);
|
|
|
|
if (status == HAL_OK) {
|
|
|
|
mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));
|
2014-04-15 14:24:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method send(send, addr=0x00, timeout=5000)
|
|
|
|
/// Send data on the bus:
|
|
|
|
///
|
|
|
|
/// - `send` is the data to send (an integer to send, or a buffer object)
|
2021-06-12 00:53:44 -04:00
|
|
|
/// - `addr` is the address to send to (only required in controller mode)
|
2014-04-29 17:55:34 -04:00
|
|
|
/// - `timeout` is the timeout in milliseconds to wait for the send
|
|
|
|
///
|
|
|
|
/// Return value: `None`.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-06-22 18:46:22 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} },
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
|
|
|
};
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// parse args
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// get the buffer to send from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
2015-06-22 18:46:22 -04:00
|
|
|
pyb_buf_get_for_send(args[0].u_obj, &bufinfo, data);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2016-11-11 01:36:19 -05:00
|
|
|
// if option is set and IRQs are enabled then we can use DMA
|
|
|
|
bool use_dma = *self->use_dma && query_irq() == IRQ_STATE_ENABLED;
|
|
|
|
|
2015-06-10 08:53:00 -04:00
|
|
|
DMA_HandleTypeDef tx_dma;
|
2016-11-11 01:36:19 -05:00
|
|
|
if (use_dma) {
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&tx_dma, self->tx_dma_descr, DMA_MEMORY_TO_PERIPH, self->i2c);
|
2015-06-10 08:53:00 -04:00
|
|
|
self->i2c->hdmatx = &tx_dma;
|
|
|
|
self->i2c->hdmarx = NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// send the data
|
|
|
|
HAL_StatusTypeDef status;
|
|
|
|
if (in_master_mode(self)) {
|
2015-06-22 18:46:22 -04:00
|
|
|
if (args[1].u_int == PYB_I2C_MASTER_ADDRESS) {
|
2016-11-11 01:36:19 -05:00
|
|
|
if (use_dma) {
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(self->tx_dma_descr);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("addr argument required"));
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_uint_t i2c_addr = args[1].u_int << 1;
|
2016-11-11 01:36:19 -05:00
|
|
|
if (!use_dma) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = HAL_I2C_Master_Transmit(self->i2c, i2c_addr, bufinfo.buf, bufinfo.len, args[2].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
} else {
|
2017-03-30 21:56:18 -04:00
|
|
|
MP_HAL_CLEAN_DCACHE(bufinfo.buf, bufinfo.len);
|
2015-06-10 08:53:00 -04:00
|
|
|
status = HAL_I2C_Master_Transmit_DMA(self->i2c, i2c_addr, bufinfo.buf, bufinfo.len);
|
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
} else {
|
2016-11-11 01:36:19 -05:00
|
|
|
if (!use_dma) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = HAL_I2C_Slave_Transmit(self->i2c, bufinfo.buf, bufinfo.len, args[2].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
} else {
|
2017-03-30 21:56:18 -04:00
|
|
|
MP_HAL_CLEAN_DCACHE(bufinfo.buf, bufinfo.len);
|
2015-06-10 08:53:00 -04:00
|
|
|
status = HAL_I2C_Slave_Transmit_DMA(self->i2c, bufinfo.buf, bufinfo.len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we used DMA, wait for it to finish
|
2016-11-11 01:36:19 -05:00
|
|
|
if (use_dma) {
|
2015-06-10 08:53:00 -04:00
|
|
|
if (status == HAL_OK) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = i2c_wait_dma_finished(self->i2c, args[2].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(self->tx_dma_descr);
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2014-04-15 19:27:14 -04:00
|
|
|
|
|
|
|
if (status != HAL_OK) {
|
2016-11-11 01:38:52 -05:00
|
|
|
i2c_reset_after_error(self->i2c);
|
2014-10-23 09:25:32 -04:00
|
|
|
mp_hal_raise(status);
|
2014-04-15 19:27:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
return mp_const_none;
|
2014-04-15 19:27:14 -04:00
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send);
|
|
|
|
|
2014-05-03 11:42:27 -04:00
|
|
|
/// \method recv(recv, addr=0x00, timeout=5000)
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// Receive data on the bus:
|
|
|
|
///
|
|
|
|
/// - `recv` can be an integer, which is the number of bytes to receive,
|
|
|
|
/// or a mutable buffer, which will be filled with received bytes
|
2021-06-12 00:53:44 -04:00
|
|
|
/// - `addr` is the address to receive from (only required in controller mode)
|
2014-04-29 17:55:34 -04:00
|
|
|
/// - `timeout` is the timeout in milliseconds to wait for the receive
|
|
|
|
///
|
|
|
|
/// Return value: if `recv` is an integer then a new buffer of the bytes received,
|
|
|
|
/// otherwise the same buffer that was passed in to `recv`.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-06-22 18:46:22 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} },
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
|
|
|
};
|
2014-04-15 19:27:14 -04:00
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// parse args
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// get the buffer to receive into
|
2015-01-21 17:48:37 -05:00
|
|
|
vstr_t vstr;
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_obj_t o_ret = pyb_buf_get_for_recv(args[0].u_obj, &vstr);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2016-11-11 01:36:19 -05:00
|
|
|
// if option is set and IRQs are enabled then we can use DMA
|
|
|
|
bool use_dma = *self->use_dma && query_irq() == IRQ_STATE_ENABLED;
|
|
|
|
|
2015-06-10 08:53:00 -04:00
|
|
|
DMA_HandleTypeDef rx_dma;
|
2016-11-11 01:36:19 -05:00
|
|
|
if (use_dma) {
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&rx_dma, self->rx_dma_descr, DMA_PERIPH_TO_MEMORY, self->i2c);
|
2015-06-10 08:53:00 -04:00
|
|
|
self->i2c->hdmatx = NULL;
|
|
|
|
self->i2c->hdmarx = &rx_dma;
|
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// receive the data
|
2014-04-15 19:27:14 -04:00
|
|
|
HAL_StatusTypeDef status;
|
2014-04-20 19:10:04 -04:00
|
|
|
if (in_master_mode(self)) {
|
2015-06-22 18:46:22 -04:00
|
|
|
if (args[1].u_int == PYB_I2C_MASTER_ADDRESS) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("addr argument required"));
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_uint_t i2c_addr = args[1].u_int << 1;
|
2016-11-11 01:36:19 -05:00
|
|
|
if (!use_dma) {
|
2020-02-26 23:36:53 -05:00
|
|
|
status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, (uint8_t *)vstr.buf, vstr.len, args[2].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
} else {
|
2017-03-30 21:56:18 -04:00
|
|
|
MP_HAL_CLEANINVALIDATE_DCACHE(vstr.buf, vstr.len);
|
2020-02-26 23:36:53 -05:00
|
|
|
status = HAL_I2C_Master_Receive_DMA(self->i2c, i2c_addr, (uint8_t *)vstr.buf, vstr.len);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2014-04-15 19:27:14 -04:00
|
|
|
} else {
|
2016-11-11 01:36:19 -05:00
|
|
|
if (!use_dma) {
|
2020-02-26 23:36:53 -05:00
|
|
|
status = HAL_I2C_Slave_Receive(self->i2c, (uint8_t *)vstr.buf, vstr.len, args[2].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
} else {
|
2017-03-30 21:56:18 -04:00
|
|
|
MP_HAL_CLEANINVALIDATE_DCACHE(vstr.buf, vstr.len);
|
2020-02-26 23:36:53 -05:00
|
|
|
status = HAL_I2C_Slave_Receive_DMA(self->i2c, (uint8_t *)vstr.buf, vstr.len);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we used DMA, wait for it to finish
|
2016-11-11 01:36:19 -05:00
|
|
|
if (use_dma) {
|
2015-06-10 08:53:00 -04:00
|
|
|
if (status == HAL_OK) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = i2c_wait_dma_finished(self->i2c, args[2].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(self->rx_dma_descr);
|
2014-04-15 19:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status != HAL_OK) {
|
2016-11-11 01:38:52 -05:00
|
|
|
i2c_reset_after_error(self->i2c);
|
2014-10-23 09:25:32 -04:00
|
|
|
mp_hal_raise(status);
|
2014-04-15 19:27:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// return the received data
|
2015-01-21 17:48:37 -05:00
|
|
|
if (o_ret != MP_OBJ_NULL) {
|
|
|
|
return o_ret;
|
2014-04-20 19:10:04 -04:00
|
|
|
} else {
|
2015-01-21 17:48:37 -05:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2014-04-15 19:27:14 -04:00
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
|
2014-04-15 19:27:14 -04:00
|
|
|
|
2014-07-21 22:45:04 -04:00
|
|
|
/// \method mem_read(data, addr, memaddr, timeout=5000, addr_size=8)
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// Read from the memory of an I2C device:
|
|
|
|
///
|
|
|
|
/// - `data` can be an integer or a buffer to read into
|
|
|
|
/// - `addr` is the I2C device address
|
|
|
|
/// - `memaddr` is the memory location within the I2C device
|
|
|
|
/// - `timeout` is the timeout in milliseconds to wait for the read
|
2014-07-21 22:45:04 -04:00
|
|
|
/// - `addr_size` selects width of memaddr: 8 or 16 bits
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// Returns the read data.
|
2021-06-12 00:53:44 -04:00
|
|
|
/// This is only valid in controller mode.
|
2015-06-22 18:46:22 -04:00
|
|
|
STATIC const mp_arg_t pyb_i2c_mem_read_allowed_args[] = {
|
2014-04-26 06:19:17 -04:00
|
|
|
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
|
2014-07-21 22:45:04 -04:00
|
|
|
{ MP_QSTR_addr_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
2014-04-20 19:10:04 -04:00
|
|
|
};
|
2014-04-15 19:27:14 -04:00
|
|
|
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_mem_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-06-22 18:46:22 -04:00
|
|
|
// parse args
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_mem_read_allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(pyb_i2c_mem_read_allowed_args), pyb_i2c_mem_read_allowed_args, args);
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
if (!in_master_mode(self)) {
|
2021-06-12 00:53:44 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("I2C must be a controller"));
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the buffer to read into
|
2015-01-21 17:48:37 -05:00
|
|
|
vstr_t vstr;
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_obj_t o_ret = pyb_buf_get_for_recv(args[0].u_obj, &vstr);
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// get the addresses
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_uint_t i2c_addr = args[1].u_int << 1;
|
|
|
|
mp_uint_t mem_addr = args[2].u_int;
|
2014-07-21 22:45:04 -04:00
|
|
|
// determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
|
2014-07-11 16:14:01 -04:00
|
|
|
mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
|
2015-06-22 18:46:22 -04:00
|
|
|
if (args[4].u_int != 8) {
|
2014-07-11 21:18:09 -04:00
|
|
|
mem_addr_size = I2C_MEMADD_SIZE_16BIT;
|
2014-07-11 16:14:01 -04:00
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2016-11-11 01:36:19 -05:00
|
|
|
// if option is set and IRQs are enabled then we can use DMA
|
|
|
|
bool use_dma = *self->use_dma && query_irq() == IRQ_STATE_ENABLED;
|
|
|
|
|
2015-06-10 08:53:00 -04:00
|
|
|
HAL_StatusTypeDef status;
|
2016-11-11 01:36:19 -05:00
|
|
|
if (!use_dma) {
|
2020-02-26 23:36:53 -05:00
|
|
|
status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, (uint8_t *)vstr.buf, vstr.len, args[3].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
} else {
|
|
|
|
DMA_HandleTypeDef rx_dma;
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&rx_dma, self->rx_dma_descr, DMA_PERIPH_TO_MEMORY, self->i2c);
|
2015-06-10 08:53:00 -04:00
|
|
|
self->i2c->hdmatx = NULL;
|
|
|
|
self->i2c->hdmarx = &rx_dma;
|
2017-03-30 21:56:18 -04:00
|
|
|
MP_HAL_CLEANINVALIDATE_DCACHE(vstr.buf, vstr.len);
|
2020-02-26 23:36:53 -05:00
|
|
|
status = HAL_I2C_Mem_Read_DMA(self->i2c, i2c_addr, mem_addr, mem_addr_size, (uint8_t *)vstr.buf, vstr.len);
|
2015-06-10 08:53:00 -04:00
|
|
|
if (status == HAL_OK) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = i2c_wait_dma_finished(self->i2c, args[3].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(self->rx_dma_descr);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2014-03-25 19:26:14 -04:00
|
|
|
|
|
|
|
if (status != HAL_OK) {
|
2016-11-11 01:38:52 -05:00
|
|
|
i2c_reset_after_error(self->i2c);
|
2014-10-23 09:25:32 -04:00
|
|
|
mp_hal_raise(status);
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// return the read data
|
2015-01-21 17:48:37 -05:00
|
|
|
if (o_ret != MP_OBJ_NULL) {
|
|
|
|
return o_ret;
|
2014-04-20 19:10:04 -04:00
|
|
|
} else {
|
2015-01-21 17:48:37 -05:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2014-07-21 22:45:04 -04:00
|
|
|
/// \method mem_write(data, addr, memaddr, timeout=5000, addr_size=8)
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// Write to the memory of an I2C device:
|
|
|
|
///
|
|
|
|
/// - `data` can be an integer or a buffer to write from
|
|
|
|
/// - `addr` is the I2C device address
|
|
|
|
/// - `memaddr` is the memory location within the I2C device
|
|
|
|
/// - `timeout` is the timeout in milliseconds to wait for the write
|
2014-07-21 22:45:04 -04:00
|
|
|
/// - `addr_size` selects width of memaddr: 8 or 16 bits
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
|
|
|
/// Returns `None`.
|
2021-06-12 00:53:44 -04:00
|
|
|
/// This is only valid in controller mode.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t pyb_i2c_mem_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-06-22 18:46:22 -04:00
|
|
|
// parse args (same as mem_read)
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_mem_read_allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(pyb_i2c_mem_read_allowed_args), pyb_i2c_mem_read_allowed_args, args);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
if (!in_master_mode(self)) {
|
2021-06-12 00:53:44 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("I2C must be a controller"));
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
2014-04-20 19:10:04 -04:00
|
|
|
// get the buffer to write from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
2015-06-22 18:46:22 -04:00
|
|
|
pyb_buf_get_for_send(args[0].u_obj, &bufinfo, data);
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// get the addresses
|
2015-06-22 18:46:22 -04:00
|
|
|
mp_uint_t i2c_addr = args[1].u_int << 1;
|
|
|
|
mp_uint_t mem_addr = args[2].u_int;
|
2014-07-21 22:45:04 -04:00
|
|
|
// determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
|
2014-07-11 16:14:01 -04:00
|
|
|
mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
|
2015-06-22 18:46:22 -04:00
|
|
|
if (args[4].u_int != 8) {
|
2014-07-11 21:18:09 -04:00
|
|
|
mem_addr_size = I2C_MEMADD_SIZE_16BIT;
|
2014-07-11 16:14:01 -04:00
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
|
2016-11-11 01:36:19 -05:00
|
|
|
// if option is set and IRQs are enabled then we can use DMA
|
|
|
|
bool use_dma = *self->use_dma && query_irq() == IRQ_STATE_ENABLED;
|
|
|
|
|
2015-06-10 08:53:00 -04:00
|
|
|
HAL_StatusTypeDef status;
|
2016-11-11 01:36:19 -05:00
|
|
|
if (!use_dma) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, args[3].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
} else {
|
|
|
|
DMA_HandleTypeDef tx_dma;
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&tx_dma, self->tx_dma_descr, DMA_MEMORY_TO_PERIPH, self->i2c);
|
2015-06-10 08:53:00 -04:00
|
|
|
self->i2c->hdmatx = &tx_dma;
|
|
|
|
self->i2c->hdmarx = NULL;
|
2017-03-30 21:56:18 -04:00
|
|
|
MP_HAL_CLEAN_DCACHE(bufinfo.buf, bufinfo.len);
|
2015-06-10 08:53:00 -04:00
|
|
|
status = HAL_I2C_Mem_Write_DMA(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len);
|
|
|
|
if (status == HAL_OK) {
|
2015-06-22 18:46:22 -04:00
|
|
|
status = i2c_wait_dma_finished(self->i2c, args[3].u_int);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(self->tx_dma_descr);
|
2015-06-10 08:53:00 -04:00
|
|
|
}
|
2014-03-25 19:26:14 -04:00
|
|
|
|
|
|
|
if (status != HAL_OK) {
|
2016-11-11 01:38:52 -05:00
|
|
|
i2c_reset_after_error(self->i2c);
|
2014-10-23 09:25:32 -04:00
|
|
|
mp_hal_raise(status);
|
2014-03-25 19:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-20 19:10:04 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_write_obj, 1, pyb_i2c_mem_write);
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_i2c_locals_dict_table[] = {
|
2014-04-20 19:10:04 -04:00
|
|
|
// instance methods
|
2017-05-06 03:03:40 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_i2c_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_i2c_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_is_ready), MP_ROM_PTR(&pyb_i2c_is_ready_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&pyb_i2c_scan_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&pyb_i2c_send_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&pyb_i2c_recv_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mem_read), MP_ROM_PTR(&pyb_i2c_mem_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mem_write), MP_ROM_PTR(&pyb_i2c_mem_write_obj) },
|
2014-04-20 19:10:04 -04:00
|
|
|
|
|
|
|
// class constants
|
2021-06-12 00:53:44 -04:00
|
|
|
/// \constant CONTROLLER - for initialising the bus to controller mode
|
|
|
|
/// \constant PERIPHERAL - for initialising the bus to peripheral mode
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_CONTROLLER), MP_ROM_INT(PYB_I2C_MASTER) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PERIPHERAL), MP_ROM_INT(PYB_I2C_SLAVE) },
|
|
|
|
// TODO - remove MASTER/SLAVE when CONTROLLER/PERIPHERAL gain wide adoption
|
2017-05-06 03:03:40 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_MASTER), MP_ROM_INT(PYB_I2C_MASTER) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SLAVE), MP_ROM_INT(PYB_I2C_SLAVE) },
|
2014-03-25 19:26:14 -04:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);
|
|
|
|
|
2014-03-25 19:26:14 -04:00
|
|
|
const mp_obj_type_t pyb_i2c_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_I2C,
|
2014-04-20 19:10:04 -04:00
|
|
|
.print = pyb_i2c_print,
|
2014-03-25 19:26:14 -04:00
|
|
|
.make_new = pyb_i2c_make_new,
|
2020-02-26 23:36:53 -05:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&pyb_i2c_locals_dict,
|
2014-03-25 19:26:14 -04:00
|
|
|
};
|
2017-12-21 23:20:42 -05:00
|
|
|
|
2018-05-13 23:53:46 -04:00
|
|
|
#endif // MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C
|