tetete

Dependencies:   FIFO

Committer:
babylonica
Date:
Wed Aug 23 01:11:52 2017 +0000
Revision:
13:e372a354d969
Parent:
12:bfe3984fb2e5
Child:
14:54cbbb00bd19
Fixed to RawSerial::attach(callback(obj, method), IrqType)

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 12:bfe3984fb2e5 24 led = new DigitalOut(LED1);
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 5:a50cdaa90d6a 37 //Initialize ISR
babylonica 13:e372a354d969 38 serial->attach(callback(this, &AsyncSerial::ISR_TX), SerialBase::TxIrq);
babylonica 13:e372a354d969 39 serial->attach(callback(this, &AsyncSerial::ISR_RX), SerialBase::RxIrq);
babylonica 12:bfe3984fb2e5 40
babylonica 13:e372a354d969 41 // Debug
babylonica 13:e372a354d969 42 //led->write(0); // Didnt go off
babylonica 13:e372a354d969 43
babylonica 10:1baa8e833ac6 44 fifo_tx->clear();
babylonica 10:1baa8e833ac6 45 fifo_rx->clear();
babylonica 5:a50cdaa90d6a 46
babylonica 5:a50cdaa90d6a 47 Is_Serial_Sending = false;
babylonica 12:bfe3984fb2e5 48
babylonica 5:a50cdaa90d6a 49 return;
babylonica 5:a50cdaa90d6a 50 }
babylonica 5:a50cdaa90d6a 51
babylonica 5:a50cdaa90d6a 52 AsyncSerial::~AsyncSerial(){
babylonica 10:1baa8e833ac6 53 serial->attach(NULL, serial->TxIrq);
babylonica 10:1baa8e833ac6 54 serial->attach(NULL, serial->RxIrq);
babylonica 10:1baa8e833ac6 55
babylonica 10:1baa8e833ac6 56 delete serial;
babylonica 5:a50cdaa90d6a 57 return;
babylonica 5:a50cdaa90d6a 58 }
babylonica 5:a50cdaa90d6a 59
babylonica 5:a50cdaa90d6a 60 void AsyncSerial::ISR_TX(void){
babylonica 5:a50cdaa90d6a 61 int data;
babylonica 13:e372a354d969 62
babylonica 10:1baa8e833ac6 63 if( fifo_tx->available() > 0 ){
babylonica 10:1baa8e833ac6 64 data = (int)fifo_tx->get();
babylonica 10:1baa8e833ac6 65 serial->putc(data);
babylonica 5:a50cdaa90d6a 66 }else{
babylonica 5:a50cdaa90d6a 67 Is_Serial_Sending = false;
babylonica 10:1baa8e833ac6 68 }
babylonica 5:a50cdaa90d6a 69 }
babylonica 5:a50cdaa90d6a 70
babylonica 5:a50cdaa90d6a 71 void AsyncSerial::ISR_RX(void){
babylonica 5:a50cdaa90d6a 72 uint8_t data;
babylonica 5:a50cdaa90d6a 73
babylonica 10:1baa8e833ac6 74 data = (uint8_t)serial->getc();
babylonica 10:1baa8e833ac6 75 fifo_rx->put(data);
babylonica 5:a50cdaa90d6a 76 }
babylonica 5:a50cdaa90d6a 77
babylonica 5:a50cdaa90d6a 78 int AsyncSerial::readable(void){
babylonica 10:1baa8e833ac6 79 return (int)fifo_rx->available();
babylonica 5:a50cdaa90d6a 80 }
babylonica 5:a50cdaa90d6a 81
babylonica 5:a50cdaa90d6a 82 int AsyncSerial::writeable(void){
babylonica 5:a50cdaa90d6a 83 return 1;
babylonica 5:a50cdaa90d6a 84 }
babylonica 5:a50cdaa90d6a 85
babylonica 5:a50cdaa90d6a 86 int AsyncSerial::getc(void){
babylonica 10:1baa8e833ac6 87 return (int)fifo_rx->get();
babylonica 5:a50cdaa90d6a 88 }
babylonica 5:a50cdaa90d6a 89
babylonica 5:a50cdaa90d6a 90 int AsyncSerial::peekc(void){
babylonica 10:1baa8e833ac6 91 return (int)fifo_rx->peek();
babylonica 5:a50cdaa90d6a 92 }
babylonica 5:a50cdaa90d6a 93
babylonica 8:7390f9bb28d3 94 void AsyncSerial::putc(int c){
babylonica 5:a50cdaa90d6a 95 if( Is_Serial_Sending ){
babylonica 10:1baa8e833ac6 96 fifo_tx->put((uint8_t)c);
babylonica 5:a50cdaa90d6a 97 }else{
babylonica 5:a50cdaa90d6a 98 Is_Serial_Sending = true;
babylonica 10:1baa8e833ac6 99 serial->putc(c);
babylonica 5:a50cdaa90d6a 100 }
babylonica 8:7390f9bb28d3 101 return;
babylonica 5:a50cdaa90d6a 102 }
babylonica 5:a50cdaa90d6a 103
babylonica 8:7390f9bb28d3 104 void AsyncSerial::puts(const char *str){
babylonica 5:a50cdaa90d6a 105 uint8_t temp;
babylonica 0:907ac3c2fadc 106
babylonica 5:a50cdaa90d6a 107 for(uint32_t i = 0; i < strlen(str); i++){
babylonica 5:a50cdaa90d6a 108 temp = (uint8_t)str[i];
babylonica 10:1baa8e833ac6 109 fifo_tx->put(temp);
babylonica 5:a50cdaa90d6a 110 }
babylonica 5:a50cdaa90d6a 111
babylonica 5:a50cdaa90d6a 112 if( !Is_Serial_Sending ){
babylonica 5:a50cdaa90d6a 113 Is_Serial_Sending = true;
babylonica 10:1baa8e833ac6 114 serial->putc((int)fifo_tx->get());
babylonica 5:a50cdaa90d6a 115 }
babylonica 6:08e3b1bba0ac 116
babylonica 6:08e3b1bba0ac 117 AsyncSerial::putc('\r');
babylonica 5:a50cdaa90d6a 118 AsyncSerial::putc('\n');
babylonica 8:7390f9bb28d3 119 return;
babylonica 5:a50cdaa90d6a 120 }
babylonica 5:a50cdaa90d6a 121
babylonica 5:a50cdaa90d6a 122 int AsyncSerial::printf(const char *format, ...){
babylonica 5:a50cdaa90d6a 123 int32_t wrote_length = 0;
babylonica 5:a50cdaa90d6a 124 char string_buffer[PRINTF_STRING_BUFFER_SIZE];
babylonica 5:a50cdaa90d6a 125
babylonica 5:a50cdaa90d6a 126 memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE);
babylonica 5:a50cdaa90d6a 127
babylonica 5:a50cdaa90d6a 128 va_list arg;
babylonica 5:a50cdaa90d6a 129 va_start(arg, format);
babylonica 5:a50cdaa90d6a 130 wrote_length = vsprintf(string_buffer, format, arg);
babylonica 5:a50cdaa90d6a 131
babylonica 5:a50cdaa90d6a 132 if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) {
babylonica 5:a50cdaa90d6a 133 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 134 va_end(arg);
babylonica 5:a50cdaa90d6a 135 return 0;
babylonica 5:a50cdaa90d6a 136 }
babylonica 5:a50cdaa90d6a 137
babylonica 5:a50cdaa90d6a 138 if( wrote_length < 0 ){
babylonica 5:a50cdaa90d6a 139 va_end(arg);
babylonica 5:a50cdaa90d6a 140 error("Function vsprintf() was failed.");
babylonica 5:a50cdaa90d6a 141 return 0;
babylonica 5:a50cdaa90d6a 142 }
babylonica 5:a50cdaa90d6a 143
babylonica 5:a50cdaa90d6a 144 va_end(arg);
babylonica 8:7390f9bb28d3 145 AsyncSerial::write((uint8_t*)string_buffer, wrote_length);
babylonica 5:a50cdaa90d6a 146
babylonica 8:7390f9bb28d3 147 return wrote_length;
babylonica 5:a50cdaa90d6a 148 }
babylonica 5:a50cdaa90d6a 149
babylonica 5:a50cdaa90d6a 150 int AsyncSerial::write(const uint8_t *buffer, int length){
babylonica 5:a50cdaa90d6a 151 uint8_t temp;
babylonica 5:a50cdaa90d6a 152
babylonica 5:a50cdaa90d6a 153 if ( length < 1 ){
babylonica 5:a50cdaa90d6a 154 return 0;
babylonica 5:a50cdaa90d6a 155 }
babylonica 5:a50cdaa90d6a 156
babylonica 5:a50cdaa90d6a 157 for(uint32_t i = 0; i < length; i++){
babylonica 5:a50cdaa90d6a 158 temp = (uint8_t)buffer[i];
babylonica 10:1baa8e833ac6 159 fifo_tx->put(temp);
babylonica 5:a50cdaa90d6a 160 }
babylonica 5:a50cdaa90d6a 161
babylonica 5:a50cdaa90d6a 162 if( !Is_Serial_Sending ){
babylonica 5:a50cdaa90d6a 163 Is_Serial_Sending = true;
babylonica 10:1baa8e833ac6 164 serial->putc((int)fifo_tx->get());
babylonica 5:a50cdaa90d6a 165 }
babylonica 5:a50cdaa90d6a 166
babylonica 8:7390f9bb28d3 167 return 1;
babylonica 5:a50cdaa90d6a 168 }
babylonica 5:a50cdaa90d6a 169
babylonica 10:1baa8e833ac6 170 void AsyncSerial::abort_read(void){
babylonica 10:1baa8e833ac6 171 fifo_rx->clear();
babylonica 10:1baa8e833ac6 172 return;
babylonica 10:1baa8e833ac6 173 }
babylonica 10:1baa8e833ac6 174
babylonica 10:1baa8e833ac6 175 void AsyncSerial::abort_write(void){
babylonica 10:1baa8e833ac6 176 fifo_tx->clear();
babylonica 5:a50cdaa90d6a 177 return;
babylonica 5:a50cdaa90d6a 178 }
babylonica 5:a50cdaa90d6a 179
babylonica 5:a50cdaa90d6a 180 void AsyncSerial::wait(void){
babylonica 10:1baa8e833ac6 181 while( fifo_tx->available() > 0 ){}
babylonica 5:a50cdaa90d6a 182 return;
babylonica 5:a50cdaa90d6a 183 }
babylonica 5:a50cdaa90d6a 184
babylonica 10:1baa8e833ac6 185 void AsyncSerial::format(int bits, RawSerial::Parity parity, int stop_bits){
babylonica 10:1baa8e833ac6 186 serial->attach(NULL, serial->TxIrq);
babylonica 10:1baa8e833ac6 187 serial->attach(NULL, serial->RxIrq);
babylonica 5:a50cdaa90d6a 188
babylonica 10:1baa8e833ac6 189 serial->format(bits, parity, stop_bits);
babylonica 5:a50cdaa90d6a 190
babylonica 12:bfe3984fb2e5 191 serial->attach(callback(this, &AsyncSerial::ISR_TX), serial->TxIrq);
babylonica 12:bfe3984fb2e5 192 serial->attach(callback(this, &AsyncSerial::ISR_RX), serial->RxIrq);
babylonica 5:a50cdaa90d6a 193 }
babylonica 5:a50cdaa90d6a 194
babylonica 5:a50cdaa90d6a 195 void AsyncSerial::baud(int baudrate){
babylonica 10:1baa8e833ac6 196 serial->attach(NULL, serial->TxIrq);
babylonica 10:1baa8e833ac6 197 serial->attach(NULL, serial->RxIrq);
babylonica 5:a50cdaa90d6a 198
babylonica 10:1baa8e833ac6 199 serial->baud(baudrate);
babylonica 5:a50cdaa90d6a 200
babylonica 12:bfe3984fb2e5 201 serial->attach(callback(this, &AsyncSerial::ISR_TX), serial->TxIrq);
babylonica 12:bfe3984fb2e5 202 serial->attach(callback(this, &AsyncSerial::ISR_RX), serial->RxIrq);
babylonica 5:a50cdaa90d6a 203 }
babylonica 5:a50cdaa90d6a 204