forked from mohr/ArduinoIO
Compare commits
3 Commits
master
...
SPIFunctio
Author | SHA1 | Date | |
---|---|---|---|
8536d04868 | |||
39c171c186 | |||
3141c7a858 |
51
main.cpp
51
main.cpp
@ -11,8 +11,8 @@
|
||||
#include "arduino_core/ArduinoCore-avr/libraries/SPI/src/SPI.h"
|
||||
|
||||
//Define which port to use for the SPI Chip Select
|
||||
#define CS_PORT PORTC
|
||||
#define CS_DDR DDRC
|
||||
#define CS_PORT PORTB
|
||||
#define CS_DDR DDRB
|
||||
|
||||
#define RING_BUFFER_SIZE 100
|
||||
#define MAX_PACKET_LENGTH 100
|
||||
@ -43,15 +43,26 @@ void sendData(uint8_t *data, size_t len) {
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.write(buffer, writtenLen);
|
||||
}
|
||||
|
||||
void transferSPI(uint8_t address, uint8_t *data, size_t datalen) {
|
||||
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE3));
|
||||
CS_PORT = ~address;
|
||||
SPI.transfer(data, datalen);
|
||||
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE3));
|
||||
//CS_PORT = ~address;
|
||||
digitalWrite(10, LOW);
|
||||
//included
|
||||
for (size_t i = 0; i < datalen; i++)
|
||||
{
|
||||
//Serial.print("SentData is: ");Serial.println(data[i]);
|
||||
data[i] = SPI.transfer(data[i]);
|
||||
//Serial.print("Back from n-1 ");Serial.println(data[i]);
|
||||
|
||||
}
|
||||
//SPI.transfer(data, datalen);
|
||||
delay(100);
|
||||
CS_PORT = 0xff;
|
||||
digitalWrite(10, HIGH);
|
||||
//CS_PORT = 0xff;
|
||||
SPI.endTransaction();
|
||||
|
||||
}
|
||||
@ -67,14 +78,14 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
|
||||
|
||||
|
||||
if (crc != 0) {
|
||||
Serial.println("invalid Checksum");
|
||||
//Serial.println("invalid Checksum");
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t payloadLen = (packet[2] << 8) | packet[3];
|
||||
|
||||
if (payloadLen != packetLen - 6) {
|
||||
Serial.println("invalid len");
|
||||
//Serial.println("invalid len");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -89,14 +100,11 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
|
||||
//echo the data back, no need to change the header fields, they are the same
|
||||
//checksum will be written by sendData()
|
||||
//check reply:
|
||||
Serial.println("Data response check: ");
|
||||
for(size_t i =0; i< packetLen; i++){
|
||||
Serial.print("packet nr ");Serial.print(i);Serial.print(" ");Serial.println(packet[i]);
|
||||
}
|
||||
|
||||
sendData(packet, packetLen);
|
||||
break;
|
||||
default:
|
||||
Serial.println("invalid command");
|
||||
//Serial.println("invalid command");
|
||||
break;
|
||||
}
|
||||
|
||||
@ -113,13 +121,13 @@ void handleNewData() {
|
||||
size_t firstSTXinRawData = 0;
|
||||
while ((firstSTXinRawData < rawDataSize)
|
||||
&& (rawData[firstSTXinRawData] != DleEncoder::STX)) {
|
||||
Serial.println(rawData[firstSTXinRawData]);
|
||||
//Serial.println(rawData[firstSTXinRawData]);
|
||||
firstSTXinRawData++;
|
||||
}
|
||||
|
||||
if (rawData[firstSTXinRawData] != DleEncoder::STX) {
|
||||
//there is no STX in our data, throw it away...
|
||||
Serial.println("NO STX");
|
||||
//Serial.println("NO STX");
|
||||
ringBuffer.deleteData(rawDataSize);
|
||||
return;
|
||||
}
|
||||
@ -159,10 +167,17 @@ void serialEvent() {
|
||||
}
|
||||
|
||||
void setup() {
|
||||
CS_DDR = 0xff;
|
||||
CS_PORT = 0xff;
|
||||
//CS_DDR = 0xff;
|
||||
//CS_PORT = 0xff;
|
||||
|
||||
//CS_DDR |= (1<<);
|
||||
//CS_PORT |= (1<<PB2);
|
||||
Serial.begin(9600);
|
||||
SPI.begin();
|
||||
pinMode(MOSI, OUTPUT);
|
||||
pinMode(SCK, OUTPUT);
|
||||
pinMode(MISO, INPUT);
|
||||
pinMode(10, OUTPUT);
|
||||
digitalWrite(10,HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user