[STM8 learning notes] STM8 series serial port interrupt sending routine

Publisher:中和子Latest update time:2020-02-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Target MCU: STM8L051F3


Function: Serial port interrupt sending. (Different from serial port polling sending)


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

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

 

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

* Header file reference

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

#include "drv_usart.h"

 

 

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

 * Static global variable definition

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

 

 

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

 *Global variable definition

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

drv_usart_s m_usart_s;

 

 

 

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

 * Internal function declaration

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

static void Drv_USART_EndTransmit_IT(void);

static void Drv_USART_ParaInit(void);

static void Drv_USART_SendByte( u8 sendData );

 

 

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

 * Global function implementation

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

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

 * 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; //input

PA_CR1_C13 = 1; //Pull-up input

PA_CR2_C23 = 0;

// PA2 USART_TX config

PA_ODR_ODR2 = 1;

PA_CR1_C12 = 1; //Push-pull output

PA_CR2_C22 = 1; //Output slew rate 10M   

PA_DDR_DDR2 = 1; // Output high level, TX idle state is high level, if not set, it will send 0x00 inexplicably

 

/* USART2 --> PCKEN33, USART3 --> PCKEN34*/

CLK_PCKENR1_PCKEN15 = 1; //Turn on USART1 peripheral clock

 

// Enable the UART function of the pin, and reuse it as the USART function pin

//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

 

//Set the serial port working mode

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 */

 

//Set the baud rate

//Set the baud rate to 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; // Enable transmission

// USART1_CR2_TIEN=0; //Turn on transmit interrupt

// USART1_CR2_TCIEN = 1; // Enable the transmit completion interrupt

USART1_CR2_REN = 1; //Enable reception

USART1_CR2_RIEN = 1; //Open receive interrupt

 

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 = "rn";

if( NULL != m_usart_s.sendBuf_p ){

if( '' == *m_usart_s.sendBuf_p ){

m_usart_s.sendBuf_p = (u8*)strEnd;

}

 

Drv_USART_SendByte(*(m_usart_s.sendBuf_p));

 

if( 'n' == *m_usart_s.sendBuf_p ){

m_usart_s.sendBuf_p = NULL;

}else{

m_usart_s.sendBuf_p++;

}

}else{

/*Disable transmit enable*/

USART1_CR2_TEN = DISABLE;

/* Disable the send completion interrupt */

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;

 

/* Enable send completion interrupt */

USART1_CR2_TCIEN = ENABLE;

Drv_USART_SendByte(*(m_usart_s.sendBuf_p++));

 

/* Enable sending */

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__

 

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

* Header file reference

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

#include "../typedef.h"

 

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

 * Macro definition

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

 

 

 

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

 * Enumeration type definition

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

 

 

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

 *Structure type definition

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

typedef struct __DRV_USART_S__{

u8* sendBuf_p;

}drv_usart_s;

 

 

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

 * Global function declaration

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

extern void drvUsart_init(void);

extern void Drv_USART_TransmitData(u8* StrData_p);

 

#endif

Reference address:[STM8 learning notes] STM8 series serial port interrupt sending routine

Previous article:Serial port interruption problem in stm8s003
Next article:STM8S handles serial port interruption

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号