-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
316 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule typec-board-base
updated
5 files
+3 −0 | Core/Inc/tim.h | |
+1 −0 | Core/Src/main.c | |
+100 −2 | Core/Src/tim.c | |
+1 −1 | Makefile | |
+34 −22 | typec-board-base.ioc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
#ifndef BSP_IMU_PWM_H | ||
#define BSP_IMU_PWM_H | ||
#include "tim.h" | ||
#include <stdint.h> | ||
|
||
extern void imu_pwm_set(uint16_t pwm); | ||
typedef struct pwm_ins_temp | ||
{ | ||
TIM_HandleTypeDef *htim; | ||
uint32_t channel; | ||
uint32_t tclk; | ||
float period; | ||
float dutyratio; | ||
uint8_t id; | ||
} PWM_Instance_t; | ||
|
||
typedef struct | ||
{ | ||
TIM_HandleTypeDef *htim; | ||
uint32_t channel; | ||
float period; | ||
float dutyratio; | ||
uint8_t id; | ||
} PWM_Config_t; | ||
|
||
PWM_Instance_t *PWM_Register(PWM_Config_t *config); | ||
void PWM_Start(PWM_Instance_t *pwm); | ||
void PWM_Stop(PWM_Instance_t *pwm); | ||
void PWM_Set_Period(PWM_Instance_t *pwm, float period); | ||
void PWM_Set_Frequency(PWM_Instance_t *pwm, float freq); | ||
void PWM_Set_Duty_Ratio(PWM_Instance_t *pwm, float dutyratio); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,100 @@ | ||
#include "bsp_pwm.h" | ||
#include "main.h" | ||
#include "stdlib.h" | ||
#include "memory.h" | ||
|
||
extern TIM_HandleTypeDef htim10; | ||
#define MAX_PWM_DEVICE_NUM (16) | ||
|
||
void BSP_PWM_SetPWM() | ||
// Interrupt and initialization support | ||
static uint8_t idx; | ||
static PWM_Instance_t *pwm_instance[MAX_PWM_DEVICE_NUM] = {NULL}; | ||
|
||
uint32_t PWM_Get_Tclk(TIM_HandleTypeDef *htim); | ||
|
||
PWM_Instance_t *PWM_Register(PWM_Config_t *config) | ||
{ | ||
if (idx >= MAX_PWM_DEVICE_NUM) | ||
while (1) | ||
; | ||
PWM_Instance_t *pwm = (PWM_Instance_t *)malloc(sizeof(PWM_Instance_t)); | ||
|
||
pwm->htim = config->htim; | ||
pwm->channel = config->channel; | ||
pwm->period = config->period; | ||
pwm->dutyratio = config->dutyratio; | ||
pwm->id = config->id; | ||
pwm->tclk = PWM_Get_Tclk(pwm->htim); | ||
|
||
// Start PWM | ||
PWM_Start(pwm); | ||
PWM_Set_Period(pwm, pwm->period); | ||
PWM_Set_Duty_Ratio(pwm, pwm->dutyratio); | ||
pwm_instance[idx++] = pwm; | ||
return pwm; | ||
} | ||
|
||
/* Only provides a formal encapsulation of HAL functions */ | ||
void PWM_Start(PWM_Instance_t *pwm) | ||
{ | ||
HAL_TIM_PWM_Start(pwm->htim, pwm->channel); | ||
} | ||
|
||
/* Only provides a formal encapsulation of HAL functions */ | ||
void PWM_Stop(PWM_Instance_t *pwm) | ||
{ | ||
HAL_TIM_PWM_Stop(pwm->htim, pwm->channel); | ||
} | ||
|
||
/* | ||
* @brief Set the PWM period | ||
* | ||
* @param pwm PWM instance | ||
* @param freq Frequency in Hz | ||
*/ | ||
void PWM_Set_Frequency(PWM_Instance_t *pwm, float freq) | ||
{ | ||
__HAL_TIM_SetAutoreload(pwm->htim, (pwm->tclk) / (freq * (pwm->htim->Init.Prescaler + 1))); | ||
} | ||
|
||
/* | ||
* @brief Set the PWM period | ||
* | ||
* @param pwm PWM instance | ||
* @param period Period in seconds | ||
*/ | ||
void PWM_Set_Period(PWM_Instance_t *pwm, float period) | ||
{ | ||
__HAL_TIM_SetAutoreload(pwm->htim, period * ((pwm->tclk) / (pwm->htim->Init.Prescaler + 1))); | ||
} | ||
|
||
/* | ||
* @brief Set the PWM duty ratio | ||
* | ||
* @param pwm PWM instance | ||
* @param dutyratio Duty ratio from 0 to 1 | ||
*/ | ||
void PWM_Set_Duty_Ratio(PWM_Instance_t *pwm, float dutyratio) | ||
{ | ||
|
||
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, dutyratio * (pwm->htim->Instance->ARR)); | ||
} | ||
|
||
void imu_pwm_set(uint16_t pwm) | ||
// Set the PWM corresponding timer clock source frequency | ||
// tim2~7,12~14:APB1 tim1,8~11:APB2 | ||
uint32_t PWM_Get_Tclk(TIM_HandleTypeDef *htim) | ||
{ | ||
__HAL_TIM_SetCompare(&htim10, TIM_CHANNEL_1, pwm); | ||
uintptr_t tclk_temp = ((uintptr_t)((htim)->Instance)); | ||
if ( | ||
(tclk_temp <= (APB1PERIPH_BASE + 0x2000UL)) && | ||
(tclk_temp >= (APB1PERIPH_BASE + 0x0000UL))) | ||
{ | ||
return (HAL_RCC_GetPCLK1Freq() * (APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] == 0 ? 1 : 2)); | ||
} | ||
else if ( | ||
((tclk_temp <= (APB2PERIPH_BASE + 0x0400UL)) && | ||
(tclk_temp >= (APB2PERIPH_BASE + 0x0000UL))) || | ||
((tclk_temp <= (APB2PERIPH_BASE + 0x4800UL)) && | ||
(tclk_temp >= (APB2PERIPH_BASE + 0x4000UL)))) | ||
{ | ||
return (HAL_RCC_GetPCLK2Freq() * (APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] == 0 ? 1 : 2)); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#ifndef BUZZER_H | ||
#define BUZZER_H | ||
|
||
#include "bsp_pwm.h" | ||
|
||
#define Note_C4 (262) // 261.63Hz, 3822us | ||
#define Note_D4 (294) // 293.66Hz, 3405us | ||
#define Note_E4 (330) // 329.63Hz, 3034us | ||
#define Note_F4 (349) // 349.23Hz, 2863us | ||
#define Note_G4 (392) // 392.00Hz, 2551us | ||
#define Note_A4 (440) // 440.00Hz, 2272us | ||
#define Note_B4 (494) // 493.88Hz, 2052us | ||
|
||
#define Note_C5 (523) // 523.25Hz, 1911us | ||
#define Note_D5 (587) // 587.33Hz, 1703us | ||
#define Note_E5 (659) // 659.26Hz, 1517us | ||
#define Note_F5 (698) // 698.46Hz, 1432us | ||
#define Note_G5 (784) // 784.00Hz, 1276us | ||
#define Note_A5 (880) // 880.00Hz, 1136us | ||
#define Note_B5 (988) // 987.77Hz, 1012us | ||
|
||
#define Note_C6 (1047) // 1046.50Hz, 956us | ||
#define Note_D6 (1175) // 1174.66Hz, 851us | ||
#define Note_E6 (1319) // 1318.51Hz, 758us | ||
#define Note_F6 (1397) // 1396.91Hz, 716us | ||
#define Note_G6 (1568) // 1567.98Hz, 638us | ||
#define Note_A6 (1760) // 1760.00Hz, 568us | ||
#define Note_B6 (1976) // 1975.53Hz, 506us | ||
|
||
#define Note_C7 (2093) // 2093.00Hz, 478us | ||
#define Note_D7 (2349) // 2349.32Hz, 426us | ||
#define Note_E7 (2637) // 2637.02Hz, 379us | ||
#define Note_F7 (2794) // 2793.83Hz, 358us | ||
#define Note_G7 (3136) // 3135.96Hz, 319us | ||
#define Note_A7 (3520) // 3520.00Hz, 284us | ||
#define Note_B7 (3951) // 3951.07Hz, 253us | ||
|
||
#define Note_00 (0) | ||
|
||
#define EIGHTH_NOTE_DURATION (125) | ||
#define FOURTH_NOTE_DURATION (500) | ||
#define HALF_NOTE_DURATION (1000) | ||
#define WHOLE_NOTE_DURATION (2000) | ||
|
||
#define SYSTEM_INITIALIZING {{Note_C4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_E4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_G4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_C5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_E5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_G5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_C6, FOURTH_NOTE_DURATION }} | ||
|
||
#define SYSTEM_READY {{Note_G4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_E5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_E5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_F5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_D5, EIGHTH_NOTE_DURATION }, \ | ||
{Note_C5, FOURTH_NOTE_DURATION }} | ||
|
||
#define SYSTEM_ERROR {{Note_C4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_G4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_E4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_C4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_G4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_E4, EIGHTH_NOTE_DURATION }, \ | ||
{Note_C4, FOURTH_NOTE_DURATION }} | ||
|
||
#define MARIO_THEME {\ | ||
{Note_E6, EIGHTH_NOTE_DURATION},\ | ||
{Note_E6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_E6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_C6, EIGHTH_NOTE_DURATION},\ | ||
{Note_E6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_G6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_G5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, FOURTH_NOTE_DURATION},\ | ||
{Note_C6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_G5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_E5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_A5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, FOURTH_NOTE_DURATION},\ | ||
{Note_B5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_A5, EIGHTH_NOTE_DURATION},\ | ||
{Note_A5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, FOURTH_NOTE_DURATION},\ | ||
{Note_G5, EIGHTH_NOTE_DURATION},\ | ||
{Note_E6, EIGHTH_NOTE_DURATION},\ | ||
{Note_G6, EIGHTH_NOTE_DURATION},\ | ||
{Note_A6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, FOURTH_NOTE_DURATION},\ | ||
{Note_F6, EIGHTH_NOTE_DURATION},\ | ||
{Note_G6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_E6, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, EIGHTH_NOTE_DURATION},\ | ||
{Note_C6, EIGHTH_NOTE_DURATION},\ | ||
{Note_D6, EIGHTH_NOTE_DURATION},\ | ||
{Note_B5, EIGHTH_NOTE_DURATION},\ | ||
{Note_00, FOURTH_NOTE_DURATION}} | ||
|
||
#define SYSTEM_INITIALIZING_NOTE_NUM (7) | ||
#define SYSTEM_READY_NOTE_NUM (6) | ||
#define SYSTEM_ERROR_NOTE_NUM (7) | ||
#define MARIO_NOTE_NUM (44) | ||
|
||
typedef struct _Melody_t | ||
{ | ||
float notes[50][2]; | ||
float loudness; | ||
uint16_t note_num; | ||
} Melody_t; | ||
|
||
typedef enum | ||
{ | ||
BUZZER_OFF = 0, | ||
BUZZER_ON, | ||
} Buzzer_State_t; | ||
|
||
typedef struct | ||
{ | ||
uint16_t frequency; | ||
Buzzer_State_t alarm_state; | ||
float loudness; | ||
} Buzzzer_Instance_t; | ||
|
||
void Buzzer_Init(void); | ||
void Buzzer_Set_State(Buzzzer_Instance_t *buzzer, Buzzer_State_t state); | ||
void Buzzer_Set_Loudness(Buzzzer_Instance_t *buzzer, float loudness); | ||
void Buzzer_Set_Note(Buzzzer_Instance_t *buzzer); | ||
void Buzzer_Play_Melody(Melody_t melody); | ||
#endif // BUZZER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "buzzer.h" | ||
|
||
#define MAX_BUZZER_NUM (1) | ||
Buzzzer_Instance_t g_buzzer; | ||
PWM_Instance_t *g_buzzer_uart_ptr; | ||
uint8_t g_buzzer_count = 0; | ||
|
||
void Buzzer_Init() | ||
{ | ||
PWM_Config_t buzzer_config = { | ||
.htim = &htim4, | ||
.channel = TIM_CHANNEL_3, | ||
.dutyratio = 0, | ||
.period = 0.001, | ||
}; | ||
g_buzzer_uart_ptr = PWM_Register(&buzzer_config); | ||
} | ||
|
||
void Buzzer_Set_Loudness(Buzzzer_Instance_t *buzzer, float loudness) | ||
{ | ||
buzzer->loudness = loudness; | ||
PWM_Set_Duty_Ratio(g_buzzer_uart_ptr, buzzer->loudness); | ||
} | ||
|
||
void Buzzer_Set_Note(Buzzzer_Instance_t *buzzer) | ||
{ | ||
PWM_Set_Frequency(g_buzzer_uart_ptr, buzzer->frequency); | ||
PWM_Set_Duty_Ratio(g_buzzer_uart_ptr, buzzer->loudness); | ||
} | ||
|
||
void Buzzer_Play_Melody(Melody_t melody) | ||
{ | ||
for (int i = 0; i < melody.note_num; i++) | ||
{ | ||
g_buzzer.frequency = melody.notes[i][0]; | ||
g_buzzer.loudness = melody.loudness; | ||
if (g_buzzer.frequency != 0) { | ||
Buzzer_Set_Note(&g_buzzer); | ||
} | ||
HAL_Delay((uint32_t)(melody.notes[i][1])); | ||
Buzzer_Set_Loudness(&g_buzzer, 0); | ||
HAL_Delay((uint32_t)(melody.notes[i][1] * 0.3)); | ||
} | ||
} |