Icarus Sensors / Mbed 2 deprecated ADXL362_test

Dependencies:   mbed

Fork of BLE_UARTConsole by Bluetooth Low Energy

main.cpp

Committer:
smigielski
Date:
2015-01-21
Revision:
7:7ba97abac55b
Parent:
5:2607923acfa1
Child:
8:8db3894cf5e6

File content as of revision 7:7ba97abac55b:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>
#include "mbed.h"
#include "BLEDevice.h"

#include "UARTService.h"

#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                               * it will have an impact on code-size and power consumption. */

#if NEED_CONSOLE_OUTPUT
#define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */


// ACC Registers
#define ID0 0x00
#define STATUS 0x0b
#define RESET 0x1f
#define INTMAP1 0x2a
#define INTMAP2 0x2b
#define FILTER_CTL 0x2c
#define POWER_CTL 0x2d
 
#define WR_SPI 0x0A     
#define RD_SPI 0x0B
#define DOWN 0
#define UP 1

BLEDevice  ble;
DigitalOut led1(LED1);
UARTService *uart;

// function definitions
void drSub();
uint8_t ACC_ReadReg( uint8_t reg );
void ACC_WriteReg( uint8_t reg, uint8_t reg );
uint32_t drFlag;
void ACC_GetXYZ12( int16_t *x, int16_t *y, int16_t *z);
void ACC_GetXYZ8( int8_t *x, int8_t *y, int8_t *z);
void retargetStdout();
 
// mbed hardware config 
// CONFIGURE:
SPI spi(p11, p12, p13); // mosi, miso, sclk
DigitalOut cs(p14);
InterruptIn dr(p15);


    

void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}

void periodicCallback(void)
{
    
        led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
    
}

int main(void)
{
       // local variables
    uint8_t reg;   
    
    int8_t x8 = 0;
    int8_t y8 = 0;
    int8_t z8 = 0;
    
    int16_t x12 = 0;
    int16_t y12 = 0;
    int16_t z12 = 0;
    
     led1 = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);

    DEBUG("Initialising the nRF51822\n\r");
    ble.init();
    ble.onDisconnection(disconnectionCallback);
    
    uart = new UARTService(ble);
    


    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));

    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
    ble.startAdvertising();
    
    retargetStdout();
    
    
        // mbed spi config
    // spi 8 bits, mode 0, 1 MHz for adxl362
    spi.format(8,0);
    // 5 MHz, max for acc - works fine
    spi.frequency(5000000);
 
    // mbed interrupt config
    // data ready for adxl362
    drFlag = 0;
    dr.mode(PullDown);
    dr.rise(&drSub);
     __disable_irq();
        
    // reset the adxl362
    wait_ms(100); 
    ACC_WriteReg(RESET, 0x52);  
    wait_ms(100); 
    
    // read adxl362 registers
    printf("\r\n");
    // read id register
    reg = ACC_ReadReg(ID0);   
    printf("ID0 = 0x%X\r\n", reg);
    reg = ACC_ReadReg(FILTER_CTL);   
    printf("FILTER_CTL = 0x%X\r\n", reg);   
    
    // set adxl362 to 4g range, 25Hz
    //ACC_WriteReg(FILTER_CTL,0x51); 
    // 2g, 25Hz  
    ACC_WriteReg(FILTER_CTL,0x11); 
    reg = ACC_ReadReg(FILTER_CTL);   
    printf("FILTER_CTL = 0x%X\r\n", reg);    
        
    // map adxl362 interrupts
    ACC_WriteReg(INTMAP1,0x01);  
    reg = ACC_ReadReg(INTMAP1);   
    printf("INTMAP1 = 0x%X\r\n", reg);   
    
    // set adxl362 to measurement mode, ultralow noise
    ACC_WriteReg(POWER_CTL,0x22);  
    reg = ACC_ReadReg(POWER_CTL);   
    printf("POWER_CTL = 0x%X\r\n", reg);    
     
    // start continuous processing adxl362 data
    __enable_irq(); 
 
    

    


    while (true) {
                if(drFlag == 8)
        {
            ACC_GetXYZ8(&x8, &y8, &z8);
            printf("%+04d %+04d %+04d\r\n", x8,y8,z8);
            drFlag = 0;     
        }
        
        else if(drFlag == 12)
        {
            ACC_GetXYZ12(&x12, &y12, &z12);
            printf("%+05d %+05d %+05d\r\n",x12, y12, z12);
            //pc.printf("%04X, %04X, %04X\r\n", x12, y12, z12);
            drFlag = 0;    
        }
        ble.waitForEvent();
    }
}


////////////////////////////////////////////////////////////////////////////////////
// read 8-bit x,y,z data 
////////////////////////////////////////////////////////////////////////////////////      
        
void ACC_GetXYZ8(int8_t* x, int8_t* y, int8_t* z)
{
    
    cs = DOWN;
    spi.write(RD_SPI); 
    spi.write(0x08);  
    
    *x = spi.write(0x00);
    *y = spi.write(0x00);
    *z = spi.write(0x00);
 
    cs = UP;
}
 
////////////////////////////////////////////////////////////////////////////////////
// read 12-bit x,y,z data
////////////////////////////////////////////////////////////////////////////////////      
        
 void ACC_GetXYZ12(int16_t* x, int16_t* y, int16_t* z)
{
    int16_t xyzVal[6] = {0, 0, 0, 0, 0, 0};
    
    cs = DOWN;
    spi.write(RD_SPI); 
    spi.write(0x0E);  
    
    for (uint32_t i = 0; i < 6; i++)
    {
        xyzVal[i] = spi.write(0x00);
    }
 
    *x = (xyzVal[1] << 8) + xyzVal[0];
    *y = (xyzVal[3] << 8) + xyzVal[2];
    *z = (xyzVal[5] << 8) + xyzVal[4];  
 
    cs = UP;
}
 
////////////////////////////////////////////////////////////////////////////////////
// read ACC 8-bit registers
////////////////////////////////////////////////////////////////////////////////////
 
uint8_t ACC_ReadReg( uint8_t reg ) 
{
    cs = DOWN;
    spi.write(RD_SPI); 
    spi.write(reg);  
    uint8_t val = spi.write(0x00);
    cs = UP;
    return (val);
}
 
////////////////////////////////////////////////////////////////////////////////////
// write ACC 8-bit register
////////////////////////////////////////////////////////////////////////////////////
 
void ACC_WriteReg( uint8_t reg, uint8_t cmd ) 
{
    cs = DOWN;
    spi.write(WR_SPI);
    spi.write(reg);
    spi.write(cmd);    
    cs = UP;
}

  /**
     * Following a call to this function, all writes to stdout (such as from
     * printf) get redirected to the outbound characteristic of this service.
     * This might be very useful when wanting to receive debug messages over BLE.
     *
     * @Note: debug messages originating from printf() like calls are buffered
     * before being sent out. A '\n' in the printf() triggers the buffer update
     * to the underlying characteristic.
     *
     * @Note: long messages need to be chopped up into 20-byte updates so that
     * they flow out completely with notifications. The receiver should be
     * prepared to stitch these messages back.
     */
    void retargetStdout() {
        freopen("/blueart", "w", stdout);
    }
 
////////////////////////////////////////////////////////////////////////////////////
// Handle data ready interrupt
// just sets data ready flag
////////////////////////////////////////////////////////////////////////////////////
 
void drSub() 
{
   drFlag = 8;
   //drFlag = 12;
}