Переход на C
Пока не работает
This commit is contained in:
parent
a71fd231fc
commit
6b39c55d91
37
SPI/SPI-Master/SPI-Master.ino
Normal file
37
SPI/SPI-Master/SPI-Master.ino
Normal file
@ -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);
|
||||
}
|
45
SPI/SPI-Master/SPIMaster.c
Normal file
45
SPI/SPI-Master/SPIMaster.c
Normal file
@ -0,0 +1,45 @@
|
||||
|
||||
#include "SPIMaster.h"
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
void SPI_MasterInit(void)
|
||||
{
|
||||
DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS);
|
||||
DDRB |= (1<<DDB2);
|
||||
SPCR = (1 << SPE) | (1 << MSTR);
|
||||
}
|
||||
|
||||
void SPI_MasterTransmit(char *data, int length)
|
||||
{
|
||||
PORTB &= ~(1<<2);
|
||||
|
||||
int size = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
for(int i = 0; i < length; i++) {
|
||||
SPDR = data[i]; // отправляем байт массива
|
||||
while (!(SPSR & (1 << SPIF))); // ждем, пока байт передастся
|
||||
}
|
||||
|
||||
PORTB |= (1<<2);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
22
SPI/SPI-Master/SPIMaster.h
Normal file
22
SPI/SPI-Master/SPIMaster.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef SPIMaster_h
|
||||
#define SPIMaster_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_MasterInit(void);
|
||||
void SPI_MasterTransmit(char *data, int length);
|
||||
char CRC8(char *data, int length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -13,11 +13,9 @@ void SPI_MasterInit(void)
|
||||
DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS);
|
||||
DDRB |= (1<<DDB2); // set SS as output
|
||||
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(char *data2, int lenght){
|
||||
Serial.println("----");
|
||||
for(int i = 0; i < lenght; i++) {
|
||||
Serial.print(data2[i], HEX);
|
||||
@ -29,35 +27,30 @@ void SPI_MasterTransmit(char *data, int length)
|
||||
{
|
||||
PORTB &= ~(1<<2);
|
||||
|
||||
// Serial.println();
|
||||
int size = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
if (data[size - 1] == SPDR){
|
||||
Serial.println("Privet") ;
|
||||
}
|
||||
byte check = 0;
|
||||
byte spdr[length];
|
||||
Serial.println();
|
||||
// Serial.println();
|
||||
|
||||
for(int i = 0; i < length; i++) {
|
||||
SPDR = data[i]; // отправляем байт массива
|
||||
Serial.print(data[i], HEX);
|
||||
Serial.print(" ");
|
||||
// Serial.print(data[i], HEX);
|
||||
// Serial.print(" ");
|
||||
while (!(SPSR & (1 << SPIF))); // ждем, пока байт передастся
|
||||
}
|
||||
|
||||
PORTB |= (1<<2);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.begin(2000000);
|
||||
SPI_MasterInit();
|
||||
Serial.println("Master Initialization ");
|
||||
}
|
||||
|
||||
// Функция для вычисления контрольной суммы 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
|
||||
@ -77,13 +70,13 @@ byte calculateXORChecksum(byte *data, int length) {
|
||||
|
||||
void loop()
|
||||
{
|
||||
char data[] = {6, 1, 3, 5, 16, 17, 18, 0};
|
||||
char data[] = {6, 1, 2, 3, 4, 5, 7, 0};
|
||||
|
||||
int length = sizeof(data);
|
||||
|
||||
const int last = data [ (sizeof(data) / sizeof(data[0]))-1];
|
||||
|
||||
byte checking = calculateXORChecksum(data, length-1);
|
||||
char checking = CRC8(data, length-1);
|
||||
int size = sizeof(data) / sizeof(data[0]);
|
||||
data[size - 1] = checking;
|
||||
Print(data, size);
|
29
SPI/slave/SPI-Slave/SPI-Slave.ino
Normal file
29
SPI/slave/SPI-Slave/SPI-Slave.ino
Normal file
@ -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);
|
||||
}
|
32
SPI/slave/SPI-Slave/aSPISlave.c
Normal file
32
SPI/slave/SPI-Slave/aSPISlave.c
Normal file
@ -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;
|
||||
}
|
24
SPI/slave/SPI-Slave/aSPISlave.h
Normal file
24
SPI/slave/SPI-Slave/aSPISlave.h
Normal file
@ -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
|
17
SPI/slave/SPI-Slave/bCommands.c
Normal file
17
SPI/slave/SPI-Slave/bCommands.c
Normal file
@ -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) {
|
||||
// Выполнение команды
|
||||
}
|
15
SPI/slave/SPI-Slave/bCommands.h
Normal file
15
SPI/slave/SPI-Slave/bCommands.h
Normal file
@ -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
|
42
SPI/slave/SPISlave.c
Normal file
42
SPI/slave/SPISlave.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <avr/io.h>
|
||||
#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;
|
||||
}
|
23
SPI/slave/SPISlave.h
Normal file
23
SPI/slave/SPISlave.h
Normal file
@ -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
|
@ -1,6 +1,6 @@
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
// #include <avr/io.h>
|
||||
// #include <GyverOLED.h>
|
||||
#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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user