I/O Retargeting  Version 1.2.0
User Code Templates for I/O Retargeting using ARM Compiler
 All Files Macros Pages
stdout_USART.c File
Note
To have the full flexibility for retargeting different channels to different target hardware, each USART user code template contains a function to initialize the USART (here: stdout_init). When using the same hardware for multiple channels, the initialization functions need to be merged in one single function. Call this function during the initialization of the device's peripherals.
/*-----------------------------------------------------------------------------
* Name: stdout_USART.c
* Purpose: STDOUT USART Template
* Rev.: 1.0.0
*-----------------------------------------------------------------------------*/
/* Copyright (c) 2013 - 2015 ARM LIMITED
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of ARM nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
*
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------*/
#include "Driver_USART.h"
//-------- <<< Use Configuration Wizard in Context Menu >>> --------------------
// <h>STDOUT USART Interface
// <o>Connect to hardware via Driver_USART# <0-255>
// <i>Select driver control block for USART interface
#define USART_DRV_NUM 0
// <o>Baudrate
#define USART_BAUDRATE 9600
// </h>
#define _USART_Driver_(n) Driver_USART##n
#define USART_Driver_(n) _USART_Driver_(n)
extern ARM_DRIVER_USART USART_Driver_(USART_DRV_NUM);
#define ptrUSART (&USART_Driver_(USART_DRV_NUM))
/**
Initialize stdout
\return 0 on success, or -1 on error.
*/
int stdout_init (void) {
int32_t status;
status = ptrUSART->Initialize(NULL);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->PowerControl(ARM_POWER_FULL);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_MODE_ASYNCHRONOUS |
ARM_USART_DATA_BITS_8 |
ARM_USART_PARITY_NONE |
ARM_USART_STOP_BITS_1 |
ARM_USART_FLOW_CONTROL_NONE,
USART_BAUDRATE);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_CONTROL_TX, 1);
if (status != ARM_DRIVER_OK) return (-1);
return (0);
}
/**
Put a character to the stdout
\param[in] ch Character to output
\return The character written, or -1 on write error.
*/
int stdout_putchar (int ch) {
uint8_t buf[1];
buf[0] = ch;
if (ptrUSART->Send(buf, 1) != ARM_DRIVER_OK) {
return (-1);
}
while (ptrUSART->GetTxCount() != 1);
return (ch);
}