Sampling analog signals on the ESP32 at different rates using I2S - sampling

I need to read high freq. analog signal data from one ADC1 channel and read low freq. data from other ADC1 pins.
I use I2S for the high freq. data read, which runs perfectly, but as soon as I2S is configured all other ADC1 pins read 4095 only.
What is the correct handling for my demands?
Can't use ADC2 because of wifi.
Code excerpt:
void readerTask(void *param) {
size_t bytesRead = 0;
while(true) {
// Get ADC data from DMA buffer
i2s_read(I2S_NUM_0, buf, sizeof(buf), &bytesRead, portMAX_DELAY);
// prevent the data getting corrupted
i2s_adc_disable(I2S_NUM_0);
/* data processing */
delay(30);
i2s_adc_enable(I2S_NUM_0);
}
}
void setup() {
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
.sample_rate = 8000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S_LSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = 1024,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0};
//install and start i2s driver
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
//init ADC pad
i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
// enable the ADC
i2s_adc_enable(I2S_NUM_0);
// start a task to read samples from I2S
xTaskCreatePinnedToCore(readerTask, "Reader Task", 2048, NULL, 1, NULL, 0);
}
void loop()
{
EVERY_N_MILLISECONDS( 100 ) {
// read low freq. data
int test = analogRead(34); // will always read 4095
}
}

you need:
i2s_adc_disable(I2S_NUM_0);
i2s_driver_uninstall(I2S_NUM_0);
analogRead(34);
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
i2s_adc_enable(I2S_NUM_0);

Related

LIS3DSH x y z axis is incorrect

I have tried to work with LIS3DSH sensor using SPI protocol. I applied procedures on STM32L0 Discovery kit LoRa. But it didn't work.
My problem is The LIS3DSH x-y-z-axis output value is invalid and the value is not changed. No matter which direction I rotate the device but I check ID of LIS3DSH is correct ( 0011 1111)
Component
STM32L0 Discovery kit LoRa
LIS3DSH (https://www.amazon.com/LIS3DSH-High-Resolution-Three-axis-Accelerometer-Triaxial/dp/B07QS5D9K9/ref=sr_1_4?dchild=1&keywords=LIS3DSH&qid=1615701212&sr=8-4)
init SPI2
...
void HW_SPI2_Init(void)
{
/*##-1- Configure the SPI2 peripheral */
/* Set the SPI parameters */
hspi2.Instance = SPI2;
hspi2.Init.BaudRatePrescaler = SpiFrequency(10000000);
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; // CPOL = 0
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE; // CPHA = 1
// hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; // CPHA = 0
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
// hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCPolynomial = 10;
SPI2_CLK_ENABLE(); // Enable clock for SPI 2
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the SPI GPIOs */
HW_SPI2_IoInit();
}
...
initialization LIS3DSH
void HW_SPI2_IoInit(void)
{
GPIO_InitTypeDef initStruct = {0};
initStruct.Mode = GPIO_MODE_AF_PP;
initStruct.Pull = GPIO_PULLUP ;
initStruct.Speed = GPIO_SPEED_FAST;
initStruct.Alternate = SPI2_AF;
HW_GPIO_Init(LIS3DH_SCLK_PORT, LIS3DH_SCLK_PIN, &initStruct);
HW_GPIO_Init(LIS3DH_MISO_PORT, LIS3DH_MISO_PIN, &initStruct);
HW_GPIO_Init(LIS3DH_MOSI_PORT, LIS3DH_MOSI_PIN, &initStruct);
initStruct.Mode = GPIO_MODE_OUTPUT_PP;
initStruct.Pull = GPIO_NOPULL;
HW_GPIO_Init(LIS3DH_NSS_PORT, LIS3DH_NSS_PIN, &initStruct);
HW_GPIO_Write(LIS3DH_NSS_PORT, LIS3DH_NSS_PIN, 1);
}
CS ON or OFF
void HW_SPI2_CS_ON (void)
{
HW_GPIO_Write(LIS3DH_NSS_PORT, LIS3DH_NSS_PIN, 0);
}
void HW_SPI2_CS_OFF (void)
{
HW_GPIO_Write(LIS3DH_NSS_PORT, LIS3DH_NSS_PIN, 1);
}
Read ID
uint8_t LIS3DSH_DH_CHECK_ID (void)
{
uint8_t addr = LIS3Dx_WHO_AM_I_ADDR | LIS3Dx_READ;
HW_SPI2_CS_ON();
HW_SPI2_SPI_1byte_Write_and_Read(addr);
HW_SPI2_CS_OFF();
UsingTypeint_LIS3DSH_DH.ID = au8BufferRead_SPI2[0];
return UsingTypeint_LIS3DSH_DH.ID;
}
Read X axis ( only) (Incorrect)
void LIS3DSH_DH_GET_XYZ (void)
{
uint8_t addr = LIS3Dx_OUT_X_L_ADDR | LIS3Dx_READ ;
HW_SPI2_CS_ON();
HW_SPI2_SPI_1byte_Write_and_Read(addr);
HW_SPI2_CS_OFF();
addr = LIS3Dx_OUT_X_H_ADDR | LIS3Dx_READ ;
HW_SPI2_CS_ON();
HW_SPI2_SPI_1byte_Write_and_Read(addr);
HW_SPI2_CS_OFF();
}
HW_SPI2_1byte_Write_and_Read
bool HW_SPI2_SPI_1byte_Write_and_Read(uint8_t u8Address)
{
if(HAL_SPI_Transmit(&hspi2, (uint8_t *)&u8Address, 1, HAL_MAX_DELAY) == HAL_OK){
if(HAL_SPI_Receive(&hspi2, (uint8_t *)&au8BufferRead_SPI2[0], 1, HAL_MAX_DELAY) == HAL_OK){
return true;
}
}
return false;
}
Signal
output X L
output X H
Am not sure if you are using the state machine and register accesses of the LIS3DSH accurately.
As a reference see the register mapping in https://github.com/STMicroelectronics/stm32-lis3dsh/blob/main/lis3dsh.h (start at line 95) and the code in https://github.com/STMicroelectronics/stm32-lis3dsh/blob/main/lis3dsh.c
The function that actually writes and reads the SPI bus is void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) and void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
This is defined in https://documentation.help/STM32F4-Discovery-BSP/stm32f4__discovery_8c_source.html at lines 560 and 594
In void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) the actual SPI write is done at lines 574 to 582
00574 SPIx_WriteRead(WriteAddr);
00577 while(NumByteToWrite >= 0x01)
00578 {
00579 SPIx_WriteRead(*pBuffer);
00580 NumByteToWrite--;
00581 pBuffer++;
00582 }
(as always with SPI first activate register, then write data...)
In SPIx_WriteRead(WriteAddr); the WriteAddr is taken from the register mapping in https://github.com/STMicroelectronics/stm32-lis3dsh/blob/main/lis3dsh.h, start at line 9
(the above code by STM for the LIS3DSH is nice work, this implementation is very clean and structured)
The datasheet of the LIS3DSH is in https://www.st.com/resource/en/datasheet/lis3dsh.pdf, the reference manual is far more detailed it is in https://www.st.com/resource/en/application_note/dm00026768-lis3dsh-3axis-digital-output-accelerometer-stmicroelectronics.pdf it also includes explanation of the state machines, that have to be configured before the device works

How to read FIFO Data from MAX30100 using STM32F4

i'm monitorating heart rate by the MAX30100(https://img.filipeflop.com/files/download/Datasheet_MAX30100.pdf) using the MCU STM32F4. I'm trying read the IR and RED data from the FIFO, but all returns are ZERO. The method MAX30100_Get_Num_Samples() returns 8. I modeled the code using the pseudo code from datasheet of MAX30100. I tried several solutions for my problem but doesn't work. I dont know if i'm following the right way to get data from the FIFO. My code:
I2C_HandleTypeDef hi2c3; //i2c used
uint16_t RED[50] = {0}, IR[50] = {0}; //buffers for RED and IR
uint8_t buffOximeter[5];
char buffer[32];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C3_Init();
MX_USB_DEVICE_Init();
MAX30100_Init();
while (1)
{
uint8_t numSamples = MAX30100_Get_Num_Samples();
MAX30100_Read_HeartBeat(numSamples);
for(int i = 0; i < numSamples; i++)
{
sprintf(buffer, "Amostra %d: %d / %d\n\r", i , IR[i], RED[i]);
CDC_Transmit_FS((char*)buffer, 50);
}
}
}
static void MX_I2C3_Init(void)
{
hi2c3.Instance = I2C3;
hi2c3.Init.ClockSpeed = 400000;
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c3.Init.OwnAddress1 = 0;
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c3.Init.OwnAddress2 = 0;
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
{
Error_Handler();
}
}
void MAX30100_Init(void)
{
HAL_I2C_Mem_Write(&hi2c3,0xAE,0x06,1,0x02,1,1000); //set heart rate mode
HAL_I2C_Mem_Write(&hi2c3,0xAE,0x09,1,0xFF,1,1000); //i50 ledCurrent
uint8_t sr = 0x01, pw = 0x3;
HAL_I2C_Mem_Write(&hi2c3,0xAE,0x07,1,(sr<<2)|pw,1,1000); //sr100, pw1600
HAL_Delay(50);
}
void MAX30100_Read_HeartBeat(uint8_t num)
{
for(int i=0;i<num;i++)
{
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAE,1,1000); //adress + write mode
HAL_I2C_Master_Transmit(&hi2c3,0x57,0x02,1,1000); //send fifo_wr_ptr
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAF,1,1000); //adress + read mode
uint8_t data;
HAL_I2C_Master_Receive(&hi2c3,0x57,&data,1,1000); //read fifo_wr_ptr
HAL_Delay(100); //STOP
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAE,1,1000); //adress + write mode
HAL_I2C_Master_Transmit(&hi2c3,0x57,0x05,1,1000); //send adress fifo data
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAF,1,1000); //adress + read mode
HAL_I2C_Mem_Read(&hi2c3,0x57,0x05,1,&buffOximeter,4,1000); //read fifo data
IR[i] = (buffOximeter[0] << 8) | buffOximeter[1];
RED[i] = (buffOximeter[2] << 8) | buffOximeter[3];
HAL_Delay(100); //STOP
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAE,1,1000); //adress + write mode
HAL_I2C_Master_Transmit(&hi2c3,0x57,0x04,1,1000); //send adress fifo_rd_ptr
HAL_Delay(100);
}
}
int MAX30100_Get_Num_Samples(void)
{
uint8_t wrPtr, rdPtr;
HAL_I2C_Mem_Read(&hi2c3,0x57,0x02,1,&wrPtr,1,1000);
HAL_Delay(50);
HAL_I2C_Mem_Read(&hi2c3,0x57,0x04,1,&rdPtr,1,1000);
HAL_Delay(50);
return (abs( 16 + wrPtr - rdPtr ) % 16);
}
connect the interrupt output pin (pin 13 active low) to an interrupt control pin on the cpu.
setup your cpu to be interrupted when the device indicates some event ready
Then, still in your interrupt handler function, read register 0 and determine which data is ready,.
the referenced datasheet, approx page 14 lists the order of communication events needed. Note that the posted code is NOT following the specified order of communication events
page 16, register 7, indicates how to initialize the SPO2 for the desired mode of operation

Old data is read from 485 port in Linux environment

I am reading data continuously from RS 485 port by C program from a device by the following code. For some reason, obtained data is not the latest data in the device. The changes of data is not reflected when I read data, it gives me old value. After couple of minutes, I get the changed value (since I am continuously reading). If I read the same device by Pytty I get the updated value immediately. So there must be some problem in my code though I couldn't figure it out. Any help would be great!
static int load_serial_port(char *port) //port is 485, port="/dev/ttyS2"/
{
int fd = 0;
fd = open (port, O_RDWR);
if (fd < 0) {
log_error("SerialPort opening failed.");
return -1;
}
struct serial_rs485 rs485conf;
/* Enable RS485 mode: */
rs485conf.flags |= SER_RS485_ENABLED;
/* Set logical level for RTS pin equal to 1 when sending: */
rs485conf.flags |= SER_RS485_RTS_ON_SEND;
/* or, set logical level for RTS pin equal to 0 when sending: */
rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
/* Set logical level for RTS pin equal to 1 after sending: */
rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
/* or, set logical level for RTS pin equal to 0 after sending: */
rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
/* Set rts delay before send, if needed: */
rs485conf.delay_rts_before_send = 0;
/* Set rts delay after send, if needed: */
rs485conf.delay_rts_after_send = 0;
/* Set this flag if you want to receive data even whilst sending data */
//rs485conf.flags |= SER_RS485_RX_DURING_TX;
if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
log_error("SerialPort config failed.");
return -1;
}
struct termios option;
tcgetattr(fd, &option);
cfsetospeed(&option, B9600); /* baud rate */
option.c_cflag &= ~PARENB; /* no parity */
option.c_cflag &= ~CSTOPB; /* 1 stop bit */
option.c_cflag &= ~CSIZE;
option.c_cflag |= CS8 | CLOCAL; /* 8 bits */
option.c_lflag = ICANON; /* canonical mode */
option.c_oflag &= ~OPOST; /* raw output */
tcsetattr(fd, TCSANOW, &option); /* apply the settings */
tcflush(fd, TCOFLUSH);
log_debug("SerialPort loaded fd %d", fd);
return fd;
}
Following is the read function
static void port_read(port_t *s)
{
uint8_t rxBuffer[20];
char portString[20] = "";
double value = 0.0;
if(s->serialPortFd > 0) {
int amount = read(s->serialPortFd, rxBuffer, 100);
int i = 0;
int charindex = 0;
if(amount > 1 ) {
for (i = 0; i< amount; i++) {
if ( isdigit( rxBuffer[i]) || ( (char)rxBuffer[i] == '-' || (char)rxBuffer[i] == '.') ) {
portString[charindex] = (char)rxBuffer[i];
charindex++;
}
}
portString[charindex] = '\0';
sscanf(portString, "%lf", &value); //value is same ???
}
}
memset(rxBuffer, 0, sizeof rxBuffer);
}
Sometimes I feel experts post, comment in this forum without reading questions carefully. I found the correct solution after reading and doing more experiment and that I was expecting to save time.
My reading goes fine with the following changes in my code:
int amount = read(s->serialPortFd, rxBuffer, 20);
tcflush(s->serialPortFd, TCIOFLUSH);
I was missing flashing the file.

Extra bytes sent when communicating at 115200 baud over USB serial (WinAPI)

I'm attempting to send serial messages over USB to an Arduino Uno, using raw WinAPI commands. When using baud rates less than 115200, it works perfectly fine. However, when I send at 115200 baud, two extra bytes are sent prefixing the data I sent, but ONLY for the first message after connecting to the Arduino. For example, if I connect to the Arduino and send two bytes, "Hi", the Arduino receives "ððHi". If I send "Hi" again, the Arduino receives "Hi" like it should. (The extra bytes are usually ð (0xF0), but not always.)
I know that my computer and the Arduino are capable of communicating at 115200 baud, because other programs such as avrdude and the Arduino IDE's serial monitor can do it fine.
I have tried clearing the RX and TX buffers on both sides and also messing the DCB settings, with no effect. Does anyone know what might be causing this?
Thanks!
Here is my code to reproduce the problem:
Computer side:
#include <Windows.h>
int main()
{
// Open device as non-overlapped
HANDLE device = CreateFile(L"COM6",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Make sure the device is valid
if(device == INVALID_HANDLE_VALUE)
return 0;
DCB dcb;
if(!GetCommState(device, &dcb))
return 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fNull = 0;
dcb.BaudRate = CBR_115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if(!SetCommState(device, &dcb))
return 0;
COMMTIMEOUTS Timeouts = { 0 };
Timeouts.ReadTotalTimeoutConstant = 1000;
Timeouts.WriteTotalTimeoutConstant = 1000;
if(!SetCommTimeouts(device, &Timeouts))
return 0;
char *buf = "abcdef";
DWORD written;
WriteFile(device, buf, 6, &written, NULL);
DWORD read;
char inbuf[100];
ReadFile(device, inbuf, 100, &read, NULL);
// When I get the result inbuf, it has 8 bytes: {0xF0, 0xF0, a, b, c, d, e, f}
// Doing a 2nd set of Write/ReadFile, with the same message, gives the correct response
return 0;
}
Arduino side:
void setup()
{
Serial.begin(115200);
}
void loop()
{
if(Serial.available())
Serial.write(Serial.read());
}

STM32F3 UART receive interrupt data hangs after receveing first set of data from Arduino

Being beginner in ARM microcontroller, I am using STM32F3 Discovery Kit to communicate with Arduino. I use EWARM for coding and Putty, for serial terminal.
I did an echo test on stm32f3 UART2. The data is displayed on the serial terminal correctly, so I confirmed the uart communication is working as expected.
Then, I try to transfer data from arduino to stm32f3 but the uart on stm32f3 hangs after the first set of data (the first set of data is displayed with correctly with some garbage character included). I have been stuck for quite some time now.
Below is the coding I used for UART. The baudrate is 9600. Can anyone help?
#define LINEMAX 15 // Maximal allowed/expected line length
volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NUL
volatile int line_valid = 0;
/**
* #brief Initialize USART2 for PD6 (USART2_RX) and PD5 (USART2_TX)
* used for receiving mouse data from arduino
* #param baudrate; by default is 9600
* #retval None
* link: http://eliaselectronics.com/stm32f4-usart-example-with-interrupt/
*/
void init_USART2(int baudrate)
{
USART_InitTypeDef USART_InitStructure; // this is for the GPIO pins used as TX and R
GPIO_InitTypeDef GPIO_InitStructure; // this is for the USART1 initilization
NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
/* Configure USART1 pins: Rx and Tx ----------------------------*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7);
/* Configure USART1 pins: --------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_DeInit(USART2);
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2,&USART_InitStructure);
USART_Cmd(USART2, ENABLE);
/* Here the USART2 receive interrupt is enabled
* and the interrupt controller is configured
* to jump to the USART2_IRQHandler() function
* if the USART2 receive interrupt occurs
*/
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // we want to configure the USART1 interrupts
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // this sets the priority group of the USART1 interrupts
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART2 interrupts are globally enabled
NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff
// finally this enables the complete USART2 peripheral
USART_Cmd(USART2, ENABLE);
}
void serial_prints (USART_TypeDef* USARTx, volatile char *buffer)
{
/* transmit till NULL character is encountered */
while(*buffer)
{
USART_SendData(USARTx, *buffer++);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
delay_us(5);
}
}
void USART2_IRQHandler(void)
{
static char rx_buffer[LINEMAX]; // Local holding buffer to build line
static int rx_index = 0;
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received character?
{
char rx = USART_ReceiveData(USART2);
if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice?
{
if (rx_index != 0) // Line has some content?
{
memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer
line_buffer[rx_index] = 0; // Add terminating NUL
line_valid = 1; // flag new line valid for processing
serial_prints(USART2, rx_buffer);
rx_index = 0; // Reset content pointer
}
}
else
{
if (rx_index == LINEMAX) // If overflows pull back to start
rx_index = 0;
rx_buffer[rx_index++] = rx; // Copy to buffer and increment
}
}
}
For arduino, I use the following code and the data is displayed correctly in the serial terminal:
printf("%d, %d \n", X, Y);
You add the terminating null character to line_buffer, but then you pass rx_buffer to the function serial_prints:
line_buffer[rx_index] = 0; // Add terminating NUL
line_valid = 1; // flag new line valid for processing
serial_prints(USART2, rx_buffer);
rx_index = 0; // Reset content pointer
This has the consequence that you will iterate past the length of buffer in the function serial_prints because buffer will not be null terminated:
while(*buffer)
{
USART_SendData(USARTx, *buffer++);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
delay_us(5);
}
On a side-node, you should also use interrupts to send your characters (TX Empty interrupts). Actively waiting in the interrupt service routine for UART characters to be sent will considerably slow-down your program.

Resources