Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * \file |
| 32 | * |
| 33 | * Implements UART console. |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /*---------------------------------------------------------------------------- |
| 38 | * Headers |
| 39 | *----------------------------------------------------------------------------*/ |
| 40 | |
| 41 | #include "board.h" |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include <stdint.h> |
| 45 | |
| 46 | #include "ringbuffer.h" |
| 47 | |
| 48 | /*---------------------------------------------------------------------------- |
| 49 | * Definitions |
| 50 | *----------------------------------------------------------------------------*/ |
| 51 | |
| 52 | /*---------------------------------------------------------------------------- |
| 53 | * Variables |
| 54 | *----------------------------------------------------------------------------*/ |
| 55 | |
| 56 | /** Is Console Initialized. */ |
| 57 | static uint8_t _ucIsConsoleInitialized=0; |
| 58 | /** Ring buffer to queue data to be sent */ |
| 59 | static ringbuf uart_tx_buffer; |
| 60 | |
| 61 | /** |
| 62 | * \brief Configures an USART peripheral with the specified parameters. |
| 63 | * |
| 64 | * \param baudrate Baudrate at which the USART should operate (in Hz). |
| 65 | * \param masterClock Frequency of the system master clock (in Hz). |
| 66 | */ |
| 67 | extern void UART_Configure( uint32_t baudrate, uint32_t masterClock) |
| 68 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 69 | const Pin pPins[] = CONSOLE_PINS; |
| 70 | Uart *pUart = CONSOLE_UART; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 71 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 72 | /* Configure PIO */ |
| 73 | PIO_Configure(pPins, PIO_LISTSIZE(pPins)); |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 74 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 75 | /* Configure PMC */ |
| 76 | PMC->PMC_PCER0 = 1 << CONSOLE_ID; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 77 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 78 | /* Reset and disable receiver & transmitter */ |
| 79 | pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX |
| 80 | | UART_CR_RXDIS | UART_CR_TXDIS; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 81 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 82 | /* Configure mode */ |
| 83 | pUart->UART_MR = UART_MR_PAR_NO; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 84 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 85 | /* Configure baudrate */ |
| 86 | /* Asynchronous, no oversampling */ |
| 87 | pUart->UART_BRGR = (masterClock / baudrate) / 16; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 88 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 89 | /* Disable PDC channel */ |
| 90 | pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 91 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 92 | /* Reset transmit ring buffer */ |
| 93 | rbuf_reset(&uart_tx_buffer); |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 94 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 95 | /* Enable TX interrupts */ |
| 96 | pUart->UART_IER = UART_IER_TXRDY; |
| 97 | NVIC_EnableIRQ(CONSOLE_IRQ); |
| 98 | |
| 99 | /* Enable receiver and transmitter */ |
| 100 | pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 101 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 102 | /* Remember the configuration is complete */ |
| 103 | _ucIsConsoleInitialized=1 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * \brief Disables the USART peripheral and related IRQ |
| 108 | */ |
| 109 | void UART_Exit(void) |
| 110 | { |
| 111 | if (!_ucIsConsoleInitialized) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | Uart *pUart = CONSOLE_UART; |
| 116 | pUart->UART_IDR = UART_IDR_TXRDY; |
| 117 | pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS | UART_CR_RSTSTA; |
| 118 | PMC->PMC_PCDR0 = 1 << CONSOLE_ID; |
| 119 | NVIC_DisableIRQ(CONSOLE_IRQ); |
| 120 | } |
| 121 | |
| 122 | /** Interrupt Service routine to transmit queued data */ |
| 123 | void CONSOLE_ISR(void) |
| 124 | { |
| 125 | Uart *uart = CONSOLE_UART; |
| 126 | if (uart->UART_SR & UART_SR_TXRDY) { |
| 127 | if (!rbuf_is_empty(&uart_tx_buffer)) { |
| 128 | uart->UART_THR = rbuf_read(&uart_tx_buffer); |
| 129 | } else { |
| 130 | uart->UART_IDR = UART_IER_TXRDY; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * \brief Outputs a character on the UART line. |
| 137 | * |
| 138 | * \note This function is synchronous (i.e. uses polling). |
| 139 | * \param c Character to send. |
| 140 | */ |
| 141 | extern void UART_PutChar( uint8_t c ) |
| 142 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 143 | Uart *pUart = CONSOLE_UART ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 144 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 145 | /* Initialize console is not already done */ |
| 146 | if ( !_ucIsConsoleInitialized ) |
| 147 | { |
| 148 | UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK); |
| 149 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 150 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 151 | /* Only store input if buffer is not full, else drop it */ |
| 152 | if (!rbuf_is_full(&uart_tx_buffer)) { |
| 153 | rbuf_write(&uart_tx_buffer, c); |
| 154 | if (!(pUart->UART_IMR & UART_IMR_TXRDY)) { |
| 155 | pUart->UART_IER = UART_IER_TXRDY; |
| 156 | CONSOLE_ISR(); |
| 157 | } |
| 158 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * \brief Input a character from the UART line. |
| 163 | * |
| 164 | * \note This function is synchronous |
| 165 | * \return character received. |
| 166 | */ |
| 167 | extern uint32_t UART_GetChar( void ) |
| 168 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 169 | Uart *pUart = CONSOLE_UART ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 170 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 171 | if ( !_ucIsConsoleInitialized ) |
| 172 | { |
| 173 | UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK); |
| 174 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 175 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 176 | while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 ) |
| 177 | WDT_Restart(WDT); |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 178 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 179 | return pUart->UART_RHR ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * \brief Check if there is Input from UART line. |
| 184 | * |
| 185 | * \return true if there is Input. |
| 186 | */ |
| 187 | extern uint32_t UART_IsRxReady( void ) |
| 188 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 189 | Uart *pUart = CONSOLE_UART; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 190 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 191 | if ( !_ucIsConsoleInitialized ) |
| 192 | { |
| 193 | UART_Configure( CONSOLE_BAUDRATE, BOARD_MCK ) ; |
| 194 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 195 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 196 | return (pUart->UART_SR & UART_SR_RXRDY) > 0 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Displays the content of the given frame on the UART0. |
| 201 | * |
| 202 | * \param pucFrame Pointer to the frame to dump. |
| 203 | * \param dwSize Buffer size in bytes. |
| 204 | */ |
| 205 | extern void UART_DumpFrame( uint8_t* pucFrame, uint32_t dwSize ) |
| 206 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 207 | uint32_t dw ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 208 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 209 | for ( dw=0 ; dw < dwSize ; dw++ ) |
| 210 | { |
| 211 | printf( "%02X ", pucFrame[dw] ) ; |
| 212 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 213 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 214 | printf( "\n\r" ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Displays the content of the given buffer on the UART0. |
| 219 | * |
| 220 | * \param pucBuffer Pointer to the buffer to dump. |
| 221 | * \param dwSize Buffer size in bytes. |
| 222 | * \param dwAddress Start address to display |
| 223 | */ |
| 224 | extern void UART_DumpMemory( uint8_t* pucBuffer, uint32_t dwSize, uint32_t dwAddress ) |
| 225 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 226 | uint32_t i ; |
| 227 | uint32_t j ; |
| 228 | uint32_t dwLastLineStart ; |
| 229 | uint8_t* pucTmp ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 230 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 231 | for ( i=0 ; i < (dwSize / 16) ; i++ ) |
| 232 | { |
| 233 | printf( "0x%08X: ", (unsigned int)(dwAddress + (i*16)) ) ; |
| 234 | pucTmp = (uint8_t*)&pucBuffer[i*16] ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 235 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 236 | for ( j=0 ; j < 4 ; j++ ) |
| 237 | { |
| 238 | printf( "%02X%02X%02X%02X ", pucTmp[0], pucTmp[1], pucTmp[2], pucTmp[3] ) ; |
| 239 | pucTmp += 4 ; |
| 240 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 241 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 242 | pucTmp=(uint8_t*)&pucBuffer[i*16] ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 243 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 244 | for ( j=0 ; j < 16 ; j++ ) |
| 245 | { |
| 246 | UART_PutChar( *pucTmp++ ) ; |
| 247 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 248 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 249 | printf( "\n\r" ) ; |
| 250 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 251 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 252 | if ( (dwSize%16) != 0 ) |
| 253 | { |
| 254 | dwLastLineStart=dwSize - (dwSize%16) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 255 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 256 | printf( "0x%08X: ", (unsigned int)(dwAddress + dwLastLineStart) ) ; |
| 257 | for ( j=dwLastLineStart ; j < dwLastLineStart+16 ; j++ ) |
| 258 | { |
| 259 | if ( (j!=dwLastLineStart) && (j%4 == 0) ) |
| 260 | { |
| 261 | printf( " " ) ; |
| 262 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 263 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 264 | if ( j < dwSize ) |
| 265 | { |
| 266 | printf( "%02X", pucBuffer[j] ) ; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | printf(" ") ; |
| 271 | } |
| 272 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 273 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 274 | printf( " " ) ; |
| 275 | for ( j=dwLastLineStart ; j < dwSize ; j++ ) |
| 276 | { |
| 277 | UART_PutChar( pucBuffer[j] ) ; |
| 278 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 279 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 280 | printf( "\n\r" ) ; |
| 281 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Reads an integer |
| 286 | * |
| 287 | * \param pdwValue Pointer to the uint32_t variable to contain the input value. |
| 288 | */ |
| 289 | extern uint32_t UART_GetInteger( uint32_t* pdwValue ) |
| 290 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 291 | uint8_t ucKey ; |
| 292 | uint8_t ucNbNb=0 ; |
| 293 | uint32_t dwValue=0 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 294 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 295 | while ( 1 ) |
| 296 | { |
| 297 | ucKey=UART_GetChar() ; |
| 298 | UART_PutChar( ucKey ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 299 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 300 | if ( ucKey >= '0' && ucKey <= '9' ) |
| 301 | { |
| 302 | dwValue = (dwValue * 10) + (ucKey - '0'); |
| 303 | ucNbNb++ ; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | if ( ucKey == 0x0D || ucKey == ' ' ) |
| 308 | { |
| 309 | if ( ucNbNb == 0 ) |
| 310 | { |
| 311 | printf( "\n\rWrite a number and press ENTER or SPACE!\n\r" ) ; |
| 312 | return 0 ; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | printf( "\n\r" ) ; |
| 317 | *pdwValue=dwValue ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 318 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 319 | return 1 ; |
| 320 | } |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | printf( "\n\r'%c' not a number!\n\r", ucKey ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 325 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 326 | return 0 ; |
| 327 | } |
| 328 | } |
| 329 | WDT_Restart(WDT); |
| 330 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Reads an integer and check the value |
| 335 | * |
| 336 | * \param pdwValue Pointer to the uint32_t variable to contain the input value. |
| 337 | * \param dwMin Minimum value |
| 338 | * \param dwMax Maximum value |
| 339 | */ |
| 340 | extern uint32_t UART_GetIntegerMinMax( uint32_t* pdwValue, uint32_t dwMin, uint32_t dwMax ) |
| 341 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 342 | uint32_t dwValue=0 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 343 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 344 | if ( UART_GetInteger( &dwValue ) == 0 ) |
| 345 | { |
| 346 | return 0 ; |
| 347 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 348 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 349 | if ( dwValue < dwMin || dwValue > dwMax ) |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 350 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 351 | printf( "\n\rThe number have to be between %d and %d\n\r", (int)dwMin, (int)dwMax ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 352 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 353 | return 0 ; |
| 354 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 355 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 356 | printf( "\n\r" ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 357 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 358 | *pdwValue = dwValue ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 359 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 360 | return 1 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Reads an hexadecimal number |
| 365 | * |
| 366 | * \param pdwValue Pointer to the uint32_t variable to contain the input value. |
| 367 | */ |
| 368 | extern uint32_t UART_GetHexa32( uint32_t* pdwValue ) |
| 369 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 370 | uint8_t ucKey ; |
| 371 | uint32_t dw = 0 ; |
| 372 | uint32_t dwValue = 0 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 373 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 374 | for ( dw=0 ; dw < 8 ; dw++ ) |
| 375 | { |
| 376 | ucKey = UART_GetChar() ; |
| 377 | UART_PutChar( ucKey ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 378 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 379 | if ( ucKey >= '0' && ucKey <= '9' ) |
| 380 | { |
| 381 | dwValue = (dwValue * 16) + (ucKey - '0') ; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | if ( ucKey >= 'A' && ucKey <= 'F' ) |
| 386 | { |
| 387 | dwValue = (dwValue * 16) + (ucKey - 'A' + 10) ; |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | if ( ucKey >= 'a' && ucKey <= 'f' ) |
| 392 | { |
| 393 | dwValue = (dwValue * 16) + (ucKey - 'a' + 10) ; |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | printf( "\n\rIt is not a hexa character!\n\r" ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 398 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 399 | return 0 ; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 404 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 405 | printf("\n\r" ) ; |
| 406 | *pdwValue = dwValue ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 407 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 408 | return 1 ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | #if defined __ICCARM__ /* IAR Ewarm 5.41+ */ |
| 412 | /** |
| 413 | * \brief Outputs a character on the UART. |
| 414 | * |
| 415 | * \param c Character to output. |
| 416 | * |
| 417 | * \return The character that was output. |
| 418 | */ |
| 419 | extern WEAK signed int putchar( signed int c ) |
| 420 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 421 | UART_PutChar( c ) ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 422 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 423 | return c ; |
Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 424 | } |
| 425 | #endif // defined __ICCARM__ |
| 426 | |