Contiki 3.x
console.c
1 /*
2  * Copyright (c) 2012, STMicroelectronics.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *
30  */
31 /*---------------------------------------------------------------------------*/
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "console.h"
35 #include "stm32l1xx.h"
36 #include "stm32l1xx_hal_dma.h"
37 #include "stm32l1xx_hal_uart.h"
38 #include "st-lib.h"
39 /*---------------------------------------------------------------------------*/
40 extern st_lib_uart_handle_typedef st_lib_uart_handle;
41 /*---------------------------------------------------------------------------*/
42 /**
43  * @brief Initialises Nucleo UART port for user IO
44  * @retval 0
45  */
46 int
47 console_init(void)
48 {
49  st_lib_uart_handle.Instance = USART2;
50 
51  st_lib_uart_handle.Init.BaudRate = 115200;
52  st_lib_uart_handle.Init.WordLength = UART_WORDLENGTH_8B;
53  st_lib_uart_handle.Init.StopBits = UART_STOPBITS_1;
54  st_lib_uart_handle.Init.Parity = UART_PARITY_NONE;
55  st_lib_uart_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
56  st_lib_uart_handle.Init.Mode = UART_MODE_TX_RX;
57 
58  st_lib_hal_uart_init(&st_lib_uart_handle);
59 
60  return 0;
61 }
62 /*---------------------------------------------------------------------------*/
63 /** @brief Sends a character to serial port
64  * @param ch Character to send
65  * @retval Character sent
66  */
67 int
68 uart_send_char(int ch)
69 {
70  st_lib_hal_uart_transmit(&st_lib_uart_handle, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
71  return ch;
72 }
73 /*---------------------------------------------------------------------------*/
74 /** @brief Receives a character from serial port
75  * @retval Character received
76  */
77 int
78 uart_receive_char(void)
79 {
80  uint8_t ch;
81  st_lib_hal_uart_receive(&st_lib_uart_handle, &ch, 1, HAL_MAX_DELAY);
82 
83  /* Echo character back to console */
84  st_lib_hal_uart_transmit(&st_lib_uart_handle, &ch, 1, HAL_MAX_DELAY);
85 
86  /* And cope with Windows */
87  if(ch == '\r') {
88  uint8_t ret = '\n';
89  st_lib_hal_uart_transmit(&st_lib_uart_handle, &ret, 1, HAL_MAX_DELAY);
90  }
91 
92  return ch;
93 }
94 /*---------------------------------------------------------------------------*/
95 #if defined(__IAR_SYSTEMS_ICC__)
96 
97 size_t __write(int Handle, const unsigned char *Buf, size_t Bufsize);
98 size_t __read(int Handle, unsigned char *Buf, size_t Bufsize);
99 
100 /** @brief IAR specific low level standard input
101  * @param handle IAR internal handle
102  * @param buf Buffer where to store characters read from stdin
103  * @param bufsize Number of characters to read
104  * @retval Number of characters read
105  */
106 size_t
107 __read(int handle, unsigned char *buf, size_t bufsize)
108 {
109  int i;
110 
111  if(handle != 0) {
112  return -1;
113  }
114 
115  for(i = 0; i < bufsize; i++) {
116  buf[i] = uart_receive_char();
117  }
118 
119  return bufsize;
120 }
121 /** @brief IAR specific low level standard output
122  * @param handle IAR internal handle
123  * @param buf Buffer containing characters to be written to stdout
124  * @param bufsize Number of characters to write
125  * @retval Number of characters read
126  */
127 size_t
128 __write(int handle, const unsigned char *buf, size_t bufsize)
129 {
130  int i;
131 
132  if(handle != 1 && handle != 2) {
133  return -1;
134  }
135 
136  for(i = 0; i < bufsize; i++) {
137  uart_send_char(buf[i]);
138  }
139 
140  return bufsize;
141 }
142 /*---------------------------------------------------------------------------*/
143 #elif defined(__CC_ARM)
144 /**
145  * @brief fputc call for standard output implementation
146  * @param ch Character to print
147  * @param f File pointer
148  * @retval Character printed
149  */
150 int
151 fputc(int ch, FILE *f)
152 {
153  return uart_send_char(ch);
154 }
155 /** @brief fgetc call for standard input implementation
156  * @param f File pointer
157  * @retval Character acquired from standard input
158  */
159 int
160 fgetc(FILE *f)
161 {
162  return uart_receive_char();
163 }
164 /*---------------------------------------------------------------------------*/
165 #elif defined(__GNUC__)
166 
167 /** @brief putchar call for standard output implementation
168  * @param ch Character to print
169  * @retval Character printed
170  */
171 int
172 __io_putchar(int ch)
173 {
174  return uart_send_char(ch);
175 }
176 /** @brief getchar call for standard input implementation
177  * @param None
178  * @retval Character acquired from standard input
179  */
180 int
181 __io_getchar(void)
182 {
183  return uart_receive_char();
184 }
185 /*---------------------------------------------------------------------------*/
186 #else
187 #error "Toolchain not supported"
188 #endif
189 /*---------------------------------------------------------------------------*/
Header file for the STM32Cube HAL APIs.