1 /*!
2  * \file      uart-board.c
3  *
4  * \brief     Target board UART driver implementation
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  *
23  * \author    Marten Lootsma(TWTG) on behalf of Microchip/Atmel (c)2017
24  */
25 #include <peripheral_clk_config.h>
26 #include <hal_gpio.h>
27 #include <hal_usart_sync.h>
28 
29 #include "board.h"
30 #include "uart-board.h"
31 
32 struct usart_sync_descriptor Usart0;
33 
UartMcuInit(Uart_t * obj,uint8_t uartId,PinNames tx,PinNames rx)34 void UartMcuInit( Uart_t *obj, uint8_t uartId, PinNames tx, PinNames rx )
35 {
36     obj->UartId = uartId;
37 
38     // Clock initialization
39     hri_gclk_write_PCHCTRL_reg( GCLK, SERCOM0_GCLK_ID_CORE, CONF_GCLK_SERCOM0_CORE_SRC | ( 1 << GCLK_PCHCTRL_CHEN_Pos ) );
40     hri_gclk_write_PCHCTRL_reg( GCLK, SERCOM0_GCLK_ID_SLOW, CONF_GCLK_SERCOM0_SLOW_SRC | ( 1 << GCLK_PCHCTRL_CHEN_Pos ) );
41     hri_mclk_set_APBCMASK_SERCOM0_bit( MCLK );
42 
43     // USART initialization
44     usart_sync_init( &Usart0, SERCOM0, ( void * )NULL );
45 
46     // UASRT GPIO initialization
47     gpio_set_pin_function( tx, PINMUX_PA04D_SERCOM0_PAD0 );
48     gpio_set_pin_function( rx, PINMUX_PA05D_SERCOM0_PAD1 );
49 
50     usart_sync_enable( &Usart0 );
51 }
52 
UartMcuConfig(Uart_t * obj,UartMode_t mode,uint32_t baudrate,WordLength_t wordLength,StopBits_t stopBits,Parity_t parity,FlowCtrl_t flowCtrl)53 void UartMcuConfig( Uart_t *obj, UartMode_t mode, uint32_t baudrate, WordLength_t wordLength, StopBits_t stopBits, Parity_t parity, FlowCtrl_t flowCtrl )
54 {
55     usart_sync_set_baud_rate( &Usart0, baudrate );
56 }
57 
UartMcuDeInit(Uart_t * obj)58 void UartMcuDeInit( Uart_t *obj )
59 {
60 
61 }
62 
UartMcuPutChar(Uart_t * obj,uint8_t data)63 uint8_t UartMcuPutChar( Uart_t *obj, uint8_t data )
64 {
65     if( io_write( &Usart0.io, &data, 1 ) == 0 )
66     {
67         return 1; // Busy
68     }
69     return 0; // OK
70 }
71 
UartMcuGetChar(Uart_t * obj,uint8_t * data)72 uint8_t UartMcuGetChar( Uart_t *obj, uint8_t *data )
73 {
74     if( io_read( &Usart0.io, data, 1 ) == 1 )
75     {
76         return 0; // OK
77     }
78     else
79     {
80         return 1; // Busy
81     }
82 }
83 
UartMcuPutBuffer(Uart_t * obj,uint8_t * buffer,uint16_t size)84 uint8_t UartMcuPutBuffer( Uart_t *obj, uint8_t *buffer, uint16_t size )
85 {
86     if( io_write( &Usart0.io, buffer, size ) == 0 )
87     {
88         return 1; //Error
89     }
90     return 0; // OK
91 }
92 
UartMcuGetBuffer(Uart_t * obj,uint8_t * buffer,uint16_t size,uint16_t * nbReadBytes)93 uint8_t UartMcuGetBuffer( Uart_t *obj, uint8_t *buffer, uint16_t size, uint16_t *nbReadBytes )
94 {
95     *nbReadBytes = io_read( &Usart0.io, buffer, size );
96     if( *nbReadBytes == 0 )
97     {
98         return 1; // Empty
99     }
100     return 0; // OK
101 }
102