Tool/software:
I have modified the Data Stream Project to enable Peripheral + Central roles and flashed it onto a CC2340R5 device. Another CC2340R5 device is running the unmodified Data Stream Project in the Peripheral role. Both devices establish a BLE connection successfully.
- UART Communication at 115200 Baud Rate: Data transmission and reception between the devices via UART (central-to-peripheral) work flawlessly.
- UART Communication at 9600 Baud Rate: After a few seconds of continuous data transmission and reception, the central device hangs.
The end device attached to the BLE module supports only a 9600 baud rate, making it essential for the communication to work seamlessly at this baud rate.
Central Device Setup and Code:
I have attached screenshots showing:
- The setup for the Central Device.
- The code handling data read/write operations for the Data Stream Service's characteristics.
Urgent Support Needed:
- Potential Issue: It appears the central device's UART interface or buffer handling may not handle slower baud rates (9600) effectively during high-frequency data transmission.
- Request for Suggestions: Please suggest modifications to the code, UART configurations, or flow control mechanisms to resolve this issue.
// Writing UART data to the peripheral from central device void sendUartToDataIn() { bStatus_t status = SUCCESS; uint16_t offset = 0; uint16_t mtuSize = 200; uint16_t headerSize = 3; // Approximate GATT header size uint16_t maxChunkSize = mtuSize - headerSize; uint16_t dataLength = BufferSize;//strlen((char*)uartReadBuffer); // Total data length attWriteReq_t req; char buffer[40]; // Loop until all data is sent while (offset < dataLength) { // Determine the chunk size for this iteration uint16_t chunkSize = (dataLength - offset > maxChunkSize) ? maxChunkSize : (dataLength - offset); bool chunkSent = false; // Retry sending the current chunk until successful while (!chunkSent) { // Allocate memory for the write request req.pValue = GATT_bm_alloc(0x0, ATT_WRITE_REQ, chunkSize, NULL); if (req.pValue != NULL) { // Set up the request details req.handle = 0x25; // Set handle for DataIn characteristic req.len = chunkSize; req.sig = FALSE; req.cmd = TRUE; // Using Write Command (Write without Response) // Copy data to the request buffer memcpy(req.pValue, uartReadBuffer + offset, chunkSize); // Attempt to send the data chunk // vTaskDelay(pdMS_TO_TICKS(5)); status = GATT_WriteNoRsp(0x0, &req); // vTaskDelay(pdMS_TO_TICKS(10)); if (status == SUCCESS) { chunkSent = true; // Exit retry loop on success offset += chunkSize; // Move to the next chunk // Only clear sent chunk data in `uartReadBuffer` if desired, or keep it until the entire buffer is read } else { // Free the allocated buffer on failure GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); vTaskDelay(pdMS_TO_TICKS(10)); } } else { // Memory allocation failed; log error and retry after delay UART2_write(uart, "Memory allocation failed\n", strlen("Memory allocation failed\n"), NULL); vTaskDelay(pdMS_TO_TICKS(50)); // Delay before retrying allocation } } } // Reset UART read buffer for new data memset(uartReadBuffer, 0, UART_MAX_READ_SIZE); UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, NULL); // Set up UART for the next data reception } // Reading data at central form peripheral and writing it to UART case ATT_HANDLE_VALUE_NOTI: { // Handle incoming notification data uint16_t handle = gattMsg->msg.handleValueNoti.handle; uint8_t *pValue = gattMsg->msg.handleValueNoti.pValue; uint16_t len = gattMsg->msg.handleValueNoti.len; uint16_t bytesWritten = 0; uint16_t chunkSize = 200; // Define the max UART write size (200 bytes in this case) // snprintf(buffer, sizeof(buffer), "Size : %d\n", len); // UART2_write(uart, buffer, strlen(buffer), NULL); // Loop to write data to UART in manageable chunks while (bytesWritten < len) { uint16_t bytesToWrite = (len - bytesWritten) > chunkSize ? chunkSize : (len - bytesWritten); // Write a chunk to UART UART2_write(uart, pValue + bytesWritten, bytesToWrite, NULL); // vTaskDelay(pdMS_TO_TICKS(5)); // Update the bytes written bytesWritten += bytesToWrite; GATT_bm_free(&gattMsg->msg,gattMsg->method); }
Thanks for your support.