1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

Importing my changes

This commit is contained in:
ficeto
2015-04-30 20:48:50 +03:00
parent cf9da93b61
commit fbec557ddb
32 changed files with 1423 additions and 1220 deletions

View File

@ -49,6 +49,7 @@ void ESP8266WiFiClass::mode(WiFiMode m)
ETS_UART_INTR_ENABLE();
}
int ESP8266WiFiClass::begin(const char* ssid)
{
return begin(ssid, 0);
@ -57,7 +58,7 @@ int ESP8266WiFiClass::begin(const char* ssid)
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase)
{
if (wifi_get_opmode() == WIFI_AP)
if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled
{
// turn on AP+STA mode
mode(WIFI_AP_STA);
@ -111,7 +112,7 @@ void ESP8266WiFiClass::softAP(const char* ssid)
void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel)
{
if (wifi_get_opmode() == WIFI_STA)
if (wifi_get_opmode() < WIFI_AP)//will be OFF or STA
{
// turn on AP+STA mode
mode(WIFI_AP_STA);
@ -260,7 +261,7 @@ void ESP8266WiFiClass::_scanDone(void* result, int status)
int8_t ESP8266WiFiClass::scanNetworks()
{
if (wifi_get_opmode() == WIFI_AP)
if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled
{
mode(WIFI_AP_STA);
}
@ -381,7 +382,10 @@ void ESP8266WiFiClass::beginSmartConfig()
if (_smartConfigStarted)
return;
WiFi.mode(WIFI_STA);
if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled
{
mode(WIFI_AP_STA);
}
_smartConfigStarted = true;

View File

@ -32,7 +32,7 @@ extern "C" {
#include "WiFiClient.h"
#include "WiFiServer.h"
enum WiFiMode { WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 };
enum WiFiMode { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 };
class ESP8266WiFiClass
{
@ -41,6 +41,7 @@ public:
ESP8266WiFiClass();
void mode(WiFiMode);
/* Start Wifi connection for OPEN networks
*

View File

@ -120,13 +120,13 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);
// send CRC
uint8_t crc = 0XFF;
if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0
if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA
uint8_t crc = 0xFF;
if (cmd == CMD0) crc = 0x95; // correct crc for CMD0 with arg 0
if (cmd == CMD8) crc = 0x87; // correct crc for CMD8 with arg 0X1AA
spiSend(crc);
// wait for response
for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++)
for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++)
;
return status_;
}
@ -239,7 +239,11 @@ uint8_t Sd2Card::eraseSingleBlockEnable(void) {
* the value zero, false, is returned for failure. The reason for failure
* can be determined by calling errorCode() and errorData().
*/
#ifdef ESP8266
uint8_t Sd2Card::init(uint32_t sckRateID, uint8_t chipSelectPin) {
#else
uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
#endif
errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0;
chipSelectPin_ = chipSelectPin;
// 16-bit init start time allows over a minute
@ -266,7 +270,11 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
SPSR &= ~(1 << SPI2X);
#else // USE_SPI_LIB
SPI.begin();
#ifdef ESP8266
settings = SPISettings(SPI_CLOCK_DIV64, MSBFIRST, SPI_MODE0);
#else
settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
#endif
#endif // USE_SPI_LIB
#endif // SOFTWARE_SPI
@ -498,11 +506,15 @@ uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
* \return The value one, true, is returned for success and the value zero,
* false, is returned for an invalid value of \a sckRateID.
*/
#ifdef ESP8266
uint8_t Sd2Card::setSckRate(uint32_t sckRateID) {
#else
uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
if (sckRateID > 6) {
error(SD_CARD_ERROR_SCK_RATE);
return false;
}
#endif
#ifndef USE_SPI_LIB
// see avr processor datasheet for SPI register bit definitions
if ((sckRateID & 1) || sckRateID == 6) {
@ -514,6 +526,9 @@ uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
| (sckRateID & 2 ? (1 << SPR0) : 0);
#else // USE_SPI_LIB
#ifdef ESP8266
settings = SPISettings(sckRateID, MSBFIRST, SPI_MODE0);
#else
switch (sckRateID) {
case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
@ -523,6 +538,7 @@ uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break;
default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0);
}
#endif
#endif // USE_SPI_LIB
return true;
}

View File

@ -25,15 +25,23 @@
*/
#include "Sd2PinMap.h"
#include "SdInfo.h"
#ifdef ESP8266
#include "SPI.h"
uint32_t const SPI_FULL_SPEED = SPI_CLOCK_DIV2;
uint32_t const SPI_HALF_SPEED = SPI_CLOCK_DIV4;
uint32_t const SPI_QUARTER_SPEED = SPI_CLOCK_DIV8;
#else
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
uint8_t const SPI_FULL_SPEED = 0;
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 1;
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 2;
#endif
/**
* USE_SPI_LIB: if set, use the SPI library bundled with Arduino IDE, otherwise
* run with a standalone driver for AVR.
* run with a standalone driver for AVR.
*/
#define USE_SPI_LIB
/**
@ -181,10 +189,17 @@ class Sd2Card {
* and the default SD chip select pin.
* See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
*/
#ifdef ESP8266
uint8_t init(uint32_t sckRateID) {
return init(sckRateID, SD_CHIP_SELECT_PIN);
}
uint8_t init(uint32_t sckRateID, uint8_t chipSelectPin);
#else
uint8_t init(uint8_t sckRateID) {
return init(sckRateID, SD_CHIP_SELECT_PIN);
}
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
#endif
void partialBlockRead(uint8_t value);
/** Returns the current value, true or false, for partial block read. */
uint8_t partialBlockRead(void) const {return partialBlockRead_;}
@ -205,7 +220,11 @@ class Sd2Card {
return readRegister(CMD9, csd);
}
void readEnd(void);
#ifdef ESP8266
uint8_t setSckRate(uint32_t sckRateID);
#else
uint8_t setSckRate(uint8_t sckRateID);
#endif
/** Return the card type: SD V1, SD V2 or SDHC */
uint8_t type(void) const {return type_;}
uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);

View File

@ -31,6 +31,19 @@ uint8_t const SCK_PIN = SCK;
#endif // Sd2PinMap_h
#elif defined(ESP8266) // Other AVR based Boards follows
#ifndef Sd2PinMap_h
#define Sd2PinMap_h
#include <Arduino.h>
uint8_t const SS_PIN = SS;
uint8_t const MOSI_PIN = MOSI;
uint8_t const MISO_PIN = MISO;
uint8_t const SCK_PIN = SCK;
#endif // Sd2PinMap_h
#elif defined(__AVR__) // Other AVR based Boards follows
// Warning this file was generated by a program.

View File

@ -22,6 +22,7 @@
#include <avr/pgmspace.h>
#endif
#include <Arduino.h>
#define PRINT_PORT Serial
//------------------------------------------------------------------------------
// callback function for date/time
void (*SdFile::dateTime_)(uint16_t* date, uint16_t* time) = NULL;
@ -215,7 +216,7 @@ void SdFile::ls(uint8_t flags, uint8_t indent) {
if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;
// print any indent spaces
for (int8_t i = 0; i < indent; i++) Serial.print(' ');
for (int8_t i = 0; i < indent; i++) PRINT_PORT.print(' ');
// print file name with possible blank fill
printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0);
@ -223,15 +224,15 @@ void SdFile::ls(uint8_t flags, uint8_t indent) {
// print modify date/time if requested
if (flags & LS_DATE) {
printFatDate(p->lastWriteDate);
Serial.print(' ');
PRINT_PORT.print(' ');
printFatTime(p->lastWriteTime);
}
// print size if requested
if (!DIR_IS_SUBDIR(p) && (flags & LS_SIZE)) {
Serial.print(' ');
Serial.print(p->fileSize);
PRINT_PORT.print(' ');
PRINT_PORT.print(p->fileSize);
}
Serial.println();
PRINT_PORT.println();
// list subdirectory content if requested
if ((flags & LS_R) && DIR_IS_SUBDIR(p)) {
@ -595,18 +596,18 @@ void SdFile::printDirName(const dir_t& dir, uint8_t width) {
for (uint8_t i = 0; i < 11; i++) {
if (dir.name[i] == ' ')continue;
if (i == 8) {
Serial.print('.');
PRINT_PORT.print('.');
w++;
}
Serial.write(dir.name[i]);
PRINT_PORT.write(dir.name[i]);
w++;
}
if (DIR_IS_SUBDIR(&dir)) {
Serial.print('/');
PRINT_PORT.print('/');
w++;
}
while (w < width) {
Serial.print(' ');
PRINT_PORT.print(' ');
w++;
}
}
@ -618,10 +619,10 @@ void SdFile::printDirName(const dir_t& dir, uint8_t width) {
* \param[in] fatDate The date field from a directory entry.
*/
void SdFile::printFatDate(uint16_t fatDate) {
Serial.print(FAT_YEAR(fatDate));
Serial.print('-');
PRINT_PORT.print(FAT_YEAR(fatDate));
PRINT_PORT.print('-');
printTwoDigits(FAT_MONTH(fatDate));
Serial.print('-');
PRINT_PORT.print('-');
printTwoDigits(FAT_DAY(fatDate));
}
//------------------------------------------------------------------------------
@ -633,9 +634,9 @@ void SdFile::printFatDate(uint16_t fatDate) {
*/
void SdFile::printFatTime(uint16_t fatTime) {
printTwoDigits(FAT_HOUR(fatTime));
Serial.print(':');
PRINT_PORT.print(':');
printTwoDigits(FAT_MINUTE(fatTime));
Serial.print(':');
PRINT_PORT.print(':');
printTwoDigits(FAT_SECOND(fatTime));
}
//------------------------------------------------------------------------------
@ -648,7 +649,7 @@ void SdFile::printTwoDigits(uint8_t v) {
str[0] = '0' + v/10;
str[1] = '0' + v % 10;
str[2] = 0;
Serial.print(str);
PRINT_PORT.print(str);
}
//------------------------------------------------------------------------------
/**

View File

@ -1,94 +0,0 @@
#include "include/HSPI.h"
#include "include/spi_register.h"
#define __min(a,b) ((a > b) ? (b):(a))
void HSPI::begin()
{
spi_fifo = (uint32_t*)SPI_FLASH_C0(hspi_port);
//bit9 of PERIPHS_IO_MUX should be cleared when HSPI clock doesn't equal CPU clock
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_HSPIQ_MISO); // gpio12
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_HSPID_MOSI); // gpio13
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_HSPI_CLK); // gpio14
// SPI clock=CPU clock/8
WRITE_PERI_REG(SPI_FLASH_CLOCK(hspi_port),
((1&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)|
((3&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|
((1&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)|
((3&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
uint32_t regvalue = SPI_FLASH_DOUT;
regvalue |= SPI_DOUTDIN | SPI_CK_I_EDGE;
regvalue &= ~(BIT2 | SPI_FLASH_USR_ADDR | SPI_FLASH_USR_DUMMY | SPI_FLASH_USR_DIN | SPI_USR_COMMAND);
WRITE_PERI_REG(SPI_FLASH_USER(hspi_port), regvalue);
WRITE_PERI_REG(SPI_FLASH_CTRL1(hspi_port), 0);
}
void HSPI::end()
{
}
void HSPI::setDataMode(uint8_t dataMode)
{
}
void HSPI::setBitOrder(uint8_t bitOrder)
{
if (!bitOrder)
{
WRITE_PERI_REG(SPI_FLASH_CTRL(hspi_port),
READ_PERI_REG(SPI_FLASH_CTRL(hspi_port)) & (~(SPI_WR_BIT_ODER | SPI_RD_BIT_ODER)));
}
else
{
WRITE_PERI_REG(SPI_FLASH_CTRL(hspi_port),
READ_PERI_REG(SPI_FLASH_CTRL(hspi_port)) | (SPI_WR_BIT_ODER | SPI_RD_BIT_ODER));
}
}
void HSPI::setClockDivider(uint8_t clockDiv)
{
uint32_t clock_reg_val = (((clockDiv - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) |
((1 & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) |
((0 & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) |
((1 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S);
WRITE_PERI_REG(SPI_FLASH_CLOCK(hspi_port), clock_reg_val);
}
uint8_t HSPI::transfer(uint8_t data)
{
hspi_wait_ready();
hspi_prepare_txrx(1);
*spi_fifo = data;
hspi_start_tx();
hspi_wait_ready();
return *spi_fifo & 0xFF;
}
uint16_t HSPI::transfer16(uint16_t data)
{
hspi_wait_ready();
hspi_prepare_txrx(2);
*spi_fifo = data;
hspi_start_tx();
hspi_wait_ready();
return *spi_fifo & 0xFFFF;
}
void HSPI::transfer(void *buf, size_t count)
{
uint32_t *_data = (uint32_t*)buf;
uint8_t i;
uint8_t words_to_send = __min((count + 3) / 4, hspi_fifo_size);
hspi_prepare_tx(count);
for(i = 0; i < words_to_send;i++)
spi_fifo[i] = _data[i];
hspi_start_tx();
}

View File

@ -1,91 +1,78 @@
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
/*
SPI.cpp - SPI library for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SPI.h"
#include "include/HSPI.h"
SPIClass SPI;
SPIClass::SPIClass(){}
SPIClass::SPIClass()
: _impl(0)
{
void SPIClass::begin(){
pinMode(SCK, SPECIAL);
pinMode(MISO, SPECIAL);
pinMode(MOSI, SPECIAL);
GPMUX = 0x105;
SPI1C = 0;
SPI1CLK = SPI_CLOCK_DIV16;//1MHz
SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO);
SPI1C1 = 0;
}
void SPIClass::begin()
{
if (_impl)
end();
_impl = new HSPI;
_impl->begin();
void SPIClass::end() {
pinMode(SCK, INPUT);
pinMode(MISO, INPUT);
pinMode(MOSI, INPUT);
}
void SPIClass::end()
{
if (!_impl)
return;
_impl->end();
delete _impl;
_impl = 0;
void SPIClass::beginTransaction(SPISettings settings) {
setClockDivider(settings._clock);
setBitOrder(settings._bitOrder);
setDataMode(settings._dataMode);
}
void SPIClass::beginTransaction(SPISettings settings)
{
if (!_impl)
return;
_impl->setBitOrder(settings._bitOrder);
_impl->setDataMode(settings._dataMode);
_impl->setClockDivider(settings._clock);
void SPIClass::endTransaction() {}
void SPIClass::setDataMode(uint8_t dataMode) {
}
uint8_t SPIClass::transfer(uint8_t data)
{
if (!_impl)
return 0;
return _impl->transfer(data);
void SPIClass::setBitOrder(uint8_t bitOrder) {
if (bitOrder == MSBFIRST){
SPI1C &= ~(SPICWBO | SPICRBO);
} else {
SPI1C |= (SPICWBO | SPICRBO);
}
}
uint16_t SPIClass::transfer16(uint16_t data)
{
if (!_impl)
return 0;
return _impl->transfer16(data);
void SPIClass::setClockDivider(uint32_t clockDiv) {
SPI1CLK = clockDiv;
}
void SPIClass::transfer(void *buf, size_t count)
{
if (!_impl)
return;
_impl->transfer(buf, count);
}
void SPIClass::setBitOrder(uint8_t bitOrder)
{
if (!_impl)
return;
_impl->setBitOrder(bitOrder);
}
void SPIClass::setDataMode(uint8_t dataMode)
{
if (!_impl)
return;
_impl->setDataMode(dataMode);
}
void SPIClass::setClockDivider(uint8_t clockDiv)
{
if (!_impl)
return;
_impl->setClockDivider(clockDiv);
uint8_t SPIClass::transfer(uint8_t data) {
while(SPI1CMD & SPIBUSY);
SPI1W0 = data;
SPI1CMD |= SPIBUSY;
while(SPI1CMD & SPIBUSY);
return (uint8_t)(SPI1W0 & 0xff);
}

View File

@ -1,62 +1,86 @@
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
/*
SPI.h - SPI library for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _SPI_H_INCLUDED
#define _SPI_H_INCLUDED
#include <Arduino.h>
#include <stdlib.h>
#include "include/SPIdef.h"
#define FCPU80 80000000L
#if F_CPU == FCPU80
#define SPI_CLOCK_DIV80M 0x80000000 //80 MHz
#define SPI_CLOCK_DIV40M 0x00001001 //40 MHz
#define SPI_CLOCK_DIV20M 0x00041001 //20 MHz
#define SPI_CLOCK_DIV16M 0x000fffc0 //16 MHz
#define SPI_CLOCK_DIV10M 0x000c1001 //10 MHz
#define SPI_CLOCK_DIV2 0x00101001 //8 MHz
#define SPI_CLOCK_DIV5M 0x001c1001 //5 MHz
#define SPI_CLOCK_DIV4 0x00241001 //4 MHz
#define SPI_CLOCK_DIV8 0x004c1001 //2 MHz
#define SPI_CLOCK_DIV16 0x009c1001 //1 MHz
#define SPI_CLOCK_DIV32 0x013c1001 //500 KHz
#define SPI_CLOCK_DIV64 0x027c1001 //250 KHz
#define SPI_CLOCK_DIV128 0x04fc1001 //125 KHz
#else
#define SPI_CLOCK_DIV160M 0x80000000 //160 MHz
#define SPI_CLOCK_DIV80M 0x00001001 //80 MHz
#define SPI_CLOCK_DIV40M 0x00041001 //40 MHz
#define SPI_CLOCK_DIV32M 0x000fffc0 //32 MHz
#define SPI_CLOCK_DIV20M 0x000c1001 //20 MHz
#define SPI_CLOCK_DIV16M 0x00101001 //16 MHz
#define SPI_CLOCK_DIV10M 0x001c1001 //10 MHz
#define SPI_CLOCK_DIV2 0x00241001 //8 MHz
#define SPI_CLOCK_DIV4 0x004c1001 //4 MHz
#define SPI_CLOCK_DIV8 0x009c1001 //2 MHz
#define SPI_CLOCK_DIV16 0x013c1001 //1 MHz
#define SPI_CLOCK_DIV32 0x027c1001 //500 KHz
#define SPI_CLOCK_DIV64 0x04fc1001 //250 KHz
#endif
struct SPISettings
{
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
: _clock(clock)
, _bitOrder(bitOrder)
, _dataMode(dataMode)
{
}
const uint8_t SPI_MODE0 = 0x00;
const uint8_t SPI_MODE1 = 0x04;
const uint8_t SPI_MODE2 = 0x08;
const uint8_t SPI_MODE3 = 0x0C;
class SPISettings {
public:
SPISettings() :_clock(SPI_CLOCK_DIV16), _bitOrder(LSBFIRST), _dataMode(SPI_MODE0){}
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) :_clock(clock), _bitOrder(bitOrder), _dataMode(dataMode){}
uint32_t _clock;
uint8_t _bitOrder;
uint8_t _dataMode;
};
class SPIImpl;
class SPIClass
{
class SPIClass {
public:
SPIClass();
void begin();
// void usingInterrupt(uint8_t interruptNumber);
void beginTransaction(SPISettings settings);
uint8_t transfer(uint8_t data);
uint16_t transfer16(uint16_t data);
void transfer(void *buf, size_t count);
void endTransaction(void);
void end();
void setBitOrder(uint8_t bitOrder);
void setDataMode(uint8_t dataMode);
void setClockDivider(uint8_t clockDiv);
private:
SPIImpl* _impl;
void setClockDivider(uint32_t clockDiv);
void beginTransaction(SPISettings settings);
uint8_t transfer(uint8_t data);
void endTransaction(void);
};
extern SPIClass SPI;

View File

@ -1,51 +0,0 @@
#ifndef HSPI_H
#define HSPI_H
#include "SPIImpl.h"
#include "SPIdef.h"
extern "C" {
#include "spi_register.h"
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
}
class HSPI : public SPIImpl
{
public:
virtual void begin();
virtual void end();
virtual void setBitOrder(uint8_t bitOrder);
virtual void setDataMode(uint8_t dataMode);
virtual void setClockDivider(uint8_t clockDiv);
virtual uint8_t transfer(uint8_t data);
virtual uint16_t transfer16(uint16_t data);
virtual void transfer(void *buf, size_t count);
private:
uint32_t _clkDiv;
uint32_t *spi_fifo;
const uint32_t hspi_port = 1;
const uint32_t hspi_fifo_size = 16;
private:
inline void hspi_wait_ready(void){while (READ_PERI_REG(SPI_FLASH_CMD(hspi_port))&SPI_FLASH_USR);}
inline void hspi_start_tx(){SET_PERI_REG_MASK(SPI_FLASH_CMD(hspi_port), SPI_FLASH_USR);}
inline void hspi_prepare_tx(uint32_t bytecount)
{
uint32_t bitcount = bytecount * 8 - 1;
WRITE_PERI_REG(SPI_FLASH_USER1(hspi_port), (bitcount & SPI_USR_OUT_BITLEN) << SPI_USR_OUT_BITLEN_S);
}
inline void hspi_prepare_txrx(uint32_t bytecount)
{
uint32_t bitcount = bytecount * 8 - 1;
WRITE_PERI_REG(SPI_FLASH_USER1(hspi_port), ((bitcount & SPI_USR_OUT_BITLEN) << SPI_USR_OUT_BITLEN_S) |
((bitcount & SPI_USR_DIN_BITLEN) << SPI_USR_DIN_BITLEN_S));
}
};
#endif//HSPI_H

View File

@ -1,21 +0,0 @@
#ifndef SPIIMPL_H
#define SPIIMPL_H
#include <cstdlib>
#include <Arduino.h>
class SPIImpl
{
public:
virtual void begin() = 0;
virtual uint8_t transfer(uint8_t data) = 0;
virtual uint16_t transfer16(uint16_t data) = 0;
virtual void transfer(void *buf, size_t count) = 0;
virtual void end() = 0;
virtual void setBitOrder(uint8_t bitOrder) = 0;
virtual void setDataMode(uint8_t dataMode) = 0;
virtual void setClockDivider(uint8_t clockDiv) = 0;
};
#endif//SPIIMPL_H

View File

@ -1,28 +0,0 @@
#ifndef SPIDEF_H
#define SPIDEF_H
#ifndef LSBFIRST
#define LSBFIRST 0
#endif
#ifndef MSBFIRST
#define MSBFIRST 1
#endif
// AVR compatibility definitions
const uint8_t SPI_CLOCK_DIV4 = 4;
const uint8_t SPI_CLOCK_DIV16 = 16;
const uint8_t SPI_CLOCK_DIV64 = 64;
const uint8_t SPI_CLOCK_DIV128 = 128;
const uint8_t SPI_CLOCK_DIV2 = 2;
const uint8_t SPI_CLOCK_DIV8 = 8;
const uint8_t SPI_CLOCK_DIV32 = 32;
const uint8_t SPI_MODE0 = 0x00;
const uint8_t SPI_MODE1 = 0x04;
const uint8_t SPI_MODE2 = 0x08;
const uint8_t SPI_MODE3 = 0x0C;
#endif//SPIDEF_H

View File

@ -1,239 +0,0 @@
//Generated at 2014-07-29 11:03:29
/*
* Copyright (c) 2010 - 2011 Espressif System
*
*/
#ifndef SPI_REGISTER_H_INCLUDED
#define SPI_REGISTER_H_INCLUDED
#define REG_SPI_BASE(i) (0x60000200-i*0x100)
#define SPI_FLASH_CMD(i) (REG_SPI_BASE(i) + 0x0)
#define SPI_FLASH_READ (BIT(31))
#define SPI_FLASH_WREN (BIT(30))
#define SPI_FLASH_WRDI (BIT(29))
#define SPI_FLASH_RDID (BIT(28))
#define SPI_FLASH_RDSR (BIT(27))
#define SPI_FLASH_WRSR (BIT(26))
#define SPI_FLASH_PP (BIT(25))
#define SPI_FLASH_SE (BIT(24))
#define SPI_FLASH_BE (BIT(23))
#define SPI_FLASH_CE (BIT(22))
#define SPI_FLASH_DP (BIT(21))
#define SPI_FLASH_RES (BIT(20))
#define SPI_FLASH_HPM (BIT(19))
#define SPI_FLASH_USR (BIT(18))
#define SPI_FLASH_ADDR(i) (REG_SPI_BASE(i) + 0x4)
#define SPI_FLASH_CTRL(i) (REG_SPI_BASE(i) + 0x8)
#define SPI_WR_BIT_ODER (BIT(26))
#define SPI_RD_BIT_ODER (BIT(25))
#define SPI_QIO_MODE (BIT(24))
#define SPI_DIO_MODE (BIT(23))
#define SPI_TWO_BYTE_STATUS_EN (BIT(22))
#define SPI_WP_REG (BIT(21))
#define SPI_QOUT_MODE (BIT(20))
#define SPI_SHARE_BUS (BIT(19))
#define SPI_HOLD_MODE (BIT(18))
#define SPI_ENABLE_AHB (BIT(17))
#define SPI_SST_AAI (BIT(16))
#define SPI_RESANDRES (BIT(15))
#define SPI_DOUT_MODE (BIT(14))
#define SPI_FASTRD_MODE (BIT(13))
#define SPI_FLASH_CTRL1(i) (REG_SPI_BASE (i) + 0xC)
#define SPI_T_CSH 0x0000000F
#define SPI_T_CSH_S 28
#define SPI_T_RES 0x00000FFF
#define SPI_T_RES_S 16
#define SPI_BUS_TIMER_LIMIT 0x0000FFFF
#define SPI_BUS_TIMER_LIMIT_S 0
#define SPI_FLASH_STATUS(i) (REG_SPI_BASE(i) + 0x10)
#define SPI_STATUS_EXT 0x000000FF
#define SPI_STATUS_EXT_S 24
#define SPI_WB_MODE 0x000000FF
#define SPI_WB_MODE_S 16
#define SPI_FLASH_STATUS_PRO_FLAG (BIT(7))
#define SPI_FLASH_TOP_BOT_PRO_FLAG (BIT(5))
#define SPI_FLASH_BP2 (BIT(4))
#define SPI_FLASH_BP1 (BIT(3))
#define SPI_FLASH_BP0 (BIT(2))
#define SPI_FLASH_WRENABLE_FLAG (BIT(1))
#define SPI_FLASH_BUSY_FLAG (BIT(0))
#define SPI_FLASH_CTRL2(i) (REG_SPI_BASE(i) + 0x14)
#define SPI_CS_DELAY_NUM 0x0000000F
#define SPI_CS_DELAY_NUM_S 28
#define SPI_CS_DELAY_MODE 0x00000003
#define SPI_CS_DELAY_MODE_S 26
#define SPI_MOSI_DELAY_NUM 0x00000007
#define SPI_MOSI_DELAY_NUM_S 23
#define SPI_MOSI_DELAY_MODE 0x00000003
#define SPI_MOSI_DELAY_MODE_S 21
#define SPI_MISO_DELAY_NUM 0x00000007
#define SPI_MISO_DELAY_NUM_S 18
#define SPI_MISO_DELAY_MODE 0x00000003
#define SPI_MISO_DELAY_MODE_S 16
#define SPI_CK_OUT_HIGH_MODE 0x0000000F
#define SPI_CK_OUT_HIGH_MODE_S 12
#define SPI_CK_OUT_LOW_MODE 0x0000000F
#define SPI_CK_OUT_LOW_MODE_S 8
#define SPI_HOLD_TIME 0x0000000F
#define SPI_HOLD_TIME_S 4
#define SPI_SETUP_TIME 0x0000000F
#define SPI_SETUP_TIME_S 0
#define SPI_FLASH_CLOCK(i) (REG_SPI_BASE(i) + 0x18)
#define SPI_CLK_EQU_SYSCLK (BIT(31))
#define SPI_CLKDIV_PRE 0x00001FFF
#define SPI_CLKDIV_PRE_S 18
#define SPI_CLKCNT_N 0x0000003F
#define SPI_CLKCNT_N_S 12
#define SPI_CLKCNT_H 0x0000003F
#define SPI_CLKCNT_H_S 6
#define SPI_CLKCNT_L 0x0000003F
#define SPI_CLKCNT_L_S 0
#define SPI_FLASH_USER(i) (REG_SPI_BASE(i) + 0x1C)
#define SPI_USR_COMMAND (BIT(31))
#define SPI_FLASH_USR_ADDR (BIT(30))
#define SPI_FLASH_USR_DUMMY (BIT(29))
#define SPI_FLASH_USR_DIN (BIT(28))
#define SPI_FLASH_DOUT (BIT(27))
#define SPI_USR_DUMMY_IDLE (BIT(26))
#define SPI_USR_DOUT_HIGHPART (BIT(25))
#define SPI_USR_DIN_HIGHPART (BIT(24))
#define SPI_USR_PREP_HOLD (BIT(23))
#define SPI_USR_CMD_HOLD (BIT(22))
#define SPI_USR_ADDR_HOLD (BIT(21))
#define SPI_USR_DUMMY_HOLD (BIT(20))
#define SPI_USR_DIN_HOLD (BIT(19))
#define SPI_USR_DOUT_HOLD (BIT(18))
#define SPI_USR_HOLD_POL (BIT(17))
#define SPI_SIO (BIT(16))
#define SPI_FWRITE_QIO (BIT(15))
#define SPI_FWRITE_DIO (BIT(14))
#define SPI_FWRITE_QUAD (BIT(13))
#define SPI_FWRITE_DUAL (BIT(12))
#define SPI_WR_BYTE_ORDER (BIT(11))
#define SPI_RD_BYTE_ORDER (BIT(10))
#define SPI_AHB_ENDIAN_MODE 0x00000003
#define SPI_AHB_ENDIAN_MODE_S 8
#define SPI_CK_OUT_EDGE (BIT(7))
#define SPI_CK_I_EDGE (BIT(6))
#define SPI_CS_SETUP (BIT(5))
#define SPI_CS_HOLD (BIT(4))
#define SPI_AHB_USR_COMMAND (BIT(3))
#define SPI_AHB_USR_COMMAND_4BYTE (BIT(1))
#define SPI_DOUTDIN (BIT(0))
#define SPI_FLASH_USER1(i) (REG_SPI_BASE(i) + 0x20)
#define SPI_USR_ADDR_BITLEN 0x0000003F
#define SPI_USR_ADDR_BITLEN_S 26
#define SPI_USR_OUT_BITLEN 0x000001FF
#define SPI_USR_OUT_BITLEN_S 17
#define SPI_USR_DIN_BITLEN 0x000001FF
#define SPI_USR_DIN_BITLEN_S 8
#define SPI_USR_DUMMY_CYCLELEN 0x000000FF
#define SPI_USR_DUMMY_CYCLELEN_S 0
#define SPI_FLASH_USER2(i) (REG_SPI_BASE(i) + 0x24)
#define SPI_USR_COMMAND_BITLEN 0x0000000F
#define SPI_USR_COMMAND_BITLEN_S 28
#define SPI_USR_COMMAND_VALUE 0x0000FFFF
#define SPI_USR_COMMAND_VALUE_S 0
#define SPI_FLASH_USER3(i) (REG_SPI_BASE(i) + 0x28)
#define SPI_FLASH_PIN(i) (REG_SPI_BASE(i) + 0x2C)
#define SPI_FLASH_SLAVE(i) (REG_SPI_BASE(i) + 0x30)
#define SPI_SYNC_RESET (BIT(31))
#define SPI_SLAVE_MODE (BIT(30))
#define SPI_SLV_WR_RD_BUF_EN (BIT(29))
#define SPI_SLV_WR_RD_STA_EN (BIT(28))
#define SPI_SLV_CMD_DEFINE (BIT(27))
#define SPI_TRANS_CNT 0x0000000F
#define SPI_TRANS_CNT_S 23
#define SPI_SLV_LAST_STATE 0x00000007
#define SPI_SLV_LAST_STATE_S 20
#define SPI_SLV_LAST_COMMAND 0x00000007
#define SPI_SLV_LAST_COMMAND_S 17
#define SPI_CS_I_MODE 0x00000003
#define SPI_CS_I_MODE_S 10
#define SPI_INT_EN 0x0000001F
#define SPI_INT_EN_S 5
#define SPI_TRANS_DONE (BIT(4))
#define SPI_SLV_WR_STA_DONE (BIT(3))
#define SPI_SLV_RD_STA_DONE (BIT(2))
#define SPI_SLV_WR_BUF_DONE (BIT(1))
#define SPI_SLV_RD_BUF_DONE (BIT(0))
#define SPI_FLASH_SLAVE1(i) (REG_SPI_BASE(i) + 0x34)
#define SPI_SLV_STATUS_BITLEN 0x0000001F
#define SPI_SLV_STATUS_BITLEN_S 27
#define SPI_SLV_STATUS_FAST_EN (BIT(26))
#define SPI_SLV_STATUS_READBACK (BIT(25))
#define SPI_SLV_BUF_BITLEN 0x000001FF
#define SPI_SLV_BUF_BITLEN_S 16
#define SPI_SLV_RD_ADDR_BITLEN 0x0000003F
#define SPI_SLV_RD_ADDR_BITLEN_S 10
#define SPI_SLV_WR_ADDR_BITLEN 0x0000003F
#define SPI_SLV_WR_ADDR_BITLEN_S 4
#define SPI_SLV_WRSTA_DUMMY_EN (BIT(3))
#define SPI_SLV_RDSTA_DUMMY_EN (BIT(2))
#define SPI_SLV_WRBUF_DUMMY_EN (BIT(1))
#define SPI_SLV_RDBUF_DUMMY_EN (BIT(0))
#define SPI_FLASH_SLAVE2(i) (REG_SPI_BASE(i) + 0x38)
#define SPI_SLV_WRBUF_DUMMY_CYCLELEN 0x000000FF
#define SPI_SLV_WRBUF_DUMMY_CYCLELEN_S 24
#define SPI_SLV_RDBUF_DUMMY_CYCLELEN 0x000000FF
#define SPI_SLV_RDBUF_DUMMY_CYCLELEN_S 16
#define SPI_SLV_WRSTA_DUMMY_CYCLELEN 0x000000FF
#define SPI_SLV_WRSTA_DUMMY_CYCLELEN_S 8
#define SPI_SLV_RDSTA_DUMMY_CYCLELEN 0x000000FF
#define SPI_SLV_RDSTA_DUMMY_CYCLELEN_S 0
#define SPI_FLASH_SLAVE3(i) (REG_SPI_BASE(i) + 0x3C)
#define SPI_SLV_WRSTA_CMD_VALUE 0x000000FF
#define SPI_SLV_WRSTA_CMD_VALUE_S 24
#define SPI_SLV_RDSTA_CMD_VALUE 0x000000FF
#define SPI_SLV_RDSTA_CMD_VALUE_S 16
#define SPI_SLV_WRBUF_CMD_VALUE 0x000000FF
#define SPI_SLV_WRBUF_CMD_VALUE_S 8
#define SPI_SLV_RDBUF_CMD_VALUE 0x000000FF
#define SPI_SLV_RDBUF_CMD_VALUE_S 0
#define SPI_FLASH_C0(i) (REG_SPI_BASE(i) +0x40)
#define SPI_FLASH_C1(i) (REG_SPI_BASE(i) +0x44)
#define SPI_FLASH_C2(i) (REG_SPI_BASE(i) +0x48)
#define SPI_FLASH_C3(i) (REG_SPI_BASE(i) +0x4C)
#define SPI_FLASH_C4(i) (REG_SPI_BASE(i) +0x50)
#define SPI_FLASH_C5(i) (REG_SPI_BASE(i) +0x54)
#define SPI_FLASH_C6(i) (REG_SPI_BASE(i) +0x58)
#define SPI_FLASH_C7(i) (REG_SPI_BASE(i) +0x5C)
#define SPI_FLASH_EXT0(i) (REG_SPI_BASE(i) + 0xF0)
#define SPI_T_PP_ENA (BIT(31))
#define SPI_T_PP_SHIFT 0x0000000F
#define SPI_T_PP_SHIFT_S 16
#define SPI_T_PP_TIME 0x00000FFF
#define SPI_T_PP_TIME_S 0
#define SPI_FLASH_EXT1(i) (REG_SPI_BASE(i) + 0xF4)
#define SPI_T_ERASE_ENA (BIT(31))
#define SPI_T_ERASE_SHIFT 0x0000000F
#define SPI_T_ERASE_SHIFT_S 16
#define SPI_T_ERASE_TIME 0x00000FFF
#define SPI_T_ERASE_TIME_S 0
#define SPI_FLASH_EXT2(i) (REG_SPI_BASE(i) + 0xF8)
#define SPI_ST 0x00000007
#define SPI_ST_S 0
#define SPI_FLASH_EXT3(i) (REG_SPI_BASE(i) + 0xFC)
#define SPI_INT_HOLD_ENA 0x00000003
#define SPI_INT_HOLD_ENA_S 0
#endif // SPI_REGISTER_H_INCLUDED

View File

@ -1,5 +1,5 @@
/*
TwoWire.cpp - TWI/I2C library for Wiring & Arduino
TwoWire.cpp - TWI/I2C library for Arduino & Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
@ -15,9 +15,10 @@
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
*/
extern "C" {
@ -26,7 +27,7 @@ extern "C" {
#include <inttypes.h>
}
#include "i2c.h"
#include "si2c.h"
#include "Wire.h"
// Initialize Class Variables //////////////////////////////////////////////////
@ -46,215 +47,137 @@ void (*TwoWire::user_onReceive)(int);
// Constructors ////////////////////////////////////////////////////////////////
TwoWire::TwoWire()
{
}
TwoWire::TwoWire(){}
// Public Methods //////////////////////////////////////////////////////////////
void TwoWire::pins(int sda, int scl)
{
i2c_init(sda, scl);
void TwoWire::begin(int sda, int scl){
twi_init(sda, scl);
flush();
}
void TwoWire::begin(void)
{
rxBufferIndex = 0;
rxBufferLength = 0;
txBufferIndex = 0;
txBufferLength = 0;
void TwoWire::begin(void){
begin(SDA, SCL);
}
void TwoWire::begin(uint8_t address)
{
void TwoWire::begin(uint8_t address){
// twi_setAddress(address);
// twi_attachSlaveTxEvent(onRequestService);
// twi_attachSlaveRxEvent(onReceiveService);
// begin();
begin();
}
void TwoWire::begin(int address)
{
// begin((uint8_t)address);
void TwoWire::begin(int address){
begin((uint8_t)address);
}
void TwoWire::setClock(uint32_t frequency)
{
i2c_freq(frequency);
void TwoWire::setClock(uint32_t frequency){
twi_setClock(frequency);
}
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
{
// clamp to buffer length
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop){
if(size > BUFFER_LENGTH){
size = BUFFER_LENGTH;
}
// perform blocking read into buffer
size_t read = i2c_master_read_from(address, rxBuffer, size, sendStop);
// set rx buffer iterator vars
size_t read = (twi_readFrom(address, rxBuffer, size, sendStop) == 0)?size:0;
rxBufferIndex = 0;
rxBufferLength = read;
return read;
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop){
return requestFrom(address, static_cast<size_t>(quantity), static_cast<bool>(sendStop));
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
{
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity){
return requestFrom(address, static_cast<size_t>(quantity), true);
}
uint8_t TwoWire::requestFrom(int address, int quantity)
{
uint8_t TwoWire::requestFrom(int address, int quantity){
return requestFrom(static_cast<uint8_t>(address), static_cast<size_t>(quantity), true);
}
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
{
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop){
return requestFrom(static_cast<uint8_t>(address), static_cast<size_t>(quantity), static_cast<bool>(sendStop));
}
void TwoWire::beginTransmission(uint8_t address)
{
// indicate that we are transmitting
void TwoWire::beginTransmission(uint8_t address){
transmitting = 1;
// set address of targeted slave
txAddress = address;
// reset tx buffer iterator vars
txBufferIndex = 0;
txBufferLength = 0;
}
void TwoWire::beginTransmission(int address)
{
void TwoWire::beginTransmission(int address){
beginTransmission((uint8_t)address);
}
//
// Originally, 'endTransmission' was an f(void) function.
// It has been modified to take one parameter indicating
// whether or not a STOP should be performed on the bus.
// Calling endTransmission(false) allows a sketch to
// perform a repeated start.
//
// WARNING: Nothing in the library keeps track of whether
// the bus tenure has been properly ended with a STOP. It
// is very possible to leave the bus in a hung state if
// no call to endTransmission(true) is made. Some I2C
// devices will behave oddly if they do not see a STOP.
//
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
// transmit buffer (blocking)
int8_t ret = i2c_master_write_to(txAddress, txBuffer, txBufferLength, sendStop);
// reset tx buffer iterator vars
uint8_t TwoWire::endTransmission(uint8_t sendStop){
int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, sendStop);
txBufferIndex = 0;
txBufferLength = 0;
// indicate that we are done transmitting
transmitting = 0;
return ret;
}
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t TwoWire::endTransmission(void)
{
uint8_t TwoWire::endTransmission(void){
return endTransmission(true);
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
size_t TwoWire::write(uint8_t data)
{
size_t TwoWire::write(uint8_t data){
if(transmitting){
// in master transmitter mode
// don't bother if buffer is full
if(txBufferLength >= BUFFER_LENGTH){
setWriteError();
return 0;
}
// put byte in tx buffer
txBuffer[txBufferIndex] = data;
++txBufferIndex;
// update amount in buffer
txBufferLength = txBufferIndex;
}else{
// in slave send mode
// reply to master
} else {
// i2c_slave_transmit(&data, 1);
}
return 1;
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
size_t TwoWire::write(const uint8_t *data, size_t quantity)
{
size_t TwoWire::write(const uint8_t *data, size_t quantity){
if(transmitting){
// in master transmitter mode
for(size_t i = 0; i < quantity; ++i){
write(data[i]);
if(!write(data[i])) return i;
}
}else{
// in slave send mode
// reply to master
// i2c_slave_transmit(data, quantity);
}
return quantity;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::available(void)
{
int TwoWire::available(void){
return rxBufferLength - rxBufferIndex;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::read(void)
{
int TwoWire::read(void){
int value = -1;
// get each successive byte on each call
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::peek(void)
{
int TwoWire::peek(void){
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}
return value;
}
void TwoWire::flush(void)
{
// XXX: to be implemented.
void TwoWire::flush(void){
rxBufferIndex = 0;
rxBufferLength = 0;
txBufferIndex = 0;
txBufferLength = 0;
}
// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{
// don't bother if user hasn't registered a callback
@ -279,9 +202,7 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
// user_onReceive(numBytes);
}
// behind the scenes function that is called when data is requested
void TwoWire::onRequestService(void)
{
void TwoWire::onRequestService(void){
// // don't bother if user hasn't registered a callback
// if(!user_onRequest){
// return;
@ -294,16 +215,12 @@ void TwoWire::onRequestService(void)
// user_onRequest();
}
// sets function called on slave write
void TwoWire::onReceive( void (*function)(int) )
{
// user_onReceive = function;
void TwoWire::onReceive( void (*function)(int) ){
//user_onReceive = function;
}
// sets function called on slave read
void TwoWire::onRequest( void (*function)(void) )
{
// user_onRequest = function;
void TwoWire::onRequest( void (*function)(void) ){
//user_onRequest = function;
}
// Preinstantiate Objects //////////////////////////////////////////////////////

View File

@ -18,6 +18,7 @@
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
*/
#ifndef TwoWire_h
@ -49,7 +50,7 @@ class TwoWire : public Stream
static void onReceiveService(uint8_t*, int);
public:
TwoWire();
void pins(int sda, int scl);
void begin(int sda, int scl);
void begin();
void begin(uint8_t);
void begin(int);

View File

@ -1,53 +0,0 @@
/*
twi.h - TWI/I2C library for Wiring & Arduino
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef twi_h
#define twi_h
#include <inttypes.h>
//#define ATMEGA8
#ifndef TWI_FREQ
#define TWI_FREQ 100000L
#endif
#ifndef TWI_BUFFER_LENGTH
#define TWI_BUFFER_LENGTH 32
#endif
#define TWI_READY 0
#define TWI_MRX 1
#define TWI_MTX 2
#define TWI_SRX 3
#define TWI_STX 4
void twi_init(void);
void twi_setAddress(uint8_t);
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t);
uint8_t twi_transmit(const uint8_t*, uint8_t);
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
void twi_attachSlaveTxEvent( void (*)(void) );
void twi_reply(uint8_t);
void twi_stop(void);
void twi_releaseBus(void);
#endif