/
exmod_udpbond.go
1776 lines (1533 loc) · 73.8 KB
/
exmod_udpbond.go
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
package bondmachine
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/BondMachineHQ/BondMachine/pkg/udpbond"
)
type NetParameters map[string]string
type Udpbond_extra struct {
Config *udpbond.Config
Cluster *udpbond.Cluster
Ips *udpbond.Ips
PeerID uint32
Maps *IOmap
Flavor string
Ip string
Broadcast string
Netmask string
Port string
NetParams *NetParameters
}
type FirmwareCommand struct {
PrimaryState string // SM state for the command
SecondaryState string // state for eventually internal State machines
Command string // ASCII command
Description string
VHDLRapp string // VHDL hex
Signal string // Eventually associated signal
Starting int // Position of start within the memory
OmitReturn bool
Payload []string // Payload to transmit
Payload_relpos []int // Pauload relative position
}
func Ascii2Hex(in string) string {
encoded := ""
remaining_string := in
for remaining_string != "" {
lidx := strings.Index(remaining_string, "<<<")
if lidx == -1 {
tenc := []byte(remaining_string)
encoded += hex.EncodeToString(tenc)
remaining_string = ""
} else {
if lidx != 0 {
tenc := []byte(remaining_string[0:lidx])
encoded += hex.EncodeToString(tenc)
}
ridx := strings.Index(remaining_string, ">>>")
if lidx != -1 {
encoded += remaining_string[lidx+3 : ridx]
remaining_string = remaining_string[ridx+3:]
}
}
}
return encoded
}
func CompleteCommands(fc []FirmwareCommand) ([]FirmwareCommand, int) {
result := make([]FirmwareCommand, len(fc))
poscounter := 0
statelen := make(map[string]int)
for i, com := range fc {
result[i].PrimaryState = com.PrimaryState
result[i].SecondaryState = com.SecondaryState
result[i].Command = com.Command
result[i].Description = com.Description
result[i].Signal = com.Signal
result[i].OmitReturn = com.OmitReturn
result[i].Starting = poscounter
result[i].Payload = append([]string(nil), com.Payload...)
result[i].Payload_relpos = append([]int(nil), com.Payload_relpos...)
result[i].VHDLRapp = ""
encoded := ""
remaining_string := com.Command
for remaining_string != "" {
lidx := strings.Index(remaining_string, "<<<")
if lidx == -1 {
tenc := []byte(remaining_string)
encoded += hex.EncodeToString(tenc)
remaining_string = ""
} else {
if lidx != 0 {
tenc := []byte(remaining_string[0:lidx])
encoded += hex.EncodeToString(tenc)
}
ridx := strings.Index(remaining_string, ">>>")
if lidx != -1 {
encoded += remaining_string[lidx+3 : ridx]
remaining_string = remaining_string[ridx+3:]
}
}
}
if !com.OmitReturn {
encoded += "0d0a"
}
encoded += "ff"
for j := 0; j < len(encoded); j = j + 2 {
result[i].VHDLRapp += "x\"" + encoded[j:j+2] + "\","
}
poscounter += len(encoded) / 2
pmstate := com.PrimaryState
if sl, ok := statelen[pmstate]; ok {
statelen[pmstate] = sl + 1
} else {
statelen[pmstate] = 1
}
}
lencounter := make(map[string]int)
for i, com := range fc {
pmstate := com.PrimaryState
nbits := Needed_bits(statelen[pmstate])
if st, ok := lencounter[pmstate]; !ok {
result[i].SecondaryState = zeros_prefix(nbits, get_binary(0))
lencounter[pmstate] = 1
} else {
result[i].SecondaryState = zeros_prefix(nbits, get_binary(st))
lencounter[pmstate] = st + 1
}
}
return result, poscounter
}
func LocateCommand(fc []FirmwareCommand, ps string, ss string) int {
for _, com := range fc {
if com.PrimaryState == ps {
if com.SecondaryState == ss {
return com.Starting
}
}
}
return -1
}
func LocateCommandbyIndex(fc []FirmwareCommand, ps string, index int) (FirmwareCommand, bool) {
i := 0
for _, com := range fc {
if com.PrimaryState == ps {
if i == index {
return com, true
}
i++
}
}
return FirmwareCommand{}, false
}
func (sl *Udpbond_extra) Get_Name() string {
return "udpbond"
}
func (sl *Udpbond_extra) Get_Params() *ExtraParams {
result := new(ExtraParams)
result.Params = make(map[string]string)
result.Params["peer_id"] = strconv.Itoa(int(sl.PeerID))
result.Params["cluster_id"] = strconv.Itoa(int(sl.Cluster.ClusterId))
result.Params["ip"] = sl.Ip
result.Params["broadcast"] = sl.Broadcast
result.Params["port"] = sl.Port
netparams := *sl.NetParams
if ssid, ok := netparams["ssid"]; ok {
result.Params["ssid"] = ssid
}
if netmask, ok := netparams["netmask"]; ok {
result.Params["netmask"] = netmask
}
if gateway, ok := netparams["gateway"]; ok {
result.Params["gateway"] = gateway
}
var mypeer udpbond.Peer
for _, peer := range sl.Cluster.Peers {
if peer.PeerId == sl.PeerID {
mypeer = peer
}
if sl.Ips != nil {
peerstr := strconv.Itoa(int(peer.PeerId))
if ipaddr, ok := sl.Ips.Assoc["peer_"+peerstr]; ok {
if ipaddr == "auto" {
result.Params["peer_"+peerstr+"_ip"] = "auto"
} else if ipaddr == "adv" {
result.Params["peer_"+peerstr+"_ip"] = "auto"
} else {
result.Params["peer_"+peerstr+"_ip"] = ipaddr
}
} else {
result.Params["peer_"+peerstr+"_ip"] = "auto"
}
}
}
result.Params["input_ids"] = ""
result.Params["inputs"] = ""
result.Params["sources"] = ""
for _, inp := range mypeer.Inputs {
for iname, ival := range sl.Maps.Assoc {
if iname[0] == 'i' && ival == strconv.Itoa(int(inp)) {
result.Params["input_ids"] += "," + ival
result.Params["inputs"] += "," + iname
ressource := ""
for _, opeer := range sl.Cluster.Peers {
for _, oout := range opeer.Outputs {
if strconv.Itoa(int(oout)) == ival {
ressource = strconv.Itoa(int(opeer.PeerId))
break
}
}
}
if ressource != "" {
result.Params["sources"] += "," + ressource
}
}
}
}
if result.Params["input_ids"] != "" {
result.Params["input_ids"] = result.Params["input_ids"][1:len(result.Params["input_ids"])]
result.Params["inputs"] = result.Params["inputs"][1:len(result.Params["inputs"])]
result.Params["sources"] = result.Params["sources"][1:len(result.Params["sources"])]
}
result.Params["output_ids"] = ""
result.Params["outputs"] = ""
// Comma separated and - separated list of peer ids
result.Params["destinations"] = ""
for _, outp := range mypeer.Outputs {
for oname, oval := range sl.Maps.Assoc {
if oname[0] == 'o' && oval == strconv.Itoa(int(outp)) {
result.Params["output_ids"] += "," + oval
result.Params["outputs"] += "," + oname
resdest := ""
for _, ipeer := range sl.Cluster.Peers {
for _, iin := range ipeer.Inputs {
//fmt.Println(ipeer.PeerId, iin, oval, strconv.Itoa(int(iin)))
if strconv.Itoa(int(iin)) == oval {
resdest += "-" + strconv.Itoa(int(ipeer.PeerId))
}
}
}
//fmt.Println("resdest", resdest)
if resdest != "" {
result.Params["destinations"] += "," + resdest[1:len(resdest)]
}
}
}
}
if result.Params["output_ids"] != "" {
result.Params["output_ids"] = result.Params["output_ids"][1:len(result.Params["output_ids"])]
result.Params["outputs"] = result.Params["outputs"][1:len(result.Params["outputs"])]
result.Params["destinations"] = result.Params["destinations"][1:len(result.Params["destinations"])]
}
return result
}
func (sl *Udpbond_extra) Import(inp string) error {
return nil
}
func (sl *Udpbond_extra) Export() string {
return ""
}
func (sl *Udpbond_extra) Check(bmach *Bondmachine) error {
return nil
}
func (sl *Udpbond_extra) Verilog_headers() string {
result := "\n"
return result
}
func (sl *Udpbond_extra) StaticVerilog() string {
result := "\n"
return result
}
func (sl *Udpbond_extra) ExtraFiles() ([]string, []string) {
rsize := int(sl.Config.Rsize)
udpbond_params := sl.Get_Params().Params
fmt.Println(udpbond_params)
payload_fractions := rsize / 8
if rsize%8 != 0 {
payload_fractions++
}
intclusid, _ := strconv.Atoi(udpbond_params["cluster_id"])
hexclusid := fmt.Sprintf("%08x", intclusid)
intpeerid, _ := strconv.Atoi(udpbond_params["peer_id"])
hexpeerid := fmt.Sprintf("%08x", intpeerid)
result := ""
result += "------------------ VHDL component for the esc8266 wireless chip -----------\n"
result += "library IEEE;\n"
result += "use IEEE.STD_LOGIC_1164.ALL;\n"
result += "use IEEE.NUMERIC_STD.ALL;\n"
result += "\n"
// Entry points to the bondmachine
result += "entity udpbond_main is\n"
result += " Port ( clk100 : in STD_LOGIC;\n"
result += " reset : in STD_LOGIC;\n"
result += " wifi_enable : out STD_LOGIC;\n"
result += " wifi_rx : in STD_LOGIC;\n"
result += " wifi_tx : out STD_LOGIC;\n"
for _, iname := range strings.Split(udpbond_params["inputs"], ",") {
if iname != "" {
result += " input_" + iname + " : out STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := (others => '0');\n"
result += " input_" + iname + "_dv : out STD_LOGIC;\n"
result += " input_" + iname + "_recv : in STD_LOGIC;\n"
}
}
for _, oname := range strings.Split(udpbond_params["outputs"], ",") {
if oname != "" {
result += " output_" + oname + " : in STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := (others => '0');\n"
result += " output_" + oname + "_dv : in STD_LOGIC;\n"
result += " output_" + oname + "_recv : out STD_LOGIC;\n"
}
}
result = result[0 : len(result)-2]
result += " );\n"
result += "end udpbond_main;\n"
result += "\n"
result += "architecture Behavioral of udpbond_main is\n"
result += "\n"
result += " component esp8266_driver is\n"
result += " Port ( clk100 : in STD_LOGIC;\n"
//result += " -- roba da lasciare per il debug --\n"
result += " powerdown : in STD_LOGIC;\n"
//result += " otherpayload : in STD_LOGIC;\n"
//result += " status_active : out STD_LOGIC;\n"
//result += " status_wifi_up : out STD_LOGIC;\n"
//result += " status_connected : out STD_LOGIC;\n"
//result += " status_sending : out STD_LOGIC;\n"
//result += " status_receiving : out STD_LOGIC;\n"
//result += " status_error : out STD_LOGIC;\n"
//counter := 0
for _, iname := range strings.Split(udpbond_params["inputs"], ",") {
if iname != "" {
result += " payload_" + iname + " : out STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := x\"00\";\n"
result += " payload_" + iname + "_dv : out STD_LOGIC;\n"
result += " payload_" + iname + "_recv : in STD_LOGIC;\n"
//counter++
}
}
for _, oname := range strings.Split(udpbond_params["outputs"], ",") {
if oname != "" {
result += " payload_" + oname + " : in STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := x\"00\";\n"
result += " payload_" + oname + "_dv : in STD_LOGIC;\n"
result += " payload_" + oname + "_recv : out STD_LOGIC;\n"
//counter++
}
}
result += " wifi_enable : out STD_LOGIC;\n"
result += " wifi_rx : in STD_LOGIC;\n"
result += " wifi_tx : out STD_LOGIC);\n"
result += " end component;\n"
result += "\n"
//result += " -- questi char non ho nai capito a cosa servono e vengono solo menzionati in questa parte di codice --\n"
//result += " signal char0 : std_logic_vector(7 downto 0) := x\"00\";\n"
//result += " signal char1 : std_logic_vector(7 downto 0) := x\"00\";\n"
//result += " signal char2 : std_logic_vector(7 downto 0) := x\"00\";\n"
//result += " signal char3 : std_logic_vector(7 downto 0) := x\"00\";\n"
result += "begin\n"
result += "\n"
result += "i_esp8226: esp8266_driver Port map (\n"
result += " clk100 => clk100,\n"
result += " powerdown => reset,\n"
//result += " -- status_active => led(0),\n"
//result += " -- status_wifi_up => led(1),\n"
//result += " -- status_connected => led(2),\n"
//result += " -- status_sending => led(3),\n"
//result += " -- status_receiving => led(4),\n"
//result += " -- status_error => led(5),\n"
for _, iname := range strings.Split(udpbond_params["inputs"], ",") {
if iname != "" {
result += " payload_" + iname + " => input_" + iname + ",\n"
result += " payload_" + iname + "_dv => input_" + iname + "_dv,\n"
result += " payload_" + iname + "_recv => input_" + iname + "_recv,\n"
}
}
for _, oname := range strings.Split(udpbond_params["outputs"], ",") {
if oname != "" {
result += " payload_" + oname + " => output_" + oname + ",\n"
result += " payload_" + oname + "_dv => output_" + oname + "_dv,\n"
result += " payload_" + oname + "_recv => output_" + oname + "_recv,\n"
}
}
result += " wifi_enable => wifi_enable,\n"
result += " wifi_rx => wifi_rx,\n"
result += " wifi_tx => wifi_tx);\n"
result += "\n"
result += "end Behavioral;\n"
result += "\n"
result += "--------------------------------------------------\n"
result += "-- esp8266_driver - Session setup and sending\n"
result += "-- packets of data using ESP8266\n"
result += "--\n"
result += "-- Author: Mike Field <hamster@snap.net.nz>\n"
result += "--\n"
result += "-- NOTE: You will need to edit the constants to put\n"
result += "-- your own SSID & password, and the IP address\n"
result += "-- and destination port number\n"
result += "--\n"
result += "-- This also has a watchdog, that resets the state\n"
result += "-- of the design if no state change has occurred \n"
result += "-- in the last 10 seconds.\n"
result += "------------------------------------------------\n"
result += "\n"
result += "library IEEE;\n"
result += "use IEEE.STD_LOGIC_1164.ALL;\n"
result += "use IEEE.NUMERIC_STD.ALL;\n"
result += "\n"
result += "entity esp8266_driver is\n"
result += " Port ( clk100 : in STD_LOGIC;\n"
result += " powerdown : in STD_LOGIC;\n"
//result += " status_active : out STD_LOGIC := '0';\n"
//result += " status_wifi_up : out STD_LOGIC := '0';\n"
//result += " status_connected : out STD_LOGIC := '0';\n"
//result += " status_sending : out STD_LOGIC := '0';\n"
//result += " status_receiving : out STD_LOGIC := '0';\n"
//result += " status_error : out STD_LOGIC := '0';\n"
//result += " \n"
for _, iname := range strings.Split(udpbond_params["inputs"], ",") {
if iname != "" {
result += " payload_" + iname + " : out STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := x\"00\";\n"
result += " payload_" + iname + "_dv : out STD_LOGIC;\n"
result += " payload_" + iname + "_recv : in STD_LOGIC;\n"
}
}
for _, oname := range strings.Split(udpbond_params["outputs"], ",") {
if oname != "" {
result += " payload_" + oname + " : in STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := x\"00\";\n"
result += " payload_" + oname + "_dv : in STD_LOGIC;\n"
result += " payload_" + oname + "_recv : out STD_LOGIC;\n"
}
}
result += " wifi_enable : out STD_LOGIC;\n"
result += " wifi_rx : in STD_LOGIC;\n"
result += " wifi_tx : out STD_LOGIC);\n"
result += "end esp8266_driver;\n"
result += "\n"
// Now lets build the firmare commands
commands := make([]FirmwareCommand, 0)
var command FirmwareCommand
// command = FirmwareCommand{PrimaryState: "", SecondaryState: "", Command: "", VHDLRapp: "", Signal: "", Starting: -1}
command = FirmwareCommand{PrimaryState: "00011", Command: "AT+CWMODE=3", Starting: -1, Description: "change mode"}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "00100", Command: "AT+CIPMUX=1", Starting: -1, Description: "single socket mode"}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "00101", Command: "AT+CWJAP=\"" + udpbond_params["ssid"] + "\",\"" + udpbond_params["password"] + "\"", Starting: -1, Description: "Connect to WIFI"}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "11100", Command: "AT+CIPSTA=\"" + udpbond_params["ip"] + "\",\"" + udpbond_params["gateway"] + "\",\"" + udpbond_params["netmask"] + "\"", Starting: -1, Description: "Assing IP address"}
commands = append(commands, command)
// Start of the connection composing
// Receiving socket
command = FirmwareCommand{PrimaryState: "01110", Command: "AT+CIPSTART=0,\"UDP\",\"0.0.0.0\"," + udpbond_params["port"] + "," + udpbond_params["port"], Starting: -1, Description: "Open UDP receiving socket"}
commands = append(commands, command)
// Multicast socket
command = FirmwareCommand{PrimaryState: "01101", Command: "AT+CIPSTART=1,\"UDP\",\"" + udpbond_params["broadcast"] + "\"," + udpbond_params["port"], Starting: -1, Description: "Multicast socket"}
commands = append(commands, command)
// Keep track to witch connection map every destination
dest2conn := make(map[string]string)
{
done := make(map[string]bool)
conn := 2
// Find the unique destinations and set a CISSTART for each one
if udpbond_params["destinations"] != "" {
for _, destlist := range strings.Split(udpbond_params["destinations"], ",") {
for _, dest := range strings.Split(destlist, "-") {
if _, ok := done[dest]; !ok {
done[dest] = true
var peerid string
var peerip string
var peerport string
if pid, ok := udpbond_params["peer_"+dest+"_ip"]; ok {
peerid = dest
peerip = strings.Split(pid, "/")[0]
peerport = strings.Split(pid, ":")[1]
} else {
break
}
command = FirmwareCommand{PrimaryState: "00110", Command: "AT+CIPSTART=" + strconv.Itoa(conn) + ",\"UDP\",\"" + peerip + "\"," + peerport, Starting: -1, Description: "Start an UDP connection to peer " + peerid}
commands = append(commands, command)
dest2conn[dest] = strconv.Itoa(conn)
conn++
}
}
}
}
// Find the received destination and set a CISSTART for them (if not already settend from the previous
if udpbond_params["sources"] != "" {
for _, source := range strings.Split(udpbond_params["sources"], ",") {
if _, ok := done[source]; !ok {
done[source] = true
var peerid string
var peerip string
var peerport string
if pid, ok := udpbond_params["peer_"+source+"_ip"]; ok {
peerid = source
peerip = strings.Split(pid, "/")[0]
peerport = strings.Split(pid, ":")[1]
} else {
break
}
command = FirmwareCommand{PrimaryState: "00110", Command: "AT+CIPSTART=" + strconv.Itoa(conn) + ",\"UDP\",\"" + peerip + "\"," + peerport, Starting: -1, Description: "Start an UDP connection to peer " + peerid}
commands = append(commands, command)
dest2conn[source] = strconv.Itoa(conn)
conn++
}
}
}
}
// Send packets mapped to 2 state with the same SecondaryState
// Processing Broadcasts
command = FirmwareCommand{PrimaryState: "00111", Command: "AT+CIPSEND=1,13", Starting: -1, Description: "Broadcast ADV_CLU cipsend", Signal: "broadcast_ready_adv_clu"}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "01000", Command: "<<<888801" + hexclusid + hexpeerid + ">>>", Starting: -1, Description: "Broadcast ADV_CLU message", Signal: "broadcast_ready_adv_clu"}
commands = append(commands, command)
if udpbond_params["input_ids"] != "" {
for i, resid := range strings.Split(udpbond_params["input_ids"], ",") {
residint, _ := strconv.Atoi(resid)
residhex := fmt.Sprintf("%08x", residint)
command = FirmwareCommand{PrimaryState: "00111", Command: "AT+CIPSEND=1,17", Starting: -1, Description: "Broadcast ADV_IN cipsend " + strconv.Itoa(i), Signal: "broadcast_ready_adv_in_" + strconv.Itoa(i)}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "01000", Command: "<<<888803" + hexclusid + hexpeerid + residhex + ">>>", Starting: -1, Description: "Broadcast ADV_IN message " + strconv.Itoa(i), Signal: "broadcast_ready_adv_in_" + strconv.Itoa(i)}
commands = append(commands, command)
}
}
if udpbond_params["output_ids"] != "" {
for i, resid := range strings.Split(udpbond_params["output_ids"], ",") {
residint, _ := strconv.Atoi(resid)
residhex := fmt.Sprintf("%08x", residint)
command = FirmwareCommand{PrimaryState: "00111", Command: "AT+CIPSEND=1,17", Starting: -1, Description: "Broadcast ADV_OUT cipsend " + strconv.Itoa(i), Signal: "broadcast_ready_adv_out_" + strconv.Itoa(i)}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "01000", Command: "<<<888804" + hexclusid + hexpeerid + residhex + ">>>", Starting: -1, Description: "Broadcast ADV_OUT message " + strconv.Itoa(i), Signal: "broadcast_ready_adv_out_" + strconv.Itoa(i)}
commands = append(commands, command)
}
}
// Processing IO transfers
if udpbond_params["output_ids"] != "" {
dests := strings.Split(udpbond_params["destinations"], ",")
outs := strings.Split(udpbond_params["outputs"], ",")
for i, resid := range strings.Split(udpbond_params["output_ids"], ",") {
oname := outs[i]
destlist := dests[i]
for _, dest := range strings.Split(destlist, "-") {
residint, _ := strconv.Atoi(resid)
residhex := fmt.Sprintf("%08x", residint)
conn := dest2conn[dest]
command = FirmwareCommand{PrimaryState: "00111", Command: "AT+CIPSEND=" + conn + "," + strconv.Itoa(19+payload_fractions), Starting: -1, Description: "Send Payload of " + strconv.Itoa(residint), Signal: "io_tr_ready_" + strconv.Itoa(residint) + "_on_" + conn}
commands = append(commands, command)
payplaceholder := strings.Repeat("X", payload_fractions)
command = FirmwareCommand{PrimaryState: "01000", Command: "<<<888805>>>XXXX<<<" + hexclusid + hexpeerid + residhex + ">>>" + payplaceholder, Payload: []string{"tag", "payload_" + oname}, Payload_relpos: []int{3, 19}, Starting: -1, Description: "Send Payload of " + strconv.Itoa(residint), Signal: "io_tr_ready_" + strconv.Itoa(residint) + "_on_" + conn, OmitReturn: true}
commands = append(commands, command)
}
}
}
// Processing ACKs
// TODO
// Processing data valid
if udpbond_params["output_ids"] != "" {
dests := strings.Split(udpbond_params["destinations"], ",")
outs := strings.Split(udpbond_params["outputs"], ",")
for i, resid := range strings.Split(udpbond_params["output_ids"], ",") {
oname := outs[i]
destlist := dests[i]
for _, dest := range strings.Split(destlist, "-") {
residint, _ := strconv.Atoi(resid)
residhex := fmt.Sprintf("%08x", residint)
conn := dest2conn[dest]
command = FirmwareCommand{PrimaryState: "00111", Command: "AT+CIPSEND=" + conn + "," + strconv.Itoa(20), Starting: -1, Description: "Send Payload data valid of " + strconv.Itoa(residint), Signal: "io_tr_dv_" + strconv.Itoa(residint) + "_on_" + conn}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "01000", Command: "<<<888806>>>XXXX<<<" + hexclusid + hexpeerid + residhex + ">>>X", Payload: []string{"tag", "payload_" + oname + "_dv"}, Payload_relpos: []int{3, 19}, Starting: -1, Description: "Send Payload data valid of " + strconv.Itoa(residint), Signal: "io_tr_dv_" + strconv.Itoa(residint) + "_on_" + conn, OmitReturn: true}
commands = append(commands, command)
}
}
}
// Processing data received
if udpbond_params["input_ids"] != "" {
ins := strings.Split(udpbond_params["inputs"], ",")
sources := strings.Split(udpbond_params["sources"], ",")
for i, resid := range strings.Split(udpbond_params["input_ids"], ",") {
iname := ins[i]
dest := sources[i]
residint, _ := strconv.Atoi(resid)
residhex := fmt.Sprintf("%08x", residint)
conn := dest2conn[dest]
command = FirmwareCommand{PrimaryState: "00111", Command: "AT+CIPSEND=" + conn + "," + strconv.Itoa(20), Starting: -1, Description: "Send Payload data received of " + strconv.Itoa(residint), Signal: "io_tr_recv_" + strconv.Itoa(residint) + "_from_" + conn}
commands = append(commands, command)
command = FirmwareCommand{PrimaryState: "01000", Command: "<<<888807>>>XXXX<<<" + hexclusid + hexpeerid + residhex + ">>>X", Payload: []string{"tag", "payload_" + iname + "_recv"}, Payload_relpos: []int{3, 19}, Starting: -1, Description: "Send Payload data received of " + strconv.Itoa(residint), Signal: "io_tr_recv_" + strconv.Itoa(residint) + "_from_" + conn, OmitReturn: true}
commands = append(commands, command)
}
}
completedcommands, totall := CompleteCommands(commands)
membits := Needed_bits(totall)
//fmt.Println(commands)
//fmt.Println(completedcommands, totall)
//sockets_bits := Needed_bits(len("TODO"))
var cipstart_loop_bits int
cipstart_loop_present := true
if cipstart_example, ok := LocateCommandbyIndex(completedcommands, "00110", 0); ok {
cipstart_loop_bits = len(cipstart_example.SecondaryState)
} else {
cipstart_loop_present = false
}
var message_loop_bits int
if send_example, ok := LocateCommandbyIndex(completedcommands, "00111", 0); ok {
message_loop_bits = len(send_example.SecondaryState)
}
result += "architecture Behavioral of esp8266_driver is\n"
result += " type a_mem is array(0 to " + strconv.Itoa(totall-1) + ") of std_logic_vector(7 downto 0);\n"
result += " signal memory : a_mem := \n"
result += " (\n"
for _, command = range completedcommands {
result += "-- " + command.Description + " - Starting from " + strconv.Itoa(command.Starting) + " - Command \"" + command.Command + "\"\n"
result += "\t" + command.VHDLRapp + "\n"
}
result = result[0:len(result)-2] + ");\n"
result += "\n"
// Old state for signal that has to go out via wireless: payloads, data valids, data received of incoming payloads
for _, oname := range strings.Split(udpbond_params["outputs"], ",") {
if oname != "" {
//for i := 0; i < payload_fractions; i++ {
// result += " signal payload_" + oname + "_f_" + strconv.Itoa(i) + " : std_logic_vector(7 downto 0) := x\"00\";\n"
//}
result += " signal payload_" + oname + "_old : STD_LOGIC_VECTOR(" + strconv.Itoa(rsize-1) + " downto 0) := x\"00\";\n"
result += " signal payload_" + oname + "_dv_old : STD_LOGIC;\n"
}
}
result += "\n"
for _, iname := range strings.Split(udpbond_params["inputs"], ",") {
if iname != "" {
result += " signal payload_" + iname + "_recv_old : STD_LOGIC;\n"
}
}
result += "\n"
// These are the received signals, one each destination
if udpbond_params["output_ids"] != "" {
dests := strings.Split(udpbond_params["destinations"], ",")
for i, resid := range strings.Split(udpbond_params["output_ids"], ",") {
destlist := dests[i]
for _, dest := range strings.Split(destlist, "-") {
conn := dest2conn[dest]
residint, _ := strconv.Atoi(resid)
cname := "io_tr_recv_" + strconv.Itoa(residint) + "_on_" + conn
result += " signal " + cname + " : STD_LOGIC;\n"
}
}
}
result += "\n"
result += " signal current_char : unsigned(" + strconv.Itoa(membits-1) + " downto 0) := (others => '0');\n"
result += " signal delay_counter : unsigned(26 downto 0) := (others => '0');\n"
result += " signal in_delay : std_logic := '0';\n"
result += "\n"
result += " component tx is\n"
result += " Port ( clk : in STD_LOGIC;\n"
result += " data : in STD_LOGIC_VECTOR (7 downto 0);\n"
result += " data_enable : in STD_LOGIC;\n"
result += " busy : out STD_LOGIC;\n"
result += " tx_out : out STD_LOGIC);\n"
result += " end component;\n"
result += "\n"
result += " component rx is\n"
result += " Port ( clk : in STD_LOGIC;\n"
result += " data : out STD_LOGIC_VECTOR (7 downto 0);\n"
result += " data_enable : out STD_LOGIC;\n"
result += " rx_in : in STD_LOGIC);\n"
result += " end component;\n"
result += "\n"
result += " signal tx_data : std_logic_vector(7 downto 0 ) := (others => '0');\n"
result += " signal tx_busy : std_logic := '0';\n"
result += " signal tx_data_enable : std_logic := '0';\n"
result += "\n"
result += " signal rx_data : std_logic_vector(7 downto 0 ) := (others => '0');\n"
result += " signal rx_data_enable : std_logic := '0';\n"
result += " signal sending : std_logic := '0';\n"
result += " signal receiving : std_logic := '0';\n"
result += " signal state : std_logic_vector(4 downto 0 ) := (others => '0');\n"
result += " signal state_last : std_logic_vector(4 downto 0 ) := (others => '0');\n"
result += " signal last_rx_chars : std_logic_vector(" + strconv.Itoa(231+rsize) + " downto 0 ) := (others => '0');\n"
result += "\n"
result += " signal rx_seeing_ok : std_logic := '0';\n"
result += " signal rx_seeing_recv : std_logic := '0';\n"
result += " signal rx_seeing_ready : std_logic := '0';\n"
result += " signal rx_seeing_change : std_logic := '0';\n"
result += " signal rx_seeing_prompt : std_logic := '0';\n"
result += "\n"
for currindex := 0; ; currindex++ {
currcom, currstat := LocateCommandbyIndex(completedcommands, "00111", currindex)
if currcom.Signal != "" {
result += " signal " + currcom.Signal + " : std_logic := '0';\n"
result += " signal reset_" + currcom.Signal + " : std_logic := '0';\n"
}
if !currstat {
break
}
}
result += "\n"
result += " signal tag : std_logic_vector(31 downto 0 ) := (others => '0');\n"
result += "\n"
result += " signal message_loop : std_logic_vector(" + strconv.Itoa(message_loop_bits-1) + " downto 0 ) := (others => '0');\n"
if cipstart_loop_present {
result += " signal cipstart_loop : std_logic_vector(" + strconv.Itoa(cipstart_loop_bits-1) + " downto 0 ) := (others => '0');\n"
}
result += "\n"
result += " -- Watchdog timer for recovery.\n"
result += " -- This has to count for the number of clock cycles in 1ms\n"
result += " signal watchdog_low : unsigned(16 downto 0) := (others => '0');\n"
result += " -- This is high for one cycle every millisecond\n"
result += " signal inc_wd_high : std_logic := '0';\n"
result += " -- This has to count to 9,9999, for the 10,000 ms timout\n"
result += " signal watchdog_high : unsigned(13 downto 0) := (others => '0');\n"
result += " signal counter : unsigned(12 downto 0) := (others => '0');\n"
result += " signal bcounter : unsigned(11 downto 0) := (others => '0');\n"
result += "begin\n"
result += "\n"
result += "i_tx : tx port map (\n"
result += " clk => clk100,\n"
result += " data => tx_data,\n"
result += " data_enable => tx_data_enable,\n"
result += " busy => tx_busy,\n"
result += " tx_out => wifi_tx);\n"
result += "\n"
result += "i_rx : rx port map (\n"
result += " clk => clk100,\n"
result += " data => rx_data,\n"
result += " data_enable => rx_data_enable,\n"
result += " rx_in => wifi_rx);\n"
result += "\n"
// These are the received signals, one each destination merged in the output received
if udpbond_params["output_ids"] != "" {
dests := strings.Split(udpbond_params["destinations"], ",")
outs := strings.Split(udpbond_params["outputs"], ",")
for i, resid := range strings.Split(udpbond_params["output_ids"], ",") {
oname := outs[i]
destlist := dests[i]
result += "merge_payload_" + oname + ": process(clk100)\n"
result += " begin\n"
result += " if rising_edge(clk100) then\n"
result += " payload_" + oname + "_recv <= "
for j, dest := range strings.Split(destlist, "-") {
conn := dest2conn[dest]
residint, _ := strconv.Atoi(resid)
cname := "io_tr_recv_" + strconv.Itoa(residint) + "_on_" + conn
if j == 0 {
result += cname
} else {
result += " and " + cname
}
}
result += ";\n"
result += " end if;\n"
result += " end process;\n"
result += "\n"
}
}
for currindex := 0; ; currindex++ {
currcom, currstat := LocateCommandbyIndex(completedcommands, "00111", currindex)
// Broadcasts triggerred avery 5s
if strings.HasPrefix(currcom.Signal, "broadcast_ready_adv") {
result += "send_" + currcom.Signal + ": process(clk100)\n"
result += " begin\n"
result += " if rising_edge(clk100) then\n"
result += " if reset_" + currcom.Signal + " = '1' then\n"
result += " " + currcom.Signal + " <= '0'; \n"
result += " else\n"
result += " if counter = 0 then\n"
result += " " + currcom.Signal + " <= '1';\n"
result += " end if;\n"
result += " end if;\n"
result += " end if;\n"
result += " end process;\n"
result += "\n"
}
if !currstat {
break
}
}
payloads_done := make(map[string]bool)
for currindex := 0; ; currindex++ {
currcom, currstat := LocateCommandbyIndex(completedcommands, "01000", currindex)
if strings.HasPrefix(currcom.Signal, "io_tr_ready_") {
asspay := ""
for _, pay := range currcom.Payload {
if strings.HasPrefix(pay, "payload_") {
asspay = pay
break
}
}
result += "send_" + currcom.Signal + ": process(clk100)\n"
result += " begin\n"
result += " if rising_edge(clk100) then\n"
result += " if reset_" + currcom.Signal + " = '1' then\n"
result += " " + currcom.Signal + " <= '0'; \n"
result += " else\n"
result += " if bcounter = 0 then\n"
result += " " + currcom.Signal + " <= '1';\n"
result += " else\n"
result += " if " + asspay + "_old /= " + asspay + " then\n"
result += " " + currcom.Signal + " <= '1';\n"
result += " end if;\n"
result += " end if;\n"
result += " end if;\n"
result += " end if;\n"
result += " end process;\n"
result += " \n"
if _, ok := payloads_done[asspay]; !ok {
payloads_done[asspay] = true
result += "check_" + asspay + ": process(clk100)\n"
result += " begin\n"
result += " if rising_edge(clk100) then\n"
result += " " + asspay + "_old <= " + asspay + ";\n"
result += " end if;\n"
result += " end process;\n"
result += " \n"
}
}
if !currstat {
break
}
}
payloads_done = make(map[string]bool)
for currindex := 0; ; currindex++ {
currcom, currstat := LocateCommandbyIndex(completedcommands, "01000", currindex)
if strings.HasPrefix(currcom.Signal, "io_tr_dv_") {
asspay := ""
for _, pay := range currcom.Payload {
if strings.HasPrefix(pay, "payload_") {
asspay = pay
break
}
}
result += "send_" + currcom.Signal + ": process(clk100)\n"
result += " begin\n"
result += " if rising_edge(clk100) then\n"
result += " if reset_" + currcom.Signal + " = '1' then\n"
result += " " + currcom.Signal + " <= '0'; \n"
result += " else\n"
result += " if bcounter = 0 then\n"
result += " " + currcom.Signal + " <= '1';\n"
result += " else\n"
result += " if " + asspay + "_old /= " + asspay + " then\n"
result += " " + currcom.Signal + " <= '1';\n"
result += " end if;\n"
result += " end if;\n"