diff --git a/SPI/SPI-Master/SPI-Master.ino b/SPI/SPI-Master/SPI-Master.ino new file mode 100644 index 0000000..f4155ab --- /dev/null +++ b/SPI/SPI-Master/SPI-Master.ino @@ -0,0 +1,37 @@ +#include "SPIMaster.h" + + +void Print(char *data2, int lenght){ + for(int i = 0; i < lenght; i++) { + Serial.print(data2[i], HEX); + Serial.print(" "); + } +} + +void setup() +{ + Serial.begin(9600); + SPI_MasterInit(); + Serial.println("Master Initialization "); +} + +void loop() +{ + char data[] = {6, 1, 3, 5, 16, 17, 18, 0}; + int length = sizeof(data); + + Serial.print("Start:"); + Print(data,length); + Serial.println(); + + char checking = CRC8(data, length-1); + int size = sizeof(data) / sizeof(data[0]); + data[size - 1] = checking; + + Serial.print("End:"); + Print(data,length); + Serial.println(); + + SPI_MasterTransmit(data, length); + _delay_ms(1000); +} \ No newline at end of file diff --git a/SPI/SPI-Master/SPIMaster.c b/SPI/SPI-Master/SPIMaster.c new file mode 100644 index 0000000..c61bcd1 --- /dev/null +++ b/SPI/SPI-Master/SPIMaster.c @@ -0,0 +1,45 @@ + +#include "SPIMaster.h" +#include +#include + +void SPI_MasterInit(void) +{ + DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS); + DDRB |= (1< 1) { + char checkSum = CRC8(data, index - 1); + char lastElement = data[index - 1]; + if (lastElement == checkSum) { + callback(data); + index = 0; + return; + } + index = 0; + } + } +} + +void loop() { + processSPI(SetCommand); +} \ No newline at end of file diff --git a/SPI/slave/SPI-Slave/aSPISlave.c b/SPI/slave/SPI-Slave/aSPISlave.c new file mode 100644 index 0000000..a8b7cc9 --- /dev/null +++ b/SPI/slave/SPI-Slave/aSPISlave.c @@ -0,0 +1,32 @@ +#include "aSPISlave.h" +#include + +ISR(SPI_STC_vect) { + char received = SPDR; + data[index] = received; + index++; +} + +void SPI_SlaveInit(void) { + DDR_SPI = (1 << DD_MISO); + SPCR = (1 << SPE); +} + +char CRC8(char *data, int length) { + char crc = 0x00; + char poly = 0x07; // полином для CRC8 + + for (int i = 0; i < length - 2; i++) { + crc ^= data[i]; // XOR текущего байта с crc + + for (int j = 0; j < length; j++) { + if (crc & 0x80) { // если старший бит crc равен 1 + crc = (crc << 1) ^ poly; // сдвигаем crc на 1 бит влево и XOR с полиномом + } else { + crc <<= 1; // иначе просто сдвигаем на 1 бит влево + } + } + } + + return crc; +} \ No newline at end of file diff --git a/SPI/slave/SPI-Slave/aSPISlave.h b/SPI/slave/SPI-Slave/aSPISlave.h new file mode 100644 index 0000000..f8fc1c2 --- /dev/null +++ b/SPI/slave/SPI-Slave/aSPISlave.h @@ -0,0 +1,24 @@ +#ifndef aSPISlave_h +#define aSPISlave_h + +#define DDR_SPI DDRB +#define DD_MOSI PB3 +#define DD_MISO PB4 +#define DD_SCK PB5 +#define DD_SS PB2 + +extern int index; +extern char data[]; + +#ifdef __cplusplus +extern "C" { +#endif + + void SPI_SlaveInit(void); + char CRC8(char *data, int length); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/SPI/slave/SPI-Slave/bCommands.c b/SPI/slave/SPI-Slave/bCommands.c new file mode 100644 index 0000000..293d255 --- /dev/null +++ b/SPI/slave/SPI-Slave/bCommands.c @@ -0,0 +1,17 @@ +#include "bCommands.h" + +void SetCommand(char *datalist, int lenght) { + char command = datalist[0]; + char result[lenght]; + char *ptr = &datalist[1]; + strcpy(result, ptr); + switch (command) { + case 6: + AddSymbol(result,lenght); + break; + } +} + +void AddSymbol(char *symbols, int lenght) { + // Выполнение команды +} \ No newline at end of file diff --git a/SPI/slave/SPI-Slave/bCommands.h b/SPI/slave/SPI-Slave/bCommands.h new file mode 100644 index 0000000..1af7bd0 --- /dev/null +++ b/SPI/slave/SPI-Slave/bCommands.h @@ -0,0 +1,15 @@ +#ifndef bCommands_h +#define bCommands_h + +#ifdef __cplusplus +extern "C" { +#endif + + void SetCommand(char *datalist, int lenght); + void AddSymbol(char *symbols, int lenght); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/SPI/slave/SPISlave.c b/SPI/slave/SPISlave.c new file mode 100644 index 0000000..da9b4b9 --- /dev/null +++ b/SPI/slave/SPISlave.c @@ -0,0 +1,42 @@ +#include +#include "SPISlave.h" + +static int index = 0; +static int arIndex = 0; +static char data[255]; + +void SPI_SlaveInit(void) +{ + DDR_SPI = (1 << DD_MISO); + SPCR = (1 << SPE) | (1 << SPIE); +} + +// Функция для вычисления контрольной суммы XOR +char CRC8(char *data, int length) { + char crc = 0x00; + char poly = 0x07; // полином для CRC8 + + for (int i = 0; i < length-2; i++) { + crc ^= data[i]; // XOR текущего байта с crc + + for (int j = 0; j < length; j++) { + if (crc & 0x80) { // если старший бит crc равен 1 + crc = (crc << 1) ^ poly; // сдвигаем crc на 1 бит влево и XOR с полиномом + } else { + crc <<= 1; // иначе просто сдвигаем на 1 бит влево + } + } + } + + return crc; +} + +// Проверка массива на ноль +char checkArray(char *arr, int size) { + for (int i = 0; i < size; i++) { + if (arr[i] != 0) { + return 1; + } + } + return 0; +} diff --git a/SPI/slave/SPISlave.h b/SPI/slave/SPISlave.h new file mode 100644 index 0000000..1884bd3 --- /dev/null +++ b/SPI/slave/SPISlave.h @@ -0,0 +1,23 @@ +#ifndef SPISlave_H +#define SPISlave_H + +#define DDR_SPI DDRB +#define DD_MOSI PB3 +#define DD_MISO PB4 +#define DD_SCK PB5 +#define DD_SS PB2 + + +#ifdef __cplusplus +extern "C" { +#endif + +void SPI_SlaveInit(void); +char CRC8(char *data, int length); +char checkArray(char *arr, int size); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/SPI/slave/slave.ino b/SPI/slave/slave.ino index e5dd3c9..3e79d18 100644 --- a/SPI/slave/slave.ino +++ b/SPI/slave/slave.ino @@ -1,6 +1,6 @@ -#include -#include +// #include // #include +#include "SPISlave.h" #define DDR_SPI DDRB #define DD_MOSI PB3 @@ -12,15 +12,13 @@ static int index = 0; static int arIndex = 0; -static byte data[255]; +static char data[255]; - -void SPI_SlaveInit(void) -{ - DDR_SPI = (1 << DD_MISO); - - SPCR = (1 << SPE) | (1 << SPIE); -} +// void SPI_SlaveInit(void) +// { +// DDR_SPI = (1 << DD_MISO); +// SPCR = (1 << SPE) | (1 << SPIE); +// } ISR(SPI_STC_vect) { @@ -31,22 +29,22 @@ ISR(SPI_STC_vect) void setup() { - Serial.begin(2000000); - SPI_SlaveInit(); - // oled.init(); // инициализация - // oled.clear(); // очистка - Serial.println(); - Serial.println("Initialization "); + Serial.begin(2000000); + SPI_SlaveInit(); + + Serial.println(); + Serial.println("Initialization "); } -void SetCommand(byte *data2){ - byte command = data2[0]; +void SetCommand(char *data2){ + char command = data2[0]; // Отрезать 1 элемент от оставшихся - byte result[index]; - byte *ptr = &data2[1]; + char result[index]; + char *ptr = &data2[1]; strcpy(result, ptr); + Serial.print("\nReceived command: "); - Serial.println(command); + Serial.println(command, HEX); switch (command){ case 6: AddSymbol(result); @@ -56,7 +54,7 @@ void SetCommand(byte *data2){ } -void AddSymbol(byte *symbols){ +void AddSymbol(char *symbols){ Serial.print("Received Add: "); // oled.setScale(3); // масштаб текста (1..4) @@ -70,27 +68,27 @@ void AddSymbol(byte *symbols){ // Функция для вычисления контрольной суммы XOR -byte calculateXORChecksum(byte *data, int length) { - byte crc = 0x00; - byte poly = 0x07; // полином для CRC8 +// char CRC8(char *data, int length) { +// char crc = 0x00; +// char poly = 0x07; // полином для CRC8 - for (int i = 0; i < length-2; i++) { - crc ^= data[i]; // XOR текущего байта с crc +// for (int i = 0; i < length-2; i++) { +// crc ^= data[i]; // XOR текущего байта с crc - for (int j = 0; j < length; j++) { - if (crc & 0x80) { // если старший бит crc равен 1 - crc = (crc << 1) ^ poly; // сдвигаем crc на 1 бит влево и XOR с полиномом - } else { - crc <<= 1; // иначе просто сдвигаем на 1 бит влево - } - } - } +// for (int j = 0; j < length; j++) { +// if (crc & 0x80) { // если старший бит crc равен 1 +// crc = (crc << 1) ^ poly; // сдвигаем crc на 1 бит влево и XOR с полиномом +// } else { +// crc <<= 1; // иначе просто сдвигаем на 1 бит влево +// } +// } +// } - return crc; -} +// return crc; +// } // Вывод массива -void arrayOut(byte *arr,int size){ +void arrayOut(char *arr,int size){ Serial.print("Array: "); for(int i = 0; i < size;i++){ Serial.print(arr[i], HEX); @@ -100,26 +98,26 @@ void arrayOut(byte *arr,int size){ } // Проверка массива на ноль -bool checkArray(byte* arr, int size) { - for (int i = 0; i < size; i++) { - if (arr[i] != 0) { - return true; - } - } - return false; -} - +// char checkArray(char *arr, int size) { +// for (int i = 0; i < size; i++) { +// if (arr[i] != 0) { +// return 1; +// } +// } +// return 0; +// } +// TODO: где то длина массива неправильно летит void loop() { if(PINB & (1 << 2)){ if(index > 0){ - byte sum = 0; - sum = calculateXORChecksum(data, index-1); + char sum = 0; + sum = CRC8(data, index-1); - bool checkNull = false; - byte last_1 = data[index - 1]; - //arrayOut(data, index); + char checkNull = 0; + char last_1 = data[index - 1]; + arrayOut(data, index); if (last_1 == sum){ Serial.println(); @@ -127,7 +125,7 @@ void loop() { Serial.println(sum, HEX); checkNull = checkArray(data, index); - if(checkNull == true){ + if(checkNull == 1){ SetCommand(data); }