USART1 @ STM32G0B1

This commit is contained in:
Edward Emelianov
2026-03-02 23:21:31 +03:00
parent 71d30dd19a
commit 8f6a80e2c7
15 changed files with 592 additions and 48 deletions

View File

@@ -17,70 +17,50 @@
*/
#include "stm32g0.h"
#include "hardware.h"
#include "usart.h"
#define KEY_PORT GPIOC
#define KEY_PIN (1<<13)
#define LED_PORT GPIOC
#define LED_PIN (1<<6)
#define KEY_PRESSED() (pin_read(KEY_PORT, KEY_PIN) == 1)
#define LED_ON() do{pin_set(LED_PORT, LED_PIN);}while(0)
#define LED_OFF() do{pin_clear(LED_PORT, LED_PIN);}while(0)
// KEY (intpullup->0) - PC13
// LED - PC6
static volatile uint32_t blink_ctr = 0;
volatile uint32_t Tms = 0; // milliseconds
/* Called when systick fires */
void sys_tick_handler(void){
++blink_ctr;
++Tms;
}
/*
* Set up timer to fire every x milliseconds
*/
static void systick_setup(uint32_t xms){ // xms < 2098!!!
blink_ctr = 0;
Tms = 0;
static uint32_t curms = 0;
if(curms == xms) return;
// 8MHz - HCLK/8
// this function also clears counter so it starts right away
SysTick_Config(8000 * xms); // arg should be < 0xffffff, so ms should be < 2098
SysTick_Config(SysFreq / 8000 * xms); // arg should be < 0xffffff, so ms should be < 2098
curms = xms;
}
static void gpio_setup(void){
RCC->IOPENR = RCC_IOPENR_GPIOCEN; // enable PC
// set PC8 as opendrain output, PC0 is pullup input, other as default (AIN)
GPIOC->MODER = (0xffffffff & ~(GPIO_MODER_MODE6 | GPIO_MODER_MODE13)) | GPIO_MODER_MODER6_O; // GPIO_MODER_MODER13_I == 0
GPIOC->PUPDR = GPIO_PUPDR13_PD; // pull down
}
static const uint32_t L[] = {125,100,125,100,125,200, 350,100,350,100,350,200, 125,100,125,100,125, 1000};
int main(void){
StartHSE();
gpio_setup();
systick_setup(500);
systick_setup(1); // run each 1ms
usart_setup(115200);
uint32_t M = 0;
int pressed = 0;
//int pressed = 0;
/* Do nothing in main loop */
while (1){
if(KEY_PRESSED()){ // key pressed - 'sos'
pressed = 1;
systick_setup(L[M]);
if(M & 1) LED_OFF();
else LED_ON();
if(++M == 18) M = 0;
while(blink_ctr == 0);
}else{ // key not pressed - blink with period of 1s
if(pressed){
M = 0;
pressed = 0;
systick_setup(500);
}
if(blink_ctr & 1) LED_ON();
else LED_OFF();
while(1){
if(Tms - M > 499){
LED_TOGGLE();
M = Tms;
}
//if(KEY_PRESSED()){ // key pressed - 'sos'
USART_flags_t f = usart_process();
if(f.rxovrfl) usart_sendstr("Rx overflow!\n");
if(f.txerr) usart_sendstr("Tx error!\n");
char *got = usart_getline();
if(got){
usart_sendstr("You sent:\n");
usart_sendstr(got);
usart_sendstr("\n=======================\n");
}
}
}