Generic Pelion Device Management example for various Advantech modules.

This example is known to work great on the following platforms:

Example Functionality

This example showcases the following device functionality:

  • On timer button increment, simulate Pelion LWM2M button resource change

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common
cd pelion-example-common

2. Download your developer certificate from pelion portal

3. Compile the program

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

4. Copy the binary file pelion-example-common.bin to your mbed device.

Committer:
chuanga
Date:
Tue Mar 12 13:48:39 2019 +0800
Revision:
0:43ff9e3bc244
copying sources from github repository

Who changed what in which revision?

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