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
targets/TARGET_Maxim/TARGET_MAX32620/pinmap.c@177:619788de047e, 2017-11-07 (annotated)
- Committer:
- maxxir
- Date:
- Tue Nov 07 16:46:29 2017 +0000
- Revision:
- 177:619788de047e
- Parent:
- 149:156823d33999
To fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..; Used direct RTC register manipulation for STM32F1xx; rtc_read() && rtc_write() (native rtc_init() - works good); also added stub for non-working on STM32F1xx rtc_read_subseconds().
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /******************************************************************************* |
<> | 149:156823d33999 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 149:156823d33999 | 5 | * copy of this software and associated documentation files (the "Software"), |
<> | 149:156823d33999 | 6 | * to deal in the Software without restriction, including without limitation |
<> | 149:156823d33999 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 149:156823d33999 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 149:156823d33999 | 9 | * Software is furnished to do so, subject to the following conditions: |
<> | 149:156823d33999 | 10 | * |
<> | 149:156823d33999 | 11 | * The above copyright notice and this permission notice shall be included |
<> | 149:156823d33999 | 12 | * in all copies or substantial portions of the Software. |
<> | 149:156823d33999 | 13 | * |
<> | 149:156823d33999 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 149:156823d33999 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 149:156823d33999 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 149:156823d33999 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 149:156823d33999 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 149:156823d33999 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 149:156823d33999 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 149:156823d33999 | 21 | * |
<> | 149:156823d33999 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 149:156823d33999 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 149:156823d33999 | 24 | * Products, Inc. Branding Policy. |
<> | 149:156823d33999 | 25 | * |
<> | 149:156823d33999 | 26 | * The mere transfer of this software does not imply any licenses |
<> | 149:156823d33999 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 149:156823d33999 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 149:156823d33999 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 149:156823d33999 | 30 | * ownership rights. |
<> | 149:156823d33999 | 31 | ******************************************************************************* |
<> | 149:156823d33999 | 32 | */ |
<> | 149:156823d33999 | 33 | |
<> | 149:156823d33999 | 34 | #include "mbed_assert.h" |
<> | 149:156823d33999 | 35 | #include "pinmap.h" |
<> | 149:156823d33999 | 36 | #include "objects.h" |
<> | 149:156823d33999 | 37 | #include "gpio_regs.h" |
<> | 149:156823d33999 | 38 | #include "ioman_regs.h" |
<> | 149:156823d33999 | 39 | |
<> | 149:156823d33999 | 40 | void pin_function(PinName name, int function) |
<> | 149:156823d33999 | 41 | { |
<> | 149:156823d33999 | 42 | MBED_ASSERT(name != (PinName)NC); |
<> | 149:156823d33999 | 43 | |
<> | 149:156823d33999 | 44 | if ((function >= 0) && (function <= 0xF)) { |
<> | 149:156823d33999 | 45 | unsigned int port = PINNAME_TO_PORT(name); |
<> | 149:156823d33999 | 46 | unsigned int pin = PINNAME_TO_PIN(name); |
<> | 149:156823d33999 | 47 | uint32_t temp = MXC_GPIO->func_sel[port] & ~(0xF << (pin*4)); |
<> | 149:156823d33999 | 48 | MXC_GPIO->func_sel[port] = temp | ((uint32_t)function << (pin*4)); |
<> | 149:156823d33999 | 49 | } else { |
<> | 149:156823d33999 | 50 | /* Assume this is a pointer to a pin function object */ |
<> | 149:156823d33999 | 51 | pin_function_t *obj = (pin_function_t*)function; |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | if ((*obj->reg_ack & obj->ack_mask) != obj->req_val) { |
<> | 149:156823d33999 | 54 | /* Request pin mapping */ |
<> | 149:156823d33999 | 55 | *obj->reg_req |= obj->req_val; |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | /* Check for acknowledgment */ |
<> | 149:156823d33999 | 58 | MBED_ASSERT((*obj->reg_ack & obj->ack_mask) == obj->req_val); |
<> | 149:156823d33999 | 59 | } |
<> | 149:156823d33999 | 60 | } |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | |
<> | 149:156823d33999 | 63 | void pin_mode(PinName name, PinMode mode) |
<> | 149:156823d33999 | 64 | { |
<> | 149:156823d33999 | 65 | MBED_ASSERT(name != (PinName)NC); |
<> | 149:156823d33999 | 66 | unsigned int port = PINNAME_TO_PORT(name); |
<> | 149:156823d33999 | 67 | unsigned int pin = PINNAME_TO_PIN(name); |
<> | 149:156823d33999 | 68 | |
<> | 149:156823d33999 | 69 | /* Must set mode while retaining direction */ |
<> | 149:156823d33999 | 70 | |
<> | 149:156823d33999 | 71 | /* Get the current direction */ |
<> | 149:156823d33999 | 72 | uint32_t out_mode = MXC_GPIO->out_mode[port]; |
<> | 149:156823d33999 | 73 | uint32_t curr_mode = (out_mode >> (pin * 4)) & 0xF; |
<> | 149:156823d33999 | 74 | PinDirection dir = PIN_OUTPUT; |
<> | 149:156823d33999 | 75 | if ((curr_mode == MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP) || |
<> | 149:156823d33999 | 76 | (curr_mode == MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLDOWN) || |
<> | 149:156823d33999 | 77 | (curr_mode == MXC_V_GPIO_OUT_MODE_NORMAL_HIGH_Z)) { |
<> | 149:156823d33999 | 78 | dir = PIN_INPUT; |
<> | 149:156823d33999 | 79 | } |
<> | 149:156823d33999 | 80 | |
<> | 149:156823d33999 | 81 | /* Set mode based on current direction */ |
<> | 149:156823d33999 | 82 | uint32_t new_mode; |
<> | 149:156823d33999 | 83 | if (dir == PIN_OUTPUT) { |
<> | 149:156823d33999 | 84 | // PullUp = not valid, |
<> | 149:156823d33999 | 85 | // PullDown = not valid, |
<> | 149:156823d33999 | 86 | // OpenDrain = MXC_V_GPIO_OUT_MODE_OD, |
<> | 149:156823d33999 | 87 | // PullNone = MXC_V_GPIO_OUT_MODE_NORMAL, |
<> | 149:156823d33999 | 88 | if (mode == OpenDrain) { |
<> | 149:156823d33999 | 89 | new_mode = MXC_V_GPIO_OUT_MODE_OPEN_DRAIN; |
<> | 149:156823d33999 | 90 | } else { |
<> | 149:156823d33999 | 91 | new_mode = MXC_V_GPIO_OUT_MODE_NORMAL; |
<> | 149:156823d33999 | 92 | } |
<> | 149:156823d33999 | 93 | } else { |
<> | 149:156823d33999 | 94 | // PullUp = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP |
<> | 149:156823d33999 | 95 | // PullDown = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLDOWN |
<> | 149:156823d33999 | 96 | // OpenDrain = MXC_V_GPIO_OUT_MODE_OD |
<> | 149:156823d33999 | 97 | // PullNone = MXC_V_GPIO_OUT_MODE_NORMAL_HIGH_Z |
<> | 149:156823d33999 | 98 | if (mode == PullUp) { |
<> | 149:156823d33999 | 99 | new_mode = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP; |
<> | 149:156823d33999 | 100 | MXC_GPIO->out_val[port] |= 1 << pin; |
<> | 149:156823d33999 | 101 | } else if (mode == PullDown) { |
<> | 149:156823d33999 | 102 | new_mode = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLDOWN; |
<> | 149:156823d33999 | 103 | MXC_GPIO->out_val[port] &= ~(1 << pin); |
<> | 149:156823d33999 | 104 | } else if (mode == OpenDrain) { |
<> | 149:156823d33999 | 105 | new_mode = MXC_V_GPIO_OUT_MODE_OPEN_DRAIN; |
<> | 149:156823d33999 | 106 | MXC_GPIO->out_val[port] |= 1 << pin; |
<> | 149:156823d33999 | 107 | } else { |
<> | 149:156823d33999 | 108 | new_mode = MXC_V_GPIO_OUT_MODE_NORMAL_HIGH_Z; |
<> | 149:156823d33999 | 109 | MXC_GPIO->out_val[port] &= ~(1 << pin); |
<> | 149:156823d33999 | 110 | } |
<> | 149:156823d33999 | 111 | } |
<> | 149:156823d33999 | 112 | |
<> | 149:156823d33999 | 113 | /* Set new mode */ |
<> | 149:156823d33999 | 114 | out_mode &= ~(0xF << (pin * 4)); |
<> | 149:156823d33999 | 115 | out_mode |= (new_mode << (pin * 4)); |
<> | 149:156823d33999 | 116 | MXC_GPIO->out_mode[port] = out_mode; |
<> | 149:156823d33999 | 117 | } |