usart.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* (c) Daniele Lacamera 2019
  2. * GPL
  3. */
  4. #include <unicore-mx/stm32/rcc.h>
  5. #include <unicore-mx/stm32/gpio.h>
  6. #include <unicore-mx/stm32/usart.h>
  7. void usart1_puts(char *s) {
  8. while(*s) {
  9. usart_send_blocking(USART1, *s);
  10. s++;
  11. }
  12. return;
  13. }
  14. uint8_t usart1_getc(void) {
  15. return usart_recv_blocking(USART1);
  16. }
  17. void usart1_putc(uint8_t c) {
  18. usart_send_blocking(USART1, c);
  19. return;
  20. }
  21. void usart1_init(void) {
  22. /* enable clock, USART1 is on GPIOA */
  23. rcc_periph_clock_enable(RCC_GPIOA);
  24. rcc_periph_clock_enable(RCC_USART1);
  25. /* setup TX pin (PA9) */
  26. gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
  27. /* setup RX pin (PA10) */
  28. gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
  29. /* Setup USART parameters. */
  30. usart_set_baudrate(USART1, 115200);
  31. usart_set_databits(USART1, 8);
  32. usart_set_stopbits(USART1, USART_STOPBITS_1);
  33. usart_set_mode(USART1, USART_MODE_TX_RX);
  34. usart_set_parity(USART1, USART_PARITY_NONE);
  35. usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
  36. /* enable USART */
  37. usart_enable(USART1);
  38. return;
  39. }