From 6a9b75d01b3acbe7799b7450ebddacdd1dad34ae Mon Sep 17 00:00:00 2001 From: Doug Brown Date: Sun, 6 Aug 2023 20:35:29 -0700 Subject: [PATCH] Implement M258KE board functions --- hal/m258ke/board.c | 71 +++++++++++++++++++++++++++++++++++++++ hal/m258ke/board_hw.h | 77 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 hal/m258ke/board.c create mode 100644 hal/m258ke/board_hw.h diff --git a/hal/m258ke/board.c b/hal/m258ke/board.c new file mode 100644 index 0000000..1cb096d --- /dev/null +++ b/hal/m258ke/board.c @@ -0,0 +1,71 @@ +/* + * board.c + * + * Created on: Jun 19, 2023 + * Author: Doug + * + * Copyright (C) 2011-2023 Doug Brown + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "board_hw.h" + +/** Initializes any board hardware-specific stuff + * + */ +void Board_Init(void) +{ + // Unlock protected registers so we can configure clocks, flash, WDT, etc. + do + { + SYS->REGLCTL = 0x59UL; + SYS->REGLCTL = 0x16UL; + SYS->REGLCTL = 0x88UL; + } while (SYS->REGLCTL == 0UL); + + // Enable 48 MHz internal high-speed RC oscillator + CLK->PWRCTL |= CLK_PWRCTL_HIRCEN_Msk; + + // Wait until it's ready + while (!(CLK->STATUS & CLK_STATUS_HIRCSTB_Msk)); + + // Clock HCLK and USB from 48 MHz HIRC + CLK->CLKSEL0 = (CLK->CLKSEL0 & (~(CLK_CLKSEL0_HCLKSEL_Msk | CLK_CLKSEL0_USBDSEL_Msk))) | + (7 << CLK_CLKSEL0_HCLKSEL_Pos) | (0 << CLK_CLKSEL0_USBDSEL_Pos); + + // SystemCoreClock, CyclesPerUs, CyclesPerUs default to correct values already + + // Enable USB device controller + CLK->APBCLK0 |= CLK_APBCLK0_USBDCKEN_Msk; + + // Enable all GPIO + CLK->AHBCLK |= + CLK_AHBCLK_GPACKEN_Msk | + CLK_AHBCLK_GPBCKEN_Msk | + CLK_AHBCLK_GPCCKEN_Msk | + CLK_AHBCLK_GPDCKEN_Msk | + CLK_AHBCLK_GPECKEN_Msk | + CLK_AHBCLK_GPFCKEN_Msk; +} + +/** Determines if a brownout was detected at startup + * + * @return True if a brownout was detected + */ +bool Board_BrownoutDetected(void) +{ + return false; +} diff --git a/hal/m258ke/board_hw.h b/hal/m258ke/board_hw.h new file mode 100644 index 0000000..3836f20 --- /dev/null +++ b/hal/m258ke/board_hw.h @@ -0,0 +1,77 @@ +/* + * board_hw.h + * + * Created on: Jun 19, 2023 + * Author: Doug + * + * Copyright (C) 2011-2023 Doug Brown + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef HAL_M258KE_BOARD_HW_H_ +#define HAL_M258KE_BOARD_HW_H_ + +#include "gpio_hw.h" +#include "../gpio.h" +#include "hardware.h" +#include "../usbcdc.h" + +#define BOARD_LED_INVERTED true + +/** Gets the GPIO pin on the board that controls the status LED + * + * @return The status LED pin + */ +static inline GPIOPin Board_LEDPin(void) +{ + return GPIO_PIN(GPIOC, 9); +} + +/** Jumps to the bootloader + * + */ +static inline void Board_EnterBootloader(void) +{ + // Insert a small delay to ensure that it arrives before rebooting. + DelayMS(1000); + + // Disable interrupts so nothing weird happens... + DisableInterrupts(); + + // Done with the USB interface -- the bootloader will re-initialize it. + USBCDC_Disable(); + + // Wait a little bit to let everything settle and let the program + // close the port after the USB disconnect + DelayMS(2000); + + // Clear reset status bits so that bootloader knows reset reason + SYS->RSTSTS = (SYS_RSTSTS_PORF_Msk | SYS_RSTSTS_PINRF_Msk); + + // Boot to LDROM next time + FMC->ISPCTL |= FMC_ISPCTL_BS_Msk; + + // Save a special flag in RAM to indicate we want to stay in the bootloader + *(uint32_t *)(0x20003FFC) = 0xBADF00D5; + + // Reset! + NVIC_SystemReset(); + + // Should never get here, but just in case... + while (1); +} + +#endif /* HAL_M258KE_BOARD_HW_H_ */