Skip to content

Instantly share code, notes, and snippets.

@anoken
Last active January 13, 2020 03:00
Show Gist options
  • Save anoken/b777e6b7910dbb7ed31ca6edbd2d5462 to your computer and use it in GitHub Desktop.
Save anoken/b777e6b7910dbb7ed31ca6edbd2d5462 to your computer and use it in GitHub Desktop.
MaixPy_SPI_test
#include "driver/spi_master.h"
#include "driver/spi_slave.h"
#include <Arduino.h>
#include <SPI.h>
// 通信サイズ
static const uint32_t TRANS_SIZE = 128;
// 通信バッファ
uint8_t* spi_slave_tx_buf;
uint8_t* spi_slave_rx_buf;
// SPI スレーブの設定
spi_slave_transaction_t spi_slave_trans;
spi_slave_interface_config_t spi_slave_cfg;
spi_bus_config_t spi_slave_bus;
// HSPI の端子設定
static const uint8_t SPI_SLAVE_CS = 5;
static const uint8_t SPI_SLAVE_CLK = 18;
static const uint8_t SPI_SLAVE_MOSI = 14;
static const uint8_t SPI_SLAVE_MISO = 23;
// デバッグ用のダンプ関数
void dump_buf( uint8_t* buf, uint32_t len){
for (uint32_t i = 0; i < len; i++) {
Serial.printf("%02X ", buf[i]);
}
Serial.println();
}
// SPI 通信に使用するバッファの初期化
void spi_buf_init(){
spi_slave_tx_buf = (uint8_t*)heap_caps_malloc(TRANS_SIZE, MALLOC_CAP_DMA);
spi_slave_rx_buf = (uint8_t*)heap_caps_malloc(TRANS_SIZE, MALLOC_CAP_DMA);
for (uint32_t i = 0; i < TRANS_SIZE; i++) {
spi_slave_tx_buf[i] = (0xFF - i) & 0xFF;
//spi_slave_tx_buf[i] = 0x00;
}
memset(spi_slave_rx_buf, 0, TRANS_SIZE);
}
// スレーブの通信完了後に呼ばれるコールバック
void spi_slave_tans_done(spi_slave_transaction_t* trans){
Serial.println("SPI Slave 通信完了");
}
// スレーブとして動作させる HSPI の初期化
void spi_slave_init(){
spi_slave_trans.length = 8 * TRANS_SIZE;
spi_slave_trans.rx_buffer = spi_slave_rx_buf;
spi_slave_trans.tx_buffer = spi_slave_tx_buf;
spi_slave_cfg.spics_io_num = SPI_SLAVE_CS;
spi_slave_cfg.flags = 0;
spi_slave_cfg.queue_size = 1;
//spi_slave_cfg.mode = SPI_MODE3;
spi_slave_cfg.mode = 0;
spi_slave_cfg.post_setup_cb = NULL;
spi_slave_cfg.post_trans_cb = spi_slave_tans_done;
spi_slave_bus.sclk_io_num = SPI_SLAVE_CLK;
spi_slave_bus.mosi_io_num = SPI_SLAVE_MOSI;
spi_slave_bus.miso_io_num = SPI_SLAVE_MISO;
spi_slave_bus.max_transfer_sz = 8192;
ESP_ERROR_CHECK(spi_slave_initialize(VSPI_HOST, &spi_slave_bus, &spi_slave_cfg, 1)); // DMA 2ch
}
void spi_init(){
spi_buf_init();
spi_slave_init();
}
void setup()
{
Serial.begin(115200);
spi_init();
}
void loop()
{
spi_transaction_t* spi_trans;
Serial.println("[LOOP]");
// スレーブの送信準備
ESP_ERROR_CHECK(spi_slave_queue_trans(VSPI_HOST, &spi_slave_trans, portMAX_DELAY));
dump_buf( spi_slave_rx_buf, TRANS_SIZE);
dump_buf( spi_slave_tx_buf, TRANS_SIZE);
delay(100);
}
from machine import SPI
from Maix import GPIO
from fpioa_manager import fm, board_info
fm.register(25,fm.fpioa.SPI1_SS0)#cs
fm.register(28,fm.fpioa.SPI1_D0)#mosi
fm.register(26,fm.fpioa.SPI1_D1)#miso
fm.register(27,fm.fpioa.SPI1_SCLK)#sclk
spi01=SPI(SPI.SPI1,mode=SPI.MODE_MASTER,baudrate=10000000,polarity=0,phase=0,bits=8,firstbit=SPI.MSB,mosi=fm.fpioa.SPI1_D0,miso=fm.fpioa.SPI1_D1,sck=fm.fpioa.SPI1_SCLK,cs0=fm.fpioa.SPI1_SS0)
buff=bytearray([0x03,0x02,0x03,0x04,0x05,0x06,0x07,0x08])
spi01.write(buff, cs=SPI.CS0)
time.sleep(0.1)
buffer_w = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2])
buffer_r = bytearray(12)
spi01.write_readinto(buffer_w, buffer_r,cs=SPI.CS0)
print(buffer_r)
time.sleep(0.1)
fm.register(25,fm.fpioa.GPIOHS0)#cs
fm.register(28,fm.fpioa.SPI1_D0)#mosi
fm.register(26,fm.fpioa.SPI1_D1)#miso
fm.register(27,fm.fpioa.SPI1_SCLK)#sclk
buffer_w = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2])
spi01=SPI(SPI.SPI1,mode=SPI.MODE_MASTER,baudrate=10000000,polarity=0,phase=0,bits=8,firstbit=SPI.MSB,mosi=fm.fpioa.SPI1_D0,miso=fm.fpioa.SPI1_D1,sck=fm.fpioa.SPI1_SCLK,cs0=-1)
ss=GPIO(GPIO.GPIOHS0,GPIO.OUT)
def spi_cs_on():
ss.value(0)
def spi_cs_off():
ss.value(1)
spi_cs_on()
spi01.write(buffer_w, cs=SPI.CS0)
spi_cs_off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment