mirror of
https://github.com/jfdelnero/rf-tools.git
synced 2026-05-27 02:26:59 -04:00
Serial shifter helper.
This commit is contained in:
parent
4a359adeaf
commit
b4f320e8db
81
src/common/serial.c
Normal file
81
src/common/serial.c
Normal file
@ -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 <stdint.h>
|
||||
#include <limits.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
54
src/common/serial.h
Normal file
54
src/common/serial.h
Normal file
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user