diff --git a/cores/arduino/hal/include/SPI_hal.h b/cores/arduino/hal/include/SPI_hal.h new file mode 100644 index 0000000..9ba7c64 --- /dev/null +++ b/cores/arduino/hal/include/SPI_hal.h @@ -0,0 +1,12 @@ + +#ifndef __ARDUINO_SPI_HAL_H +#define __ARDUINO_SPI_HAL_H + +#define SPI_MUTEX_LOCK() +#define SPI_MUTEX_UNLOCK() + + +#endif //__ARDUINO_SPI_HAL_H + + + diff --git a/libraries/SPI/examples/basic.ino b/libraries/SPI/examples/basic.ino new file mode 100644 index 0000000..6070bfa --- /dev/null +++ b/libraries/SPI/examples/basic.ino @@ -0,0 +1,20 @@ +#include "SPI.h" + +SPIClass spi0(SPI0); + +void setup() +{ + spi0.setFrequency(10000000); + spi0.begin(27, 26, 28, 29); + spi0.transfer(1); + spi0.transfer(2); + spi0.transfer(3); + spi0.transfer(0xEE); +} + +void loop() +{ + +} + + diff --git a/libraries/SPI/examples/ssd1306.ino b/libraries/SPI/examples/ssd1306.ino new file mode 100644 index 0000000..c1947f6 --- /dev/null +++ b/libraries/SPI/examples/ssd1306.ino @@ -0,0 +1,429 @@ +/************************************************************************** + This is an example for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + + This example is for a 128x64 pixel display using SPI to communicate + 4 or 5 pins are required to interface. + + Adafruit invests time and resources providing this open + source code, please support Adafruit and open-source + hardware by purchasing products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries, + with contributions from the open source community. + BSD license, check license.txt for more information + All text above, and the splash screen below must be + included in any redistribution. + **************************************************************************/ + +#include +// #include +#include +#include + +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels + +// Declaration for SSD1306 display connected using software SPI (default case): +#define OLED_MOSI 28 +#define OLED_CLK 27 +#define OLED_DC 21 +#define OLED_CS 19 +#define OLED_RESET 22 +#define OLED_FREQ 10000000 //10MHz + +// SPIClass spi0(SPI0); // equal to object SPI +SPIClass spi0(SPI0, OLED_CLK, -1, OLED_MOSI, -1, OLED_FREQ); // just for k210 + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &spi0, + OLED_DC, OLED_RESET, OLED_CS); + +/* Comment out above, uncomment this block to use hardware SPI +#define OLED_DC 6 +#define OLED_CS 7 +#define OLED_RESET 8 +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, + &SPI, OLED_DC, OLED_RESET, OLED_CS); +*/ + +#define NUMFLAKES 10 // Number of snowflakes in the animation example + +#define LOGO_HEIGHT 16 +#define LOGO_WIDTH 16 +static const unsigned char PROGMEM logo_bmp[] = +{ B00000000, B11000000, + B00000001, B11000000, + B00000001, B11000000, + B00000011, B11100000, + B11110011, B11100000, + B11111110, B11111000, + B01111110, B11111111, + B00110011, B10011111, + B00011111, B11111100, + B00001101, B01110000, + B00011011, B10100000, + B00111111, B11100000, + B00111111, B11110000, + B01111100, B11110000, + B01110000, B01110000, + B00000000, B00110000 }; + +void setup() { + Serial.begin(9600); + spi0.setFrequency(10000000); + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + if(!display.begin(SSD1306_SWITCHCAPVCC)) { + Serial.println(F("SSD1306 allocation failed")); + for(;;); // Don't proceed, loop forever + } + + + // Show initial display buffer contents on the screen -- + // the library initializes this with an Adafruit splash screen. + display.display(); + delay(2000); // Pause for 2 seconds + + // Clear the buffer + display.clearDisplay(); + + // Draw a single pixel in white + display.drawPixel(10, 10, WHITE); + + // Show the display buffer on the screen. You MUST call display() after + // drawing commands to make them visible on screen! + display.display(); + delay(2000); + // display.display() is NOT necessary after every single drawing command, + // unless that's what you want...rather, you can batch up a bunch of + // drawing operations and then update the screen all at once by calling + // display.display(). These examples demonstrate both approaches... + + testdrawline(); // Draw many lines + + testdrawrect(); // Draw rectangles (outlines) + + testfillrect(); // Draw rectangles (filled) + + testdrawcircle(); // Draw circles (outlines) + + testfillcircle(); // Draw circles (filled) + + testdrawroundrect(); // Draw rounded rectangles (outlines) + + testfillroundrect(); // Draw rounded rectangles (filled) + + testdrawtriangle(); // Draw triangles (outlines) + + testfilltriangle(); // Draw triangles (filled) + + testdrawchar(); // Draw characters of the default font + + testdrawstyles(); // Draw 'stylized' characters + + testscrolltext(); // Draw scrolling text + + testdrawbitmap(); // Draw a small bitmap image + + // Invert and restore display, pausing in-between + display.invertDisplay(true); + delay(1000); + display.invertDisplay(false); + delay(1000); + + testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps +} + +void loop() { +} + +void testdrawline() { + int16_t i; + + display.clearDisplay(); // Clear display buffer + + for(i=0; i=0; i-=4) { + display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); + display.display(); + delay(1); + } + delay(250); + + display.clearDisplay(); + + for(i=display.width()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); + display.display(); + delay(1); + } + for(i=display.height()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); + display.display(); + delay(1); + } + delay(250); + + display.clearDisplay(); + + for(i=0; i0; i-=3) { + // The INVERSE color is used so circles alternate white/black + display.fillCircle(display.width() / 2, display.height() / 2, i, INVERSE); + display.display(); // Update screen with each newly-drawn circle + delay(1); + } + + delay(2000); +} + +void testdrawroundrect(void) { + display.clearDisplay(); + + for(int16_t i=0; i0; i-=5) { + // The INVERSE color is used so triangles alternate white/black + display.fillTriangle( + display.width()/2 , display.height()/2-i, + display.width()/2-i, display.height()/2+i, + display.width()/2+i, display.height()/2+i, INVERSE); + display.display(); + delay(1); + } + + delay(2000); +} + +void testdrawchar(void) { + display.clearDisplay(); + + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(WHITE); // Draw white text + display.setCursor(0, 0); // Start at top-left corner + display.cp437(true); // Use full 256 char 'Code Page 437' font + + // Not all the characters will fit on the display. This is normal. + // Library will draw what it can and the rest will be clipped. + for(int16_t i=0; i<256; i++) { + if(i == '\n') display.write(' '); + else display.write(i); + } + + display.display(); + delay(2000); +} + +void testdrawstyles(void) { + display.clearDisplay(); + + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(WHITE); // Draw white text + display.setCursor(0,0); // Start at top-left corner + display.println(F("Hello, world!")); + + display.setTextColor(BLACK, WHITE); // Draw 'inverse' text + display.println(3.141592); + + display.setTextSize(2); // Draw 2X-scale text + display.setTextColor(WHITE); + display.print(F("0x")); display.println(0xDEADBEEF, HEX); + + display.display(); + delay(2000); +} + +void testscrolltext(void) { + display.clearDisplay(); + + display.setTextSize(2); // Draw 2X-scale text + display.setTextColor(WHITE); + display.setCursor(10, 0); + display.println(F("scroll")); + display.display(); // Show initial text + delay(100); + + // Scroll in various directions, pausing in-between: + display.startscrollright(0x00, 0x0F); + delay(2000); + display.stopscroll(); + delay(1000); + display.startscrollleft(0x00, 0x0F); + delay(2000); + display.stopscroll(); + delay(1000); + display.startscrolldiagright(0x00, 0x07); + delay(2000); + display.startscrolldiagleft(0x00, 0x07); + delay(2000); + display.stopscroll(); + delay(1000); +} + +void testdrawbitmap(void) { + display.clearDisplay(); + + display.drawBitmap( + (display.width() - LOGO_WIDTH ) / 2, + (display.height() - LOGO_HEIGHT) / 2, + logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1); + display.display(); + delay(1000); +} + +#define XPOS 0 // Indexes into the 'icons' array in function below +#define YPOS 1 +#define DELTAY 2 + +void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) { + int8_t f, icons[NUMFLAKES][3]; + + // Initialize 'snowflake' positions + for(f=0; f< NUMFLAKES; f++) { + icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); + icons[f][YPOS] = -LOGO_HEIGHT; + icons[f][DELTAY] = random(1, 6); + // Serial.print(F("x: ")); + // Serial.print(icons[f][XPOS], DEC); + // Serial.print(F(" y: ")); + // Serial.print(icons[f][YPOS], DEC); + // Serial.print(F(" dy: ")); + // Serial.println(icons[f][DELTAY], DEC); + } + + for(;;) { // Loop forever... + display.clearDisplay(); // Clear the display buffer + + // Draw each snowflake: + for(f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); + } + + display.display(); // Show the display buffer on the screen + delay(200); // Pause for 1/10 second + + // Then update coordinates of each flake... + for(f=0; f< NUMFLAKES; f++) { + icons[f][YPOS] += icons[f][DELTAY]; + // If snowflake is off the bottom of the screen... + if (icons[f][YPOS] >= display.height()) { + // Reinitialize to a random position, just off the top + icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); + icons[f][YPOS] = -LOGO_HEIGHT; + icons[f][DELTAY] = random(1, 6); + } + } + } +} diff --git a/libraries/SPI/keywords.txt b/libraries/SPI/keywords.txt new file mode 100644 index 0000000..51ca29f --- /dev/null +++ b/libraries/SPI/keywords.txt @@ -0,0 +1,35 @@ +####################################### +# Syntax Coloring Map SPI +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SPI KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +begin KEYWORD2 +end KEYWORD2 +transfer KEYWORD2 +transferBytes KEYWORD2 +setBitOrder KEYWORD2 +setDataMode KEYWORD2 +setFrequency KEYWORD2 +beginTransaction KEYWORD2 +endTransaction KEYWORD2 +bus KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### +SPI_MODE0 LITERAL1 +SPI_MODE1 LITERAL1 +SPI_MODE2 LITERAL1 +SPI_MODE3 LITERAL1 +SPI_MSBFIRST LITERAL1 +SPI0 LITERAL1 +SPI1 LITERAL1 diff --git a/libraries/SPI/library.properties b/libraries/SPI/library.properties new file mode 100644 index 0000000..735fe2e --- /dev/null +++ b/libraries/SPI/library.properties @@ -0,0 +1,9 @@ +name=SPI +version=1.0 +author=Neucrack +maintainer=Neucrack +sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus. +paragraph=SPI is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It uses three lines common to all devices (MISO, MOSI and SCK) and one specific for each device. +category=Communication +url=http://arduino.cc/en/Reference/SPI +architectures=k210 diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index e69de29..f357ce0 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -0,0 +1,389 @@ + +#include "SPI.h" + +#include "stdio.h" +#include "utils.h" +#include "fpioa.h" +#include "stdint.h" +#include "stdbool.h" +#include "spi.h" +#include "SPI_hal.h" +#include "sysctl.h" + +SPIClass SPI; + +extern volatile spi_t *const spi[4]; + +static spi_transfer_width_t sipeed_spi_get_frame_size(size_t data_bit_length) +{ + if (data_bit_length < 8) + return SPI_TRANS_CHAR; + else if (data_bit_length < 16) + return SPI_TRANS_SHORT; + return SPI_TRANS_INT; +} + +static void sipeed_spi_set_tmod(uint8_t spi_num, uint32_t tmod) +{ + configASSERT(spi_num < SPI_DEVICE_MAX && spi_num != 2); + volatile spi_t *spi_handle = spi[spi_num]; + uint8_t tmod_offset = 0; + switch(spi_num) + { + case 0: + case 1: + tmod_offset = 8; + break; + case 2: + configASSERT(!"Spi Bus 2 Not Support!"); + break; + case 3: + default: + tmod_offset = 10; + break; + } + set_bit(&spi_handle->ctrlr0, 3 << tmod_offset, tmod << tmod_offset); +} + +/** + * + * @param chip_select -1: not use cs + */ +void sipeed_spi_transfer_data_standard(spi_device_num_t spi_num, int8_t chip_select, const uint8_t *tx_buff,uint8_t *rx_buff, size_t len) +{ + configASSERT(spi_num < SPI_DEVICE_MAX && spi_num != 2); + configASSERT(len > 0); + size_t index, fifo_len; + size_t rx_len = len; + size_t tx_len = rx_len; + sipeed_spi_set_tmod(spi_num, SPI_TMOD_TRANS_RECV); + + volatile spi_t *spi_handle = spi[spi_num]; + + uint8_t dfs_offset; + switch(spi_num){ + case 0: + case 1: + dfs_offset = 16; + break; + case 2: + configASSERT(!"Spi Bus 2 Not Support!"); + break; + case 3: + default: + dfs_offset = 0; + break; + } + uint32_t data_bit_length = (spi_handle->ctrlr0 >> dfs_offset) & 0x1F; + spi_transfer_width_t frame_width = sipeed_spi_get_frame_size(data_bit_length); + spi_handle->ctrlr1 = (uint32_t)(tx_len/frame_width - 1); + spi_handle->ssienr = 0x01; + spi_handle->ser = 1U << chip_select; + uint32_t i = 0; + while (tx_len) + { + fifo_len = 32 - spi_handle->txflr; + fifo_len = fifo_len < tx_len ? fifo_len : tx_len; + switch(frame_width) + { + case SPI_TRANS_INT: + fifo_len = fifo_len / 4 * 4; + for (index = 0; index < fifo_len / 4; index++) + spi_handle->dr[0] = ((uint32_t *)tx_buff)[i++]; + break; + case SPI_TRANS_SHORT: + fifo_len = fifo_len / 2 * 2; + for (index = 0; index < fifo_len / 2; index++) + spi_handle->dr[0] = ((uint16_t *)tx_buff)[i++]; + break; + default: + for (index = 0; index < fifo_len; index++) + spi_handle->dr[0] = tx_buff[i++]; + break; + } + tx_len -= fifo_len; + } + + while ((spi_handle->sr & 0x05) != 0x04) + ; + if(rx_buff) + { + i = 0; + while (rx_len) + { + fifo_len = spi_handle->rxflr; + fifo_len = fifo_len < rx_len ? fifo_len : rx_len; + switch(frame_width) + { + case SPI_TRANS_INT: + fifo_len = fifo_len / 4 * 4; + for (index = 0; index < fifo_len / 4; index++) + ((uint32_t *)rx_buff)[i++] = spi_handle->dr[0]; + break; + case SPI_TRANS_SHORT: + fifo_len = fifo_len / 2 * 2; + for (index = 0; index < fifo_len / 2; index++) + ((uint16_t *)rx_buff)[i++] = (uint16_t)spi_handle->dr[0]; + break; + default: + for (index = 0; index < fifo_len; index++) + rx_buff[i++] = (uint8_t)spi_handle->dr[0]; + break; + } + + rx_len -= fifo_len; + } + } + spi_handle->ser = 0x00; + spi_handle->ssienr = 0x00; +} + +void sipeed_spi_deinit(spi_device_num_t spi_num) +{ + volatile spi_t *spi_handle = spi[spi_num]; + spi_handle->ssienr = 0x00; + sysctl_clock_disable( sysctl_clock_t(SYSCTL_CLOCK_SPI0 + spi_num)); +} + + + +typedef struct{ + int8_t pin; + bool used; +} spi_ss_t; + +static spi_ss_t g_ss_table[4]={ + {.pin = -1, .used = false}, + {.pin = -1, .used = false}, + {.pin = -1, .used = false}, + {.pin = -1, .used = false} +}; + +int8_t getSsByPin(int8_t pin) +{ + uint8_t i; + + if(pin<0) + return -1; + for(i=0; i<4; ++i) + { + if(g_ss_table[i].used && g_ss_table[i].pin == pin) + return i; + } + return -1; +} + +int8_t getSsNumLast() +{ + uint8_t i=0, count=0; + for(i=0; i<4; ++i) + { + if(!g_ss_table[i].used) + ++count; + } + return count; +} + +bool checkSs(int8_t ss) +{ + int8_t ssPeriph; + if( ss < 0) // not use ss + return true; + ssPeriph = getSsByPin(ss); + if(ssPeriph >= 0) + return true; + if(getSsNumLast() > 0) + return true; + return false; +} + +int8_t setSs(int8_t pin) +{ + uint8_t i=0; + for(i=0; i<4; ++i) + { + if(!g_ss_table[i].used) + { + g_ss_table[i].used = true; + g_ss_table[i].pin = pin; + return i; + } + } + return -1; +} + +/** + * @param miso -1: not use miso + * @param mosi must >= 0 + * @param ss -1: not use hardware ss + */ +bool checkPinParam(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) +{ + if(!checkSs(ss)) + return false; + //TODO: + return true; +} + +bool checkFreq(uint32_t freq) +{ + return true; +} +bool checkBitOder(uint8_t bitOrder) +{ + //TODO: support LSB + if(bitOrder!=SPI_MSBFIRST) + return false; + return true; +} +bool checkDataMode(uint8_t dataMode) +{ + if(dataMode > SPI_MODE3) + return false; + return true; +} + +SPIClass::SPIClass(spi_id_t spi_bus) +:_spiNum(spi_bus), _freq(1000000), + _sck(-1), _miso(-1), _mosi(-1), _ss(-1), + _inTransaction(false), + _initPinsInConstruct(false) +{ + configASSERT(_spiNum==SPI_SOFT || _spiNum==SPI0 || _spiNum==SPI1); +} + +SPIClass::SPIClass(spi_id_t spi_bus, int8_t sck, int8_t miso, int8_t mosi, int8_t ss, uint32_t freq) +:_spiNum(spi_bus), _freq(freq), + _sck(sck), _miso(miso), _mosi(mosi), _ss(ss), + _inTransaction(false), + _initPinsInConstruct(true) +{ + +} + + +/** + * + * @param ss -1: not use + */ +void SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) +{ + if(_initPinsInConstruct) + { + sck = _sck; + miso = _miso; + mosi = _mosi; + ss = _ss; + } + configASSERT(checkPinParam(sck, miso, mosi, ss)); + + if(_spiNum == SPI_SOFT) + { + configASSERT(!"Soft SPI Not Support Yet!"); + return ; + } + //hardware SPI + if(_spiNum == SPI0) + { + fpioa_set_function(sck, FUNC_SPI0_SCLK); + if( ss >= 0) + { + fpioa_function_t a = (fpioa_function_t)(FUNC_SPI0_SS0+setSs(ss)); + fpioa_set_function(ss, a); + } + fpioa_set_function(mosi, FUNC_SPI0_D0); + if(miso>=0) + fpioa_set_function(miso, FUNC_SPI0_D1); + } + else if(_spiNum == SPI1) + { + fpioa_set_function(sck, FUNC_SPI1_SCLK); + if( ss >= 0) + { + fpioa_set_function(ss, (fpioa_function_t)(FUNC_SPI1_SS0+setSs(ss))); + } + fpioa_set_function(mosi, FUNC_SPI1_D0); + if(miso>=0) + fpioa_set_function(miso, FUNC_SPI1_D1); + } + _mosi = mosi; + _miso = miso; + _sck = sck; + _ss = ss; + _ssPeriph = getSsByPin(ss); + if(_ssPeriph<0) + _ssPeriph = 0; // default to cs0 TODO: optimize? + spi_init(spi_device_num_t(_spiNum), spi_work_mode_t(_dataMode), SPI_FF_STANDARD, 8, 0); + spi_set_clk_rate(spi_device_num_t(_spiNum), _freq); +} + +void SPIClass::end() +{ + sipeed_spi_deinit(spi_device_num_t(_spiNum)); +} + +void SPIClass::setBitOrder(uint8_t bitOrder) +{ + //TODO: support LSB + configASSERT(bitOrder==SPI_MSBFIRST); +} + +void SPIClass::setDataMode(uint8_t dataMode) +{ + _dataMode = dataMode; + spi_init(spi_device_num_t(_spiNum), spi_work_mode_t(_dataMode), SPI_FF_STANDARD, 8, 0); +} + +void SPIClass::setFrequency(uint32_t freq) +{ + _freq = freq; + spi_set_clk_rate(spi_device_num_t(_spiNum), _freq); +} + +void SPIClass::beginTransaction(SPISettings settings) +{ + SPI_MUTEX_LOCK(); + _inTransaction = true; + if( settings._freq!=_freq || settings._dataMode!=_dataMode || settings._bitOrder!=_bitOrder) + { + _dataMode = settings._dataMode; + _bitOrder = settings._bitOrder; //TODO: bit order + _freq = settings._freq; + spi_init(spi_device_num_t(_spiNum), spi_work_mode_t(_dataMode), SPI_FF_STANDARD, 8, 0); + spi_set_clk_rate(spi_device_num_t(_spiNum), _freq); + } + +} + +void SPIClass::endTransaction(void) +{ + _inTransaction = false; + SPI_MUTEX_UNLOCK(); +} + +void SPIClass::transfer(uint8_t * data, uint32_t size) +{ + sipeed_spi_transfer_data_standard(spi_device_num_t(_spiNum), _ssPeriph, data, NULL, size); +} + +uint8_t SPIClass::transfer(uint8_t data) +{ + uint8_t temp; + sipeed_spi_transfer_data_standard(spi_device_num_t(_spiNum), _ssPeriph, &data, &temp, 1); + return temp; +} + +void SPIClass::transferBytes(uint8_t * data, uint8_t * out, uint32_t size) +{ + sipeed_spi_transfer_data_standard(spi_device_num_t(_spiNum), _ssPeriph, data, out, size); +} + + +SPISettings::SPISettings(uint32_t freq, uint8_t bitOrder, uint8_t dataMode) +:_freq(freq), _bitOrder(bitOrder), _dataMode(dataMode) +{ + configASSERT(checkFreq(freq)); + configASSERT(checkBitOder(bitOrder)); + configASSERT(checkDataMode(dataMode)); +} + diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index e69de29..2f0e647 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -0,0 +1,75 @@ +#ifndef __SPI_H +#define __SPI_H + +#include "stdint.h" + +typedef enum{ + SPI_SOFT = -1, + SPI0 = 0, + SPI1, + SPI2, + SPI3, + SPI_MAX +} spi_id_t; + + +#define SPI_LSBFIRST 0 +#define SPI_MSBFIRST 1 + +#define SPI_MODE0 0 +#define SPI_MODE1 1 +#define SPI_MODE2 2 +#define SPI_MODE3 3 + + +class SPISettings +{ +public: + SPISettings() :_freq(1000000), _bitOrder(SPI_MSBFIRST), _dataMode(SPI_MODE0) {} + SPISettings(uint32_t freq, uint8_t bitOrder, uint8_t dataMode); + uint32_t _freq; + uint8_t _bitOrder; + uint8_t _dataMode; +}; + +class SPIClass +{ +private: + spi_id_t _spiNum; + uint32_t _freq; + int8_t _sck; + int8_t _miso; + int8_t _mosi; + int8_t _ss; //pin + int8_t _ssPeriph; //peripheral + uint8_t _dataMode; + uint8_t _bitOrder; + bool _inTransaction; + bool _initPinsInConstruct; + +public: + SPIClass(spi_id_t spi_bus = SPI0); + /* + * API just for k210 + */ + SPIClass(spi_id_t spi_bus, int8_t sck, int8_t miso, int8_t mosi, int8_t ss, uint32_t freq = 1000000); + void begin(int8_t sck=27, int8_t miso=26, int8_t mosi=28, int8_t ss=29); + void end(); + + void setBitOrder(uint8_t bitOrder); + void setDataMode(uint8_t dataMode); + void setFrequency(uint32_t freq); + + void beginTransaction(SPISettings settings); + void endTransaction(void); + void transfer(uint8_t * data, uint32_t size); + uint8_t transfer(uint8_t data); + + void transferBytes(uint8_t * data, uint8_t * out, uint32_t size); + +}; + +extern SPIClass SPI; + + +#endif //__SPI_H