Christina Quast | f1192c6 | 2014-12-05 13:03:27 +0100 | [diff] [blame] | 1 | #include "board.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | /*---------------------------------------------------------------------------- |
| 7 | * * Definitions |
| 8 | * *----------------------------------------------------------------------------*/ |
| 9 | |
| 10 | /** Console baudrate always using 115200. */ |
| 11 | #define CONSOLE_BAUDRATE 115200 |
| 12 | /** Usart Hw interface used by the console (UART0). */ |
| 13 | #define CONSOLE_USART UART0 |
| 14 | /** Usart Hw ID used by the console (UART0). */ |
| 15 | #define CONSOLE_ID ID_UART0 |
| 16 | /** Pins description corresponding to Rxd,Txd, (UART pins) */ |
| 17 | #define CONSOLE_PINS {PINS_UART} |
| 18 | |
| 19 | /*---------------------------------------------------------------------------- |
| 20 | * * Variables |
| 21 | * *----------------------------------------------------------------------------*/ |
| 22 | |
| 23 | const Pin redled = {LED_RED, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 24 | const Pin greenled = {LED_GREEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 25 | |
| 26 | static const Pin *led; |
| 27 | |
| 28 | /** Is Console Initialized. */ |
| 29 | static uint8_t _ucIsConsoleInitialized=0 ; |
| 30 | |
| 31 | /** |
| 32 | * \brief Configures an USART peripheral with the specified parameters. |
| 33 | * |
| 34 | * \param baudrate Baudrate at which the USART should operate (in Hz). |
| 35 | * \param masterClock Frequency of the system master clock (in Hz). |
| 36 | */ |
| 37 | extern void UART_Configure( uint32_t baudrate, uint32_t masterClock) |
| 38 | { |
| 39 | const Pin pPins[] = CONSOLE_PINS; |
| 40 | Uart *pUart = CONSOLE_USART; |
| 41 | |
| 42 | |
| 43 | /* Configure PIO */ |
| 44 | PIO_Configure(pPins, PIO_LISTSIZE(pPins)); |
| 45 | |
| 46 | /* Configure PMC */ |
| 47 | /* PMC_PCER0 : (PMC Offset: 0x0010) Peripheral Clock Enable Register 0 -------- */ |
| 48 | PMC->PMC_PCER0 = 1 << CONSOLE_ID; |
| 49 | |
| 50 | /* Reset and disable receiver & transmitter */ |
| 51 | pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX |
| 52 | | UART_CR_RXDIS | UART_CR_TXDIS; |
| 53 | |
| 54 | /* Configure mode */ |
| 55 | pUart->UART_MR = UART_MR_PAR_NO; // No parity |
| 56 | |
| 57 | /* Configure baudrate */ |
| 58 | /* Asynchronous, no oversampling */ |
| 59 | // BRG: Baud rate generator |
| 60 | pUart->UART_BRGR = (masterClock / baudrate) / 16; |
| 61 | |
| 62 | /* Disable PDC channel */ |
| 63 | // PDC: Peripheral DMA |
| 64 | // TCR: Transfer Control Register |
| 65 | pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS; |
| 66 | |
| 67 | /* Enable receiver and transmitter */ |
| 68 | |
| 69 | pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN; |
| 70 | |
| 71 | _ucIsConsoleInitialized=1 ; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * \brief Outputs a character on the UART line. |
| 77 | * |
| 78 | * \note This function is synchronous (i.e. uses polling). |
| 79 | * \param c Character to send. |
| 80 | */ |
| 81 | extern void UART_PutChar( uint8_t c ) |
| 82 | { |
| 83 | Uart *pUart=CONSOLE_USART ; |
| 84 | |
| 85 | if ( !_ucIsConsoleInitialized ) |
| 86 | { |
| 87 | UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK); |
| 88 | } |
| 89 | |
| 90 | /* Wait for the transmitter to be ready */ |
| 91 | // UART_SR: Statzs register |
| 92 | while ( (pUart->UART_SR & UART_SR_TXEMPTY) == 0 ) ; |
| 93 | |
| 94 | /* Send character */ |
| 95 | // THR: transmit holding register |
| 96 | pUart->UART_THR=c ; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * \brief Input a character from the UART line. |
| 104 | * |
| 105 | * \note This function is synchronous |
| 106 | * \return character received. |
| 107 | */ |
| 108 | extern uint32_t UART_GetChar( void ) |
| 109 | { |
| 110 | Uart *pUart=CONSOLE_USART ; |
| 111 | |
| 112 | if ( !_ucIsConsoleInitialized ) |
| 113 | { |
| 114 | UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK); |
| 115 | } |
| 116 | |
| 117 | while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 ) ; |
| 118 | |
| 119 | return pUart->UART_RHR ; |
| 120 | } |
| 121 | |
| 122 | void Configure_LED() { |
| 123 | PIO_Configure(&greenled, PIO_LISTSIZE(greenled)); |
| 124 | PIO_Configure(&redled, PIO_LISTSIZE(redled)); |
| 125 | PIO_Set(&redled); |
| 126 | PIO_Set(&greenled); |
| 127 | led = &redled; |
| 128 | } |
| 129 | |
| 130 | void UART_PutString(char *str, int len) { |
| 131 | int i; |
| 132 | for (i=0; i<len; i++) { |
| 133 | UART_PutChar(*str++); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | int main() { |
| 138 | char *cmdp; |
| 139 | // FIXME: initialize system clock done in lowlevelinit? |
| 140 | |
| 141 | Configure_LED(); |
| 142 | |
Christina Quast | 72ff456 | 2014-12-05 15:51:43 +0100 | [diff] [blame] | 143 | size_t ret = asprintf(&cmdp, "Clockval: %d\r\n", BOARD_MCK); |
Christina Quast | f1192c6 | 2014-12-05 13:03:27 +0100 | [diff] [blame] | 144 | |
| 145 | if (ret != strlen(cmdp)){ |
| 146 | PIO_Clear(&redled); |
| 147 | } else { |
| 148 | PIO_Clear(&greenled); |
| 149 | while (1) { |
| 150 | UART_PutString(cmdp, strlen(cmdp)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |