Skip to content

Commit 74e54b4

Browse files
committed
Update code, add current measurements
1 parent 3eaa55c commit 74e54b4

21 files changed

+196
-55
lines changed

code/SLSTK3400A_ADXL362/inc/accel.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/***************************************************************************//**
22
* @file accel.h
33
* @brief All code for the ADXL362 accelerometer.
4-
* @version 2.0
4+
* @version 2.1
55
* @author Brecht Van Eeckhoudt
66
******************************************************************************/
77

@@ -23,7 +23,7 @@
2323
#include "../inc/debugging.h" /* Enable or disable printing to UART */
2424

2525

26-
/* ADXL GPOI */
26+
/* ADXL SPI GPOI */
2727
#define ADXL_CLK_PORT gpioPortE
2828
#define ADXL_CLK_PIN 12
2929
#define ADXL_MOSI_PORT gpioPortE
@@ -32,8 +32,12 @@
3232
#define ADXL_MISO_PIN 11
3333
#define ADXL_NCS_PORT gpioPortD /* Can't use the US0_CS port (PE13) to manually set/clear CS line */
3434
#define ADXL_NCS_PIN 4
35+
36+
/* Other ADXL GPOI */
3537
#define ADXL_INT1_PORT gpioPortD
3638
#define ADXL_INT1_PIN 7
39+
#define ADXL_VCC_PORT gpioPortD
40+
#define ADXL_VCC_PIN 5
3741

3842

3943
/* ADXL REGISTERS */
@@ -58,8 +62,13 @@
5862

5963

6064
/* Prototypes */
65+
void initADXL_VCC (void);
66+
void powerADXL (bool enabled);
67+
6168
void initADXL_SPI (void);
6269

70+
void testADXL (void);
71+
6372
void readValuesADXL (void);
6473
void resetHandlerADXL (void);
6574

@@ -69,6 +78,7 @@ void readADXL_XYZDATA (void);
6978

7079
void measureADXL (bool enabled);
7180
void configADXL_range (uint8_t givenRange);
81+
void configADXL_ODR (uint8_t givenODR);
7282
void configADXL_activity (uint8_t gThreshold);
7383

7484
void softResetADXL (void);

code/SLSTK3400A_ADXL362/inc/handlers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
/* Global variables (project-wide accessible) */
24-
extern volatile bool triggered; /* TODO: before static - Accelerometer triggered interrupt */
24+
extern volatile bool triggered; /* Accelerometer triggered interrupt */
2525

2626

2727
#endif /* _HANDLERS_H_ */

code/SLSTK3400A_ADXL362/inc/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/* Prototypes */
2323
void initLEDS (void);
2424
void Error (uint8_t number);
25-
void Delay (uint32_t dlyTicks); /* TODO before: Static so the function is only "seen" in the file it's declared in. */
25+
void Delay (uint32_t dlyTicks);
2626
void disableSystick (void);
2727
void enableSystick (void);
2828

code/SLSTK3400A_ADXL362/src/accel.c

Lines changed: 162 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file accel.c
33
* @brief All code for the ADXL362 accelerometer.
44
* @details Started with code from the UART example (main_series0_HG.c) from SiLabs Github.
5-
* @version 2.0
5+
* @version 2.1
66
* @author Brecht Van Eeckhoudt
77
******************************************************************************/
88

@@ -15,6 +15,52 @@ volatile int8_t XYZDATA[3] = { 0x00, 0x00, 0x00 };
1515
uint8_t range = 0;
1616

1717

18+
/**************************************************************************//**
19+
* @brief
20+
* Initialize the GPIO pin to supply the accelerometer with power.
21+
*****************************************************************************/
22+
void initADXL_VCC (void)
23+
{
24+
GPIO_PinModeSet(ADXL_VCC_PORT, ADXL_VCC_PIN, gpioModePushPull, 1);
25+
GPIO_PinOutSet(ADXL_VCC_PORT, ADXL_VCC_PIN); /* Enable VCC pin */
26+
27+
#ifdef DEBUGGING /* DEBUGGING */
28+
dbinfo("Accelerometer powered");
29+
#endif /* DEBUGGING */
30+
31+
}
32+
33+
/**************************************************************************//**
34+
* @brief
35+
* Enable or disable the power to the accelerometer.
36+
*
37+
* @param[in] enabled
38+
* @li True - Enable the GPIO pin connected to the VCC pin of the accelerometer.
39+
* @li False - Disable the GPIO pin connected to the VCC pin of the accelerometer.
40+
*****************************************************************************/
41+
void powerADXL (bool enabled)
42+
{
43+
if (enabled)
44+
{
45+
GPIO_PinOutSet(ADXL_VCC_PORT, ADXL_VCC_PIN); /* Enable VCC pin */
46+
47+
#ifdef DEBUGGING /* DEBUGGING */
48+
dbinfo("Accelerometer powered");
49+
#endif /* DEBUGGING */
50+
51+
}
52+
else
53+
{
54+
GPIO_PinOutClear(ADXL_VCC_PORT, ADXL_VCC_PIN); /* Disable VCC pin */
55+
56+
#ifdef DEBUGGING /* DEBUGGING */
57+
dbwarn("Accelerometer powered down");
58+
#endif /* DEBUGGING */
59+
60+
}
61+
}
62+
63+
1864
/**************************************************************************//**
1965
* @brief
2066
* Initialize USART0 in SPI mode according to the settings required
@@ -62,6 +108,11 @@ void initADXL_SPI (void)
62108

63109
/* Set CS high (active low!) */
64110
GPIO_PinOutSet(gpioPortE, 13);
111+
112+
#ifdef DEBUGGING /* DEBUGGING */
113+
dbinfo("Accelerometer SPI initialized");
114+
#endif /* DEBUGGING */
115+
65116
}
66117

67118

@@ -147,11 +198,6 @@ void readValuesADXL (void)
147198
{
148199
uint32_t counter = 0;
149200

150-
/* Get (range) settings */
151-
uint8_t reg = readADXL(ADXL_REG_FILTER_CTL);
152-
reg = reg | 0b00010000; /* ODR (last 3 bits) 12,5Hz */
153-
writeADXL(ADXL_REG_FILTER_CTL, reg);
154-
155201
/* Enable measurement mode */
156202
measureADXL(true);
157203

@@ -206,48 +252,60 @@ void readValuesADXL (void)
206252
*****************************************************************************/
207253
void resetHandlerADXL (void)
208254
{
255+
uint8_t retries = 0;
256+
209257
/* Soft reset ADXL */
210258
softResetADXL();
211259

212-
/* Read DEVID_AD */
213-
if (checkID_ADXL())
214-
{
215-
216-
#ifdef DEBUGGING /* DEBUGGING */
217-
dbinfo("Soft reset ADXL - correct ID!");
218-
#endif /* DEBUGGING */
219-
220-
}
221-
else
260+
/* First try to get the correct ID failed */
261+
if (!checkID_ADXL())
222262
{
223-
224-
#ifdef DEBUGGING /* DEBUGGING */
225-
dbwarn("Soft reset ADXL - Incorrect ID!");
226-
#endif /* DEBUGGING */
263+
retries++;
227264

228265
Delay(1000);
229266

230267
/* Soft reset */
231268
softResetADXL();
232269

233-
if (checkID_ADXL())
270+
/* Second try to get the correct ID failed */
271+
if (!checkID_ADXL())
234272
{
273+
retries++;
235274

236-
#ifdef DEBUGGING /* DEBUGGING */
237-
dbinfo("Retry soft reset - correct ID!");
238-
#endif /* DEBUGGING */
275+
Delay(1000);
276+
277+
/* Soft reset */
278+
softResetADXL();
279+
280+
/* Third try to get the correct ID failed
281+
* resorting to "hard" reset! */
282+
if (!checkID_ADXL())
283+
{
284+
retries++;
285+
286+
powerADXL(false);
287+
Delay(1000);
288+
powerADXL(true);
289+
290+
Delay(1000);
239291

292+
/* Soft reset */
293+
softResetADXL();
294+
295+
/* Last try to get the correct ID failed */
296+
if (!checkID_ADXL())
297+
{
298+
Error(0);
299+
}
300+
}
240301
}
241-
else
242-
{
302+
}
243303

244304
#ifdef DEBUGGING /* DEBUGGING */
245-
dbcrit("Retry soft reset - Incorrect ID!");
305+
if (retries < 2) dbinfoInt("Soft reset ADXL done (", retries, " retries)", false);
306+
else dbwarnInt("Soft reset ADXL done, had to \"hard reset\" (", retries, " retries)", false);
246307
#endif /* DEBUGGING */
247308

248-
Error(0);
249-
}
250-
}
251309
}
252310

253311

@@ -330,6 +388,50 @@ void readADXL_XYZDATA (void)
330388
}
331389

332390

391+
/**************************************************************************//**
392+
* @brief
393+
* Configure the Output Data Rate (ODR).
394+
*
395+
* @param[in] givenODR
396+
* @li 0 - 12.5 Hz
397+
* @li 1 - 25 Hz
398+
* @li 2 - 50 Hz
399+
* @li 3 - 100 Hz (reset default)
400+
* @li 4 - 200 Hz
401+
* @li 5 - 400 Hz
402+
*****************************************************************************/
403+
void configADXL_ODR (uint8_t givenODR)
404+
{
405+
/* Get value in register */
406+
uint8_t reg = readADXL(ADXL_REG_FILTER_CTL);
407+
408+
/* AND with mask to keep the bits we don't want to change */
409+
reg = reg & 0b11111000;
410+
411+
/* OR with new setting bits */
412+
413+
/* Set ODR (last three bits) */
414+
if (givenODR == 0) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000000));
415+
if (givenODR == 1) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000001));
416+
if (givenODR == 2) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000010));
417+
if (givenODR == 3) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000011));
418+
if (givenODR == 4) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000100));
419+
if (givenODR == 5) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000101));
420+
else writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000011));
421+
422+
#ifdef DEBUGGING /* DEBUGGING */
423+
if (givenODR == 0) dbinfo("ODR set at 12.5 Hz");
424+
else if (givenODR == 1) dbinfo("ODR set at 25 Hz");
425+
else if (givenODR == 2) dbinfo("ODR set at 50 Hz");
426+
else if (givenODR == 3) dbinfo("ODR set at 100 Hz");
427+
else if (givenODR == 4) dbinfo("ODR set at 200 Hz");
428+
else if (givenODR == 5) dbinfo("ODR set at 400 Hz");
429+
else dbinfo("ODR set at 100 Hz (reset default)");
430+
#endif /* DEBUGGING */
431+
432+
}
433+
434+
333435
/**************************************************************************//**
334436
* @brief
335437
* Configure the measurement range and store the selected one in
@@ -345,19 +447,22 @@ void configADXL_range (uint8_t givenRange)
345447
/* Get value in register */
346448
uint8_t reg = readADXL(ADXL_REG_FILTER_CTL);
347449

348-
/* TODO: fix masking */
450+
/* AND with mask to keep the bits we don't want to change */
451+
reg = reg & 0b00111111;
452+
453+
/* OR with new setting bits */
349454

350455
/* Set measurement range (first two bits) */
351456
if (givenRange == 0) {
352-
writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00010011));
457+
writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000000));
353458
range = 0;
354459
}
355460
else if (givenRange == 1) {
356-
writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b01010011));
461+
writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b01000000));
357462
range = 1;
358463
}
359464
else if (givenRange == 2) {
360-
writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b10010011));
465+
writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b10000000));
361466
range = 2;
362467
}
363468

@@ -383,8 +488,6 @@ void configADXL_range (uint8_t givenRange)
383488
*****************************************************************************/
384489
void configADXL_activity (uint8_t gThreshold)
385490
{
386-
/* TODO: maybe fix masking */
387-
388491
/* Map activity detector to INT1 pin */
389492
writeADXL(ADXL_REG_INTMAP1, 0b00010000); /* Bit 4 selects activity detector */
390493

@@ -420,16 +523,22 @@ void configADXL_activity (uint8_t gThreshold)
420523
*
421524
* @param[in] enabled
422525
* @li True - Enable measurement mode.
423-
* @li False - Disable measurement mode.
526+
* @li False - Disable measurement mode (standby).
424527
*****************************************************************************/
425528
void measureADXL (bool enabled)
426529
{
427530
if (enabled)
428531
{
429-
/* TODO: fix masking */
532+
/* Get value in register */
533+
uint8_t reg = readADXL(ADXL_REG_POWER_CTL);
534+
535+
/* AND with mask to keep the bits we don't want to change */
536+
reg = reg & 0b11111100;
537+
538+
/* OR with new setting bits */
430539

431540
/* Enable measurements */
432-
writeADXL(ADXL_REG_POWER_CTL, 0b00000010); /* Last 2 bits are measurement mode */
541+
writeADXL(ADXL_REG_POWER_CTL, reg | 0b00000010); /* Last 2 bits are measurement mode */
433542

434543
#ifdef DEBUGGING /* DEBUGGING */
435544
dbinfo("Measurement enabled");
@@ -438,7 +547,20 @@ void measureADXL (bool enabled)
438547
}
439548
else
440549
{
441-
/* TODO */
550+
/* Get value in register */
551+
uint8_t reg = readADXL(ADXL_REG_POWER_CTL);
552+
553+
/* AND with mask to keep the bits we don't want to change */
554+
reg = reg & 0b11111100;
555+
556+
/* OR with new setting bits */
557+
558+
/* Enable measurements */
559+
writeADXL(ADXL_REG_POWER_CTL, reg | 0b00000000); /* Last 2 bits are measurement mode */
560+
561+
#ifdef DEBUGGING /* DEBUGGING */
562+
dbinfo("Measurement disabled (standby)");
563+
#endif /* DEBUGGING */
442564
}
443565
}
444566

@@ -462,7 +584,6 @@ void softResetADXL (void)
462584
*****************************************************************************/
463585
bool checkID_ADXL (void)
464586
{
465-
uint8_t test = readADXL(ADXL_REG_DEVID_AD);
466587
return (readADXL(ADXL_REG_DEVID_AD) == 0xAD);
467588
}
468589

0 commit comments

Comments
 (0)