From ee762f3150bf0ab7655ebc6dc6d368764e5cbb28 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Jan 2015 04:19:04 +0300 Subject: [PATCH] Add WIP SPI library --- libraries/SPI/SPI.cpp | 75 +++++++++++++++++++++++++ libraries/SPI/SPI.h | 63 +++++++++++++++++++++ libraries/SPI/include/HSPI.h | 94 ++++++++++++++++++++++++++++++++ libraries/SPI/include/SPIImpl.h | 21 +++++++ libraries/SPI/include/SPIdef.h | 28 ++++++++++ libraries/SPI/keywords.txt | 36 ++++++++++++ libraries/SPI/library.properties | 8 +++ 7 files changed, 325 insertions(+) create mode 100644 libraries/SPI/SPI.cpp create mode 100644 libraries/SPI/SPI.h create mode 100644 libraries/SPI/include/HSPI.h create mode 100644 libraries/SPI/include/SPIImpl.h create mode 100644 libraries/SPI/include/SPIdef.h create mode 100644 libraries/SPI/keywords.txt create mode 100644 libraries/SPI/library.properties diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp new file mode 100644 index 000000000..ea39b57f4 --- /dev/null +++ b/libraries/SPI/SPI.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2010 by Cristian Maglie + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) + * Copyright (c) 2014 by Matthijs Kooijman (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. + */ + +#include "SPI.h" +#include "HSPI.h" + +SPIClass SPI; + + +SPIClass::SPIClass() +: _impl(0) +{ +} + +void SPIClass::begin() +{ + if (_impl) + end(); + _impl = new HSPI; + _impl->begin(); +} + +void SPIClass::end() +{ + if (!_impl) + return; + _impl->end(); + delete _impl; + _impl = 0; +} + +uint8_t SPIClass::transfer(uint8_t data) +{ + if (!_impl) + return; + return _impl->transfer(data); +} + +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); +} + diff --git a/libraries/SPI/SPI.h b/libraries/SPI/SPI.h new file mode 100644 index 000000000..72a0b79c3 --- /dev/null +++ b/libraries/SPI/SPI.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2010 by Cristian Maglie + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) + * Copyright (c) 2014 by Matthijs Kooijman (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. + */ + +#ifndef _SPI_H_INCLUDED +#define _SPI_H_INCLUDED + +#include +#include "include/SPIdef.h" + + + +struct SPISettings +{ + 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 +{ +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; +}; + +extern SPIClass SPI; + +#endif diff --git a/libraries/SPI/include/HSPI.h b/libraries/SPI/include/HSPI.h new file mode 100644 index 000000000..dcbfa9a2f --- /dev/null +++ b/libraries/SPI/include/HSPI.h @@ -0,0 +1,94 @@ +#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() + { + //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 + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_HSPI_CS0); // gpio15 + + + SET_PERI_REG_MASK(SPI_USER(HSPI), SPI_CS_SETUP|SPI_CS_HOLD|SPI_USR_COMMAND); + CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE); + + // SPI clock=CPU clock/8 + WRITE_PERI_REG(SPI_CLOCK(HSPI), + ((1&SPI_CLKDIV_PRE)< + +class SPIImpl +{ +public: + virtual void begin() = 0; + virtual uint8_t transfer(uint8_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 diff --git a/libraries/SPI/include/SPIdef.h b/libraries/SPI/include/SPIdef.h new file mode 100644 index 000000000..c10e1cd86 --- /dev/null +++ b/libraries/SPI/include/SPIdef.h @@ -0,0 +1,28 @@ +#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 diff --git a/libraries/SPI/keywords.txt b/libraries/SPI/keywords.txt new file mode 100644 index 000000000..fa7616581 --- /dev/null +++ b/libraries/SPI/keywords.txt @@ -0,0 +1,36 @@ +####################################### +# Syntax Coloring Map SPI +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SPI KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +begin KEYWORD2 +end KEYWORD2 +transfer KEYWORD2 +setBitOrder KEYWORD2 +setDataMode KEYWORD2 +setClockDivider KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### +SPI_CLOCK_DIV4 LITERAL1 +SPI_CLOCK_DIV16 LITERAL1 +SPI_CLOCK_DIV64 LITERAL1 +SPI_CLOCK_DIV128 LITERAL1 +SPI_CLOCK_DIV2 LITERAL1 +SPI_CLOCK_DIV8 LITERAL1 +SPI_CLOCK_DIV32 LITERAL1 +SPI_CLOCK_DIV64 LITERAL1 +SPI_MODE0 LITERAL1 +SPI_MODE1 LITERAL1 +SPI_MODE2 LITERAL1 +SPI_MODE3 LITERAL1 \ No newline at end of file diff --git a/libraries/SPI/library.properties b/libraries/SPI/library.properties new file mode 100644 index 000000000..bbe0ddb8e --- /dev/null +++ b/libraries/SPI/library.properties @@ -0,0 +1,8 @@ +name=SPI +version=1.0 +author=Arduino +maintainer=Arduino +sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus. For all Arduino boards, BUT Arduino DUE. +paragraph= +url=http://arduino.cc/en/Reference/SPI +architectures=esp8266