⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csp_usart.c

📁 IAR 平台ATMEL 的例程, 和说明
💻 C
📖 第 1 页 / 共 2 页
字号:


/******************************************************************************
* Function          : CSP_USARTDisable
* Description       : Disable USART Rx and/or Tx
* Inputs            : <*usart>       = Pointer to USART structure
*                     <disable_mask> = Configure which functions (RX and/or TX) 
*                     are disabled
* Functions called  : None
* Returns           : None
******************************************************************************/
void CSP_USARTDisable(CSP_USART_T *const usart, U32_T disable_mask)
{
   /* USART Rx and/or Tx Disable */
   CSP_USART_SET_CR(usart, disable_mask);
}
 

/******************************************************************************
* Function          : CSP_USARTReceive
* Description       : Configure USART to receive data. They will be transferred 
*                     automatically to memory thanks to PDC.
* Preconditions     : You must wait 1 baud rate period after resetting the receiver 
*                     (RSTRX in CR) before receiving data
* Inputs            : <*usart> = Pointer to USART structure
*                     <*data>  = Pointer to an array where data packet received will be store
*                     <length> = Number of byte to receive
* Functions called  : CSP_PDCStartRx
* Returns           : None
******************************************************************************/
void CSP_USARTReceive(CSP_USART_T *const usart, U8_T *data, U16_T length)
{
   /* Disable Reception */
   CSP_USART_SET_CR(usart, RXDIS);

   /* Configure PDC Rx Line to Receive a packet */
   CSP_PDCStartRx((U32_T) usart, (U32_T) data, length);

   /* Clear Status */
   CSP_USART_SET_CR(usart, STTTO);

   /* Enable Reception (if RTOR = 0 time out is disabled) */
   CSP_USART_SET_CR(usart, RXEN);
}


/******************************************************************************
* Function          : CSP_USARTTransmit
* Description       : Configure USART to transmit data. They are transferred 
*                     automatically from memory to USART transmission buffer thanks to PDC.
* Preconditions     : You must wait 1 baud rate period after resetting the transmitter 
*                     (RSTTX in CR) before transmitting data
* Inputs            : <*usart> = Pointer to USART structure
*                     <*data>  = Pointer to an array where data packet sent is stored
*                     <length> = Number of byte to transmit
* Functions called  : CSP_PDCStartTx
* Returns           : None
******************************************************************************/
void CSP_USARTTransmit(CSP_USART_T *const usart, U8_T *data, U16_T length)
{
   /* Disable Transmission */
   CSP_USART_SET_CR(usart, TXDIS);

   /* Configure PDC Tx Line to Send a packet */
   CSP_PDCStartTx((U32_T) usart, (U32_T) data, length);

   /* Enable Transmission */
   CSP_USART_SET_CR(usart, TXEN);
}


/******************************************************************************
* Function          : CSP_USARTTransmitLinHeaderFrame
* Description       : Transmit a LIN header Frame
* Postconditions    : You should wait the set of the ENDHEADER bit in SR
* Inputs            : <*usart>     = Pointer to USART structure
*                     <sblr>       = Configure the Synchro Break Length 
*                     <identifier> = Configure the LIN's identifier
* Functions called  : None
* Returns           : None
******************************************************************************/
void CSP_USARTTransmitLinHeaderFrame(CSP_USART_T *const usart, U8_T sblr, U8_T identifier)
{
   /* Configure the Sync Break Length */
   CSP_USART_SET_SBLR(usart, sblr);
   
   /* Configure identifier */
   CSP_USART_SET_LIR(usart, identifier);

   /* Transmit */
   CSP_USART_SET_CR(usart, STHEADER);
}


/******************************************************************************
* Function          : CSP_USARTTransmitLinResponseFrame
* Description       : Transmit a LIN Response Frame
* Preconditions     : You should use the CSP_USARTTransmitLinHeaderFrame function 
*                     before using this function
* Postconditions    : You should wait the set of the ENDMESS bit in SR
* Inputs            : <*usart> = Pointer to USART structure
*                     <*data>  = Pointer to an array where data packet sent is stored
*                     <length> = Number of byte to transmit
* Postconditions    : You should wait the set of the ENDMESS bit in SR
* Functions called  : None
* Returns           : None
******************************************************************************/
void CSP_USARTTransmitLinResponseFrame(CSP_USART_T *const usart, U8_T *data, U8_T length)
{
   /* Local Variables */
   U8_T i = 0; 
   U8_T dfwr[8] = {0, 0, 0, 0, 0, 0, 0, 0};     /* Clear DFWR */

   /* Copy N bytes in DFWR to Send */
   for(i = 0; i < length; i++)
   {
      dfwr[i] = data[i];
   }
   
   /* The writing in DFWR must be in 32 bits */
   CSP_USART_SET_DFWR0(usart, (*(U32_T*)&dfwr[0]));
   CSP_USART_SET_DFWR1(usart, (*(U32_T*)&dfwr[4]));

   /* Transmit */
   CSP_USART_SET_CR(usart, STRESP);
}


/******************************************************************************
* Function          : CSP_USARTPioInit
* Description       : Configure USART PIO 
* Inputs            : <*usart>     = Pointer to USART structure
*                     <pio_mask>   = Configure which pins are activated
*                     <output_pio> = Configure which pins are configured as output 
* Functions called  : None
* Returns           : None
******************************************************************************/
void CSP_USARTPioInit(CSP_USART_T *const usart, U32_T pio_mask, U32_T output_pio)
{
   /* Enable PIO block */
   CSP_USART_SET_ECR(usart, PIO);

   /* Disable all PIO */
   CSP_USART_SET_PDR(usart, 0xFFFFFFFF);
   CSP_USART_SET_ODR(usart, 0xFFFFFFFF);

   /* Enable PIO */
   CSP_USART_SET_PER(usart, pio_mask);
  
   /* Set Output PIO */
   CSP_USART_SET_OER(usart, output_pio);
}


/******************************************************************************
* Function          : CSP_USARTPioGetStatus
* Description       : Read the pin data status
* Inputs            : <*usart> = Pointer to USART structure
* Functions called  : None
* Returns           : 32-bit value of pin data status
******************************************************************************/
U32_T CSP_USARTPioGetStatus(CSP_USART_T *const usart)
{
   /* Return PIO State */
   return (CSP_USART_GET_PDSR(usart));
}


/******************************************************************************
* Function          : CSP_USARTPioClear
* Description       : Set the PIO to low level 
* Inputs            : <*usart>   = Pointer to USART structure
*                     <pio_mask> = Configure which pins are set to low level
* Functions called  : None
* Returns           : None
******************************************************************************/
void CSP_USARTPioClear(CSP_USART_T *const usart, U32_T pio_mask)
{
   /* Set PIO State */
   CSP_USART_SET_CODR(usart, pio_mask);
}


/******************************************************************************
* Function          : CSP_USARTPioSet
* Description       : Set the PIO to high level 
* Inputs            : <*usart>   = Pointer to USART structure
*                     <pio_mask> = Configure which pins are set to high level
* Functions called  : None
* Returns           : None
******************************************************************************/
void CSP_USARTPioSet(CSP_USART_T *const usart, U32_T pio_mask)
{
   /* Set PIO State */
   CSP_USART_SET_SODR(usart, pio_mask);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -