Skip to content

Commit abc6930

Browse files
authored
Merge pull request #25 from MrYsLab/spi_changes_2
Updated SPI support
2 parents e9cd616 + de2539b commit abc6930

File tree

2 files changed

+81
-19
lines changed

2 files changed

+81
-19
lines changed

examples/Telemetrix4Arduino/Telemetrix4Arduino.ino

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2020-2021 Alan Yorinks All rights reserved.
2+
Copyright (c) 2020-2023 Alan Yorinks All rights reserved.
33
44
This program is free software; you can redistribute it and/or
55
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@@ -10,7 +10,7 @@
1010
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111
General Public License for more details.
1212
13-
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSEf
13+
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
1414
along with this library; if not, write to the Free Software
1515
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1616
*/
@@ -181,6 +181,9 @@
181181
#define SONAR_SCAN_OFF 55
182182
#define SONAR_SCAN_ON 56
183183

184+
#define BOARD_HARD_RESET 57
185+
186+
184187

185188
/* Command Forward References*/
186189

@@ -303,6 +306,8 @@ extern void sonar_disable();
303306

304307
extern void sonar_enable();
305308

309+
extern void board_hard_reset();
310+
306311
// When adding a new command update the command_table.
307312
// The command length is the number of bytes that follow
308313
// the command byte itself, and does not include the command
@@ -378,6 +383,7 @@ command_descriptor command_table[] =
378383
(&get_features),
379384
(&sonar_disable),
380385
(&sonar_enable),
386+
(&board_hard_reset),
381387
};
382388

383389

@@ -425,6 +431,17 @@ byte i2c_report_message[64];
425431

426432
#ifdef SPI_ENABLED
427433
byte spi_report_message[64];
434+
435+
// SPI settings
436+
uint32_t spi_clock_freq = F_CPU / 4;
437+
uint8_t spi_clock_divider = 4;
438+
#if defined (__AVR__)
439+
uint8_t spi_bit_order = MSBFIRST;
440+
#else
441+
BitOrder spi_bit_order = MSBFIRST;
442+
#endif
443+
uint8_t spi_mode= SPI_MODE0;
444+
428445
#endif
429446

430447
bool stop_reports = false; // a flag to stop sending all report messages
@@ -445,11 +462,10 @@ bool sonar_reporting_enabled = true; // flag to start and stop sonar reporing
445462

446463
// firmware version - update this when bumping the version
447464
#define FIRMWARE_MAJOR 5
448-
#define FIRMWARE_MINOR 2
465+
#define FIRMWARE_MINOR 3
449466
#define FIRMWARE_PATCH 1
450467

451468

452-
453469
// Feature Masks And Storage
454470

455471
#define ONEWIRE_FEATURE 0x01
@@ -552,6 +568,8 @@ TwoWire *current_i2c_port;
552568
// equivalent, this array is used to look up the value to use for the pin.
553569
#ifdef ARDUINO_SAMD_MKRWIFI1010
554570
int analog_read_pins[20] = {A0, A1, A2, A3, A4, A5, A6};
571+
#elif ARDUINO_FSP
572+
int analog_read_pins[20] = {A0, A1, A2, A3, A4, A5};
555573
#else
556574
int analog_read_pins[20] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
557575
#endif
@@ -1093,6 +1111,13 @@ void sonar_enable(){
10931111
sonar_reporting_enabled = true;
10941112
}
10951113

1114+
void board_hard_reset(){
1115+
#if defined (ARDUINO_FSP)
1116+
NVIC_SystemReset();
1117+
delay(2000);
1118+
#endif
1119+
}
1120+
10961121
/***********************************
10971122
DHT adding a new device
10981123
**********************************/
@@ -1134,17 +1159,37 @@ void init_spi() {
11341159
// write a number of blocks to the SPI device
11351160
void write_blocking_spi() {
11361161
#ifdef SPI_ENABLED
1137-
int num_bytes = command_buffer[0];
1162+
int num_bytes = command_buffer[1];
11381163

1164+
//send_debug_info(4, spi_bit_order);
1165+
//send_debug_info(5, spi_mode);
1166+
SPI.beginTransaction(SPISettings(spi_clock_freq, spi_bit_order, spi_mode));
1167+
digitalWrite(command_buffer[0], 0);
11391168
for (int i = 0; i < num_bytes; i++) {
1140-
SPI.transfer(command_buffer[1 + i] );
1169+
SPI.transfer(command_buffer[2 + i] );
11411170
}
1171+
digitalWrite(command_buffer[0], 1);
1172+
1173+
SPI.endTransaction();
11421174
#endif
11431175
}
11441176

11451177
// read a number of bytes from the SPI device
11461178
void read_blocking_spi() {
11471179
#ifdef SPI_ENABLED
1180+
1181+
spi_report_message[0] = command_buffer[1] + 4; // packet length
1182+
spi_report_message[1] = SPI_REPORT;
1183+
spi_report_message[2] = command_buffer[0]; // chip select pin
1184+
spi_report_message[3] = command_buffer[2]; // register
1185+
spi_report_message[4] = command_buffer[1]; // number of bytes read
1186+
1187+
//send_debug_info(command_buffer[0], 0);
1188+
//send_debug_info(command_buffer[1], 1);
1189+
//send_debug_info(command_buffer[2], 2);
1190+
//send_debug_info(command_buffer[3], 3);
1191+
1192+
SPI.beginTransaction(SPISettings(spi_clock_freq, spi_bit_order, spi_mode));
11481193
// command_buffer[0] == number of bytes to read
11491194
// command_buffer[1] == read register
11501195

@@ -1156,29 +1201,39 @@ void read_blocking_spi() {
11561201

11571202
// configure the report message
11581203
// calculate the packet length
1159-
spi_report_message[0] = command_buffer[0] + 3; // packet length
1160-
spi_report_message[1] = SPI_REPORT;
1161-
spi_report_message[2] = command_buffer[1]; // register
1162-
spi_report_message[3] = command_buffer[0]; // number of bytes read
1204+
digitalWrite(command_buffer[0], 0);
1205+
1206+
// write the register out. OR it with 0x80 to indicate a read
11631207

1164-
// write the register out. OR it with 0x80 to indicate a read
1165-
SPI.transfer(command_buffer[1] | 0x80);
1208+
SPI.transfer(command_buffer[2] | 0x80);
1209+
delay(100);
11661210

11671211
// now read the specified number of bytes and place
11681212
// them in the report buffer
1169-
for (int i = 0; i < command_buffer[0] ; i++) {
1170-
spi_report_message[i + 4] = SPI.transfer(0x00);
1213+
for (int i = 0; i < command_buffer[1] ; i++) {
1214+
spi_report_message[i + 5] = SPI.transfer(0x00);
1215+
11711216
}
1172-
Serial.write(spi_report_message, command_buffer[0] + 4);
1217+
//send_debug_info(SPI.transfer(0x00), 2);
1218+
digitalWrite(command_buffer[0], 1);
1219+
1220+
SPI.endTransaction();
1221+
1222+
Serial.write(spi_report_message, command_buffer[1] + 5);
1223+
11731224
#endif
11741225
}
11751226

11761227
// modify the SPI format
11771228
void set_format_spi() {
11781229
#ifdef SPI_ENABLED
11791230

1231+
spi_clock_freq = F_CPU / command_buffer[0];
1232+
spi_mode = command_buffer[2];
1233+
11801234
#if defined(__AVR__)
1181-
SPISettings(command_buffer[0], command_buffer[1], command_buffer[2]);
1235+
spi_bit_order = command_buffer[1] ;
1236+
//SPISettings(command_buffer[0], command_buffer[1], command_buffer[2]);
11821237
#else
11831238
BitOrder b;
11841239

@@ -1187,7 +1242,7 @@ void set_format_spi() {
11871242
} else {
11881243
b = LSBFIRST;
11891244
}
1190-
SPISettings(command_buffer[0], b, command_buffer[2]);
1245+
spi_bit_order = b;
11911246
#endif // avr
11921247
#endif // SPI_ENABLED
11931248
}
@@ -2045,6 +2100,13 @@ void setup()
20452100
init_pin_structures();
20462101

20472102
Serial.begin(115200);
2103+
pinMode(13, OUTPUT);
2104+
for( int i = 0; i < 4; i++){
2105+
digitalWrite(13, HIGH);
2106+
delay(250);
2107+
digitalWrite(13, LOW);
2108+
delay(250);
2109+
}
20482110
}
20492111

20502112
void loop()
@@ -2071,4 +2133,4 @@ void loop()
20712133
run_steppers();
20722134
#endif
20732135
}
2074-
}
2136+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Telemetrix4Arduino
2-
version=5.2.1
2+
version=5.3.1
33
author=Alan Yorinks
44
maintainer=https://github.com/MrYsLab/
55
sentence=The server for the Telemetrix Project.

0 commit comments

Comments
 (0)