board_android-IoT/ABOVE.A31G112CL

A31G112CL / A31G11x , Software Serial (TX only, for debugging , STM32)

cskimair 2021. 8. 27. 13:46

장점 pron: 사용안하는 pin을 UART출력으로 사용해서 디버깅에 활용

단점 con: 딜레이를 사용하므로 디버깅단계 이후에는 별도의 루틴을 만들거나 타이머를 따로 할당해야 함. 

 

/*

참고:
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=chandong83&logNo=221399704423
STM32 소프트웨어 UART Tx 만들어보기

FILE: software_uart.c
*/
/***************************************************************************//**
* @file     software-serial.c
* @brief    Contains all macro definitions and function prototypes
*           support for Example Code on A31G11x
*
*
* @version  0.01
* @date     2020-08-26
* @author   SkyBlue Application Team
*
* Copyright(C) 2021, SkyBlue.
* All rights reserved.
*
*//****************************************************************************/

#include "software_uart.h" 

//보레이트 비트 지연 시간 
#define BAUD_RATE_2400_DELAY (1000000U/2400)
#define BAUD_RATE_9600_DELAY (1000000U/9600)
#define BAUD_RATE_115200_DELAY (1000000U/115200)

//TX 포트 
#define ABOVE_CPU_A31G112CL  1

#ifdef ABOVE_CPU_A31G112CL 
#warning "ABOVE_CPU is defined" 
#define UART_PORT     PA  //  GPIOB
#define UART_PIN      5 // GPIO_PIN of UART_PORT 

#else 

#warning "STM32 cpu  is defined" 
#define UART_PORT   GPIOB
#define UART_PIN      GPIO_PIN_15

#endif 





//1us delay 함수
void DelayUs(uint32_t udelay)
{
  __IO uint32_t Delay = udelay * (SystemCoreClock / 10U / 1000000U);
  //위의 변수에서 10U는 조절해보면서 맞춰야함.
  do
  {
    __NOP();
  }
  while (Delay --);
}

//uart 비트 딜레이 함수
void uartDelay()
{
DelayUs(BAUD_RATE_2400_DELAY ) ; //  BAUD_RATE_115200_DELAY);
}

/*
         //HAL_GPIO_SetPin( ( Pn_Type* )PA, _BIT( 5 ) ); // software serial port 

         // output low
         HAL_GPIO_ClearPin( ( Pn_Type* )PE, _BIT( 0 ) );

*/
void sendUartBit(uint8_t b)
{
#if 1 // ABOVE_CPU_A31G112CL 
    //비트가 참이면 핀을 high
if(b)
{
HAL_GPIO_SetPin( ( Pn_Type* )UART_PORT, _BIT( UART_PIN ) ); // software serial port 
}
    //비트가 거짓이면 핀을 low
else
{
HAL_GPIO_ClearPin( ( Pn_Type* )UART_PORT, _BIT( UART_PIN ) );
}
#endif 

/* // STM32F CPU 
    //비트가 참이면 핀을 high
if(b)
{
UART_PORT->BSRR = UART_PIN;
}
    //비트가 거짓이면 핀을 low
else
{
UART_PORT->BSRR = (uint32_t)UART_PIN << 16U;
}
*/
}

//uart TX함수
void uartTx(uint8_t c)
{
int i=0;
//start bit
sendUartBit(0);
uartDelay();

//data lsb
for(;i<8;i++)
{
sendUartBit(c&0x01);
uartDelay();
c>>=1;
}

    //stop bit
    sendUartBit(1);
    uartDelay();
}

//int fputc(int c, FILE *stream)
//{
// uartTx( (uint8_t) c ) ; 
// return 1 ; 
//      //return(ITM_SendChar(c));
//}


/*-------------------------------------------------------------------------*//**
 * @brief         Puts a string to UART port
 * @param[in]     UARTx
 *                   Pointer to the target UART
 *                   -  UART0 ~ UART1
 * @param[in]     str
 *                   String to put
 * @return        None
 *//*-------------------------------------------------------------------------*/
void Dbg_UARTPuts( /*UARTn_Type* UARTx,*/ const void* str )
{
   uint8_t*    s = ( uint8_t* )str;

   while( *s )
   {
      uartTx( *s++ );
   //UARTPutChar( UARTx, *s++ );
   }
}

/*-------------------------------------------------------------------------*//**
 * @brief         Puts a string to UART port and print new line
 * @param[in]     UARTx
 *                   Pointer to the target UART
 *                   -  UART0 ~ UART1
 * @param[in]     str
 *                   String to put
 * @return        None
 *//*-------------------------------------------------------------------------*/
void Dbg_UARTPuts_( /*UARTn_Type* UARTx, */const void* str )
{
   Dbg_UARTPuts( /*UARTx, */ str );
   Dbg_UARTPuts( /*UARTx, */"\n\r" );
}

void DbgMsg_( /*UARTn_Type* UARTx, */const void* str )
{
Dbg_UARTPuts_( str) ; 
}

/***************************************************************************//**
* @file     software-serial.h
* @brief    Contains all macro definitions and function prototypes
*           support for Example Code on A31G11x
*
*
* @version  0.01
* @date     2020-08-26
* @author   SkyBlue Application Team
*
* Copyright(C) 2021, SkyBlue
* All rights reserved.
*
*//****************************************************************************/

#ifndef __software_uart_H
#define __software_uart_H

/* Includes ----------------------------------------------------------------- */
/* #include "stdint.h"  */
#include "A31G11x.h"
#include "A31G11x_hal_pcu.h"


#ifdef __cplusplus
extern "C"
{
#endif

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */


/* Private function prototypes ---------------------------------------------- */

void uartTx(uint8_t c) ; 

#ifdef __cplusplus
}
#endif

#endif   /* __software_uart_H */

 

 

- output, result screen:

- STM32 Software serial, SOFTWARE UART

 

- arduino software serial

https://www.arduino.cc/en/Tutorial/LibraryExamples/SoftwareSerialExample

 

Software Serial Example

Open-source electronic prototyping platform enabling users to create interactive electronic objects.

www.arduino.cc

 

#ABOVE_CPU #STM32

#A31G11x

#CoretexM0 #Coretex_M0

#A31G112CL _software_serial

#STM32_Software_UART

#software_UART