Переход на C
Пока не работает
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#include "aSPISlave.h"
|
||||
#include "bCommands.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(2000000);
|
||||
SPI_SlaveInit();
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Initialization ");
|
||||
}
|
||||
|
||||
void processSPI(void (*callback)(char*)) {
|
||||
if (PINB & (1 << 2)) {
|
||||
if (index > 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);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "aSPISlave.h"
|
||||
#include <avr/io.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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) {
|
||||
// Выполнение команды
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user