1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Add WIP SPI library

This commit is contained in:
Ivan Grokhotkov 2015-01-21 04:19:04 +03:00
parent 045c446213
commit ee762f3150
7 changed files with 325 additions and 0 deletions

75
libraries/SPI/SPI.cpp Normal file
View File

@ -0,0 +1,75 @@
/*
* 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.
*/
#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);
}

63
libraries/SPI/SPI.h Normal file
View File

@ -0,0 +1,63 @@
/*
* 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.
*/
#ifndef _SPI_H_INCLUDED
#define _SPI_H_INCLUDED
#include <Arduino.h>
#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

View File

@ -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)<<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
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_USR_COMMAND|SPI_USR_MOSI);
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE);
//clear Daul or Quad lines transmission mode
CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);
WRITE_PERI_REG(SPI_CLOCK(spi_no),
((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
//set 8bit output buffer length, the buffer is the low 8bit of register"SPI_FLASH_C0"
WRITE_PERI_REG(SPI_USER1(spi_no),
((7&SPI_USR_MOSI_BITLEN)<<SPI_USR_MOSI_BITLEN_S)|
((7&SPI_USR_MISO_BITLEN)<<SPI_USR_MISO_BITLEN_S));
}
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 void transfer(void *buf, size_t count)
{
}
private:
uint32_t _clkDiv;
};
#endif//HSPI_H

View File

@ -0,0 +1,21 @@
#ifndef SPIIMPL_H
#define SPIIMPL_H
#include <stdint.h>
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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,8 @@
name=SPI
version=1.0
author=Arduino
maintainer=Arduino <info@arduino.cc>
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