Skip to content

Commit

Permalink
Initial ESP8266 HW SPI implementation
Browse files Browse the repository at this point in the history
ILI9341 lib as a client for SPI lib
  • Loading branch information
Andrey Filimonov committed Apr 1, 2015
1 parent 3278e1a commit 644c0ef
Show file tree
Hide file tree
Showing 24 changed files with 2,225 additions and 74 deletions.
95 changes: 95 additions & 0 deletions hardware/esp8266com/esp8266/libraries/SPI/HSPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#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
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_HSPI_CS0); // gpio15

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);

SET_PERI_REG_MASK(SPI_FLASH_USER(hspi_port), regvalue);

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

}

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();
}



20 changes: 18 additions & 2 deletions hardware/esp8266com/esp8266/libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

#include "SPI.h"
#include "HSPI.h"
#include "include\HSPI.h"

SPIClass SPI;

Expand All @@ -38,13 +38,29 @@ void SPIClass::end()
_impl = 0;
}

void SPIClass::beginTransaction(SPISettings settings)
{
if (!_impl)
return;
_impl->setBitOrder(settings._bitOrder);
_impl->setDataMode(settings._dataMode);
_impl->setClockDivider(settings._clock);
}

uint8_t SPIClass::transfer(uint8_t data)
{
if (!_impl)
return;
return 0;
return _impl->transfer(data);
}

uint16_t SPIClass::transfer16(uint16_t data)
{
if (!_impl)
return 0;
return _impl->transfer16(data);
}

void SPIClass::transfer(void *buf, size_t count)
{
if (!_impl)
Expand Down
7 changes: 4 additions & 3 deletions hardware/esp8266com/esp8266/libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define _SPI_H_INCLUDED

#include <Arduino.h>
#include <stdlib.h>
#include "include/SPIdef.h"


Expand Down Expand Up @@ -42,11 +43,11 @@ class SPIClass
void begin();

// void usingInterrupt(uint8_t interruptNumber);
// void beginTransaction(SPISettings settings);
void beginTransaction(SPISettings settings);
uint8_t transfer(uint8_t data);
// uint16_t transfer16(uint16_t data);
uint16_t transfer16(uint16_t data);
void transfer(void *buf, size_t count);
// void endTransaction(void);
void endTransaction(void);

void end();

Expand Down
89 changes: 23 additions & 66 deletions hardware/esp8266com/esp8266/libraries/SPI/include/HSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,35 @@ extern "C" {
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 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);

virtual void setClockDivider(uint8_t clockDiv)
{

}
private:
uint32_t _clkDiv;
uint32_t *spi_fifo;
const uint32_t hspi_port = 1;
const uint32_t hspi_fifo_size = 32;

virtual uint8_t transfer(uint8_t data)
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);
}

virtual void transfer(void *buf, size_t count)
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));
}

private:
uint32_t _clkDiv;


};

Expand Down
6 changes: 3 additions & 3 deletions hardware/esp8266com/esp8266/libraries/SPI/include/SPIImpl.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#ifndef SPIIMPL_H
#define SPIIMPL_H

#include <stdint.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;

};


Expand Down
Loading

0 comments on commit 644c0ef

Please sign in to comment.