tetete

Dependencies:   FIFO

Committer:
babylonica
Date:
Sat Jun 17 11:44:36 2017 +0000
Revision:
8:7390f9bb28d3
Parent:
6:08e3b1bba0ac
Child:
9:709719dddd3e
FIFO Buffer Update.; Bug Fixed.

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 8:7390f9bb28d3 7 @version 1.2
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 5:a50cdaa90d6a 12 @see
babylonica 5:a50cdaa90d6a 13 Copyright (C) 2017 T.Kawamura.
babylonica 5:a50cdaa90d6a 14 Released under the MIT license.
babylonica 5:a50cdaa90d6a 15 http://opensource.org/licenses/mit-license.php
babylonica 5:a50cdaa90d6a 16
babylonica 5:a50cdaa90d6a 17 */
babylonica 5:a50cdaa90d6a 18
babylonica 5:a50cdaa90d6a 19 #include "AsyncSerial.hpp"
babylonica 5:a50cdaa90d6a 20
babylonica 5:a50cdaa90d6a 21 AsyncSerial::AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate, uint32_t buffer_size) : RawSerial(txpin, rxpin, baudrate), fifo_tx(buffer_size), fifo_rx(buffer_size){
babylonica 5:a50cdaa90d6a 22 //Initialize ISR
babylonica 5:a50cdaa90d6a 23 RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
babylonica 5:a50cdaa90d6a 24 RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
babylonica 5:a50cdaa90d6a 25
babylonica 5:a50cdaa90d6a 26 fifo_tx.clear();
babylonica 5:a50cdaa90d6a 27 fifo_rx.clear();
babylonica 5:a50cdaa90d6a 28
babylonica 5:a50cdaa90d6a 29 Is_Serial_Sending = false;
babylonica 5:a50cdaa90d6a 30
babylonica 5:a50cdaa90d6a 31 return;
babylonica 5:a50cdaa90d6a 32 }
babylonica 5:a50cdaa90d6a 33
babylonica 5:a50cdaa90d6a 34 AsyncSerial::~AsyncSerial(){
babylonica 5:a50cdaa90d6a 35 RawSerial::attach(NULL, RawSerial::TxIrq);
babylonica 5:a50cdaa90d6a 36 RawSerial::attach(NULL, RawSerial::RxIrq);
babylonica 5:a50cdaa90d6a 37
babylonica 5:a50cdaa90d6a 38 return;
babylonica 5:a50cdaa90d6a 39 }
babylonica 5:a50cdaa90d6a 40
babylonica 5:a50cdaa90d6a 41 void AsyncSerial::ISR_TX(void){
babylonica 5:a50cdaa90d6a 42 int data;
babylonica 5:a50cdaa90d6a 43
babylonica 5:a50cdaa90d6a 44 if( fifo_tx.available() > 0 ){
babylonica 5:a50cdaa90d6a 45 data = (int)fifo_tx.get();
babylonica 5:a50cdaa90d6a 46 RawSerial::putc(data);
babylonica 5:a50cdaa90d6a 47 }else{
babylonica 5:a50cdaa90d6a 48 Is_Serial_Sending = false;
babylonica 5:a50cdaa90d6a 49 }
babylonica 5:a50cdaa90d6a 50 }
babylonica 5:a50cdaa90d6a 51
babylonica 5:a50cdaa90d6a 52 void AsyncSerial::ISR_RX(void){
babylonica 5:a50cdaa90d6a 53 uint8_t data;
babylonica 5:a50cdaa90d6a 54
babylonica 5:a50cdaa90d6a 55 data = (uint8_t)RawSerial::getc();
babylonica 5:a50cdaa90d6a 56 fifo_rx.put(data);
babylonica 5:a50cdaa90d6a 57 }
babylonica 5:a50cdaa90d6a 58
babylonica 5:a50cdaa90d6a 59 int AsyncSerial::readable(void){
babylonica 5:a50cdaa90d6a 60 return (int)fifo_rx.available();
babylonica 5:a50cdaa90d6a 61 }
babylonica 5:a50cdaa90d6a 62
babylonica 5:a50cdaa90d6a 63 int AsyncSerial::writeable(void){
babylonica 5:a50cdaa90d6a 64 return 1;
babylonica 5:a50cdaa90d6a 65 }
babylonica 5:a50cdaa90d6a 66
babylonica 5:a50cdaa90d6a 67 int AsyncSerial::getc(void){
babylonica 5:a50cdaa90d6a 68 return (int)fifo_rx.get();
babylonica 5:a50cdaa90d6a 69 }
babylonica 5:a50cdaa90d6a 70
babylonica 5:a50cdaa90d6a 71 int AsyncSerial::peekc(void){
babylonica 5:a50cdaa90d6a 72 return (int)fifo_rx.peek();
babylonica 5:a50cdaa90d6a 73 }
babylonica 5:a50cdaa90d6a 74
babylonica 8:7390f9bb28d3 75 void AsyncSerial::putc(int c){
babylonica 5:a50cdaa90d6a 76 if( Is_Serial_Sending ){
babylonica 8:7390f9bb28d3 77 fifo_tx.put((uint8_t)c);
babylonica 5:a50cdaa90d6a 78 }else{
babylonica 5:a50cdaa90d6a 79 Is_Serial_Sending = true;
babylonica 5:a50cdaa90d6a 80 RawSerial::putc(c);
babylonica 5:a50cdaa90d6a 81 }
babylonica 8:7390f9bb28d3 82 return;
babylonica 5:a50cdaa90d6a 83 }
babylonica 5:a50cdaa90d6a 84
babylonica 8:7390f9bb28d3 85 void AsyncSerial::puts(const char *str){
babylonica 5:a50cdaa90d6a 86 uint8_t temp;
babylonica 0:907ac3c2fadc 87
babylonica 5:a50cdaa90d6a 88 for(uint32_t i = 0; i < strlen(str); i++){
babylonica 5:a50cdaa90d6a 89 temp = (uint8_t)str[i];
babylonica 8:7390f9bb28d3 90 fifo_tx.put(temp);
babylonica 5:a50cdaa90d6a 91 }
babylonica 5:a50cdaa90d6a 92
babylonica 5:a50cdaa90d6a 93 if( !Is_Serial_Sending ){
babylonica 5:a50cdaa90d6a 94 Is_Serial_Sending = true;
babylonica 5:a50cdaa90d6a 95 RawSerial::putc((int)fifo_tx.get());
babylonica 5:a50cdaa90d6a 96 }
babylonica 6:08e3b1bba0ac 97
babylonica 6:08e3b1bba0ac 98 AsyncSerial::putc('\r');
babylonica 5:a50cdaa90d6a 99 AsyncSerial::putc('\n');
babylonica 8:7390f9bb28d3 100 return;
babylonica 5:a50cdaa90d6a 101 }
babylonica 5:a50cdaa90d6a 102
babylonica 5:a50cdaa90d6a 103 int AsyncSerial::printf(const char *format, ...){
babylonica 5:a50cdaa90d6a 104 int32_t wrote_length = 0;
babylonica 5:a50cdaa90d6a 105 char string_buffer[PRINTF_STRING_BUFFER_SIZE];
babylonica 5:a50cdaa90d6a 106
babylonica 5:a50cdaa90d6a 107 memset(string_buffer, 0, PRINTF_STRING_BUFFER_SIZE);
babylonica 5:a50cdaa90d6a 108
babylonica 5:a50cdaa90d6a 109 va_list arg;
babylonica 5:a50cdaa90d6a 110 va_start(arg, format);
babylonica 5:a50cdaa90d6a 111 wrote_length = vsprintf(string_buffer, format, arg);
babylonica 5:a50cdaa90d6a 112
babylonica 5:a50cdaa90d6a 113 if( wrote_length > PRINTF_STRING_BUFFER_SIZE ) {
babylonica 5:a50cdaa90d6a 114 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 115 va_end(arg);
babylonica 5:a50cdaa90d6a 116 return 0;
babylonica 5:a50cdaa90d6a 117 }
babylonica 5:a50cdaa90d6a 118
babylonica 5:a50cdaa90d6a 119 if( wrote_length < 0 ){
babylonica 5:a50cdaa90d6a 120 va_end(arg);
babylonica 5:a50cdaa90d6a 121 error("Function vsprintf() was failed.");
babylonica 5:a50cdaa90d6a 122 return 0;
babylonica 5:a50cdaa90d6a 123 }
babylonica 5:a50cdaa90d6a 124
babylonica 5:a50cdaa90d6a 125 va_end(arg);
babylonica 8:7390f9bb28d3 126 AsyncSerial::write((uint8_t*)string_buffer, wrote_length);
babylonica 5:a50cdaa90d6a 127
babylonica 8:7390f9bb28d3 128 return wrote_length;
babylonica 5:a50cdaa90d6a 129 }
babylonica 5:a50cdaa90d6a 130
babylonica 5:a50cdaa90d6a 131 int AsyncSerial::write(const uint8_t *buffer, int length){
babylonica 5:a50cdaa90d6a 132 uint8_t temp;
babylonica 5:a50cdaa90d6a 133
babylonica 5:a50cdaa90d6a 134 if ( length < 1 ){
babylonica 5:a50cdaa90d6a 135 return 0;
babylonica 5:a50cdaa90d6a 136 }
babylonica 5:a50cdaa90d6a 137
babylonica 5:a50cdaa90d6a 138 for(uint32_t i = 0; i < length; i++){
babylonica 5:a50cdaa90d6a 139 temp = (uint8_t)buffer[i];
babylonica 8:7390f9bb28d3 140 fifo_tx.put(temp);
babylonica 5:a50cdaa90d6a 141 }
babylonica 5:a50cdaa90d6a 142
babylonica 5:a50cdaa90d6a 143 if( !Is_Serial_Sending ){
babylonica 5:a50cdaa90d6a 144 Is_Serial_Sending = true;
babylonica 5:a50cdaa90d6a 145 RawSerial::putc((int)fifo_tx.get());
babylonica 5:a50cdaa90d6a 146 }
babylonica 5:a50cdaa90d6a 147
babylonica 8:7390f9bb28d3 148 return 1;
babylonica 5:a50cdaa90d6a 149 }
babylonica 5:a50cdaa90d6a 150
babylonica 5:a50cdaa90d6a 151 void AsyncSerial::flush(void){
babylonica 5:a50cdaa90d6a 152 fifo_rx.clear();
babylonica 5:a50cdaa90d6a 153 return;
babylonica 5:a50cdaa90d6a 154 }
babylonica 5:a50cdaa90d6a 155
babylonica 5:a50cdaa90d6a 156 void AsyncSerial::wait(void){
babylonica 5:a50cdaa90d6a 157 while( fifo_rx.available() > 0 ){}
babylonica 5:a50cdaa90d6a 158 return;
babylonica 5:a50cdaa90d6a 159 }
babylonica 5:a50cdaa90d6a 160
babylonica 5:a50cdaa90d6a 161 void AsyncSerial::format(int bits, Parity parity, int stop_bits){
babylonica 5:a50cdaa90d6a 162 RawSerial::attach(NULL, RawSerial::TxIrq);
babylonica 5:a50cdaa90d6a 163 RawSerial::attach(NULL, RawSerial::RxIrq);
babylonica 5:a50cdaa90d6a 164
babylonica 5:a50cdaa90d6a 165 RawSerial::format(bits, parity, stop_bits);
babylonica 5:a50cdaa90d6a 166
babylonica 5:a50cdaa90d6a 167 RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
babylonica 5:a50cdaa90d6a 168 RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
babylonica 5:a50cdaa90d6a 169 }
babylonica 5:a50cdaa90d6a 170
babylonica 5:a50cdaa90d6a 171 void AsyncSerial::baud(int baudrate){
babylonica 5:a50cdaa90d6a 172 RawSerial::attach(NULL, RawSerial::TxIrq);
babylonica 5:a50cdaa90d6a 173 RawSerial::attach(NULL, RawSerial::RxIrq);
babylonica 5:a50cdaa90d6a 174
babylonica 5:a50cdaa90d6a 175 RawSerial::baud(baudrate);
babylonica 5:a50cdaa90d6a 176
babylonica 5:a50cdaa90d6a 177 RawSerial::attach(this, &AsyncSerial::ISR_TX, RawSerial::TxIrq);
babylonica 5:a50cdaa90d6a 178 RawSerial::attach(this, &AsyncSerial::ISR_RX, RawSerial::RxIrq);
babylonica 5:a50cdaa90d6a 179 }
babylonica 5:a50cdaa90d6a 180