[STM 8 학습 노트] STM 8 시리즈 직렬 포트 중단 발송 루틴

대상 MCU: STM8L051F 3
기능: 직렬 포트 전송 중단.(직렬 폴 링 발송 과 구별)
drv_usart.c
/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_usart.c
 * brief :  .c files
 * History:    Author        Version          ChangeContent            Date
 *               xxx                             NewFile             2019.04.28
 *****************************************************************************************/

/**********************************************
*      
**********************************************/
#include "drv_usart.h"


/**********************************************
 *         
**********************************************/


/**********************************************
 *      
**********************************************/
drv_usart_s m_usart_s;



/**********************************************
 *       
 **********************************************/ 
static void Drv_USART_EndTransmit_IT(void);
static void Drv_USART_ParaInit(void);
static void Drv_USART_SendByte( u8 sendData );


/**********************************************
 *        
 **********************************************/
 /****************************************************************************************
 * FunctionName   : drvUsart_init
 * Abstract       : Init Usart driver.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
void drvUsart_init(void)
{
	
	/*PA2 --> USART_TX
	*PA3 --> USART_RX*/
	// PA3 USART_RX config
	PA_DDR_DDR3 = 0;		 //  
	PA_CR1_C13	= 1;		 //    
	PA_CR2_C23	= 0;
	// PA2 USART_TX config
	PA_ODR_ODR2 = 1;
	PA_CR1_C12	= 1;		 //    
	PA_CR2_C22	= 1;		 //    10M	  
	PA_DDR_DDR2 = 1;		 //     ,TX        ,     ,        0x00

	/* USART2 --> PCKEN33, USART3 --> PCKEN34*/
	CLK_PCKENR1_PCKEN15 = 1;					//  USART1    

	//     UART  ,     USART    
	//00: USART1_TX on PC2 and USART1_RX on PC3
	//01: USART1_TX on PA2 and USART1_RX on PA3
	//10: USART1_TX on PC5 and USART1_RX on PC6
	SYSCFG_RMPCR1_USART1TR_REMAP = 1;			// system configuration PA2,PA3

	//        
	USART1_CR1_M	 = 0;				// 1 Start bit, 8 Data bits
	USART1_CR3_STOP0 = 0;				// 1 STOP bit
	USART1_CR3_STOP1 = 0;	
	USART1_CR1_PCEN  = 0;				/* No Parity : Parity control disabled */

	//     
	//      9600
	// 2000000/9600=208.333
	//208.333(DEC)=00D0(HEX)
	USART1_BRR2 = 0x00;				//the BRR2 should be programmed before BRR1
	USART1_BRR1 = 0x0D;

//	USART1_CR2_TEN = 1;			//    
	//	USART1_CR2_TIEN=0;		//      
//	USART1_CR2_TCIEN = 1;			//        
	USART1_CR2_REN = 1;				//    
	USART1_CR2_RIEN = 1;			//      

	USART1_CR1_USARTD = 0;			//Enable the USART peripheral

	Drv_USART_ParaInit();
}

/****************************************************************************************
 * FunctionName   : Drv_USART_ParaInit
 * Abstract       : 
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/08 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
static void Drv_USART_ParaInit(void)
{
	m_usart_s.sendBuf_p = NULL;
}


 /****************************************************************************************
 * FunctionName   : Drv_USART_Rx_IRQHandler
 * Abstract       : usart rx interrupt handler.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
#pragma vector = USART_R_RXNE_vector
__interrupt void Drv_USART_Rx_IRQHandler(void)
{
	if( 1 == USART1_SR_RXNE )
	{

	}
}

 /****************************************************************************************
 * FunctionName   : Drv_USART_Tx_IRQHandler
 * Abstract       : usart tx interrupt handler.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
#pragma vector = USART_T_TC_vector
__interrupt void Drv_USART_Tx_IRQHandler(void)
{
	if( (RESET != USART1_SR_TC) && (RESET != USART1_CR2_TCIEN) )
	{
		Drv_USART_EndTransmit_IT();
	}
}

/****************************************************************************************
 * FunctionName   : Drv_USART_SendByte
 * Abstract       : usart send byte data.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/18 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
static void Drv_USART_SendByte( u8 sendData )
{
	USART1_DR = sendData;
}

/****************************************************************************************
 * FunctionName   : Drv_USART_EndTransmit_IT
 * Abstract       : Wrap up transmission in non-blocking mode.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2018/12/18 , DJWT_zhenggp  New
 * History        :
****************************************************************************************/
static void Drv_USART_EndTransmit_IT(void)
{
	static const u8* strEnd = "\r
"; if( NULL != m_usart_s.sendBuf_p ){ if( '\0' == *m_usart_s.sendBuf_p ){ m_usart_s.sendBuf_p = (u8*)strEnd; } Drv_USART_SendByte(*(m_usart_s.sendBuf_p)); if( '
' == *m_usart_s.sendBuf_p ){ m_usart_s.sendBuf_p = NULL; }else{ m_usart_s.sendBuf_p++; } }else{ /* */ USART1_CR2_TEN = DISABLE; /* */ USART1_CR2_TCIEN = DISABLE; } } /**************************************************************************************** * FunctionName : Drv_USART_EndTransmit_IT * Abstract : Wrap up transmission in non-blocking mode. * Argument1(in) : void * Argument2(out) : * Return Value : void * Remarks : * Create : 2018/12/18 , DJWT_zhenggp New * History : ****************************************************************************************/ void Drv_USART_TransmitData(u8* StrData_p) { if( NULL == StrData_p ){ return; } m_usart_s.sendBuf_p = StrData_p; /* */ USART1_CR2_TCIEN = ENABLE; Drv_USART_SendByte(*(m_usart_s.sendBuf_p++)); /* */ USART1_CR2_TEN = ENABLE; }

drv_usart.h
/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_usart.h
 * brief :  .h files
 * History:    Author        Version          ChangeContent            Date
 *               zhenggp                                 NewFile             2019.04.28
 *****************************************************************************************/

#ifndef __DRV_USART_H__
#define __DRV_USART_H__

/**********************************************
*      
**********************************************/
#include "../typedef.h"

/**********************************************
 *    
**********************************************/



/**********************************************
 *      
**********************************************/


/**********************************************
 *       
**********************************************/
typedef struct __DRV_USART_S__{
	u8* sendBuf_p;
}drv_usart_s;


/**********************************************
 *       
**********************************************/
extern void drvUsart_init(void);
extern void Drv_USART_TransmitData(u8* StrData_p);

#endif

 
 

좋은 웹페이지 즐겨찾기