-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1926 lines (1560 loc) · 58.7 KB
/
main.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
//status 27 july 2020
//gps issue appears resolved? but system crashes after only 1-2 pps... pps pulses are now shorter
//10ms as opposed to 100ms. This might be an issue.
//check activities within the pps interrupt next time.
//status 20th july 2020
//code is fully operational - sensors and cosmic rays are output, together with GPS data
//picked up without issue by the python daemons on the Pi
//things which still don't work 100% are:
//DMA transfers - switched back to conventional for now, DMA usart was causing crashes/hanging
//DMA interrupts are not handled - might be a cause of this, but handling/interrupts isn't required.
//Strig A and Strig B signals for calibration don't report in, consider changing the types to volatile
//code is rather dirty and needs a good clean up!
//I have a feeling that some events are missed - i.e. every so often data for 1 second doesn't make it in to the output
//but it's hard to test this without statistical analysis on a decent chunk of data
//rates reported are within expectations.
//Calibration values I've been using are DACs at 700-800 (700 > more events)
//HV at 180 (probably works from 200, but we have some margin here).
//after calibration is complete, restarting the detector in software causes a crash
//this doesn't happen when we quit/use the shell scripts
//shell scripts also need tidying: First flash = Erase and flash, then calibration, then any subsequent flashes (flash/upgrade) should be flashes without erase.
//consider modification to print calibration values during start up
//old statuses can be mostly ignored now.
//status 20th july - updated
//doesn't seem to work on units that weren't the sample - crashing.
//found and fixed a bug in the eeprom loading routine, regarding readback values for the key (was wrong variable)
//somehow missing the timer info from the calibration routine
//events/second value counts up (events or seconds?) when running calibration on units that aren't the golden unit.
//more testing required.
//status 12th july.
//eeprom isn't working!? need to fix it. can't read/write reliably.. strange behaviour.
//trying different libraries and changes to .ld file, no success as yet
//july 27th - still random issues with GPS. now unit is working, but crashes on first pps. ?
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
//status as of 12/07/20 18:09.
//things that work:
//eeprom read/write; there was an issue with the offset, now resolved.
//data input/output seems ok.
//timers sorted, event interrupt not tested (using open dev board)
//next to do: rebuild calibration routines.
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
#include "bmp280/bmp280.h"
#include "lsm9ds1/lsm9ds1_reg.h"
#include "math.h"
#include "stdlib.h"
#include "eeprom.h"
/* USER CODE END Includes */
//__attribute__((__section__(".user_data"))) const uint16_t userConfig[64];
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
#define RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" // RCM & GGA
#define ZDA "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0*29\r\n" // ZDA
#define GGAZDA "$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0*28\r\n" // GGA & ZDA
#define GGA "$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" // GGA
#define GPSBAUDF "$PMTK251,115200*1F\r\n" //set as a one time thing; then we need to reboot the uart.
#define GPSBAUDM "$PMTK251,19200*25\r\n" //set as a one time thing; then we need to reboot the uart.
#define GPSBAUDS "$PMTK251,9600*17\r\n" //set as a one time thing; then we need to reboot the uart.
#define GPSSETPPS "$PMTK285,1,100*3D\r\n" //set the PPS to work
#define GPSRESET "$PMTK104*37\r\n" //full reset of the GPS unit, all settings 0
#define GPSOFFSET "$PMTK255,1*2D\r\n" //make the nema come after the pps.
#define FMWVERS "$PMTK605*31\r\n" // PMTK_Q_RELEASE gets the firmware version
// Sets the update intervall
#define NORMAL "$PMTK220,1000*1F\r\n" // PMTK_SET_NMEA_UPDATE_1HZ
// disables updates for the antenna status (only Adafruit ultimate GPS?)
#define NOANTENNA "$PGCMD,33,0*6D\r\n" // PGCMD_NOAN -> not required for L76
#define dac1_default 750
#define dac2_default 750
#define hv1_default 180
#define hv2_default 180
#define little_g 9.80665
//some eeprom stuff - probably not used? check and delete
#define DATA_EEPROM_BASE_ADDR ((uint32_t)0x08060000) /* Data EEPROM base address */
#define DATA_EEPROM_END_ADDR ((uint32_t)0x08060080) /* Data EEPROM end address */
uint16_t eeprom_addr_offset = 0x0040;
//eeprom stuff that definitely is used.
uint16_t VirtAddVarTab[NB_OF_VAR] = {0x5000, 0x6000, 0x7000, 0x8000, 0x9000};
uint16_t VarDataTab[NB_OF_VAR] = {0, 0, 0, 0, 0};
uint16_t VarValue,VarDataTmp = 0;
uint32_t eeprom_value=0; //variable for reading/writing from/to eeprom
//we define 32 addresses, each one is 32 bits long
//define for the lsm9ds1 - sensor bus for i2c, specific.
#define SENSOR_BUS hi2c1
ADC_HandleTypeDef hadc1;
I2C_HandleTypeDef hi2c1;
SPI_HandleTypeDef hspi2;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart1_tx;
DMA_HandleTypeDef hdma_usart2_rx;
/* USER CODE BEGIN PV */
//define bmp280
BMP280_HandleTypedef bmp280;
//print out variables for IMU
float accelx=0;
float accely=0;
float accelz=0;
float magx=0;
float magy=0;
float magz=0;
float pressure, temperature, humidity;
uint16_t size;
uint8_t Data[256];
//calibration variables - used when checking performance during cal mode.
uint16_t cal_events=0; //number of events per second in calibration mode
uint16_t cal_cumulative=0; //cumulative count of events in calibration mode
uint16_t a_events=0; //events on channel a in calibration mode
uint16_t b_events=0; //events on channel b in calibration mode
float rolling_average=0; //moving average of events
uint16_t cal_timer=0; //timer for calibration mode.
//text output; one buffer used at present
//will switch to using both via DMA when DMA is working.
uint8_t TextOutBuf[1024];
uint8_t Bbuffer[1024];
volatile uint8_t toggle = 0; //buffer toggle, not yet used.
//dma handler for usart1
DMA_HandleTypeDef hdma_usart1_tx;
uint32_t oldtimestamp =0;
int16_t convtemp=0;
float float_temp=0;
//timer values for the first interrupt (ch1) - also not sure if used.
uint32_t IC_Value1 = 0;
uint32_t IC_Value2 = 0;
uint32_t Difference = 0;
uint32_t Frequency = 0;
uint8_t Is_First_Captured = 0; // 0- not captured, 1- captured
uint32_t Evt_stack =0; //number of cosmic ray events this second
uint32_t Evt_timestamps[30]; //space for 30 events per second, no overflow as yet!
uint32_t Evt_total = 0; //total events since start of operation
uint32_t gps_timestamp =0; //value of TIM2 when GPS PPS arrives.
uint8_t data_ready=0; //flag for data ready to send to UART1 via DMA.
uint8_t cal_mode=0; //flag for calibration mode
uint8_t imu_failed=0; //flag for failure of imu/mmu chip. One board had a failed IMU in testing
uint8_t pps_started=0;
//eeprom usage map
/*
* 0 = is eeprom used? if /=0 then ignore. key
* 1 = DAC channel 1
* 2 = DAC channel 2
* 3 = HV channel 1
* 4 = HV channel 2
*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_I2C1_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI2_Init(void);
/* USER CODE BEGIN PFP */
uint16_t eeprom_read(uint16_t eeprom_address);
static void EXTILine11_Config(void); //the interrupt thingy.
static void EXTILine12_Config(void); //the interrupt thingy.
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void debugPrint(UART_HandleTypeDef *huart, char _out[])
{
HAL_UART_Transmit(huart, (uint8_t *) _out, strlen(_out), 10);
}
void debugPrintln(UART_HandleTypeDef *huart, char _out[])
{
HAL_UART_Transmit(huart, (uint8_t *) _out, strlen(_out), 10);
char newline[2] = "\r\n";
HAL_UART_Transmit(huart, (uint8_t *) newline, 2, 10);
}
//types for accelerometer
typedef union{
int16_t i16bit[3];
uint8_t u8bit[6];
} axis3bit16_t;
typedef struct {
void* hbus;
uint8_t i2c_address;
uint8_t cs_port;
uint8_t cs_pin;
} sensbus_t;
//manually set the I2C addresses here
static sensbus_t imu_bus = {&SENSOR_BUS,
0xD5,
0,
0};
static sensbus_t mag_bus = {&SENSOR_BUS,
0x39,
0,
0};
static axis3bit16_t data_raw_acceleration;
static axis3bit16_t data_raw_angular_rate;
static axis3bit16_t data_raw_magnetic_field;
static float acceleration_mg[3];
//static float angular_rate_mdps[3];
static float magnetic_field_mgauss[3];
static lsm9ds1_id_t whoamI;
static lsm9ds1_status_t reg;
static uint8_t rst;
static uint8_t tx_buffer[1000];
//instances for the accel and mag
//init the device here;
stmdev_ctx_t dev_ctx_imu;
stmdev_ctx_t dev_ctx_mag;
uint8_t imu_temp[2];
//usart part printf- doesn't seem to work anymore.
//did work when it was first added?
/* We need to implement own __FILE struct */
/* FILE struct is used from __FILE */
struct __FILE {
int dummy;
};
//declare a file
FILE __stdout;
int fputc(int ch, FILE *f){
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
int ferror(FILE *f){
/* Your implementation of ferror(). */
return 0;
}
void GPS_repeater(void)
{
//modify the gps routine so that it runs continuously to allow config changes in realtime.
//while(1){
uint8_t buffer[1];
uint8_t byte;
//memset(&buffer[0], 0, sizeof(buffer));
HAL_UART_Receive(&huart2, &buffer, 1, 400); //delay is arbitrary here - less than 1s otherwise interrupt
HAL_UART_Transmit(&huart1, &buffer, 1, HAL_MAX_DELAY); //delay here has no function, it comes out as soon as it goes in.
//modified for bidirectional 27/07/20
//use only when debugging
//memset(&TextOutBuf[0], 0, sizeof(TextOutBuf));
//memset(&buffer[0], 0, sizeof(buffer));
//HAL_UART_Receive(&huart1, &byte, 1, 100);
// HAL_UART_Transmit(&huart2, &byte, 1, 10);
//}
}
void set_HV(uint8_t chan, uint8_t voltage)
{
debugPrint(&huart1, "Setting HV"); // print
debugPrint(&huart1, "\r\n"); // print
//printf("Channel: %d", chan); // print
//HAL_UART_Transmit(&huart1, chan, sizeof(chan), HAL_MAX_DELAY);
//printf(
//debugPrint(&huart1, chan); // print
//printf(" Voltage: %d", voltage); // print
//HAL_UART_Transmit(&huart1, voltage, sizeof(voltage), HAL_MAX_DELAY);
//debugPrint(&huart1, voltage); // print
//debugPrint(&huart1, "\r\n"); // print
if (chan == 1)
{
HAL_GPIO_WritePin(hvpsu_cs1_GPIO_Port, hvpsu_cs1_Pin, GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(hvpsu_cs2_GPIO_Port, hvpsu_cs2_Pin, GPIO_PIN_RESET);
}
//HAL_Delay(100);
HAL_SPI_Transmit(&hspi2,&voltage,1,100);
while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);
HAL_GPIO_WritePin(hvpsu_cs1_GPIO_Port, hvpsu_cs1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(hvpsu_cs2_GPIO_Port, hvpsu_cs2_Pin, GPIO_PIN_SET);
HAL_Delay(10);
debugPrint(&huart1, "Setting HV completed.\r\n"); // print
}
void set_DAC(uint8_t chan, uint16_t thresh)
{
debugPrint(&huart1, "Setting DAC"); // print
debugPrint(&huart1, "\r\n"); // print
//printf("Channel: %d", chan); // print
//HAL_UART_Transmit(&huart1, chan, sizeof(chan), HAL_MAX_DELAY);
//printf(
//debugPrint(&huart1, chan); // print
//printf(" Threshold: %d", thresh); // print
//HAL_UART_Transmit(&huart1, voltage, sizeof(voltage), HAL_MAX_DELAY);
//debugPrint(&huart1, voltage); // print
debugPrint(&huart1, "\r\n"); // print
unsigned char buffer[3];
buffer[1]=(thresh >> 8);
buffer[2]=(thresh & 0xFF);
if (chan == 1)
{
buffer[0]=0x00;
HAL_I2C_Master_Transmit(&hi2c1,0x60<<1,buffer,3,100);
//HAL_I2C_Mem_Write(&hi2c1, DAC_addr, DAC_ch1, 2, (uint8_t*)(thresh), 2, HAL_MAX_DELAY);
//HAL_I2C_Master_Transmit(&hi2c1, DAC_addr, (uint8_t)(DAC_ch1), 1, HAL_MAX_DELAY);
}
else
{
buffer[0]=0x08;
HAL_I2C_Master_Transmit(&hi2c1,0x60<<1,buffer,3,100);
}
debugPrint(&huart1, "Setting DAC completed.\r\n"); // print
}
//global variables for eeprom ops
uint16_t key = 0;
uint16_t DAC_channel1=800;
uint16_t DAC_channel2=800;
uint16_t HV_channel1=190;
uint16_t HV_channel2=190;
void gps_init()
{
//init GPS to send the right strings only.
//we do this a bunch of times to be sure it works.
printf("Start GPS init.\r\n");
HAL_Delay(5000);
HAL_UART_Transmit(&huart2, GPSRESET, sizeof(GPSRESET), HAL_MAX_DELAY);
HAL_Delay(5000);
HAL_UART_Transmit(&huart2, GPSSETPPS, sizeof(GPSSETPPS), HAL_MAX_DELAY);
//HAL_Delay(100);
HAL_Delay(100);
HAL_UART_Transmit(&huart2, GGAZDA, sizeof(GGAZDA), HAL_MAX_DELAY);
//HAL_Delay(100);
HAL_Delay(100);
//HAL_Delay(1500); //1.5s delay for boot
HAL_UART_Transmit(&huart2, GPSSETPPS, sizeof(GPSSETPPS), HAL_MAX_DELAY);
//HAL_Delay(100);
HAL_Delay(100);
HAL_UART_Transmit(&huart2, GGAZDA, sizeof(GGAZDA), HAL_MAX_DELAY);
HAL_Delay(100);
//HAL_Delay(100);
HAL_UART_Transmit(&huart2, GGAZDA, sizeof(GGAZDA), HAL_MAX_DELAY);
HAL_Delay(100);
HAL_UART_Transmit(&huart2, GPSSETPPS, sizeof(GPSSETPPS), HAL_MAX_DELAY);
HAL_Delay(100); //1.5s delay for boot
HAL_UART_Transmit(&huart2, GPSSETPPS, sizeof(GPSSETPPS), HAL_MAX_DELAY);
HAL_Delay(100);
HAL_UART_Transmit(&huart2, GPSOFFSET, sizeof(GPSSETPPS), HAL_MAX_DELAY);
debugPrint(&huart1, "Completed GPS init.\r\n");
}
void bme_readout()
{
float altitude=0;
//HAL_Delay(100);
while (!bmp280_read_float(&bmp280, &temperature, &pressure, &humidity)) {
size = sprintf((char *)TextOutBuf+strlen(TextOutBuf),
"Temperature/pressure reading failed\n");
//HAL_UART_Transmit(&huart1, Data, size, 1000);
//HAL_Delay(2000);
}
//convert pascals to mbar
pressure = pressure/100;
altitude = 44330.0f*( 1.0f - pow((pressure/1013.25f), (1.0f/5.255f)))+18; // Calculate altitude in meters
size = sprintf((char *)TextOutBuf+strlen(TextOutBuf),"Altitude: %3.6f;\r\nTemperatureCBaro: %3.6f;\r\nPressure: %4.6f;\r\nHumidity: %2.6f;\r\n", altitude, temperature, pressure, humidity);
//HAL_UART_Transmit(&huart1, Data, size, 1000);
//size = sprintf((char *)Data,"Pressure: %.2f Pa, Temperature: %.2f C",
// pressure, temperature);
//HAL_UART_Transmit(&huart1, Data, size, 1000);
//if (bme280p) {
// size = sprintf((char *)Data,", Humidity: %.2f\n", humidity);
// HAL_UART_Transmit(&huart1, Data, size, 1000);
// }
//else {
// size = sprintf((char *)Data, "\n");
// HAL_UART_Transmit(&huart1, Data, size, 1000);
// }
}
static void platform_init(void)
{
}
static void tx_com(uint8_t *tx_buffer, uint16_t len)
{
HAL_UART_Transmit(&huart1, tx_buffer, len, 1000);
}
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
sensbus_t *sensbus = (sensbus_t*)handle;
if (sensbus->hbus == &hi2c1) {
HAL_I2C_Mem_Read(sensbus->hbus, sensbus->i2c_address, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
}
return 0;
}
static int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
sensbus_t *sensbus = (sensbus_t*)handle;
if (sensbus->hbus == &hi2c1) {
HAL_I2C_Mem_Write(sensbus->hbus, sensbus->i2c_address, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
}
return 0;
}
void lsm9ds1_read_data_polling(void)
{
debugPrint(&huart1, "Reg writing IMU\r\n");
/* Initialize inertial sensors (IMU) driver interface */
dev_ctx_imu.write_reg = platform_write;
dev_ctx_imu.read_reg = platform_read;
dev_ctx_imu.handle = (void*)&imu_bus;
debugPrint(&huart1, "Reg writing mmu\r\n");
/* Initialize magnetic sensors driver interface */
dev_ctx_mag.write_reg = platform_write;
dev_ctx_mag.read_reg = platform_read;
dev_ctx_mag.handle = (void*)&mag_bus;
debugPrint(&huart1, "Platform init...\r\n");
/* Initialize platform specific hardware */
platform_init();
debugPrint(&huart1, "Boot delay...\r\n");
/* Wait sensor boot time */
HAL_Delay(100);
/* Check device ID */
debugPrint(&huart1, "ID check...\r\n");
lsm9ds1_dev_id_get(&dev_ctx_mag, &dev_ctx_imu, &whoamI);
debugPrint(&huart1, "ID return...\r\n");
size = sprintf((char *)Data, "Whoamivals imu: %d, mag: %d \r\n", whoamI.imu, whoamI.mag);
HAL_UART_Transmit(&huart1, Data, size, 1000);
if (whoamI.imu != LSM9DS1_IMU_ID || whoamI.mag != LSM9DS1_MAG_ID){
//while(1){
/* manage here device not found */
debugPrint(&huart1, "IMU error. IMU/MAG offline\r\n");
imu_failed = 1;
// printf("address error");
//}
}
/* Restore default configuration */
debugPrint(&huart1, "Config restore...\r\n");
lsm9ds1_dev_reset_set(&dev_ctx_mag, &dev_ctx_imu, PROPERTY_ENABLE);
do {
lsm9ds1_dev_reset_get(&dev_ctx_mag, &dev_ctx_imu, &rst);
} while (rst);
/* Enable Block Data Update */
lsm9ds1_block_data_update_set(&dev_ctx_mag, &dev_ctx_imu, PROPERTY_ENABLE);
debugPrint(&huart1, "Set scale...\r\n");
/* Set full scale */
lsm9ds1_xl_full_scale_set(&dev_ctx_imu, LSM9DS1_2g);
lsm9ds1_gy_full_scale_set(&dev_ctx_imu, LSM9DS1_2000dps);
lsm9ds1_mag_full_scale_set(&dev_ctx_mag, LSM9DS1_16Ga);
debugPrint(&huart1, "Set bandwidth...\r\n");
/* Configure filtering chain - See datasheet for filtering chain details */
/* Accelerometer filtering chain */
lsm9ds1_xl_filter_aalias_bandwidth_set(&dev_ctx_imu, LSM9DS1_AUTO);
lsm9ds1_xl_filter_lp_bandwidth_set(&dev_ctx_imu, LSM9DS1_LP_ODR_DIV_50);
lsm9ds1_xl_filter_out_path_set(&dev_ctx_imu, LSM9DS1_LP_OUT);
/* Gyroscope filtering chain */
lsm9ds1_gy_filter_lp_bandwidth_set(&dev_ctx_imu, LSM9DS1_LP_ULTRA_LIGHT);
lsm9ds1_gy_filter_hp_bandwidth_set(&dev_ctx_imu, LSM9DS1_HP_MEDIUM);
lsm9ds1_gy_filter_out_path_set(&dev_ctx_imu, LSM9DS1_LPF1_HPF_LPF2_OUT);
debugPrint(&huart1, "Set outputmode...\r\n");
/* Set Output Data Rate / Power mode */
lsm9ds1_imu_data_rate_set(&dev_ctx_imu, LSM9DS1_IMU_59Hz5);
lsm9ds1_mag_data_rate_set(&dev_ctx_mag, LSM9DS1_MAG_UHP_10Hz);
/* Read samples in polling mode (no int) */
//while(1)
//{
/* Read device status register */
debugPrint(&huart1, "Completed.\r\n");
}
void read_imu()
{
if (imu_failed == 0) {
lsm9ds1_dev_status_get(&dev_ctx_mag, &dev_ctx_imu, ®);
if ( reg.status_imu.xlda && reg.status_imu.gda )
{
/* Read imu data */
memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
memset(data_raw_angular_rate.u8bit, 0x00, 3 * sizeof(int16_t));
lsm9ds1_acceleration_raw_get(&dev_ctx_imu, data_raw_acceleration.u8bit);
lsm9ds1_angular_rate_raw_get(&dev_ctx_imu, data_raw_angular_rate.u8bit);
acceleration_mg[0] = lsm9ds1_from_fs4g_to_mg(data_raw_acceleration.i16bit[0]);
acceleration_mg[1] = lsm9ds1_from_fs4g_to_mg(data_raw_acceleration.i16bit[1]);
acceleration_mg[2] = lsm9ds1_from_fs4g_to_mg(data_raw_acceleration.i16bit[2]);
//angular_rate_mdps[0] = lsm9ds1_from_fs2000dps_to_mdps(data_raw_angular_rate.i16bit[0]);
//angular_rate_mdps[1] = lsm9ds1_from_fs2000dps_to_mdps(data_raw_angular_rate.i16bit[1]);
//angular_rate_mdps[2] = lsm9ds1_from_fs2000dps_to_mdps(data_raw_angular_rate.i16bit[2]);
accelx = ((acceleration_mg[0] /1000)*little_g/2);
accely = ((acceleration_mg[1] /1000)*little_g/2);
accelz = ((acceleration_mg[2] /1000)*little_g/2);
sprintf((char*)TextOutBuf+strlen(TextOutBuf), "AccelX: %2.6f;\r\nAccelY: %2.6f;\r\nAccelZ: %2.6f;\r\n",
accelx, accely, accelz);
//angular_rate_mdps[0], angular_rate_mdps[1], angular_rate_mdps[2]);
//tx_com(tx_buffer, strlen((char const*)tx_buffer));
//sprintf(IMUtext, "MagX: %2.6f;\r\nMagY: %2.6f;\r\nMagZ: %2.6f;\r\n", magx, magy, magz);
//WriteStringToOutputBuff(IMUtext);
//sprintf(IMUtext, "AccelX: %2.6f;\r\nAccelY: %2.6f;\r\nAccelZ: %2.6f;\r\n",gravx, gravy, gravz);
//WriteStringToOutputBuff(IMUtext);
}
if ( reg.status_mag.zyxda )
{
/* Read magnetometer data */
memset(data_raw_magnetic_field.u8bit, 0x00, 3 * sizeof(int16_t));
lsm9ds1_magnetic_raw_get(&dev_ctx_mag, data_raw_magnetic_field.u8bit);
magnetic_field_mgauss[0] = lsm9ds1_from_fs16gauss_to_mG(data_raw_magnetic_field.i16bit[0]);
magnetic_field_mgauss[1] = lsm9ds1_from_fs16gauss_to_mG(data_raw_magnetic_field.i16bit[1]);
magnetic_field_mgauss[2] = lsm9ds1_from_fs16gauss_to_mG(data_raw_magnetic_field.i16bit[2]);
sprintf((char*)TextOutBuf+strlen(TextOutBuf), "MagX: %2.6f;\r\nMagY: %2.6f;\r\nMagZ: %2.6f;\r\n",
(magnetic_field_mgauss[0]/1000), (magnetic_field_mgauss[1]/1000), (magnetic_field_mgauss[2]/1000));
//tx_com(tx_buffer, strlen((char const*)tx_buffer));
//there is a problem with this temperature reading. 160720 - starts reading out 4000? was working before.
//now we print out the temp
lsm9ds1_temperature_raw_get(&dev_ctx_imu, imu_temp);
convtemp = ((imu_temp[1] << 8) + imu_temp[0]);
float_temp = (convtemp / 16) + 27.5f;
//sprintf((char*)TextOutBuf+strlen(TextOutBuf), "acceltemp:%3.6f;\r\n",convtemp);
sprintf((char*)TextOutBuf+strlen(TextOutBuf), "TemperatureCHumid:%3.6f;\r\n",float_temp);
//tx_com(tx_buffer, strlen((char const*)tx_buffer));
//temp is working again now? not sure why it broke...
}
//}
}
}
void avg_temp_print(void)
{
float avg_temp = (temperature+float_temp)/2;
sprintf((char*)TextOutBuf+strlen(TextOutBuf), "TemperatureC: %3.6f;\r\n",avg_temp);
//tx_com(tx_buffer, strlen((char const*)tx_buffer));
}
void print_buffer(void)
{
//TextOutBufSize = strlen(TextOutBuf); //check characters in buffer
if (strlen(TextOutBuf) >0 )
{
//print one if there is a character to print
//problem here, it only transmits the first letter, over and over
//see if there's a DMA route?
//DMA instruction to buffer -> UART would be ideal.
HAL_UART_Transmit(&huart1, TextOutBuf, 1, 1000);
//HAL_UART_Transmit(&huart1, TextOutBuf, strlen(TextOutBuf), 1000);
}
//if (strlen(TextOutBuf)==0 )
//{
//memset(&TextOutBuf[0], 0, sizeof(TextOutBuf));
//}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
HAL_NVIC_DisableIRQ(TIM2_IRQn); //disable interrupts on call, regardless of type. renable at the end.
//memset(TextOutBuf,0,strlen(TextOutBuf));
//debugPrint(&huart1, "TIM2\r\n");
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) // if interrput source is channel 1
{
pps_started = 1;
//here we are in the PPS case. Clear the buffer
memset(TextOutBuf,0,strlen(TextOutBuf));
//debugPrint(&huart1, "GPSPPS\r\n");
HAL_GPIO_TogglePin(pwr_led_GPIO_Port,pwr_led_Pin);
sprintf((char*)TextOutBuf+strlen(TextOutBuf), "PPS: GPS lock:1;\r\n");
if (cal_mode==1)
{
//in calibration mode, put the number of events in a print buffer and print it directly.
cal_cumulative = cal_cumulative + cal_events; //add in the events this second to the cumulative total
if (cal_timer <120)
{
cal_timer++; //increment the calibration timer (starts at 0 - number of seconds that cal has been running; assume instant start up! could be dangerous so we'll reset every 2 minutes to be sure)
}
else
{
//reset condition every 2 minutes, and we don't set it to 0 otherwise we'd get a div0
cal_timer = 1;
cal_cumulative = cal_events;
}
rolling_average = (float)cal_cumulative/(float)cal_timer;
memset(&TextOutBuf[0], 0, sizeof(TextOutBuf));
sprintf((char*)TextOutBuf, "Cal - Events: %d, Ch_A: %d, Ch_B: %d, Timer: %d, Rolling average: %f\r\n", cal_events, a_events, b_events, cal_timer, rolling_average);
//bme_readout();
//read_imu();
//avg_temp_print();
//now reset the counters
cal_events = 0;
a_events = 0;
b_events = 0;
//HAL_UART_Transmit_DMA(&huart1, TextOutBuf, 1024); //this is the best way to send the data,
//but probably can't be dynamically sized. must find a way to send it all.
data_ready=1;
}
else
{
//normal operation mode
//debugPrint(&huart1, "PPS\r\n");
//oldtimestamp = gps_timestamp; //backup the old value
gps_timestamp = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_1); // capture the first value
//HAL_GPIO_WritePin(evt_led_GPIO_Port, evt_led_Pin, GPIO_PIN_SET);
//here goes the code to do the readouts; write second
//readout events
//readout secondary data
//now reset the counter to 0;
//print sensors was here, moving to main loop
bme_readout();
read_imu();
avg_temp_print();
if (Evt_stack > 0) {
for (uint8_t prt_ctr=0; prt_ctr<(Evt_stack+1); prt_ctr++)
{
sprintf((char*)TextOutBuf+strlen(TextOutBuf), "Event: sub second micros:%d/%d; Event Count:%d\r\n", Evt_timestamps[prt_ctr], gps_timestamp, (Evt_total+prt_ctr)+1);
//sprintf((char*)TextOutBuf, "GPS_PPS\r\n");
}
}
Evt_total = Evt_total+Evt_stack; //increment total events
//HAL_GPIO_TogglePin(pwr_led_GPIO_Port,pwr_led_Pin);
//sprintf((char*)TextOutBuf, "GPS_PPS\r\n");
//HAL_UART_Transmit(&huart1, TextOutBuf, sizeof(TextOutBuf), 1000);
data_ready=1;
//reset ctr
TIM2->CNT = 0; //reset the ctr
TIM2->CR1 |= 0x01;
//after we print, set the event stack back to 0;
Evt_stack=0;
//TIM2->CCMR1 |= TIM_CCMR1_CC1S_0;
//TIM2->CCER |= TIM_CCER_CC1E;
//TIM2->CR1 |= TIM_CR1_CEN;
//TIM2->SR = ~TIM_SR_CC1IF;
}
}
else
//here we are in the event case. Which is all other times we execute this routine if channel 1 wasn't used.
//if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) // if interrput source is channel 2, cosmic event
{
HAL_GPIO_WritePin(evt_led_GPIO_Port,evt_led_Pin, GPIO_PIN_SET);
//debugPrint(&huart1, "evt\r\n");
//when we have an event, we read the timer into the nth slot of the stack.
if (pps_started)
{
Evt_timestamps[Evt_stack] = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2); // capture the first value
Evt_stack++;
}
HAL_GPIO_WritePin(evt_led_GPIO_Port,evt_led_Pin, GPIO_PIN_SET);
//if (Evt_stack>30) sprintf((char*)TextOutBuf, "Event overflow");
//HAL_GPIO_WritePin(evt_led_GPIO_Port, evt_led_Pin, GPIO_PIN_SET); //set event pin, we'll reset it in the main loop after a v. short delay.
if (cal_mode==1)
{
cal_events++;
}
}
//data_ready=1;
HAL_NVIC_EnableIRQ(TIM2_IRQn); //disable interrupts on call, regardless of type. renable at the end.
//HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
void set_cal_interrupt()
//this doesn't work? Don't know why
//it's not essential but would facilitate calibration
{
debugPrint(&huart1, "Setting interrupts for calibration\r\n");
EXTILine12_Config();
EXTILine11_Config();
debugPrint(&huart1, "Strig_a & Strig_b interrupts set\r\n");
}
void release_cal_interrupt()
//ditto also not working, but only because the interrupts don't bind in the first place.
{
debugPrint(&huart1, "Releasing interrupts for calibration\r\n");
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
debugPrint(&huart1, "Interrupts released.\r\n");
}
void cal_routine()
{
//this code enables manual calibration over the serial port, requires the flag pin to be set high when booting the STM32.
//there are scripts in the pi image to do this automatically
char calstat[2];
char valstat[4];
char hv1stat[4];
char hv2stat[4];
char dac1stat[5];
char dac2stat[5];
uint8_t selector = 0;
uint16_t settingval = 0;
uint32_t eepromcomplete = 1;
//start a while loop here somehow
if (data_ready)
{
sprintf((char*)TextOutBuf, "Cal - CMF events: %d, Events/sec: %d, Ch_A: %d, Ch_B: %d, Timer: %d, average: %f\r\n", cal_cumulative, cal_events, a_events, b_events, cal_timer, rolling_average);
//bme_readout();
//read_imu();
//avg_temp_print();
HAL_UART_Transmit(&huart1, TextOutBuf, sizeof(TextOutBuf), 1000);
data_ready=0;
}
memset(&calstat[0], 0, sizeof(calstat));
memset(&valstat[0], 0, sizeof(valstat));
debugPrint(&huart1, "Calibration Mode for Cosmic Pi V.1.7\r\n");
debugPrint(&huart1, "Choose option:\r\n");
debugPrint(&huart1, "1: Set DAC channel 1\r\n");
debugPrint(&huart1, "2: Set DAC channel 2\r\n");
debugPrint(&huart1, "3: Set HV channel 1\r\n");
debugPrint(&huart1, "4: Set HV channel 2\r\n");
debugPrint(&huart1, "5: Set DAC channels 1 & 2 at the same time\r\n");
debugPrint(&huart1, "6: Set HV channels 1 & 2 at the same time\r\n");
debugPrint(&huart1, "7: enable calibration interrupts\r\n");
debugPrint(&huart1, "8: write to eeprom\r\n");
debugPrint(&huart1, "9: quit debug mode\r\n");
debugPrint(&huart1, "0: repeat menu\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)calstat, 1, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)calstat, 1, 30000);
debugPrint(&huart1, "\r\n");
selector = 0;
selector = atoi(calstat);
switch (selector)
{
case 0:
debugPrint(&huart1, "Choose option:\r\n");
debugPrint(&huart1, "1: Set DAC channel 1\r\n");
debugPrint(&huart1, "2: Set DAC channel 2\r\n");
debugPrint(&huart1, "3: Set HV channel 1\r\n");
debugPrint(&huart1, "4: Set HV channel 2\r\n");
debugPrint(&huart1, "5: Set DAC channels 1 & 2 at the same time\r\n");
debugPrint(&huart1, "6: Set HV channels 1 & 2 at the same time\r\n");
debugPrint(&huart1, "7: enable calibration interrupts\r\n");
debugPrint(&huart1, "8: write to eeprom\r\n");
debugPrint(&huart1, "9: quit debug mode\r\n");
//nb when we press 9 and try starting the detector, it almost always crashes.
//don't know why. a command line reset fixes it.
debugPrint(&huart1, "0: repeat menu\r\n");
break;
case 1:
memset(&dac1stat[0], 0, sizeof(dac1stat));
debugPrint(&huart1, "Set DAC channel 1, enter 4 digits from 0000 to 1024\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)dac1stat, 4, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)dac1stat, 4, 30000);
debugPrint(&huart1, "\r\n");
settingval=0;
DAC_channel1 = atoi(dac1stat);
size = sprintf((char *)Data, "DAC Channel 1: %d \r\n", DAC_channel1);
HAL_UART_Transmit(&huart1, Data, size, 1000);
set_DAC(1,DAC_channel1);
debugPrint(&huart1, "DAC channel 1 setting completed \r\n");
break;
case 2:
memset(&dac2stat[0], 0, sizeof(dac2stat));
debugPrint(&huart1, "Set DAC channel 2, enter 4 digits from 0000 to 1024\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)dac2stat, 4, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)dac2stat, 4, 30000);
debugPrint(&huart1, "\r\n");
DAC_channel2 = atoi(dac2stat);
size = sprintf((char *)Data, "DAC Channel 2: %d \r\n", DAC_channel2);
HAL_UART_Transmit(&huart1, Data, size, 1000);
set_DAC(2,DAC_channel2);
debugPrint(&huart1, "DAC channel 2 setting completed \r\n");
break;
case 3:
memset(&valstat[0], 0, sizeof(valstat));
debugPrint(&huart1, "Set HV channel 1, enter 3 digits from 000 to 255\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)hv1stat, 3, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)hv1stat, 3, 30000);
debugPrint(&huart1, "\r\n");
HV_channel1 = atoi(hv1stat);
set_HV(1,HV_channel1);
debugPrint(&huart1, "HV channel 1 setting completed \r\n");
break;
case 4:
memset(&valstat[0], 0, sizeof(valstat));
debugPrint(&huart1, "Set HV channel 2, enter 3 digits from 000 to 255\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)valstat, 3, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)valstat, 3, 30000);
debugPrint(&huart1, "\r\n");
settingval=0;
settingval = atoi(valstat);
HV_channel2 = settingval;
set_HV(2,settingval);
debugPrint(&huart1, "HV channel 2 setting completed \r\n");
break;
case 5:
memset(&dac2stat[0], 0, sizeof(dac2stat));
debugPrint(&huart1, "Set DAC channels 1 & 2, enter 4 digits from 0000 to 1024\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)dac2stat, 4, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)dac2stat, 4, 30000);
debugPrint(&huart1, "\r\n");
settingval=0;
settingval = atoi(dac2stat);
DAC_channel1 = settingval;
DAC_channel2 = settingval;
set_DAC(1,settingval);
set_DAC(2,settingval);
debugPrint(&huart1, "DAC channels 1 & 2 setting completed \r\n");
break;
case 6:
memset(&valstat[0], 0, sizeof(valstat));
debugPrint(&huart1, "Set HV channels 1 & 2, enter 3 digits from 000 to 255\r\n");
HAL_UART_Receive(&huart1, (uint8_t *)valstat, 3, 30000); //if it times out, then we need to repeat it.
debugPrint(&huart1, "\r\n");
debugPrint(&huart1, "Read input value: ");
HAL_UART_Transmit(&huart1, (uint8_t *)valstat, 3, 30000);
debugPrint(&huart1, "\r\n");