Skip to content

Instantly share code, notes, and snippets.

@a-sakharov
Created October 7, 2025 13:39
Show Gist options
  • Select an option

  • Save a-sakharov/576a50e1a47e87902071a585067a591b to your computer and use it in GitHub Desktop.

Select an option

Save a-sakharov/576a50e1a47e87902071a585067a591b to your computer and use it in GitHub Desktop.
Simple bitbag implementation of tx-only uart
#define DEBUG_BITBANG_LOGGING 1
#if defined(DEBUG_BITBANG_LOGGING) && DEBUG_BITBANG_LOGGING
#define TIME_DELAY_SLOT 0x100
#define DBGPIN_INIT() {\
\
}
#define DBGPIN_LOW()
#define DBGPIN_HIGH()
//functions below doesnt need any porting. everything above should be changed according to platform
static void BitDelay() {
static volatile int i;
for(i=0; i<TIME_DELAY_SLOT; ++i) {
asm("");
}
}
static void DebugPrepare() {
DBGPIN_INIT();
//set initial level
DBGPIN_HIGH();
//wait a bit to setup line
int i;
for (i=0; i<16; ++i) {
BitDelay();
}
}
static void DebugTransmitByte(unsigned char byte) {
//start bit
DBGPIN_LOW();
BitDelay();
//byte
int i;
for(i=0; i<8; ++i) {
if(byte & 1) {
DBGPIN_HIGH();
} else {
DBGPIN_LOW();
}
byte >>= 1;
BitDelay();
}
//stop bit + 3 idle time slot
DBGPIN_HIGH();
BitDelay();
BitDelay();
BitDelay();
BitDelay();
}
void DebugTransmitStr(const char *str) {
while(*str) {
DebugTransmitByte(*str++);
}
}
#endif
@a-sakharov
Copy link
Author

Simple bitbag implementation of tx-only uart. Useful for debuggin MCU systems when no uart available or necessity to debug system without periphery initialization.
You only need to define DBGPIN_INIT, DBGPIN_LOW and DBGPIN_HIGH. Then call DebugPrepare at start, after that transmission will work. Adjust TIME_DELAY_SLOT to comply with any standart baudrate (value highly depends on your system) or use logic analyzer to decode arbitrary baudrate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment