![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code for Technion Formula car sensors reader
Dependencies: mbed Buffer FATFileSystem
Fork of SX1272PingPong by
This is code is part of a Technion course project in advanced IoT, implementing a device to read and transmit sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.
How to install
- Create an account on Mbed: https://os.mbed.com/account/signup/
- Import project into Compiler
- In the Program Workspace select "Formula_Nucleo_Reader"
- Select a Platform like so:
- Click button at top-left
- Add Board
- Search "NUCLEO F103RB" and then "Add to your Mbed Compiler"
- Finally click "Compile", if the build was successful, the binary would download automatically
- To install it on device simply plug it in to a PC, open device drive and drag then drop binary file in it
BufferedSerial/BufferedSerial.h@15:2e0d977dbb31, 2018-05-17 (annotated)
- Committer:
- wardm
- Date:
- Thu May 17 20:37:41 2018 +0000
- Revision:
- 15:2e0d977dbb31
V1.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wardm | 15:2e0d977dbb31 | 1 | |
wardm | 15:2e0d977dbb31 | 2 | /** |
wardm | 15:2e0d977dbb31 | 3 | * @file BufferedSerial.h |
wardm | 15:2e0d977dbb31 | 4 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
wardm | 15:2e0d977dbb31 | 5 | * @author sam grove |
wardm | 15:2e0d977dbb31 | 6 | * @version 1.0 |
wardm | 15:2e0d977dbb31 | 7 | * @see |
wardm | 15:2e0d977dbb31 | 8 | * |
wardm | 15:2e0d977dbb31 | 9 | * Copyright (c) 2013 |
wardm | 15:2e0d977dbb31 | 10 | * |
wardm | 15:2e0d977dbb31 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
wardm | 15:2e0d977dbb31 | 12 | * you may not use this file except in compliance with the License. |
wardm | 15:2e0d977dbb31 | 13 | * You may obtain a copy of the License at |
wardm | 15:2e0d977dbb31 | 14 | * |
wardm | 15:2e0d977dbb31 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
wardm | 15:2e0d977dbb31 | 16 | * |
wardm | 15:2e0d977dbb31 | 17 | * Unless required by applicable law or agreed to in writing, software |
wardm | 15:2e0d977dbb31 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
wardm | 15:2e0d977dbb31 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
wardm | 15:2e0d977dbb31 | 20 | * See the License for the specific language governing permissions and |
wardm | 15:2e0d977dbb31 | 21 | * limitations under the License. |
wardm | 15:2e0d977dbb31 | 22 | */ |
wardm | 15:2e0d977dbb31 | 23 | |
wardm | 15:2e0d977dbb31 | 24 | #ifndef BUFFEREDSERIAL_H |
wardm | 15:2e0d977dbb31 | 25 | #define BUFFEREDSERIAL_H |
wardm | 15:2e0d977dbb31 | 26 | |
wardm | 15:2e0d977dbb31 | 27 | #include "mbed.h" |
wardm | 15:2e0d977dbb31 | 28 | #include "MyBuffer.h" |
wardm | 15:2e0d977dbb31 | 29 | |
wardm | 15:2e0d977dbb31 | 30 | /** A serial port (UART) for communication with other serial devices |
wardm | 15:2e0d977dbb31 | 31 | * |
wardm | 15:2e0d977dbb31 | 32 | * Can be used for Full Duplex communication, or Simplex by specifying |
wardm | 15:2e0d977dbb31 | 33 | * one pin as NC (Not Connected) |
wardm | 15:2e0d977dbb31 | 34 | * |
wardm | 15:2e0d977dbb31 | 35 | * Example: |
wardm | 15:2e0d977dbb31 | 36 | * @code |
wardm | 15:2e0d977dbb31 | 37 | * #include "mbed.h" |
wardm | 15:2e0d977dbb31 | 38 | * #include "BufferedSerial.h" |
wardm | 15:2e0d977dbb31 | 39 | * |
wardm | 15:2e0d977dbb31 | 40 | * BufferedSerial pc(USBTX, USBRX); |
wardm | 15:2e0d977dbb31 | 41 | * |
wardm | 15:2e0d977dbb31 | 42 | * int main() |
wardm | 15:2e0d977dbb31 | 43 | * { |
wardm | 15:2e0d977dbb31 | 44 | * while(1) |
wardm | 15:2e0d977dbb31 | 45 | * { |
wardm | 15:2e0d977dbb31 | 46 | * Timer s; |
wardm | 15:2e0d977dbb31 | 47 | * |
wardm | 15:2e0d977dbb31 | 48 | * s.start(); |
wardm | 15:2e0d977dbb31 | 49 | * pc.printf("Hello World - buffered\n"); |
wardm | 15:2e0d977dbb31 | 50 | * int buffered_time = s.read_us(); |
wardm | 15:2e0d977dbb31 | 51 | * wait(0.1f); // give time for the buffer to empty |
wardm | 15:2e0d977dbb31 | 52 | * |
wardm | 15:2e0d977dbb31 | 53 | * s.reset(); |
wardm | 15:2e0d977dbb31 | 54 | * printf("Hello World - blocking\n"); |
wardm | 15:2e0d977dbb31 | 55 | * int polled_time = s.read_us(); |
wardm | 15:2e0d977dbb31 | 56 | * s.stop(); |
wardm | 15:2e0d977dbb31 | 57 | * wait(0.1f); // give time for the buffer to empty |
wardm | 15:2e0d977dbb31 | 58 | * |
wardm | 15:2e0d977dbb31 | 59 | * pc.printf("printf buffered took %d us\n", buffered_time); |
wardm | 15:2e0d977dbb31 | 60 | * pc.printf("printf blocking took %d us\n", polled_time); |
wardm | 15:2e0d977dbb31 | 61 | * wait(0.5f); |
wardm | 15:2e0d977dbb31 | 62 | * } |
wardm | 15:2e0d977dbb31 | 63 | * } |
wardm | 15:2e0d977dbb31 | 64 | * @endcode |
wardm | 15:2e0d977dbb31 | 65 | */ |
wardm | 15:2e0d977dbb31 | 66 | |
wardm | 15:2e0d977dbb31 | 67 | /** |
wardm | 15:2e0d977dbb31 | 68 | * @class BufferedSerial |
wardm | 15:2e0d977dbb31 | 69 | * @brief Software buffers and interrupt driven tx and rx for Serial |
wardm | 15:2e0d977dbb31 | 70 | */ |
wardm | 15:2e0d977dbb31 | 71 | class BufferedSerial : public RawSerial |
wardm | 15:2e0d977dbb31 | 72 | { |
wardm | 15:2e0d977dbb31 | 73 | private: |
wardm | 15:2e0d977dbb31 | 74 | MyBuffer <char> _rxbuf; |
wardm | 15:2e0d977dbb31 | 75 | MyBuffer <char> _txbuf; |
wardm | 15:2e0d977dbb31 | 76 | uint32_t _buf_size; |
wardm | 15:2e0d977dbb31 | 77 | uint32_t _tx_multiple; |
wardm | 15:2e0d977dbb31 | 78 | |
wardm | 15:2e0d977dbb31 | 79 | void rxIrq(void); |
wardm | 15:2e0d977dbb31 | 80 | void txIrq(void); |
wardm | 15:2e0d977dbb31 | 81 | void prime(void); |
wardm | 15:2e0d977dbb31 | 82 | |
wardm | 15:2e0d977dbb31 | 83 | public: |
wardm | 15:2e0d977dbb31 | 84 | /** Create a BufferedSerial port, connected to the specified transmit and receive pins |
wardm | 15:2e0d977dbb31 | 85 | * @param tx Transmit pin |
wardm | 15:2e0d977dbb31 | 86 | * @param rx Receive pin |
wardm | 15:2e0d977dbb31 | 87 | * @param buf_size printf() buffer size |
wardm | 15:2e0d977dbb31 | 88 | * @param tx_multiple amount of max printf() present in the internal ring buffer at one time |
wardm | 15:2e0d977dbb31 | 89 | * @param name optional name |
wardm | 15:2e0d977dbb31 | 90 | * @note Either tx or rx may be specified as NC if unused |
wardm | 15:2e0d977dbb31 | 91 | */ |
wardm | 15:2e0d977dbb31 | 92 | BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); |
wardm | 15:2e0d977dbb31 | 93 | |
wardm | 15:2e0d977dbb31 | 94 | /** Destroy a BufferedSerial port |
wardm | 15:2e0d977dbb31 | 95 | */ |
wardm | 15:2e0d977dbb31 | 96 | virtual ~BufferedSerial(void); |
wardm | 15:2e0d977dbb31 | 97 | |
wardm | 15:2e0d977dbb31 | 98 | /** Check on how many bytes are in the rx buffer |
wardm | 15:2e0d977dbb31 | 99 | * @return 1 if something exists, 0 otherwise |
wardm | 15:2e0d977dbb31 | 100 | */ |
wardm | 15:2e0d977dbb31 | 101 | virtual int readable(void); |
wardm | 15:2e0d977dbb31 | 102 | |
wardm | 15:2e0d977dbb31 | 103 | /** Check to see if the tx buffer has room |
wardm | 15:2e0d977dbb31 | 104 | * @return 1 always has room and can overwrite previous content if too small / slow |
wardm | 15:2e0d977dbb31 | 105 | */ |
wardm | 15:2e0d977dbb31 | 106 | virtual int writeable(void); |
wardm | 15:2e0d977dbb31 | 107 | |
wardm | 15:2e0d977dbb31 | 108 | /** Get a single byte from the BufferedSerial Port. |
wardm | 15:2e0d977dbb31 | 109 | * Should check readable() before calling this. |
wardm | 15:2e0d977dbb31 | 110 | * @return A byte that came in on the Serial Port |
wardm | 15:2e0d977dbb31 | 111 | */ |
wardm | 15:2e0d977dbb31 | 112 | virtual int getc(void); |
wardm | 15:2e0d977dbb31 | 113 | |
wardm | 15:2e0d977dbb31 | 114 | /** Write a single byte to the BufferedSerial Port. |
wardm | 15:2e0d977dbb31 | 115 | * @param c The byte to write to the Serial Port |
wardm | 15:2e0d977dbb31 | 116 | * @return The byte that was written to the Serial Port Buffer |
wardm | 15:2e0d977dbb31 | 117 | */ |
wardm | 15:2e0d977dbb31 | 118 | virtual int putc(int c); |
wardm | 15:2e0d977dbb31 | 119 | |
wardm | 15:2e0d977dbb31 | 120 | /** Write a string to the BufferedSerial Port. Must be NULL terminated |
wardm | 15:2e0d977dbb31 | 121 | * @param s The string to write to the Serial Port |
wardm | 15:2e0d977dbb31 | 122 | * @return The number of bytes written to the Serial Port Buffer |
wardm | 15:2e0d977dbb31 | 123 | */ |
wardm | 15:2e0d977dbb31 | 124 | virtual int puts(const char *s); |
wardm | 15:2e0d977dbb31 | 125 | |
wardm | 15:2e0d977dbb31 | 126 | /** Write a formatted string to the BufferedSerial Port. |
wardm | 15:2e0d977dbb31 | 127 | * @param format The string + format specifiers to write to the Serial Port |
wardm | 15:2e0d977dbb31 | 128 | * @return The number of bytes written to the Serial Port Buffer |
wardm | 15:2e0d977dbb31 | 129 | */ |
wardm | 15:2e0d977dbb31 | 130 | virtual int printf(const char* format, ...); |
wardm | 15:2e0d977dbb31 | 131 | |
wardm | 15:2e0d977dbb31 | 132 | /** Write data to the Buffered Serial Port |
wardm | 15:2e0d977dbb31 | 133 | * @param s A pointer to data to send |
wardm | 15:2e0d977dbb31 | 134 | * @param length The amount of data being pointed to |
wardm | 15:2e0d977dbb31 | 135 | * @return The number of bytes written to the Serial Port Buffer |
wardm | 15:2e0d977dbb31 | 136 | */ |
wardm | 15:2e0d977dbb31 | 137 | virtual ssize_t write(const void *s, std::size_t length); |
wardm | 15:2e0d977dbb31 | 138 | }; |
wardm | 15:2e0d977dbb31 | 139 | |
wardm | 15:2e0d977dbb31 | 140 | #endif |