-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapp_httpd.cpp
1921 lines (1557 loc) · 56.7 KB
/
app_httpd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------
// Copyright © 2020 Raymund Sarmiento
//
// Permission is hereby granted to use this Software for any purpose
// including combining with commercial products, creating derivative
// works, and redistribution of source or binary code, without
// limitation or consideration. Any redistributed copies of this
// Software must include the above Copyright Notice.
//
// THIS SOFTWARE IS PROVIDED "AS IS". THE AUTHOR OF THIS CODE MAKES NO
// WARRANTIES REGARDING THIS SOFTWARE, EXPRESS OR IMPLIED, AS TO ITS
// SUITABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// DISCLAIMER:
// You can use the information on this site COMPLETELY AT YOUR OWN RISK.
// The modification steps and other information on this site is provided
// to you "AS IS" and WITHOUT WARRANTY OF ANY KIND, express, statutory,
// implied or otherwise, including without limitation any warranty of
// merchantability or fitness for any particular or intended purpose.
// In no event the author will be liable for any direct, indirect,
// punitive, special, incidental or consequential damages or loss of any
// kind whether or not the author has been advised of the possibility
// of such loss.
//---------------------------------------------------------------------
#ifndef COMPONENTS_PERFMON_INCLUDE_PERFMON_H_
#define COMPONENTS_PERFMON_INCLUDE_PERFMON_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "esp_err.h"
esp_err_t perfmon_start();
esp_err_t perfmon_stop();
#ifdef __cplusplus
}
#endif
#endif /* COMPONENTS_PERFMON_INCLUDE_PERFMON_H_ */
#include "esp_http_server.h"
#include "SPIFFS.h"
#include "SdFat.h"
SdFs SD;
const uint8_t SD_CS_PIN = SS;
// Slow or Fast Cards
//#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, 100000)
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, 25000000)
#define MAXFILESIZE 40000
#define MAXFILESIZE2 200000
#define FLASHSIZE 512
#define BLINK2 2
#define blinkDelay 50
#define LEDPIN0 GPIO_NUM_2
#define ETAGVAL "1667595595"
#define MAXAGEVAL "max-age=7200, public"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_freertos_hooks.h"
#include "sdkconfig.h"
#include "esp_log.h"
static const char *TAG = "obd_httpd";
static uint64_t idle0Calls = 0;
static uint64_t idle1Calls = 0;
static int cpuload_offset = 0;
static int perfmonflag = 0;
httpd_handle_t obd_httpd = NULL;
#define WSCMDCNT 6 // Number of commands below
#define ToTPIDStack 500
#define conncount 20
#define FD_CMD_001 "speed_rpm200"
#define FD_CID_001 01
#define FD_DUR_001 200
#define FD_CMD_002 "all_value2000"
#define FD_CID_002 02
#define FD_DUR_002 2000
#define FD_CMD_003 "all_value3000"
#define FD_CID_003 03
#define FD_DUR_003 3000
#define FD_CMD_004 "tpms_2000"
#define FD_CID_004 04
#define FD_DUR_004 2000
#define FD_CMD_005 "bm2_1000"
#define FD_CID_005 05
#define FD_DUR_005 1000
#define FD_CMD_098 "cpu_stats"
#define FD_CID_098 98
#define FD_DUR_098 500
#define FD_CMD_099 "speed_rpm500"
#define FD_CID_099 99
#define FD_DUR_099 500
#define FD_CID_DIS -1
#define FD_DUR_DIS 0
static const size_t max_clients = 20;
#define GraphQueueCount 10
#define CPUGraphCount 20
int wscnt[WSCMDCNT+1];
float p1[GraphQueueCount+1];
float p2[GraphQueueCount+1];
float c0[CPUGraphCount+1];
float c1[CPUGraphCount+1];
unsigned long perfmillis;
// Start addresses on DUP (Increased buffer size improves performance)
#define ADDR_BUF0 0x0000 // Buffer (512 bytes)
#define ADDR_DMA_DESC_0 0x0200 // DMA descriptors (8 bytes)
#define ADDR_DMA_DESC_1 (ADDR_DMA_DESC_0 + 8)
// DMA channels used on DUP
#define CH_DBG_TO_BUF0 0x01 // Channel 0
#define CH_BUF0_TO_FLASH 0x02 // Channel 1
// Debug commands
#define CMD_CHIP_ERASE 0x10
#define CMD_WR_CONFIG 0x19
#define CMD_RD_CONFIG 0x24
#define CMD_READ_STATUS 0x30
#define CMD_RESUME 0x4C
#define CMD_DEBUG_INSTR_1B (0x54|1)
#define CMD_DEBUG_INSTR_2B (0x54|2)
#define CMD_DEBUG_INSTR_3B (0x54|3)
#define CMD_BURST_WRITE 0x80
#define CMD_GET_CHIP_ID 0x68
// Debug status bitmasks
#define STATUS_CHIP_ERASE_BUSY_BM 0x80 // New debug interface
#define STATUS_PCON_IDLE_BM 0x40
#define STATUS_CPU_HALTED_BM 0x20
#define STATUS_PM_ACTIVE_BM 0x10
#define STATUS_HALT_STATUS_BM 0x08
#define STATUS_DEBUG_LOCKED_BM 0x04
#define STATUS_OSC_STABLE_BM 0x02
#define STATUS_STACK_OVERFLOW_BM 0x01
// DUP registers (XDATA space address)
#define DUP_DBGDATA 0x6260 // Debug interface data buffer
#define DUP_FCTL 0x6270 // Flash controller
#define DUP_FADDRL 0x6271 // Flash controller addr
#define DUP_FADDRH 0x6272 // Flash controller addr
#define DUP_FWDATA 0x6273 // Clash controller data buffer
#define DUP_CLKCONSTA 0x709E // Sys clock status
#define DUP_CLKCONCMD 0x70C6 // Sys clock configuration
#define DUP_MEMCTR 0x70C7 // Flash bank xdata mapping
#define DUP_DMA1CFGL 0x70D2 // Low byte, DMA config ch. 1
#define DUP_DMA1CFGH 0x70D3 // Hi byte , DMA config ch. 1
#define DUP_DMA0CFGL 0x70D4 // Low byte, DMA config ch. 0
#define DUP_DMA0CFGH 0x70D5 // Low byte, DMA config ch. 0
#define DUP_DMAARM 0x70D6 // DMA arming register
// Utility macros
//! Low nibble of 16bit variable
#define LOBYTE(w) ((unsigned char)(w))
//! High nibble of 16bit variable
#define HIBYTE(w) ((unsigned char)(((unsigned short)(w) >> 8) & 0xFF))
//! Convert XREG register declaration to an XDATA integer address
//#define XREG(addr) ((unsigned char volatile __xdata *) 0)[addr]
//#define FCTL XREG( 0x6270 )
//#define XREG_TO_INT(a) ((unsigned short)(&(a)))
// Debug control pins & the indicate LED
int CC2541DD = GPIO_NUM_25; //GPIO14=D5 on NodeMCU/WeMos D1 Mini
int CC2541DC = GPIO_NUM_26; //GPIO4=D2 on NodeMCU/WeMos D1 Mini
int CC2541RESET = GPIO_NUM_27; //GPIO5=D1 on NodeMCU/WeMos D1 Mini
#define tireFL 0x1006b5
#define tireFR 0x2006b6
#define tireRL 0x3006b7
#define tireRR 0x4006b8
#define tireSP 0x5006b9
#define TPMS_COUNT 5
float tpms_pressure[TPMS_COUNT];
float tpms_temperature[TPMS_COUNT];
float tpms_voltage[TPMS_COUNT];
float bm2_voltage = 0;
// Handle BM2 Data
void store_BM2( float voltage)
{
bm2_voltage = voltage;
}
// Handle TPMS Data
void store_TPMS(unsigned long tireID, float pressure, float temperature, float voltage)
{
//Serial.printf("tireID: %06x - Pressure: %5.2f psi, Temperature: %5.2f degC, Sensor Voltage: %4.2f V\n", tireID,pressure,temperature,voltage);
switch(tireID)
{
case tireFL:
tpms_pressure[0] = pressure;
tpms_temperature[0] = temperature;
tpms_voltage[0] = voltage;
break;
case tireFR:
tpms_pressure[1] = pressure;
tpms_temperature[1] = temperature;
tpms_voltage[1] = voltage;
break;
case tireRL:
tpms_pressure[2] = pressure;
tpms_temperature[2] = temperature;
tpms_voltage[2] = voltage;
break;
case tireRR:
tpms_pressure[3] = pressure;
tpms_temperature[3] = temperature;
tpms_voltage[3] = voltage;
break;
case tireSP:
tpms_pressure[4] = pressure;
tpms_temperature[4] = temperature;
tpms_voltage[4] = voltage;
break;
default:
break;
}
}
/******************************************************************************
VARIABLES*/
//! DUP DMA descriptor
const unsigned char dma_desc_0[8] =
{
// Debug Interface -> Buffer
HIBYTE(DUP_DBGDATA), // src[15:8]
LOBYTE(DUP_DBGDATA), // src[7:0]
HIBYTE(ADDR_BUF0), // dest[15:8]
LOBYTE(ADDR_BUF0), // dest[7:0]
0, // len[12:8] - filled in later
0, // len[7:0]
31, // trigger: DBG_BW
0x11 // increment destination
};
//! DUP DMA descriptor
const unsigned char dma_desc_1[8] =
{
// Buffer -> Flash controller
HIBYTE(ADDR_BUF0), // src[15:8]
LOBYTE(ADDR_BUF0), // src[7:0]
HIBYTE(DUP_FWDATA), // dest[15:8]
LOBYTE(DUP_FWDATA), // dest[7:0]
0, // len[12:8] - filled in later
0, // len[7:0]
18, // trigger: FLASH
0x42, // increment source
};
/**************************************************************************//**
* @brief Writes a byte on the debug interface. Requires DD to be
* output when function is called.
* @param data Byte to write
* @return None.
******************************************************************************/
#pragma inline
void write_debug_byte(unsigned char data)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
// Set clock high and put data on DD line
digitalWrite( CC2541DC, HIGH);
if(data & 0x80)
{
digitalWrite(CC2541DD, HIGH);
}
else
{
digitalWrite(CC2541DD, LOW);
}
data <<= 1;
digitalWrite(CC2541DC, LOW); // set clock low (DUP capture flank)
}
}
/**************************************************************************//**
* @brief Reads a byte from the debug interface. Requires DD to be
* input when function is called.
* @return Returns the byte read.
******************************************************************************/
#pragma inline
unsigned char read_debug_byte(void)
{
unsigned char i;
unsigned char data = 0x00;
for (i = 0; i < 8; i++)
{
digitalWrite(CC2541DC, HIGH); // DC high
data <<= 1;
if(HIGH == digitalRead(CC2541DD))
{
data |= 0x01;
}
digitalWrite(CC2541DC, LOW); // DC low
}
return data;
}
/**************************************************************************//**
* @brief Function waits for DUP to indicate that it is ready. The DUP will
* pulls DD line low when it is ready. Requires DD to be input when
* function is called.
* @return Returns 0 if function timed out waiting for DD line to go low
* @return Returns 1 when DUP has indicated it is ready.
******************************************************************************/
#pragma inline
unsigned char wait_dup_ready(void)
{
// DUP pulls DD low when ready
unsigned int count = 0;
while ((HIGH == digitalRead(CC2541DD)) && count < 16)
{
// Clock out 8 bits before checking if DD is low again
read_debug_byte();
count++;
}
return (count == 16) ? 0 : 1;
}
/**************************************************************************//**
* @brief Issues a command on the debug interface. Only commands that return
* one output byte are supported.
* @param cmd Command byte
* @param cmd_bytes Pointer to the array of data bytes following the
* command byte [0-3]
* @param num_cmd_bytes The number of data bytes (input to DUP) [0-3]
* @return Data returned by command
******************************************************************************/
unsigned char debug_command(unsigned char cmd, unsigned char *cmd_bytes,
unsigned short num_cmd_bytes)
{
unsigned short i;
unsigned char output = 0;
// Make sure DD is output
pinMode(CC2541DD, OUTPUT);
// Send command
write_debug_byte(cmd);
// Send bytes
for (i = 0; i < num_cmd_bytes; i++)
{
write_debug_byte(cmd_bytes[i]);
}
// Set DD as input
pinMode(CC2541DD, INPUT);
digitalWrite(CC2541DD, HIGH);
// Wait for data to be ready
wait_dup_ready();
// Read returned byte
output = read_debug_byte();
// Set DD as output
pinMode(CC2541DD, OUTPUT);
return output;
}
/**************************************************************************//**
* @brief Resets the DUP into debug mode. Function assumes that
* the programmer I/O has already been configured using e.g.
* ProgrammerInit().
* @return None.
******************************************************************************/
void debug_init(void)
{
volatile unsigned char i;
// Send two flanks on DC while keeping RESET_N low
// All low (incl. RESET_N)
digitalWrite(CC2541DD, LOW);
digitalWrite(CC2541DC, LOW);
digitalWrite(CC2541RESET, LOW);
delay(10); // Wait
digitalWrite(CC2541DC, HIGH); // DC high
delay(10); // Wait
digitalWrite(CC2541DC, LOW); // DC low
delay(10); // Wait
digitalWrite(CC2541DC, HIGH); // DC high
delay(10); // Wait
digitalWrite(CC2541DC, LOW); // DC low
delay(10); // Wait
digitalWrite(CC2541RESET, HIGH); // Release RESET_N
delay(10); // Wait
}
/**************************************************************************//**
* @brief Reads the chip ID over the debug interface using the
* GET_CHIP_ID command.
* @return Returns the chip id returned by the DUP
******************************************************************************/
unsigned char read_chip_id(void)
{
unsigned char id = 0;
// Make sure DD is output
pinMode(CC2541DD, OUTPUT);
delay(1);
// Send command
write_debug_byte(CMD_GET_CHIP_ID);
// Set DD as input
pinMode(CC2541DD, INPUT);
digitalWrite(CC2541DD, HIGH);
delay(1);
// Wait for data to be ready
if(wait_dup_ready() == 1)
{
// Read ID and revision
id = read_debug_byte(); // ID
read_debug_byte(); // Revision (discard)
}
// Set DD as output
pinMode(CC2541DD, OUTPUT);
return id;
}
/**************************************************************************//**
* @brief Sends a block of data over the debug interface using the
* BURST_WRITE command.
* @param src Pointer to the array of input bytes
* @param num_bytes The number of input bytes
* @return None.
******************************************************************************/
void burst_write_block(unsigned char *src, unsigned short num_bytes)
{
unsigned short i;
// Make sure DD is output
pinMode(CC2541DD, OUTPUT);
write_debug_byte(CMD_BURST_WRITE | HIBYTE(num_bytes));
write_debug_byte(LOBYTE(num_bytes));
for (i = 0; i < num_bytes; i++)
{
write_debug_byte(src[i]);
}
// Set DD as input
pinMode(CC2541DD, INPUT);
digitalWrite(CC2541DD, HIGH);
// Wait for DUP to be ready
wait_dup_ready();
read_debug_byte(); // ignore output
// Set DD as output
pinMode(CC2541DD, OUTPUT);
}
/**************************************************************************//**
* @brief Issues a CHIP_ERASE command on the debug interface and waits for it
* to complete.
* @return None.
******************************************************************************/
void chip_erase(void)
{
volatile unsigned char status;
// Send command
debug_command(CMD_CHIP_ERASE, 0, 0);
// Wait for status bit 7 to go low
do {
status = debug_command(CMD_READ_STATUS, 0, 0);
} while((status & STATUS_CHIP_ERASE_BUSY_BM));
}
/**************************************************************************//**
* @brief Writes a block of data to the DUP's XDATA space.
* @param address XDATA start address
* @param values Pointer to the array of bytes to write
* @param num_bytes Number of bytes to write
* @return None.
******************************************************************************/
void write_xdata_memory_block(unsigned short address,
const unsigned char *values,
unsigned short num_bytes)
{
unsigned char instr[3];
unsigned short i;
// MOV DPTR, address
instr[0] = 0x90;
instr[1] = HIBYTE(address);
instr[2] = LOBYTE(address);
debug_command(CMD_DEBUG_INSTR_3B, instr, 3);
for (i = 0; i < num_bytes; i++)
{
// MOV A, values[i]
instr[0] = 0x74;
instr[1] = values[i];
debug_command(CMD_DEBUG_INSTR_2B, instr, 2);
// MOV @DPTR, A
instr[0] = 0xF0;
debug_command(CMD_DEBUG_INSTR_1B, instr, 1);
// INC DPTR
instr[0] = 0xA3;
debug_command(CMD_DEBUG_INSTR_1B, instr, 1);
}
}
/**************************************************************************//**
* @brief Writes a byte to a specific address in the DUP's XDATA space.
* @param address XDATA address
* @param value Value to write
* @return None.
******************************************************************************/
void write_xdata_memory(unsigned short address, unsigned char value)
{
unsigned char instr[3];
// MOV DPTR, address
instr[0] = 0x90;
instr[1] = HIBYTE(address);
instr[2] = LOBYTE(address);
debug_command(CMD_DEBUG_INSTR_3B, instr, 3);
// MOV A, values[i]
instr[0] = 0x74;
instr[1] = value;
debug_command(CMD_DEBUG_INSTR_2B, instr, 2);
// MOV @DPTR, A
instr[0] = 0xF0;
debug_command(CMD_DEBUG_INSTR_1B, instr, 1);
}
/**************************************************************************//**
* @brief Read a byte from a specific address in the DUP's XDATA space.
* @param address XDATA address
* @return Value read from XDATA
******************************************************************************/
unsigned char read_xdata_memory(unsigned short address)
{
unsigned char instr[3];
// MOV DPTR, address
instr[0] = 0x90;
instr[1] = HIBYTE(address);
instr[2] = LOBYTE(address);
debug_command(CMD_DEBUG_INSTR_3B, instr, 3);
// MOVX A, @DPTR
instr[0] = 0xE0;
return debug_command(CMD_DEBUG_INSTR_1B, instr, 1);
}
/**************************************************************************//**
* @brief Reads 1-32767 bytes from DUP's flash to a given buffer on the
* programmer.
* @param bank Flash bank to read from [0-7]
* @param address Flash memory start address [0x0000 - 0x7FFF]
* @param values Pointer to destination buffer.
* @return None.
******************************************************************************/
void read_flash_memory_block(unsigned char bank,unsigned short flash_addr,
unsigned short num_bytes, unsigned char *values)
{
unsigned char instr[3];
unsigned short i;
unsigned short xdata_addr = (0x8000 + flash_addr);
// 1. Map flash memory bank to XDATA address 0x8000-0xFFFF
write_xdata_memory(DUP_MEMCTR, bank);
// 2. Move data pointer to XDATA address (MOV DPTR, xdata_addr)
instr[0] = 0x90;
instr[1] = HIBYTE(xdata_addr);
instr[2] = LOBYTE(xdata_addr);
debug_command(CMD_DEBUG_INSTR_3B, instr, 3);
for (i = 0; i < num_bytes; i++)
{
// 3. Move value pointed to by DPTR to accumulator (MOVX A, @DPTR)
instr[0] = 0xE0;
values[i] = debug_command(CMD_DEBUG_INSTR_1B, instr, 1);
// 4. Increment data pointer (INC DPTR)
instr[0] = 0xA3;
debug_command(CMD_DEBUG_INSTR_1B, instr, 1);
}
}
/**************************************************************************//**
* @brief Writes 4-2048 bytes to DUP's flash memory. Parameter \c num_bytes
* must be a multiple of 4.
* @param src Pointer to programmer's source buffer (in XDATA space)
* @param start_addr FLASH memory start address [0x0000 - 0x7FFF]
* @param num_bytes Number of bytes to transfer [4-1024]
* @return None.
******************************************************************************/
void write_flash_memory_block(unsigned char *src, unsigned long start_addr,
unsigned short num_bytes)
{
// 1. Write the 2 DMA descriptors to RAM
write_xdata_memory_block(ADDR_DMA_DESC_0, dma_desc_0, 8);
write_xdata_memory_block(ADDR_DMA_DESC_1, dma_desc_1, 8);
// 2. Update LEN value in DUP's DMA descriptors
unsigned char len[2] = {HIBYTE(num_bytes), LOBYTE(num_bytes)};
write_xdata_memory_block((ADDR_DMA_DESC_0+4), len, 2); // LEN, DBG => ram
write_xdata_memory_block((ADDR_DMA_DESC_1+4), len, 2); // LEN, ram => flash
// 3. Set DMA controller pointer to the DMA descriptors
write_xdata_memory(DUP_DMA0CFGH, HIBYTE(ADDR_DMA_DESC_0));
write_xdata_memory(DUP_DMA0CFGL, LOBYTE(ADDR_DMA_DESC_0));
write_xdata_memory(DUP_DMA1CFGH, HIBYTE(ADDR_DMA_DESC_1));
write_xdata_memory(DUP_DMA1CFGL, LOBYTE(ADDR_DMA_DESC_1));
// 4. Set Flash controller start address (wants 16MSb of 18 bit address)
write_xdata_memory(DUP_FADDRH, HIBYTE( (start_addr)));//>>2) ));
write_xdata_memory(DUP_FADDRL, LOBYTE( (start_addr)));//>>2) ));
// 5. Arm DBG=>buffer DMA channel and start burst write
write_xdata_memory(DUP_DMAARM, CH_DBG_TO_BUF0);
burst_write_block(src, num_bytes);
// 6. Start programming: buffer to flash
write_xdata_memory(DUP_DMAARM, CH_BUF0_TO_FLASH);
write_xdata_memory(DUP_FCTL, 0x0A);//0x06
// 7. Wait until flash controller is done
while (read_xdata_memory(DUP_FCTL) & 0x80);
}
void RunDUP(void)
{
volatile unsigned char i;
// Send two flanks on DC while keeping RESET_N low
// All low (incl. RESET_N)
digitalWrite(CC2541DD, LOW);
digitalWrite(CC2541DC, LOW);
digitalWrite(CC2541RESET, LOW);
delay(10); // Wait
digitalWrite(CC2541RESET, HIGH);
delay(10); // Wait
}
void ProgrammerInit(void)
{
pinMode(CC2541DD, OUTPUT);
pinMode(CC2541DC, OUTPUT);
pinMode(CC2541RESET, OUTPUT);
pinMode(LEDPIN0, OUTPUT);
digitalWrite(CC2541DD, LOW);
digitalWrite(CC2541DC, LOW);
digitalWrite(CC2541RESET, HIGH);
digitalWrite(LEDPIN0, LOW);
}
int Flash_CC2541(const char * path)
{
unsigned char chip_id = 0;
unsigned char debug_config = 0;
unsigned char Continue = 0;
unsigned char Verify = 0;
long fsize = 0;
int BlkTot = 0;
int Remain = 0;
int BlkNum = 0;
FsFile mfile=SD.open(path);
if(!mfile)
return 0;
Serial.printf("Found %s \n",path);
fsize = mfile.size();
if (fsize < 20)
{
mfile.close();
return 2; // No chip detected, run loop again.
}
Remain = fsize % 512;
if(Remain != 0)
{
BlkTot = fsize / 512 + 1;
Serial.printf("!!WARNING: File's size isn't the integer multiples of 512 bytes. \n");
Serial.printf(" the last block will be filled in up to 512 bytes with 0xFF! \n");
}
else
{
BlkTot = fsize / 512;
}
Serial.printf("Block total: %d\n\n", BlkTot);
BlkNum = 0;
ProgrammerInit();
debug_init();
chip_id = read_chip_id();
if(chip_id == 0)
{
Serial.printf("No CC2541 chip detected \n");
mfile.close();
return 2; // No chip detected, run loop again.
}
RunDUP();
debug_init();
chip_erase();
RunDUP();
debug_init();
// Switch DUP to external crystal osc. (XOSC) and wait for it to be stable.
// This is recommended if XOSC is available during programming. If
// XOSC is not available, comment out these two lines.
write_xdata_memory(DUP_CLKCONCMD, 0x80);
while (read_xdata_memory(DUP_CLKCONSTA) != 0x80);//0x80)
// Enable DMA (Disable DMA_PAUSE bit in debug configuration)
debug_config = 0x22;
debug_command(CMD_WR_CONFIG, &debug_config, 1);
// Program data (start address must be word aligned [32 bit])
digitalWrite(LEDPIN0, HIGH);
unsigned char rxBuf[512];
unsigned char read_data[512];
unsigned int addr = 0x0000;
int i;
while(mfile.available()){
mfile.read(&rxBuf,512);
Serial.printf("Wrting CC2541 Flash data at : %06x \n",addr);
//delay(100);
write_flash_memory_block(rxBuf, addr, 512); // src, address, count
if(Verify)
{
unsigned char bank = addr / (512 * 16);
unsigned int offset = (addr % (512 * 16)) * 4;
read_flash_memory_block(bank, offset, 512, read_data); // Bank, address, count, dest.
for(unsigned int i = 0; i < 512; i++)
{
if(read_data[i] != rxBuf[i])
{
// Fail
mfile.close();
Serial.printf("Failed Verification at Bank %06x Offset %06x \n", bank, offset);
chip_erase();
return 2;
}
}
}
addr += (unsigned int)128;
}
mfile.close();
digitalWrite(LEDPIN0, LOW);
RunDUP();
pinMode(CC2541RESET, OUTPUT);
digitalWrite(CC2541RESET, HIGH);
delay(200);
digitalWrite(CC2541RESET, LOW);
delay(200);
digitalWrite(CC2541RESET, HIGH);
return 1;
}
static bool idle_task_0()
{
idle0Calls += 1;
return false;
}
static bool idle_task_1()
{
idle1Calls += 1;
return false;
}
static void perfmon_task(void *args)
{
while (perfmonflag)
{
int i;
uint64_t idle0 = idle0Calls;
uint64_t idle1 = idle1Calls;
idle0Calls = 0;
idle1Calls = 0;
// 5 second sampling
//float cpu0 = 100.f - (((float)idle0 / (float)1702840) * 100.f);
//float cpu1 = 100.f - (((float)idle1 / (float)6501072) * 100.f);
// 1 second sampling
//float cpu0 = 100.f - (((float)idle0 / (float)340568) * 100.f);
//float cpu1 = 100.f - (((float)idle1 / (float)1300214) * 100.f);
// .5 second sampling
float cpu0 = 100.f - (((float)idle0 / (float)170284) * 100.f);
//float cpu0 = 100.f - (((float)idle0 / (float)650107) * 100.f);
float cpu1 = 100.f - (((float)idle1 / (float)650107) * 100.f);
if ( cpu0 < 0) cpu0 = 0;
if ( cpu1 < 0) cpu1 = 0;
c0[CPUGraphCount]=cpu0;
c1[CPUGraphCount]=cpu1;
for(i=0;i<CPUGraphCount;i++)
{
c0[i] = c0[i+1];
c1[i] = c1[i+1];
}
vTaskDelay(500 / portTICK_PERIOD_MS);
}
esp_deregister_freertos_idle_hook_for_cpu(idle_task_0, 0);
esp_deregister_freertos_idle_hook_for_cpu(idle_task_1, 1);
vTaskDelete(NULL);
}
esp_err_t perfmon_start()
{
int i;
if (perfmonflag)
return ESP_OK;
idle0Calls = 170284;
idle1Calls = 650107;
for(i=0;i<=CPUGraphCount;i++)
{
c0[i]=0;
c1[i]=0;
}
ESP_ERROR_CHECK(esp_register_freertos_idle_hook_for_cpu(idle_task_0, 0));
ESP_ERROR_CHECK(esp_register_freertos_idle_hook_for_cpu(idle_task_1, 1));
// TODO calculate optimal stack size
perfmonflag = 1;
xTaskCreate(perfmon_task, "perfmon", 2048, NULL, 1, NULL);
Serial.println("Performance Monitor Started \n");
return ESP_OK;
}
esp_err_t perfmon_stop()
{
if (!perfmonflag)
return ESP_OK;
perfmonflag = 0;
Serial.println("Performance Monitor Stopped/Killed \n");
return ESP_OK;
}
void blinkX_ok(int num)
{
int i;
digitalWrite(LEDPIN0, LOW);
delay(blinkDelay);
for(i=0;i<num;i++)
{
delay(blinkDelay);
digitalWrite(LEDPIN0, HIGH);
delay(blinkDelay);
digitalWrite(LEDPIN0, LOW);
}
}
struct async_resp_arg {
httpd_handle_t hd;
int fd;
};
int pidstack[ToTPIDStack];
float pidstackval[ToTPIDStack];
float tsens_out;
int fd_cmd[conncount];
unsigned long fd_millis[conncount],fd_curmillis[conncount];
void SDwriteFile(const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);
FsFile mfile = SD.open(path, (O_RDWR | O_CREAT | O_AT_END));
if(!mfile){
Serial.println("Failed to open file for writing");
return;
}
if(mfile.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
mfile.close();
}
int SDreadFile(const char * path, uint8_t ptr[]){
int i,len,totlen;
Serial.printf("Copying SD to SPIFFS file: %s\n", path);
FsFile mfile=SD.open(path);
if(!mfile){
Serial.println("Failed to open file for reading");
return -1;
}
totlen = 0;
i = 0;
while(mfile.available()){
len = mfile.read(&ptr[i],1024);
i=i+len;
totlen=totlen+len;
// vTaskDelay(5 / portTICK_PERIOD_MS);
}
ptr[totlen+1]=0;
mfile.close();
vTaskDelay(10 / portTICK_PERIOD_MS);
return totlen;
}
void SD_LoadSPIFF(const char * dirname, uint8_t levels){
long len,dsize;
int flag;
FsFile ddir;
FsFile dfile;