tetete

Dependencies:   FIFO

Committer:
babylonica
Date:
Mon Aug 28 12:25:37 2017 +0000
Revision:
14:54cbbb00bd19
Parent:
13:e372a354d969
Child:
15:278f7f125495
This is stable version. Require the mbed Library Rev.147 or later.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
babylonica 5:a50cdaa90d6a 1 // -*- coding: utf-8 -*-
babylonica 5:a50cdaa90d6a 2 /**
babylonica 5:a50cdaa90d6a 3 @file AsyncSerial.cpp
babylonica 5:a50cdaa90d6a 4 @brief Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
babylonica 5:a50cdaa90d6a 5
babylonica 5:a50cdaa90d6a 6 @author T.Kawamura
babylonica 13:e372a354d969 7 @version 1.3
babylonica 5:a50cdaa90d6a 8 @date 2017-03-29 T.Kawamura Written for C++/mbed.
babylonica 5:a50cdaa90d6a 9 @date 2017-03-30 T.Kawamura Bug Fixed: Cannot use format(), baud().
babylonica 8:7390f9bb28d3 10 @date 2017-06-17 T.Kawamura Update: FIFO Buffer Fixed.
babylonica 5:a50cdaa90d6a 11
babylonica 13:e372a354d969 12
babylonica 5:a50cdaa90d6a 13 @see
babylonica 5:a50cdaa90d6a 14 Copyright (C) 2017 T.Kawamura.
babylonica 5:a50cdaa90d6a 15 Released under the MIT license.
babylonica 5:a50cdaa90d6a 16 http://opensource.org/licenses/mit-license.php
babylonica 5:a50cdaa90d6a 17
babylonica 5:a50cdaa90d6a 18 */
babylonica 5:a50cdaa90d6a 19
babylonica 5:a50cdaa90d6a 20 #include "AsyncSerial.hpp"
babylonica 5:a50cdaa90d6a 21
babylonica 10:1baa8e833ac6 22 AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size){
babylonica 12:bfe3984fb2e5 23
babylonica 14:54cbbb00bd19 24 //led = new DigitalOut(LED2);
babylonica 12:bfe3984fb2e5 25
babylonica 10:1baa8e833ac6 26 // RawSerial port init
babylonica 10:1baa8e833ac6 27 serial = new RawSerial(txpin, rxpin, baudrate);
babylonica 10:1baa8e833ac6 28
babylonica 10:1baa8e833ac6 29 // FIFO init
babylonica 10:1baa8e833ac6 30 fifo_tx = new FIFO<uint8_t>(buffer_size);
babylonica 10:1baa8e833ac6 31 fifo_rx = new FIFO<uint8_t>(buffer_size);
babylonica 10:1baa8e833ac6 32
babylonica 13:e372a354d969 33 // Debug
babylonica 13:e372a354d969 34 //led->write(1);
babylonica 13:e372a354d969 35 //led->write(0); // Went off
babylonica 13:e372a354d969 36
babylonica 14:54cbbb00bd19 37
babylonica 14:54cbbb00bd19 38 fifo_tx->clear();
babylonica 14:54cbbb00bd19 39 fifo_rx->clear();
babylonica 14:54cbbb00bd19 40
babylonica 14:54cbbb00bd19 41 Is_Serial_Sending = false;
babylonica 14:54cbbb00bd19 42
babylonica 5:a50cdaa90d6a 43 //Initialize ISR
babylonica 13:e372a354d969 44 serial->attach(callback(this, &AsyncSerial::ISR_TX), SerialBase::TxIrq);
babylonica 13:e372a354d969 45 serial->attach(callback(this, &AsyncSerial::ISR_RX), SerialBase::RxIrq);
babylonica 12:bfe3984fb2e5 46
babylonica 13:e372a354d969 47 // Debug
babylonica 13:e372a354d969 48 //led->write(0); // Didnt go off
babylonica 13:e372a354d969 49
babylonica 5:a50cdaa90d6a 50 return;
babylonica 5:a50cdaa90d6a 51 }
babylonica 5:a50cdaa90d6a 52
babylonica 5:a50cdaa90d6a 53 AsyncSerial::~AsyncSerial(){
babylonica 10:1baa8e833ac6 54 serial->attach(NULL, serial->TxIrq);
babylonica 10:1baa8e833ac6 55 serial->attach(NULL, serial->RxIrq);
babylonica 14:54cbbb00bd19 56 led->write(1);
babylonica 10:1baa8e833ac6 57
babylonica 10:1baa8e833ac6 58 delete serial;
babylonica 5:a50cdaa90d6a 59 return;
babylonica 5:a50cdaa90d6a 60 }
babylonica 5:a50cdaa90d6a 61
babylonica 5:a50cdaa90d6a 62 void AsyncSerial::ISR_TX(void){
babylonica 5:a50cdaa90d6a 63 int data;
babylonica 13:e372a354d969 64
babylonica 10:1baa8e833ac6 65 if( fifo_tx->available() > 0 ){
babylonica 10:1baa8e833ac6 66 data = (int)fifo_tx->get();
babylonica 10:1baa8e833ac6 67 serial->putc(data);
babylonica 5:a50cdaa90d6a 68 }else{
babylonica 5:a50cdaa90d6a 69 Is_Serial_Sending = false;
babylonica 10:1baa8e833ac6 70 }
babylonica 5:a50cdaa90d6a 71 }
babylonica 5:a50cdaa90d6a 72
babylonica 5:a50cdaa90d6a 73 void AsyncSerial::ISR_RX(void){
babylonica 5:a50cdaa90d6a 74 uint8_t data;
babylonica 5:a50cdaa90d6a 75
babylonica 10:1baa8e833ac6 76 data = (uint8_t)serial->getc();
babylonica 10:1baa8e833ac6 77 fifo_rx->put(data);
babylonica 5:a50cdaa90d6a 78 }
babylonica 5:a50cdaa90d6a 79
babylonica 5:a50cdaa90d6a 80 int AsyncSerial::readable(void){
babylonica 10:1baa8e833ac6 81 return (int)fifo_rx->available();
babylonica 5:a50cdaa90d6a 82 }
babylonica 5:a50cdaa90d6a 83
babylonica 5:a50cdaa90d6a 84 int AsyncSerial::writeable(void){
babylonica 5:a50cdaa90d6a 85 return 1;
babylonica 5:a50cdaa90d6a 86 }
babylonica 5:a50cdaa90d6a 87
babylonica 5:a50cdaa90d6a 88 int AsyncSerial::getc(void){
babylonica 10:1baa8e833ac6 89 return (int)fifo_rx->get();
babylonica 5:a50cdaa90d6a 90 }
babylonica 5:a50cdaa90d6a 91
babylonica 5:a50cdaa90d6a 92 int AsyncSerial::peekc(void){
babylonica 10:1baa8e833ac6 93 return (int)fifo_rx->peek();
babylonica 5:a50cdaa90d6a 94 }
babylonica 5:a50cdaa90d6a 95
babylonica 8:7390f9bb28d3 96 void AsyncSerial::putc(int c){
babylonica 5:a50cdaa90d6a 97 if( Is_Serial_Sending ){
babylonica 10:1baa8e833ac6 98 fifo_tx->put((uint8_t)c);
babylonica 5:a50cdaa90d6a 99 }else{
babylonica 5:a50cdaa90d6a 100 Is_Serial_Sending = true;
babylonica 10:1baa8e833ac6 101 serial->putc(c);
babylonica 5:a50cdaa90d6a 102 }
babylonica 8:7390f9bb28d3 103 return;
babylonica 5:a50cdaa90d6a 104 }
babylonica 5:a50cdaa90d6a 105
babylonica 8:7390f9bb28d3 106 void AsyncSerial::puts(const char *str){
babylonica 5:a50cdaa90d6a 107 uint8_t temp;
babylonica 0:907ac3c2fadc 108
babylonica 5:a50cdaa90d6a 109 for(uint32_t i = 0; i < strlen(str); i++){
babylonica 5:a50cdaa90d6a 110 temp = (uint8_t)str[i];
babylonica 10:1baa8e833ac6 111 fifo_tx->put(temp);
babylonica 5:a50cdaa90d6a 112 }
babylonica 5:a50cdaa90d6a 113
babylonica 5:a50cdaa90d6a 114 if( !Is_Serial_Sending ){
babylonica 5:a50cdaa90d6a 115 Is_Serial_Sending = true;
babylonica 10:1baa8e833ac6 116 serial->putc((int)fifo_tx->get());
babylonica 5:a50cdaa90d6a 117 }
babylonica 6:08e3b1bba0ac 118
babylonica 6:08e3b1bba0ac 119 AsyncSerial::putc('\r');
babylonica 5:a50cdaa90d6a 120 AsyncSerial::putc('\n');
babylonica 8:7390f9bb28d3 121 return;
babylonica 5:a50cdaa90d6a 122 }
babylonica 5:a50cdaa90d6a 123
babylonica 5:a50cdaa90d6a 124 int AsyncSerial::printf(const char *format, ...){
babylonica 5:a50cdaa90d6a 125 int32_t wrote_length = 0;
babylonica 5:a50cdaa90d6a 126 char string_buffer[PRINTF_STRING_BUFFER_SIZE];
babylonica 5:a50cdaa90d6a 127
babylonica 5:a50cdaa90d6a 128 memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE);
babylonica 5:a50cdaa90d6a 129
babylonica 5:a50cdaa90d6a 130 va_list arg;
babylonica 5:a50cdaa90d6a 131 va_start(arg, format);
babylonica 5:a50cdaa90d6a 132 wrote_length = vsprintf(string_buffer, format, arg);
babylonica 5:a50cdaa90d6a 133
babylonica 5:a50cdaa90d6a 134 if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) {
babylonica 5:a50cdaa90d6a 135 error("%s @ %d : String is too large, string buffer overwrite. (Max buffer size: %d Wrote length: %d)\n", __FILE__, __LINE__, PRINTF_STRING_BUFFER_SIZE, wrote_length);
babylonica 5:a50cdaa90d6a 136 va_end(arg);
babylonica 5:a50cdaa90d6a 137 return 0;
babylonica 5:a50cdaa90d6a 138 }
babylonica 5:a50cdaa90d6a 139
babylonica 5:a50cdaa90d6a 140 if( wrote_length < 0 ){
babylonica 5:a50cdaa90d6a 141 va_end(arg);
babylonica 5:a50cdaa90d6a 142 error("Function vsprintf() was failed.");
babylonica 5:a50cdaa90d6a 143 return 0;
babylonica 5:a50cdaa90d6a 144 }
babylonica 5:a50cdaa90d6a 145
babylonica 5:a50cdaa90d6a 146 va_end(arg);
babylonica 8:7390f9bb28d3 147 AsyncSerial::write((uint8_t*)string_buffer, wrote_length);
babylonica 5:a50cdaa90d6a 148
babylonica 8:7390f9bb28d3 149 return wrote_length;
babylonica 5:a50cdaa90d6a 150 }
babylonica 5:a50cdaa90d6a 151
babylonica 5:a50cdaa90d6a 152 int AsyncSerial::write(const uint8_t *buffer, int length){
babylonica 5:a50cdaa90d6a 153 uint8_t temp;
babylonica 5:a50cdaa90d6a 154
babylonica 5:a50cdaa90d6a 155 if ( length < 1 ){
babylonica 5:a50cdaa90d6a 156 return 0;
babylonica 5:a50cdaa90d6a 157 }
babylonica 5:a50cdaa90d6a 158
babylonica 5:a50cdaa90d6a 159 for(uint32_t i = 0; i < length; i++){
babylonica 5:a50cdaa90d6a 160 temp = (uint8_t)buffer[i];
babylonica 10:1baa8e833ac6 161 fifo_tx->put(temp);
babylonica 5:a50cdaa90d6a 162 }
babylonica 5:a50cdaa90d6a 163
babylonica 5:a50cdaa90d6a 164 if( !Is_Serial_Sending ){
babylonica 5:a50cdaa90d6a 165 Is_Serial_Sending = true;
babylonica 10:1baa8e833ac6 166 serial->putc((int)fifo_tx->get());
babylonica 5:a50cdaa90d6a 167 }
babylonica 5:a50cdaa90d6a 168
babylonica 8:7390f9bb28d3 169 return 1;
babylonica 5:a50cdaa90d6a 170 }
babylonica 5:a50cdaa90d6a 171
babylonica 10:1baa8e833ac6 172 void AsyncSerial::abort_read(void){
babylonica 10:1baa8e833ac6 173 fifo_rx->clear();
babylonica 10:1baa8e833ac6 174 return;
babylonica 10:1baa8e833ac6 175 }
babylonica 10:1baa8e833ac6 176
babylonica 10:1baa8e833ac6 177 void AsyncSerial::abort_write(void){
babylonica 10:1baa8e833ac6 178 fifo_tx->clear();
babylonica 5:a50cdaa90d6a 179 return;
babylonica 5:a50cdaa90d6a 180 }
babylonica 5:a50cdaa90d6a 181
babylonica 5:a50cdaa90d6a 182 void AsyncSerial::wait(void){
babylonica 10:1baa8e833ac6 183 while( fifo_tx->available() > 0 ){}
babylonica 5:a50cdaa90d6a 184 return;
babylonica 5:a50cdaa90d6a 185 }
babylonica 5:a50cdaa90d6a 186
babylonica 10:1baa8e833ac6 187 void AsyncSerial::format(int bits, RawSerial::Parity parity, int stop_bits){
babylonica 10:1baa8e833ac6 188 serial->attach(NULL, serial->TxIrq);
babylonica 10:1baa8e833ac6 189 serial->attach(NULL, serial->RxIrq);
babylonica 5:a50cdaa90d6a 190
babylonica 10:1baa8e833ac6 191 serial->format(bits, parity, stop_bits);
babylonica 5:a50cdaa90d6a 192
babylonica 12:bfe3984fb2e5 193 serial->attach(callback(this, &AsyncSerial::ISR_TX), serial->TxIrq);
babylonica 12:bfe3984fb2e5 194 serial->attach(callback(this, &AsyncSerial::ISR_RX), serial->RxIrq);
babylonica 5:a50cdaa90d6a 195 }
babylonica 5:a50cdaa90d6a 196
babylonica 5:a50cdaa90d6a 197 void AsyncSerial::baud(int baudrate){
babylonica 10:1baa8e833ac6 198 serial->attach(NULL, serial->TxIrq);
babylonica 10:1baa8e833ac6 199 serial->attach(NULL, serial->RxIrq);
babylonica 5:a50cdaa90d6a 200
babylonica 10:1baa8e833ac6 201 serial->baud(baudrate);
babylonica 5:a50cdaa90d6a 202
babylonica 12:bfe3984fb2e5 203 serial->attach(callback(this, &AsyncSerial::ISR_TX), serial->TxIrq);
babylonica 12:bfe3984fb2e5 204 serial->attach(callback(this, &AsyncSerial::ISR_RX), serial->RxIrq);
babylonica 5:a50cdaa90d6a 205 }
babylonica 5:a50cdaa90d6a 206