Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
LidarSpi.h
00001 #ifndef LIDARSPI_H 00002 #define LIDARSPI_H 00003 00004 #include "mbed.h" 00005 #include "typeDef.h" 00006 #include "FunctionPointer.h" 00007 00008 /** LidarSpi class 00009 * This class defines methods and structs to interface with the MLX75320 lidar sensing product 00010 * 00011 */ 00012 00013 class LidarSpi 00014 { 00015 public: 00016 00017 // Type used to read/write register values. Here, it is 16 bits 00018 typedef uint16_t REGTYPE; 00019 typedef unsigned char uchar; 00020 00021 static const uint16_t CRC_SZ = 2; // Bytes in SPI CRC field 00022 static const uint16_t SHORT_SZ = 8; // Bytes in short SPI packet 00023 static const uint16_t LONG_SZ = 300; // Bytes in long SPI packet 00024 static const uint16_t HDR_SZ = 2; // Bytes in SPI header field 00025 00026 static const uint16_t MAXCH = 16; // Number of channels 00027 static const uint16_t MAXSET = 2; // Number of acquisition sets 00028 static const uint16_t MAXPTCNT = 64; // Max number of base points 00029 static const uint16_t MAXECH = 64 ; // Max echo count 00030 static const uint16_t MAXACCLOG = 10; // Max accumulation log2 00031 static const uint16_t MAXOVRLOG = 3; // Max oversampling log2 00032 static const uint16_t MAXLED = 16; // Max LED power 00033 static const uint16_t MAXACC = 1<<MAXACCLOG; 00034 static const uint16_t MAXOVR = 1<<MAXOVRLOG; 00035 static const uint16_t MAXTRCLEN = 74*8*MAXCH*2; // Max length of a trace in bytes (samples * MAXOVR * MAXCH * 2bytes) 00036 static const uint16_t BYTES_PER_ECH = 24; // Nb of bytes per echo 00037 static const uint16_t START_DELAY =10; 00038 00039 // Used on header ADDR field to request all channels and as idx argument of GetTrace function 00040 static const uint16_t ADDR_ALLCH= 0x1F; 00041 00042 00043 // Structure for the processed data returned by the sensor 00044 struct Echo 00045 { 00046 uint32_t mDistance; 00047 uint32_t mAmplitude; 00048 uint32_t mBase; 00049 uint16_t mMaxIndex; 00050 uint8_t mChannelIndex; 00051 uint8_t mValid; 00052 uint32_t mAmplitudeLowScale; 00053 uint32_t mSaturationWidth; 00054 }; 00055 00056 /// \enum eASICREG 00057 /// \brief Lists all registers. Enum value is directly the register address. 00058 /// Naming convention: REG_ + <name> + <L/H> + <#> 00059 /// <name> = name of register 00060 /// <L/H> = optional low or high register part 00061 /// <#> = optional number, either for acquisition source 0/1 or index 00062 typedef enum 00063 { 00064 REG_MEACL = 0x00, 00065 REG_MEACH, 00066 REG_CONTROL, 00067 REG_ACQCTL0, 00068 REG_ACQCTL1, 00069 REG_PTCNT, 00070 REG_SCANCTL, 00071 REG_TRIGCTL, 00072 REG_DELAY, 00073 REG_TMPSET, 00074 REG_TMPSHUT, 00075 REG_TMPIC, 00076 REG_TMPSRC, 00077 REG_GAIN0, 00078 REG_GAIN1, 00079 REG_GAIN2, 00080 REG_GAIN3, 00081 REG_CHANNEL, 00082 REG_PWMPERIOD0, 00083 REG_PWMPERIOD1, 00084 REG_PWMCOUNT, 00085 REG_PWMWIDTH0, 00086 REG_PWMWIDTH1 = REG_PWMWIDTH0 + 8, 00087 REG_FILTER0 = REG_PWMWIDTH1 + 8, 00088 REG_FILTER1 = REG_FILTER0 + 16, 00089 REG_THRNEARL0 = REG_FILTER1 + 16, 00090 REG_THRNEARH0, 00091 REG_THRMEDL0, 00092 REG_THRMEDH0, 00093 REG_THRFARL0, 00094 REG_THRFARH0, 00095 REG_THRNEARL1, 00096 REG_THRNEARH1, 00097 REG_THRMEDL1, 00098 REG_THRMEDH1, 00099 REG_THRFARL1, 00100 REG_THRFARH1, 00101 REG_NOISE0, 00102 REG_NOISE1, 00103 REG_OFFSETL0, 00104 REG_OFFSETH0 = REG_OFFSETL0 + 16, 00105 REG_OFFSETL1 = REG_OFFSETH0 + 16, 00106 REG_OFFSETH1 = REG_OFFSETL1 + 16, 00107 REG_SCALEL0 = REG_OFFSETH1 + 16, 00108 REG_SCALEH0, 00109 REG_SCALEL1, 00110 REG_SCALEH1, 00111 REG_FACTORY, 00112 REG_ASIL, 00113 REG_OTP0, 00114 REG_OTP1, 00115 REG_OTP2, 00116 REG_OTP3, 00117 REG_OTP4, 00118 REG_OTP5, 00119 REG_OTP6, 00120 REG_OTP7, 00121 REG_SOFTVER, 00122 REG_ASICVER, 00123 REG_WATCHDOGL, 00124 REG_WATCHDOGH, 00125 // Wai: Changed from REG_MAX 00126 REG_MAX = 0xFFFF // Number of registers 00127 } eASICREG; 00128 00129 //Internal type used by low level functions: SPI packet type 00130 enum PackType 00131 { 00132 PACK_RREG = 0, ///< Read register request 00133 PACK_WREG, ///< Write register request 00134 PACK_RFIRM, ///< Read firmware request 00135 PACK_WFIRM, ///< Write firmware request 00136 PACK_STATUS_S, ///< Status short 00137 PACK_STATUS_L, ///< Status long 00138 PACK_RDATA_RESP_S, ///< Read data response short 00139 PACK_RDATA_RESP_L, ///< Read data response long 00140 PACK_WDATA_L, ///< Write data long 00141 }; 00142 //Internal type used by low level functions: SPI status status type 00143 enum StatusType 00144 { 00145 STAT_OK = 0, 00146 STAT_BUSY, 00147 STAT_CRC, 00148 STAT_INVALID_REQ, 00149 STAT_SEQ_NB, 00150 STAT_TIMEOUT, 00151 }; 00152 //Internal type used by low level functions: SPI firmware packet type 00153 enum FirmType 00154 { 00155 FW_OTP = 0, 00156 FW_PROCESSED, 00157 FW_RAW, 00158 FW_PATCH, 00159 FW_PRELOAD, 00160 FW_TEST, 00161 }; 00162 00163 00164 00165 /*===================================================================================================================================== 00166 //===================================================================================================================================== 00167 _____ _ _ _ __ _ _ 00168 | __ \ | | | (_) / _| | | (_) 00169 | |__) | _| |__ | |_ ___ | |_ _ _ _ __ ___| |_ _ ___ _ __ ___ 00170 | ___/ | | | '_ \| | |/ __| | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __| 00171 | | | |_| | |_) | | | (__ | | | |_| | | | | (__| |_| | (_) | | | \__ \ 00172 |_| \__,_|_.__/|_|_|\___| |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ 00173 00174 //===================================================================================================================================== 00175 //====================================================================================================================================*/ 00176 00177 00178 /** Constructor 00179 * @param: pin names for mosi, miso, clk, chipselect, dataready, reset, trigger and sample*/ 00180 LidarSpi(PinName mosi, PinName miso, PinName clk, PinName chipSelect, PinName dr, PinName rs, PinName tr, PinName smpl); 00181 00182 /** SpiSetting 00183 * Change frequency and SPI mode(clock phase and polarity) 00184 * @param: frequency in Hz, mode ranging from 0 to 3, pointer to Serial interface for debugging 00185 * @return: 0 on success */ 00186 int SpiSetting(long freq, int mode, Serial* pc); 00187 00188 /** ReadReg 00189 * Read register at address 00190 * @param: register address, pointer to variable to register valueresult 00191 * @return: 0 on success */ 00192 int ReadReg ( uint32_t reg, uint32_t *val); 00193 00194 /** WriteReg 00195 * Write register to address 00196 * @param: register address, value to write 00197 * @return: 0 on success */ 00198 int WriteReg ( uint32_t reg, uint32_t val); 00199 00200 00201 /** GetEchoes 00202 * Perform measurement and read processed data 00203 * @param: pointer to Echo structure array to store measurement, structure array size (must be >=64) 00204 * @return: 0 on success */ 00205 int GetEchoes ( Echo *ech, uint16_t maxN); 00206 00207 /** GetEchoes 00208 * Perform measurement and read processed data 00209 * @param: pointer to Echo structure array to store measurement, structure array size (must be >=64), pointer to serial interface for debugging 00210 * @return: 0 on success */ 00211 int GetEchoes ( Echo *ech, uint16_t maxN, Serial* pc); 00212 00213 /** GetTrace 00214 * Perform measurement and read raw trace buffer 00215 * @param: pointer to int array to store trace buffer, structure array size (must be >=9472*2), pointer to serial interface for debugging 00216 * @return: 0 on success */ 00217 int GetTrace ( uint16_t *buf, uint16_t maxN, Serial* pc); 00218 00219 /** LoadPatch 00220 * Load patch stored on mbed Flash memory in MLX75320 and enable patch 00221 * @param: path of patch.hex location, , pointer to serial interface for debugging 00222 * @return: 0 on success */ 00223 int LoadPatch (const char *patch, Serial *pc); 00224 00225 /** SetTrace 00226 * Set a variety of registers according to the preset values for a raw data measurement 00227 * @return: 0 on success */ 00228 int setTrace(void); 00229 00230 /** SetTrace 00231 * Set a variety of registers according to the preset values for a processed data measurement 00232 * @return: 0 on success */ 00233 int setEcho(void); 00234 00235 /** SetLed 00236 * Set a variety of registers according to the preset values to either fire or don't fire the LEDs during a measurement 00237 * @param: True to enable LEDs, False to disable */ 00238 int setLed(bool state); 00239 00240 00241 /*===================================================================================================================================== 00242 //===================================================================================================================================== 00243 _____ _ _ _ _ _ __ _ _ 00244 |_ _| | | | | | | | | (_) / _| | | (_) 00245 | | _ __ | |_ ___ _ __ _ __ __ _| | ___ _ __ __| | ___| |__ _ _ __ _ __ _ _ _ __ __ _ | |_ _ _ _ __ ___| |_ _ ___ _ __ ___ 00246 | | | '_ \| __/ _ \ '__| '_ \ / _` | | / _ \| '__| / _` |/ _ \ '_ \| | | |/ _` |/ _` | | '_ \ / _` | | _| | | | '_ \ / __| __| |/ _ \| '_ \/ __| 00247 _| |_| | | | || __/ | | | | | (_| | | | (_) | | | (_| | __/ |_) | |_| | (_| | (_| | | | | | (_| | | | | |_| | | | | (__| |_| | (_) | | | \__ \ 00248 |_____|_| |_|\__\___|_| |_| |_|\__,_|_| \___/|_| \__,_|\___|_.__/ \__,_|\__, |\__, |_|_| |_|\__, | |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ 00249 __/ | __/ | __/ | 00250 |___/ |___/ |___/ 00251 //===================================================================================================================================== 00252 //====================================================================================================================================*/ 00253 00254 00255 00256 /** ReadReg (overloaded) 00257 * Read register at address 00258 * @param: register address, pointer to variable to register valueresult, pointer to serial interface for debugging 00259 * @return: 0 on success */ 00260 int ReadReg(uint32_t reg, uint32_t *val, Serial* pc); 00261 00262 /** WriteReg 00263 * Write register to address 00264 * @param: register address, value to write, pointer to serial interface for debugging 00265 * @return: 0 on success */ 00266 int WriteReg ( uint32_t reg, uint32_t val, Serial* pc); 00267 00268 /** GetTraceOne 00269 * Perform measurement and read raw trace buffer, this method only reads back a single SPI packet 00270 * @param: pointer to int array to store trace buffer, structure array size (must be >=9472*2), pointer to serial interface for debugging 00271 * @return: 0 on success */ 00272 int GetTraceOne ( uint16_t *buf, uint16_t maxN, uint16_t nSam, uint16_t idx,int index , Serial* pc); 00273 00274 /** Trigger 00275 * Change pin to high or low to use as trigger point for oscilloscope 00276 * @param: 1 or 0 to set voltage level */ 00277 void Trigger(int level); 00278 00279 00280 int PrintAllReg (uint16_t * regs, uint32_t * val, uint16_t size); 00281 00282 int TxPacket(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz); 00283 int TxPacketWord(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz); 00284 int TxPacket(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz, Serial* pc); 00285 int TxPacketSlow(uint8_t* rData, uint16_t *rSz, uint8_t *tData, uint16_t tSz, uint16_t usDelay); 00286 int BasicRead(); 00287 int BasicTransfer(uint8_t* rData, uint16_t rSz, uint8_t *wData, uint16_t wSz, const event_callback_t callback); 00288 int WriteRegSpeed ( uint32_t reg, uint32_t val, uint16_t usDelay); 00289 int WriteRegSpeed ( uint32_t reg, uint32_t val, uint16_t usDelay, Serial* pc); 00290 00291 //int SetConfig ( int configNum); 00292 //int SetAcqCfg ( uint16_t set, uint16_t led, uint16_t accLog, uint16_t ovrLog); 00293 //int Acquire ( uchar ab, uchar evGain, uchar evCh, uchar odGain, uchar odCh); 00294 //int GetFrame ( uint16_t *buf, uint16_t maxN, uint16_t nSam, uint16_t nFrm); 00295 00296 00297 00298 00299 private: 00300 SPI device; 00301 DigitalOut chipS; 00302 DigitalIn dataReady; 00303 DigitalOut resetPin; 00304 DigitalOut trigger; 00305 DigitalIn sampling; 00306 00307 int parse_hex_line(char *theline, uint8_t bytes[], uint16_t *addr, uint16_t *num, uint16_t *code); 00308 int LoadPatchFragment(const char *patch, uint16_t *addrStart, uint16_t *startLine, uint16_t *nBytes, uint8_t *memory, Serial* pc); 00309 00310 00311 }; 00312 00313 00314 #endif
Generated on Sun Jul 17 2022 06:16:31 by
1.7.2