1
1
/*
2
- Copyright (c) 2020-2021 Alan Yorinks All rights reserved.
2
+ Copyright (c) 2020-2023 Alan Yorinks All rights reserved.
3
3
4
4
This program is free software; you can redistribute it and/or
5
5
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
11
General Public License for more details.
12
12
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
14
14
along with this library; if not, write to the Free Software
15
15
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
16
*/
181
181
#define SONAR_SCAN_OFF 55
182
182
#define SONAR_SCAN_ON 56
183
183
184
+ #define BOARD_HARD_RESET 57
185
+
186
+
184
187
185
188
/* Command Forward References*/
186
189
@@ -303,6 +306,8 @@ extern void sonar_disable();
303
306
304
307
extern void sonar_enable ();
305
308
309
+ extern void board_hard_reset ();
310
+
306
311
// When adding a new command update the command_table.
307
312
// The command length is the number of bytes that follow
308
313
// the command byte itself, and does not include the command
@@ -378,6 +383,7 @@ command_descriptor command_table[] =
378
383
(&get_features),
379
384
(&sonar_disable),
380
385
(&sonar_enable),
386
+ (&board_hard_reset),
381
387
};
382
388
383
389
@@ -425,6 +431,17 @@ byte i2c_report_message[64];
425
431
426
432
#ifdef SPI_ENABLED
427
433
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
+
428
445
#endif
429
446
430
447
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
445
462
446
463
// firmware version - update this when bumping the version
447
464
#define FIRMWARE_MAJOR 5
448
- #define FIRMWARE_MINOR 2
465
+ #define FIRMWARE_MINOR 3
449
466
#define FIRMWARE_PATCH 1
450
467
451
468
452
-
453
469
// Feature Masks And Storage
454
470
455
471
#define ONEWIRE_FEATURE 0x01
@@ -552,6 +568,8 @@ TwoWire *current_i2c_port;
552
568
// equivalent, this array is used to look up the value to use for the pin.
553
569
#ifdef ARDUINO_SAMD_MKRWIFI1010
554
570
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};
555
573
#else
556
574
int analog_read_pins[20 ] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
557
575
#endif
@@ -1093,6 +1111,13 @@ void sonar_enable(){
1093
1111
sonar_reporting_enabled = true ;
1094
1112
}
1095
1113
1114
+ void board_hard_reset (){
1115
+ #if defined (ARDUINO_FSP)
1116
+ NVIC_SystemReset ();
1117
+ delay (2000 );
1118
+ #endif
1119
+ }
1120
+
1096
1121
/* **********************************
1097
1122
DHT adding a new device
1098
1123
**********************************/
@@ -1134,17 +1159,37 @@ void init_spi() {
1134
1159
// write a number of blocks to the SPI device
1135
1160
void write_blocking_spi () {
1136
1161
#ifdef SPI_ENABLED
1137
- int num_bytes = command_buffer[0 ];
1162
+ int num_bytes = command_buffer[1 ];
1138
1163
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 );
1139
1168
for (int i = 0 ; i < num_bytes; i++) {
1140
- SPI.transfer (command_buffer[1 + i] );
1169
+ SPI.transfer (command_buffer[2 + i] );
1141
1170
}
1171
+ digitalWrite (command_buffer[0 ], 1 );
1172
+
1173
+ SPI.endTransaction ();
1142
1174
#endif
1143
1175
}
1144
1176
1145
1177
// read a number of bytes from the SPI device
1146
1178
void read_blocking_spi () {
1147
1179
#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));
1148
1193
// command_buffer[0] == number of bytes to read
1149
1194
// command_buffer[1] == read register
1150
1195
@@ -1156,29 +1201,39 @@ void read_blocking_spi() {
1156
1201
1157
1202
// configure the report message
1158
1203
// 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
1163
1207
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 );
1166
1210
1167
1211
// now read the specified number of bytes and place
1168
1212
// 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
+
1171
1216
}
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
+
1173
1224
#endif
1174
1225
}
1175
1226
1176
1227
// modify the SPI format
1177
1228
void set_format_spi () {
1178
1229
#ifdef SPI_ENABLED
1179
1230
1231
+ spi_clock_freq = F_CPU / command_buffer[0 ];
1232
+ spi_mode = command_buffer[2 ];
1233
+
1180
1234
#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]);
1182
1237
#else
1183
1238
BitOrder b;
1184
1239
@@ -1187,7 +1242,7 @@ void set_format_spi() {
1187
1242
} else {
1188
1243
b = LSBFIRST;
1189
1244
}
1190
- SPISettings (command_buffer[ 0 ], b, command_buffer[ 2 ]) ;
1245
+ spi_bit_order = b ;
1191
1246
#endif // avr
1192
1247
#endif // SPI_ENABLED
1193
1248
}
@@ -2045,6 +2100,13 @@ void setup()
2045
2100
init_pin_structures ();
2046
2101
2047
2102
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
+ }
2048
2110
}
2049
2111
2050
2112
void loop ()
@@ -2071,4 +2133,4 @@ void loop()
2071
2133
run_steppers ();
2072
2134
#endif
2073
2135
}
2074
- }
2136
+ }
0 commit comments