From b4f320e8db75bcb602c4a109df0c7c4898326e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20DEL=20NERO?= Date: Sat, 23 May 2026 09:57:47 +0200 Subject: [PATCH] Serial shifter helper. --- src/common/serial.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ src/common/serial.h | 54 ++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/common/serial.c create mode 100644 src/common/serial.h diff --git a/src/common/serial.c b/src/common/serial.c new file mode 100644 index 0000000..fa8c743 --- /dev/null +++ b/src/common/serial.c @@ -0,0 +1,81 @@ +/////////////////////////////////////////////////////////////////////////////////// +//-------------------------------------------------------------------------------// +//-------------------------------------------------------------------------------// +//-----------H----H--X----X-----CCCCC----22222----0000-----0000------11----------// +//----------H----H----X-X-----C--------------2---0----0---0----0--1--1-----------// +//---------HHHHHH-----X------C----------22222---0----0---0----0-----1------------// +//--------H----H----X--X----C----------2-------0----0---0----0-----1-------------// +//-------H----H---X-----X---CCCCC-----222222----0000-----0000----1111------------// +//-------------------------------------------------------------------------------// +//----------------------------------------------------- http://hxc2001.free.fr --// +/////////////////////////////////////////////////////////////////////////////////// +// File : serial.c +// Contains: serial helpers +// +// This file is part of rf-tools. +// +// Written by: Jean-François DEL NERO +// +// Copyright (C) 2026 Jean-François DEL NERO +// +// You are free to do what you want with this code. +// A credit is always appreciated if you use it into your product :) +// +// Change History (most recent first): +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +#include "serial.h" + +void serial_init(serial_gen * ctx, int bitcnt, unsigned int sample_rate, unsigned int baud_rate) +{ + ctx->bits_per_word = bitcnt; + ctx->start_bit_cnt = 0; + ctx->stop_bit_cnt = 0; + ctx->lsb_first = 0; + + ctx->mask = 0; + + ctx->sample_rate = sample_rate; + + ctx->inc_acc = (uint32_t)( ( (uint64_t)(0xFFFFFFFF) * (uint64_t)baud_rate ) / (uint64_t)sample_rate ); + + ctx->acc = 0; + ctx->cur_bit = 0; +} + +int serial_tx_getlinestate(serial_gen * ctx) +{ + if ( ctx->acc > (((uint32_t)0xFFFFFFFF) - ctx->inc_acc) ) + { + // Overflow... Next bit + ctx->cur_bit = ctx->tx_reg & ctx->mask; + // If mask == 0 -> shifter empty ! + ctx->mask >>= 1; + } + + ctx->acc += ctx->inc_acc; + + if( ctx->cur_bit ) + return 1; + else + return 0; + +} + +int serial_is_tx_reg_empty(serial_gen * ctx) +{ + if(!ctx->mask) + return 1; + else + return 0; +} + +void serial_tx_settxreg(serial_gen * ctx,uint32_t data) +{ + ctx->mask = (0x1 << (ctx->bits_per_word - 1)); + ctx->tx_reg = data; +} + diff --git a/src/common/serial.h b/src/common/serial.h new file mode 100644 index 0000000..7ecf921 --- /dev/null +++ b/src/common/serial.h @@ -0,0 +1,54 @@ +/////////////////////////////////////////////////////////////////////////////////// +//-------------------------------------------------------------------------------// +//-------------------------------------------------------------------------------// +//-----------H----H--X----X-----CCCCC----22222----0000-----0000------11----------// +//----------H----H----X-X-----C--------------2---0----0---0----0--1--1-----------// +//---------HHHHHH-----X------C----------22222---0----0---0----0-----1------------// +//--------H----H----X--X----C----------2-------0----0---0----0-----1-------------// +//-------H----H---X-----X---CCCCC-----222222----0000-----0000----1111------------// +//-------------------------------------------------------------------------------// +//----------------------------------------------------- http://hxc2001.free.fr --// +/////////////////////////////////////////////////////////////////////////////////// +// File : serial.h +// Contains: serial helpers +// +// This file is part of rf-tools. +// +// Written by: Jean-François DEL NERO +// +// Copyright (C) 2026 Jean-François DEL NERO +// +// You are free to do what you want with this code. +// A credit is always appreciated if you use it into your product :) +// +// Change History (most recent first): +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef __SERIAL_H__ +#define __SERIAL_H__ + +typedef struct serial_gen_ +{ + int bits_per_word; + int start_bit_cnt; + int stop_bit_cnt; + int lsb_first; + + uint32_t sample_rate; + uint32_t baud_rate; + + uint32_t mask; + uint32_t tx_reg; + + uint32_t acc; + uint32_t inc_acc; + uint32_t cur_bit; +}serial_gen; + +void serial_init(serial_gen * ctx, int bitcnt, unsigned int sample_rate, unsigned int baud_rate); +int serial_tx_getlinestate(serial_gen * ctx); +int serial_is_tx_reg_empty(serial_gen * ctx); +void serial_tx_settxreg(serial_gen * ctx,uint32_t data); + +#endif +