Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/Drivers/eep.cpp

Committer:
davidjhoward
Date:
2016-09-14
Revision:
58:a4422d19b2ea
Child:
116:7337ed514891

File content as of revision 58:a4422d19b2ea:

// eep.c - EEPROM (AT24CM02)
// -------------------------------------------------------------------------------------------------

#include "mbed.h"
#include "global.h"
#include "eep.h"

// -------------------------------------------------------------------------------------------------
// globals

int eep_verbose = 0;

// -------------------------------------------------------------------------------------------------
// byte functions

unsigned char eep_get_byte(int dev, unsigned long addr)
{
    if(eep_verbose) printf("[eep_get_byte] dev=%d, addr=%u\n\n", dev, addr);

    unsigned char rbyte = 0;
    unsigned char dbyte = 0;
    unsigned char addr0 = 0;
    unsigned char addr1 = 0;

// build address bytes
    
    if     (dev == 0) dbyte = EE0_ADDR;
    else if(dev == 1) dbyte = EE1_ADDR;
    else
    {
        printf("[eep_get_byte] device error\n"); return(0);
    }

    if(addr < EEP_NBYT)
    {
        dbyte |= (addr & 0x00030000)>>16; // mask A17-A16 bits, add to dbyte
        addr0  = (addr & 0x0000FF00)>>8;  // mask A15-A08 bits
        addr1  = (addr & 0x000000FF);     // mask A07-A00 bits
    }
    else
    {
        printf("[eep_get_byte] address error\n"); return(0);
    }


    if(eep_verbose)
    {
        printf("   dbyte = 0x%x\n", dbyte);
        printf("   addr0 = 0x%x\n", addr0);
        printf("   addr1 = 0x%x", addr1);
    }

// perform read

    const int ldata = 2;
    char data[ldata];
    data[0] = addr0;
    data[1] = addr1;

    if(i2c->write(dbyte, data, 2, true )) printf("no ack 1\n");
    if(i2c->read (dbyte, data, 1, false)) printf("no ack 2\n");
    rbyte = data[0];

    return(rbyte);
}

void eep_set_byte(int dev, unsigned long addr, unsigned char wbyte)
{
    if(eep_verbose) printf("[eep_set_byte] dev=%d, addr=%u, wbyte=%u\n\n", dev, addr, wbyte);

    unsigned char dbyte = 0;
    unsigned char addr0 = 0;
    unsigned char addr1 = 0;

// build address bytes
    
    if     (dev == 0) dbyte = EE0_ADDR;
    else if(dev == 1) dbyte = EE1_ADDR;
    else
    {
        printf("[eep_set_byte] device error\n"); return;
    }

    if(addr < EEP_NBYT)
    {
        dbyte |= (addr & 0x00030000)>>16; // mask A17-A16 bits, add to dbyte
        addr0  = (addr & 0x0000FF00)>>8;  // mask A15-A08 bits
        addr1  = (addr & 0x000000FF);     // mask A07-A00 bits
    }
    else
    {
        printf("[eep_set_byte] address error\n"); return;
    }

    if(eep_verbose)
    {
        printf("   dbyte = 0x%x", dbyte);
        printf("   addr0 = 0x%x", addr0);
        printf("   addr1 = 0x%x", addr1);
        printf("   wbyte = 0x%x", wbyte);
    }

// perform read

    const int ldata = 3;
    char data[ldata];
    data[0] = addr0;
    data[1] = addr1;
    data[2] = wbyte;

    if(i2c->write(dbyte, data, ldata, false)) printf("no ack\n");
}

// -------------------------------------------------------------------------------------------------