forked from sparkfun/AmbiqSuiteSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patham_devices_fireball.c
1325 lines (1189 loc) · 45.8 KB
/
am_devices_fireball.c
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
//*****************************************************************************
//
//! @file am_devices_fireball.c
//!
//! @brief Control the Ambiq FIREBALL board.
//!
//! This module contains functions for controlling the Ambiq internal FIREBALL
//! board.
//
//*****************************************************************************
//*****************************************************************************
//
// Copyright (c) 2020, Ambiq Micro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// Third party software included in this distribution is subject to the
// additional license terms as defined in the /docs/licenses directory.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// This is part of revision 2.4.2 of the AmbiqSuite Development Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "am_mcu_apollo.h"
#include "am_bsp.h"
#include "am_devices_fireball.h"
//*****************************************************************************
//
// Macros.
//
//*****************************************************************************
//
// The IOM to be used is defined by the Apollo3 MCU Engineering Board.
//
#define FIREBALL_IOM_MODULE 5
//
// Define a board number.
//
#define FIREBALL_BOARD_NUM 1
//
// Helper macros
//
#define FB_CMD(op, pio) (((op & 0x03) << 6) | ((pio & 0x3F) << 0))
#define FB_OP_LOW 0
#define FB_OP_HI 1
#define FB_OP_TRI 2
//
// Fireball version info
// Note: the 16-bit ID value is located on the Fireball Apollo IOS at 0x40/0x41.
// FIREBALL_ID 0x7710
// FIREBALL2_ID 0x7712
// FIREBALL3_ID 0x7713
//
#define FIREBALL_OFFSET_ID 0x40 // (16 bits at 0x40/0x41)
#define FIREBALL_OFFSET_SWREV 0x42 // 1 byte
//
// Apollo3 IOM5 pin definitions.
// These should be defined in the BSP. If not, use these for Apollo3.
//
#if 0
#define am_bsp_pin_enable(name) \
am_hal_gpio_pin_config(AM_BSP_GPIO_ ## name, AM_BSP_GPIO_CFG_ ## name);
#define AM_BSP_GPIO_IOM5_MISO 49
#define AM_BSP_GPIO_CFG_IOM5_MISO AM_HAL_PIN_49_M5MISO
#define AM_BSP_GPIO_IOM5_MOSI 47
#define AM_BSP_GPIO_CFG_IOM5_MOSI AM_HAL_PIN_47_M5MOSI
#define AM_BSP_GPIO_IOM5_SCK 48
#define AM_BSP_GPIO_CFG_IOM5_SCK (AM_HAL_PIN_48_M5SCK | AM_HAL_GPIO_INPEN | AM_HAL_GPIO_HIGH_DRIVE)
#define AM_BSP_GPIO_FIREBALL_CE 30
#define AM_BSP_GPIO_CFG_FIREBALL_CE (AM_HAL_PIN_30_NCE30 | (AM_REG_GPIO_CFGD_GPIO30OUTCFG_M5nCE3 >> 16) | AM_HAL_GPIO_DRIVE_12MA)
#define AM_BSP_FIREBALL_CE_CHNL 3
#endif
//*****************************************************************************
//
// Globals.
//
//*****************************************************************************
static void *g_pIOM5Handle = 0;
static uint32_t g_ui32FireballID = 0;
static uint32_t g_ui32FireballFWVer = 0;
static const am_hal_iom_config_t g_FireballIOM5Config =
{
.eInterfaceMode = AM_HAL_IOM_SPI_MODE,
.ui32ClockFreq = AM_HAL_IOM_1MHZ,
.eSpiMode = AM_HAL_IOM_SPI_MODE_0,
.pNBTxnBuf = 0,
.ui32NBTxnBufLength = 0
};
static am_hal_iom_transfer_t g_sFireballTransaction =
{
.uPeerInfo.ui32SpiChipSelect = 0,
.ui32InstrLen = 0,
.ui32Instr = 0,
.ui32NumBytes = 0,
.eDirection = AM_HAL_IOM_TX,
.pui32TxBuffer = 0,
.pui32RxBuffer = 0,
.bContinue = false,
.ui8RepeatCount = 0,
.ui8Priority = 0
};
static am_hal_iom_transfer_t g_sFireballReadIDTransaction =
{
.uPeerInfo.ui32SpiChipSelect = 0,
.ui32InstrLen = 0,
.ui32Instr = 0,
.ui32NumBytes = 0,
.eDirection = AM_HAL_IOM_RX,
.pui32TxBuffer = 0,
.pui32RxBuffer = 0,
.bContinue = false,
.ui8RepeatCount = 0,
.ui8Priority = 0
};
//*****************************************************************************
//
// Prototypes.
//
//*****************************************************************************
static uint32_t fireball_id_get(uint32_t *pui32ID);
//*****************************************************************************
//
// fireball_init()
//
//*****************************************************************************
static uint32_t
fireball_init(uint32_t ui32Board, uint32_t ui32Module)
{
uint32_t ui32Ret;
if ( ui32Board != FIREBALL_BOARD_NUM )
{
return 1;
}
if ( g_pIOM5Handle )
{
//
// Already initialized
//
return 0;
}
//
// Fireball is communicated with from the Apollo3 MCU engineering board via
// SPI on IOM5.
// The 1-byte commands are defined as follows:
// [7:6] = OP
// [5:0] = GPIO number
// OP:
// 0 = Set the GPIO low.
// 1 = Set the GPIO high.
// 2 = Set the GPIO tri-state.
// 3 = Reserved.
//
// Note - the commands are handled by a macro, FB_CMD(op, pio).
//
//
// Set up IOM5 to talk to Fireball.
//
if ( am_hal_iom_initialize(ui32Module, &g_pIOM5Handle) ||
am_hal_iom_power_ctrl(g_pIOM5Handle, AM_HAL_SYSCTRL_WAKE, false) ||
am_hal_iom_configure(g_pIOM5Handle,
(am_hal_iom_config_t*)&g_FireballIOM5Config) ||
am_hal_iom_enable(g_pIOM5Handle))
{
return 2;
}
//
// Set up the pins, including CE, for Apollo3 IOM5 pins that will
// communicate with the Apollo IOS SPI device.
//
ui32Ret = am_hal_gpio_pinconfig(AM_BSP_GPIO_IOM5_SCK, g_AM_BSP_GPIO_IOM5_SCK);
ui32Ret |= am_hal_gpio_pinconfig(AM_BSP_GPIO_IOM5_MISO, g_AM_BSP_GPIO_IOM5_MISO);
ui32Ret |= am_hal_gpio_pinconfig(AM_BSP_GPIO_IOM5_MOSI, g_AM_BSP_GPIO_IOM5_MOSI);
ui32Ret |= am_hal_gpio_pinconfig(AM_BSP_GPIO_FIREBALL_CE, g_AM_BSP_GPIO_FIREBALL_CE);
if ( ui32Ret )
{
return 3;
}
//
// Initialize the write transaction structure.
// After this, we should only need to set ui32InstrLen and ui32Instr.
//
g_sFireballTransaction.uPeerInfo.ui32SpiChipSelect = AM_BSP_FIREBALL_CE_CHNL;
g_sFireballTransaction.ui32InstrLen = 0;
g_sFireballTransaction.ui32Instr = 0;
g_sFireballTransaction.ui32NumBytes = 0;
g_sFireballTransaction.eDirection = AM_HAL_IOM_TX;
g_sFireballTransaction.pui32TxBuffer = 0;
g_sFireballTransaction.pui32RxBuffer = 0;
g_sFireballTransaction.bContinue = false;
g_sFireballTransaction.ui8RepeatCount = 0;
g_sFireballTransaction.ui8Priority = 0;
//
// Initialize the read transaction structure.
// After this, we should only need to set ui32InstrLen, ui32Instr, and
// pui32RxBuffer.
//
g_sFireballReadIDTransaction.uPeerInfo.ui32SpiChipSelect = AM_BSP_FIREBALL_CE_CHNL;
g_sFireballReadIDTransaction.ui32InstrLen = 0;
g_sFireballReadIDTransaction.ui32Instr = 0;
g_sFireballReadIDTransaction.ui32NumBytes = 0;
g_sFireballReadIDTransaction.eDirection = AM_HAL_IOM_RX;
g_sFireballReadIDTransaction.pui32TxBuffer = 0;
g_sFireballReadIDTransaction.pui32RxBuffer = 0;
g_sFireballReadIDTransaction.bContinue = false;
g_sFireballReadIDTransaction.ui8RepeatCount = 0;
g_sFireballReadIDTransaction.ui8Priority = 0;
//
// Get and save the Fireball ID.
//
if ( fireball_id_get(&g_ui32FireballID) != AM_HAL_STATUS_SUCCESS )
{
return 4;
}
//
// Mask out the upper 16-bits of the ID.
//
g_ui32FireballID &= 0xFFFF;
//
// If the ID is 0xFFFF, this indicates that nothing is really out there.
//
if ( g_ui32FireballID == 0xFFFF )
{
return 5;
}
return 0;
} // fireball_init()
//*****************************************************************************
//
// fireball_deinit()
//
//*****************************************************************************
static uint32_t
fireball_deinit(uint32_t ui32Board)
{
if ( ui32Board != FIREBALL_BOARD_NUM )
{
return 1;
}
if ( g_pIOM5Handle )
{
//
// am_hal_iom_uninitialize() calls am_hal_iom_disable(), etc.
//
am_hal_iom_uninitialize(g_pIOM5Handle);
g_pIOM5Handle = 0;
//
// Disconnect pins.
//
am_hal_gpio_pinconfig(AM_BSP_GPIO_IOM5_SCK, g_AM_HAL_GPIO_DISABLE);
am_hal_gpio_pinconfig(AM_BSP_GPIO_IOM5_MISO, g_AM_HAL_GPIO_DISABLE);
am_hal_gpio_pinconfig(AM_BSP_GPIO_IOM5_MOSI, g_AM_HAL_GPIO_DISABLE);
am_hal_gpio_pinconfig(AM_BSP_GPIO_FIREBALL_CE, g_AM_HAL_GPIO_DISABLE);
}
//
// Invalidate the ID and version variables.
//
g_ui32FireballID = g_ui32FireballFWVer = 0;
return 0;
} // fireball_deinit()
//*****************************************************************************
// Return the Fireball currently in use.
// 2 = Fireball (original) or Fireball2.
// 3 = Fireball3.
//
// This function assumes that fireball_init() has already been called!
//
//*****************************************************************************
static uint32_t
FBn_get(void)
{
switch ( g_ui32FireballID )
{
case FIREBALL_ID:
case FIREBALL2_ID:
return 2;
case FIREBALL3_ID:
return 3;
default:
return 0xFFFFFFFF;
}
} // FBn_get()
//*****************************************************************************
// fireball_write_cmd()
//*****************************************************************************
static uint32_t
fireball_write_cmd(uint32_t ui32value, uint32_t ui32GPIOnum)
{
uint32_t ui32FBn = FBn_get();
uint32_t ui32Ret;
if ( ui32FBn <= 2 )
{
//
// On the Apollo slave, bit7 of the offset byte determines write or read.
// 1=write, 0=read.
//
g_sFireballTransaction.ui32InstrLen = 2;
g_sFireballTransaction.ui32Instr = (0x80 << 8) |
FB_CMD(ui32value, ui32GPIOnum);
}
else if ( ui32FBn == 3 )
{
//
// FB3 requires 3 bytes.
// Whereas FB1/2 combined the high/low values with the GPIO number,
// FB3 splits them into separate bytes.
// byte0: R/W and address, same as before. On the Apollo IOS, bit7 of
// the offset determines wr or rd (1=wr, 0=rd), the lower 7 bits
// are the address.
// byte1: 0 to set low, 1 to set high.
// byte2: GPIO number
//
g_sFireballTransaction.ui32InstrLen = 3;
g_sFireballTransaction.ui32Instr = (0x80 << 16) |
((ui32GPIOnum & 0xFF) << 8) |
((ui32value & 0x3) << 0);
}
else
{
return 0xFFFFFFFF;
}
ui32Ret = am_hal_iom_blocking_transfer(g_pIOM5Handle,
&g_sFireballTransaction);
//
// Give the FB plenty of time to process the command.
// Recommendation is 10ms (although 1ms was found to work).
//
am_hal_flash_delay(FLASH_CYCLES_US(10000));
return ui32Ret;
} // fireball_write_cmd()
//*****************************************************************************
// fireball_id_get()
//*****************************************************************************
static uint32_t
fireball_id_get(uint32_t *pui32ID)
{
//
// The ID is at offset 0x40, the SW Rev at 0x42.
// We'll read all 3 bytes here.
// On the Apollo slave, bit7 of the offset byte determines write or read.
// 1=write, 0=read.
//
g_sFireballReadIDTransaction.ui32InstrLen = 1;
g_sFireballReadIDTransaction.ui32Instr = 0x00 | FIREBALL_OFFSET_ID;
g_sFireballReadIDTransaction.ui32NumBytes = 3;
g_sFireballReadIDTransaction.pui32RxBuffer = pui32ID;
return am_hal_iom_blocking_transfer(g_pIOM5Handle,
&g_sFireballReadIDTransaction);
} // fireball_id_get()
//*****************************************************************************
// fireball_set()
//*****************************************************************************
static uint32_t
fireball_set(uint64_t ui64GPIOLowMask[2], uint64_t ui64GPIOHighMask[2])
{
uint32_t ui32GPIOnum, ui32Ret;
ui32GPIOnum = 0;
while ( ui64GPIOLowMask[0] || ui64GPIOHighMask[0] )
{
if ( ui64GPIOLowMask[0] & 0x1 )
{
ui32Ret = fireball_write_cmd(FB_OP_LOW, ui32GPIOnum);
if ( ui32Ret )
{
return (1 << 8) | ui32Ret;
}
//
// Give the Fireball Apollo a little time to process the command.
//
am_hal_flash_delay(FLASH_CYCLES_US(1));
}
ui64GPIOLowMask[0] >>= 1;
if ( ui64GPIOHighMask[0] & 0x1 )
{
ui32Ret = fireball_write_cmd(FB_OP_HI, ui32GPIOnum);
if ( ui32Ret )
{
return (2 << 8) | ui32Ret;
}
//
// Give the Fireball Apollo a little time to process the command.
//
am_hal_flash_delay(FLASH_CYCLES_US(1));
}
ui64GPIOHighMask[0] >>= 1;
ui32GPIOnum++;
}
//
// FB3 requires more than 64 pins.
// Repeat the previous loop to handle the extended pins.
//
ui32GPIOnum = 64;
while ( ui64GPIOLowMask[1] || ui64GPIOHighMask[1] )
{
if ( ui64GPIOLowMask[1] & 0x1 )
{
ui32Ret = fireball_write_cmd(FB_OP_LOW, ui32GPIOnum);
if ( ui32Ret )
{
return (1 << 8) | ui32Ret;
}
//
// Give the Fireball Apollo a little time to process the command.
//
am_hal_flash_delay(FLASH_CYCLES_US(1));
}
ui64GPIOLowMask[1] >>= 1;
if ( ui64GPIOHighMask[1] & 0x1 )
{
ui32Ret = fireball_write_cmd(FB_OP_HI, ui32GPIOnum);
if ( ui32Ret )
{
return (2 << 8) | ui32Ret;
}
//
// Give the Fireball Apollo a little time to process the command.
//
am_hal_flash_delay(FLASH_CYCLES_US(1));
}
ui64GPIOHighMask[1] >>= 1;
ui32GPIOnum++;
}
return 0;
} // fireball_set()
//*****************************************************************************
// device_reset()
//*****************************************************************************
static uint32_t
device_reset(uint32_t ui32GPIO, bool bAssertLow)
{
uint32_t ui32Assert, ui32Deassert;
ui32Assert = bAssertLow ? FB_OP_LOW : FB_OP_HI;
ui32Deassert = bAssertLow ? FB_OP_HI : FB_OP_LOW;
//
// Assert the reset signal.
//
if ( fireball_write_cmd(ui32Assert, ui32GPIO) )
{
return 1;
}
//
// Reset pulse width. (1ms)
//
am_hal_flash_delay(FLASH_CYCLES_US(1000));
//
// De-assert the reset signal.
//
if ( fireball_write_cmd(ui32Deassert, ui32GPIO) )
{
return 3;
}
return 0;
} // device_reset()
//*****************************************************************************
//
// am_devices_fireball_control()
// Set FIREBALL state.
//
//*****************************************************************************
uint32_t
am_devices_fireball_control(am_devices_fireball_control_e eControl,
void *pArgs)
{
// FB3 requires more than 64 pin masks for some settings.
uint64_t ui64GPIOLowMask[2], ui64GPIOHighMask[2];
uint32_t ui32RetVal = AM_HAL_STATUS_SUCCESS;
if ( eControl >= AM_DEVICES_FIREBALL_STATE_INVALID )
{
return 1;
}
if ( fireball_init(FIREBALL_BOARD_NUM, FIREBALL_IOM_MODULE) )
{
return 2;
}
//
// Validate eControl against the detected Fireball board.
//
if ( (eControl >= AM_DEVICES_FIREBALL_STATE_FIRST) &&
(eControl <= AM_DEVICES_FIREBALL_STATE_LAST) )
{
//
// Make sure we're running on an original Fireball.
//
if (( g_ui32FireballID != FIREBALL_ID ) &&
( g_ui32FireballID != FIREBALL2_ID ) &&
( g_ui32FireballID != FIREBALL3_ID ))
{
fireball_deinit(FIREBALL_BOARD_NUM);
return 3;
}
}
else if ( (eControl >= AM_DEVICES_FIREBALL2_STATE_FIRST) &&
(eControl <= AM_DEVICES_FIREBALL2_STATE_LAST) )
{
//
// Make sure we're running on a Fireball 2.
//
if ( g_ui32FireballID != FIREBALL2_ID )
{
fireball_deinit(FIREBALL_BOARD_NUM);
return 4;
}
}
else if ( (eControl >= AM_DEVICES_FIREBALL3_STATE_FIRST) &&
(eControl <= AM_DEVICES_FIREBALL3_STATE_LAST) )
{
//
// Make sure we're running on a Fireball 3.
//
if ( g_ui32FireballID != FIREBALL3_ID )
{
fireball_deinit(FIREBALL_BOARD_NUM);
return 5;
}
}
//
// Initialize the mask variables to assume that we will NOT be setting the
// Fireball state. Both variables have to be touched.
//
ui64GPIOLowMask[0] = ui64GPIOHighMask[0] =
ui64GPIOLowMask[1] = ui64GPIOHighMask[1] = 0xFFFFFFFF;
if ( FBn_get() >= 3 )
{
//
// If we're running on a FB3 but a FB2 state was specified, convert it
// to a FB3 state. Note that the states that are compatible between the
// Fireball 2 and 3 are expected to be in the same order and offset by
// a known quantity.
//
if ( (eControl >= AM_DEVICES_FIREBALL2_STATE_FIRST) &&
(eControl <= AM_DEVICES_FIREBALL2_STATE_LAST) )
{
eControl += (AM_DEVICES_FIREBALL3_STATE_FIRST - AM_DEVICES_FIREBALL2_STATE_FIRST);
}
}
switch ( eControl )
{
// ********************************************************************
//
// Begin Fireball(1) commands
//
// ********************************************************************
case AM_DEVICES_FIREBALL_STATE_FBGEN_GET:
if ( pArgs )
{
uint32_t ui32FBrev = 0;
ui32RetVal = fireball_id_get(&g_ui32FireballID);
g_ui32FireballFWVer = ui32RetVal ? 0 : (g_ui32FireballID >> 16) & 0xFF;
g_ui32FireballID = ui32RetVal ? 0 : g_ui32FireballID & 0xFFFF;
if ( g_ui32FireballID == FIREBALL_ID )
{
ui32FBrev = 1; // Original Fireball
}
else if ( g_ui32FireballID == FIREBALL2_ID )
{
ui32FBrev = 2; // Fireball2
}
else if ( g_ui32FireballID == FIREBALL3_ID )
{
ui32FBrev = 3; // Fireball3
}
*((uint32_t*)pArgs) = ui32FBrev;
}
else
{
ui32RetVal = AM_HAL_STATUS_INVALID_ARG;
}
break;
case AM_DEVICES_FIREBALL_STATE_ID_GET:
if ( pArgs )
{
ui32RetVal = fireball_id_get(&g_ui32FireballID);
g_ui32FireballFWVer = ui32RetVal ? 0 : (g_ui32FireballID >> 16) & 0xFF;
g_ui32FireballID = ui32RetVal ? 0 : g_ui32FireballID & 0xFFFF;
*((uint32_t*)pArgs) = g_ui32FireballID;
}
else
{
ui32RetVal = AM_HAL_STATUS_INVALID_ARG;
}
break;
case AM_DEVICES_FIREBALL_STATE_VER_GET:
if ( pArgs )
{
ui32RetVal = fireball_id_get(&g_ui32FireballFWVer);
g_ui32FireballID = ui32RetVal ? 0 : g_ui32FireballFWVer & 0xFFFF;
g_ui32FireballFWVer = ui32RetVal ? 0 : (g_ui32FireballFWVer >> 16) & 0xFF;
*((uint32_t*)pArgs) = g_ui32FireballFWVer;
}
else
{
ui32RetVal = AM_HAL_STATUS_INVALID_ARG;
}
break;
case AM_DEVICES_FIREBALL_STATE_LED_BLINK:
if ( pArgs )
{
uint32_t ui32Cnt = *(uint32_t*)pArgs;
while (ui32Cnt)
{
ui32RetVal = fireball_write_cmd(1, 39);
am_hal_flash_delay(FLASH_CYCLES_US(100000)); // 100ms
ui32RetVal = fireball_write_cmd(0, 39);
am_hal_flash_delay(FLASH_CYCLES_US(100000)); // 100ms
ui32Cnt--;
}
}
else
{
ui32RetVal = AM_HAL_STATUS_INVALID_ARG;
}
break;
case AM_DEVICES_FIREBALL_STATE_SPI_FLASH:
//
// GPIO LOW: 4-7,10,11
// GPIO HIGH: -
//
ui64GPIOLowMask[0] = ((uint64_t)0xF << 4) | ((uint64_t)0x3 << 10);
ui64GPIOHighMask[0] = ((uint64_t)0x0 << 0);
break;
case AM_DEVICES_FIREBALL_STATE_SPI_FRAM:
//
// GPIO LOW: 4-7,10
// GPIO HIGH: 11
//
ui64GPIOLowMask[0] = ((uint64_t)0xF << 4) | ((uint64_t)0x1 << 10);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 11);
break;
case AM_DEVICES_FIREBALL_STATE_I2C_IOM0:
//
// GPIO LOW: 5,12-13
// GPIO HIGH: 4
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 5) | ((uint64_t)0x3 << 12);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4);
break;
case AM_DEVICES_FIREBALL_STATE_I2C_IOM1:
//
// GPIO LOW: 5,12
// GPIO HIGH: 4,13
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 5) | ((uint64_t)0x1 << 12);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 13);
break;
case AM_DEVICES_FIREBALL_STATE_I2C_IOM2:
//
// GPIO LOW: 5,13
// GPIO HIGH: 4,12
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 5) | ((uint64_t)0x1 << 13);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 12);
break;
case AM_DEVICES_FIREBALL_STATE_I2C_IOM3:
//
// GPIO LOW: 5,14-15
// GPIO HIGH: 4
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 5) | ((uint64_t)0x3 << 14);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4);
break;
case AM_DEVICES_FIREBALL_STATE_I2C_IOM4:
//
// GPIO LOW: 5,14
// GPIO HIGH: 4,15
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 5) | ((uint64_t)0x1 << 14);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 15);
break;
case AM_DEVICES_FIREBALL_STATE_I2C_IOM5:
//
// GPIO LOW: 5,15
// GPIO HIGH: 4,14
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 5) | ((uint64_t)0x1 << 15);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 14);
break;
case AM_DEVICES_FIREBALL_STATE_OCTAL_FLASH_CE0:
//
// GPIO LOW: 8-9,35-36
// GPIO HIGH: 34,37
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 8) | ((uint64_t)0x3 << 35);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 34) | ((uint64_t)0x1 << 37);
break;
case AM_DEVICES_FIREBALL_STATE_OCTAL_FLASH_CE1:
//
// GPIO LOW: 8-9,37
// GPIO HIGH: 36
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 8) | ((uint64_t)0x1 << 37);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 36);
break;
case AM_DEVICES_FIREBALL_STATE_TWIN_QUAD_CE0_CE1:
//
// GPIO LOW: 8-9,35-37
// GPIO HIGH: 34
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 8) | ((uint64_t)0x7 << 35);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 34);
break;
case AM_DEVICES_FIREBALL_STATE_ALL_RESET:
//
// SX9300: 43
// BNO055: 42
// MSPI: 41
// MKB1: 40
// MKB2: 39
//
device_reset(43, true);
device_reset(42, true);
device_reset(41, true);
device_reset(40, false);
device_reset(39, false);
ui32RetVal = 0;
break;
case AM_DEVICES_FIREBALL_STATE_SX9300_RESET:
ui32RetVal = device_reset(43, true);
break;
case AM_DEVICES_FIREBALL_STATE_BNO055_RESET:
ui32RetVal = device_reset(42, true);
break;
case AM_DEVICES_FIREBALL_STATE_MSPI_RESET:
ui32RetVal = device_reset(41, true);
break;
case AM_DEVICES_FIREBALL_STATE_MKB1_RESET:
//
// The 2 click reset signals actually drive a transistor.
// Therefore, the signal ends up inverted.
//
ui32RetVal = device_reset(40, false);
break;
case AM_DEVICES_FIREBALL_STATE_MKB2_RESET:
//
// The 2 click reset signals actually drive a transistor.
// Therefore, the signal ends up inverted.
//
ui32RetVal = device_reset(39, false);
break;
// ********************************************************************
//
// Begin Fireball2 commands
//
// ********************************************************************
case AM_DEVICES_FIREBALL2_STATE_SPI_FRAM_PSRAM_1P8:
//
// GPIO LOW: 4-7,10
// GPIO HIGH: 11
//
ui64GPIOLowMask[0] = ((uint64_t)0xF << 4) | ((uint64_t)0x1 << 10);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 11);
break;
case AM_DEVICES_FIREBALL2_STATE_SPI_PSRAM_FLASH_3P3:
//
// GPIO LOW: 4,6,10-11
// GPIO HIGH: 5,7
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 10) | ((uint64_t)0x5 << 4);
ui64GPIOHighMask[0] = ((uint64_t)0x5 << 5);
break;
case AM_DEVICES_FIREBALL2_STATE_I2C_IOM0:
//
// GPIO LOW: 5,12,13
// GPIO HIGH: 4
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 12) | ((uint64_t)0x1 << 5);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4);
break;
case AM_DEVICES_FIREBALL2_STATE_I2C_IOM1:
//
// GPIO LOW: 5,12
// GPIO HIGH: 4,13
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 12) | ((uint64_t)0x1 << 5);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 13);
break;
case AM_DEVICES_FIREBALL2_STATE_I2C_IOM2:
//
// GPIO LOW: 5,13
// GPIO HIGH: 4,12
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 13) | ((uint64_t)0x1 << 5);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 12);
break;
case AM_DEVICES_FIREBALL2_STATE_I2C_IOM3:
//
// GPIO LOW: 5,14,15
// GPIO HIGH: 4
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 14) | ((uint64_t)0x1 << 5);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4);
break;
case AM_DEVICES_FIREBALL2_STATE_I2C_IOM4:
//
// GPIO LOW: 5,14
// GPIO HIGH: 4,15
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 14) | ((uint64_t)0x1 << 5);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 15);
break;
case AM_DEVICES_FIREBALL2_STATE_I2C_IOM5:
//
// GPIO LOW: 5,15
// GPIO HIGH: 4,14
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 15) | ((uint64_t)0x1 << 5);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 4) | ((uint64_t)0x1 << 14);
break;
case AM_DEVICES_FIREBALL2_STATE_MSPI_FRAM_PSRAM_FLASH_1P8:
//
// GPIO LOW: 8,9,35,36
// GPIO HIGH: 34,37
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 35) | ((uint64_t)0x3 << 8);
ui64GPIOHighMask[0] = ((uint64_t)0x9 << 34);
break;
case AM_DEVICES_FIREBALL2_STATE_MSPI_PSRAM_FLASH_3P3:
//
// GPIO LOW: 8,35,37
// GPIO HIGH: 9,34,36
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 8) | ((uint64_t)0x5 << 35);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 9) | ((uint64_t)0x5 << 34);
break;
case AM_DEVICES_FIREBALL2_STATE_SC_8_9_16:
//
// GPIO LOW: 4,11,16,17,22,23
// GPIO HIGH: 5,10
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 22) | ((uint64_t)0x3 << 16) | ((uint64_t)0x1 << 11) | ((uint64_t)0x1 << 4);
ui64GPIOHighMask[0] = ((uint64_t)0x5 << 10) | ((uint64_t)0x1 << 5);
break;
case AM_DEVICES_FIREBALL2_STATE_SC_17_32_26:
//
// GPIO LOW: 9,16,22
// GPIO HIGH: 8,17,23
//
ui64GPIOLowMask[0] = ((uint64_t)0x1 << 22) | ((uint64_t)0x1 << 16) | ((uint64_t)0x1 << 9);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 23) | ((uint64_t)0x1 << 17) | ((uint64_t)0x1 << 8);
break;
case AM_DEVICES_FIREBALL2_STATE_SC_19_18_46:
//
// GPIO LOW: 18-19,24-25,31
// GPIO HIGH:
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 18) | ((uint64_t)0x3 << 24) | ((uint64_t)0x1 << 31);
ui64GPIOHighMask[0] = 0;
break;
case AM_DEVICES_FIREBALL2_STATE_SC_31_37_21:
//
// GPIO LOW: 18,24,30-31
// GPIO HIGH: 19,25
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 30) | ((uint64_t)0x1 << 24) | ((uint64_t)0x1 << 18);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 25) | ((uint64_t)0x1 << 19);
break;
case AM_DEVICES_FIREBALL2_STATE_PDM_10_29:
//
// GPIO LOW: 7,26-27
// GPIO HIGH: 6
//
ui64GPIOLowMask[0] = ((uint64_t)0x3 << 26) | ((uint64_t)0x1 << 7);
ui64GPIOHighMask[0] = ((uint64_t)0x1 << 6);
break;
case AM_DEVICES_FIREBALL2_STATE_PDM_12_15:
//