MAX10 & Arduino SPI通信 (3)通信テスト【Arduino プログラム】

シェアする

元記事はこちら

以下、プログラムとなります。

/**********************************************
Arduino nano SPI(Master)
SCK D13
MISO D12
MOSI D11
**********************************************/
#include <SPI.h>
#define SS_PIN 4
String inByte;
String buf;
int AD = 0;
int ADD = 0;
int RW = 0;
int RWD = 0;
int DA = 0;
int DAD = 0;
byte MOSI_Byte = 0;
byte MISO_Byte = 0;
void setup() {
Serial.begin(115200);
pinMode(SS_PIN,OUTPUT);
digitalWrite(SS_PIN, HIGH);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();    //SPI有効
Serial.println("/***************************/");
Serial.println("START Arduino");
Serial.println("SPI Master");
Serial.println("8MHz");
Serial.println("/******* SPI format ********/");
Serial.println("write:[x,y,z]HEX");
Serial.println("x=AD  : 0-FF");
Serial.println("y=R/W : 00/01");
Serial.println("z=DA  : 0-FF");
Serial.println("/******** test CMD *********/");
Serial.println("0001:LED test1");
Serial.println("0002:LED test2");
Serial.println("0003:DOT test1");
Serial.println("0004:DOT test2");
delay(100);
}
void loop() {
while(1){
if(Serial.available() > 0) {
inByte = Serial.readString();
Serial.println(inByte);
break;
}
}
/**********************************************
FPGAテストコマンド
0001:LEDがR=>G=>Bの順に点灯
0002:DOTを5秒間、網目状に点灯
0003:DOTの網目状点灯を1秒ごとに反転
**********************************************/
if(inByte.indexOf(',')==-1){
if(inByte.startsWith("0001")){
LED_test1();
}else if(inByte.startsWith("0002")){
LED_test2();
}else if(inByte.startsWith("0003")){
DOT_test1();
}else if(inByte.startsWith("0004")){
DOT_test2();
}
goto SPIend;
}
/**********************************************
SPIデータ送信フォーマット
1Byte : bit0~7 AD
2Byte : bit7 HIGH write / LOW read
3Byte : bit0~7 DATA
AA,RW,DD
**********************************************/
//AA
buf = inByte.substring(0,inByte.indexOf(','));
if((buf.charAt(1) >= '0') && (buf.charAt(1) <= '9')){
AD = buf.charAt(1) & ~0x30;
}else if((buf.charAt(1) >= 'A') && (buf.charAt(1) <= 'F')){
AD = (buf.charAt(1) & ~0x40)+9;
}else if((buf.charAt(1) >= 'a') && (buf.charAt(1) <= 'f')){
AD = (buf.charAt(1) & ~0x60)+9;
}else{
goto FormatError;
}
if((buf.charAt(0) >= '0') && (buf.charAt(0) <= '9')){
AD = AD + (buf.charAt(0) & ~0x30)*16;
}else if((buf.charAt(0) >= 'A') && (buf.charAt(0) <= 'F')){
AD = AD + ((buf.charAt(0) & ~0x40)+9)*16;
}else if((buf.charAt(0) >= 'a') && (buf.charAt(0) <= 'f')){
AD = AD + ((buf.charAt(0) & ~0x60)+9)*16;
}else{
goto FormatError;
}
Serial.print("AD:");
Serial.print(buf);
Serial.print(",");
Serial.println(AD);
//RW
buf = inByte.substring(inByte.indexOf(',')+1,inByte.lastIndexOf(','));
if((buf.charAt(1) >= '0') && (buf.charAt(1) <= '9')){
RW = buf.charAt(1) & ~0x30;
}else if((buf.charAt(1) >= 'A') && (buf.charAt(1) <= 'F')){
RW = (buf.charAt(1) & ~0x40)+9;
}else if((buf.charAt(1) >= 'a') && (buf.charAt(1) <= 'f')){
RW = (buf.charAt(1) & ~0x60)+9;
}else{
goto FormatError;
}
if((buf.charAt(0) >= '0') && (buf.charAt(0) <= '9')){
RW = RW + (buf.charAt(0) & ~0x30)*16;
}else if((buf.charAt(0) >= 'A') && (buf.charAt(0) <= 'F')){
RW = RW + ((buf.charAt(0) & ~0x40)+9)*16;
}else if((buf.charAt(0) >= 'a') && (buf.charAt(0) <= 'f')){
RW = RW + ((buf.charAt(0) & ~0x60)+9)*16;
}else{
goto FormatError;
}
Serial.print("RW:");
Serial.print(buf);
Serial.print(",");
Serial.println(RW);
//DD
buf = inByte.substring(inByte.lastIndexOf(',')+1,inByte.lastIndexOf('\n'));
if((buf.charAt(1) >= '0') && (buf.charAt(1) <= '9')){
DA = buf.charAt(1) & ~0x30;
}else if((buf.charAt(1) >= 'A') && (buf.charAt(1) <= 'F')){
DA = (buf.charAt(1) & ~0x40)+9;
}else if((buf.charAt(1) >= 'a') && (buf.charAt(1) <= 'f')){
DA = (buf.charAt(1) & ~0x60)+9;
}else{
goto FormatError;
}
if((buf.charAt(0) >= '0') && (buf.charAt(0) <= '9')){
DA = DA + (buf.charAt(0) & ~0x30)*16;
}else if((buf.charAt(0) >= 'A') && (buf.charAt(0) <= 'F')){
DA = DA + ((buf.charAt(0) & ~0x40)+9)*16;
}else if((buf.charAt(0) >= 'a') && (buf.charAt(0) <= 'f')){
DA = DA + ((buf.charAt(0) & ~0x60)+9)*16;
}else{
goto FormatError;
}
Serial.print("DA:");
Serial.print(buf);
Serial.print(",");
Serial.println(DA);
/********  SPI START ********/
digitalWrite(SS_PIN, LOW);    //SS 有効
ADD = SPI.transfer(AD);//送受信処理
RWD = SPI.transfer(RW);//送受信処理
DAD = SPI.transfer(DA);//送受信処理
digitalWrite(SS_PIN, HIGH);   //SS 終了
Serial.print("write:");
Serial.print(AD);
Serial.print("read:");
Serial.println(ADD);
Serial.print("write:");
Serial.print(RW);
Serial.print("read:");
Serial.println(RWD);
Serial.print("write:");
Serial.print(DA);
Serial.print("read:");
Serial.println(DAD);
goto SPIend;
/****************************/
FormatError:
Serial.println("Format Error");
SPIend:
MOSI_Byte = 0;
MISO_Byte = 0;
inByte = '0';
}
void SPIset(int ad, int rw ,int da){
digitalWrite(SS_PIN, LOW);
SPI.transfer(ad);
SPI.transfer(rw);
MISO_Byte = SPI.transfer(da);
digitalWrite(SS_PIN, HIGH);
return MISO_Byte;
}
void LED_test1(){
Serial.println("LED test1");
for(int i=0;i<=5;i++){
SPIset(0,1,255);
delay(100);
SPIset(0,1,0);
SPIset(1,1,255);
delay(100);
SPIset(1,1,0);
SPIset(2,1,255);
delay(100);
SPIset(2,1,0);
}
}
void LED_test2(){
for(int a=0;a<=2;a++){
for(int d=0;d<=255;d++){
SPIset(a,1,d);
delay(5);
}
for(int d=255;d>=0;d--){
SPIset(a,1,d);
delay(5);
}
}
delay(100);
SPIset(0,1,0);
SPIset(1,1,0);
SPIset(2,1,0);
}
void DOT_test1(){
Serial.println("DOT test1");
SPIset(16,1,85);
SPIset(17,1,170);
SPIset(18,1,85);
SPIset(19,1,170);
SPIset(20,1,85);
SPIset(21,1,170);
SPIset(22,1,85);
SPIset(23,1,170);
SPIset(8,1,1);
delay(5000);
SPIset(8,1,0);
}
void DOT_test2(){
Serial.println("DOT test2");
SPIset(16,1,0);
SPIset(17,1,0);
SPIset(18,1,0);
SPIset(19,1,0);
SPIset(20,1,0);
SPIset(21,1,0);
SPIset(22,1,0);
SPIset(23,1,0);
SPIset(8,1,1);
SPIset(16,1,85);
SPIset(17,1,170);
SPIset(18,1,85);
SPIset(19,1,170);
SPIset(20,1,85);
SPIset(21,1,170);
SPIset(22,1,85);
SPIset(23,1,170);
delay(1000);
SPIset(16,1,170);
SPIset(17,1,85);
SPIset(18,1,170);
SPIset(19,1,85);
SPIset(20,1,170);
SPIset(21,1,85);
SPIset(22,1,170);
SPIset(23,1,85);
delay(1000);
SPIset(16,1,85);
SPIset(17,1,170);
SPIset(18,1,85);
SPIset(19,1,170);
SPIset(20,1,85);
SPIset(21,1,170);
SPIset(22,1,85);
SPIset(23,1,170);
delay(1000);
SPIset(16,1,170);
SPIset(17,1,85);
SPIset(18,1,170);
SPIset(19,1,85);
SPIset(20,1,170);
SPIset(21,1,85);
SPIset(22,1,170);
SPIset(23,1,85);
delay(1000);
SPIset(8,1,0);
}

フォローする