This is a part of the Kinetiszer project.

Dependencies:   inc

Dependents:   kinetisizer

Committer:
Clemo
Date:
Tue Oct 28 20:09:12 2014 +0000
Revision:
1:8ae4ab73ca6a
Parent:
0:cb80470434eb
First publication (untested)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Clemo 0:cb80470434eb 1 /*
Clemo 0:cb80470434eb 2 * @brief Serial port driver
Clemo 0:cb80470434eb 3 *
Clemo 0:cb80470434eb 4 * @note
Clemo 0:cb80470434eb 5 * Copyright (C) Elektor, 2014
Clemo 0:cb80470434eb 6 * All rights reserved.
Clemo 0:cb80470434eb 7 *
Clemo 0:cb80470434eb 8 * @par
Clemo 0:cb80470434eb 9 * This software is supplied "AS IS" without any warranties of any kind,
Clemo 0:cb80470434eb 10 * and Elektor and its licensor disclaim any and all warranties, express
Clemo 0:cb80470434eb 11 * or implied, including all implied warranties of merchantability,
Clemo 0:cb80470434eb 12 * fitness for a particular purpose and non-infringement of intellectual
Clemo 0:cb80470434eb 13 * property rights. Elektor assumes no responsibility or liability for
Clemo 0:cb80470434eb 14 * the use of the software, conveys no license or rights under any patent,
Clemo 0:cb80470434eb 15 * copyright, mask work right, or any other intellectual property rights in
Clemo 0:cb80470434eb 16 * or to any products. Elektor reserves the right to make changes in the
Clemo 0:cb80470434eb 17 * software without notification. Elektor also makes no representation or
Clemo 0:cb80470434eb 18 * warranty that such application will be suitable for the specified use
Clemo 0:cb80470434eb 19 * without further testing or modification.
Clemo 0:cb80470434eb 20 *
Clemo 0:cb80470434eb 21 * @par
Clemo 0:cb80470434eb 22 * Permission to use, copy, modify, and distribute this software and its
Clemo 0:cb80470434eb 23 * documentation is hereby granted, under Elektor's and its licensor's
Clemo 0:cb80470434eb 24 * relevant copyrights in the software, without fee. This copyright,
Clemo 0:cb80470434eb 25 * permission, and disclaimer notice must appear in all copies of this code.
Clemo 0:cb80470434eb 26 */
Clemo 0:cb80470434eb 27
Clemo 0:cb80470434eb 28 //#include "chip.h"
Clemo 0:cb80470434eb 29 #include "atmegatron.h"
Clemo 0:cb80470434eb 30
Clemo 0:cb80470434eb 31
Clemo 0:cb80470434eb 32 size_t serial_write(uint8_t value);
Clemo 0:cb80470434eb 33 int serial_read(void);
Clemo 0:cb80470434eb 34 void serial_flush(void);
Clemo 0:cb80470434eb 35 int serial_available(void);
Clemo 0:cb80470434eb 36 void serial_begin(uint32_t baudrate);
Clemo 0:cb80470434eb 37
Clemo 0:cb80470434eb 38
Clemo 0:cb80470434eb 39 const serial2_t Serial =
Clemo 0:cb80470434eb 40 {
Clemo 0:cb80470434eb 41 serial_write,
Clemo 0:cb80470434eb 42 serial_read,
Clemo 0:cb80470434eb 43 serial_flush,
Clemo 0:cb80470434eb 44 serial_available,
Clemo 0:cb80470434eb 45 serial_begin
Clemo 0:cb80470434eb 46 };
Clemo 0:cb80470434eb 47
Clemo 0:cb80470434eb 48 #define STATIC static
Clemo 0:cb80470434eb 49 typedef uint32_t RINGBUFF_T;
Clemo 0:cb80470434eb 50
Clemo 0:cb80470434eb 51 // Transmit and receive ring buffers.
Clemo 0:cb80470434eb 52 //STATIC RINGBUFF_T txring;
Clemo 0:cb80470434eb 53 //STATIC RINGBUFF_T rxring;
Clemo 0:cb80470434eb 54
Clemo 0:cb80470434eb 55 // Transmit and receive ring buffer sizes.
Clemo 0:cb80470434eb 56 #define UART_TX_RBUFFER_SIZE (64)
Clemo 0:cb80470434eb 57 #define UART_RX_RBUFFER_SIZE (64)
Clemo 0:cb80470434eb 58
Clemo 0:cb80470434eb 59 // Transmit and receive buffers.
Clemo 0:cb80470434eb 60 //static uint8_t rxbuff[UART_RX_RBUFFER_SIZE];
Clemo 0:cb80470434eb 61 //static uint8_t txbuff[UART_TX_RBUFFER_SIZE];
Clemo 0:cb80470434eb 62
Clemo 0:cb80470434eb 63
Clemo 0:cb80470434eb 64 static void Init_UART_PinMux(void)
Clemo 0:cb80470434eb 65 {
Clemo 0:cb80470434eb 66 // Pin muxing for serial port is already set by board layer library.
Clemo 0:cb80470434eb 67 // (See board_sysinit.c files in J2B_board/src/)
Clemo 0:cb80470434eb 68 }
Clemo 0:cb80470434eb 69
Clemo 0:cb80470434eb 70
Clemo 0:cb80470434eb 71 void UART_IRQHandler(void)
Clemo 0:cb80470434eb 72 {
Clemo 0:cb80470434eb 73 // Want to handle any errors? Do it here.
Clemo 0:cb80470434eb 74
Clemo 0:cb80470434eb 75 // Use default ring buffer handler.
Clemo 0:cb80470434eb 76 //Chip_UART_IRQRBHandler(LPC_USART,&rxring,&txring);
Clemo 0:cb80470434eb 77 }
Clemo 0:cb80470434eb 78
Clemo 0:cb80470434eb 79
Clemo 0:cb80470434eb 80 size_t serial_write(uint8_t value)
Clemo 0:cb80470434eb 81 {
Clemo 0:cb80470434eb 82 //Chip_UART_SendRB(LPC_USART,&txring,(const uint8_t *)&value,1);
Clemo 0:cb80470434eb 83 return 1;
Clemo 0:cb80470434eb 84 }
Clemo 0:cb80470434eb 85
Clemo 0:cb80470434eb 86
Clemo 0:cb80470434eb 87 int serial_read(void)
Clemo 0:cb80470434eb 88 {
Clemo 0:cb80470434eb 89 uint8_t value = 0;
Clemo 0:cb80470434eb 90 //Chip_UART_ReadRB(LPC_USART,&rxring,&value,1);
Clemo 0:cb80470434eb 91 return (int)value;
Clemo 0:cb80470434eb 92 }
Clemo 0:cb80470434eb 93
Clemo 0:cb80470434eb 94
Clemo 0:cb80470434eb 95 void serial_flush(void)
Clemo 0:cb80470434eb 96 {
Clemo 0:cb80470434eb 97 //RingBuffer_Flush(&txring);
Clemo 0:cb80470434eb 98 }
Clemo 0:cb80470434eb 99
Clemo 0:cb80470434eb 100
Clemo 0:cb80470434eb 101 int serial_available(void)
Clemo 0:cb80470434eb 102 {
Clemo 0:cb80470434eb 103 //return RingBuffer_GetCount(&rxring);
Clemo 0:cb80470434eb 104 return 0;
Clemo 0:cb80470434eb 105 }
Clemo 0:cb80470434eb 106
Clemo 0:cb80470434eb 107
Clemo 0:cb80470434eb 108 void serial_begin(uint32_t baudrate)
Clemo 0:cb80470434eb 109 {
Clemo 0:cb80470434eb 110 Init_UART_PinMux();
Clemo 0:cb80470434eb 111
Clemo 0:cb80470434eb 112 // Setup UART.
Clemo 0:cb80470434eb 113 //Chip_UART_Init(LPC_USART);
Clemo 0:cb80470434eb 114 //Chip_UART_SetBaud(LPC_USART,baudrate);
Clemo 0:cb80470434eb 115 //Chip_UART_ConfigData(LPC_USART,(UART_LCR_WLEN8|UART_LCR_SBS_1BIT));
Clemo 0:cb80470434eb 116 //Chip_UART_SetupFIFOS(LPC_USART,(UART_FCR_FIFO_EN|UART_FCR_TRG_LEV2));
Clemo 0:cb80470434eb 117 //Chip_UART_TXEnable(LPC_USART);
Clemo 0:cb80470434eb 118
Clemo 0:cb80470434eb 119 // Before using the ring buffers, initialize them using the ring buffer init function.
Clemo 0:cb80470434eb 120 // RingBuffer_Init(&rxring,rxbuff,1,UART_RX_RBUFFER_SIZE);
Clemo 0:cb80470434eb 121 // RingBuffer_Init(&txring,txbuff,1,UART_TX_RBUFFER_SIZE);
Clemo 0:cb80470434eb 122
Clemo 0:cb80470434eb 123 // Enable receive data and line status interrupt, the transmit interrupt is handled by the driver.
Clemo 0:cb80470434eb 124 //Chip_UART_IntEnable(LPC_USART,(UART_IER_RBRINT|UART_IER_RLSINT));
Clemo 0:cb80470434eb 125
Clemo 0:cb80470434eb 126 // preemption = 1, sub-priority = 1.
Clemo 0:cb80470434eb 127 // 0 is highest priority, 32 is lowest.
Clemo 0:cb80470434eb 128 // All interrupt priorities default to 0 at power up.
Clemo 0:cb80470434eb 129 //NVIC_SetPriority(UART0_IRQn,2);
Clemo 0:cb80470434eb 130 //NVIC_EnableIRQ(UART0_IRQn);
Clemo 0:cb80470434eb 131 }