Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/family/arm/musca_a/power_ctrl.c@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file power_ctrl.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief power control sequence logic for Musca A |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2009-2019, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 7 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include "string.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "stdio.h" |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "stdint.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "syscon.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "gpio.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #include "utils.h" |
Pawel Zarembski |
0:01f31e923fe2 | 28 | #include "uart.h" |
Pawel Zarembski |
0:01f31e923fe2 | 29 | |
Pawel Zarembski |
0:01f31e923fe2 | 30 | // Configure SCC |
Pawel Zarembski |
0:01f31e923fe2 | 31 | void configure_syscon(unsigned int pc) |
Pawel Zarembski |
0:01f31e923fe2 | 32 | { |
Pawel Zarembski |
0:01f31e923fe2 | 33 | unsigned int din; |
Pawel Zarembski |
0:01f31e923fe2 | 34 | |
Pawel Zarembski |
0:01f31e923fe2 | 35 | // Dummy read |
Pawel Zarembski |
0:01f31e923fe2 | 36 | syscon_readreg(0x004, &din); |
Pawel Zarembski |
0:01f31e923fe2 | 37 | |
Pawel Zarembski |
0:01f31e923fe2 | 38 | // CPU0 boot from QSPI or SRAM |
Pawel Zarembski |
0:01f31e923fe2 | 39 | syscon_writereg(0x020, pc); |
Pawel Zarembski |
0:01f31e923fe2 | 40 | |
Pawel Zarembski |
0:01f31e923fe2 | 41 | // CPU1 boot from QSPI or SRAM |
Pawel Zarembski |
0:01f31e923fe2 | 42 | syscon_writereg(0x028, pc); |
Pawel Zarembski |
0:01f31e923fe2 | 43 | |
Pawel Zarembski |
0:01f31e923fe2 | 44 | // Set IO drive strength and slew rate |
Pawel Zarembski |
0:01f31e923fe2 | 45 | syscon_writereg(0x068, 0x03F00000); |
Pawel Zarembski |
0:01f31e923fe2 | 46 | syscon_writereg(0x06C, 0x000FFFFF); |
Pawel Zarembski |
0:01f31e923fe2 | 47 | syscon_writereg(0x078, 0xFFFFFFFF); |
Pawel Zarembski |
0:01f31e923fe2 | 48 | } |
Pawel Zarembski |
0:01f31e923fe2 | 49 | |
Pawel Zarembski |
0:01f31e923fe2 | 50 | // Brown Out Detect |
Pawel Zarembski |
0:01f31e923fe2 | 51 | void enable_BOD(int enable) |
Pawel Zarembski |
0:01f31e923fe2 | 52 | { |
Pawel Zarembski |
0:01f31e923fe2 | 53 | if (enable) |
Pawel Zarembski |
0:01f31e923fe2 | 54 | { |
Pawel Zarembski |
0:01f31e923fe2 | 55 | // Set BOD interrupt to 2.80-2.90V |
Pawel Zarembski |
0:01f31e923fe2 | 56 | LPC_SYSCON->BODCTRL |= 0x1C; |
Pawel Zarembski |
0:01f31e923fe2 | 57 | NVIC_EnableIRQ(BOD_IRQn); /* Enable BOD interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 58 | } |
Pawel Zarembski |
0:01f31e923fe2 | 59 | else |
Pawel Zarembski |
0:01f31e923fe2 | 60 | { |
Pawel Zarembski |
0:01f31e923fe2 | 61 | LPC_SYSCON->BODCTRL = 0x00; |
Pawel Zarembski |
0:01f31e923fe2 | 62 | NVIC_DisableIRQ(BOD_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 63 | NVIC_ClearPendingIRQ(BOD_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 64 | } |
Pawel Zarembski |
0:01f31e923fe2 | 65 | } |
Pawel Zarembski |
0:01f31e923fe2 | 66 | |
Pawel Zarembski |
0:01f31e923fe2 | 67 | //Power off / shutdown sequence |
Pawel Zarembski |
0:01f31e923fe2 | 68 | void power_off_sequence() |
Pawel Zarembski |
0:01f31e923fe2 | 69 | { |
Pawel Zarembski |
0:01f31e923fe2 | 70 | // Disable Brown Out Detection |
Pawel Zarembski |
0:01f31e923fe2 | 71 | enable_BOD(0); |
Pawel Zarembski |
0:01f31e923fe2 | 72 | |
Pawel Zarembski |
0:01f31e923fe2 | 73 | // Apply CS_nSRST |
Pawel Zarembski |
0:01f31e923fe2 | 74 | LPC_GPIO->CLR[PIN_nRESET_PORT] = PIN_nRESET; |
Pawel Zarembski |
0:01f31e923fe2 | 75 | |
Pawel Zarembski |
0:01f31e923fe2 | 76 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 77 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 78 | |
Pawel Zarembski |
0:01f31e923fe2 | 79 | // Apply CB_nRST |
Pawel Zarembski |
0:01f31e923fe2 | 80 | LPC_GPIO->CLR[PIN_CB_nRST_PORT] = PIN_CB_nRST; |
Pawel Zarembski |
0:01f31e923fe2 | 81 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 82 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 83 | |
Pawel Zarembski |
0:01f31e923fe2 | 84 | // Apply CFG_nRST |
Pawel Zarembski |
0:01f31e923fe2 | 85 | LPC_GPIO->CLR[PIN_CFG_nRST_PORT] = PIN_CFG_nRST; |
Pawel Zarembski |
0:01f31e923fe2 | 86 | |
Pawel Zarembski |
0:01f31e923fe2 | 87 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 88 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 89 | } |
Pawel Zarembski |
0:01f31e923fe2 | 90 | |
Pawel Zarembski |
0:01f31e923fe2 | 91 | //Power on sequence |
Pawel Zarembski |
0:01f31e923fe2 | 92 | void power_on_sequence() |
Pawel Zarembski |
0:01f31e923fe2 | 93 | { |
Pawel Zarembski |
0:01f31e923fe2 | 94 | // Drive SCC signals |
Pawel Zarembski |
0:01f31e923fe2 | 95 | LPC_GPIO->DIR[PIN_SCC_CLK_PORT] |= PIN_SCC_CLK; |
Pawel Zarembski |
0:01f31e923fe2 | 96 | LPC_GPIO->DIR[PIN_SCC_DATAIN_PORT] |= PIN_SCC_DATAIN; |
Pawel Zarembski |
0:01f31e923fe2 | 97 | LPC_GPIO->DIR[PIN_SCC_DATAOUT_PORT] &= ~PIN_SCC_DATAOUT; |
Pawel Zarembski |
0:01f31e923fe2 | 98 | LPC_GPIO->DIR[PIN_SCC_WNR_PORT] |= PIN_SCC_WNR; |
Pawel Zarembski |
0:01f31e923fe2 | 99 | LPC_GPIO->DIR[PIN_SCC_LOAD_PORT] |= PIN_SCC_LOAD; |
Pawel Zarembski |
0:01f31e923fe2 | 100 | |
Pawel Zarembski |
0:01f31e923fe2 | 101 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 102 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 103 | |
Pawel Zarembski |
0:01f31e923fe2 | 104 | // Release CFG_nRST to allow SCC config |
Pawel Zarembski |
0:01f31e923fe2 | 105 | LPC_GPIO->SET[PIN_CFG_nRST_PORT] = PIN_CFG_nRST; |
Pawel Zarembski |
0:01f31e923fe2 | 106 | |
Pawel Zarembski |
0:01f31e923fe2 | 107 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 108 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 109 | |
Pawel Zarembski |
0:01f31e923fe2 | 110 | // Configure SCC |
Pawel Zarembski |
0:01f31e923fe2 | 111 | configure_syscon(0x10200000); |
Pawel Zarembski |
0:01f31e923fe2 | 112 | |
Pawel Zarembski |
0:01f31e923fe2 | 113 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 114 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 115 | |
Pawel Zarembski |
0:01f31e923fe2 | 116 | // Release SCC signals |
Pawel Zarembski |
0:01f31e923fe2 | 117 | LPC_GPIO->DIR[PIN_SCC_CLK_PORT] &= ~PIN_SCC_CLK; |
Pawel Zarembski |
0:01f31e923fe2 | 118 | LPC_GPIO->DIR[PIN_SCC_DATAIN_PORT] &= ~PIN_SCC_DATAIN; |
Pawel Zarembski |
0:01f31e923fe2 | 119 | LPC_GPIO->DIR[PIN_SCC_DATAOUT_PORT] &= ~PIN_SCC_DATAOUT; |
Pawel Zarembski |
0:01f31e923fe2 | 120 | LPC_GPIO->DIR[PIN_SCC_WNR_PORT] &= ~PIN_SCC_WNR; |
Pawel Zarembski |
0:01f31e923fe2 | 121 | LPC_GPIO->DIR[PIN_SCC_LOAD_PORT] &= ~PIN_SCC_LOAD; |
Pawel Zarembski |
0:01f31e923fe2 | 122 | |
Pawel Zarembski |
0:01f31e923fe2 | 123 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 124 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 125 | |
Pawel Zarembski |
0:01f31e923fe2 | 126 | // Release CB_nRST |
Pawel Zarembski |
0:01f31e923fe2 | 127 | LPC_GPIO->SET[PIN_CB_nRST_PORT] = PIN_CB_nRST; |
Pawel Zarembski |
0:01f31e923fe2 | 128 | |
Pawel Zarembski |
0:01f31e923fe2 | 129 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 130 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 131 | |
Pawel Zarembski |
0:01f31e923fe2 | 132 | // Release CS_nSRST |
Pawel Zarembski |
0:01f31e923fe2 | 133 | LPC_GPIO->SET[PIN_nRESET_PORT] = PIN_nRESET; |
Pawel Zarembski |
0:01f31e923fe2 | 134 | |
Pawel Zarembski |
0:01f31e923fe2 | 135 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 136 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 137 | |
Pawel Zarembski |
0:01f31e923fe2 | 138 | // Enable Brown Out Detection |
Pawel Zarembski |
0:01f31e923fe2 | 139 | enable_BOD(1); |
Pawel Zarembski |
0:01f31e923fe2 | 140 | } |
Pawel Zarembski |
0:01f31e923fe2 | 141 | |
Pawel Zarembski |
0:01f31e923fe2 | 142 | // BOD Interrupt Service Routine |
Pawel Zarembski |
0:01f31e923fe2 | 143 | void BOD_IRQHandler(void) |
Pawel Zarembski |
0:01f31e923fe2 | 144 | { |
Pawel Zarembski |
0:01f31e923fe2 | 145 | NVIC_DisableIRQ(BOD_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 146 | gpio_set_cdc_led(GPIO_LED_OFF); // ON GREEN |
Pawel Zarembski |
0:01f31e923fe2 | 147 | |
Pawel Zarembski |
0:01f31e923fe2 | 148 | // go into controlled shutdown |
Pawel Zarembski |
0:01f31e923fe2 | 149 | power_off_sequence(); |
Pawel Zarembski |
0:01f31e923fe2 | 150 | |
Pawel Zarembski |
0:01f31e923fe2 | 151 | while(1) |
Pawel Zarembski |
0:01f31e923fe2 | 152 | { |
Pawel Zarembski |
0:01f31e923fe2 | 153 | delay(100); |
Pawel Zarembski |
0:01f31e923fe2 | 154 | gpio_set_hid_led(GPIO_LED_ON); |
Pawel Zarembski |
0:01f31e923fe2 | 155 | delay(100); |
Pawel Zarembski |
0:01f31e923fe2 | 156 | gpio_set_hid_led(GPIO_LED_OFF); |
Pawel Zarembski |
0:01f31e923fe2 | 157 | } |
Pawel Zarembski |
0:01f31e923fe2 | 158 | } |
Pawel Zarembski |
0:01f31e923fe2 | 159 | |
Pawel Zarembski |
0:01f31e923fe2 | 160 | // Function to wait till PBON button is pressed and released |
Pawel Zarembski |
0:01f31e923fe2 | 161 | void wait_for_pbon(void) |
Pawel Zarembski |
0:01f31e923fe2 | 162 | { |
Pawel Zarembski |
0:01f31e923fe2 | 163 | // Standby - wait for PBON |
Pawel Zarembski |
0:01f31e923fe2 | 164 | while (!gpio_get_pbon_btn()) |
Pawel Zarembski |
0:01f31e923fe2 | 165 | { |
Pawel Zarembski |
0:01f31e923fe2 | 166 | // Do something with leds? |
Pawel Zarembski |
0:01f31e923fe2 | 167 | gpio_set_cdc_led(GPIO_LED_ON); |
Pawel Zarembski |
0:01f31e923fe2 | 168 | delay(100); |
Pawel Zarembski |
0:01f31e923fe2 | 169 | gpio_set_cdc_led(GPIO_LED_OFF); |
Pawel Zarembski |
0:01f31e923fe2 | 170 | delay(100); |
Pawel Zarembski |
0:01f31e923fe2 | 171 | } |
Pawel Zarembski |
0:01f31e923fe2 | 172 | // Wait for PBON to go low then high |
Pawel Zarembski |
0:01f31e923fe2 | 173 | while (gpio_get_pbon_btn()) |
Pawel Zarembski |
0:01f31e923fe2 | 174 | { |
Pawel Zarembski |
0:01f31e923fe2 | 175 | // Do something with leds? |
Pawel Zarembski |
0:01f31e923fe2 | 176 | gpio_set_hid_led(GPIO_LED_ON); |
Pawel Zarembski |
0:01f31e923fe2 | 177 | delay(100); |
Pawel Zarembski |
0:01f31e923fe2 | 178 | gpio_set_hid_led(GPIO_LED_OFF); |
Pawel Zarembski |
0:01f31e923fe2 | 179 | delay(100); |
Pawel Zarembski |
0:01f31e923fe2 | 180 | } |
Pawel Zarembski |
0:01f31e923fe2 | 181 | // Wait 10ms |
Pawel Zarembski |
0:01f31e923fe2 | 182 | delay(10); |
Pawel Zarembski |
0:01f31e923fe2 | 183 | |
Pawel Zarembski |
0:01f31e923fe2 | 184 | } |