Select Git revision
quickref.rst
modpybi2c.c 10.20 KiB
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 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.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "ets_sys.h"
#include "etshal.h"
#include "osapi.h"
#include "gpio.h"
#include "py/runtime.h"
#include "modpyb.h"
typedef struct _pyb_i2c_obj_t {
mp_obj_base_t base;
pyb_pin_obj_t *scl;
pyb_pin_obj_t *sda;
} pyb_i2c_obj_t;
// these set the frequency of SCL
#define mphal_i2c_wait_a() os_delay_us(2)
#define mphal_i2c_wait_b() os_delay_us(1)
STATIC void mphal_i2c_set_sda(pyb_i2c_obj_t *self, uint8_t sda) {
uint32_t port = self->sda->phys_port;
sda &= 0x01;
gpio_output_set(sda << port, (1 - sda) << port, 1 << port, 0);
}
STATIC void mphal_i2c_set_scl(pyb_i2c_obj_t *self, uint8_t scl) {
uint32_t port = self->scl->phys_port;
scl &= 0x01;
gpio_output_set(scl << port, (1 - scl) << port, 1 << port, 0);
}
STATIC int mphal_i2c_get_sda(pyb_i2c_obj_t *self) {
return GPIO_INPUT_GET(GPIO_ID_PIN(self->sda->phys_port));
}
STATIC void mphal_i2c_start(pyb_i2c_obj_t *self) {
mphal_i2c_set_sda(self, 1);
mphal_i2c_wait_a();
mphal_i2c_set_scl(self, 1);
mphal_i2c_wait_a();
mphal_i2c_set_sda(self, 0);