STM3 ESC dual brushless motor controller. 10-60v, motor power rating tiny to kW. Ganged or independent motor control As used in 'The Brute' locomotive - www.jons-workshop.com

Dependencies:   mbed BufferedSerial Servo FastPWM

24LC64_eeprom.cpp

Committer:
JonFreeman
Date:
2018-03-01
Revision:
0:435bf84ce48a
Child:
1:0fabe6fdb55b

File content as of revision 0:435bf84ce48a:

#include "mbed.h"
//#include "rtos.h"
#include "BufferedSerial.h"
extern  BufferedSerial pc;
extern  I2C i2c;
    //  Code for 24LC64 eeprom
    //  Code based on earlier work using memory FM24W256, also at i2c address 0xa0;
 
const int addr_rd = 0xa1;  //  set bit 0 for read, clear bit 0 for write
const int addr_wr = 0xa0;  //  set bit 0 for read, clear bit 0 for write
const int ACK     = 1;

bool    ack_poll    ()  {   //  wait short while for any previous memory operation to complete
    const int poll_tries    = 40;
    int poll_count = 0;
    bool    i2cfree = false;
    while   (poll_count++ < poll_tries && !i2cfree)  {
        i2c.start   ();
        if  (i2c.write(addr_wr) == ACK)
            i2cfree = true;
        else
//            Thread::wait(1);   //   1ms
            wait   (1);
    }
//    pc.printf   ("ack_poll, count = %d, i2cfree = %s\r\n", poll_count, i2cfree ? "true" : "false");
    return  i2cfree;
}

bool    set_24LC64_internal_address (int    start_addr)   {
    if  (!ack_poll())
    {
        pc.printf   ("Err in set_24LC64_internal_address, no ACK writing device address byte\r\n");
        i2c.stop();
        return  false;
    }
    int err = 0;
    if  (i2c.write(start_addr >> 8)   != ACK) err++;
    if  (i2c.write(start_addr & 0xff) != ACK) err++;
    if  (err)   {
        pc.printf   ("In set_24LC64_internal_address, Believe Device present, failed in writing 2 mem addr bytes %d\r\n", err);
        i2c.stop();
        return  false;
    }
    return  true;
}

bool    wr_24LC64  (int start_addr, char * source, int length)   {
    int err = 0;
    if(length < 1 || length > 32)   {
        pc.printf   ("Length out of range %d in wr_24LC64\r\n", length);
        return  false;
    }
    if  (!set_24LC64_internal_address   (start_addr))   {
        pc.printf   ("In wr_24LC64, Believe Device present, failed in writing 2 mem addr bytes %d\r\n", err);
        return  false;
    }
    while(length--)
        err += ACK - i2c.write(*source++);
    i2c.stop();
    if  (err)   {
        pc.printf   ("in wr_24LC64, device thought good, mem addr write worked, failed writing string\r\n");
        return  false;
    }
    pc.printf   ("In wr_24LC64 No Errors Found!\r\n");
    return  true;
}

bool rd_24LC64  (int start_addr, char * dest, int length)   {
    int acknak = ACK;
    if(length < 1)
        return false;
    if  (!set_24LC64_internal_address   (start_addr))   {
        pc.printf   ("In rd_24LC64, failed to set_ramaddr\r\n");
        return  false;
    }
    i2c.start();
    if  (i2c.write(addr_rd) != ACK) {
        pc.printf   ("Errors in rd_24LC64 sending addr_rd\r\n");
        return  false;
    }
    while(length--) {
        if(length == 0)
            acknak = 0;
        *dest++ = i2c.read(acknak);
    }
    i2c.stop();
    return  true;
}

int check_24LC64   ()  {     //  Call from near top of main() to init i2c bus
    i2c.frequency(400000);      //  Speed 400000 max.
    int last_found = 0, q;      //  Note address bits 3-1 to match addr pins on device
    for (int i = 0; i < 255; i += 2)    {   //  Search for devices at all possible i2c addresses
        i2c.start();
        q = i2c.write(i);   //  may return error code 2 when no start issued
        switch  (q) {
            case    ACK:
                pc.printf   ("I2C device found at 0x%x\r\n", i);
                last_found = i;
            case    2:      //  Device not seen at this address
            break;
            default:
                pc.printf   ("Unknown error %d in check_24LC64\r\n", q);
            break;
        }
    }
    i2c.stop();
    return  last_found;
}