Добавлен CRC
This commit is contained in:
parent
f40daf62d4
commit
a71fd231fc
@ -10,16 +10,17 @@
|
|||||||
|
|
||||||
void SPI_MasterInit(void)
|
void SPI_MasterInit(void)
|
||||||
{
|
{
|
||||||
DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS);
|
DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS);
|
||||||
DDRB |= (1<<DDB2); // set SS as output
|
DDRB |= (1<<DDB2); // set SS as output
|
||||||
|
SPCR = (1 << SPE) | (1 << MSTR);
|
||||||
SPCR = (1 << SPE) | (1 << MSTR);
|
// SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << DORD) | (1 << CPOL) | (1 << CPHA) | (1 << SPE);
|
||||||
|
// SPSR = (1 << SPI2X) | (1 << WCOL) | (1 << SPIF);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Print(byte *data2, int lenght){
|
void Print(byte *data2, int lenght){
|
||||||
Serial.println("----");
|
Serial.println("----");
|
||||||
for(int i = 0; i < lenght; i++) {
|
for(int i = 0; i < lenght; i++) {
|
||||||
Serial.print(data2[i]);
|
Serial.print(data2[i], HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -44,25 +45,6 @@ void SPI_MasterTransmit(char *data, int length)
|
|||||||
while (!(SPSR & (1 << SPIF))); // ждем, пока байт передастся
|
while (!(SPSR & (1 << SPIF))); // ждем, пока байт передастся
|
||||||
}
|
}
|
||||||
PORTB |= (1<<2);
|
PORTB |= (1<<2);
|
||||||
|
|
||||||
|
|
||||||
// Print(spdr, length);
|
|
||||||
// return check;
|
|
||||||
// //ждем, пока придет контрольная сумма
|
|
||||||
// while (!(SPSR & (1 << SPIF)));
|
|
||||||
// uint8_t received_checksum = SPDR;
|
|
||||||
|
|
||||||
// // вычисляем контрольную сумму XOR для принятых данных
|
|
||||||
// uint8_t calculated_checksum = calculateXORChecksum((byte*)data, length);
|
|
||||||
// Serial.println();
|
|
||||||
// Serial.println(received_checksum);
|
|
||||||
// // проверяем, совпадает ли принятая контрольная сумма с вычисленной
|
|
||||||
// if (received_checksum == calculated_checksum) {
|
|
||||||
// Serial.println("\nChecksum is correct");
|
|
||||||
|
|
||||||
// } else {
|
|
||||||
// Serial.println("\nChecksum is incorrect");
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
@ -77,7 +59,7 @@ byte calculateXORChecksum(byte *data, int length) {
|
|||||||
byte crc = 0x00;
|
byte crc = 0x00;
|
||||||
byte poly = 0x07; // полином для CRC8
|
byte poly = 0x07; // полином для CRC8
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length-2; i++) {
|
||||||
crc ^= data[i]; // XOR текущего байта с crc
|
crc ^= data[i]; // XOR текущего байта с crc
|
||||||
|
|
||||||
for (int j = 0; j < length; j++) {
|
for (int j = 0; j < length; j++) {
|
||||||
@ -92,53 +74,21 @@ byte calculateXORChecksum(byte *data, int length) {
|
|||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
char data[8] = {6, 1, 3, 8, 8, 8, 8, 0};
|
char data[] = {6, 1, 3, 5, 16, 17, 18, 0};
|
||||||
|
|
||||||
int length = sizeof(data);
|
int length = sizeof(data);
|
||||||
|
|
||||||
const int last = data [ (sizeof(data) / sizeof(data[0]))-1];
|
const int last = data [ (sizeof(data) / sizeof(data[0]))-1];
|
||||||
|
|
||||||
byte checking = calculateXORChecksum(data, length);
|
byte checking = calculateXORChecksum(data, length-1);
|
||||||
int size = sizeof(data) / sizeof(data[0]);
|
int size = sizeof(data) / sizeof(data[0]);
|
||||||
data[size - 1] = checking;
|
data[size - 1] = checking;
|
||||||
Print(data, size);
|
Print(data, size);
|
||||||
// Serial.println(last);
|
// Serial.println(last);
|
||||||
|
|
||||||
SPI_MasterTransmit(data, length);
|
SPI_MasterTransmit(data, length);
|
||||||
_delay_ms(1000);
|
_delay_ms(1000);
|
||||||
|
|
||||||
char data2[8] = {6, 9, 7, 10, 8, 8, 8, 0};
|
|
||||||
int length2 = sizeof(data2);
|
|
||||||
const int last2 = data2 [ (sizeof(data2) / sizeof(data2[0]))-1];
|
|
||||||
byte checking2 = calculateXORChecksum(data2, length2);
|
|
||||||
int size2 = sizeof(data2) / sizeof(data2[0]);
|
|
||||||
data2[size2 - 1] = checking2;
|
|
||||||
Print(data2, size2);
|
|
||||||
SPI_MasterTransmit(data2, length2);
|
|
||||||
|
|
||||||
_delay_ms(1000);
|
|
||||||
|
|
||||||
char data3[8] = {6, 11, 12, 13, 8, 8, 8, 0};
|
|
||||||
int length3 = sizeof(data3);
|
|
||||||
const int last3 = data3 [ (sizeof(data3) / sizeof(data3[0]))-1];
|
|
||||||
byte checking3 = calculateXORChecksum(data3, length3);
|
|
||||||
int size3 = sizeof(data3) / sizeof(data3[0]);
|
|
||||||
data3[size3 - 1] = checking3;
|
|
||||||
Print(data3, size3);
|
|
||||||
SPI_MasterTransmit(data3, length3);
|
|
||||||
|
|
||||||
_delay_ms(1000);
|
|
||||||
// ожидаем ответа
|
|
||||||
// while (!(SPSR & (1 << SPIF)));
|
|
||||||
// byte response = SPDR;
|
|
||||||
// if (response == 0x55) { // 0x55 - успешный ответ
|
|
||||||
// Serial.println("Command executed successfully");
|
|
||||||
// } else if (response == 0xAA) { // 0xAA - ошибка контрольной суммы
|
|
||||||
// Serial.println("Error: Checksum does not match");
|
|
||||||
// } else { // другие значения - неизвестный ответ
|
|
||||||
// Serial.println("Error: Unknown response");
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#include <GyverOLED.h>
|
// #include <GyverOLED.h>
|
||||||
|
|
||||||
#define DDR_SPI DDRB
|
#define DDR_SPI DDRB
|
||||||
#define DD_MOSI PB3
|
#define DD_MOSI PB3
|
||||||
@ -8,66 +8,62 @@
|
|||||||
#define DD_SCK PB5
|
#define DD_SCK PB5
|
||||||
#define DD_SS PB2
|
#define DD_SS PB2
|
||||||
|
|
||||||
GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;
|
// GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;
|
||||||
|
|
||||||
static int index = 0;
|
static int index = 0;
|
||||||
static int arIndex = 0;
|
static int arIndex = 0;
|
||||||
static byte data[255];
|
static byte data[255];
|
||||||
static byte ar1[5];
|
|
||||||
static byte ar2[5];
|
|
||||||
static byte ar3[5];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SPI_SlaveInit(void)
|
void SPI_SlaveInit(void)
|
||||||
{
|
{
|
||||||
DDR_SPI = (1 << DD_MISO);
|
DDR_SPI = (1 << DD_MISO);
|
||||||
|
|
||||||
SPCR = (1 << SPE) | (1 << SPIE);
|
SPCR = (1 << SPE) | (1 << SPIE);
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(SPI_STC_vect)
|
ISR(SPI_STC_vect)
|
||||||
{
|
{
|
||||||
char received = SPDR;
|
char received = SPDR;
|
||||||
data[index] = received;
|
data[index] = received;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(2000000);
|
Serial.begin(2000000);
|
||||||
SPI_SlaveInit();
|
SPI_SlaveInit();
|
||||||
oled.init(); // инициализация
|
// oled.init(); // инициализация
|
||||||
oled.clear(); // очистка
|
// oled.clear(); // очистка
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("Initialization ");
|
Serial.println("Initialization ");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetCommand(byte *data2){
|
void SetCommand(byte *data2){
|
||||||
byte command = data2[0];
|
byte command = data2[0];
|
||||||
// Отрезать 1 элемент от оставшихся
|
// Отрезать 1 элемент от оставшихся
|
||||||
byte result[index];
|
byte result[index];
|
||||||
byte *ptr = &data2[1];
|
byte *ptr = &data2[1];
|
||||||
strcpy(result, ptr);
|
strcpy(result, ptr);
|
||||||
Serial.print("\nReceived command: ");
|
Serial.print("\nReceived command: ");
|
||||||
Serial.println(command);
|
Serial.println(command);
|
||||||
switch (command){
|
switch (command){
|
||||||
case 6:
|
case 6:
|
||||||
AddSymbol(result);
|
AddSymbol(result);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AddSymbol(byte *symbols){
|
void AddSymbol(byte *symbols){
|
||||||
Serial.print("Received Add: ");
|
Serial.print("Received Add: ");
|
||||||
|
|
||||||
oled.setScale(3); // масштаб текста (1..4)
|
// oled.setScale(3); // масштаб текста (1..4)
|
||||||
oled.home(); // курсор в 0,0
|
// oled.home(); // курсор в 0,0
|
||||||
oled.print("Привет!");
|
// oled.print("Привет!");
|
||||||
for(int i = 0; i < index - 1; i++) {
|
for(int i = 0; i < index - 1; i++) {
|
||||||
Serial.print(symbols[i]);
|
Serial.print(symbols[i], HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,18 +71,29 @@ void AddSymbol(byte *symbols){
|
|||||||
|
|
||||||
// Функция для вычисления контрольной суммы XOR
|
// Функция для вычисления контрольной суммы XOR
|
||||||
byte calculateXORChecksum(byte *data, int length) {
|
byte calculateXORChecksum(byte *data, int length) {
|
||||||
byte checksum = 0;
|
byte crc = 0x00;
|
||||||
for (int i = 0; i < length - 1; i++) {
|
byte poly = 0x07; // полином для CRC8
|
||||||
checksum ^= data[i];
|
|
||||||
}
|
for (int i = 0; i < length-2; i++) {
|
||||||
return checksum;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Вывод массива
|
// Вывод массива
|
||||||
void arrayOut(byte *arr,int size){
|
void arrayOut(byte *arr,int size){
|
||||||
Serial.print("Array: ");
|
Serial.print("Array: ");
|
||||||
for(int i = 0;i<size;i++){
|
for(int i = 0; i < size;i++){
|
||||||
Serial.print(arr[i]);
|
Serial.print(arr[i], HEX);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
}
|
}
|
||||||
Serial.println(".");
|
Serial.println(".");
|
||||||
@ -102,90 +109,35 @@ bool checkArray(byte* arr, int size) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if(PINB & (1 << 2))
|
if(PINB & (1 << 2)){
|
||||||
{
|
if(index > 0){
|
||||||
if(index > 0)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (arIndex == 0){
|
|
||||||
arIndex = 1;
|
|
||||||
memcpy(ar1,data,5);
|
|
||||||
}else{
|
|
||||||
if(arIndex == 1){
|
|
||||||
arIndex = 2;
|
|
||||||
memcpy(ar2,data,5);
|
|
||||||
}else{
|
|
||||||
arIndex = 0;
|
|
||||||
memcpy(ar3,data,5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
byte sum = 0;
|
byte sum = 0;
|
||||||
|
sum = calculateXORChecksum(data, index-1);
|
||||||
|
|
||||||
//arrayOut(ar1,5);
|
|
||||||
int lenght = 5;
|
|
||||||
sum = calculateXORChecksum(ar1, lenght);
|
|
||||||
bool checkNull = false;
|
bool checkNull = false;
|
||||||
|
byte last_1 = data[index - 1];
|
||||||
byte last_1 = ar1[index - 1];
|
//arrayOut(data, index);
|
||||||
if (last_1 == sum)
|
|
||||||
{
|
if (last_1 == sum){
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("Старт вывода массивов");
|
Serial.println("Старт вывода массивов");
|
||||||
Serial.println(sum);
|
Serial.println(sum, HEX);
|
||||||
checkNull = checkArray(ar1, lenght);
|
checkNull = checkArray(data, index);
|
||||||
if(checkNull == true)
|
|
||||||
{
|
if(checkNull == true){
|
||||||
SetCommand(ar1);
|
SetCommand(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("Стоп вывода массивов");
|
Serial.println("Стоп вывода массивов");
|
||||||
index = 0;
|
index = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//arrayOut(ar2,5);
|
|
||||||
sum = calculateXORChecksum(ar2, lenght);
|
|
||||||
|
|
||||||
byte last_2 = ar2[index - 1];
|
|
||||||
if (last_2 == sum)
|
|
||||||
{
|
|
||||||
Serial.println();
|
|
||||||
Serial.println("Старт вывода массивов");
|
|
||||||
Serial.println(sum);
|
|
||||||
checkNull = checkArray(ar2, lenght);
|
|
||||||
if(checkNull == true)
|
|
||||||
{
|
|
||||||
SetCommand(ar2);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Стоп вывода массивов");
|
|
||||||
index = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//arrayOut(ar3,5);
|
|
||||||
sum = calculateXORChecksum(ar3, lenght);
|
|
||||||
|
|
||||||
byte last_3 = ar3[index - 1];
|
|
||||||
if (last_3 == sum)
|
|
||||||
{
|
|
||||||
Serial.println();
|
|
||||||
Serial.println("Старт вывода массивов");
|
|
||||||
Serial.println(sum);
|
|
||||||
checkNull = checkArray(ar3, lenght);
|
|
||||||
if(checkNull == true)
|
|
||||||
{
|
|
||||||
SetCommand(ar3);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Стоп вывода массивов");
|
|
||||||
index = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Nothing ...");
|
Serial.println("Nothing ...");
|
||||||
index=0;
|
|
||||||
|
index = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user