mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-01 03:47:23 +03:00
SPI library to the new format and moved Robot_Motor and Robot_Control libraries
This commit is contained in:
129
libraries/SPI/arch/sam/SPI.cpp
Normal file
129
libraries/SPI/arch/sam/SPI.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
* 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_Class.h"
|
||||
|
||||
SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
|
||||
spi(_spi), id(_id), initCb(_initCb)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
void SPIClass::begin() {
|
||||
initCb();
|
||||
SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
|
||||
SPI_Enable(spi);
|
||||
|
||||
// NPCS control is left to the user
|
||||
|
||||
// Default speed set to 4Mhz
|
||||
setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
|
||||
setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
|
||||
setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
|
||||
}
|
||||
|
||||
void SPIClass::begin(uint8_t _pin) {
|
||||
uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
|
||||
PIO_Configure(
|
||||
g_APinDescription[spiPin].pPort,
|
||||
g_APinDescription[spiPin].ulPinType,
|
||||
g_APinDescription[spiPin].ulPin,
|
||||
g_APinDescription[spiPin].ulPinConfiguration);
|
||||
|
||||
// Default speed set to 4Mhz
|
||||
setClockDivider(_pin, 21);
|
||||
setDataMode(_pin, SPI_MODE0);
|
||||
setBitOrder(_pin, MSBFIRST);
|
||||
}
|
||||
|
||||
void SPIClass::end(uint8_t _pin) {
|
||||
uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
|
||||
// Setting the pin as INPUT will disconnect it from SPI peripheral
|
||||
pinMode(spiPin, INPUT);
|
||||
}
|
||||
|
||||
void SPIClass::end() {
|
||||
SPI_Disable(spi);
|
||||
}
|
||||
|
||||
void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
|
||||
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
|
||||
bitOrder[ch] = _bitOrder;
|
||||
}
|
||||
|
||||
void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
|
||||
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
|
||||
mode[ch] = _mode | SPI_CSR_CSAAT;
|
||||
// SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
|
||||
// transfer. Some device needs that for working properly.
|
||||
SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
|
||||
}
|
||||
|
||||
void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
|
||||
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
|
||||
divider[ch] = _divider;
|
||||
// SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
|
||||
// transfer. Some device needs that for working properly.
|
||||
SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
|
||||
}
|
||||
|
||||
byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
|
||||
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
|
||||
// Reverse bit order
|
||||
if (bitOrder[ch] == LSBFIRST)
|
||||
_data = __REV(__RBIT(_data));
|
||||
uint32_t d = _data | SPI_PCS(ch);
|
||||
if (_mode == SPI_LAST)
|
||||
d |= SPI_TDR_LASTXFER;
|
||||
|
||||
// SPI_Write(spi, _channel, _data);
|
||||
while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
|
||||
;
|
||||
spi->SPI_TDR = d;
|
||||
|
||||
// return SPI_Read(spi);
|
||||
while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
|
||||
;
|
||||
d = spi->SPI_RDR;
|
||||
// Reverse bit order
|
||||
if (bitOrder[ch] == LSBFIRST)
|
||||
d = __REV(__RBIT(d));
|
||||
return d & 0xFF;
|
||||
}
|
||||
|
||||
void SPIClass::attachInterrupt(void) {
|
||||
// Should be enableInterrupt()
|
||||
}
|
||||
|
||||
void SPIClass::detachInterrupt(void) {
|
||||
// Should be disableInterrupt()
|
||||
}
|
||||
|
||||
#if SPI_INTERFACES_COUNT > 0
|
||||
static void SPI_0_Init(void) {
|
||||
PIO_Configure(
|
||||
g_APinDescription[PIN_SPI_MOSI].pPort,
|
||||
g_APinDescription[PIN_SPI_MOSI].ulPinType,
|
||||
g_APinDescription[PIN_SPI_MOSI].ulPin,
|
||||
g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
|
||||
PIO_Configure(
|
||||
g_APinDescription[PIN_SPI_MISO].pPort,
|
||||
g_APinDescription[PIN_SPI_MISO].ulPinType,
|
||||
g_APinDescription[PIN_SPI_MISO].ulPin,
|
||||
g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
|
||||
PIO_Configure(
|
||||
g_APinDescription[PIN_SPI_SCK].pPort,
|
||||
g_APinDescription[PIN_SPI_SCK].ulPinType,
|
||||
g_APinDescription[PIN_SPI_SCK].ulPin,
|
||||
g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
|
||||
}
|
||||
|
||||
SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
|
||||
#endif
|
69
libraries/SPI/arch/sam/SPI_Class.h
Normal file
69
libraries/SPI/arch/sam/SPI_Class.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
* 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 "variant.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define SPI_MODE0 0x02
|
||||
#define SPI_MODE1 0x00
|
||||
#define SPI_MODE2 0x03
|
||||
#define SPI_MODE3 0x01
|
||||
|
||||
enum SPITransferMode {
|
||||
SPI_CONTINUE,
|
||||
SPI_LAST
|
||||
};
|
||||
|
||||
class SPIClass {
|
||||
public:
|
||||
SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void));
|
||||
|
||||
byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(BOARD_SPI_DEFAULT_SS, _data, _mode); }
|
||||
byte transfer(byte _channel, uint8_t _data, SPITransferMode _mode = SPI_LAST);
|
||||
|
||||
// SPI Configuration methods
|
||||
|
||||
void attachInterrupt(void);
|
||||
void detachInterrupt(void);
|
||||
|
||||
void begin(void);
|
||||
void end(void);
|
||||
|
||||
// Attach/Detach pin to/from SPI controller
|
||||
void begin(uint8_t _pin);
|
||||
void end(uint8_t _pin);
|
||||
|
||||
// These methods sets a parameter on a single pin
|
||||
void setBitOrder(uint8_t _pin, BitOrder);
|
||||
void setDataMode(uint8_t _pin, uint8_t);
|
||||
void setClockDivider(uint8_t _pin, uint8_t);
|
||||
|
||||
// These methods sets the same parameters but on default pin BOARD_SPI_DEFAULT_SS
|
||||
void setBitOrder(BitOrder _order) { setBitOrder(BOARD_SPI_DEFAULT_SS, _order); };
|
||||
void setDataMode(uint8_t _mode) { setDataMode(BOARD_SPI_DEFAULT_SS, _mode); };
|
||||
void setClockDivider(uint8_t _div) { setClockDivider(BOARD_SPI_DEFAULT_SS, _div); };
|
||||
|
||||
private:
|
||||
Spi *spi;
|
||||
uint32_t id;
|
||||
BitOrder bitOrder[SPI_CHANNELS_NUM];
|
||||
uint32_t divider[SPI_CHANNELS_NUM];
|
||||
uint32_t mode[SPI_CHANNELS_NUM];
|
||||
void (*initCb)(void);
|
||||
};
|
||||
|
||||
#if SPI_INTERFACES_COUNT > 0
|
||||
extern SPIClass SPI;
|
||||
#endif
|
||||
|
||||
#endif
|
31
libraries/SPI/arch/sam/keywords.txt
Normal file
31
libraries/SPI/arch/sam/keywords.txt
Normal file
@ -0,0 +1,31 @@
|
||||
#######################################
|
||||
# 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_MODE0 LITERAL1
|
||||
SPI_MODE1 LITERAL1
|
||||
SPI_MODE2 LITERAL1
|
||||
SPI_MODE3 LITERAL1
|
||||
|
||||
SPI_CONTINUE LITERAL1
|
||||
SPI_LAST LITERAL1
|
Reference in New Issue
Block a user