Skip to content

Commit 124cb78

Browse files
author
Owen L - SFE
committed
add lis2dh12 driver for Edge variant
1 parent b038c67 commit 124cb78

File tree

4 files changed

+3327
-0
lines changed

4 files changed

+3327
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright (c) 2019 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "lis2dh12_platform_apollo3.h"
24+
25+
/*
26+
* @brief Write generic device register (platform dependent)
27+
*
28+
* @param handle customizable argument. In this examples is used in
29+
* order to select the correct sensor bus handler.
30+
* @param reg register to write
31+
* @param bufp pointer to data to write in register reg
32+
* @param len number of consecutive register to write
33+
*
34+
*/
35+
int32_t lis2dh12_write_platform_apollo3(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
36+
{
37+
uint32_t retVal32 = 0;
38+
lis2dh12_platform_apollo3_if_t* pif = (lis2dh12_platform_apollo3_if_t*)handle;
39+
am_hal_iom_transfer_t iomTransfer = {0};
40+
41+
if( bufp == NULL ) { return AM_HAL_STATUS_FAIL; }
42+
if( pif == NULL ) { return AM_HAL_STATUS_FAIL; }
43+
if( pif->iomHandle == NULL) { return AM_HAL_STATUS_FAIL; }
44+
45+
// Set up transfer
46+
iomTransfer.uPeerInfo.ui32I2CDevAddr = pif->addCS;
47+
iomTransfer.ui32InstrLen = 1;
48+
iomTransfer.ui32Instr = (reg | 0x80);
49+
iomTransfer.ui32NumBytes = len;
50+
iomTransfer.eDirection = AM_HAL_IOM_TX;
51+
iomTransfer.pui32TxBuffer = (uint32_t*)bufp;
52+
iomTransfer.pui32RxBuffer = NULL;
53+
iomTransfer.bContinue = false;
54+
55+
if( pif->useSPI ){
56+
// ToDo: Support SPI w/ CS assertion
57+
}
58+
59+
// Send the transfer
60+
retVal32 = am_hal_iom_blocking_transfer(pif->iomHandle, &iomTransfer);
61+
62+
if( pif->useSPI ){
63+
// ToDo: Support SPI / CS de-assertion
64+
}
65+
66+
if( retVal32 != AM_HAL_STATUS_SUCCESS ){ return retVal32; }
67+
68+
return 0;
69+
}
70+
71+
/*
72+
* @brief Read generic device register (platform dependent)
73+
*
74+
* @param handle customizable argument. In this examples is used in
75+
* order to select the correct sensor bus handler.
76+
* @param reg register to read
77+
* @param bufp pointer to buffer that store the data read
78+
* @param len number of consecutive register to read
79+
*
80+
*/
81+
int32_t lis2dh12_read_platform_apollo3(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
82+
{
83+
uint32_t retVal32 = 0;
84+
lis2dh12_platform_apollo3_if_t* pif = (lis2dh12_platform_apollo3_if_t*)handle;
85+
am_hal_iom_transfer_t iomTransfer = {0};
86+
87+
if( bufp == NULL ) { return AM_HAL_STATUS_FAIL; }
88+
if( pif == NULL ) { return AM_HAL_STATUS_FAIL; }
89+
if( pif->iomHandle == NULL) { return AM_HAL_STATUS_FAIL; }
90+
91+
// Set up first transfer
92+
iomTransfer.uPeerInfo.ui32I2CDevAddr = pif->addCS;
93+
iomTransfer.ui32InstrLen = 1;
94+
iomTransfer.ui32Instr = (reg | 0x80);
95+
iomTransfer.ui32NumBytes = 0;
96+
iomTransfer.eDirection = AM_HAL_IOM_TX;
97+
iomTransfer.bContinue = true;
98+
99+
if( pif->useSPI ){
100+
// ToDo: Support SPI w/ CS assertion
101+
}
102+
103+
// Send the first transfer
104+
retVal32 = am_hal_iom_blocking_transfer(pif->iomHandle, &iomTransfer);
105+
if( retVal32 != AM_HAL_STATUS_SUCCESS ){ return retVal32; }
106+
107+
// Change direction, and add the rx buffer
108+
iomTransfer.eDirection = AM_HAL_IOM_RX;
109+
iomTransfer.pui32RxBuffer = (uint32_t*)bufp;
110+
iomTransfer.ui32NumBytes = len;
111+
iomTransfer.bContinue = false;
112+
113+
// Send the second transfer
114+
retVal32 = am_hal_iom_blocking_transfer(pif->iomHandle, &iomTransfer);
115+
116+
if( retVal32 != AM_HAL_STATUS_SUCCESS ){ return retVal32; }
117+
118+
return 0;
119+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright (c) 2019 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#ifndef _LIS2DH12_PLATFORM_APOLLO3_H_
24+
#define _LIS2DH12_PLATFORM_APOLLO3_H_
25+
26+
#include "am_mcu_apollo.h"
27+
#include "lis2dh12_reg.h"
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
typedef struct _lis2dh12_platform_apollo3_if_t {
34+
void* iomHandle; // IO Master instance
35+
uint8_t addCS; // I2C mode: the 7-bit I2C address (either 0x18 or 0x19 depeding on SA0 pin)
36+
// SPI mode: the Apollo3 pad to use for chip select
37+
bool useSPI; // Set 'true' if using SPI mode, else 'false'
38+
}lis2dh12_platform_apollo3_if_t;
39+
40+
int32_t lis2dh12_write_platform_apollo3(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
41+
int32_t lis2dh12_read_platform_apollo3(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+
47+
#endif // _LIS2DH12_PLATFORM_APOLLO3_H_

0 commit comments

Comments
 (0)