tetete

Dependencies:   FIFO

Committer:
kanope_benny
Date:
Thu Jul 23 08:45:42 2020 +0000
Revision:
16:96935ea8f8b0
Parent:
15:278f7f125495
tetete

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