Fixed issues with NRF_BUSY between USB transactions

This commit is contained in:
Musa Mahmood 2025-03-22 06:48:32 -04:00
parent 44b5e30ded
commit 9f5d281a62
4 changed files with 196 additions and 32 deletions

View File

@ -16,6 +16,7 @@
#include <string.h>
//#include "usb_logging.h"
uint8_t ads1298_default_regs[] = {
ADS1298_REGDEFAULT_CONFIG1,

31
base/types.h Normal file
View File

@ -0,0 +1,31 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef s8 b8;
typedef s16 b16;
typedef s32 b32;
typedef float f32;
#define Create_String(s) ((String){.count=(sizeof(s)-1), .data=(s)})
typedef struct {
s32 count;
u8* data;
} String;
String create_string(char* data) {
s32 count = strlen(data);
String s = {count, data};
return s;
}

70
base/usb_logging.h Normal file
View File

@ -0,0 +1,70 @@
// Helper code for saving data to USB
// Global log buffer and usage:
static u8 log_buffer[4096];
static s32 m_log_usage = 0;
// Sending logs
static bool m_send_logs = 0;
static s32 m_log_buffer_send_index = 0;
static volatile bool tx_complete = true;
static bool full_tx_complete = false;
// Functions for adding to and resetting buffer:
#define LOG_MODE 0
// Mode 0: do not wrap around, do not append to buffer
// Mode 1: wrap around to beginning.
static void reset_log_buffer(void) {
// If debug, then memset buffer
memset(log_buffer, 0, sizeof(log_buffer));
m_log_usage = 0;
m_log_buffer_send_index = 0;
tx_complete = true;
full_tx_complete = false;
}
static void add_to_log_buffer(String str) {
// Bounds check the buffer:
if (m_log_usage + str.count >= sizeof(log_buffer)) {
// Failure!.. How to handle? Turn on LEDs?
return; // Do not add to buffer
}
memcpy(&log_buffer[m_log_usage], str.data, str.count);
m_log_usage += str.count;
}
// FOR STRING LITERALS ONLY
// #define JIIM_LOG_USB(strlit) add_to_log_buffer(Create_String(strlit))
// Dynamic strings
static u8 sprint_buffer[128];
static s32 sprint_length;
// #TODO: Inline
void add_format_string_to_log_buffer(void) {
// Replace null terminator with \n where required.
sprint_buffer[sprint_length] = '\n';
String s = {sprint_length+1, sprint_buffer};
add_to_log_buffer(s);
}
// #TODO: I need to append a \n here
#define JIIM_LOG(fmt, ...) \
sprint_length = sprintf(sprint_buffer, fmt, ##__VA_ARGS__); \
add_format_string_to_log_buffer()
/*
void send_usb_log(String str) {
// Zero buffer:
//memset(str.data, 0, NRF_DRV_USBD_EPSIZE);
// Copy string:
//memcpy(m_tx_buffer, str.data, str.count);
app_usbd_cdc_acm_write(&m_app_cdc_acm, str.data, str.count);
}
void log_usb(char* data) {
send_usb_log(create_string(data));
}
*/

126
main.c
View File

@ -1,8 +1,9 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
// What I want is to be able to send logs ONLY in log mode, so I need to keep a buffer of strings
// String logs[64], pointing to different strings.
// I just fucking hate strlen for this kind of thing
// If only there's a way to precompute string lengths.
// Nordic drivers and application code:
#include "nrf.h"
#include "nrf_drv_usbd.h"
#include "nrf_drv_clock.h"
@ -24,6 +25,13 @@
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
// **** jiim base code ****
#include "base/types.h"
#include "base/usb_logging.h"
// **** Sensor driver APIs ****
#include "ads1298.h"
/**
* @brief Enable power USB detection
*
@ -55,8 +63,7 @@ APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
CDC_ACM_COMM_EPIN,
CDC_ACM_DATA_EPIN,
CDC_ACM_DATA_EPOUT,
APP_USBD_CDC_COMM_PROTOCOL_AT_V250
);
APP_USBD_CDC_COMM_PROTOCOL_AT_V250);
#define READ_SIZE 1
@ -68,15 +75,11 @@ static bool m_send_flag = 0;
* @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t (headphones)
* */
static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
app_usbd_cdc_acm_user_event_t event)
{
app_usbd_cdc_acm_user_event_t event) {
app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
switch (event)
{
case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
{
switch (event) {
case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN: {
/*Setup first transfer*/
ret_code_t ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
m_rx_buffer,
@ -87,22 +90,26 @@ static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
break;
case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
tx_complete = true;
if (m_send_logs && full_tx_complete) { reset_log_buffer(); }
break;
case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
{
case APP_USBD_CDC_ACM_USER_EVT_RX_DONE: {
ret_code_t ret;
NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
do
{
do {
/*Get amount of data transfered*/
size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
NRF_LOG_INFO("RX: size: %lu char: %c", size, m_rx_buffer[0]);
if (m_rx_buffer[0] == 1) {
if (m_rx_buffer[0] == 0) {
m_send_flag = false;
m_send_logs = false;
}
if (m_rx_buffer[0] == 1) {
m_send_flag = true;
}
if (m_rx_buffer[0] == 2) {
m_send_flag = true;
m_send_logs = true;
}
/* Fetch data until internal buffer is empty */
@ -118,10 +125,8 @@ static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
}
}
static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
switch (event)
{
static void usbd_user_ev_handler(app_usbd_event_type_t event) {
switch (event) {
case APP_USBD_EVT_DRV_SUSPEND:
break;
case APP_USBD_EVT_DRV_RESUME:
@ -134,8 +139,7 @@ static void usbd_user_ev_handler(app_usbd_event_type_t event)
case APP_USBD_EVT_POWER_DETECTED:
NRF_LOG_INFO("USB power detected");
if (!nrf_drv_usbd_is_enabled())
{
if (!nrf_drv_usbd_is_enabled()) {
app_usbd_enable();
}
break;
@ -152,12 +156,34 @@ static void usbd_user_ev_handler(app_usbd_event_type_t event)
}
}
ads1298_info_t m_info;
static ble_exg_t m_exg;
// Global Data:
// Unfortunately I need to precompute these strlens manually :skull-emoji:
// String hello_string = {21, "Hello, I am a string\n"};
void Send_USB_Logs(void) {
if (!tx_complete) return;
// We should check that the USB data is
s32 bytes_remaining = m_log_usage - m_log_buffer_send_index;
if (bytes_remaining <= 0) {
full_tx_complete = true;
return;
}
app_usbd_cdc_acm_write(&m_app_cdc_acm, &log_buffer[m_log_buffer_send_index], 64);
m_log_buffer_send_index += 64;
tx_complete = false;
}
int main(void) {
ret_code_t ret;
static const app_usbd_config_t usbd_config = {
.ev_state_proc = usbd_user_ev_handler
};
// #TODO: remove logging maybe?
ret = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(ret);
@ -176,7 +202,7 @@ int main(void) {
ret = app_usbd_init(&usbd_config);
APP_ERROR_CHECK(ret);
NRF_LOG_INFO("USBD CDC ACM example started.");
// NRF_LOG_INFO("USBD CDC ACM example started.");
app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
ret = app_usbd_class_append(class_cdc_acm);
@ -186,20 +212,52 @@ int main(void) {
ret = app_usbd_power_events_enable();
APP_ERROR_CHECK(ret);
} else {
NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
JIIM_LOG("No USB power detection enabled\r\nStarting USB now");
app_usbd_enable();
app_usbd_start();
}
// Throughput testing:
memset(m_tx_buffer, 0x41/*"A"*/, NRF_DRV_USBD_EPSIZE);
size_t size = NRF_DRV_USBD_EPSIZE;
//m_send_flag = true;
reset_log_buffer(); // initialize log buffer with `A`
JIIM_LOG("Hello world!");
JIIM_LOG("HELLO WORLD %d FORMATTED", 2);
JIIM_LOG("sizeof(log_buffer): %d", sizeof(log_buffer));
// TODO#: ads1298_init_gpio().
// TODO#: Push logs over USB.
JIIM_LOG("Hello, I am a string2");
JIIM_LOG("Hello, I am a string2");
JIIM_LOG("Hello, I am a string2");
JIIM_LOG("Hello, I am a string2");
JIIM_LOG("Hello, I am a string2");
JIIM_LOG("Hello, I am a string2");
// String hello_string = Create_String("Hello, I am a string2\n");
// add_to_log_buffer(hello_string);
// add_to_log_buffer(hello_string);
// add_to_log_buffer(hello_string);
ads1298_initialize(&m_exg, &m_info);
if (m_info.nChs > 0) {
// #TODO: replace with sprintf()...
JIIM_LOG("Number of channels available: %d", m_info.nChs);
} else {
JIIM_LOG("ERROR: ADS129x NOT DETECTED!");
}
//memset(m_tx_buffer, 0x41/*"A"*/, NRF_DRV_USBD_EPSIZE);
//size_t size = NRF_DRV_USBD_EPSIZE;
//s32 count = 0;
while (true) {
while (app_usbd_event_queue_process()) {/* Nothing to do */}
if(m_send_flag) {
if (m_send_flag) {
}
if (m_send_logs) {
// static int frame_counter;
// size_t size = sprintf(m_tx_buffer, "Hello USB CDC FA demo: %u\r\n", frame_counter);
@ -207,7 +265,11 @@ int main(void) {
// ret = app_usbd_cdc_acm_write(&m_app_cdc_acm, m_tx_buffer, size);
// if (ret == NRF_SUCCESS) { ++frame_counter; }
app_usbd_cdc_acm_write(&m_app_cdc_acm, m_tx_buffer, size);
// app_usbd_cdc_acm_write(&m_app_cdc_acm, m_tx_buffer, size);
// send_usb_log(hello_string);
//log_usb("TestTestTest");
// count += 1;
Send_USB_Logs();
}
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());