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
Previous article:Serial port interruption problem in stm8s003
Next article:STM8S handles serial port interruption
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Rapid Design of TMS320LF2407 Program Based on Matlab
- MSP430G2553 collects AC VPP
- 3m semi-anechoic chamber, 10m semi-anechoic chamber
- [2022 Digi-Key Innovation Design Competition] Part 3: Understanding the setting of Chinese characters and TouchGFX with development board installed
- 【TouchGFX Design】+ Several problems encountered and attempts at button functions
- N32G430C8L7 development board ADC temperature test
- [Xianji HPM6750EVKMINI Review] 6# HPM6750 for SD Operation
- EEWORLD University ---- Computer Vision and Deep Learning
- Google Chrome homepage is changed automatically for no apparent reason?
- Newbie asks for help: "Can the SMA interface antenna and antenna base hardware be connected?"