;=============================================================================
; Purpose: putchar function (for STM8 devices).
;-----------------------------------------------------------------------------
; Input:       A   character to print.
; Output:      A   the character written.
; Read only:   A, X, Y, BH, BL, CH, CL
; Modified:    -
;-----------------------------------------------------------------------------
; Notes:
; - In projects that do not use the (un)getchar functionality, some extra code
; can be removed from the present file. Just set the NO_UNGETCHAR macro to 1
; and unnecessary conditional assembly blocks will be removed.
; - If you do not want the '\n' (Line Feed) character converted to CR/LF, set
; the NO_CRLF_CONV macro to 1 and unnecessary instructions will be removed.
; - If your project uses the large model, using the RCST7 directive STM8(LARGE),
; functions will be called using CALLF, and will return using RETF.
; For the following code to work properly, you must set the LARGE macro to 1
; so that the proper CALL/RET scheme will be used.
;-----------------------------------------------------------------------------
; (c) Copyright Raisonance S.A.S. 1989-2008, All rights reserved.
;=============================================================================

$MODESTM8
;$SET(LARGE = 1)          ; Uncomment if you are in LARGE mode

;$SET(NO_UNGETCHAR = 1)   ; Uncomment if you do not use the ungetchar function
;$SET(NO_CRLF_CONV = 1)   ; Uncomment if you do not want LF to CR/LF conversion

$IF(LARGE==1)
 BIBCALL MACRO address
    CALLF address
 ENDM
 BIBRET  MACRO
    RETF
 ENDM
$ELSE
 BIBCALL MACRO address
    CALL address
 ENDM
 BIBRET  MACRO
    RET
 ENDM
$ENDIF

    ; Exports public symbols
    PUBLIC  putchar
    PUBLIC  ?putchar
    ; These publics are required for getchar
    PUBLIC  USART_SR
    PUBLIC  USART_DR


    ; Symbols for SCI registers depend on the target architecture
    USART_SR    DATA   05230h      ; USART status register  
    USART_DR    DATA   05231h      ; USART data register     
    USART_BRR1  DATA   05232h      ; USART baud rate register   
    USART_BRR2  DATA   05233h      ; USART DIV mantissa[11:8] SCIDIV fraction 
    USART_CR1   DATA   05234h      ; USART control register 1     
    USART_CR2   DATA   05235h      ; USART control register 2     
    USART_CR3   DATA   05236h      ; USART control register 3      
    USART_CR4   DATA   05237h      ; USART control register 4      
    USART_CR5   DATA   05238h      ; USART control register 5       
    USART_GT    DATA   05239h      ; USART guard time register     
    USART_PSCR  DATA   0523Ah      ; USART prescaler register     

    ; Baud rate divisor settings (For 9600 bps)
    ; When using the 16MHz internal R/C clock, and HSIDIV divides by 8 (default)
    BDRATDIV1  EQU    00Dh        ; Baud rate divisor (depends on the CPU frequency)
    BDRATDIV2  EQU    000h        ; Baud rate divisor 2 (depends on the CPU frequency)
    ; Use the following settings when using w/ a 16MHz R/C clock undivided
    ; BDRATDIV1  EQU    068h        ; Baud rate divisor (depends on the CPU frequency)
    ; BDRATDIV2  EQU    003h        ; Baud rate divisor 2 (depends on the CPU frequency)

    ; USART_SR bit definitions
    TDRE       EQU    7           ; Transmit data register empty
    TC         EQU    6           ; Transmission complete
    RDRF       EQU    5           ; Received data ready flag
    IDLE       EQU    4           ; Idle line detect
    OERR       EQU    3           ; Overrun error
    NF         EQU    2           ; Noise flag
    FE         EQU    1           ; Framing error
    PE         EQU    0           ; Parity error

    ; USART Control Register 1 bit definitions
    R8          EQU     7       ; Receive data bit 8
    T8          EQU     6       ; Transmit data bit 8
    SCID        EQU     5       ; Disabled for low power consumption
    M           EQU     4       ; Word length
    WAKE        EQU     3       ; Wake-Up method
    PCE         EQU     2       ; Parity control enable
    PS          EQU     1       ; Parity selection
    PIE         EQU     0       ; Parity interrupt enable

    ; USART Control Register 2 bit definitions
    TIE         EQU     7       ; Transmitter interrupt enable.
    TCIE        EQU     6       ; Transmission complete interrupt enable
    RIE         EQU     5       ; Receiver interrupt enable.
    ILIE        EQU     4       ; Idle line interrupt enable.
    TE          EQU     3       ; Transmitter enable.
    RE          EQU     2       ; Receiver enable.
    RWU         EQU     1       ; Receiver wake-up.
    SBK         EQU     0       ; Send break.


    ; Character Codes
    LF      EQU     0Ah         ; Line Feed
    CR      EQU     0Dh         ; Carriage Return


$IF(NO_UNGETCHAR <> 1)          ; This can be skipped if getchar/ungetchar are not necessary

;---------------------------------------------------------------------------
; Flag for getchar/ungetchar SCI buffer full bit
;---------------------------------------------------------------------------

    PUBLIC  FULLB_SCI

    RSG_BTM1_GETFLAG    SEGMENT BIT

    RSEG                RSG_BTM1_GETFLAG

FULLB_SCI:    DBIT    1         ; Read bit indicates whether the buffer has been read

$ENDIF

;---------------------------------------------------------------------------
; Actual putchar function.
; Note that the SCI initialization is performed by this function upon its
; first call, hence it is not necessary to initialize the SCI elsewhere.
; For this reason, it is necessary to call putchar at least once before
; calling the getchar function, otherwise the SCI will not be initialized.
;---------------------------------------------------------------------------

    CSTDLIB             SEGMENT CODE INSECTION0
    RSEG                CSTDLIB

?putchar:
putchar:

;-----------------------------------------------------------------------------
; Initialize the SCI if not yet done
;-----------------------------------------------------------------------------

    BTJT    USART_CR2, #TE, _convert   ; Jump to _convert if USART already initialized

    ; Load Default value into control registers
    CLR     USART_CR1
    CLR     USART_CR3

    ; Set baudrate to 9600 bauds @ 16 MHz
$IF(BDRATDIV2 <> 0)
    MOV     USART_BRR2, #BDRATDIV2
$ELSE
    CLR     USART_BRR2
$ENDIF
    MOV     USART_BRR1, #BDRATDIV1

    ; Enable Transmission and Reception
    BSET    USART_CR2, #TE
    BSET    USART_CR2, #RE

$IF(NO_UNGETCHAR <> 1)                 ; This can be skipped if getchar/ungetchar are not necessary
    ; Specifies that no char is in the getchar/ungetchar buffer yet
    BRES    FULLB_SCI
$ENDIF

;-----------------------------------------------------------------------------
; Convert LF to CR/LF (to keep Hyperterminal happy)
;-----------------------------------------------------------------------------

_convert:
$IF(NO_CRLF_CONV <> 1)                 ; This can be skipped if CR/LF conversion is not necessary
    CP      A, #LF
    JRNE    _emit
    ; Reentrant call to putchar
    PUSH    A
    LD      A, #CR
    BIBCALL putchar
    POP     A
$ENDIF

_emit:

    BTJF    USART_SR, #TC, $           ; Wait end of (previous) transmission
    LD      USART_DR, A                ; Send the character to the SCI
    BTJF    USART_SR, #TC, $           ; Wait end of transmission

    BIBRET

END
