Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 6 months ago.
Unable to Use function attach for passing function and interrupt request for serial Communication
.cpp code
include the mbed library with this snippet
#include "mbed.h"
#include "DataAnalysis.h"
#include <inttypes.h>
char buffer_size = 255;
SerialData::SerialData(PinName Tx, PinName Rx):
_serial_comm(Tx,Rx)
{
_serial_comm.baud(115200);
}
void SerialData::Serial_Comm_init(void)
{
// Setup a serial interrupt function to receive data
_serial_comm.attach(&Rx_interrupt, SerialData::RxIrq);
// Setup a serial interrupt function to transmit data
_serial_comm.attach(&Tx_interrupt, SerialData::TxIrq);
}
// Interupt Routine to read in data from serial port
void SerialData::Rx_interrupt(void)
{
// Loop just in case more than one character is in UART's receive FIFO buffer
// Stop if buffer full
while ((_serial_comm.readable()) || (((rx_in + 1) % buffer_size) == rx_out))
{
rx_buffer[rx_in] = _serial_comm.getc();
// Uncomment to Echo to USB serial to watch data flow
// monitor_device.putc(rx_buffer[rx_in]);
rx_in = (rx_in + 1) % buffer_size;
}
return;
}
// Interupt Routine to write out data to serial port
void SerialData::Tx_interrupt(void)
{
// Loop to fill more than one character in UART's transmit FIFO buffer
// Stop if buffer empty
while ((_serial_comm.writeable()) && (tx_in != tx_out))
{
_serial_comm.putc(tx_buffer[tx_out]);
tx_out = (tx_out + 1) % buffer_size;
}
return;
}
// Copy tx line buffer to large tx buffer for tx interrupt routine
void SerialData::send_line(void)
{
int i;
char temp_char;
bool empty;
i = 0;
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USART1_IRQn);
empty = (tx_in == tx_out);
while ((i==0) || (tx_line[i-1] != '\n'))
{
// Wait if buffer full
if (((tx_in + 1) % buffer_size) == tx_out)
{
// End Critical Section - need to let interrupt routine empty buffer by sending
NVIC_EnableIRQ(USART1_IRQn);
while (((tx_in + 1) % buffer_size) == tx_out)
{
}
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USART1_IRQn);
}
tx_buffer[tx_in] = tx_line[i];
i++;
tx_in = (tx_in + 1) % buffer_size;
}
if (_serial_comm.writeable() && (empty))
{
temp_char = tx_buffer[tx_out];
tx_out = (tx_out + 1) % buffer_size;
// Send first character to start tx interrupts, if stopped
_serial_comm.putc(temp_char);
}
// End Critical Section
NVIC_EnableIRQ(USART1_IRQn);
return;
}
// Read a line from the large rx buffer from rx interrupt routine
void SerialData::read_line(void)
{
int i;
i = 0;
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USART1_IRQn);
// Loop reading rx buffer characters until end of line character
while ((i==0) || (rx_line[i-1] != '\r')) {
// Wait if buffer empty
if (rx_in == rx_out) {
// End Critical Section - need to allow rx interrupt to get new characters for buffer
NVIC_EnableIRQ(USART1_IRQn);
while (rx_in == rx_out) {
}
// Start Critical Section - don't interrupt while changing global buffer variables
NVIC_DisableIRQ(USART1_IRQn);
}
rx_line[i] = rx_buffer[rx_out];
i++;
rx_out = (rx_out + 1) % buffer_size;
}
// End Critical Section
NVIC_EnableIRQ(USART1_IRQn);
rx_line[i-1] = 0;
return;
}
Here Is my header .h file
#ifndef DataAnalysis_H_
#define DataAnalysis_H_
#include "mbed.h"
#include <inttypes.h>
/** Digole Serial LCD/OLED Library
* www.digole.com/index.php?productID=535
*
* Includes Arduino Print class member functions
*/
class SerialData {
public:
/** Create a new USART interface
*
* @param TX is the pin for SERIAL_TX
* @param RX is the pin for SERIAL_RX
*/
SerialData(PinName Tx, PinName Rx);
// Interupt Routine to read in data from serial port
void Rx_interrupt(void);
// Interupt Routine to write out data to serial port
void Tx_interrupt(void);
// Copy tx line buffer to large tx buffer for tx interrupt routine
void send_line();
// Read a line from the large rx buffer from rx interrupt routine
void read_line();
// Serial Tranmit and receive Init
void Serial_Comm_init(void);
private:
Serial _serial_comm;
// Circular buffers for serial TX and RX data - used by interrupt routines
// might need to increase buffer size for high baud rates
char tx_buffer[255];
char rx_buffer[255];
// Circular buffer pointers
// volatile makes read-modify-write atomic
volatile int tx_in;
volatile int tx_out;
volatile int rx_in;
volatile int rx_out;
// Line buffers for sprintf and sscanf
char tx_line[80];
char rx_line[80];
};
#endif
Heading 1
I am using STm32l053r8 board and programming through Mbed there is problem with USART function I get an error as
<<quote>>
Error: Class "SerialData" has no member "RxIrq" in "DataAnalysis/DataAnalysis.cpp", Line: 16, Col: 64
<<quote>>
<<quote>>
Error: No instance of overloaded function "mbed::Serial::attach" matches the argument list in "DataAnalysis/DataAnalysis.cpp", Line: 16, Col: 19
<<quote>>
<<quote>>
Error: Class "SerialData" has no member "TxIrq" in "DataAnalysis/DataAnalysis.cpp", Line: 18, Col: 64
<<quote>>
<<quote>>
Error: No instance of overloaded function "mbed::Serial::attach" matches the argument list in "DataAnalysis/DataAnalysis.cpp", Line: 18, Col: 19
<<quote>>
What is the error and how can i fix it? I have used Serial::RxIrq but didnot work out. I posted the code for more clarity. i get the same errors again
1 Answer
9 years, 6 months ago.
replace RxIrq with Serial::RxIrq when you attach to the interrupt. Same for the Tx.