Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UDP Debug in Sming and SmingRTOS #138

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sming/sming/include/sming_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@
#include "sming/include/sming_config.h"

#include "sming/system/m_printf.h"
#include "sming/system/udpDebug.h"

#define __ESP8266_EX__ // System definition ESP8266 SOC
#define __UDP_DEBUG__
//#define __SERIAL_DEBUG__

//#define IRAM_ATTR __attribute__((section(".iram.text")))
#define __forceinline __attribute__((always_inline)) inline
#define STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))
#define STORE_ATTR __attribute__((aligned(4)))

//#undef assert
#define debugf(fmt, ...) m_printf(fmt"\r\n", ##__VA_ARGS__)
#if defined(__SERIAL_DEBUG__)
#define debugf(fmt, ...) m_printf(fmt"\r\n", ##__VA_ARGS__)
#elif defined(__UDP_DEBUG__)
#define debugf(fmt, ...) m_printf_udp(fmt"\r\n", ##__VA_ARGS__)
#endif
//#define assert(condition) if (!(condition)) SYSTEM_ERROR("ASSERT: %s %d", __FUNCTION__, __LINE__)
#define SYSTEM_ERROR(fmt, ...) printf("ERROR: " fmt "\r\n", ##__VA_ARGS__)

Expand Down
98 changes: 98 additions & 0 deletions sming/sming/system/udpDebug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Code to redirect both os_printf and debugf to UDP connection.
Created for use with Sming and SmingRTOS
Padelis Floudas 2016
*/

#include "udpDebug.h"
#include "../include/sming_global.h"
#include "../core/stringconversion.h"
#include "../include/lwip_includes.h"

#define debugPort 5000
#define DBG_BUFFER_LEN 256
#define MPRINTF_BUF_SIZE 256

char dbgBuffer[DBG_BUFFER_LEN];
uint8_t dbg_BufferLen = 0;

/*
tx_udp_char

This function is for use with os_install_putc1();
The original serial function sends each character separately.
To make this work with UDP connection i have to add each character to the char array
and when the NULL char is received, i send the packet.
*/

void tx_udp_char(char c)
{
dbgBuffer[dbg_BufferLen++] = c; // add the character to the array
// and increase the index

// check for NULL character and send
if(((c == 0x0A) && (dbg_BufferLen > 1)) || (dbg_BufferLen == DBG_BUFFER_LEN))
{
struct udp_pcb *pcb;
struct pbuf *p;
struct ip_addr dst;

pcb = udp_new();

// if there was an error creating the pcb then return
if(pcb == NULL) {
return;
}
p = pbuf_alloc(PBUF_TRANSPORT, dbg_BufferLen, PBUF_RAM);
memcpy(p->payload, dbgBuffer, dbg_BufferLen);
IP4_ADDR(&dst,192,168,4,255);
udp_sendto(pcb, p, &dst, debugPort);
pbuf_free(p);

dbg_BufferLen = 0;
}
}

/*
m_printf_udp

This fuction replaces the Sming m_printf() function
It is used in the same way as in Sming
*/
int m_printf_udp(const char *fmt, ...)
{
char buf[MPRINTF_BUF_SIZE], *tmp;
va_list args;
struct udp_pcb *pcb;
struct pbuf *p;
struct ip_addr dst;

va_start(args, fmt);
m_vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);

// find the null character
tmp = buf;
int len = 0;
while (*tmp)
{
tmp++;
len++;
}
// len holds the size of the string that we want to send.

pcb = udp_new();

// if there was an error creating the pcb then return
if(pcb == NULL) {
return 0;
}

p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); // allocate memory for the buffer
memcpy(p->payload, buf, len); // copy data to buffer
IP4_ADDR(&dst,192,168,4,255); // create IP address
udp_sendto(pcb, p, &dst, debugPort); // broadcast message
pbuf_free(p); // free memory

return 1;
}
11 changes: 11 additions & 0 deletions sming/sming/system/udpDebug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#ifdef __cplusplus
extern "C" {
#endif

void tx_udp_char(char c);
int m_printf_udp(const char *fmt, ...);

#ifdef __cplusplus
}
#endif