#include "stm32c0xx_hal.h"
#include <stdio.h> // For printf
/* -------- Pins -------- */
#define BTN_WAKE GPIO_PIN_0 // PA0
#define BTN_NOISE GPIO_PIN_1 // PA1
#define BTN_SILENCE GPIO_PIN_2 // PA2
#define LED_STATUS GPIO_PIN_6 // PC6
/* -------- Function Prototypes -------- */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void blink_times(uint8_t count);
/* -------- Main -------- */
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
printf("System Ready\r\n");
while (1)
{
/* WAKE WORD */
if (HAL_GPIO_ReadPin(GPIOA, BTN_WAKE) == GPIO_PIN_RESET)
{
printf("Wake-word button pressed\r\n");
blink_times(1);
while (HAL_GPIO_ReadPin(GPIOA, BTN_WAKE) == GPIO_PIN_RESET); // wait release
HAL_Delay(50); // debounce
}
/* NOISE */
else if (HAL_GPIO_ReadPin(GPIOA, BTN_NOISE) == GPIO_PIN_RESET)
{
printf("Noise button pressed\r\n");
blink_times(2);
while (HAL_GPIO_ReadPin(GPIOA, BTN_NOISE) == GPIO_PIN_RESET);
HAL_Delay(50);
}
/* SILENCE */
else if (HAL_GPIO_ReadPin(GPIOA, BTN_SILENCE) == GPIO_PIN_RESET)
{
printf("Silence button pressed\r\n");
HAL_GPIO_WritePin(GPIOC, LED_STATUS, GPIO_PIN_RESET); // turn off LED
while (HAL_GPIO_ReadPin(GPIOA, BTN_SILENCE) == GPIO_PIN_RESET);
HAL_Delay(50);
}
}
}
/* -------- Blink LED -------- */
void blink_times(uint8_t count)
{
for (uint8_t i = 0; i < count; i++)
{
HAL_GPIO_WritePin(GPIOC, LED_STATUS, GPIO_PIN_SET);
printf("LED ON\r\n");
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOC, LED_STATUS, GPIO_PIN_RESET);
printf("LED OFF\r\n");
HAL_Delay(200);
}
}
/* -------- GPIO Init -------- */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/* Buttons as input with pull-up */
GPIO_InitStruct.Pin = BTN_WAKE | BTN_NOISE | BTN_SILENCE;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* LED as output */
GPIO_InitStruct.Pin = LED_STATUS;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
/* -------- Clock Config -------- */
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK |
RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
}