mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c

Dependents:   Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:9b334a45a8ff 1 /* mbed Microcontroller Library
bogdanm 0:9b334a45a8ff 2 *******************************************************************************
bogdanm 0:9b334a45a8ff 3 * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
bogdanm 0:9b334a45a8ff 4 * All rights reserved.
bogdanm 0:9b334a45a8ff 5 *
bogdanm 0:9b334a45a8ff 6 * Redistribution and use in source and binary forms, with or without
bogdanm 0:9b334a45a8ff 7 * modification, are permitted provided that the following conditions are met:
bogdanm 0:9b334a45a8ff 8 *
bogdanm 0:9b334a45a8ff 9 * 1. Redistributions of source code must retain the above copyright notice,
bogdanm 0:9b334a45a8ff 10 * this list of conditions and the following disclaimer.
bogdanm 0:9b334a45a8ff 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
bogdanm 0:9b334a45a8ff 12 * this list of conditions and the following disclaimer in the documentation
bogdanm 0:9b334a45a8ff 13 * and/or other materials provided with the distribution.
bogdanm 0:9b334a45a8ff 14 * 3. Neither the name of ARM Limited nor the names of its contributors
bogdanm 0:9b334a45a8ff 15 * may be used to endorse or promote products derived from this software
bogdanm 0:9b334a45a8ff 16 * without specific prior written permission.
bogdanm 0:9b334a45a8ff 17 *
bogdanm 0:9b334a45a8ff 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bogdanm 0:9b334a45a8ff 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bogdanm 0:9b334a45a8ff 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bogdanm 0:9b334a45a8ff 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bogdanm 0:9b334a45a8ff 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bogdanm 0:9b334a45a8ff 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bogdanm 0:9b334a45a8ff 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bogdanm 0:9b334a45a8ff 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bogdanm 0:9b334a45a8ff 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bogdanm 0:9b334a45a8ff 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bogdanm 0:9b334a45a8ff 28 *******************************************************************************
bogdanm 0:9b334a45a8ff 29 */
bogdanm 0:9b334a45a8ff 30
bogdanm 0:9b334a45a8ff 31 #include "mbed_assert.h"
bogdanm 0:9b334a45a8ff 32 #include "pinmap.h"
bogdanm 0:9b334a45a8ff 33 #include "PortNames.h"
bogdanm 0:9b334a45a8ff 34 #include "mbed_error.h"
bogdanm 0:9b334a45a8ff 35 #include "W7500x.h"
bogdanm 0:9b334a45a8ff 36 #include "W7500x_gpio.h"
bogdanm 0:9b334a45a8ff 37
bogdanm 0:9b334a45a8ff 38
bogdanm 0:9b334a45a8ff 39 uint32_t Get_GPIO_BaseAddress(uint32_t port_idx)
bogdanm 0:9b334a45a8ff 40 {
bogdanm 0:9b334a45a8ff 41 uint32_t gpio_add = 0;
bogdanm 0:9b334a45a8ff 42 switch(port_idx) {
bogdanm 0:9b334a45a8ff 43 case PortA:
bogdanm 0:9b334a45a8ff 44 gpio_add = GPIOA_BASE;
bogdanm 0:9b334a45a8ff 45 break;
bogdanm 0:9b334a45a8ff 46 case PortB:
bogdanm 0:9b334a45a8ff 47 gpio_add = GPIOB_BASE;
bogdanm 0:9b334a45a8ff 48 break;
bogdanm 0:9b334a45a8ff 49 case PortC:
bogdanm 0:9b334a45a8ff 50 gpio_add = GPIOC_BASE;
bogdanm 0:9b334a45a8ff 51 break;
bogdanm 0:9b334a45a8ff 52 case PortD:
bogdanm 0:9b334a45a8ff 53 gpio_add = GPIOD_BASE;
bogdanm 0:9b334a45a8ff 54 break;
bogdanm 0:9b334a45a8ff 55 default:
bogdanm 0:9b334a45a8ff 56 error("Pinmap error: wrong port number.");
bogdanm 0:9b334a45a8ff 57 break;
bogdanm 0:9b334a45a8ff 58 }
bogdanm 0:9b334a45a8ff 59 return gpio_add;
bogdanm 0:9b334a45a8ff 60 }
bogdanm 0:9b334a45a8ff 61
bogdanm 0:9b334a45a8ff 62 /**
bogdanm 0:9b334a45a8ff 63 * Configure pin (input, output, alternate function or analog) + output speed + AF
bogdanm 0:9b334a45a8ff 64 */
bogdanm 0:9b334a45a8ff 65
bogdanm 0:9b334a45a8ff 66 void pin_function(PinName pin, int data) {
bogdanm 0:9b334a45a8ff 67 MBED_ASSERT(pin != (PinName)NC);
bogdanm 0:9b334a45a8ff 68 // Get the pin informations
bogdanm 0:9b334a45a8ff 69 uint32_t mode = WIZ_PIN_MODE(data);
bogdanm 0:9b334a45a8ff 70 uint32_t pupd = WIZ_PIN_PUPD(data);
bogdanm 0:9b334a45a8ff 71 uint32_t afnum = WIZ_PIN_AFNUM(data);
bogdanm 0:9b334a45a8ff 72
bogdanm 0:9b334a45a8ff 73 uint32_t port_num = WIZ_PORT(pin);
bogdanm 0:9b334a45a8ff 74 uint32_t pin_index = WIZ_PIN_INDEX(pin);
bogdanm 0:9b334a45a8ff 75
bogdanm 0:9b334a45a8ff 76 GPIO_TypeDef *gpio;
bogdanm 0:9b334a45a8ff 77
bogdanm 0:9b334a45a8ff 78 // Configure Alternate Function
bogdanm 0:9b334a45a8ff 79 // Warning: Must be done before the GPIO is initialized
bogdanm 0:9b334a45a8ff 80 switch (afnum) {
bogdanm 0:9b334a45a8ff 81 case 0:
bogdanm 0:9b334a45a8ff 82 HAL_PAD_AFConfig((PAD_Type)port_num, (uint16_t)pin_index, (PAD_AF_TypeDef)Px_AFSR_AF0);
bogdanm 0:9b334a45a8ff 83 break;
bogdanm 0:9b334a45a8ff 84 case 1:
bogdanm 0:9b334a45a8ff 85 HAL_PAD_AFConfig((PAD_Type)port_num, (uint16_t)pin_index, (PAD_AF_TypeDef)Px_AFSR_AF1);
bogdanm 0:9b334a45a8ff 86 break;
bogdanm 0:9b334a45a8ff 87 case 2:
bogdanm 0:9b334a45a8ff 88 HAL_PAD_AFConfig((PAD_Type)port_num, (uint16_t)pin_index, (PAD_AF_TypeDef)Px_AFSR_AF2);
bogdanm 0:9b334a45a8ff 89 break;
bogdanm 0:9b334a45a8ff 90 case 3:
bogdanm 0:9b334a45a8ff 91 HAL_PAD_AFConfig((PAD_Type)port_num, (uint16_t)pin_index, (PAD_AF_TypeDef)Px_AFSR_AF3);
bogdanm 0:9b334a45a8ff 92 break;
bogdanm 0:9b334a45a8ff 93 default:
bogdanm 0:9b334a45a8ff 94 break;
bogdanm 0:9b334a45a8ff 95 }
bogdanm 0:9b334a45a8ff 96
bogdanm 0:9b334a45a8ff 97 if(mode == WIZ_MODE_AF)
bogdanm 0:9b334a45a8ff 98 return;
bogdanm 0:9b334a45a8ff 99
bogdanm 0:9b334a45a8ff 100 // Configure GPIO
bogdanm 0:9b334a45a8ff 101 gpio = (GPIO_TypeDef *)Get_GPIO_BaseAddress(port_num);
bogdanm 0:9b334a45a8ff 102
bogdanm 0:9b334a45a8ff 103 GPIO_InitTypeDef GPIO_InitStructure;
bogdanm 0:9b334a45a8ff 104 GPIO_InitStructure.GPIO_Pin = pin_index;
bogdanm 0:9b334a45a8ff 105 GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode;
bogdanm 0:9b334a45a8ff 106 GPIO_InitStructure.GPIO_Pad = (GPIOPad_TypeDef)pupd;
bogdanm 0:9b334a45a8ff 107 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
bogdanm 0:9b334a45a8ff 108 }
bogdanm 0:9b334a45a8ff 109
bogdanm 0:9b334a45a8ff 110 /**
bogdanm 0:9b334a45a8ff 111 * Configure pin pull-up/pull-down
bogdanm 0:9b334a45a8ff 112 */
bogdanm 0:9b334a45a8ff 113 void pin_mode(PinName pin, PinMode pupd)
bogdanm 0:9b334a45a8ff 114 {
bogdanm 0:9b334a45a8ff 115 MBED_ASSERT(pin != (PinName)NC);
bogdanm 0:9b334a45a8ff 116
bogdanm 0:9b334a45a8ff 117 uint32_t port_num = WIZ_PORT(pin);
bogdanm 0:9b334a45a8ff 118 uint32_t pin_num = WIZ_PIN_NUM(pin);
bogdanm 0:9b334a45a8ff 119
bogdanm 0:9b334a45a8ff 120 switch(port_num) {
bogdanm 0:9b334a45a8ff 121 case PortA:
bogdanm 0:9b334a45a8ff 122 PA_PCR->Port[pin_num] |= pupd;
bogdanm 0:9b334a45a8ff 123 break;
bogdanm 0:9b334a45a8ff 124 case PortB:
bogdanm 0:9b334a45a8ff 125 PB_PCR->Port[pin_num] |= pupd;
bogdanm 0:9b334a45a8ff 126 break;
bogdanm 0:9b334a45a8ff 127 case PortC:
bogdanm 0:9b334a45a8ff 128 PC_PCR->Port[pin_num] |= pupd;
bogdanm 0:9b334a45a8ff 129 break;
bogdanm 0:9b334a45a8ff 130 case PortD:
bogdanm 0:9b334a45a8ff 131 PD_PCR->Port[pin_num] |= pupd;
bogdanm 0:9b334a45a8ff 132 break;
bogdanm 0:9b334a45a8ff 133 default:
bogdanm 0:9b334a45a8ff 134 error("Pinmap error: wrong port number.");
bogdanm 0:9b334a45a8ff 135 return;
bogdanm 0:9b334a45a8ff 136 }
bogdanm 0:9b334a45a8ff 137 }