diff options
Diffstat (limited to 'c/src/lib/libbsp/lm32/shared/milkymist_console')
3 files changed, 321 insertions, 0 deletions
diff --git a/c/src/lib/libbsp/lm32/shared/milkymist_console/console.c b/c/src/lib/libbsp/lm32/shared/milkymist_console/console.c new file mode 100644 index 0000000000..05ac7803df --- /dev/null +++ b/c/src/lib/libbsp/lm32/shared/milkymist_console/console.c @@ -0,0 +1,226 @@ +/* + * Console driver for Lattice Mico32 (lm32). + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + * + * Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008, + * Micro-Research Finland Oy + * + * COPYRIGHT (c) Yann Sionneau <yann.sionneau@telecom-sudparis.eu>, GSoc 2010 + * Telecom SudParis + */ + +#define NO_BSP_INIT + +#include <rtems.h> +#include <bsp.h> +#include <rtems/libio.h> + +void BSP_uart_polled_write(char ch); +int BSP_uart_polled_read( void ); +char BSP_uart_is_character_ready(char *ch); + +/* console_initialize + * + * This routine initializes the console IO driver. + * + * Input parameters: NONE + * + * Output parameters: NONE + * + * Return values: + */ + +rtems_device_driver console_initialize( + rtems_device_major_number major, + rtems_device_minor_number minor, + void *arg +) +{ + rtems_status_code status; + + printk("console_initialize\n"); + + status = rtems_io_register_name( + "/dev/console", + major, + (rtems_device_minor_number) 0 + ); + + if (status != RTEMS_SUCCESSFUL) + rtems_fatal_error_occurred(status); + + return RTEMS_SUCCESSFUL; +} + +/* is_character_ready + * + * This routine returns TRUE if a character is available. + * + * Input parameters: NONE + * + * Output parameters: NONE + * + * Return values: + */ + +bool is_character_ready( + char *ch +) +{ + return BSP_uart_is_character_ready(ch); +} + +/* inbyte + * + * This routine reads a character from the SOURCE. + * + * Input parameters: NONE + * + * Output parameters: NONE + * + * Return values: + * character read from SOURCE + */ + +int inbyte( void ) +{ + /* + * If polling, wait until a character is available. + */ + + return BSP_uart_polled_read(); +} + +/* outbyte + * + * This routine transmits a character out the SOURCE. It may support + * XON/XOFF flow control. + * + * Input parameters: + * ch - character to be transmitted + * + * Output parameters: NONE + */ + +void outbyte( + char ch +) +{ + /* + * If polling, wait for the transmitter to be ready. + * Check for flow control requests and process. + * Then output the character. + */ + + BSP_uart_polled_write(ch); +} + +/* + * Open entry point + */ + +rtems_device_driver console_open( + rtems_device_major_number major, + rtems_device_minor_number minor, + void * arg +) +{ + return RTEMS_SUCCESSFUL; +} + +/* + * Close entry point + */ + +rtems_device_driver console_close( + rtems_device_major_number major, + rtems_device_minor_number minor, + void * arg +) +{ + return RTEMS_SUCCESSFUL; +} + +/* + * read bytes from the serial port. We only have stdin. + */ + +rtems_device_driver console_read( + rtems_device_major_number major, + rtems_device_minor_number minor, + void * arg +) +{ + rtems_libio_rw_args_t *rw_args; + char *buffer; + int maximum; + int count = 0; + + rw_args = (rtems_libio_rw_args_t *) arg; + + buffer = rw_args->buffer; + maximum = rw_args->count; + + for (count = 0; count < maximum; count++) { + buffer[ count ] = inbyte(); + if (buffer[ count ] == '\n' || buffer[ count ] == '\r') { + buffer[ count++ ] = '\n'; + break; + } + } + + rw_args->bytes_moved = count; + return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED; +} + +/* + * write bytes to the serial port. Stdout and stderr are the same. + */ + +rtems_device_driver console_write( + rtems_device_major_number major, + rtems_device_minor_number minor, + void * arg +) +{ + int count; + int maximum; + rtems_libio_rw_args_t *rw_args; + char *buffer; + + rw_args = (rtems_libio_rw_args_t *) arg; + + buffer = rw_args->buffer; + maximum = rw_args->count; + + for (count = 0; count < maximum; count++) { + if ( buffer[ count ] == '\n') { + outbyte('\r'); + } + outbyte( buffer[ count ] ); + } + + rw_args->bytes_moved = maximum; + return 0; +} + +/* + * IO Control entry point + */ + +rtems_device_driver console_control( + rtems_device_major_number major, + rtems_device_minor_number minor, + void * arg +) +{ + return RTEMS_SUCCESSFUL; +} + +BSP_output_char_function_type BSP_output_char = BSP_uart_polled_write; +BSP_polling_getchar_function_type BSP_poll_char = BSP_uart_polled_read; diff --git a/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.c b/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.c new file mode 100644 index 0000000000..229ee40985 --- /dev/null +++ b/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.c @@ -0,0 +1,69 @@ +/* + * Uart driver for Lattice Mico32 (lm32) UART + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + * + * COPYRIGHT (c) Yann Sionneau <yann.sionneau@telecom-sudparis.eu> (GSoC 2010) + * Telecom SudParis + */ + +#include "../include/system_conf.h" +#include "uart.h" +#include <rtems/libio.h> + +static inline int uartread(unsigned int reg) +{ + return *((int*)(reg)); +} + +static inline void uartwrite(unsigned int reg, int value) +{ + *((int*)(reg)) = value; +} + +void BSP_uart_init(int baud) +{ + + /* Set baud rate */ + uartwrite(MM_UART_DIV, CPU_FREQUENCY/baud/16); +} + +void BSP_uart_polled_write(char ch) +{ + int ip; + /* Wait until THR is empty. */ + uartwrite(MM_UART_RXTX, ch); + do { + lm32_read_interrupts(ip); + } while (! (ip & MM_IRQ_UARTTX) ); + lm32_interrupt_ack(MM_IRQ_UARTTX); +} + +char BSP_uart_polled_read( void ) +{ + int ip; + /* Wait until there is a byte in RBR */ + do { + lm32_read_interrupts(ip); + } while(! (ip & MM_IRQ_UARTRX) ); + lm32_interrupt_ack(MM_IRQ_UARTRX); + return (char) uartread(MM_UART_RXTX); +} + +char BSP_uart_is_character_ready(char *ch) +{ + int ip; + lm32_read_interrupts(ip); + if (ip & MM_IRQ_UARTRX) + { + *ch = (char) uartread(MM_UART_RXTX); + lm32_interrupt_ack(MM_IRQ_UARTRX); + return true; + } + *ch = '0'; + return false; +} diff --git a/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.h b/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.h new file mode 100644 index 0000000000..1aded4231d --- /dev/null +++ b/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.h @@ -0,0 +1,26 @@ +/* + * This file contains definitions for LatticeMico32 UART + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + * + * COPYRIGHT (c) Yann Sionneau <yann.sionneau@telecom-sudparis.eu> (GSoC 2010) + * Telecom SudParis + */ + +#ifndef _BSPUART_H +#define _BSPUART_H + +void BSP_uart_init(int baud); + +#define MM_UART_RXTX (0xe0000000) + +#define MM_UART_DIV (0xe0000004) + +#define MM_IRQ_UARTTX (0x00000010) /* 4 */ +#define MM_IRQ_UARTRX (0x00000008) /* 3 */ + +#endif /* _BSPUART_H */ |