Software UART program using Infra-Red LED and IR-Detector

Dependents:   TestVirtualisation Bf_SoftSerial_IR

Committer:
kenjiArai
Date:
Fri May 15 04:11:01 2020 +0000
Revision:
15:8d343be3382d
Parent:
14:dc766032cdd6
Updated several functions based on SoftSerial library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 15:8d343be3382d 1 /*
kenjiArai 15:8d343be3382d 2 Original Library by Erik- & SonyPony
kenjiArai 15:8d343be3382d 3 https://os.mbed.com/users/Sissors/code/SoftSerial/
kenjiArai 15:8d343be3382d 4
kenjiArai 15:8d343be3382d 5 Modified by K.Arai / JH1PJL May 15th, 2020
kenjiArai 13:2b5649a1278a 6
kenjiArai 15:8d343be3382d 7 modified functions
kenjiArai 15:8d343be3382d 8 tx_handler()
kenjiArai 15:8d343be3382d 9 prepare_tx()
kenjiArai 15:8d343be3382d 10 prime()
kenjiArai 15:8d343be3382d 11 rx_gpio_irq_handler() and others
kenjiArai 15:8d343be3382d 12 */
kenjiArai 15:8d343be3382d 13
kenjiArai 15:8d343be3382d 14 #ifndef SOFTSERIAL_IR_H
kenjiArai 15:8d343be3382d 15 #define SOFTSERIAL_IR_H
Sissors 0:8edaa7abe724 16
Sissors 0:8edaa7abe724 17 #include "mbed.h"
kenjiArai 12:79d63607bbb1 18 #include "SoftSerial_Ticker_IR.h"
kenjiArai 13:2b5649a1278a 19
kenjiArai 15:8d343be3382d 20 #define DEBUG_TIMING 2 // 0=no debug, 1=tx, 2=rx
kenjiArai 15:8d343be3382d 21
kenjiArai 15:8d343be3382d 22 #if (MBED_MAJOR_VERSION == 2)
kenjiArai 15:8d343be3382d 23 # define YIELD {;}
kenjiArai 15:8d343be3382d 24 #elif (MBED_MAJOR_VERSION == 5)
kenjiArai 15:8d343be3382d 25 # define YIELD {ThisThread::yield();}
kenjiArai 15:8d343be3382d 26 #endif
kenjiArai 15:8d343be3382d 27
Sissors 0:8edaa7abe724 28 /** A software serial implementation
Sissors 0:8edaa7abe724 29 *
Sissors 0:8edaa7abe724 30 */
kenjiArai 15:8d343be3382d 31 class SoftSerial_IR: public Stream
kenjiArai 15:8d343be3382d 32 {
Sissors 0:8edaa7abe724 33
Sissors 0:8edaa7abe724 34 public:
Sissors 0:8edaa7abe724 35 /**
Sissors 0:8edaa7abe724 36 * Constructor
Sissors 0:8edaa7abe724 37 *
Sissors 0:8edaa7abe724 38 * @param TX Name of the TX pin, NC for not connected
kenjiArai 15:8d343be3382d 39 * @param RX Name of the RX pin, NC for not connected,
kenjiArai 15:8d343be3382d 40 * must be capable of being InterruptIn
Sissors 5:acfd0329f648 41 * @param name Name of the connection
Sissors 0:8edaa7abe724 42 */
kenjiArai 12:79d63607bbb1 43 SoftSerial_IR(PinName TX, PinName RX, const char* name = NULL);
kenjiArai 12:79d63607bbb1 44 virtual ~SoftSerial_IR();
Sissors 0:8edaa7abe724 45
kenjiArai 15:8d343be3382d 46 /** Set the baud rate of the serial port
kenjiArai 15:8d343be3382d 47 *
kenjiArai 15:8d343be3382d 48 * @param baudrate The baudrate of the serial port (default = 2400).
kenjiArai 15:8d343be3382d 49 */
kenjiArai 15:8d343be3382d 50 void baud(int baudrate);
kenjiArai 15:8d343be3382d 51
Sissors 0:8edaa7abe724 52 enum Parity {
Sissors 0:8edaa7abe724 53 None = 0,
Sissors 0:8edaa7abe724 54 Odd,
Sissors 0:8edaa7abe724 55 Even,
Sissors 0:8edaa7abe724 56 Forced1,
Sissors 0:8edaa7abe724 57 Forced0
Sissors 0:8edaa7abe724 58 };
Sissors 0:8edaa7abe724 59
Sissors 0:8edaa7abe724 60 enum IrqType {
Sissors 0:8edaa7abe724 61 RxIrq = 0,
Sissors 0:8edaa7abe724 62 TxIrq
Sissors 0:8edaa7abe724 63 };
Sissors 0:8edaa7abe724 64
Sissors 0:8edaa7abe724 65 /** Set the transmission format used by the serial port
Sissors 0:8edaa7abe724 66 *
Sissors 0:8edaa7abe724 67 * @param bits The number of bits in a word (default = 8)
kenjiArai 13:2b5649a1278a 68 * @param parity The parity used
kenjiArai 15:8d343be3382d 69 * (SoftSerial_IR::None, SoftSerial_IR::Odd, SoftSerial_IR::Even,
kenjiArai 15:8d343be3382d 70 * SoftSerial_IR::Forced1, SoftSerial_IR::Forced0;
kenjiArai 15:8d343be3382d 71 * default = SoftSerial_IR::None)
Sissors 0:8edaa7abe724 72 * @param stop The number of stop bits (default = 1)
Sissors 0:8edaa7abe724 73 */
kenjiArai 12:79d63607bbb1 74 void format(int bits=8, Parity parity=SoftSerial_IR::None, int stop_bits=1);
Sissors 0:8edaa7abe724 75
Sissors 0:8edaa7abe724 76 /** Determine if there is a character available to read
Sissors 0:8edaa7abe724 77 *
Sissors 0:8edaa7abe724 78 * @returns
Sissors 0:8edaa7abe724 79 * 1 if there is a character available to read,
Sissors 0:8edaa7abe724 80 * 0 otherwise
Sissors 0:8edaa7abe724 81 */
Sissors 0:8edaa7abe724 82 int readable();
Sissors 0:8edaa7abe724 83
Sissors 0:8edaa7abe724 84 /** Determine if there is space available to write a character
Sissors 0:8edaa7abe724 85 *
Sissors 0:8edaa7abe724 86 * @returns
Sissors 0:8edaa7abe724 87 * 1 if there is space to write a character,
Sissors 0:8edaa7abe724 88 * 0 otherwise
Sissors 0:8edaa7abe724 89 */
Sissors 0:8edaa7abe724 90 int writeable();
Sissors 0:8edaa7abe724 91
Sissors 0:8edaa7abe724 92 /** Attach a function to call whenever a serial interrupt is generated
Sissors 0:8edaa7abe724 93 *
Sissors 0:8edaa7abe724 94 * @param fptr A pointer to a void function, or 0 to set as none
kenjiArai 15:8d343be3382d 95 * @param type Which serial interrupt to attach the member function
kenjiArai 15:8d343be3382d 96 * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Sissors 0:8edaa7abe724 97 */
kenjiArai 15:8d343be3382d 98 void attach(void (*fptr)(void), IrqType type=RxIrq)
kenjiArai 15:8d343be3382d 99 {
kenjiArai 15:8d343be3382d 100 fpointer[type] = Callback<void()>(fptr);
Sissors 4:c010265ed202 101 }
Sissors 0:8edaa7abe724 102
kenjiArai 15:8d343be3382d 103 /** Attach a member function to call
kenjiArai 15:8d343be3382d 104 * whenever a serial interrupt is generated
Sissors 0:8edaa7abe724 105 *
Sissors 0:8edaa7abe724 106 * @param tptr pointer to the object to call the member function on
Sissors 0:8edaa7abe724 107 * @param mptr pointer to the member function to be called
kenjiArai 13:2b5649a1278a 108 * @param type Which serial interrupt to attach the member function
kenjiArai 15:8d343be3382d 109 * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Sissors 0:8edaa7abe724 110 */
Sissors 0:8edaa7abe724 111 template<typename T>
kenjiArai 15:8d343be3382d 112 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq)
kenjiArai 15:8d343be3382d 113 {
kenjiArai 15:8d343be3382d 114 fpointer[type] = Callback<void()>(tptr, mptr);
Sissors 4:c010265ed202 115 }
Sissors 0:8edaa7abe724 116
Sissors 0:8edaa7abe724 117 /** Generate a break condition on the serial line
Sissors 0:8edaa7abe724 118 */
Sissors 0:8edaa7abe724 119 void send_break();
Sissors 0:8edaa7abe724 120
Sissors 0:8edaa7abe724 121 protected:
kenjiArai 15:8d343be3382d 122 //DigitalOut *tx;
kenjiArai 12:79d63607bbb1 123 PwmOut *tx;
Sissors 0:8edaa7abe724 124 InterruptIn *rx;
kenjiArai 15:8d343be3382d 125
Sissors 0:8edaa7abe724 126 bool tx_en, rx_en;
Sissors 0:8edaa7abe724 127 int bit_period;
Sissors 2:9e01a38606b4 128 int _bits, _stop_bits, _total_bits;
Sissors 0:8edaa7abe724 129 Parity _parity;
kenjiArai 12:79d63607bbb1 130
kenjiArai 15:8d343be3382d 131 Callback<void()> fpointer[2];
kenjiArai 13:2b5649a1278a 132
Sissors 4:c010265ed202 133 //rx
Sissors 0:8edaa7abe724 134 void rx_gpio_irq_handler(void);
Sissors 1:f8b4b764ace7 135 void rx_handler(void);
Sissors 1:f8b4b764ace7 136 int read_buffer, rx_bit;
Sissors 1:f8b4b764ace7 137 volatile int out_buffer;
Sissors 1:f8b4b764ace7 138 volatile bool out_valid;
Sissors 1:f8b4b764ace7 139 bool rx_error;
kenjiArai 15:8d343be3382d 140 FlexTicker rxticker;
kenjiArai 15:8d343be3382d 141
kenjiArai 15:8d343be3382d 142 bool rx_1st;
kenjiArai 15:8d343be3382d 143 uint32_t rx_st_time;
kenjiArai 15:8d343be3382d 144
Sissors 0:8edaa7abe724 145 //tx
Sissors 0:8edaa7abe724 146 void tx_handler(void);
Sissors 2:9e01a38606b4 147 void prepare_tx(int c);
kenjiArai 15:8d343be3382d 148 FlexTicker txticker;
Sissors 0:8edaa7abe724 149 int _char;
Sissors 0:8edaa7abe724 150 volatile int tx_bit;
kenjiArai 12:79d63607bbb1 151
Sissors 0:8edaa7abe724 152 virtual int _getc();
Sissors 0:8edaa7abe724 153 virtual int _putc(int c);
Sissors 0:8edaa7abe724 154 };
Sissors 0:8edaa7abe724 155
Sissors 0:8edaa7abe724 156 #endif