#include <stdio.h>
#include <stm32l476xx.h>
// 供printf使用, 項目屬性中勾選Use MicroLIB後有效
int fputc(int ch, FILE *fp)
{
if (fp == &__stdout)
{
if (ch == '\n')
{
// 自動添加\r
while ((USART2->ISR & USART_ISR_TXE) == 0); // 等待TDR可寫入
USART2->TDR = '\r';
}
while ((USART2->ISR & USART_ISR_TXE) == 0);
USART2->TDR = ch;
}
return ch;
}
int main(void)
{
RCC->AHB2ENR = RCC_AHB2ENR_GPIOAEN; // 開PA時鐘
RCC->APB1ENR1 = RCC_APB1ENR1_LPTIM1EN | RCC_APB1ENR1_PWREN | RCC_APB1ENR1_USART2EN; // 開低功耗定時器1和串口2時鐘
RCC->CSR |= RCC_CSR_LSION; // 開LSI
while ((RCC->CSR & RCC_CSR_LSIRDY) == 0); // 等待LSI穩定
GPIOA->MODER &= ~(GPIO_MODER_MODE2_0 | GPIO_MODER_MODE3_0); // 串口2埠: PA2~3設為復用模式
GPIOA->MODER &= ~GPIO_MODER_MODE5_1; // LED燈: PA5設為普通輸出(推挽輸出)
// 根據晶片//chip手冊的Table 16. Alternate function AF0 to AF7可知
// PA2-3的復用功能應映射為AF7才是USART2的功能
GPIOA->AFR[0] = GPIO_AFRL_AFSEL2_2 | GPIO_AFRL_AFSEL2_1 | GPIO_AFRL_AFSEL2_0;
GPIOA->AFR[0] |= GPIO_AFRL_AFSEL3_2 | GPIO_AFRL_AFSEL3_1 | GPIO_AFRL_AFSEL3_0;
USART2->BRR = 417; // 波特率: 9600
USART2->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; // 打開串口2, 允許收發, 開接收中斷
NVIC_EnableIRQ(USART2_IRQn); // 允許執行串口中斷函數
RCC->CCIPR = RCC_CCIPR_LPTIM1SEL_0; // LPTIM1時鐘選LSI
LPTIM1->CFGR = LPTIM_CFGR_PRESC_1; // 4分頻
LPTIM1->IER = LPTIM_IER_ARRMIE; // 開定時器中斷
NVIC_EnableIRQ(LPTIM1_IRQn); // 允許執行定時器中斷函數
LPTIM1->CR = LPTIM_CR_ENABLE; // 打開LPTIM, 但暫不開始計時
LPTIM1->ARR = 39999; // 計時5s
printf("Press 1 to enter stop mode: \n");
while (1);
}
// 低功耗定時器中斷
void LPTIM1_IRQHandler(void)
{
if (LPTIM1->ISR & LPTIM_ISR_ARRM)
{
LPTIM1->ICR = LPTIM_ICR_ARRMCF;
SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk; // 退出中斷時不再自動進入低功耗模式
USART2->CR1 |= USART_CR1_UE; // 開串口
printf("Waked up!\n");
}
}
// 串口2中斷
void USART2_IRQHandler(void)
{
uint8_t ch;
if (USART2->ISR & USART_ISR_RXNE)
{
ch = USART2->RDR; // 讀取接收到的數據
if (ch == '1')
{
// 串口發送1進入睡眠模式
printf("Enter stop mode!\n");
while ((USART2->ISR & USART_ISR_TC) == 0); // 等待串口數據收發完畢
USART2->CR1 &= ~USART_CR1_UE; // 關閉串口
LPTIM1->CR |= LPTIM_CR_SNGSTRT; // 定時器開始計時
PWR->CR1 |= PWR_CR1_LPMS_STOP2;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk; // 離開中斷時自動進入Stop 2模式
}
}
}