UART with stdio functions

This commit is contained in:
Christina Quast
2014-12-05 13:03:27 +01:00
parent d51b5f4843
commit f1192c6cad
2 changed files with 160 additions and 1 deletions

View File

@@ -67,9 +67,12 @@ OBJ = obj
# Tool suffix when cross-compiling
CROSS_COMPILE = arm-none-eabi-
LIBC_PATH=../Baselibc
LIBS = -Wl,--start-group -lgcc -lc -Wl,--end-group
#LIB_PATH+=-L=/lib/thumb2
#LIB_PATH+=-L=/../lib/gcc/arm-none-eabi/4.4.1/thumb2
LIB+=-L=$(LIBC_PATH) -lc
# Compilation tools
CC = $(CROSS_COMPILE)gcc
@@ -83,6 +86,7 @@ NM = $(CROSS_COMPILE)nm
# Flags
INCLUDES = -Iinclude -Isam3s_examples_include
INCLUDES += -Icmsis
INCLUDES += -I$(LIBC_PATH)/include
#INCLUDES += -I$(LIBRARIES)
CFLAGS += -Wall -Wchar-subscripts -Wcomment -Wformat=2 -Wimplicit-int
@@ -107,7 +111,7 @@ CFLAGS += -Dprintf=iprintf
CFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m3 -mthumb # -mfix-cortex-m3-ldrd
CFLAGS += -ffunction-sections -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL)
ASFLAGS = -mcpu=cortex-m3 -mthumb -Wall -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
LDFLAGS = -mcpu=cortex-m3 -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=ResetException -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols
LDFLAGS = -mcpu=cortex-m3 -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=ResetException -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols $(LIB)
#LD_OPTIONAL=-Wl,--print-gc-sections -Wl,--stats
#-------------------------------------------------------------------------------

View File

@@ -0,0 +1,155 @@
#include "board.h"
#include <stdio.h>
#include <string.h>
/*----------------------------------------------------------------------------
* * Definitions
* *----------------------------------------------------------------------------*/
/** Console baudrate always using 115200. */
#define CONSOLE_BAUDRATE 115200
/** Usart Hw interface used by the console (UART0). */
#define CONSOLE_USART UART0
/** Usart Hw ID used by the console (UART0). */
#define CONSOLE_ID ID_UART0
/** Pins description corresponding to Rxd,Txd, (UART pins) */
#define CONSOLE_PINS {PINS_UART}
/*----------------------------------------------------------------------------
* * Variables
* *----------------------------------------------------------------------------*/
const Pin redled = {LED_RED, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
const Pin greenled = {LED_GREEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
static const Pin *led;
/** Is Console Initialized. */
static uint8_t _ucIsConsoleInitialized=0 ;
/**
* \brief Configures an USART peripheral with the specified parameters.
*
* \param baudrate Baudrate at which the USART should operate (in Hz).
* \param masterClock Frequency of the system master clock (in Hz).
*/
extern void UART_Configure( uint32_t baudrate, uint32_t masterClock)
{
const Pin pPins[] = CONSOLE_PINS;
Uart *pUart = CONSOLE_USART;
/* Configure PIO */
PIO_Configure(pPins, PIO_LISTSIZE(pPins));
/* Configure PMC */
/* PMC_PCER0 : (PMC Offset: 0x0010) Peripheral Clock Enable Register 0 -------- */
PMC->PMC_PCER0 = 1 << CONSOLE_ID;
/* Reset and disable receiver & transmitter */
pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX
| UART_CR_RXDIS | UART_CR_TXDIS;
/* Configure mode */
pUart->UART_MR = UART_MR_PAR_NO; // No parity
/* Configure baudrate */
/* Asynchronous, no oversampling */
// BRG: Baud rate generator
pUart->UART_BRGR = (masterClock / baudrate) / 16;
/* Disable PDC channel */
// PDC: Peripheral DMA
// TCR: Transfer Control Register
pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
/* Enable receiver and transmitter */
pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
_ucIsConsoleInitialized=1 ;
}
/**
* \brief Outputs a character on the UART line.
*
* \note This function is synchronous (i.e. uses polling).
* \param c Character to send.
*/
extern void UART_PutChar( uint8_t c )
{
Uart *pUart=CONSOLE_USART ;
if ( !_ucIsConsoleInitialized )
{
UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
}
/* Wait for the transmitter to be ready */
// UART_SR: Statzs register
while ( (pUart->UART_SR & UART_SR_TXEMPTY) == 0 ) ;
/* Send character */
// THR: transmit holding register
pUart->UART_THR=c ;
}
/**
* \brief Input a character from the UART line.
*
* \note This function is synchronous
* \return character received.
*/
extern uint32_t UART_GetChar( void )
{
Uart *pUart=CONSOLE_USART ;
if ( !_ucIsConsoleInitialized )
{
UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
}
while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 ) ;
return pUart->UART_RHR ;
}
void Configure_LED() {
PIO_Configure(&greenled, PIO_LISTSIZE(greenled));
PIO_Configure(&redled, PIO_LISTSIZE(redled));
PIO_Set(&redled);
PIO_Set(&greenled);
led = &redled;
}
void UART_PutString(char *str, int len) {
int i;
for (i=0; i<len; i++) {
UART_PutChar(*str++);
}
}
int main() {
char *cmdp;
// FIXME: initialize system clock done in lowlevelinit?
Configure_LED();
int ret = asprintf(&cmdp, "Clockval: %d\r\n", BOARD_MCK);
if (ret != strlen(cmdp)){
PIO_Clear(&redled);
} else {
PIO_Clear(&greenled);
while (1) {
UART_PutString(cmdp, strlen(cmdp));
}
}
return 0;
}