mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Thu Dec 12 10:45:05 2013 +0000
Revision:
56:99eb381a3269
Parent:
55:3b765ca737a5
Child:
70:c1fbde68b492
Synchronized with git revision bd51e4eb73a1706f1d5379ec5cebcbd6d978cb4f

Full URL: https://github.com/mbedmicro/mbed/commit/bd51e4eb73a1706f1d5379ec5cebcbd6d978cb4f/

[NUCLEO_F103RB] Add I2C master, code cleanup, ...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /* mbed Microcontroller Library
mbed_official 52:a51c77007319 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 52:a51c77007319 3 *
mbed_official 52:a51c77007319 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 52:a51c77007319 5 * you may not use this file except in compliance with the License.
mbed_official 52:a51c77007319 6 * You may obtain a copy of the License at
mbed_official 52:a51c77007319 7 *
mbed_official 52:a51c77007319 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 52:a51c77007319 9 *
mbed_official 52:a51c77007319 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 52:a51c77007319 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 52:a51c77007319 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 52:a51c77007319 13 * See the License for the specific language governing permissions and
mbed_official 52:a51c77007319 14 * limitations under the License.
mbed_official 52:a51c77007319 15 */
mbed_official 52:a51c77007319 16 #include "serial_api.h"
mbed_official 52:a51c77007319 17 #include "cmsis.h"
mbed_official 52:a51c77007319 18 #include "pinmap.h"
mbed_official 52:a51c77007319 19 #include "error.h"
mbed_official 52:a51c77007319 20 #include <string.h>
mbed_official 52:a51c77007319 21
mbed_official 52:a51c77007319 22 static const PinMap PinMap_UART_TX[] = {
mbed_official 52:a51c77007319 23 {PA_9, UART_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 52:a51c77007319 24 {PA_2, UART_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 52:a51c77007319 25 {NC, NC, 0}
mbed_official 52:a51c77007319 26 };
mbed_official 52:a51c77007319 27
mbed_official 52:a51c77007319 28 static const PinMap PinMap_UART_RX[] = {
mbed_official 52:a51c77007319 29 {PA_10, UART_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 52:a51c77007319 30 {PA_3, UART_2, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 52:a51c77007319 31 {NC, NC, 0}
mbed_official 52:a51c77007319 32 };
mbed_official 52:a51c77007319 33
mbed_official 52:a51c77007319 34 #define UART_NUM (2)
mbed_official 52:a51c77007319 35
mbed_official 52:a51c77007319 36 static uint32_t serial_irq_ids[UART_NUM] = {0};
mbed_official 52:a51c77007319 37
mbed_official 52:a51c77007319 38 static uart_irq_handler irq_handler;
mbed_official 52:a51c77007319 39
mbed_official 52:a51c77007319 40 int stdio_uart_inited = 0;
mbed_official 52:a51c77007319 41 serial_t stdio_uart;
mbed_official 52:a51c77007319 42
mbed_official 56:99eb381a3269 43 static void init_usart(serial_t *obj) {
mbed_official 56:99eb381a3269 44 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 45 USART_InitTypeDef USART_InitStructure;
mbed_official 52:a51c77007319 46
mbed_official 56:99eb381a3269 47 USART_Cmd(usart, DISABLE);
mbed_official 56:99eb381a3269 48
mbed_official 56:99eb381a3269 49 USART_InitStructure.USART_BaudRate = obj->baudrate;
mbed_official 56:99eb381a3269 50 USART_InitStructure.USART_WordLength = obj->databits;
mbed_official 56:99eb381a3269 51 USART_InitStructure.USART_StopBits = obj->stopbits;
mbed_official 56:99eb381a3269 52 USART_InitStructure.USART_Parity = obj->parity;
mbed_official 56:99eb381a3269 53 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
mbed_official 56:99eb381a3269 54 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
mbed_official 56:99eb381a3269 55 USART_Init(usart, &USART_InitStructure);
mbed_official 56:99eb381a3269 56
mbed_official 56:99eb381a3269 57 USART_Cmd(usart, ENABLE);
mbed_official 56:99eb381a3269 58 }
mbed_official 56:99eb381a3269 59
mbed_official 56:99eb381a3269 60 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 52:a51c77007319 61 // Determine the UART to use (UART_1, UART_2, ...)
mbed_official 52:a51c77007319 62 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 63 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 52:a51c77007319 64
mbed_official 52:a51c77007319 65 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
mbed_official 52:a51c77007319 66 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 52:a51c77007319 67
mbed_official 52:a51c77007319 68 if (obj->uart == (UARTName)NC) {
mbed_official 52:a51c77007319 69 error("Serial pinout mapping failed");
mbed_official 52:a51c77007319 70 }
mbed_official 56:99eb381a3269 71
mbed_official 52:a51c77007319 72 // Enable USART clock
mbed_official 52:a51c77007319 73 if (obj->uart == UART_1) {
mbed_official 52:a51c77007319 74 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 52:a51c77007319 75 }
mbed_official 52:a51c77007319 76 if (obj->uart == UART_2) {
mbed_official 52:a51c77007319 77 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 52:a51c77007319 78 }
mbed_official 52:a51c77007319 79
mbed_official 52:a51c77007319 80 // Configure the UART pins
mbed_official 52:a51c77007319 81 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 82 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 52:a51c77007319 83
mbed_official 52:a51c77007319 84 // Configure UART
mbed_official 52:a51c77007319 85 obj->baudrate = 9600;
mbed_official 52:a51c77007319 86 obj->databits = USART_WordLength_8b;
mbed_official 52:a51c77007319 87 obj->stopbits = USART_StopBits_1;
mbed_official 52:a51c77007319 88 obj->parity = USART_Parity_No;
mbed_official 52:a51c77007319 89
mbed_official 56:99eb381a3269 90 init_usart(obj);
mbed_official 52:a51c77007319 91
mbed_official 52:a51c77007319 92 // The index is used by irq
mbed_official 52:a51c77007319 93 if (obj->uart == UART_1) obj->index = 0;
mbed_official 52:a51c77007319 94 if (obj->uart == UART_2) obj->index = 1;
mbed_official 52:a51c77007319 95
mbed_official 52:a51c77007319 96 // For stdio management
mbed_official 52:a51c77007319 97 if (obj->uart == STDIO_UART) {
mbed_official 52:a51c77007319 98 stdio_uart_inited = 1;
mbed_official 52:a51c77007319 99 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 52:a51c77007319 100 }
mbed_official 52:a51c77007319 101
mbed_official 52:a51c77007319 102 }
mbed_official 52:a51c77007319 103
mbed_official 52:a51c77007319 104 void serial_free(serial_t *obj) {
mbed_official 52:a51c77007319 105 serial_irq_ids[obj->index] = 0;
mbed_official 52:a51c77007319 106 }
mbed_official 52:a51c77007319 107
mbed_official 52:a51c77007319 108 void serial_baud(serial_t *obj, int baudrate) {
mbed_official 52:a51c77007319 109 obj->baudrate = baudrate;
mbed_official 56:99eb381a3269 110 init_usart(obj);
mbed_official 52:a51c77007319 111 }
mbed_official 52:a51c77007319 112
mbed_official 52:a51c77007319 113 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
mbed_official 52:a51c77007319 114 if (data_bits == 8) {
mbed_official 52:a51c77007319 115 obj->databits = USART_WordLength_8b;
mbed_official 52:a51c77007319 116 }
mbed_official 52:a51c77007319 117 else {
mbed_official 52:a51c77007319 118 obj->databits = USART_WordLength_9b;
mbed_official 52:a51c77007319 119 }
mbed_official 52:a51c77007319 120
mbed_official 52:a51c77007319 121 switch (parity) {
mbed_official 52:a51c77007319 122 case ParityOdd:
mbed_official 52:a51c77007319 123 case ParityForced0:
mbed_official 52:a51c77007319 124 obj->parity = USART_Parity_Odd;
mbed_official 52:a51c77007319 125 break;
mbed_official 52:a51c77007319 126 case ParityEven:
mbed_official 52:a51c77007319 127 case ParityForced1:
mbed_official 52:a51c77007319 128 obj->parity = USART_Parity_Even;
mbed_official 52:a51c77007319 129 break;
mbed_official 52:a51c77007319 130 default: // ParityNone
mbed_official 52:a51c77007319 131 obj->parity = USART_Parity_No;
mbed_official 52:a51c77007319 132 break;
mbed_official 52:a51c77007319 133 }
mbed_official 52:a51c77007319 134
mbed_official 52:a51c77007319 135 if (stop_bits == 2) {
mbed_official 52:a51c77007319 136 obj->stopbits = USART_StopBits_2;
mbed_official 52:a51c77007319 137 }
mbed_official 52:a51c77007319 138 else {
mbed_official 52:a51c77007319 139 obj->stopbits = USART_StopBits_1;
mbed_official 52:a51c77007319 140 }
mbed_official 52:a51c77007319 141
mbed_official 56:99eb381a3269 142 init_usart(obj);
mbed_official 52:a51c77007319 143 }
mbed_official 52:a51c77007319 144
mbed_official 52:a51c77007319 145 /******************************************************************************
mbed_official 52:a51c77007319 146 * INTERRUPTS HANDLING
mbed_official 52:a51c77007319 147 ******************************************************************************/
mbed_official 52:a51c77007319 148
mbed_official 52:a51c77007319 149 // not api
mbed_official 55:3b765ca737a5 150 static void uart_irq(USART_TypeDef* usart, int id) {
mbed_official 55:3b765ca737a5 151 if (serial_irq_ids[id] != 0) {
mbed_official 55:3b765ca737a5 152 if (USART_GetITStatus(usart, USART_IT_TC) != RESET) {
mbed_official 55:3b765ca737a5 153 irq_handler(serial_irq_ids[id], TxIrq);
mbed_official 55:3b765ca737a5 154 USART_ClearITPendingBit(usart, USART_IT_TC);
mbed_official 52:a51c77007319 155 }
mbed_official 52:a51c77007319 156 if (USART_GetITStatus(usart, USART_IT_RXNE) != RESET) {
mbed_official 55:3b765ca737a5 157 irq_handler(serial_irq_ids[id], RxIrq);
mbed_official 55:3b765ca737a5 158 USART_ClearITPendingBit(usart, USART_IT_RXNE);
mbed_official 52:a51c77007319 159 }
mbed_official 52:a51c77007319 160 }
mbed_official 52:a51c77007319 161 }
mbed_official 52:a51c77007319 162
mbed_official 55:3b765ca737a5 163 static void uart1_irq(void) {uart_irq((USART_TypeDef*)UART_1, 0);}
mbed_official 55:3b765ca737a5 164 static void uart2_irq(void) {uart_irq((USART_TypeDef*)UART_2, 1);}
mbed_official 52:a51c77007319 165
mbed_official 52:a51c77007319 166 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
mbed_official 52:a51c77007319 167 irq_handler = handler;
mbed_official 52:a51c77007319 168 serial_irq_ids[obj->index] = id;
mbed_official 52:a51c77007319 169 }
mbed_official 52:a51c77007319 170
mbed_official 52:a51c77007319 171 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
mbed_official 52:a51c77007319 172 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 52:a51c77007319 173 uint32_t vector = 0;
mbed_official 52:a51c77007319 174 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 175
mbed_official 52:a51c77007319 176 if (obj->uart == UART_1) {
mbed_official 52:a51c77007319 177 irq_n = USART1_IRQn;
mbed_official 52:a51c77007319 178 vector = (uint32_t)&uart1_irq;
mbed_official 52:a51c77007319 179 }
mbed_official 52:a51c77007319 180
mbed_official 52:a51c77007319 181 if (obj->uart == UART_2) {
mbed_official 52:a51c77007319 182 irq_n = USART2_IRQn;
mbed_official 52:a51c77007319 183 vector = (uint32_t)&uart2_irq;
mbed_official 52:a51c77007319 184 }
mbed_official 52:a51c77007319 185
mbed_official 52:a51c77007319 186 if (enable) {
mbed_official 52:a51c77007319 187
mbed_official 52:a51c77007319 188 if (irq == RxIrq) {
mbed_official 52:a51c77007319 189 USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
mbed_official 52:a51c77007319 190 }
mbed_official 52:a51c77007319 191 else { // TxIrq
mbed_official 55:3b765ca737a5 192 USART_ITConfig(usart, USART_IT_TC, ENABLE);
mbed_official 52:a51c77007319 193 }
mbed_official 52:a51c77007319 194
mbed_official 52:a51c77007319 195 NVIC_SetVector(irq_n, vector);
mbed_official 52:a51c77007319 196 NVIC_EnableIRQ(irq_n);
mbed_official 52:a51c77007319 197
mbed_official 52:a51c77007319 198 } else { // disable
mbed_official 52:a51c77007319 199
mbed_official 52:a51c77007319 200 int all_disabled = 0;
mbed_official 52:a51c77007319 201
mbed_official 52:a51c77007319 202 if (irq == RxIrq) {
mbed_official 52:a51c77007319 203 USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
mbed_official 52:a51c77007319 204 // Check if TxIrq is disabled too
mbed_official 52:a51c77007319 205 if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
mbed_official 52:a51c77007319 206 }
mbed_official 52:a51c77007319 207 else { // TxIrq
mbed_official 52:a51c77007319 208 USART_ITConfig(usart, USART_IT_TXE, DISABLE);
mbed_official 52:a51c77007319 209 // Check if RxIrq is disabled too
mbed_official 52:a51c77007319 210 if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
mbed_official 52:a51c77007319 211 }
mbed_official 52:a51c77007319 212
mbed_official 52:a51c77007319 213 if (all_disabled) NVIC_DisableIRQ(irq_n);
mbed_official 52:a51c77007319 214
mbed_official 52:a51c77007319 215 }
mbed_official 52:a51c77007319 216 }
mbed_official 52:a51c77007319 217
mbed_official 52:a51c77007319 218 /******************************************************************************
mbed_official 52:a51c77007319 219 * READ/WRITE
mbed_official 52:a51c77007319 220 ******************************************************************************/
mbed_official 52:a51c77007319 221
mbed_official 52:a51c77007319 222 int serial_getc(serial_t *obj) {
mbed_official 52:a51c77007319 223 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 224 while (!serial_readable(obj));
mbed_official 52:a51c77007319 225 return (int)(USART_ReceiveData(usart));
mbed_official 52:a51c77007319 226 }
mbed_official 52:a51c77007319 227
mbed_official 52:a51c77007319 228 void serial_putc(serial_t *obj, int c) {
mbed_official 52:a51c77007319 229 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 230 while (!serial_writable(obj));
mbed_official 52:a51c77007319 231 USART_SendData(usart, (uint16_t)c);
mbed_official 52:a51c77007319 232 }
mbed_official 52:a51c77007319 233
mbed_official 52:a51c77007319 234 int serial_readable(serial_t *obj) {
mbed_official 52:a51c77007319 235 int status;
mbed_official 52:a51c77007319 236 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 237 // Check if data is received
mbed_official 52:a51c77007319 238 status = ((USART_GetFlagStatus(usart, USART_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 239 return status;
mbed_official 52:a51c77007319 240 }
mbed_official 52:a51c77007319 241
mbed_official 52:a51c77007319 242 int serial_writable(serial_t *obj) {
mbed_official 52:a51c77007319 243 int status;
mbed_official 52:a51c77007319 244 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 245 // Check if data is transmitted
mbed_official 52:a51c77007319 246 status = ((USART_GetFlagStatus(usart, USART_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 247 return status;
mbed_official 52:a51c77007319 248 }
mbed_official 52:a51c77007319 249
mbed_official 52:a51c77007319 250 void serial_clear(serial_t *obj) {
mbed_official 52:a51c77007319 251 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 252 USART_ClearFlag(usart, USART_FLAG_TXE);
mbed_official 52:a51c77007319 253 USART_ClearFlag(usart, USART_FLAG_RXNE);
mbed_official 52:a51c77007319 254 }
mbed_official 52:a51c77007319 255
mbed_official 52:a51c77007319 256 void serial_pinout_tx(PinName tx) {
mbed_official 52:a51c77007319 257 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 258 }
mbed_official 52:a51c77007319 259
mbed_official 52:a51c77007319 260 void serial_break_set(serial_t *obj) {
mbed_official 52:a51c77007319 261 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 262 USART_SendBreak(usart);
mbed_official 52:a51c77007319 263 }
mbed_official 52:a51c77007319 264
mbed_official 52:a51c77007319 265 void serial_break_clear(serial_t *obj) {
mbed_official 52:a51c77007319 266 }