forked from devanlai/dap42
-
Notifications
You must be signed in to change notification settings - Fork 6
/
target.c
1518 lines (1281 loc) · 49.1 KB
/
target.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
/*
* Copyright (c) 2016, Devan Lai
* Copyright (c) 2019, Unwired Devices LLC <info@unwds.com>
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice
* appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/crs.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/stm32/flash.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/dma.h>
#include <stdio.h>
#include <string.h>
#include "tick.h"
#include "target.h"
#include "config.h"
#include "console.h"
#include "DAP/CMSIS_DAP_config.h"
#include "DAP/CMSIS_DAP.h"
#include "DFU/DFU.h"
#include "USB/vcdc.h"
#include "tic33m.h"
#define ENABLE_DEBUG (0)
#define FLASH_CONFIG_PAGE 31 /* last of 32 pages */
#define FLASH_CONFIG_ADDR (FLASH_BASE + 1024*FLASH_CONFIG_PAGE)
#define FLASH_CONFIG_MAGIC 0xDEADF00D
#define TIM2_PRESCALER_3US 75
#define TIM2_PRESCALER_10US 225
#define TIM2_PRESCALER_50US 1250
#define TIM2_PRESCALER TIM2_PRESCALER_3US
#define ADC_SAMPLE_TIME ADC_SMPTIME_007DOT5
#define DMA_DATA_SIZE 400
static uint16_t dma_data[DMA_DATA_SIZE];
static tic33m tic33m_dev;
/*
* Divide positive or negative dividend by positive divisor and round
* to closest integer. Result is undefined for negative divisors and
* for negative dividends if the divisor variable type is unsigned.
*/
#define DIV_ROUND_CLOSEST(x, divisor)( \
{ \
typeof(x) __x = x; \
typeof(divisor) __d = divisor; \
(((typeof(x))-1) > 0 || \
((typeof(divisor))-1) > 0 || (__x) > 0) ? \
(((__x) + ((__d) / 2)) / (__d)) : \
(((__x) - ((__d) / 2)) / (__d)); \
} \
)
/* Reconfigure processor settings */
void cpu_setup(void) {
}
/* Set STM32 to 48 MHz. */
void clock_setup(void) {
rcc_clock_setup_in_hsi48_out_48mhz();
/* Trim from USB sync frame */
crs_autotrim_usb_enable();
rcc_set_usbclk_source(RCC_HSI48);
}
/* 1000 Hz frequency */
static const uint16_t frequency = 1;
/* 1000 ms blink period */
static const uint32_t blink_period_boot = 1000;
static uint32_t button1_counter = 0;
static uint32_t button2_counter = 0;
static uint32_t button3_counter = 0;
static uint16_t target_release_reset = 0;
static uint16_t target_release_boot = 0;
static uint16_t target_start_measurements = 0;
static bool target_power_state = false;
static bool target_boot_state = false;
static uint32_t blink_counter = 0;
static uint16_t target_power_failure = 0;
static volatile uint32_t adc_voltage_raw = 0;
static volatile uint32_t seconds_passed = 0;
static bool display_counter = false;
static uint32_t display_mode = 0;
static uint32_t vdda = 0;
static uint32_t current_report_counter = 0;
static bool is_interface_connected = false;
static uint64_t energy_accumultated_uwh = 0;
static uint64_t energy_accumultated_uah = 0;
static uint32_t energy_ahr = 0;
static uint32_t energy_whr = 0;
static uint32_t current_max_ua = 0;
static bool update_display = false;
static bool banner_displayed = false;
static uint32_t do_calibrate = 0;
static volatile uint32_t cal_voltage = 0;
static volatile int current_power_range = 0;
static volatile uint8_t cmd_int = 0;
static volatile bool dap_connected = false;
static volatile bool board_v2 = false;
#define USB_COMMAND_SIZE 20
static volatile struct {
uint32_t current[3];
uint32_t raw_current[3];
uint32_t count[3];
uint32_t voltage;
uint32_t vcount;
} adc_data;
typedef enum {
SHOW_SECONDS = 1 << 0,
SHOW_VOLTAGE = 1 << 1,
SHOW_CURRENT = 1 << 2,
SHOW_AMPEREHOURS = 1 << 3,
SHOW_WATTHOURS = 1 << 4,
} show_values_t;
typedef enum {
CMD_INT_CALIBRATE = 1 << 0,
CMD_INT_CONSOLEOUT = 1 << 1,
CMD_INT_LCDOUT = 1 << 2,
} internal_commands_t;
/* emb_settings size must be multiple of 4 */
static volatile struct {
uint32_t magic;
uint32_t voltage_coeff;
uint32_t period;
uint32_t show;
uint32_t baudrate;
uint32_t dap_active;
} emb_settings;
/* last flash page (1KB on STM32F042) is for settings */
/* so max firmware size is 32768 - 1024 = 31744 bytes */
static void save_settings(void) {
flash_unlock();
flash_erase_page(FLASH_CONFIG_ADDR);
flash_program_word(FLASH_CONFIG_ADDR, FLASH_CONFIG_MAGIC);
uint32_t *settings = (void *)&emb_settings;
for (unsigned i = 1; i < sizeof(emb_settings)/4; i++) {
flash_program_word(FLASH_CONFIG_ADDR + i*4, settings[i]);
}
flash_lock();
vcdc_println("[INF] Settings saved");
}
/* stop measurements on DAP connect */
void DAP_On_Connect(void) {
vcdc_println("[INF] DAP connect");
if (!emb_settings.dap_active) {
dap_connected = true;
timer_disable_counter(TIM2);
}
}
/* restart measurements on DAP connect */
void DAP_On_Disconnect(void) {
vcdc_println("[INF] DAP disconnect");
if (!emb_settings.dap_active) {
dap_connected = false;
timer_enable_counter(TIM2);
}
}
static void disable_power(void) {
/* Stop TIM2 */
timer_disable_counter(TIM2);
/* disable DMA channel */
dma_disable_channel(DMA1, DMA_CHANNEL1);
adc_disable_dma(ADC1);
/* Stop ADC */
adc_power_off(ADC1);
/* Disable output power */
gpio_clear(POWER_OUTPUT_EN_PORT, POWER_OUTPUT_EN_PIN);
target_power_state = false;
target_boot_state = false;
gpio_clear(CURRENT_RANGE0_PORT, CURRENT_RANGE0_PIN);
gpio_clear(CURRENT_RANGE1_PORT, CURRENT_RANGE1_PIN);
gpio_clear(CURRENT_RANGE2_PORT, CURRENT_RANGE2_PIN);
vcdc_println("[INF] power disabled");
}
static void adc_setup_common(void) {
rcc_periph_clock_enable(RCC_ADC1);
gpio_mode_setup(CURRENT_SENSE_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, CURRENT_SENSE_PIN);
adc_set_operation_mode(ADC1, ADC_MODE_SCAN);
adc_disable_discontinuous_mode(ADC1);
adc_enable_external_trigger_regular(ADC1, ADC_CFGR1_EXTSEL_TIM2_TRGO, ADC_CFGR1_EXTEN_RISING_EDGE);
adc_set_right_aligned(ADC1);
adc_disable_temperature_sensor();
adc_disable_dma(ADC1);
adc_set_resolution(ADC1, ADC_RESOLUTION_12BIT);
/* set 1 us sampling time */
adc_set_sample_time_on_all_channels(ADC1, ADC_SAMPLE_TIME);
/* Disable end of conversion IRQ */
adc_disable_eoc_interrupt(ADC1);
/* Enable VREF */
adc_enable_vrefint();
/* Enable analog watchdog on PB0 */
adc_enable_analog_watchdog_on_selected_channel(ADC1, 8);
ADC_TR1(ADC1) = (ADC_TR1(ADC1) & ~ADC_TR1_HT) | ADC_TR1_HT_VAL(CURRENT_HIGHER_THRESHOLD);
ADC_TR1(ADC1) = (ADC_TR1(ADC1) & ~ADC_TR1_LT) | ADC_TR1_LT_VAL(CURRENT_LOWER_THRESHOLD);
/* Analog watchdog interrupt highest priority */
nvic_set_priority(NVIC_ADC_COMP_IRQ, 0);
nvic_enable_irq(NVIC_ADC_COMP_IRQ);
/* DMA interrupt with medium priority */
nvic_set_priority(NVIC_DMA1_CHANNEL1_IRQ, 64);
nvic_enable_irq(NVIC_DMA1_CHANNEL1_IRQ);
/* SysTick interrupt with low priority */
nvic_set_priority(NVIC_SYSTICK_IRQ, 128);
rcc_periph_clock_enable(RCC_DMA1);
dma_channel_reset(DMA1, DMA_CHANNEL1);
/* Medium priority. */
dma_set_priority(DMA1, DMA_CHANNEL1, DMA_CCR_PL_MEDIUM);
/* ADC is 16 bit */
dma_set_memory_size(DMA1, DMA_CHANNEL1, DMA_CCR_MSIZE_16BIT);
dma_set_peripheral_size(DMA1, DMA_CHANNEL1, DMA_CCR_PSIZE_16BIT);
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL1);
dma_disable_peripheral_increment_mode(DMA1, DMA_CHANNEL1);
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL1);
dma_enable_half_transfer_interrupt(DMA1, DMA_CHANNEL1);
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL1);
dma_set_peripheral_address(DMA1, DMA_CHANNEL1, (uint32_t)(&ADC1_DR));
dma_set_memory_address(DMA1, DMA_CHANNEL1, (uint32_t)(dma_data));
dma_set_number_of_data(DMA1, DMA_CHANNEL1, DMA_DATA_SIZE);
dma_enable_circular_mode(DMA1, DMA_CHANNEL1);
dma_enable_channel(DMA1, DMA_CHANNEL1);
/* overwrite data in case of overrun */
ADC_CFGR1(ADC1) |= ADC_CFGR1_OVRMOD;
/* enable DMA circular mode */
ADC_CFGR1(ADC1) |= ADC_CFGR1_DMACFG;
}
static void adc_measure_current(void) {
/* stop any ongoing conversion */
ADC_CR(ADC1) = ADC_CR_ADSTP;
while (ADC_CR(ADC1) & ADC_CR_ADSTP) { }
/* Measurements to be triggered by TIM2 */
adc_enable_external_trigger_regular(ADC1, ADC_CFGR1_EXTSEL_TIM2_TRGO, ADC_CFGR1_EXTEN_RISING_EDGE);
/* PB0 channel */
uint8_t adc_channels = 8;
adc_set_regular_sequence(ADC1, 1, &adc_channels);
/* Enable watchdog interrupt */
adc_enable_watchdog_interrupt(ADC1);
adc_enable_dma(ADC1);
/* start ADC measurements */
adc_start_conversion_regular(ADC1);
/* start TIM2 */
timer_enable_counter(TIM2);
}
static void adc_measure_vdda(void) {
/* stop any ongoing conversion */
ADC_CR(ADC1) = ADC_CR_ADSTP;
while (ADC_CR(ADC1) & ADC_CR_ADSTP) { }
adc_disable_dma(ADC1);
/* disable ADC */
adc_power_off(ADC1);
/* calibration and clock source selection must be done with ADC disabled */
adc_set_clk_source(ADC1, ADC_CLKSOURCE_ADC);
adc_calibrate(ADC1);
/* enable ADC */
adc_power_on(ADC1);
/* ~5 us sampling time */
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPTIME_071DOT5);
/* ADC will be run once */
adc_disable_external_trigger_regular(ADC1);
/* disable analog watchdog interrupt */
adc_disable_watchdog_interrupt(ADC1);
/* VREF channel only */
uint8_t adc_channels = 17;
adc_set_regular_sequence(ADC1, 1, &adc_channels);
/* discard old data if present */
adc_read_regular(ADC1);
/* start ADC and wait for it to finish */
adc_start_conversion_regular(ADC1);
while (!(adc_eoc(ADC1)));
/* VREF in ADC counts */
vdda = adc_read_regular(ADC1);
uint16_t cal_vref = ST_VREFINT_CAL;
/* VDDA in millivolts */
vdda = (3300 * cal_vref) / vdda;
char vdda_str[20];
snprintf(vdda_str, 20, "[VDD] %lu", vdda);
vcdc_println(vdda_str);
/* set 1 us sampling time */
adc_set_sample_time_on_all_channels(ADC1, ADC_SAMPLE_TIME);
}
static uint32_t adc_measure_voltage(void) {
/* stop any ongoing conversion */
ADC_CR(ADC1) = ADC_CR_ADSTP;
while (ADC_CR(ADC1) & ADC_CR_ADSTP) { }
adc_disable_dma(ADC1);
/* ADC will be run once */
adc_disable_external_trigger_regular(ADC1);
/* disable analog watchdog interrupt */
adc_disable_watchdog_interrupt(ADC1);
/* VREF channel only */
uint8_t adc_channels = 9;
adc_set_regular_sequence(ADC1, 1, &adc_channels);
/* discard old data if present */
adc_read_regular(ADC1);
/* start ADC and wait for it to finish */
adc_start_conversion_regular(ADC1);
while (!(adc_eoc(ADC1)));
uint32_t adc_read = adc_read_regular(ADC1);
uint32_t voltage = (emb_settings.voltage_coeff * vdda * adc_read) / (4095*10);
return voltage;
}
static void calibrate_voltage(uint32_t cal_value) {
adc_measure_vdda();
/* PB1 channel only */
uint8_t adc_channels = 9;
adc_set_regular_sequence(ADC1, 1, &adc_channels);
uint32_t voltage_mv = 0;
for (int i = 0; i < 10; i++) {
/* start ADC and wait for it to finish */
adc_start_conversion_regular(ADC1);
while (!(adc_eoc(ADC1)));
voltage_mv += adc_read_regular(ADC1);
/* 100 us delay between measurements */
volatile uint32_t k = 4800;
do {
k--;
} while (k);
}
voltage_mv = (vdda * voltage_mv) / 4095;
/* coeff is x10 for better accuracy */
emb_settings.voltage_coeff = (100 * cal_value)/voltage_mv;
char coeff_str[15];
snprintf(coeff_str, 15, "[CAL] %lu", emb_settings.voltage_coeff);
vcdc_println(coeff_str);
adc_power_off(ADC1);
save_settings();
}
void dma1_channel1_isr(void) {
/* using local variables for data processing */
uint32_t data = 0;
uint32_t range = current_power_range;
/* half transfer event */
if ((DMA1_ISR & DMA_ISR_HTIF1) != 0) {
for (int i = 0; i < DMA_DATA_SIZE/2; i++) {
data += dma_data[i];
}
data = DIV_ROUND_CLOSEST(data, DMA_DATA_SIZE/2);
adc_data.raw_current[range] += data;
adc_data.count[range] += 1;
}
/* transfer completed event */
if ((DMA1_ISR & DMA_ISR_TCIF1) != 0) {
for (int i = DMA_DATA_SIZE/2; i < DMA_DATA_SIZE; i++) {
data += dma_data[i];
}
data = DIV_ROUND_CLOSEST(data, DMA_DATA_SIZE/2);
adc_data.raw_current[range] += data;
adc_data.count[range] += 1;
}
/* clear DMA interrupt flags */
DMA1_IFCR |= DMA_IFCR_CGIF1;
}
/* analog watchdog interrupt */
void adc_comp_isr(void)
{
/* stop ADC timer */
TIM_CR1(TIM2) &= ~TIM_CR1_CEN;
uint16_t adc_sample = ADC_DR(ADC1);
/* do not switch ranges if it was a short glitch or other ISR (should not happen) */
if (!(ADC_ISR(ADC1) & ADC_ISR_AWD1) ||
((adc_sample > CURRENT_LOWER_THRESHOLD) && (adc_sample < CURRENT_HIGHER_THRESHOLD))) {
/* reset ADC watchdog flag */
ADC_ISR(ADC1) |= ADC_ISR_AWD1;
/* restart timer */
TIM_CR1(TIM2) |= TIM_CR1_CEN;
return;
}
/* disable DMA channel */
DMA_CCR(DMA1, DMA_CHANNEL1) &= ~DMA_CCR_EN;
/* using local variables for data processing */
int range = current_power_range;
int delta = 0;
while (ADC_ISR(ADC1) & ADC_ISR_AWD1)
{
/* reset ADC watchdog flag */
ADC_ISR(ADC1) |= ADC_ISR_AWD1;
if (adc_sample < CURRENT_LOWER_THRESHOLD) {
if ((range + delta) != 0) {
if ((range + delta) == 2) {
/* make-before-break! */
GPIO_BSRR(CURRENT_RANGE1_PORT) = CURRENT_RANGE1_PIN;
GPIO_BRR(CURRENT_RANGE2_PORT) = CURRENT_RANGE2_PIN;
/* reenable watchdog high threshold */
ADC_TR1(ADC1) = (ADC_TR1(ADC1) & ~ADC_TR1_HT) | ADC_TR1_HT_VAL(CURRENT_HIGHER_THRESHOLD);
}
else { /* range 1 */
/* disable watchdog low threshold by setting it to 0 */
ADC_TR1(ADC1) = (ADC_TR1(ADC1) & ~ADC_TR1_LT);
/* make-before-break! */
GPIO_BSRR(CURRENT_RANGE0_PORT) = CURRENT_RANGE0_PIN;
GPIO_BRR(CURRENT_RANGE1_PORT) = CURRENT_RANGE1_PIN;
}
delta--;
} else { /* lowest range already */
}
} else { /* adc_sample > CURRENT_HIGHER_THRESHOLD */
if ((range + delta) != 2) {
if ((range + delta) == 0) {
/* make-before-break! */
GPIO_BSRR(CURRENT_RANGE1_PORT) = CURRENT_RANGE1_PIN;
GPIO_BRR(CURRENT_RANGE0_PORT) = CURRENT_RANGE0_PIN;
/* reenable watchdog low threshold */
ADC_TR1(ADC1) |= ADC_TR1_LT_VAL(CURRENT_LOWER_THRESHOLD);
} else { /* range 2 */
/* make-before-break! */
GPIO_BSRR(CURRENT_RANGE2_PORT) = CURRENT_RANGE2_PIN;
GPIO_BRR(CURRENT_RANGE1_PORT) = CURRENT_RANGE1_PIN;
/* disable watchdog high threshold by setting it to maximum */
ADC_TR1(ADC1) |= ADC_TR1_HT;
}
delta++;
}
}
if ((range + delta) == 1) {
/* restart data acquisition with small delay */
ADC_ISR(ADC1) |= ADC_ISR_EOC;
TIM_CNT(TIM2) = 0;
TIM_CR1(TIM2) |= TIM_CR1_CEN;
/* sample ADC to check if another correction is needed */
while (!(ADC_ISR(ADC1) & ADC_ISR_EOC));
TIM_CR1(TIM2) &= ~TIM_CR1_CEN;
ADC_ISR(ADC1) |= ADC_ISR_EOC;
} else {
break;
}
}
current_power_range += delta;
uint32_t data = 0;
uint32_t size = DMA_DATA_SIZE - DMA_CNDTR(DMA1, DMA_CHANNEL1);
/* copy data to temporary array if needed */
uint16_t data_tmp[DMA_DATA_SIZE/2];
uint16_t *data_ptr;
if (size) {
if (size > DMA_DATA_SIZE/2) {
size -= DMA_DATA_SIZE/2;
data_ptr = &dma_data[DMA_DATA_SIZE/2];
} else {
data_ptr = data_tmp;
memcpy((void *)data_ptr, (void *)dma_data, size * sizeof(uint16_t));
}
}
/* reset DMA channel data counter and interrupt flags */
DMA_CNDTR(DMA1, DMA_CHANNEL1) = DMA_DATA_SIZE;
DMA1_IFCR |= DMA_IFCR_CGIF1;
/* reenable DMA channel */
DMA_CCR(DMA1, DMA_CHANNEL1) |= DMA_CCR_EN;
/* restart acquisition timer */
TIM_CNT(TIM2) = 0;
TIM_CR1(TIM2) |= TIM_CR1_CEN;
/* clear possible pending watchdog interrupt */
NVIC_ICPR(NVIC_ADC_COMP_IRQ / 32) = (1 << (NVIC_ADC_COMP_IRQ % 32));
/* process data */
if (size) {
for (uint32_t i = 0; i < size; i++) {
data += data_ptr[i];
}
data = DIV_ROUND_CLOSEST(data, size);
adc_data.raw_current[range] += data;
adc_data.count[range] += 1;
}
}
static void console_command_parser(uint8_t *usb_command) {
const char *help_period = "period <ms> - set period in milliseconds, 10 to 1000";
const char *help_iface = "iface <on|off> - enable/disable UART and SWD interfaces";
const char *help_power = "power <on|off> - enable/disable onboard DC/DC";
const char *help_maxreset = "maxreset - reset maximum current";
const char *help_display = "display <N> - set display mode by number";
const char *help_calibrate = "calibrate <mV> - calibrate voltage divider";
const char *help_show = "show <SEC|VOL|CUR|AHR|WHR|all> - report values";
const char *help_hide = "hide <SEC|VOL|CUR|AHR|WHR|all> - don't report values";
const char *help_baudrate = "baudrate <bps> - set target UART baudrate";
const char *help_reset = "reset - reset target";
const char *help_boot = "boot - switch target to bootloader mode";
const char *help_dap = "dap <on|off> - stop current measurements when DAP is active";
int cmdlen;
if (memcmp((char *)usb_command, "help", strlen("help")) == 0) {
vcdc_println(help_period);
vcdc_println(help_reset);
vcdc_println(help_boot);
vcdc_println(help_iface);
vcdc_println(help_power);
vcdc_println(help_display);
vcdc_println(help_maxreset);
vcdc_println(help_calibrate);
vcdc_println(help_show);
vcdc_println(help_hide);
vcdc_println(help_baudrate);
vcdc_println(help_dap);
}
else
if (memcmp((char *)usb_command, "maxreset", strlen("maxreset")) == 0) {
current_max_ua = 0;
}
else
if (memcmp((char *)usb_command, "period ", cmdlen = strlen("period ")) == 0) {
int period = strtol((char *)&usb_command[cmdlen], NULL, 10);
if ((period >= 10) && (period <= 1000)) {
vcdc_print("[INF] Period is ");
char str[10];
snprintf(str, 10, "%d", period);
vcdc_print(str);
vcdc_println(" ms");
emb_settings.period = period;
save_settings();
}
else {
vcdc_println(help_period);
vcdc_send_buffer_space();
}
}
else
if (memcmp((char *)usb_command, "baudrate ", cmdlen = strlen("baudrate ")) == 0) {
int baudrate = strtol((char *)&usb_command[cmdlen], NULL, 10);
if (baudrate != 0) {
vcdc_print("[INF] Baudrate is ");
char str[10];
snprintf(str, 10, "%d", baudrate);
vcdc_print(str);
vcdc_println(" bps");
console_reconfigure(baudrate, 8, USART_STOPBITS_1, USART_PARITY_NONE);
emb_settings.baudrate = baudrate;
save_settings();
}
else {
vcdc_println(help_baudrate);
vcdc_send_buffer_space();
}
}
else
if (memcmp((char *)usb_command, "iface ", cmdlen = strlen("iface ")) == 0) {
if (memcmp((char *)&usb_command[cmdlen], "on", 2) == 0) {
is_interface_connected = false;
button2_counter = 100;
}
else
if (memcmp((char *)&usb_command[cmdlen], "off", 3) == 0) {
is_interface_connected = true;
button2_counter = 100;
}
else {
vcdc_println(help_iface);
vcdc_send_buffer_space();
}
}
if (memcmp((char *)usb_command, "dap ", cmdlen = strlen("dap ")) == 0) {
if (memcmp((char *)&usb_command[cmdlen], "on", 2) == 0) {
emb_settings.dap_active = 1;
save_settings();
}
else
if (memcmp((char *)&usb_command[cmdlen], "off", 3) == 0) {
emb_settings.dap_active = 0;
save_settings();
}
else {
vcdc_println(help_iface);
vcdc_send_buffer_space();
}
}
else
if (memcmp((char *)usb_command, "power ", cmdlen = strlen("power ")) == 0) {
if (memcmp((char *)&usb_command[cmdlen], "on", 2) == 0) {
target_power_state = false;
button1_counter = 100;
}
else
if (memcmp((char *)&usb_command[cmdlen], "off", 3) == 0) {
target_power_state = true;
button1_counter = 100;
}
else
{
vcdc_println(help_power);
vcdc_send_buffer_space();
}
}
else
if (memcmp((char *)usb_command, "display ", cmdlen = strlen("display ")) == 0) {
display_mode = strtol((char *)&usb_command[cmdlen], NULL, 10);
}
else
if (memcmp((char *)usb_command, "reset ", cmdlen = strlen("reset ")) == 0) {
#if nRESET_GPIO_INVERT
gpio_set(nRESET_GPIO_PORT, nRESET_GPIO_PIN);
#else
gpio_clear(nRESET_GPIO_PORT, nRESET_GPIO_PIN);
#endif
target_release_reset = 100; /* 100 ms */
}
else
if (memcmp((char *)usb_command, "boot ", cmdlen = strlen("boot ")) == 0) {
#if nRESET_GPIO_INVERT
gpio_set(nRESET_GPIO_PORT, nRESET_GPIO_PIN);
#else
gpio_clear(nRESET_GPIO_PORT, nRESET_GPIO_PIN);
#endif
gpio_clear(TARGET_BOOT_PORT, TARGET_BOOT_PIN); /* inverted */
target_release_boot = 750; /* 750 ms */
target_release_reset = 100; /* 100 ms */
}
else
if (memcmp((char *)usb_command, "calibrate ", cmdlen = strlen("calibrate ")) == 0) {
cal_voltage = strtol((char *)&usb_command[cmdlen], NULL, 10);
if ((cal_voltage > 0) && (cal_voltage < 20000)) {
/* Enable DC/DC power */
gpio_set(POWER_OUTPUT_EN_PORT, POWER_OUTPUT_EN_PIN);
/* delay before calibration 1500 ms */
do_calibrate = 1500;
char str[20] = { };
snprintf(str, 20, "CAL %lu", cal_voltage);
tic33m_display_string(&tic33m_dev, str, strlen(str));
current_report_counter = 1;
} else {
vcdc_println(help_calibrate);
}
}
else
if (memcmp((char *)usb_command, "show ", cmdlen = strlen("show ")) == 0) {
if (memcmp((char *)&usb_command[cmdlen], "all", 3) == 0) {
emb_settings.show = 0xFF;
save_settings();
}
else
if (memcmp((char *)&usb_command[cmdlen], "SEC", 3) == 0) {
if (!(emb_settings.show & SHOW_SECONDS)) {
emb_settings.show |= SHOW_SECONDS;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "VOL", 3) == 0) {
if (!(emb_settings.show & SHOW_VOLTAGE)) {
emb_settings.show |= SHOW_VOLTAGE;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "CUR", 3) == 0) {
if (!(emb_settings.show & SHOW_CURRENT)) {
emb_settings.show |= SHOW_CURRENT;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "AHR", 3) == 0) {
if (!(emb_settings.show & SHOW_AMPEREHOURS)) {
emb_settings.show |= SHOW_AMPEREHOURS;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "WHR", 3) == 0) {
if (!(emb_settings.show & SHOW_WATTHOURS)) {
emb_settings.show |= SHOW_WATTHOURS;
save_settings();
}
}
else
{
vcdc_println(help_show);
vcdc_send_buffer_space();
}
}
else
if (memcmp((char *)usb_command, "hide ", cmdlen = strlen("hide ")) == 0) {
if (memcmp((char *)&usb_command[cmdlen], "all", 3) == 0) {
emb_settings.show = 0;
save_settings();
}
else
if (memcmp((char *)&usb_command[cmdlen], "SEC", 3) == 0) {
if (emb_settings.show & SHOW_SECONDS) {
emb_settings.show &= ~SHOW_SECONDS;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "VOL", 3) == 0) {
if (emb_settings.show & SHOW_VOLTAGE) {
emb_settings.show &= ~SHOW_VOLTAGE;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "CUR", 3) == 0) {
if (emb_settings.show & SHOW_CURRENT) {
emb_settings.show &= ~SHOW_CURRENT;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "AHR", 3) == 0) {
if (emb_settings.show & SHOW_AMPEREHOURS) {
emb_settings.show &= ~SHOW_AMPEREHOURS;
save_settings();
}
}
else
if (memcmp((char *)&usb_command[cmdlen], "WHR", 3) == 0) {
if (emb_settings.show & SHOW_WATTHOURS) {
emb_settings.show &= ~SHOW_WATTHOURS;
save_settings();
}
}
else
{
vcdc_println(help_hide);
vcdc_send_buffer_space();
}
}
}
/* ticks every 1 ms */
void systick_activity(void)
{
current_report_counter++;
/* every 10 ms */
if (current_report_counter && (current_report_counter % 10 == 0)) {
tic33m_lclk(&tic33m_dev);
}
/* every 100 ms */
if (current_report_counter && (current_report_counter % 100 == 0)) {
/* LED functions on v1 and v2 boards are different */
if (board_v2 && !target_boot_state) {
gpio_clear(LED_RANGE0_GPIO_PORT, LED_RANGE0_GPIO_PIN);
gpio_set(LED_RANGE1_GPIO_PORT, LED_RANGE1_GPIO_PIN);
gpio_set(LED_RANGE2_GPIO_PORT, LED_RANGE2_GPIO_PIN);
if (target_power_state) {
switch (current_power_range) {
case 2:
gpio_clear(LED_RANGE2_GPIO_PORT, LED_RANGE2_GPIO_PIN);
/* fall-through */
case 1:
gpio_clear(LED_RANGE1_GPIO_PORT, LED_RANGE1_GPIO_PIN);
/* fall-through */
case 0:
break;
default:
break;
}
}
}
/* console command parser */
static uint8_t usb_command[USB_COMMAND_SIZE];
if (vcdc_recv_buffered(usb_command, USB_COMMAND_SIZE) != 0) {
console_command_parser(usb_command);
}
}
if (do_calibrate) {
do_calibrate--;
if (!do_calibrate) {
/* calibration at 3.0V */
cmd_int |= CMD_INT_CALIBRATE;
}
}
/* only once 500 ms after start */
if ((!banner_displayed) && (current_report_counter == 500)) {
#if defined(BANNER_STR1)
vcdc_println(BANNER_STR1);
#endif
#if defined(BANNER_STR2)
vcdc_println(BANNER_STR2);
#endif
#if defined(BANNER_STR3)
vcdc_println(BANNER_STR3);
#endif
banner_displayed = true;
if (emb_settings.magic != FLASH_CONFIG_MAGIC) {
vcdc_println("[ERR] Configuration NOT loaded");
} else {
vcdc_println("[INF] Configuration loaded");
}
char str[30];
snprintf(str, 30, "[INF] Period is %lu ms", emb_settings.period);
vcdc_println(str);
}
/* every 'emb_settings.period' ms */
if (current_report_counter &&
(current_report_counter % emb_settings.period == 0) &&
!dap_connected) {
/* calculate average ADC value */
if (target_power_state && !target_start_measurements) {
/* disable timer and DMA channel */
timer_disable_counter(TIM2);
adc_disable_dma(ADC1);
/* measure voltage */
adc_data.voltage = adc_measure_voltage();
/* convert to millivolts x10 */
for (int i = 0; i < 3; i++) {
if (adc_data.count[i]) {
adc_data.current[i] = (vdda * (DIV_ROUND_CLOSEST(10*adc_data.raw_current[i], adc_data.count[i]))) / 4095;
#if ENABLE_DEBUG
char str[30];
snprintf(str, 30, "RAW ADC: %d: ", i);
vcdc_print(str);
snprintf(str, 30, "%lu / ", adc_data.raw_current[i]);
vcdc_print(str);
snprintf(str, 30, "%lu = ", adc_data.count[i]);
vcdc_print(str);
snprintf(str, 30, "%lu", DIV_ROUND_CLOSEST(adc_data.raw_current[i], adc_data.count[i]));
vcdc_println(str);
#endif
} else {
adc_data.current[i] = 0;
}
adc_data.raw_current[i] = 0;
adc_data.count[i] = 0;
}
adc_measure_current();
cmd_int |= CMD_INT_CONSOLEOUT;
}
}
if (target_release_reset) {
if (--target_release_reset == 0) {
/* Release reset */
#if nRESET_GPIO_INVERT
gpio_clear(nRESET_GPIO_PORT, nRESET_GPIO_PIN);
#else
gpio_set(nRESET_GPIO_PORT, nRESET_GPIO_PIN);
#endif
vcdc_println("[INF] target reset");
}
}
if (target_start_measurements) {
if (--target_start_measurements == 0) {
for (int i = 0; i < 3; i++) {
adc_data.raw_current[i] = 0;
adc_data.count[i] = 0;
}
adc_setup_common();
adc_measure_vdda();
adc_measure_current();
}