Coder Social home page Coder Social logo

liyanboy74 / soft-uart Goto Github PK

View Code? Open in Web Editor NEW
76.0 3.0 35.0 12.77 MB

Multi Software Serial (UART) for STM32

License: BSD 3-Clause "New" or "Revised" License

C 99.59% Makefile 0.14% Assembly 0.27%
uart softuart stm32 hal cubemx software-serial serial software-uart soft-uart multi-software-serial

soft-uart's Introduction

Multi Software Serial (UART) For STM32

The library work fine for virtualize 6 UART full duplex in baud rate 9600. All UART work together parallelly!

Library Dir:

Handler & baud rate

The function SoftUartHandler(void) must call in interrupt every 0.2*(1/BR) .

if BR=9600 then 0.2*(1/9600)=20.8333333 uS

highly recommended set maximum CPU clock for Run Handler faster as possible! you also available for use lite version by limited options for slow MCUs.

The library don't change any GPIO config! before using must config TX pins as output and RX pins as input , any TX pin must set to HIGH and any RX pin must be Pullup.

Example Timer Config

for Baud Rate 9600 :

Config value
Timer Clock 72 MHz
Prescaler (PSC - 16 bits value) 74
Counter Period (AutoReload Register - 16 bits value ) 19
auto-reload preload Enable
Tim global interrupt Enable

You also can use Timer frequency calculator.

Calling handler

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIMX)
	{
		SoftUartHandler();
	}
}

Config Soft UART

Open softuart.h and edit bellow line as you want:

#define 	Number_Of_SoftUarts	 6
#define		SoftUartTxBufferSize	32
#define		SoftUartRxBufferSize	64

If Number_Of_SoftUarts=6 that mean you SoftUartNumber is 0,1,2,3,4,5

After config timer for config Soft UART use SoftUartInit:

SoftUartState_E SoftUartInit(uint8_t SoftUartNumber,GPIO_TypeDef *TxPort,uint16_t TxPin,GPIO_TypeDef *RxPort,uint16_t RxPin);

Using Soft UART

Transmit always is Possible but for Receiving data you must enable listening by calling:

SoftUartState_E SoftUartEnableRx(uint8_t SoftUartNumber);

Received data stored in buffer accessible by below functions:

uint8_t 	SoftUartRxAlavailable(uint8_t SoftUartNumber);
SoftUartState_E SoftUartReadRxBuffer(uint8_t SoftUartNumber,uint8_t *Buffer,uint8_t Len);

Transmit data:

SoftUartState_E SoftUartPuts(uint8_t SoftUartNumber,uint8_t *Str,uint8_t Len);
void 		SoftUartWaitUntilTxComplate(uint8_t SoftUartNumber);

Example test:

Transmit

while(1)
{
	SoftUartPuts(0,(uint8_t *)"Hello",5);
	SoftUartPuts(1,(uint8_t *)"My",2);
	SoftUartPuts(2,(uint8_t *)"Name",4);
	SoftUartPuts(3,(uint8_t *)"Is",2);
	SoftUartPuts(4,(uint8_t *)"Esmaeill",8);
	SoftUartPuts(5,(uint8_t *)"--------",8);
}

LogicAnalizer

Receive

Example 1:

uint8_t getchar(uint8_t SoftUartNumber)
{
    uint8_t ch;
    while(SoftUartRxAlavailable(SoftUartNumber)==0);
    SoftUartReadRxBuffer(SoftUartNumber,&ch,1);
    return ch;
}

Example 2:

uint8_t Buffer[SIZE],Len;

while(1)
{
	// Read >= 10 Byte Data if Received
	if(Len=SoftUartRxAlavailable(0),Len>=10)
	{
		// Move Received Data To Another Buffer
		if(SoftUartReadRxBuffer(0,Buffer,Len)==SoftUart_OK)
		{
			// Done
		}
	}
}

Full Example 3:

#include "softuart.h"

...

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIMX)
	{
		SoftUartHandler();
	}
}

uint8_t getchar(uint8_t SoftUartNumber)
{
    uint8_t ch;
    while(SoftUartRxAlavailable(SoftUartNumber)==0);
    SoftUartReadRxBuffer(SoftUartNumber,&ch,1);
    return ch;
}

int main(void)
{
    uint8_t ch;
    
    ...
    
    HAL_TIM_Base_Start_IT(&htimX);
    
    SoftUartInit(0,SU_TX_GPIO_Port,SU_TX_Pin,SU_RX_GPIO_Port,SU_RX_Pin);
    SoftUartEnableRx(0);
    
    ...
    
    while (1)
    {
        ch=getchar(0);
        SoftUartPuts(0,&ch,1);
        //SoftUartWaitUntilTxComplate(0);
        HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);
    }
}

soft-uart's People

Contributors

liyanboy74 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

soft-uart's Issues

Block code

Hello, help me please. I use STM32F103C8T6 blue pill, i configured timer and soft uart. I'm trying send message, but it isn't work, for example, after HAL_TIM_Base_Start_IT(&htim3); the code is not executed

Interrupt interval vs. baud rate

If I understand correctly, the code fires interrupts at a constant frequency, during which you sample the inputs, detect start bits, determine the time slot (0..4) for reading the data bits on a given input, and send if there is outgoing data.

This is different from other implementations which have an interrupt triggered by the edge of the start bit (not a timer) and then read data via a timer interrupt set up when the start bit is detected to hit exactly the middle of the data bits.

In your implementation, you get the benefit of many inputs at the cost of timing accuracy, since you will not sample in the middle of the data bit but rather at the fixed time slot that's closest to it. So the higher the oversampling, the closer you get to that middle.

(did I understand everything correctly?)

Now my question is: how did you get to factor 5x for oversampling? why that number? Let's say you did 3x oversampling, would that not be good enough? Did you do calculations on how much the frequencies can deviate from the ideal value while still sampling the correct bit?

Receive data anytime

Can you help me with the issue when I want to be ready to receive with external interrupts on Rx_Pin?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.