wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Committer:
JMF
Date:
Wed Sep 05 14:28:24 2018 +0000
Revision:
0:24d3eb812fd4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:24d3eb812fd4 1
JMF 0:24d3eb812fd4 2 /**
JMF 0:24d3eb812fd4 3 * @file BufferedSpi.h
JMF 0:24d3eb812fd4 4 * @brief Software Buffer - Extends mbed SPI functionallity
JMF 0:24d3eb812fd4 5 * @author Armelle Duboc
JMF 0:24d3eb812fd4 6 * @version 1.0
JMF 0:24d3eb812fd4 7 * @see
JMF 0:24d3eb812fd4 8 *
JMF 0:24d3eb812fd4 9 * Copyright (c) STMicroelectronics 2017
JMF 0:24d3eb812fd4 10 *
JMF 0:24d3eb812fd4 11 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:24d3eb812fd4 12 * you may not use this file except in compliance with the License.
JMF 0:24d3eb812fd4 13 * You may obtain a copy of the License at
JMF 0:24d3eb812fd4 14 *
JMF 0:24d3eb812fd4 15 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:24d3eb812fd4 16 *
JMF 0:24d3eb812fd4 17 * Unless required by applicable law or agreed to in writing, software
JMF 0:24d3eb812fd4 18 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:24d3eb812fd4 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:24d3eb812fd4 20 * See the License for the specific language governing permissions and
JMF 0:24d3eb812fd4 21 * limitations under the License.
JMF 0:24d3eb812fd4 22 */
JMF 0:24d3eb812fd4 23
JMF 0:24d3eb812fd4 24 #ifndef BUFFEREDSPI_H
JMF 0:24d3eb812fd4 25 #define BUFFEREDSPI_H
JMF 0:24d3eb812fd4 26
JMF 0:24d3eb812fd4 27 #include "mbed.h"
JMF 0:24d3eb812fd4 28 #include "MyBuffer.h"
JMF 0:24d3eb812fd4 29
JMF 0:24d3eb812fd4 30 /** A spi port (SPI) for communication with wifi device
JMF 0:24d3eb812fd4 31 *
JMF 0:24d3eb812fd4 32 * Can be used for Full Duplex communication, or Simplex by specifying
JMF 0:24d3eb812fd4 33 * one pin as NC (Not Connected)
JMF 0:24d3eb812fd4 34 *
JMF 0:24d3eb812fd4 35 * Example:
JMF 0:24d3eb812fd4 36 * @code
JMF 0:24d3eb812fd4 37 * #include "mbed.h"
JMF 0:24d3eb812fd4 38 * #include "BufferedSerial.h"
JMF 0:24d3eb812fd4 39 *
JMF 0:24d3eb812fd4 40 * BufferedSerial pc(USBTX, USBRX);
JMF 0:24d3eb812fd4 41 *
JMF 0:24d3eb812fd4 42 * int main()
JMF 0:24d3eb812fd4 43 * {
JMF 0:24d3eb812fd4 44 * while(1)
JMF 0:24d3eb812fd4 45 * {
JMF 0:24d3eb812fd4 46 * Timer s;
JMF 0:24d3eb812fd4 47 *
JMF 0:24d3eb812fd4 48 * s.start();
JMF 0:24d3eb812fd4 49 * pc.printf("Hello World - buffered\n");
JMF 0:24d3eb812fd4 50 * int buffered_time = s.read_us();
JMF 0:24d3eb812fd4 51 * wait(0.1f); // give time for the buffer to empty
JMF 0:24d3eb812fd4 52 *
JMF 0:24d3eb812fd4 53 * s.reset();
JMF 0:24d3eb812fd4 54 * printf("Hello World - blocking\n");
JMF 0:24d3eb812fd4 55 * int polled_time = s.read_us();
JMF 0:24d3eb812fd4 56 * s.stop();
JMF 0:24d3eb812fd4 57 * wait(0.1f); // give time for the buffer to empty
JMF 0:24d3eb812fd4 58 *
JMF 0:24d3eb812fd4 59 * pc.printf("printf buffered took %d us\n", buffered_time);
JMF 0:24d3eb812fd4 60 * pc.printf("printf blocking took %d us\n", polled_time);
JMF 0:24d3eb812fd4 61 * wait(0.5f);
JMF 0:24d3eb812fd4 62 * }
JMF 0:24d3eb812fd4 63 * }
JMF 0:24d3eb812fd4 64 * @endcode
JMF 0:24d3eb812fd4 65 */
JMF 0:24d3eb812fd4 66
JMF 0:24d3eb812fd4 67 /**
JMF 0:24d3eb812fd4 68 * @class BufferedSpi
JMF 0:24d3eb812fd4 69 * @brief Software buffers and interrupt driven tx and rx for Serial
JMF 0:24d3eb812fd4 70 */
JMF 0:24d3eb812fd4 71 class BufferedSpi : public SPI {
JMF 0:24d3eb812fd4 72 private:
JMF 0:24d3eb812fd4 73 DigitalOut nss;
JMF 0:24d3eb812fd4 74 MyBuffer <char> _txbuf;
JMF 0:24d3eb812fd4 75 uint32_t _buf_size;
JMF 0:24d3eb812fd4 76 uint32_t _tx_multiple;
JMF 0:24d3eb812fd4 77 volatile int _timeout;
JMF 0:24d3eb812fd4 78 void txIrq(void);
JMF 0:24d3eb812fd4 79 void prime(void);
JMF 0:24d3eb812fd4 80
JMF 0:24d3eb812fd4 81 InterruptIn *_datareadyInt;
JMF 0:24d3eb812fd4 82 volatile int _cmddata_rdy_rising_event;
JMF 0:24d3eb812fd4 83 void DatareadyRising(void);
JMF 0:24d3eb812fd4 84 int wait_cmddata_rdy_rising_event(void);
JMF 0:24d3eb812fd4 85 int wait_cmddata_rdy_high(void);
JMF 0:24d3eb812fd4 86
JMF 0:24d3eb812fd4 87
JMF 0:24d3eb812fd4 88 Callback<void()> _cbs[2];
JMF 0:24d3eb812fd4 89
JMF 0:24d3eb812fd4 90 Callback<void()> _sigio_cb;
JMF 0:24d3eb812fd4 91 uint8_t _sigio_event;
JMF 0:24d3eb812fd4 92
JMF 0:24d3eb812fd4 93 public:
JMF 0:24d3eb812fd4 94 MyBuffer <char> _rxbuf;
JMF 0:24d3eb812fd4 95 DigitalIn dataready;
JMF 0:24d3eb812fd4 96 enum IrqType {
JMF 0:24d3eb812fd4 97 RxIrq = 0,
JMF 0:24d3eb812fd4 98 TxIrq,
JMF 0:24d3eb812fd4 99
JMF 0:24d3eb812fd4 100 IrqCnt
JMF 0:24d3eb812fd4 101 };
JMF 0:24d3eb812fd4 102
JMF 0:24d3eb812fd4 103 /** Create a BufferedSpi Port, connected to the specified transmit and receive pins
JMF 0:24d3eb812fd4 104 * @param SPI mosi pin
JMF 0:24d3eb812fd4 105 * @param SPI miso pin
JMF 0:24d3eb812fd4 106 * @param SPI sclk pin
JMF 0:24d3eb812fd4 107 * @param SPI nss pin
JMF 0:24d3eb812fd4 108 * @param Dataready pin
JMF 0:24d3eb812fd4 109 * @param buf_size printf() buffer size
JMF 0:24d3eb812fd4 110 * @param tx_multiple amount of max printf() present in the internal ring buffer at one time
JMF 0:24d3eb812fd4 111 * @param name optional name
JMF 0:24d3eb812fd4 112 */
JMF 0:24d3eb812fd4 113 BufferedSpi(PinName mosi, PinName miso, PinName sclk, PinName nss, PinName datareadypin, uint32_t buf_size = 2500, uint32_t tx_multiple = 1, const char *name = NULL);
JMF 0:24d3eb812fd4 114
JMF 0:24d3eb812fd4 115 /** Destroy a BufferedSpi Port
JMF 0:24d3eb812fd4 116 */
JMF 0:24d3eb812fd4 117 virtual ~BufferedSpi(void);
JMF 0:24d3eb812fd4 118
JMF 0:24d3eb812fd4 119 /** call to SPI frequency Function
JMF 0:24d3eb812fd4 120 */
JMF 0:24d3eb812fd4 121 virtual void frequency(int hz);
JMF 0:24d3eb812fd4 122
JMF 0:24d3eb812fd4 123 /** clear the transmit buffer
JMF 0:24d3eb812fd4 124 */
JMF 0:24d3eb812fd4 125 virtual void flush_txbuf(void);
JMF 0:24d3eb812fd4 126
JMF 0:24d3eb812fd4 127 /** call to SPI format function
JMF 0:24d3eb812fd4 128 */
JMF 0:24d3eb812fd4 129 virtual void format(int bits, int mode);
JMF 0:24d3eb812fd4 130
JMF 0:24d3eb812fd4 131 virtual void enable_nss(void);
JMF 0:24d3eb812fd4 132
JMF 0:24d3eb812fd4 133 virtual void disable_nss(void);
JMF 0:24d3eb812fd4 134
JMF 0:24d3eb812fd4 135 /** Check on how many bytes are in the rx buffer
JMF 0:24d3eb812fd4 136 * @return 1 if something exists, 0 otherwise
JMF 0:24d3eb812fd4 137 */
JMF 0:24d3eb812fd4 138 virtual int readable(void);
JMF 0:24d3eb812fd4 139
JMF 0:24d3eb812fd4 140 /** Check to see if the tx buffer has room
JMF 0:24d3eb812fd4 141 * @return 1 always has room and can overwrite previous content if too small / slow
JMF 0:24d3eb812fd4 142 */
JMF 0:24d3eb812fd4 143 virtual int writeable(void);
JMF 0:24d3eb812fd4 144
JMF 0:24d3eb812fd4 145 /** Get a single byte from the BufferedSpi Port.
JMF 0:24d3eb812fd4 146 * Should check readable() before calling this.
JMF 0:24d3eb812fd4 147 * @return A byte that came in on the SPI Port
JMF 0:24d3eb812fd4 148 */
JMF 0:24d3eb812fd4 149 virtual int getc(void);
JMF 0:24d3eb812fd4 150
JMF 0:24d3eb812fd4 151 /** Write a single byte to the BufferedSpi Port.
JMF 0:24d3eb812fd4 152 * @param c The byte to write to the SPI Port
JMF 0:24d3eb812fd4 153 * @return The byte that was written to the SPI Port Buffer
JMF 0:24d3eb812fd4 154 */
JMF 0:24d3eb812fd4 155 virtual int putc(int c);
JMF 0:24d3eb812fd4 156
JMF 0:24d3eb812fd4 157 /** Write a string to the BufferedSpi Port. Must be NULL terminated
JMF 0:24d3eb812fd4 158 * @param s The string to write to the Spi Port
JMF 0:24d3eb812fd4 159 * @return The number of bytes written to the Spi Port Buffer
JMF 0:24d3eb812fd4 160 */
JMF 0:24d3eb812fd4 161 virtual int puts(const char *s);
JMF 0:24d3eb812fd4 162
JMF 0:24d3eb812fd4 163 /** Write a formatted string to the BufferedSpi Port.
JMF 0:24d3eb812fd4 164 * @param format The string + format specifiers to write to the Spi Port
JMF 0:24d3eb812fd4 165 * @return The number of bytes written to the Spi Port Buffer
JMF 0:24d3eb812fd4 166 */
JMF 0:24d3eb812fd4 167 virtual int printf(const char *format, ...);
JMF 0:24d3eb812fd4 168
JMF 0:24d3eb812fd4 169 /** Write data to the Buffered Spi Port
JMF 0:24d3eb812fd4 170 * @param s A pointer to data to send
JMF 0:24d3eb812fd4 171 * @param length The amount of data being pointed to
JMF 0:24d3eb812fd4 172 * @return The number of bytes written to the Spi Port Buffer
JMF 0:24d3eb812fd4 173 */
JMF 0:24d3eb812fd4 174 virtual ssize_t buffwrite(const void *s, std::size_t length);
JMF 0:24d3eb812fd4 175
JMF 0:24d3eb812fd4 176 /** Send datas to the Spi port that are already present
JMF 0:24d3eb812fd4 177 * in the internal _txbuffer
JMF 0:24d3eb812fd4 178 * @param length
JMF 0:24d3eb812fd4 179 * @return the number of bytes written on the SPI port
JMF 0:24d3eb812fd4 180 */
JMF 0:24d3eb812fd4 181 virtual ssize_t buffsend(size_t length);
JMF 0:24d3eb812fd4 182
JMF 0:24d3eb812fd4 183 /** Read data from the Spi Port to the _rxbuf
JMF 0:24d3eb812fd4 184 * @param max: optional. = max sieze of the input read
JMF 0:24d3eb812fd4 185 * @return The number of bytes read from the SPI port and written to the _rxbuf
JMF 0:24d3eb812fd4 186 */
JMF 0:24d3eb812fd4 187 virtual ssize_t read();
JMF 0:24d3eb812fd4 188 virtual ssize_t read(uint32_t max);
JMF 0:24d3eb812fd4 189
JMF 0:24d3eb812fd4 190 /**
JMF 0:24d3eb812fd4 191 * Allows timeout to be changed between commands
JMF 0:24d3eb812fd4 192 *
JMF 0:24d3eb812fd4 193 * @param timeout timeout of the connection in ms
JMF 0:24d3eb812fd4 194 */
JMF 0:24d3eb812fd4 195 void setTimeout(int timeout)
JMF 0:24d3eb812fd4 196 {
JMF 0:24d3eb812fd4 197 /* this is a safe guard timeout at SPI level in case module is stuck */
JMF 0:24d3eb812fd4 198 _timeout = timeout;
JMF 0:24d3eb812fd4 199 }
JMF 0:24d3eb812fd4 200
JMF 0:24d3eb812fd4 201 /** Register a callback once any data is ready for sockets
JMF 0:24d3eb812fd4 202 * @param func Function to call on state change
JMF 0:24d3eb812fd4 203 */
JMF 0:24d3eb812fd4 204 virtual void sigio(Callback<void()> func);
JMF 0:24d3eb812fd4 205
JMF 0:24d3eb812fd4 206 /** Attach a function to call whenever a serial interrupt is generated
JMF 0:24d3eb812fd4 207 * @param func A pointer to a void function, or 0 to set as none
JMF 0:24d3eb812fd4 208 * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
JMF 0:24d3eb812fd4 209 */
JMF 0:24d3eb812fd4 210 virtual void attach(Callback<void()> func, IrqType type = RxIrq);
JMF 0:24d3eb812fd4 211
JMF 0:24d3eb812fd4 212 /** Attach a member function to call whenever a serial interrupt is generated
JMF 0:24d3eb812fd4 213 * @param obj pointer to the object to call the member function on
JMF 0:24d3eb812fd4 214 * @param method pointer to the member function to call
JMF 0:24d3eb812fd4 215 * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
JMF 0:24d3eb812fd4 216 */
JMF 0:24d3eb812fd4 217 template <typename T>
JMF 0:24d3eb812fd4 218 void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
JMF 0:24d3eb812fd4 219 {
JMF 0:24d3eb812fd4 220 attach(Callback<void()>(obj, method), type);
JMF 0:24d3eb812fd4 221 }
JMF 0:24d3eb812fd4 222
JMF 0:24d3eb812fd4 223 /** Attach a member function to call whenever a serial interrupt is generated
JMF 0:24d3eb812fd4 224 * @param obj pointer to the object to call the member function on
JMF 0:24d3eb812fd4 225 * @param method pointer to the member function to call
JMF 0:24d3eb812fd4 226 * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
JMF 0:24d3eb812fd4 227 */
JMF 0:24d3eb812fd4 228 template <typename T>
JMF 0:24d3eb812fd4 229 void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
JMF 0:24d3eb812fd4 230 {
JMF 0:24d3eb812fd4 231 attach(Callback<void()>(obj, method), type);
JMF 0:24d3eb812fd4 232 }
JMF 0:24d3eb812fd4 233 };
JMF 0:24d3eb812fd4 234 #endif