1 /******************************************************************************* 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included 12 * in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Except as contained in this notice, the name of Maxim Integrated 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated 24 * Products, Inc. Branding Policy. 25 * 26 * The mere transfer of this software does not imply any licenses 27 * of trade secrets, proprietary technology, copyrights, patents, 28 * trademarks, maskwork rights, or any other form of intellectual 29 * property whatsoever. Maxim Integrated Products, Inc. retains all 30 * ownership rights. 31 * 32 * $Date: 2016-03-17 14:27:29 -0700 (Thu, 17 Mar 2016) $ 33 * $Revision: 21966 $ 34 * 35 ******************************************************************************/ 36 37 #include <stdio.h> 38 #include "mxc_config.h" 39 #include "mxc_assert.h" 40 #include "board.h" 41 #include "gpio.h" 42 #include "uart.h" 43 #include "spim.h" 44 #include "max14690n.h" 45 46 #define UART_ERRORS (MXC_F_UART_INTEN_RX_FIFO_OVERFLOW | \ 47 MXC_F_UART_INTEN_RX_FRAMING_ERR | \ 48 MXC_F_UART_INTEN_RX_PARITY_ERR) 49 50 /***** Global Variables *****/ 51 52 // LEDs 53 // Note: EvKit board uses 3.3v supply so these must be open-drain. 54 const gpio_cfg_t led_pin[] = { 55 { PORT_2, PIN_4, GPIO_FUNC_GPIO, GPIO_PAD_OPEN_DRAIN }, 56 { PORT_2, PIN_5, GPIO_FUNC_GPIO, GPIO_PAD_OPEN_DRAIN }, 57 { PORT_2, PIN_6, GPIO_FUNC_GPIO, GPIO_PAD_OPEN_DRAIN }, 58 }; 59 const unsigned int num_leds = (sizeof(led_pin) / sizeof(gpio_cfg_t)); 60 61 // Pushbuttons 62 const gpio_cfg_t pb_pin[] = { 63 { PORT_2, PIN_3, GPIO_FUNC_GPIO, GPIO_PAD_INPUT_PULLUP }, 64 }; 65 const unsigned int num_pbs = (sizeof(pb_pin) / sizeof(gpio_cfg_t)); 66 67 // Console UART configuration 68 const uart_cfg_t console_uart_cfg = { 69 .parity = UART_PARITY_DISABLE, 70 .size = UART_DATA_SIZE_8_BITS, 71 .extra_stop = 0, 72 .cts = 0, 73 .rts = 0, 74 .baud = CONSOLE_BAUD, 75 }; 76 const sys_cfg_uart_t console_sys_cfg = { 77 .clk_scale = CLKMAN_SCALE_AUTO, 78 .io_cfg = IOMAN_UART(CONSOLE_UART, IOMAN_MAP_A, IOMAN_MAP_UNUSED, IOMAN_MAP_UNUSED, 1, 0, 0) 79 }; 80 81 // MAX14690 PMIC 82 const ioman_cfg_t max14690_io_cfg = IOMAN_I2CM2(IOMAN_MAP_A, 1); 83 const gpio_cfg_t max14690_int = { PORT_3, PIN_7, GPIO_FUNC_GPIO, GPIO_PAD_INPUT_PULLUP }; 84 const gpio_cfg_t max14690_mpc0 = { PORT_2, PIN_7, GPIO_FUNC_GPIO, GPIO_PAD_NORMAL }; 85 86 /***** File Scope Variables *****/ 87 88 /******************************************************************************/ 89 void mxc_assert(const char *expr, const char *file, int line) 90 { 91 printf("MXC_ASSERT %s #%d: (%s)\n", file, line, expr); 92 while (1); 93 } 94 95 /******************************************************************************/ 96 int Console_Init(void) 97 { 98 int err; 99 100 if ((err = UART_Init(MXC_UART_GET_UART(CONSOLE_UART), &console_uart_cfg, &console_sys_cfg)) != E_NO_ERROR) { 101 MXC_ASSERT_FAIL(); 102 return err; 103 } 104 105 // Setup the interrupt 106 NVIC_ClearPendingIRQ(MXC_UART_GET_IRQ(CONSOLE_UART)); 107 NVIC_DisableIRQ(MXC_UART_GET_IRQ(CONSOLE_UART)); 108 NVIC_SetPriority(MXC_UART_GET_IRQ(CONSOLE_UART), 1); 109 NVIC_EnableIRQ(MXC_UART_GET_IRQ(CONSOLE_UART)); 110 MXC_UART1->inten |= (MXC_F_UART_INTEN_RX_FIFO_NOT_EMPTY | UART_ERRORS); 111 112 return E_NO_ERROR; 113 } 114 115 /******************************************************************************/ 116 int Console_PrepForSleep(void) 117 { 118 fflush(stdout); 119 return UART_PrepForSleep(MXC_UART_GET_UART(CONSOLE_UART)); 120 } 121 122 void UART0_IRQHandler(void) 123 { 124 UART_Handler(MXC_UART_GET_UART(0)); 125 } 126 127 void UART1_IRQHandler(void) 128 { 129 UART_Handler(MXC_UART_GET_UART(1)); 130 } 131 132 void UART2_IRQHandler(void) 133 { 134 UART_Handler(MXC_UART_GET_UART(2)); 135 } 136 137 void UART3_IRQHandler(void) 138 { 139 UART_Handler(MXC_UART_GET_UART(3)); 140 } 141 142 /******************************************************************************/ 143 int Board_Init(void) 144 { 145 int err; 146 147 if ((err = Console_Init()) != E_NO_ERROR) { 148 MXC_ASSERT_FAIL(); 149 return err; 150 } 151 152 if ((err = LED_Init()) != E_NO_ERROR) { 153 MXC_ASSERT_FAIL(); 154 return err; 155 } 156 157 if ((err = PB_Init()) != E_NO_ERROR) { 158 MXC_ASSERT_FAIL(); 159 return err; 160 } 161 162 /* On the Pegasus board MPC1 is connected to CAP which is high when VBUS is present. 163 * The LDO_OUTPUT_MPC1 setting will automatically enable the output when VBUS is present. 164 * The LDO_OUTPUT_MPC1 setting will also disable the output when powered from the battery. 165 * The Pegasus board uses LDO2 for VDDB (USB), LEDs and the SD card connector. 166 * Use the MAX14690_LDO2setMode(mode) function to enable LDO2 when needed. 167 */ 168 if ((err = MAX14690N_Init(3.3, LDO_OUTPUT_MPC1, 3.3, LDO_OUTPUT_DISABLED)) != E_NO_ERROR) { 169 MXC_ASSERT_FAIL(); 170 return err; 171 } 172 173 return E_NO_ERROR; 174 } 175 176 #ifdef CRASH_DUMP 177 void FaultISR_C(uint32_t *hardfault_args) 178 { 179 unsigned int stacked_r0; 180 unsigned int stacked_r1; 181 unsigned int stacked_r2; 182 unsigned int stacked_r3; 183 unsigned int stacked_r12; 184 unsigned int stacked_lr; 185 unsigned int stacked_pc; 186 unsigned int stacked_psr; 187 188 volatile unsigned char mmsr; 189 volatile unsigned char bfsr; 190 volatile unsigned short ufsr; 191 volatile unsigned int hfsr; 192 193 stacked_r0 = ((unsigned int) hardfault_args[0]); 194 stacked_r1 = ((unsigned int) hardfault_args[1]); 195 stacked_r2 = ((unsigned int) hardfault_args[2]); 196 stacked_r3 = ((unsigned int) hardfault_args[3]); 197 stacked_r12 = ((unsigned int) hardfault_args[4]); 198 stacked_lr = ((unsigned int) hardfault_args[5]); 199 stacked_pc = ((unsigned int) hardfault_args[6]); 200 stacked_psr = ((unsigned int) hardfault_args[7]); 201 202 printf("\n\n[Hard fault handler - all numbers in hex]\n"); 203 printf("R0 = 0x%08x\n", stacked_r0); 204 printf("R1 = 0x%08x\n", stacked_r1); 205 printf("R2 = 0x%08x\n", stacked_r2); 206 printf("R3 = 0x%08x\n", stacked_r3); 207 printf("R12 = 0x%08x\n", stacked_r12); 208 printf("LR [R14] = 0x%08x subroutine call return address\n", stacked_lr); 209 printf("PC [R15] = 0x%08x program counter address\n", stacked_pc); 210 printf("PSR = 0x%08x\n", stacked_psr); 211 printf("MMAR = 0x%08x memory manage fault address\n", (*((volatile unsigned int *) (0xE000ED34)))); 212 printf("BFAR = 0x%08x bus fault address\n", (*((volatile unsigned int *) (0xE000ED38)))); 213 214 /*********************************************************************************************** 215 * Memory Management Fault Status Register: (0xE000ED28) 216 * Bit Name Description 217 * 7 MMARVALID MMAR is valid (0x40) 218 * 4 MSTKERR Stacking error (0x10) 219 * 3 MUNSTKERR Unstacking error (0x8) 220 * 1 DACCVIOL Data access violation (0x2) 221 * 0 IACCVIOL Instruction access violation (0x1) 222 ***********************************************************************************************/ 223 mmsr = (*((volatile unsigned char *) (0xE000ED28))); 224 printf("MMSR = 0x%02x ", mmsr); 225 if (mmsr & 0x40) 226 printf("MMARVALID: MMAR is valid "); 227 if (mmsr & 0x10) 228 printf("MSTKERR: Stacking error\n"); 229 else if (mmsr & 0x8) 230 printf("MUNSTKERR: Unstacking error\n"); 231 else if (mmsr & 0x2) 232 printf("DACCVIOL: Data access violation\n"); 233 else if (mmsr & 0x1) 234 printf("IACCVIOL: Instruction access violation\n"); 235 else 236 printf("\n"); 237 238 /*********************************************************************************************** 239 * Bus Fault Status Register: (0xE000ED28) 240 * Bit Name Description 241 * 7 BFARVALID BFAR is valid (0x80) 242 * 4 STKERR Stacking error (0x10) 243 * 3 UNSTKERR Unstacking error (0x8) 244 * 2 IMPREISERR Imprecise data access violation (0x4) 245 * 1 PRECISERR Precise data access violation (0x2) 246 * 0 IBUSERR Instruction access violation (0x1) 247 ***********************************************************************************************/ 248 bfsr = (*((volatile unsigned char *) (0xE000ED29))); 249 printf("BFSR = 0x%02x ", bfsr); 250 if (bfsr & 0x80) 251 printf("BFARVALID: BFAR is valid "); 252 if (bfsr & 0x10) 253 printf("STKERR: Stacking error\n"); 254 else if (bfsr & 0x8) 255 printf("UNSTKERR: Unstacking error\n"); 256 else if (bfsr & 0x4) 257 printf("IMPREISERR: Imprecise data access violation\n"); 258 else if (bfsr & 0x2) 259 printf("PRECISERR: Precise data access violation\n"); 260 else if (bfsr & 0x1) 261 printf("IBUSERR: Instruction access violation\n"); 262 else 263 printf("\n"); 264 265 /*********************************************************************************************** 266 * Usage Fault Status Register: (0xE000ED2A) 267 * Bit Name Description 268 * 9 DIVBYZERO Divide by zero will take place (0x200) 269 * 8 UNALIGNED Unaligned access will take place (0x100) 270 * 3 NOCP Attempt to execute a coprocessor instruction (0x8) 271 * 2 INVPC Attempt to do exception with bad value (0x4) 272 * 1 INVSTATE Attempt to switch to invalid state (0x2) 273 * 0 UNDEFINSTR Attempt to execute an undefined instruction (0x1) 274 ***********************************************************************************************/ 275 ufsr = (*((volatile unsigned short *) (0xE000ED2A))); 276 printf("UFSR = 0x%04x ", ufsr); 277 if (ufsr & 0x200) 278 printf("DIVBYZERO: Divide by zero will take place\n"); 279 else if (ufsr & 0x100) 280 printf("UNALIGNED: Unaligned access will take place\n"); 281 else if (ufsr & 0x8) 282 printf("NOCP: Attempt to execute a coprocessor instruction\n"); 283 else if (ufsr & 0x4) 284 printf("INVPC: Attempt to do exception with bad value\n"); 285 else if (ufsr & 0x2) 286 printf("INVSTATE: Attempt to switch to invalid state\n"); 287 else if (ufsr & 0x1) 288 printf("UNDEFINSTR: Attempt to execute an undefined instruction\n"); 289 else 290 printf("\n"); 291 292 /*********************************************************************************************** 293 * Usage Fault Status Register: (0xE000ED2A) 294 * Bit Name Description 295 * 31 DEBUGEVT Hard fault caused by debug event (0x8000_0000) 296 * 30 FORCED Hard fault caused by bus/memory management/usage fault (0x4000_0000) 297 * 1 VECTBL Hard fault caused by failed vector fetch (0x1) 298 ***********************************************************************************************/ 299 hfsr = (*((volatile unsigned int *) (0xE000ED2C))); 300 printf("HFSR = 0x%08x ", hfsr); 301 if (hfsr & 0x80000000) 302 printf("DEBUGEVT: Hard fault caused by debug event\n"); 303 else if (hfsr & 0x40000000) 304 printf("FORCED: Hard fault caused by bus/memory management/usage fault\n"); 305 else if (hfsr & 0x1) 306 printf("VECTBL: Hard fault caused by failed vector fetch\n"); 307 else 308 printf("\n"); 309 310 printf ("AFSR = 0x%08x\n", (*((volatile unsigned int *)(0xE000ED3C)))); 311 printf ("SCB_SHCSR = %x\n", SCB->SHCSR); 312 313 while (1) ; /* Spin so we can use a debugger to anlayzer the situation */ 314 } 315 #else /* ENABLE_CRASH_DUMP */ 316 void FaultISR_C(uint32_t *hardfault_args) 317 { 318 /* spin so we can use a debugger to anlayze the situation */ 319 while(1); 320 /* reset the system */ 321 //NVIC_SystemReset(); 322 } 323 #endif /* CRASH_DUMP */ 324 325 326 void HardFault_Handler(void) 327 { 328 printf("HardFault_Handler! (main)\n"); 329 __asm( 330 " TST LR, #4\n" 331 " ITE EQ \n" 332 " MRSEQ R0, MSP \n" 333 " MRSNE R0, PSP \n" 334 " B FaultISR_C \n"); 335 } 336