中级会员
- 积分
- 354
- 金钱
- 354
- 注册时间
- 2018-9-14
- 在线时间
- 157 小时
|
楼主 |
发表于 2020-12-5 12:58:07
|
显示全部楼层
本帖最后由 sppz 于 2020-12-5 15:57 编辑
具体行为直接上代码
- import px.base_object;
- import px.cpu.stm32mp157.RCC;
- import px.cpu.stm32mp157.GPIO;
- import px.cpu.stm32mp157.USART;
- import px.cpu.stm32mp157.GIC;
- using namespace px_namespace;
- using namespace px_namespace::cpu::stm32mp157;
- static void reset_clock() noexcept
- {
- // clock use HSI
- {
- auto ocensetr(RCC::OCENSETR());
- ocensetr.HSION() = 1;
- ocensetr.push();
- for (auto ocrdyr(RCC::OCRDYR()); ocrdyr.HSIRDY() == 0; ocrdyr.pull()) {}
- auto hsicfgr(RCC::HSICFGR());
- hsicfgr.HSIDIV() = 0;
- hsicfgr.push();
- for (auto ocrdyr(RCC::OCRDYR()); ocrdyr.HSIDIVRDY() == 0; ocrdyr.pull()) {}
- auto mpckselr(RCC::MPCKSELR());
- mpckselr.MPUSRC() = 0;
- mpckselr.push();
- do { mpckselr.pull(); } while (mpckselr.MPUSRCRDY() == 0);
- auto axidivr(RCC::AXIDIVR());
- axidivr.AXIDIV() = 0;
- axidivr.push();
- do { axidivr.pull(); } while (axidivr.AXIDIVRDY() == 0);
- auto assckselr(RCC::ASSCKSELR());
- assckselr.AXISSRC() = 0;
- assckselr.push();
- do { assckselr.pull(); } while (assckselr.AXISSRCRDY() == 0);
- auto mcudivr(RCC::MCUDIVR());
- mcudivr.MCUDIV() = 0;
- mcudivr.push();
- do { mcudivr.pull(); } while (mcudivr.MCUDIVRDY() == 0);
- auto mssckselr(RCC::MSSCKSELR());
- mssckselr.MCUSSRC() = 0;
- mssckselr.push();
- do { mssckselr.pull(); } while (mssckselr.MCUSSRCRDY() == 0);
- }
- // close PLLs
- {
- auto pll1cr(RCC::PLL1CR());
- pll1cr.DIVPEN() = 0;
- pll1cr.DIVQEN() = 0;
- pll1cr.DIVREN() = 0;
- pll1cr.push();
- pll1cr.PLLON() = 0;
- pll1cr.push();
- do { pll1cr.pull(); } while (pll1cr.PLL1RDY() == 1);
- auto pll2cr(RCC::PLL2CR());
- pll2cr.DIVPEN() = 0;
- pll2cr.DIVQEN() = 0;
- pll2cr.DIVREN() = 0;
- pll2cr.push();
- pll2cr.PLLON() = 0;
- pll2cr.push();
- do { pll2cr.pull(); } while (pll2cr.PLL2RDY() == 1);
- auto pll3cr(RCC::PLL3CR());
- pll3cr.DIVPEN() = 0;
- pll3cr.DIVQEN() = 0;
- pll3cr.DIVREN() = 0;
- pll3cr.push();
- pll3cr.PLLON() = 0;
- pll3cr.push();
- do { pll3cr.pull(); } while (pll3cr.PLL3RDY() == 1);
- auto pll4cr(RCC::PLL4CR());
- pll4cr.DIVPEN() = 0;
- pll4cr.DIVQEN() = 0;
- pll4cr.DIVREN() = 0;
- pll4cr.push();
- pll4cr.PLLON() = 0;
- pll4cr.push();
- do { pll4cr.pull(); } while (pll4cr.PLL4RDY() == 1);
- }
- }
- // MPU 600Mhz, AXI 264Mhz, MCU 200Mhz
- static void set_clock() noexcept
- {
- // open HSE
- {
- auto ocensetr(RCC::OCENSETR());
- ocensetr.HSEON();
- ocensetr.push();
- for (auto ocrdyr(RCC::OCRDYR()); ocrdyr.HSERDY() == 0; ocrdyr.pull()) {}
- }
- // PLLs source select
- {
- auto rck12selr(RCC::RCK12SELR());
- rck12selr.PLL12SRC() = 1;
- rck12selr.push();
- do { rck12selr.pull(); } while (rck12selr.PLL12SRCRDY() == 0);
- auto rck3selr(RCC::RCK3SELR());
- rck3selr.PLL3SRC() = 1;
- rck3selr.push();
- do { rck3selr.pull(); } while (rck3selr.PLL3SRCRDY() == 0);
- }
- // PLL1 set
- {
- auto pll1cfgr1(RCC::PLL1CFGR1());
- pll1cfgr1.DIVN() = 49;
- pll1cfgr1.DIVM1() = 1;
- pll1cfgr1.push();
- auto pll1cfgr2(RCC::PLL1CFGR2());
- pll1cfgr2.DIVP() = 0;
- pll1cfgr2.push();
- auto pll1fracr(RCC::PLL1FRACR());
- pll1fracr.FRACV() = 0;
- pll1fracr.FRACLE() = 0;
- pll1fracr.push();
- pll1fracr.FRACLE() = 1;
- pll1fracr.push();
- auto pll1cr(RCC::PLL1CR());
- pll1cr.SSCG_CTRL() = 0;
- pll1cr.PLLON() = 1;
- pll1cr.push();
- do { pll1cr.pull(); } while (pll1cr.PLL1RDY() == 0);
- pll1cr.DIVPEN() = 1;
- pll1cr.push();
- }
- // PLL2 set
- {
- auto pll2cfgr1(RCC::PLL2CFGR1());
- pll2cfgr1.DIVN() = 43;
- pll2cfgr1.DIVM2() = 1;
- pll2cfgr1.push();
- auto pll2cfgr2(RCC::PLL2CFGR2());
- pll2cfgr2.DIVP() = 1;
- pll2cfgr2.push();
- auto pll2fracr(RCC::PLL2FRACR());
- pll2fracr.FRACV() = 0;
- pll2fracr.FRACLE() = 0;
- pll2fracr.push();
- pll2fracr.FRACLE() = 1;
- pll2fracr.push();
- auto pll2csgr(RCC::PLL2CSGR());
- pll2csgr.MOD_PER() = 0;
- pll2csgr.SSCG_MODE() = 1;
- pll2csgr.INC_STEP() = 0;
- pll2csgr.push();
- auto pll2cr(RCC::PLL2CR());
- pll2cr.SSCG_CTRL() = 1;
- pll2cr.PLLON() = 1;
- pll2cr.push();
- do { pll2cr.pull(); } while (pll2cr.PLL2RDY() == 0);
- pll2cr.DIVPEN() = 1;
- pll2cr.push();
- }
- // PLL3 set
- {
- auto pll3cfgr1(RCC::PLL3CFGR1());
- pll3cfgr1.DIVN() = 49;
- pll3cfgr1.DIVM3() = 2;
- pll3cfgr1.IFRGE() = 1;
- pll3cfgr1.push();
- auto pll3cfgr2(RCC::PLL3CFGR2());
- pll3cfgr2.DIVP() = 1;
- pll3cfgr2.push();
- auto pll3fracr(RCC::PLL3FRACR());
- pll3fracr.FRACV() = 0;
- pll3fracr.FRACLE() = 0;
- pll3fracr.push();
- pll3fracr.FRACLE() = 1;
- pll3fracr.push();
- auto pll3cr(RCC::PLL3CR());
- pll3cr.PLLON() = 1;
- pll3cr.push();
- do { pll3cr.pull(); } while (pll3cr.PLL3RDY() == 0);
- pll3cr.DIVPEN() = 1;
- pll3cr.push();
- }
- // MCU set
- {
- auto mcudivr(RCC::MCUDIVR());
- mcudivr.MCUDIV() = 0;
- mcudivr.push();
- do { mcudivr.pull(); } while (mcudivr.MCUDIVRDY() == 0);
- }
- // AXI set
- {
- auto axidivr(RCC::AXIDIVR());
- axidivr.AXIDIV() = 0;
- axidivr.push();
- do { axidivr.pull(); } while (axidivr.AXIDIVRDY() == 0);
- }
- // APB4 set
- {
- auto apb4divr(RCC::APB4DIVR());
- apb4divr.APB4DIV() = 1;
- apb4divr.push();
- do { apb4divr.pull(); } while (apb4divr.APB4DIVRDY() == 0);
- }
- // APB5
- {
- auto apb5divr(RCC::APB5DIVR());
- apb5divr.APB5DIV() = 2;
- apb5divr.push();
- do { apb5divr.pull(); } while (apb5divr.APB5DIVRDY() == 0);
- }
- // APB1
- {
- auto apb1divr(RCC::APB1DIVR());
- apb1divr.APB1DIV() = 1;
- apb1divr.push();
- do { apb1divr.pull(); } while (apb1divr.APB1DIVRDY() == 0);
- }
- // APB2
- {
- auto apb2divr(RCC::APB2DIVR());
- apb2divr.APB2DIV() = 1;
- apb2divr.push();
- do { apb2divr.pull(); } while (apb2divr.APB2DIVRDY() == 0);
- }
- // APB3
- {
- auto apb3divr(RCC::APB3DIVR());
- apb3divr.APB3DIV() = 1;
- apb3divr.push();
- do { apb3divr.pull(); } while (apb3divr.APB3DIVRDY() == 0);
- }
- // switch
- {
- auto mpckselr(RCC::MPCKSELR());
- mpckselr.MPUSRC() = 2;
- mpckselr.push();
- do { mpckselr.pull(); } while (mpckselr.MPUSRCRDY() == 0);
- auto assckselr(RCC::ASSCKSELR());
- assckselr.AXISSRC() = 2;
- assckselr.push();
- do { assckselr.pull(); } while (assckselr.AXISSRCRDY() == 0);
- auto mssckselr(RCC::MSSCKSELR());
- mssckselr.MCUSSRC() = 3;
- mssckselr.push();
- do { mssckselr.pull(); } while (mssckselr.MCUSSRCRDY() == 0);
- }
- }
- static void initialize_clock() noexcept
- {
- reset_clock();
- set_clock();
- }
- void initialize_gic() noexcept
- {
- //84 uart4
- auto igroupr(GICD::IGROUPR(84));
- igroupr.GROUP_STATUS() = 1;
- igroupr.push();
- auto isenabler(GICD::ISENABLER(84));
- isenabler.SET_ENABLE() = 1;
- isenabler.push();
- auto ipriorityr(GICD::IPRIORITYR(84));
- ipriorityr.PRIORITY() = 0;
- ipriorityr.push();
- auto itargetsr(GICD::ITARGETSR(84));
- itargetsr.CPU_TARGETS() = 1;
- itargetsr.push();
- auto icfgr(GICD::ICFGR(84));
- icfgr.INT_CONFIG() = 1;
- icfgr.push();
- }
- static void initialize_gpio() noexcept
- {
- // open GPIO[B, F, G, I]
- {
- auto mp_ahb4ensetr(RCC::MP_AHB4ENSETR());
- mp_ahb4ensetr.GPIOBEN() = 1;
- mp_ahb4ensetr.GPIOFEN() = 1;
- mp_ahb4ensetr.GPIOGEN() = 1;
- mp_ahb4ensetr.GPIOIEN() = 1;
- mp_ahb4ensetr.push();
- }
- { // B.2 UART4_RX
- auto moder(GPIOB::MODER());
- moder.MODE2() = 2;
- moder.push();
- auto otyper(GPIOB::OTYPER());
- otyper.OT2() = 0;
- otyper.push();
- auto ospeedr(GPIOB::OSPEEDR());
- ospeedr.OS2() = 3;
- ospeedr.push();
- auto pupdr(GPIOB::PUPDR());
- pupdr.PUPD2() = 1;
- pupdr.push();
- auto afrl(GPIOB::AFRL());
- afrl.AF2() = 8;
- afrl.push();
- }
- { // F.3 led
- auto moder(GPIOF::MODER());
- moder.MODE3() = 1;
- moder.push();
- auto otyper(GPIOF::OTYPER());
- otyper.OT3() = 0;
- otyper.push();
- auto ospeedr(GPIOF::OSPEEDR());
- ospeedr.OS3() = 0;
- ospeedr.push();
- auto pupdr(GPIOF::PUPDR());
- pupdr.PUPD3() = 1;
- pupdr.push();
- auto odr(GPIOF::ODR());
- odr.OD3() = 1;
- odr.push();
- }
- { // G.11 UART4_TX
- auto moder(GPIOG::MODER());
- moder.MODE11() = 2;
- moder.push();
- auto otyper(GPIOG::OTYPER());
- otyper.OT11() = 0;
- otyper.push();
- auto ospeedr(GPIOG::OSPEEDR());
- ospeedr.OS11() = 3;
- ospeedr.push();
- auto pupdr(GPIOG::PUPDR());
- pupdr.PUPD11() = 1;
- pupdr.push();
- auto afrh(GPIOG::AFRH());
- afrh.AF11() = 6;
- afrh.push();
- }
- { // I.0 led
- auto moder(GPIOI::MODER());
- moder.MODE0() = 1;
- moder.push();
- auto otyper(GPIOI::OTYPER());
- otyper.OT0() = 0;
- otyper.push();
- auto ospeedr(GPIOI::OSPEEDR());
- ospeedr.OS0() = 0;
- ospeedr.push();
- auto pupdr(GPIOI::PUPDR());
- pupdr.PUPD0() = 1;
- pupdr.push();
- auto odr(GPIOI::ODR());
- odr.OD0() = 0;
- odr.push();
- }
- }
- static void initialize_uart4() noexcept
- {
- auto apb1rstsetr(RCC::APB1RSTSETR());
- apb1rstsetr.UART4RST() = 1;
- apb1rstsetr.push();
- auto apb1rstclrr(RCC::APB1RSTCLRR());
- while (apb1rstclrr.UART4RST() == 0) { apb1rstclrr.pull(); }
- apb1rstclrr.UART4RST() = 1;
- apb1rstclrr.push();
- do { apb1rstclrr.pull(); } while (apb1rstclrr.UART4RST() == 1);
- auto uart24ckselr(RCC::UART24CKSELR());
- uart24ckselr.UART24SRC() = 0;
- uart24ckselr.push();
- auto mp_apb1ensetr(RCC::MP_APB1ENSETR());
- mp_apb1ensetr.UART4EN() = 1;
- mp_apb1ensetr.push();
- auto presc(UART4::PRESC());
- presc.PRESCALER() = 0;
- presc.push();
- auto brr(UART4::BRR());
- brr.BR() = 100000000 / 115200;
- brr.push();
- auto cr1(UART4::CR1());
- cr1.UE() = 1;
- cr1.RE() = 1;
- cr1.TE() = 1;
- cr1.RXNEIE() = 1;
- cr1.M0() = 0;
- cr1.OVER8() = 0;
- cr1.M1() = 0;
- cr1.push();
- }
- void put_character(const character_type value) noexcept
- {
- for (auto isr(UART4::ISR()); isr.TXE() == 0; isr.pull()) {}
- auto tdr(UART4::TDR(0));
- tdr.TD() = value;
- tdr.push();
- }
- void put_string(const character_type* value) noexcept
- {
- while (*value != '\0')
- {
- put_character(*value);
- ++value;
- }
- }
- void put_size(const size& value) noexcept
- {
- if (value < 10)
- {
- put_character('0' + value.value());
- return;
- }
- put_size(value / 10);
- put_character('0' + (value % 10).value());
- }
- static void delay(size i) noexcept
- {
- i *= 1000 * 128;
- while (i > 0)
- {
- asm volatile
- (// 256
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;" "sub %[i], #1;"
- : [i]"+r"(i)
- :
- :
- );
- }
- }
- void initialize() noexcept
- {
- initialize_clock();
- initialize_gic();
- initialize_gpio();
- initialize_uart4();
- put_string("boot.\r\n");
- while (yes)
- {
- delay(600);
- put_string("switch.\r\n");
- {
- auto odr(GPIOF::ODR());
- odr.OD3() ^= 1;
- odr.push();
- }
- }
- }
- extern "C"
- {
- void px_GIC_UART4() noexcept // 最简单的处理
- {
- auto isr(UART4::ISR());
- if (isr.RXNE() == 1)
- {
- put_character((character_type)UART4::RDR().RD());
- }
- }
- void px_GIC_default() noexcept
- {
- put_string("GIC_default\r\n");
- }
- }
复制代码
这里和上一篇的变化主要是
1.增加了初始化GIC的代码
2.增加了px_GIC_default, px_GIC_UART4两个中断处理函数
|
|