This is a class which contains function to interface with the MLX75320

Dependents:   MLX75320_API

LidarSpi.h

Committer:
TNU
Date:
2016-04-15
Revision:
11:bd2dee9957a9
Parent:
9:067f75510d67
Child:
12:d1767e2bd3a8

File content as of revision 11:bd2dee9957a9:

#ifndef LIDARSPI_H
#define LIDARSPI_H

#include "mbed.h"
#include "typeDef.h"
#include "FunctionPointer.h"



class LidarSpi
{
    public:
        
        // Type used to read/write register values. Here, it is 16 bits
        typedef uint16_t REGTYPE;
        typedef unsigned char uchar;
        
        static const uint16_t CRC_SZ   = 2;   // Bytes in SPI CRC field
        static const uint16_t SHORT_SZ = 8;   // Bytes in short SPI packet
        static const uint16_t LONG_SZ  = 300; // Bytes in long SPI packet
        static const uint16_t HDR_SZ   = 2;   // Bytes in SPI header field
    
        static const uint16_t MAXCH     = 16;  // Number of channels
        static const uint16_t MAXSET    = 2;   // Number of acquisition sets
        static const uint16_t MAXPTCNT  = 64;  // Max number of base points
        static const uint16_t MAXECH    = 64 ; // Max echo count
        static const uint16_t MAXACCLOG = 10;  // Max accumulation log2
        static const uint16_t MAXOVRLOG = 3;   // Max oversampling log2
        static const uint16_t MAXLED    = 16;  // Max LED power
        static const uint16_t MAXACC    = 1<<MAXACCLOG;
        static const uint16_t MAXOVR    = 1<<MAXOVRLOG;
        static const uint16_t MAXTRCLEN = 74*8*MAXCH*2;  // Max length of a trace in bytes (samples * MAXOVR * MAXCH * 2bytes)
        static const uint16_t BYTES_PER_ECH = 24; // Nb of bytes per echo
        static const uint16_t START_DELAY =10;
    
        // Used on header ADDR field to request all channels and as idx argument of GetTrace function
        static const uint16_t ADDR_ALLCH= 0x1F;
        
        struct Echo
        {
            uint32_t mDistance;
            uint32_t mAmplitude;
            uint32_t mBase;
            uint16_t mMaxIndex;
            uint8_t   mChannelIndex;
            uint8_t   mValid;
            uint32_t mAmplitudeLowScale;
            uint32_t mSaturationWidth;
        };
    
        /// \enum  eASICREG
        /// \brief Lists all registers. Enum value is directly the register address.
        ///        Naming convention: REG_ + <name> + <L/H> + <#>
        ///        <name> = name of register
        ///        <L/H>  = optional low or high register part
        ///        <#>    = optional number, either for acquisition source 0/1 or index
        typedef enum
        {
            REG_MEACL = 0x00,
            REG_MEACH,
            REG_CONTROL,
            REG_ACQCTL0,
            REG_ACQCTL1,
            REG_PTCNT,
            REG_SCANCTL,
            REG_TRIGCTL,
            REG_DELAY,
            REG_TMPSET,
            REG_TMPSHUT,
            REG_TMPIC,
            REG_TMPSRC,
            REG_GAIN0,
            REG_GAIN1,
            REG_GAIN2,
            REG_GAIN3,
            REG_CHANNEL,
            REG_PWMPERIOD0,
            REG_PWMPERIOD1,
            REG_PWMCOUNT,
            REG_PWMWIDTH0,
            REG_PWMWIDTH1   = REG_PWMWIDTH0 + 8,
            REG_FILTER0     = REG_PWMWIDTH1 + 8,
            REG_FILTER1     = REG_FILTER0   + 16,
            REG_THRNEARL0   = REG_FILTER1   + 16,
            REG_THRNEARH0,
            REG_THRMEDL0,
            REG_THRMEDH0,
            REG_THRFARL0,
            REG_THRFARH0,
            REG_THRNEARL1,
            REG_THRNEARH1,
            REG_THRMEDL1,
            REG_THRMEDH1,
            REG_THRFARL1,
            REG_THRFARH1,
            REG_NOISE0,
            REG_NOISE1,
            REG_OFFSETL0,
            REG_OFFSETH0 = REG_OFFSETL0 + 16,
            REG_OFFSETL1 = REG_OFFSETH0 + 16,
            REG_OFFSETH1 = REG_OFFSETL1 + 16,
            REG_SCALEL0  = REG_OFFSETH1 + 16,
            REG_SCALEH0,
            REG_SCALEL1,
            REG_SCALEH1,
            REG_FACTORY,
            REG_ASIL,
            REG_OTP0,
            REG_OTP1,
            REG_OTP2,
            REG_OTP3,
            REG_OTP4,
            REG_OTP5,
            REG_OTP6,
            REG_OTP7,
            REG_SOFTVER,
            REG_ASICVER,
            REG_WATCHDOGL,
            REG_WATCHDOGH,
            // Wai: Changed from REG_MAX
            REG_MAX    =   0xFFFF   // Number of registers
        } eASICREG;
    
        enum PackType
        {
            PACK_RREG = 0,      ///< Read register request
            PACK_WREG,          ///< Write register request
            PACK_RFIRM,         ///< Read firmware request
            PACK_WFIRM,         ///< Write firmware request
            PACK_STATUS_S,      ///< Status short
            PACK_STATUS_L,      ///< Status long
            PACK_RDATA_RESP_S,  ///< Read data response short
            PACK_RDATA_RESP_L,  ///< Read data response long
            PACK_WDATA_L,       ///< Write data long
        };
    
        enum StatusType
        {
            STAT_OK = 0,
            STAT_BUSY,
            STAT_CRC,
            STAT_INVALID_REQ,
            STAT_SEQ_NB,
            STAT_TIMEOUT,
        };
    
        enum FirmType
        {
            FW_OTP = 0,
            FW_PROCESSED,
            FW_RAW,
            FW_PATCH,
            FW_PRELOAD,
            FW_TEST,
        };
    
    
        LidarSpi(PinName mosi, PinName miso, PinName clk, PinName chipSelect, PinName dr, PinName rs, PinName tr, PinName smpl);
        int SpiSetting(long freq, int mode, Serial* pc);
        int TxPacket(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz);   
        int TxPacketWord(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz);   
        int TxPacket(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz, Serial* pc);   
        int TxPacketSlow(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz, uint16_t usDelay);
        int BasicRead();
        int BasicTransfer(uint8_t* rData, uint16_t rSz, uint8_t *wData, uint16_t wSz, const event_callback_t callback);
        int ReadReg   ( uint32_t reg, uint32_t *val);
        int ReadReg(uint32_t reg, uint32_t *val, Serial* pc);
        int WriteReg  ( uint32_t reg, uint32_t val);
        int WriteRegSpeed  ( uint32_t reg, uint32_t val, uint16_t usDelay);
        int WriteReg  ( uint32_t reg, uint32_t val, Serial* pc);
        int WriteRegSpeed  ( uint32_t reg, uint32_t val, uint16_t usDelay, Serial* pc);
        //int GetRegNfo ( uint16_t reg, const RegInfo **nfo);
        int GetEchoes ( Echo *ech, uint16_t maxN, uint16_t mode);
        int GetEchoes ( Echo *ech, uint16_t maxN, uint16_t mode, Serial* pc);
        int GetTrace  ( uint16_t *buf, uint16_t maxN, uint16_t nSam, uint16_t idx, Serial* pc);
        int GetTraceOne  ( uint16_t *buf, uint16_t maxN, uint16_t nSam, uint16_t idx,int index , Serial* pc);
        int LoadPatch (const char *patch, Serial *pc);
        int setTrace(void);
        int setEcho(void);
        int PrintAllReg (uint16_t * regs, uint32_t * val, uint16_t size);
        
        void Trigger(int level);
        //int SetConfig ( int configNum);
        //int SetAcqCfg ( uint16_t set, uint16_t led, uint16_t accLog, uint16_t ovrLog);
        //int Acquire   ( uchar ab, uchar evGain, uchar evCh, uchar odGain, uchar odCh);
        //int GetFrame  ( uint16_t *buf, uint16_t maxN, uint16_t nSam, uint16_t nFrm);
        
    
        
        
    private:
        SPI device;
        DigitalOut chipS;
        DigitalIn dataReady;
        DigitalOut resetPin;
        DigitalOut trigger;
        DigitalIn sampling;
        
        int parse_hex_line(char *theline, uint8_t bytes[], uint16_t *addr, uint16_t *num, uint16_t *code);
        int LoadPatchFragment(const char *patch, uint16_t *addrStart, uint16_t *startLine, uint16_t *nBytes, uint8_t *memory, Serial* pc);

          
};


#endif