Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BufferedSpi.h Source File

BufferedSpi.h

Go to the documentation of this file.
00001 
00002 /**
00003  * @file    BufferedSpi.h
00004  * @brief   Software Buffer - Extends mbed SPI functionallity
00005  * @author  Armelle Duboc
00006  * @version 1.0
00007  * @see
00008  *
00009  * Copyright (c) STMicroelectronics 2017
00010  *
00011  * Licensed under the Apache License, Version 2.0 (the "License");
00012  * you may not use this file except in compliance with the License.
00013  * You may obtain a copy of the License at
00014  *
00015  *     http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  * Unless required by applicable law or agreed to in writing, software
00018  * distributed under the License is distributed on an "AS IS" BASIS,
00019  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  * See the License for the specific language governing permissions and
00021  * limitations under the License.
00022  */
00023 
00024 #ifndef BUFFEREDSPI_H
00025 #define BUFFEREDSPI_H
00026  
00027 #include "mbed.h"
00028 #include "MyBuffer.h"
00029 
00030 /** A spi port (SPI) for communication with wifi device
00031  *
00032  * Can be used for Full Duplex communication, or Simplex by specifying
00033  * one pin as NC (Not Connected)
00034  *
00035  * Example:
00036  * @code
00037  *  #include "mbed.h"
00038  *  #include "BufferedSerial.h"
00039  *
00040  *  BufferedSerial pc(USBTX, USBRX);
00041  *
00042  *  int main()
00043  *  { 
00044  *      while(1)
00045  *      {
00046  *          Timer s;
00047  *        
00048  *          s.start();
00049  *          pc.printf("Hello World - buffered\n");
00050  *          int buffered_time = s.read_us();
00051  *          wait(0.1f); // give time for the buffer to empty
00052  *        
00053  *          s.reset();
00054  *          printf("Hello World - blocking\n");
00055  *          int polled_time = s.read_us();
00056  *          s.stop();
00057  *          wait(0.1f); // give time for the buffer to empty
00058  *        
00059  *          pc.printf("printf buffered took %d us\n", buffered_time);
00060  *          pc.printf("printf blocking took %d us\n", polled_time);
00061  *          wait(0.5f);
00062  *      }
00063  *  }
00064  * @endcode
00065  */
00066 
00067 /**
00068  *  @class BufferedSpi
00069  *  @brief Software buffers and interrupt driven tx and rx for Serial
00070  */  
00071 class BufferedSpi : public SPI 
00072 {
00073 private:
00074     DigitalOut    nss;
00075     MyBuffer <char>  _txbuf;
00076     uint32_t      _buf_size;
00077     uint32_t      _tx_multiple;
00078     volatile int _timeout;
00079     void txIrq(void);
00080     void prime(void);
00081 
00082     InterruptIn* _datareadyInt;
00083     volatile int _cmddata_rdy_rising_event;
00084     void DatareadyRising(void);
00085     int wait_cmddata_rdy_rising_event(void);
00086     int wait_cmddata_rdy_high(void);
00087 
00088 
00089     Callback<void()> _cbs[2];
00090 
00091     Callback<void()> _sigio_cb;
00092     uint8_t          _sigio_event;
00093 
00094 public:
00095     MyBuffer <char>  _rxbuf;
00096     DigitalIn dataready;
00097     enum IrqType {
00098         RxIrq = 0,
00099         TxIrq,
00100 
00101         IrqCnt
00102     };
00103 
00104     /** Create a BufferedSpi Port, connected to the specified transmit and receive pins
00105      *  @param SPI mosi pin
00106      *  @param SPI miso pin
00107      *  @param SPI sclk pin
00108      *  @param SPI nss pin
00109      *  @param Dataready pin
00110      *  @param buf_size printf() buffer size
00111      *  @param tx_multiple amount of max printf() present in the internal ring buffer at one time
00112      *  @param name optional name
00113     */
00114     BufferedSpi(PinName mosi, PinName miso, PinName sclk, PinName nss, PinName datareadypin, uint32_t buf_size = 1480, uint32_t tx_multiple = 4,const char* name=NULL);
00115     
00116     /** Destroy a BufferedSpi Port
00117      */
00118     virtual ~BufferedSpi(void);
00119     
00120     /** call to SPI frequency Function
00121      */
00122     virtual void frequency(int hz);
00123     
00124     /** clear the transmit buffer
00125      */
00126     virtual void flush_txbuf(void);
00127     
00128     /** call to SPI format function 
00129      */
00130     virtual void format(int bits, int mode);
00131     
00132     virtual void enable_nss(void);
00133     
00134     virtual void disable_nss(void);
00135     
00136     /** Check on how many bytes are in the rx buffer
00137      *  @return 1 if something exists, 0 otherwise
00138      */
00139     virtual int readable(void);
00140     
00141     /** Check to see if the tx buffer has room
00142      *  @return 1 always has room and can overwrite previous content if too small / slow
00143      */
00144     virtual int writeable(void);
00145     
00146     /** Get a single byte from the BufferedSpi Port.
00147      *  Should check readable() before calling this.
00148      *  @return A byte that came in on the SPI Port
00149      */
00150     virtual int getc(void);
00151     
00152     /** Write a single byte to the BufferedSpi Port.
00153      *  @param c The byte to write to the SPI Port
00154      *  @return The byte that was written to the SPI Port Buffer
00155      */
00156     virtual int putc(int c);
00157     
00158     /** Write a string to the BufferedSpi Port. Must be NULL terminated
00159      *  @param s The string to write to the Spi Port
00160      *  @return The number of bytes written to the Spi Port Buffer
00161      */
00162     virtual int puts(const char *s);
00163     
00164     /** Write a formatted string to the BufferedSpi Port.
00165      *  @param format The string + format specifiers to write to the Spi Port
00166      *  @return The number of bytes written to the Spi Port Buffer
00167      */
00168     virtual int printf(const char* format, ...);
00169     
00170     /** Write data to the Buffered Spi Port
00171      *  @param s A pointer to data to send
00172      *  @param length The amount of data being pointed to
00173      *  @return The number of bytes written to the Spi Port Buffer
00174      */
00175     virtual ssize_t buffwrite(const void *s, std::size_t length);
00176 
00177     /** Send datas to the Spi port that are already present
00178      *  in the internal _txbuffer
00179      *  @param length
00180      *  @return the number of bytes written on the SPI port
00181      */
00182     virtual ssize_t buffsend(size_t length);
00183 
00184     /** Read data from the Spi Port to the _rxbuf
00185      *  @param max: optional. = max sieze of the input read
00186      *  @return The number of bytes read from the SPI port and written to the _rxbuf
00187      */
00188     virtual ssize_t read();
00189     virtual ssize_t read(uint32_t max);
00190 
00191     /**
00192     * Allows timeout to be changed between commands
00193     *
00194     * @param timeout timeout of the connection
00195     */
00196     void setTimeout(int timeout)
00197     {
00198         /*  this is a safe guard timeout in case module is stuck
00199          *  so take 5 sec margin compared to module timeout, to
00200          *  really only detect case where module is stuck */
00201         _timeout = timeout + 5000;
00202     }
00203 
00204     /** Register a callback once any data is ready for sockets
00205      *  @param func     Function to call on state change
00206      */
00207     virtual void sigio(Callback<void()> func);
00208     
00209     /** Attach a function to call whenever a serial interrupt is generated
00210      *  @param func A pointer to a void function, or 0 to set as none
00211      *  @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
00212      */
00213      virtual void attach(Callback<void()> func, IrqType type=RxIrq);
00214      
00215     /** Attach a member function to call whenever a serial interrupt is generated
00216      *  @param obj pointer to the object to call the member function on
00217      *  @param method pointer to the member function to call
00218      *  @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
00219      */
00220     template <typename T>
00221     void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
00222         attach(Callback<void()>(obj, method), type);
00223     }
00224 
00225     /** Attach a member function to call whenever a serial interrupt is generated
00226      *  @param obj pointer to the object to call the member function on
00227      *  @param method pointer to the member function to call
00228      *  @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
00229      */
00230     template <typename T>
00231     void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
00232         attach(Callback<void()>(obj, method), type);
00233     }
00234 };
00235 #endif