mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
56:99eb381a3269
Child:
167:d5744491c362
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /* mbed Microcontroller Library
mbed_official 70:c1fbde68b492 2 *******************************************************************************
mbed_official 70:c1fbde68b492 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 70:c1fbde68b492 4 * All rights reserved.
mbed_official 52:a51c77007319 5 *
mbed_official 70:c1fbde68b492 6 * Redistribution and use in source and binary forms, with or without
mbed_official 70:c1fbde68b492 7 * modification, are permitted provided that the following conditions are met:
mbed_official 52:a51c77007319 8 *
mbed_official 70:c1fbde68b492 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 70:c1fbde68b492 10 * this list of conditions and the following disclaimer.
mbed_official 70:c1fbde68b492 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 70:c1fbde68b492 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 70:c1fbde68b492 13 * and/or other materials provided with the distribution.
mbed_official 70:c1fbde68b492 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 70:c1fbde68b492 15 * may be used to endorse or promote products derived from this software
mbed_official 70:c1fbde68b492 16 * without specific prior written permission.
mbed_official 52:a51c77007319 17 *
mbed_official 70:c1fbde68b492 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 70:c1fbde68b492 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 70:c1fbde68b492 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 70:c1fbde68b492 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 70:c1fbde68b492 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 70:c1fbde68b492 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 70:c1fbde68b492 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 70:c1fbde68b492 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 70:c1fbde68b492 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 70:c1fbde68b492 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 70:c1fbde68b492 28 *******************************************************************************
mbed_official 52:a51c77007319 29 */
mbed_official 52:a51c77007319 30 #include "serial_api.h"
mbed_official 52:a51c77007319 31 #include "cmsis.h"
mbed_official 52:a51c77007319 32 #include "pinmap.h"
mbed_official 52:a51c77007319 33 #include "error.h"
mbed_official 52:a51c77007319 34 #include <string.h>
mbed_official 52:a51c77007319 35
mbed_official 52:a51c77007319 36 static const PinMap PinMap_UART_TX[] = {
mbed_official 52:a51c77007319 37 {PA_9, UART_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 52:a51c77007319 38 {PA_2, UART_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 52:a51c77007319 39 {NC, NC, 0}
mbed_official 52:a51c77007319 40 };
mbed_official 52:a51c77007319 41
mbed_official 52:a51c77007319 42 static const PinMap PinMap_UART_RX[] = {
mbed_official 52:a51c77007319 43 {PA_10, UART_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 52:a51c77007319 44 {PA_3, UART_2, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 52:a51c77007319 45 {NC, NC, 0}
mbed_official 52:a51c77007319 46 };
mbed_official 52:a51c77007319 47
mbed_official 52:a51c77007319 48 #define UART_NUM (2)
mbed_official 52:a51c77007319 49
mbed_official 52:a51c77007319 50 static uint32_t serial_irq_ids[UART_NUM] = {0};
mbed_official 52:a51c77007319 51
mbed_official 52:a51c77007319 52 static uart_irq_handler irq_handler;
mbed_official 52:a51c77007319 53
mbed_official 52:a51c77007319 54 int stdio_uart_inited = 0;
mbed_official 52:a51c77007319 55 serial_t stdio_uart;
mbed_official 52:a51c77007319 56
mbed_official 56:99eb381a3269 57 static void init_usart(serial_t *obj) {
mbed_official 56:99eb381a3269 58 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 59 USART_InitTypeDef USART_InitStructure;
mbed_official 52:a51c77007319 60
mbed_official 56:99eb381a3269 61 USART_Cmd(usart, DISABLE);
mbed_official 56:99eb381a3269 62
mbed_official 56:99eb381a3269 63 USART_InitStructure.USART_BaudRate = obj->baudrate;
mbed_official 56:99eb381a3269 64 USART_InitStructure.USART_WordLength = obj->databits;
mbed_official 56:99eb381a3269 65 USART_InitStructure.USART_StopBits = obj->stopbits;
mbed_official 56:99eb381a3269 66 USART_InitStructure.USART_Parity = obj->parity;
mbed_official 56:99eb381a3269 67 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
mbed_official 56:99eb381a3269 68 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
mbed_official 56:99eb381a3269 69 USART_Init(usart, &USART_InitStructure);
mbed_official 56:99eb381a3269 70
mbed_official 56:99eb381a3269 71 USART_Cmd(usart, ENABLE);
mbed_official 56:99eb381a3269 72 }
mbed_official 56:99eb381a3269 73
mbed_official 56:99eb381a3269 74 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 52:a51c77007319 75 // Determine the UART to use (UART_1, UART_2, ...)
mbed_official 52:a51c77007319 76 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 77 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 52:a51c77007319 78
mbed_official 52:a51c77007319 79 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
mbed_official 52:a51c77007319 80 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 52:a51c77007319 81
mbed_official 52:a51c77007319 82 if (obj->uart == (UARTName)NC) {
mbed_official 52:a51c77007319 83 error("Serial pinout mapping failed");
mbed_official 52:a51c77007319 84 }
mbed_official 56:99eb381a3269 85
mbed_official 52:a51c77007319 86 // Enable USART clock
mbed_official 52:a51c77007319 87 if (obj->uart == UART_1) {
mbed_official 52:a51c77007319 88 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 52:a51c77007319 89 }
mbed_official 52:a51c77007319 90 if (obj->uart == UART_2) {
mbed_official 52:a51c77007319 91 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 52:a51c77007319 92 }
mbed_official 52:a51c77007319 93
mbed_official 52:a51c77007319 94 // Configure the UART pins
mbed_official 52:a51c77007319 95 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 96 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 52:a51c77007319 97
mbed_official 52:a51c77007319 98 // Configure UART
mbed_official 52:a51c77007319 99 obj->baudrate = 9600;
mbed_official 52:a51c77007319 100 obj->databits = USART_WordLength_8b;
mbed_official 52:a51c77007319 101 obj->stopbits = USART_StopBits_1;
mbed_official 52:a51c77007319 102 obj->parity = USART_Parity_No;
mbed_official 52:a51c77007319 103
mbed_official 56:99eb381a3269 104 init_usart(obj);
mbed_official 52:a51c77007319 105
mbed_official 52:a51c77007319 106 // The index is used by irq
mbed_official 52:a51c77007319 107 if (obj->uart == UART_1) obj->index = 0;
mbed_official 52:a51c77007319 108 if (obj->uart == UART_2) obj->index = 1;
mbed_official 52:a51c77007319 109
mbed_official 52:a51c77007319 110 // For stdio management
mbed_official 52:a51c77007319 111 if (obj->uart == STDIO_UART) {
mbed_official 52:a51c77007319 112 stdio_uart_inited = 1;
mbed_official 52:a51c77007319 113 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 52:a51c77007319 114 }
mbed_official 52:a51c77007319 115
mbed_official 52:a51c77007319 116 }
mbed_official 52:a51c77007319 117
mbed_official 52:a51c77007319 118 void serial_free(serial_t *obj) {
mbed_official 52:a51c77007319 119 serial_irq_ids[obj->index] = 0;
mbed_official 52:a51c77007319 120 }
mbed_official 52:a51c77007319 121
mbed_official 52:a51c77007319 122 void serial_baud(serial_t *obj, int baudrate) {
mbed_official 52:a51c77007319 123 obj->baudrate = baudrate;
mbed_official 56:99eb381a3269 124 init_usart(obj);
mbed_official 52:a51c77007319 125 }
mbed_official 52:a51c77007319 126
mbed_official 52:a51c77007319 127 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
mbed_official 52:a51c77007319 128 if (data_bits == 8) {
mbed_official 52:a51c77007319 129 obj->databits = USART_WordLength_8b;
mbed_official 52:a51c77007319 130 }
mbed_official 52:a51c77007319 131 else {
mbed_official 52:a51c77007319 132 obj->databits = USART_WordLength_9b;
mbed_official 52:a51c77007319 133 }
mbed_official 52:a51c77007319 134
mbed_official 52:a51c77007319 135 switch (parity) {
mbed_official 52:a51c77007319 136 case ParityOdd:
mbed_official 52:a51c77007319 137 case ParityForced0:
mbed_official 52:a51c77007319 138 obj->parity = USART_Parity_Odd;
mbed_official 52:a51c77007319 139 break;
mbed_official 52:a51c77007319 140 case ParityEven:
mbed_official 52:a51c77007319 141 case ParityForced1:
mbed_official 52:a51c77007319 142 obj->parity = USART_Parity_Even;
mbed_official 52:a51c77007319 143 break;
mbed_official 52:a51c77007319 144 default: // ParityNone
mbed_official 52:a51c77007319 145 obj->parity = USART_Parity_No;
mbed_official 52:a51c77007319 146 break;
mbed_official 52:a51c77007319 147 }
mbed_official 52:a51c77007319 148
mbed_official 52:a51c77007319 149 if (stop_bits == 2) {
mbed_official 52:a51c77007319 150 obj->stopbits = USART_StopBits_2;
mbed_official 52:a51c77007319 151 }
mbed_official 52:a51c77007319 152 else {
mbed_official 52:a51c77007319 153 obj->stopbits = USART_StopBits_1;
mbed_official 52:a51c77007319 154 }
mbed_official 52:a51c77007319 155
mbed_official 56:99eb381a3269 156 init_usart(obj);
mbed_official 52:a51c77007319 157 }
mbed_official 52:a51c77007319 158
mbed_official 52:a51c77007319 159 /******************************************************************************
mbed_official 52:a51c77007319 160 * INTERRUPTS HANDLING
mbed_official 52:a51c77007319 161 ******************************************************************************/
mbed_official 52:a51c77007319 162
mbed_official 52:a51c77007319 163 // not api
mbed_official 55:3b765ca737a5 164 static void uart_irq(USART_TypeDef* usart, int id) {
mbed_official 55:3b765ca737a5 165 if (serial_irq_ids[id] != 0) {
mbed_official 55:3b765ca737a5 166 if (USART_GetITStatus(usart, USART_IT_TC) != RESET) {
mbed_official 55:3b765ca737a5 167 irq_handler(serial_irq_ids[id], TxIrq);
mbed_official 55:3b765ca737a5 168 USART_ClearITPendingBit(usart, USART_IT_TC);
mbed_official 52:a51c77007319 169 }
mbed_official 52:a51c77007319 170 if (USART_GetITStatus(usart, USART_IT_RXNE) != RESET) {
mbed_official 55:3b765ca737a5 171 irq_handler(serial_irq_ids[id], RxIrq);
mbed_official 55:3b765ca737a5 172 USART_ClearITPendingBit(usart, USART_IT_RXNE);
mbed_official 52:a51c77007319 173 }
mbed_official 52:a51c77007319 174 }
mbed_official 52:a51c77007319 175 }
mbed_official 52:a51c77007319 176
mbed_official 55:3b765ca737a5 177 static void uart1_irq(void) {uart_irq((USART_TypeDef*)UART_1, 0);}
mbed_official 55:3b765ca737a5 178 static void uart2_irq(void) {uart_irq((USART_TypeDef*)UART_2, 1);}
mbed_official 52:a51c77007319 179
mbed_official 52:a51c77007319 180 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
mbed_official 52:a51c77007319 181 irq_handler = handler;
mbed_official 52:a51c77007319 182 serial_irq_ids[obj->index] = id;
mbed_official 52:a51c77007319 183 }
mbed_official 52:a51c77007319 184
mbed_official 52:a51c77007319 185 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
mbed_official 52:a51c77007319 186 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 52:a51c77007319 187 uint32_t vector = 0;
mbed_official 52:a51c77007319 188 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 189
mbed_official 52:a51c77007319 190 if (obj->uart == UART_1) {
mbed_official 52:a51c77007319 191 irq_n = USART1_IRQn;
mbed_official 52:a51c77007319 192 vector = (uint32_t)&uart1_irq;
mbed_official 52:a51c77007319 193 }
mbed_official 52:a51c77007319 194
mbed_official 52:a51c77007319 195 if (obj->uart == UART_2) {
mbed_official 52:a51c77007319 196 irq_n = USART2_IRQn;
mbed_official 52:a51c77007319 197 vector = (uint32_t)&uart2_irq;
mbed_official 52:a51c77007319 198 }
mbed_official 52:a51c77007319 199
mbed_official 52:a51c77007319 200 if (enable) {
mbed_official 52:a51c77007319 201
mbed_official 52:a51c77007319 202 if (irq == RxIrq) {
mbed_official 52:a51c77007319 203 USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
mbed_official 52:a51c77007319 204 }
mbed_official 52:a51c77007319 205 else { // TxIrq
mbed_official 55:3b765ca737a5 206 USART_ITConfig(usart, USART_IT_TC, ENABLE);
mbed_official 52:a51c77007319 207 }
mbed_official 52:a51c77007319 208
mbed_official 52:a51c77007319 209 NVIC_SetVector(irq_n, vector);
mbed_official 52:a51c77007319 210 NVIC_EnableIRQ(irq_n);
mbed_official 52:a51c77007319 211
mbed_official 52:a51c77007319 212 } else { // disable
mbed_official 52:a51c77007319 213
mbed_official 52:a51c77007319 214 int all_disabled = 0;
mbed_official 52:a51c77007319 215
mbed_official 52:a51c77007319 216 if (irq == RxIrq) {
mbed_official 52:a51c77007319 217 USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
mbed_official 52:a51c77007319 218 // Check if TxIrq is disabled too
mbed_official 52:a51c77007319 219 if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
mbed_official 52:a51c77007319 220 }
mbed_official 52:a51c77007319 221 else { // TxIrq
mbed_official 52:a51c77007319 222 USART_ITConfig(usart, USART_IT_TXE, DISABLE);
mbed_official 52:a51c77007319 223 // Check if RxIrq is disabled too
mbed_official 52:a51c77007319 224 if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
mbed_official 52:a51c77007319 225 }
mbed_official 52:a51c77007319 226
mbed_official 52:a51c77007319 227 if (all_disabled) NVIC_DisableIRQ(irq_n);
mbed_official 52:a51c77007319 228
mbed_official 52:a51c77007319 229 }
mbed_official 52:a51c77007319 230 }
mbed_official 52:a51c77007319 231
mbed_official 52:a51c77007319 232 /******************************************************************************
mbed_official 52:a51c77007319 233 * READ/WRITE
mbed_official 52:a51c77007319 234 ******************************************************************************/
mbed_official 52:a51c77007319 235
mbed_official 52:a51c77007319 236 int serial_getc(serial_t *obj) {
mbed_official 52:a51c77007319 237 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 238 while (!serial_readable(obj));
mbed_official 52:a51c77007319 239 return (int)(USART_ReceiveData(usart));
mbed_official 52:a51c77007319 240 }
mbed_official 52:a51c77007319 241
mbed_official 52:a51c77007319 242 void serial_putc(serial_t *obj, int c) {
mbed_official 52:a51c77007319 243 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 244 while (!serial_writable(obj));
mbed_official 52:a51c77007319 245 USART_SendData(usart, (uint16_t)c);
mbed_official 52:a51c77007319 246 }
mbed_official 52:a51c77007319 247
mbed_official 52:a51c77007319 248 int serial_readable(serial_t *obj) {
mbed_official 52:a51c77007319 249 int status;
mbed_official 52:a51c77007319 250 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 251 // Check if data is received
mbed_official 52:a51c77007319 252 status = ((USART_GetFlagStatus(usart, USART_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 253 return status;
mbed_official 52:a51c77007319 254 }
mbed_official 52:a51c77007319 255
mbed_official 52:a51c77007319 256 int serial_writable(serial_t *obj) {
mbed_official 52:a51c77007319 257 int status;
mbed_official 52:a51c77007319 258 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 259 // Check if data is transmitted
mbed_official 52:a51c77007319 260 status = ((USART_GetFlagStatus(usart, USART_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 261 return status;
mbed_official 52:a51c77007319 262 }
mbed_official 52:a51c77007319 263
mbed_official 52:a51c77007319 264 void serial_clear(serial_t *obj) {
mbed_official 52:a51c77007319 265 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 266 USART_ClearFlag(usart, USART_FLAG_TXE);
mbed_official 52:a51c77007319 267 USART_ClearFlag(usart, USART_FLAG_RXNE);
mbed_official 52:a51c77007319 268 }
mbed_official 52:a51c77007319 269
mbed_official 52:a51c77007319 270 void serial_pinout_tx(PinName tx) {
mbed_official 52:a51c77007319 271 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 272 }
mbed_official 52:a51c77007319 273
mbed_official 52:a51c77007319 274 void serial_break_set(serial_t *obj) {
mbed_official 52:a51c77007319 275 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 276 USART_SendBreak(usart);
mbed_official 52:a51c77007319 277 }
mbed_official 52:a51c77007319 278
mbed_official 52:a51c77007319 279 void serial_break_clear(serial_t *obj) {
mbed_official 52:a51c77007319 280 }