gonzui


Format: Advanced Search

mtkernel_3/lib/libtm/sysdepend/iote_stm32l4/tm_com.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    micro T-Kernel 3.00.04
    4:  *
    5:  *    Copyright (C) 2006-2021 by Ken Sakamura.
    6:  *    This software is distributed under the T-License 2.2.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by TRON Forum(http://www.tron.org) at 2021/05/17.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: 
   14: /*
   15:  *    tm_com.c
   16:  *    T-Monitor Communication low-level device driver (STM32L4 IoT-Engine)
   17:  */
   18: 
   19: #include <tk/typedef.h>
   20: #include <sys/sysdef.h>
   21: 
   22: #if USE_TMONITOR
   23: #include "../../libtm.h"
   24: 
   25: #ifdef IOTE_STM32L4
   26: #if TM_COM_SERIAL_DEV
   27: 
   28: /* UART register definition (Use USART2) */
   29: #define UART_BASE               (0x40004400UL)               /* USART2 register base address */
   30: 
   31: #define UART_CR1        (*(_UW*)(UART_BASE+0x0000))    /* Control register.1 */
   32: #define UART_CR2        (*(_UW*)(UART_BASE+0x0004))    /* Control register.2 */
   33: #define UART_CR3        (*(_UW*)(UART_BASE+0x0008))    /* Control register.3 */
   34: #define UART_BRR        (*(_UW*)(UART_BASE+0x000C))    /* Baud Rate register */
   35: #define UART_GTPR       (*(_UW*)(UART_BASE+0x0010))   /* GUARD TIME and PRESCALER register */
   36: #define UART_RTOR       (*(_UW*)(UART_BASE+0x0014))   /* RECEIVER TIMEOUT register */
   37: #define UART_RQR        (*(_UW*)(UART_BASE+0x0018))    /* REQUEST register */
   38: #define UART_ISR        (*(_UW*)(UART_BASE+0x001C))    /* INTERRUPT and STATUS register */
   39: #define UART_ICR        (*(_UW*)(UART_BASE+0x0020))    /* INTERRUPT FLAG CLEAR register */
   40: #define UART_RDR        (*(_UW*)(UART_BASE+0x0024))    /* RECEIVE DATA register */
   41: #define UART_TDR        (*(_UW*)(UART_BASE+0x0028))    /* TRANSMIT DATA register */
   42: 
   43: #define CR1_UE          (0x00000001)                    /* Enable UART */
   44: #define CR1_RE          (0x00000004)                    /* Enable reception */
   45: #define CR1_TE          (0x00000008)                    /* Enable sending */
   46: 
   47: #define ISR_TXE         (0x00000080)                   /* Transmit data register empty */
   48: #define ISR_TC          (0x00000040)                    /* Transmission complete */
   49: #define ISR_RXNE        (0x00000020)                   /* Read data register not empty */
   50: 
   51: /* Communication speed */
   52: #define UART_BAUD       (115200)                      /* 115200 bps */
   53: 
   54: EXPORT  void     tm_snd_dat( const UB* buf, INT size )
   55: {
   56:         UB     *b;
   57: 
   58:         for( b = (UB *)buf; size > 0; size--, b++ ){
   59:                 while ((UART_ISR & ISR_TXE) == 0 );
   60:                 UART_TDR = *b;
   61:                 while ((UART_ISR & ISR_TC) == 0 );
   62:         }
   63: }
   64: 
   65: 
   66: EXPORT  void     tm_rcv_dat( UB* buf, INT size )
   67: {
   68:         for( ; size > 0; size--, buf++ ){
   69:                 while ( (UART_ISR & ISR_RXNE) == 0 );
   70:                 *buf = UART_RDR & 0xff;
   71:         }
   72: }
   73: 
   74: 
   75: EXPORT  void     tm_com_init(void)
   76: {
   77:         /* Initialize serial communication. Disable all interrupt. */
   78:         UART_CR1 = 0;          /* 8bit, Non parity (Reset value) */
   79:         UART_CR2 = 0;          /* Stop bit 1 (Reset value) */
   80:         UART_CR3 = 0;          /* No hard flow control (Reset value) */
   81: 
   82:         /* Set baud rate */
   83:         UART_BRR = ((TMCLK*1000*1000) + UART_BAUD/2)/UART_BAUD;
   84: 
   85:         UART_CR1 = CR1_UE | CR1_RE |CR1_TE;    /* Start UART */
   86: }
   87: 
   88: #endif /* TM_COM_SERIAL_DEV */
   89: #endif /* IOTE_STM32 */
   90: #endif /* USE_TMONITOR */