Initial version for the 2 chip MMA8653/MAG3110 version of the BBC Microbit
Dependencies: mbed AES BLE_API nRF51822 smallAES
Revision 4:052ba96b27f5, committed 2019-10-02
- Comitter:
- f3d
- Date:
- Wed Oct 02 18:52:21 2019 +0000
- Parent:
- 3:b9783107a8c4
- Commit message:
- Initial commit for MMA8653 version of BBC Microbit
Changed in this revision
diff -r b9783107a8c4 -r 052ba96b27f5 accelService.h --- a/accelService.h Mon Sep 30 11:59:42 2019 +0000 +++ b/accelService.h Wed Oct 02 18:52:21 2019 +0000 @@ -19,8 +19,8 @@ #include <mbed.h> // Accelerometer : LSM303 I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0 -const int LSM303_ADDRESS = (0x19<<1); -//const int MMA8653_ID = 0x5a; +const int MMA8653_ADDRESS = (0x1d<<1); +const int MMA8653_ID = 0x5a; class ACCELService { public: const static uint16_t ACCEL_SERVICE_UUID = 0xA012; @@ -34,16 +34,12 @@ GattCharacteristic *charTable[] = {&AccelX,&AccelY,&AccelZ}; GattService AccelService(ACCEL_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.addService(AccelService); - // Wake the accelerometer from sleep mode by writing disabling low power mode, setting freq to 10Hz and enabling all three axes + // Wake the accelerometer from sleep mode by writing 1 to register number 0x2a char Data[8]; // Declare a buffer for data transfer int Status; - Data[0]=0x20; // wake up LSM303 (max speed, all accel channels) - Data[1]=0x77; - Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register - - Data[0]=0x23; // enable high resolution mode - Data[1]=0x0e; - Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register + Data[0]=0x2a; + Data[1]=1; + Status = i2c.write(MMA8653_ADDRESS,Data,2); // Write data to register } GattAttribute::Handle_t getValueHandle() const { @@ -63,27 +59,28 @@ char Data[8]; // Declare a buffer for data transfer int Status; int16_t X; - - - Data[0]=0x80 + 0x28; // Register number 0x28 has the X data (2 bytes) - Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number - Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents - X = Data[1]; - X = (X << 8) + Data[0]; + Data[0]=0x01; // Register number 1 has the X data (2 bytes) + Status = i2c.write(MMA8653_ADDRESS,Data,1,true); // Write register number + Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents + X = Data[0]; + X = (X << 8) + Data[1]; + X = X >> 6; // only 10 bits of data are available int16_t Y; - Data[0]=0x80 + 0x2a; // Register number 0x2a has the Y data (2 bytes) - Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number - Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents - Y = Data[1]; - Y = (Y << 8) + Data[0]; + Data[0]=0x03; // Register number 3 has the Y data (2 bytes) + Status = i2c.write(MMA8653_ADDRESS,Data,1,true); // Write register number + Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents + Y = Data[0]; + Y = (Y << 8) + Data[1]; + Y = Y >> 6; // only 10 bits of data are available int16_t Z; - Data[0]=0x80 + 0x2c; // Register number 0x2c has the Z data (2 bytes) - Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number - Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents - Z = Data[1]; - Z = (Z << 8) + Data[0]; + Data[0]=0x05; // Register number 1 has the Z data (2 bytes) + Status = i2c.write(MMA8653_ADDRESS,Data,1,true); // Write register number + Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents + Z = Data[0]; + Z = (Z << 8) + Data[1]; + Z = Z >> 6; // only 10 bits of data are available updateAccelX(X); updateAccelY(Y);
diff -r b9783107a8c4 -r 052ba96b27f5 magservice.h --- a/magservice.h Mon Sep 30 11:59:42 2019 +0000 +++ b/magservice.h Wed Oct 02 18:52:21 2019 +0000 @@ -17,10 +17,7 @@ #ifndef __BLE_MAG_SERVICE_H__ #define __BLE_MAG_SERVICE_H__ #include <mbed.h> -// Accelerometer : LSM303 -//I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0 -const int LSM303_ADDRESS = (0x19<<1); -//const int MMA8653_ID = 0x5a; + class MAGService { public: const static uint16_t MAG_SERVICE_UUID = 0xfff3; @@ -34,16 +31,7 @@ GattCharacteristic *charTable[] = {&MagX,&MagY,&MagZ}; GattService MagService(MAG_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.addService(MagService); - // Wake the accelerometer from sleep mode by writing disabling low power mode, setting freq to 10Hz and enabling all three axes - char Data[8]; // Declare a buffer for data transfer - int Status; - Data[0]=0x20; // wake up LSM303 (max speed, all accel channels) - Data[1]=0x77; - Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register - - Data[0]=0x23; // enable high resolution mode - Data[1]=0x0e; - Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register + } GattAttribute::Handle_t getValueHandle() const { @@ -62,29 +50,10 @@ { char Data[8]; // Declare a buffer for data transfer int Status; - int16_t X; + int16_t X,Y,Z; // Modify to suit your chip: - // Goal get X,Y and Z readings off magnetometer. - Data[0]=0x80 + 0x28; // Register number 0x28 has the X data (2 bytes) - Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number - Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents - X = Data[1]; - X = (X << 8) + Data[0]; - - int16_t Y; - Data[0]=0x80 + 0x2a; // Register number 0x2a has the Y data (2 bytes) - Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number - Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents - Y = Data[1]; - Y = (Y << 8) + Data[0]; - - int16_t Z; - Data[0]=0x80 + 0x2c; // Register number 0x2c has the Z data (2 bytes) - Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number - Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents - Z = Data[1]; - Z = (Z << 8) + Data[0]; + updateMagX(X); updateMagY(Y);
diff -r b9783107a8c4 -r 052ba96b27f5 main.cpp --- a/main.cpp Mon Sep 30 11:59:42 2019 +0000 +++ b/main.cpp Wed Oct 02 18:52:21 2019 +0000 @@ -31,7 +31,7 @@ DigitalOut alivenessLED(P0_13, 0); DigitalOut actuatedLED(P0_14, 0); -const static char DEVICE_NAME[] = "front1"; +const static char DEVICE_NAME[] = "Microbit1"; //static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID,ACCELService::ACCEL_SERVICE_UUID,ButtonAService::BUTTONA_SERVICE_UUID}; static const uint16_t uuid16_list[] = {0xA012,0xFFF3}; @@ -109,9 +109,9 @@ bool initialValueForLEDCharacteristic = false; ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); btnAServicePtr = new ButtonAService(ble); - int16_t Tst=0; - AccelServicePtr = new ACCELService(ble,Tst); - MagServicePtr = new MAGService(ble,Tst); + int16_t InitialValue=0; + AccelServicePtr = new ACCELService(ble,InitialValue); + MagServicePtr = new MAGService(ble,InitialValue); /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));