eeprom adding

Fork of SEEED_CAN by Sophie Dexter

seeed_can.h

Committer:
Just4pLeisure
Date:
2013-11-05
Revision:
0:f5d099885d3d
Child:
1:ad71faa09868

File content as of revision 0:f5d099885d3d:

/* seeed_can.h
 * Copyright (c) 2013 Sophie Dexter
 *
 * 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.
 */
#ifndef _SEEED_CAN_H_
#define _SEEED_CAN_H_

#include "seeed_can_api.h"

// if print debug information
#define DEBUG

/** CANMessage class
 */
class SEEED_CANMessage : public CAN_Message
{

public:
    /** Creates empty CAN message.
     */
    SEEED_CANMessage() {
        id     = 0;
        memset(data, 0, 8);
        len    = 8;
        type   = CANData;
        format = CANStandard;
    }

    /** Creates CAN message with specific content.
     */
    SEEED_CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
        id     = _id;
        memcpy(data, _data, _len);
        len    = _len & 0xF;
        type   = _type;
        format = _format;
    }

    /** Creates CAN remote message.
     */
    SEEED_CANMessage(int _id, CANFormat _format = CANStandard) {
        id     = _id;
        memset(data, 0, 8);
        len    = 0;
        type   = CANRemote;
        format = _format;
    }
};


/** A can bus client, used for communicating with Seeed Studios' CAN-BUS Arduino Shield.
 */
class SEEED_CAN
{
    /** can operator functions
     */
public:
    /** Create a SEEED_CAN interface, connected to the specified pins
     *
     * The Seeed Studio CAN-BUS shield is an Arduino compatible shield and connects to the FRDM-KL25Z SPI0 interface using pins PTD2 (mosi) PTD3 (miso) PTD1 (clk). The Active low chip select normally connects to the FRDM-KL25Z's PTD0 pin, but there is an option on the Seeed Studio CAN-BUS shield to connect to the PTD5 pin. The CAN-BUS shield uses the FRDM-KL25Z's PTD4 pin for its (active low) interrupt capability.
     *
     * @param ncs Active low chip select (This function accepts mbed (PTD0/5) and Seeed Studios' (CS/IO9) pin names. The default value would be PTD0 or CS but if you change the link on the Seeed Studios CAN-BUS shield you should use a volue of PTD5 or IO9 instead).
     * @param irq Active low interrupt pin (default SEEED_CAN_IRQ is FRDM-KL25Z PTD4 pin).
     * @param mosi SPI Master Out, Slave In pin  (default SEEED_CAN_MOSI is FRDM-KL25Z PTD2 pin).
     * @param miso SPI Master In, Slave Out pin  (default SEEED_CAN_MISO is FRDM-KL25Z PTD3 pin).
     * @param clk SPI Clock pin (default SEEED_CAN_MISO is FRDM-KL25Z PTD1 pin).
     * @param spiBitrate SPI Clock frequency (default: 1 MHz).
     * @param canBitrate CAN Bus Clock frequency (default: 100 kHz).
     */
    SEEED_CAN(PinName ncs, PinName irq=SEEED_CAN_IRQ, PinName mosi=SEEED_CAN_MOSI, PinName miso=SEEED_CAN_MISO, PinName clk=SEEED_CAN_CLK, int spiBitrate=1000000, int canBitrate=100000);
//    virtual ~SEEED_CAN(); // !!! Need a de-constructor for the interrrupt pin !!!

    /** Set the frequency of the CAN interface
     *
     *  @param hz The bus frequency in Hertz
     *
     *  @returns
     *    1 if successful,
     *    0 otherwise
     */
    int frequency(int setBitRate);

    /** Write a CANMessage to the bus.
     *
     *  @param msg The CANMessage to write.
     *
     *  @returns
     *    0 if write failed,
     *    1 if write was successful
     */
    int write(SEEED_CANMessage msg);

    /** Read a CANMessage from the bus.
     *
     *  @param msg A CANMessage to read to.
     *
     *  @returns
     *    0 if no message arrived,
     *    1 if message arrived
     */
    int read(SEEED_CANMessage &msg);

    /** Configure one of the Accpetance Masks (0 or 1)
     *
     *  @param maskNum The number of the Acceptance Mask to configure (Acceptance Mask 0 is associated with Filters 0 and 1, Acceptance Mask 1 is associated with Filters 2 through 5).
     *  @param canId CAN Id Mask bits (Acceptance Filters are only compared against bits that are set to '1' in an Acceptance Mask (e.g. mask 0x07F0 and filter 0x03F0 would allow through messages with CAN Id's 0x03F0 through 0x03FF because the 4 LSBs of the CAN Id are not filtered).
     *  @param format Describes if the Acceptance Mask is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard).
     *
     *  @returns
     *    0 if the Acceptance Mask could not be set
     *    1 if Acceptance Mask was set
     */
    int Mask(int maskNum, int canId, CANFormat format = CANStandard);

    /** Configure one of the Acceptance Filters (0 through 5)
     *
     *  @param filterNum The number of the Acceptance Filter to configure (Acceptance Filters 0 and 1 are associated with Mask 0, Acceptance Filters 2 through 5 are associated with Mask 1).
     *  @param canId CAN Id Filter bits (Acceptance Filters are only compared against bits that are set to '1' in an Acceptance Mask (e.g. mask 0x07F0 and filter 0x03F0 would allow through messages with CAN Id's 0x03F0 through 0x03FF because the 4 LSBs of the CAN Id are not filtered).
     *  @param format Describes if the Acceptance Filter is for a standard (CANStandard) or extended (CANExtended) CAN message frame format (default: CANStandard).
     *
     *  @returns
     *    0 if the Acceptance Filter could not be set
     *    1 if Acceptance Filter was set
     */
    int Filter(int filterNum, int canId, CANFormat format = CANStandard);

    /** Returns number of message reception (read) errors to detect read overflow errors.
     */
    unsigned char rderror(void);

    /** Returns number of message transmission (write) errors to detect write overflow errors.
     */
    unsigned char tderror(void);

    /** Check if any type of error
     *
     *  @returns
     *    0 if no errors
     *    1 if any type of error has been detected
     */
    int errors(void);

    /** Puts or removes the Seeed Studios CAN-BUS shield into or from silent monitoring mode
     *
     *  @param silent boolean indicating whether to go into silent mode or not
     */
    void monitor(bool silent);

    enum Mode {
        Normal = 0,
        Sleep,
        Loopback,
        Monitor,
        Config,
        Reset
    };

    /** Change CAN operation to the specified mode
     *
     *  @param mode The new operation mode (SEED_CAN::Normal, SEED_CAN::Sleep, SEED_CAN::Loopback, SEED_CAN::Monitor, SEEED_CAN::Reset)
     *
     *  @returns
     *    0 if mode change failed or unsupported,
     *    1 if mode change was successful
     */
    int mode(Mode mode);

    enum IrqType {
        RxIrq = 0,
        TxIrq,
        EwIrq,
        DoIrq,
        WuIrq,
        EpIrq,
        AlIrq,
        BeIrq,
        IdIrq
    };

    /** Attach a function to call whenever a CAN frame received interrupt is
     *  generated.
     *
     *  @param fptr A pointer to a void function, or 0 to set as none
     *  @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
     */
    void attach(void (*fptr)(void), IrqType type=RxIrq);

    /** Attach a member function to call whenever a CAN frame received interrupt
     *  is generated.
     *
     *  @param tptr pointer to the object to call the member function on
     *  @param mptr pointer to the member function to be called
     *  @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
     */
    template<typename T>
    void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
        _can.irq.fall(tptr, mptr);
/*        if((mptr != NULL) && (tptr != NULL)) {
            _irq[type].attach(tptr, mptr);
            can_irq_set(&_can, (CanIrqType)type, 1);
        } else {
            can_irq_set(&_can, (CanIrqType)type, 0);
        }*/
    }

protected:
    SPI     _spi;
    can_t   _can;

};

#endif      // SEEED_CAN_H