2016年8月13日 星期六

7688 i2c & arduino slaver code



Ref : https://docs.labs.mediatek.com/resource/linkit-smart-7688/en/tutorials/peripheral-support-on-linkit-smart-7688-development-board/mraa






















P20  ->   I2C  SDA

P21   ->  I2C  SCK



使用MRAA為library..... 系統預設已經裝好...

MRAA

libmraa is a C/C++ library to interface with the peripherals on LinkIt Smart 7688 development board. libmraa is pre-installed in the system image of the board and supports C++, Python and Node.js bindings.

Basic concepts

The majority of hardware modules such as GPIO, UART, SPI and PWM are represented as objects. These modules are initialized on certain pins that are identified by pin numbers. The pin numbers in the libmraa on the board are identical to the GPIO numbers in the data sheet and in the Linux GPIO subsystem.
The following Python example creates GPIO object on GPIO pin 2: 
import mraa
pin = mraa.Gpio(2)    # Initialize GPIO2 (P10 on LinkIt Smart 7688 board)

I2C

Inter-Integrated Circuit (I2C) is a widely used protocol among peripherals. It consists of 2 signal pins — usually named SDA and SCL. LinkIt Smart 7688 development board has one set of I2C on GPIO4 (P21) and GPIO5 (P20) as SCL and SDA, respectively. The way I2Cs are initialized is slightly different from GPIO modules. Instead of using pins, I2Cs are initialized based on the device index. Since there is only one set of I2C master device on the board, you can simply pass 0, and it's always on pins GPIO4 and GPIO5.
import mraa 
i2c = mraa.I2c(0) 
I2C is capable of connecting multiple slave devices to a single I2C master. Each slave device is identified by a 7-bit address. The following example scans for a Seeed Studio 3-Axis Digital Accelerometer attached to the LinkIt Smart 7688 development board by reading the device ID from its registers.
import mraa
 
i2c = mraa.I2c(0)
# Grove - 3-Axis Digital Accelerometer(+-16g)
# is a ADXL345 configured to I2C address 0x53.
i2c.address(0x53)
# The device ID should be 
if 0xE5 == i2c.readReg(0x00):
 print "Grove - 3-Axis Digital Accelerometer found on I2C Bus"
else:
 print "Grove - 3-Axis Digital Accelerometer not found" 


mraa 在 i2c 下面用的 api 

address(I2c selfuint8_t address) → mraa::Result[source]
address: uint8_t
frequency(I2c selfmraa::I2cMode mode) → mraa::Result[source]
mode: enum mraa::I2cMode
read(I2c selfuint8_t * data) → int[source]
data: uint8_t *
readByte(I2c self) → uint8_t[source]
self: mraa::I2c *
readBytesReg(I2c selfuint8_t reguint8_t * data) → int[source]
reg: uint8_t data: uint8_t *
readReg(I2c selfuint8_t reg) → uint8_t[source]
reg: uint8_t
readWordReg(I2c selfuint8_t reg) → uint16_t[source]
reg: uint8_t
write(I2c selfuint8_t const * data) → mraa::Result[source]
data: uint8_t const *
writeByte(I2c selfuint8_t data) → mraa::Result[source]
data: uint8_t
writeReg(I2c selfuint8_t reguint8_t data) → mraa::Result[source]
reg: uint8_t data: uint8_t
writeWordReg(I2c selfuint8_t reguint16_t data) → mraa::Result[source]
reg: uint8_t data: uint16_t


======================================================
Ref  wire ->  slaver_reader sample

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

=============================================
slaver_sender


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}













沒有留言:

張貼留言