Christina Quast | 53b2105 | 2014-12-09 15:34:35 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * \file |
| 32 | * |
| 33 | * \par Purpose |
| 34 | * |
| 35 | * This module provides several definitions and methods for using an USART |
| 36 | * peripheral. |
| 37 | * |
| 38 | * \par Usage |
| 39 | * |
| 40 | * -# Enable the USART peripheral clock in the PMC. |
| 41 | * -# Enable the required USART PIOs (see pio.h). |
| 42 | * -# Configure the UART by calling USART_Configure. |
| 43 | * -# Enable the transmitter and/or the receiver of the USART using |
| 44 | * USART_SetTransmitterEnabled and USART_SetReceiverEnabled. |
| 45 | * -# Send data through the USART using the USART_Write and |
| 46 | * USART_WriteBuffer methods. |
| 47 | * -# Receive data from the USART using the USART_Read and |
| 48 | * USART_ReadBuffer functions; the availability of data can be polled |
| 49 | * with USART_IsDataAvailable. |
| 50 | * -# Disable the transmitter and/or the receiver of the USART with |
| 51 | * USART_SetTransmitterEnabled and USART_SetReceiverEnabled. |
| 52 | */ |
| 53 | |
| 54 | #ifndef _USART_ |
| 55 | #define _USART_ |
| 56 | |
| 57 | /*------------------------------------------------------------------------------ |
| 58 | * Headers |
| 59 | *------------------------------------------------------------------------------*/ |
| 60 | |
| 61 | #include "chip.h" |
| 62 | |
| 63 | #include <stdint.h> |
| 64 | |
| 65 | /*------------------------------------------------------------------------------ |
| 66 | * Definitions |
| 67 | *------------------------------------------------------------------------------*/ |
| 68 | |
| 69 | /** \section USART_mode USART modes |
| 70 | * This section lists several common operating modes for an USART peripheral. |
| 71 | * |
| 72 | * \b Modes |
| 73 | * - USART_MODE_ASYNCHRONOUS |
| 74 | * - USART_MODE_IRDA |
| 75 | */ |
| 76 | |
| 77 | /** Basic asynchronous mode, i.e. 8 bits no parity.*/ |
| 78 | #define USART_MODE_ASYNCHRONOUS (US_MR_CHRL_8_BIT | US_MR_PAR_NO) |
| 79 | |
| 80 | /** IRDA mode*/ |
| 81 | #define USART_MODE_IRDA (AT91C_US_USMODE_IRDA | AT91C_US_CHRL_8_BITS | AT91C_US_PAR_NONE | AT91C_US_FILTER) |
| 82 | |
| 83 | /** SPI mode*/ |
| 84 | #define AT91C_US_USMODE_SPIM 0xE |
| 85 | #define US_SPI_CPOL_0 (0x0<<16) |
| 86 | #define US_SPI_CPHA_0 (0x0<<8) |
| 87 | #define US_SPI_CPOL_1 (0x1<<16) |
| 88 | #define US_SPI_CPHA_1 (0x1<<8) |
| 89 | #define US_SPI_BPMODE_0 (US_SPI_CPOL_0|US_SPI_CPHA_1) |
| 90 | #define US_SPI_BPMODE_1 (US_SPI_CPOL_0|US_SPI_CPHA_0) |
| 91 | #define US_SPI_BPMODE_2 (US_SPI_CPOL_1|US_SPI_CPHA_1) |
| 92 | #define US_SPI_BPMODE_3 (US_SPI_CPOL_1|US_SPI_CPHA_0) |
| 93 | |
| 94 | #ifdef __cplusplus |
| 95 | extern "C" { |
| 96 | #endif |
| 97 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 98 | typedef struct Usart_info { |
| 99 | Usart *base; |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 100 | uint8_t state; |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 101 | uint32_t id; |
| 102 | } Usart_info; |
| 103 | |
Christina Quast | 53b2105 | 2014-12-09 15:34:35 +0100 | [diff] [blame] | 104 | /*------------------------------------------------------------------------------*/ |
| 105 | /* Exported functions */ |
| 106 | /*------------------------------------------------------------------------------*/ |
| 107 | |
| 108 | extern void USART_Configure( Usart *usart, uint32_t mode, uint32_t baudrate, uint32_t masterClock ) ; |
| 109 | extern uint32_t USART_GetStatus( Usart *usart ) ; |
| 110 | extern void USART_EnableIt( Usart *usart,uint32_t mode ) ; |
| 111 | extern void USART_DisableIt( Usart *usart,uint32_t mode ) ; |
| 112 | extern void USART_SetTransmitterEnabled( Usart *usart, uint8_t enabled ) ; |
| 113 | |
| 114 | extern void USART_SetReceiverEnabled( Usart *usart, uint8_t enabled ) ; |
| 115 | |
| 116 | extern void USART_Write( Usart *usart, uint16_t data, volatile uint32_t timeOut ) ; |
| 117 | |
| 118 | extern uint8_t USART_WriteBuffer( Usart *usart, void *buffer, uint32_t size ) ; |
| 119 | |
| 120 | extern uint16_t USART_Read( Usart *usart, volatile uint32_t timeOut ) ; |
| 121 | |
| 122 | extern uint8_t USART_ReadBuffer( Usart *usart, void *buffer, uint32_t size ) ; |
| 123 | |
| 124 | extern uint8_t USART_IsDataAvailable( Usart *usart ) ; |
| 125 | |
| 126 | extern void USART_SetIrdaFilter(Usart *pUsart, uint8_t filter); |
| 127 | |
| 128 | extern void USART_PutChar( Usart *usart, uint8_t c ) ; |
| 129 | |
| 130 | extern uint32_t USART_IsRxReady( Usart *usart ) ; |
| 131 | |
| 132 | extern uint8_t USART_GetChar( Usart *usart ) ; |
| 133 | |
| 134 | #ifdef __cplusplus |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #endif /* #ifndef _USART_ */ |
| 139 | |