Uart with ADXL362

Dependencies:   ADXL362

My Memo's for EV-COG-AD4050LZ Pinname

<<CODE>>
#define GPIO_PORT_SHIFT 12


typedef enum {
    P0_00  = (0 << GPIO_PORT_SHIFT | 0 ),
    P0_01  = (0 << GPIO_PORT_SHIFT | 1 ),
    P0_02  = (0 << GPIO_PORT_SHIFT | 2 ),
    P0_03  = (0 << GPIO_PORT_SHIFT | 3 ),
    P0_04  = (0 << GPIO_PORT_SHIFT | 4 ),
    P0_05  = (0 << GPIO_PORT_SHIFT | 5 ),
    P0_06  = (0 << GPIO_PORT_SHIFT | 6 ),
    P0_07  = (0 << GPIO_PORT_SHIFT | 7 ),
    P0_08  = (0 << GPIO_PORT_SHIFT | 8 ),
    P0_09  = (0 << GPIO_PORT_SHIFT | 9 ),
    P0_10 = (0 << GPIO_PORT_SHIFT | 10),
    P0_11 = (0 << GPIO_PORT_SHIFT | 11),
    P0_12 = (0 << GPIO_PORT_SHIFT | 12),
    P0_13 = (0 << GPIO_PORT_SHIFT | 13),
    P0_14 = (0 << GPIO_PORT_SHIFT | 14),
    P0_15 = (0 << GPIO_PORT_SHIFT | 15),
    P1_00  = (1 << GPIO_PORT_SHIFT | 0 ),
    P1_01  = (1 << GPIO_PORT_SHIFT | 1 ),
    P1_02  = (1 << GPIO_PORT_SHIFT | 2 ),
    P1_03  = (1 << GPIO_PORT_SHIFT | 3 ),
    P1_04  = (1 << GPIO_PORT_SHIFT | 4 ),
    P1_05  = (1 << GPIO_PORT_SHIFT | 5 ),
    P1_06  = (1 << GPIO_PORT_SHIFT | 6 ),
    P1_07  = (1 << GPIO_PORT_SHIFT | 7 ),
    P1_08  = (1 << GPIO_PORT_SHIFT | 8 ),
    P1_09  = (1 << GPIO_PORT_SHIFT | 9 ),
    P1_10 = (1 << GPIO_PORT_SHIFT | 10),
    P1_11 = (1 << GPIO_PORT_SHIFT | 11),
    P1_12 = (1 << GPIO_PORT_SHIFT | 12),
    P1_13 = (1 << GPIO_PORT_SHIFT | 13),
    P1_14 = (1 << GPIO_PORT_SHIFT | 14),
    P1_15 = (1 << GPIO_PORT_SHIFT | 15),
    P2_00  = (2 << GPIO_PORT_SHIFT | 0 ),
    P2_01  = (2 << GPIO_PORT_SHIFT | 1 ),
    P2_02  = (2 << GPIO_PORT_SHIFT | 2 ),
    P2_03  = (2 << GPIO_PORT_SHIFT | 3 ),
    P2_04  = (2 << GPIO_PORT_SHIFT | 4 ),
    P2_05  = (2 << GPIO_PORT_SHIFT | 5 ),
    P2_06  = (2 << GPIO_PORT_SHIFT | 6 ),
    P2_07  = (2 << GPIO_PORT_SHIFT | 7 ),
    P2_08  = (2 << GPIO_PORT_SHIFT | 8 ),
    P2_09  = (2 << GPIO_PORT_SHIFT | 9 ),
    P2_10 = (2 << GPIO_PORT_SHIFT | 10),
    P2_11 = (2 << GPIO_PORT_SHIFT | 11),

    // USB Pins
    USBTX = P0_10,
    USBRX = P0_11,
    USBTX1 = P1_15,
    USBRX1 = P2_00,

    // mbed original LED naming
    LED1 = P2_02,
    LED2 = P2_10,
    LED3 = LED2,
    LED4 = LED1,

    //Push buttons
    PB0 = P1_00,        // BTN1
    PB1 = P0_09,        // BTN2
    BUTTON1 = P1_00,    // BTN1
    BUTTON2 = P0_09,    // BTN2

    BOOT = P1_01,
    WAKE0 = P0_15,      // JP15 to select
    WAKE1 = P1_00,      // JP8 (BTN1 jumper) to select
    WAKE2 = P0_13,      // JP4 to select
    WAKE3 = P2_01,      // JP15 to select

    // SPI Pins
    SPI0_SCLK = P0_00,
    SPI0_MOSI = P0_01,
    SPI0_MISO = P0_02,
    SPI0_CS0 = P0_03,
    SPI0_CS1 = P1_10,
    SPI0_CS2 = P2_08,
    SPI0_CS3 = P2_09,

    SPI1_SCLK = P1_06,
    SPI1_MOSI = P1_07,
    SPI1_MISO = P1_08,
    SPI1_CS0 = P1_09,
    SPI1_CS1 = P2_11,
    SPI1_CS2 = P2_02,
    SPI1_CS3 = P1_10,

    SPI2_SCLK = P1_02,
    SPI2_MOSI = P1_03,
    SPI2_MISO = P1_04,
    SPI2_CS0 = P1_05,
    SPI2_CS1 = P0_09,
    SPI2_CS2 = P2_10,
    SPI2_CS3 = P2_07,

    // ADC Pins
    ADC_VIN0 = P2_03,
    ADC_VIN1 = P2_04,
    ADC_VIN2 = P2_05,
    ADC_VIN3 = P2_06,
    ADC_VIN4 = P2_07,
    ADC_VIN5 = P2_08,
    ADC_VIN6 = P2_09,
    ADC_VIN7 = P2_10,

    // Arduino Headers
    D0 = P0_10,        // UART0_TXD
    D1 = P0_11,        // UART0_RXD
    D2 = P0_15,        // INT_WAKE0
    D3 = P0_13,        // EXT_INT_WAKE2
    D4 = P0_09,        // EXT_SPI2_CS1
    D5 = P2_01,        // INT_WAKE3 or EXT_RTC1_SS1 via JP8
    D6 = P1_11,        // GPIO_27
    D7 = P0_12,        // GPIO_08 or GPIO_12 via JP7

    D8 = P1_12,         // GPIO_28
    D9 = P1_14,         // GPIO_30
    D10 = SPI0_CS2,     // P2_08
    D11 = SPI0_MOSI,    // P0_01
    D12 = SPI0_MISO,    // P0_02
    D13 = SPI0_SCLK,    // P0_00
    I2C_SCL = P0_04,    // I2C_SCL
    I2C_SDA = P0_05,    // I2C_SDA

    A0 = P2_03,         // ADC0
    A1 = P2_04,         // EXT_ADC1
    A2 = P2_05,         // EXT_ADC2
    A3 = P2_06,         // ADC3
    A4 = P2_07,         // SPI2_CS3
    A5 = P2_10,         // EXT_GPIO42

    // Not connected
    NC = (int)0xFFFFFFFF
} PinName;
<</CODE>>

main.cpp

Committer:
APS_Lab
Date:
2019-01-11
Revision:
0:5868fc6c16b3

File content as of revision 0:5868fc6c16b3:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "stats_report.h"
#include "ADXL362.h"
#include "math.h"

#define G_2 1.0
#define G_4 2.0
#define G_8 4.0



#define MODE_NORMAL 0
#define MODE_LOW_POWER 1
#define MODE_ULTRA_LOW_POWER 2

DigitalOut led1(LED1);
Serial PC(USBTX, USBRX);

char Buf[128];
char command[8];
char mode[8];
char val[8];
int f_val;

int set_mode(int m);
int set_freq(int v);
int set_grav(int v);
void set_start(void);
void set_stop(void);
static void clearDisplay(void);
static void drawCLI(ADXL362 *adxlCtrl, AccelTemp *getData);
volatile int f_run = 0;
volatile int f_wake = 0;
SPI         *spi1;
ADXL362     *adxlCtrl;
AccelTemp GetData;
DigitalIn ADXL_WAKE(WAKE2);
 
        
// main() runs in its own thread in the OS
int main()
{
    PC.baud(115200);
    PC.printf("EV-COG-AD4050LZ Demo\n");

    spi1 = new SPI(SPI1_MOSI, SPI1_MISO, SPI1_SCLK);
    adxlCtrl = new ADXL362(&PC, spi1); 

    clearDisplay();
    
    while (true) {
        // Blink LED and wait 0.5 seconds
        led1 = !led1;
        wait_ms(500);

        PC.scanf("%s", command);
        
        if(strncmp(command, "set", 3) == 0)
        {
            PC.scanf("%s",mode);
            
            if(strncmp(mode, "freq", 4) == 0)
            {
                PC.scanf("%s", val);
                f_val = atoi(val);
                set_freq(f_val);
            }
            else if(strncmp(mode, "mode", 4) == 0)
            {
                PC.scanf("%s", val);
                f_val = atoi(val);
                set_mode(f_val);
            }
            else if(strncmp(mode, "grav", 4) == 0)
            {
                PC.scanf("%s", val);
                f_val = atoi(val);
                set_grav(f_val);
            }
            else if(strncmp(mode, "wakeup", 6) == 0)
            {
                PC.printf("ADXL362 wakeup mode\n");
                adxlCtrl->set_wakeupmode();
                f_wake = 1;
                PC.printf("ADXL362 wakeup waiting......\n");
                while(f_wake)
                {
                    if()
                    {
                        f_wake = 0;
                        PC.printf("ADXL362 Wake-up\n");
                    }
                }
                
                
            }else
            {
                PC.printf("Command Invalid\n");
            }
        }
        else if(strncmp(command, "start", 5) == 0)
        {
            set_start();
            while(f_run)
            {
                adxlCtrl->SensorRead(&GetData);
                drawCLI(adxlCtrl, &GetData);
                wait(0.5);
            }
        }
        //else if(strncmp(command, "stop", 4) == 0)
        //{
        //    set_stop();
        //}
        else
        {
            PC.printf("Command Usage\n");
            PC.printf("Command : only 'set'\n");
            PC.printf("Mode    : 'freq' is ODR configuration\n");
            PC.printf("        : ODR frequency from 12, 25, 50, 100, 200, 400Hz\n");
            PC.printf("Mode    : 'grav' is select gravity\n");
            PC.printf("        : 0:2g 1:4g 2:8g\n");
            PC.printf("Mode    : 'mode' is power mode\n");
            PC.printf("        : 0:Normal 1:Low Power 2:Ultra Low Power(freq is fixed at 100Hz)\n");
            PC.printf("i.e.  'set mode 0'\n");
            PC.printf("i.e.  'set freq 1000'\n");
            PC.printf("i.e.  'start'\n");
        }
        // Following the main thread wait, report on the current system status
        //sys_state.report_state();
    }
}


int set_freq(int v)
{
    if((v < 12) || (v > 400)) 
    {
        PC.printf("ODR Freqency Range is over/under. Please configure for 12-400 Hz.\n");
        return 1;
    }
    else
    {
        PC.printf("Set Frequency %d Hz\n", v);
        return 0;
    }
}

int set_grav(int v)
{
    switch(v)
    {
        case 0:
            PC.printf("Set gravity 2g\n");
            adxlCtrl->set_gravity(GRAVITY_2G);
            break;
        case 1:
            PC.printf("Set gravity 4g\n");
            adxlCtrl->set_gravity(GRAVITY_4G);
            break;
        case 2:
            PC.printf("Set gravity 8g\n");
            adxlCtrl->set_gravity(GRAVITY_8G);
            break;
        default:
            PC.printf("Unknown Command\n");
            break;
    };
    
    return 0;
}
int set_mode(int m)
{
    switch(m)
    {
        case MODE_NORMAL:
            PC.printf("Set Normal Mode\n");
            adxlCtrl->set_powermode(POWER_CTL_PARAM_LOWNOISE_NORM);
            break;
        case MODE_LOW_POWER:
            PC.printf("Set Low Power Mode\n");
            adxlCtrl->set_powermode(POWER_CTL_PARAM_LOWNOISE_LOW);
            break;
        case MODE_ULTRA_LOW_POWER:
            PC.printf("Set Ultra Low Power Mode\n");
            adxlCtrl->set_powermode(POWER_CTL_PARAM_LOWNOISE_ULTRA);
            break;
        default:
            PC.printf("Unknown Command\n");
            break;
    };
    
    return 0;
}

void set_start(void)
{
    f_run = 1;
    adxlCtrl->start();
    PC.printf("Start Accel\n");
}

void set_stop(void)
{
    f_run = 0;
    adxlCtrl->stop();
    PC.printf("Stop Accel\n");
}

static void clearDisplay(void)
{
    PC.printf("\033[2J");
    PC.printf("\033[0;0H");
    PC.printf("\033[0m\033[37m");    
}

static void drawCLI(ADXL362 *adxlCtrl, AccelTemp *getData)
{
    AccelTemp *min = adxlCtrl->GetMinInfo();
    AccelTemp *max = adxlCtrl->GetMaxInfo();
    AccelTemp *p;
    float x, y, z, t;
    
    PC.printf("\033[2J");
    
    /* BLANK LINE */
    
    PC.printf("\033[0m\033[33m\033[1m");
    PC.printf("\033[2;1H");
    PC.printf("\033[K");
    PC.printf("ACCELEROMETERs");
    
    PC.printf("\033[0m\033[37m");
    PC.printf("\033[3;3H");
    PC.printf("\033[K");
    PC.printf("\033[3;10H-X---\033[3;20H-Y---\033[3;30H-Z---");
    
    PC.printf("\033[0m\033[35m");
    PC.printf("\033[4;3H");
    PC.printf("\033[K");
    p = min;
    x = sin(adxlCtrl->ConvAccel(p->ax));
    y = adxlCtrl->ConvAccel(p->ay);
    z = adxlCtrl->ConvAccel(p->az);
    PC.printf("min\033[4;10H%04.2f\033[4;20H%04.2f\033[4;30H%04.2f", x, y, z);
    
    PC.printf("\033[0m\033[37m\033[1m");
    PC.printf("\033[5;3H");
    PC.printf("\033[K");
    p = getData;
    x = adxlCtrl->ConvAccel(p->ax);
    y = adxlCtrl->ConvAccel(p->ay);
    z = adxlCtrl->ConvAccel(p->az);
    PC.printf("-->\033[5;10H%04.2f\033[5;20H%04.2f\033[5;30H%04.2f", x, y, z);
    
    PC.printf("\033[0m\033[36m");
    PC.printf("\033[6;3H");
    PC.printf("\033[K");
    p = max;
    x = adxlCtrl->ConvAccel(p->ax);
    y = adxlCtrl->ConvAccel(p->ay);
    z = adxlCtrl->ConvAccel(p->az);
    PC.printf("max\033[6;10H%04.2f\033[6;20H%04.2f\033[6;30H%04.2f", x, y, z);
    
    /* BLANK LINE */
        
    PC.printf("\033[0m\033[31m\033[1m");
    PC.printf("\033[8;1H");
    PC.printf("\033[K");
    PC.printf("Temperature");

    /* BLANK LINE */

    PC.printf("\033[0m\033[35m");
    PC.printf("\033[10;3H");
    PC.printf("\033[K");
    p = min;
    t = adxlCtrl->ConvThermal(p->tm);
    PC.printf("min\033[10;10H%04.2f", t);

    PC.printf("\033[0m\033[37m\033[1m");
    PC.printf("\033[11;3H");
    PC.printf("\033[K");
    p = getData;
    t = adxlCtrl->ConvThermal(p->tm);
    PC.printf("-->\033[11;10H%04.2f", t);
    
    PC.printf("\033[0m\033[36m");
    PC.printf("\033[12;3H");
    PC.printf("\033[K");
    p = max;
    t = adxlCtrl->ConvThermal(p->tm);
    PC.printf("max\033[12;10H%04.2f", t);
}