Skip to content

Instantly share code, notes, and snippets.

@ftp27
Created April 11, 2014 09:51
Show Gist options
  • Save ftp27/10454616 to your computer and use it in GitHub Desktop.
Save ftp27/10454616 to your computer and use it in GitHub Desktop.
Echo with USART1 on STM32L-Discovery
#include "stm32l1xx.h"
int main(void)
{
RCC->CR |= RCC_CR_HSEON;
while(!(RCC->CR & RCC_CR_HSERDY));
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; //USART1 Clock ON
USART1->BRR = 0xD05; // Bodrate for 9600 on 32Mhz
USART1->CR1 |= USART_CR1_UE | USART_CR1_TE | USART_CR1_RE; // USART1 ON, TX ON, RX ON
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBLPENR_GPIOALPEN;
GPIOA->AFR[1] |= 0x770; //AF7(USART1..3) to pins 9,10
GPIOA->OTYPER &= ~GPIO_OTYPER_ODR_9; // Output push-pull (reset state)
GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR9; // No pull-up, pull-down
GPIOA->MODER |= GPIO_MODER_MODER9_1; // Alternate function mode
GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR9_1; // 40 MHz High speed
GPIOA->MODER |= GPIO_MODER_MODER10_1; // Alternate function mode
GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR10_1; // 40 MHz High speed
USART1->CR1 |= USART_CR1_RXNEIE; // RXNE Int ON
NVIC_EnableIRQ (USART1_IRQn);
__enable_irq ();
while (1) {
}
}
void USART1_IRQHandler(void) {
if (USART1->SR & USART_SR_RXNE) {
while(!(USART1->SR & USART_SR_TC));
USART1->DR = USART1->DR;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment