SerialUSB using HSI@48MHz

LibMaple (The core that Roger's repo uses)
Post Reply
edogaldo
Posts: 283
Joined: Fri Jun 03, 2016 8:19 am

SerialUSB using HSI@48MHz

Post by edogaldo » Mon Oct 09, 2017 4:08 pm

Not supported but seems working:

Code: Select all

#include <libmaple/rcc.h>

#define BOARD_RCC_PLLMUL RCC_PLLMUL_12
static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL};
rcc_pll_cfg w_board_pll_cfg = {RCC_PLLSRC_HSI_DIV_2, &pll_data};

void switch_to_HSI_48MHz_USB();

void setup() {
  // put your setup code here, to run once:
  switch_to_HSI_48MHz_USB();
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("SerialUSB with HSI@48MHz");
  uint hseon=(RCC_BASE->CR & RCC_CR_HSEON)>>RCC_CR_HSEON_BIT;
  uint hsion=(RCC_BASE->CR & RCC_CR_HSION)>>RCC_CR_HSION_BIT;
  uint pllsrc=(RCC_BASE->CFGR & RCC_CFGR_PLLSRC)>>RCC_CFGR_PLLSRC_BIT;
  uint sws=(RCC_BASE->CFGR & RCC_CFGR_SWS)>>2;
  uint sw=(RCC_BASE->CFGR & RCC_CFGR_SW);
  Serial.print("HSEON=");Serial.println(hseon?"Yes":"No");
  Serial.print("HSION=");Serial.println(hsion?"Yes":"No");
  Serial.print("PLLSRC=");Serial.println(pllsrc?"HSE":"HSI");
  Serial.print("SWS=");Serial.println(sws==0?"HSI":sws==1?"HSE":sws==2?"PLL":"NA");
  Serial.print("SW=");Serial.println(sw==0?"HSI":sw==1?"HSE":sw==2?"PLL":"NA");
  Serial.println();
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100); 
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(900); 

}

void switch_to_HSI_48MHz_USB()
{
    // Turn on HSI. We'll switch to and run off of this while we're
    // setting up the main PLL.
    rcc_turn_on_clk(RCC_CLK_HSI);

    // Turn off and reset the clock subsystems we'll be using, as well
    // as the clock security subsystem (CSS). Note that resetting CFGR
    // to its default value of 0 implies a switch to HSI for SYSCLK.
    RCC_BASE->CFGR = 0x00000000;
    rcc_disable_css();
    rcc_turn_off_clk(RCC_CLK_PLL);
    rcc_turn_off_clk(RCC_CLK_HSE);
    //wirish::priv::board_reset_pll();
    
    // Clear clock readiness interrupt flags and turn off clock
    // readiness interrupts.
    RCC_BASE->CIR = 0x00000000;

    rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1);
    rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2);
    rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1);
    rcc_clk_disable(RCC_USB);
    rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1);     

    rcc_configure_pll(&w_board_pll_cfg);

    // Enable the PLL, and wait until it's ready.
    rcc_turn_on_clk(RCC_CLK_PLL);
    while(!rcc_is_clk_ready(RCC_CLK_PLL))
        ;

    // Finally, switch to the now-ready PLL as the main clock source.
    rcc_switch_sysclk(RCC_CLKSRC_PLL);

}

Post Reply