Future Electronics
/
sequana-ble-lab-base
This is a basic code to be used for Sequana BLE Lab exercises.
source/Kmx65.h@4:44690f4495ef, 2019-03-22 (annotated)
- Committer:
- lru
- Date:
- Fri Mar 22 10:11:59 2019 +0000
- Revision:
- 4:44690f4495ef
- Parent:
- 2:06e62a299a74
Initialized properly device name characteristic and changed type of advertising MAC address used to guarantee uniqueness.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lru | 0:ff033dfc838b | 1 | /* |
lru | 0:ff033dfc838b | 2 | * Copyright (c) 2017-2019 Future Electronics |
lru | 0:ff033dfc838b | 3 | * |
lru | 0:ff033dfc838b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lru | 0:ff033dfc838b | 5 | * you may not use this file except in compliance with the License. |
lru | 0:ff033dfc838b | 6 | * You may obtain a copy of the License at |
lru | 0:ff033dfc838b | 7 | * |
lru | 0:ff033dfc838b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lru | 0:ff033dfc838b | 9 | * |
lru | 0:ff033dfc838b | 10 | * Unless required by applicable law or agreed to in writing, software |
lru | 0:ff033dfc838b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lru | 0:ff033dfc838b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lru | 0:ff033dfc838b | 13 | * See the License for the specific language governing permissions and |
lru | 0:ff033dfc838b | 14 | * limitations under the License. |
lru | 0:ff033dfc838b | 15 | */ |
lru | 0:ff033dfc838b | 16 | |
lru | 2:06e62a299a74 | 17 | #ifndef KMX65_H_ |
lru | 2:06e62a299a74 | 18 | #define KMX65_H_ |
lru | 0:ff033dfc838b | 19 | |
lru | 0:ff033dfc838b | 20 | #include <stdint.h> |
lru | 0:ff033dfc838b | 21 | #include <Sensor.h> |
lru | 0:ff033dfc838b | 22 | |
lru | 0:ff033dfc838b | 23 | |
lru | 2:06e62a299a74 | 24 | /** Represents measurement result received from KMX65 accelerometer and magnetometer sensor. |
lru | 0:ff033dfc838b | 25 | * When this matches format of the sensor characteristic then |
lru | 0:ff033dfc838b | 26 | * no conversion is needed. |
lru | 0:ff033dfc838b | 27 | */ |
lru | 2:06e62a299a74 | 28 | struct Kmx65Value { |
lru | 0:ff033dfc838b | 29 | int16_t acc_x; |
lru | 0:ff033dfc838b | 30 | int16_t acc_y; |
lru | 0:ff033dfc838b | 31 | int16_t acc_z; |
lru | 0:ff033dfc838b | 32 | int16_t mag_x; |
lru | 0:ff033dfc838b | 33 | int16_t mag_y; |
lru | 0:ff033dfc838b | 34 | int16_t mag_z; |
lru | 0:ff033dfc838b | 35 | }; |
lru | 0:ff033dfc838b | 36 | |
lru | 0:ff033dfc838b | 37 | |
lru | 2:06e62a299a74 | 38 | /** Driver for KMX65 sensor. |
lru | 0:ff033dfc838b | 39 | */ |
lru | 2:06e62a299a74 | 40 | class Kmx65Driver { |
lru | 0:ff033dfc838b | 41 | public: |
lru | 0:ff033dfc838b | 42 | /** Last measurement result status. |
lru | 0:ff033dfc838b | 43 | */ |
lru | 0:ff033dfc838b | 44 | enum Status { |
lru | 0:ff033dfc838b | 45 | STATUS_OK = 0, |
lru | 0:ff033dfc838b | 46 | STATUS_STALLED, |
lru | 0:ff033dfc838b | 47 | STATUS_NOT_READY |
lru | 0:ff033dfc838b | 48 | }; |
lru | 0:ff033dfc838b | 49 | |
lru | 0:ff033dfc838b | 50 | /** KX64 chip register addresses. |
lru | 0:ff033dfc838b | 51 | */ |
lru | 0:ff033dfc838b | 52 | enum Register { |
lru | 0:ff033dfc838b | 53 | WHO_AM_I = 0x00, |
lru | 0:ff033dfc838b | 54 | ODCNTL = 0x38, |
lru | 0:ff033dfc838b | 55 | CNTL1 = 0x39, |
lru | 0:ff033dfc838b | 56 | CNTL2 = 0x3A, |
lru | 0:ff033dfc838b | 57 | BUF_CTRL1 = 0x77, |
lru | 0:ff033dfc838b | 58 | BUF_CTRL2 = 0x78, |
lru | 0:ff033dfc838b | 59 | BUF_CTRL3 = 0x79, |
lru | 0:ff033dfc838b | 60 | BUF_CLEAR = 0x7A, |
lru | 0:ff033dfc838b | 61 | BUF_STATUS_1 = 0x7B, |
lru | 0:ff033dfc838b | 62 | BUF_STATUS_2 = 0x7C, |
lru | 0:ff033dfc838b | 63 | BUF_STATUS_3 = 0x7D, |
lru | 0:ff033dfc838b | 64 | BUF_READ = 0x7E, |
lru | 0:ff033dfc838b | 65 | READ_MASK = 0x80 |
lru | 0:ff033dfc838b | 66 | }; |
lru | 0:ff033dfc838b | 67 | |
lru | 0:ff033dfc838b | 68 | public: |
lru | 0:ff033dfc838b | 69 | /** Create and initialize driver. |
lru | 0:ff033dfc838b | 70 | * |
lru | 0:ff033dfc838b | 71 | * @param bus SPI bus to use |
lru | 0:ff033dfc838b | 72 | * @param cs CS/SS pin to use to select sensor on a bus |
lru | 0:ff033dfc838b | 73 | */ |
lru | 2:06e62a299a74 | 74 | Kmx65Driver(SPI& bus, PinName cs); |
lru | 0:ff033dfc838b | 75 | |
lru | 0:ff033dfc838b | 76 | /** Obtain reading of the sensor. |
lru | 0:ff033dfc838b | 77 | * |
lru | 0:ff033dfc838b | 78 | * @param[out] value measurement result |
lru | 0:ff033dfc838b | 79 | * @returns operation status |
lru | 0:ff033dfc838b | 80 | */ |
lru | 2:06e62a299a74 | 81 | Status read(Kmx65Value& value); |
lru | 0:ff033dfc838b | 82 | |
lru | 0:ff033dfc838b | 83 | /** Initialize sensor chip after reset. |
lru | 0:ff033dfc838b | 84 | */ |
lru | 0:ff033dfc838b | 85 | void init_chip(); |
lru | 0:ff033dfc838b | 86 | |
lru | 0:ff033dfc838b | 87 | /** Clear readout buffer. |
lru | 0:ff033dfc838b | 88 | * |
lru | 0:ff033dfc838b | 89 | * Sensor read out is perform via the FIFO buffer included within the chip. |
lru | 0:ff033dfc838b | 90 | * To make sure the readout is synchronized with measurement this buffer |
lru | 0:ff033dfc838b | 91 | * needs to be cleared prior to starting the next measurement cycle. |
lru | 0:ff033dfc838b | 92 | */ |
lru | 0:ff033dfc838b | 93 | void clear_buffer(); |
lru | 0:ff033dfc838b | 94 | |
lru | 0:ff033dfc838b | 95 | protected: |
lru | 0:ff033dfc838b | 96 | uint8_t spi_transaction(uint8_t address, uint8_t data); |
lru | 0:ff033dfc838b | 97 | void spi_read_multiple(uint8_t address, uint8_t *data, uint32_t length); |
lru | 0:ff033dfc838b | 98 | |
lru | 0:ff033dfc838b | 99 | SPI& _spi; |
lru | 0:ff033dfc838b | 100 | DigitalOut _chip_select; |
lru | 0:ff033dfc838b | 101 | char _tx_buffer[2]; |
lru | 0:ff033dfc838b | 102 | char _rx_buffer[2]; |
lru | 0:ff033dfc838b | 103 | }; |
lru | 0:ff033dfc838b | 104 | |
lru | 0:ff033dfc838b | 105 | |
lru | 0:ff033dfc838b | 106 | /** KX64 accelerometer/magnetometer sensor interface. |
lru | 0:ff033dfc838b | 107 | */ |
lru | 2:06e62a299a74 | 108 | class Kmx65Sensor : public Sensor<Kmx65Value> { |
lru | 0:ff033dfc838b | 109 | public: |
lru | 0:ff033dfc838b | 110 | /** Create and initialize sensor interface. |
lru | 0:ff033dfc838b | 111 | * |
lru | 0:ff033dfc838b | 112 | * @param spi SPI bus to use |
lru | 0:ff033dfc838b | 113 | * @param cs CS/SS pin to use to select sensor on a bus |
lru | 0:ff033dfc838b | 114 | */ |
lru | 2:06e62a299a74 | 115 | Kmx65Sensor(SPI &spi, PinName cs) : _driver(spi, cs) {} |
lru | 0:ff033dfc838b | 116 | |
lru | 0:ff033dfc838b | 117 | /** Schedule measurement process. |
lru | 0:ff033dfc838b | 118 | */ |
lru | 0:ff033dfc838b | 119 | virtual void start(EventQueue& ev_queue); |
lru | 0:ff033dfc838b | 120 | |
lru | 0:ff033dfc838b | 121 | protected: |
lru | 0:ff033dfc838b | 122 | void updater(); |
lru | 0:ff033dfc838b | 123 | |
lru | 2:06e62a299a74 | 124 | Kmx65Driver _driver; |
lru | 0:ff033dfc838b | 125 | }; |
lru | 0:ff033dfc838b | 126 | |
lru | 0:ff033dfc838b | 127 | #endif // KX64_H_ |