mirror of
https://gitee.com/dlmu-cone/tronone-h7-scaffold
synced 2026-07-25 03:47:46 +08:00
format
This commit is contained in:
@@ -6,7 +6,8 @@
|
|||||||
static SPIInstance *spi_instance[SPI_DEVICE_CNT] = {NULL};
|
static SPIInstance *spi_instance[SPI_DEVICE_CNT] = {NULL};
|
||||||
static uint8_t idx = 0; // 配合中断以及初始化
|
static uint8_t idx = 0; // 配合中断以及初始化
|
||||||
|
|
||||||
SPIInstance *SPIRegister(SPI_Init_Config_s *conf) {
|
SPIInstance *SPIRegister(SPI_Init_Config_s *conf)
|
||||||
|
{
|
||||||
if (idx >= MX_SPI_BUS_SLAVE_CNT) // 超过最大实例数
|
if (idx >= MX_SPI_BUS_SLAVE_CNT) // 超过最大实例数
|
||||||
while (1);
|
while (1);
|
||||||
SPIInstance *instance = (SPIInstance *) malloc(sizeof(SPIInstance));
|
SPIInstance *instance = (SPIInstance *) malloc(sizeof(SPIInstance));
|
||||||
@@ -23,10 +24,12 @@ SPIInstance *SPIRegister(SPI_Init_Config_s *conf) {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) {
|
void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
|
||||||
|
{
|
||||||
// 拉低片选,开始传输(选中从机)
|
// 拉低片选,开始传输(选中从机)
|
||||||
HAL_GPIO_WritePin(spi_ins->GPIOx, spi_ins->cs_pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(spi_ins->GPIOx, spi_ins->cs_pin, GPIO_PIN_RESET);
|
||||||
switch (spi_ins->spi_work_mode) {
|
switch (spi_ins->spi_work_mode)
|
||||||
|
{
|
||||||
case SPI_DMA_MODE:
|
case SPI_DMA_MODE:
|
||||||
HAL_SPI_Transmit_DMA(spi_ins->spi_handle, ptr_data, len);
|
HAL_SPI_Transmit_DMA(spi_ins->spi_handle, ptr_data, len);
|
||||||
break;
|
break;
|
||||||
@@ -44,13 +47,15 @@ void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) {
|
void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
|
||||||
|
{
|
||||||
// 用于稍后回调使用
|
// 用于稍后回调使用
|
||||||
spi_ins->rx_size = len;
|
spi_ins->rx_size = len;
|
||||||
spi_ins->rx_buffer = ptr_data;
|
spi_ins->rx_buffer = ptr_data;
|
||||||
// 拉低片选,开始传输
|
// 拉低片选,开始传输
|
||||||
HAL_GPIO_WritePin(spi_ins->GPIOx, spi_ins->cs_pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(spi_ins->GPIOx, spi_ins->cs_pin, GPIO_PIN_RESET);
|
||||||
switch (spi_ins->spi_work_mode) {
|
switch (spi_ins->spi_work_mode)
|
||||||
|
{
|
||||||
case SPI_DMA_MODE:
|
case SPI_DMA_MODE:
|
||||||
HAL_SPI_Receive_DMA(spi_ins->spi_handle, ptr_data, len);
|
HAL_SPI_Receive_DMA(spi_ins->spi_handle, ptr_data, len);
|
||||||
break;
|
break;
|
||||||
@@ -68,13 +73,15 @@ void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_tx, uint8_t len) {
|
void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_tx, uint8_t len)
|
||||||
|
{
|
||||||
// 用于稍后回调使用,请保证ptr_data_rx在回调函数被调用之前仍然在作用域内,否则析构之后的行为是未定义的!!!
|
// 用于稍后回调使用,请保证ptr_data_rx在回调函数被调用之前仍然在作用域内,否则析构之后的行为是未定义的!!!
|
||||||
spi_ins->rx_size = len;
|
spi_ins->rx_size = len;
|
||||||
spi_ins->rx_buffer = ptr_data_rx;
|
spi_ins->rx_buffer = ptr_data_rx;
|
||||||
// 拉低片选,开始传输
|
// 拉低片选,开始传输
|
||||||
HAL_GPIO_WritePin(spi_ins->GPIOx, spi_ins->cs_pin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(spi_ins->GPIOx, spi_ins->cs_pin, GPIO_PIN_RESET);
|
||||||
switch (spi_ins->spi_work_mode) {
|
switch (spi_ins->spi_work_mode)
|
||||||
|
{
|
||||||
case SPI_DMA_MODE:
|
case SPI_DMA_MODE:
|
||||||
HAL_SPI_TransmitReceive_DMA(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len);
|
HAL_SPI_TransmitReceive_DMA(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len);
|
||||||
break;
|
break;
|
||||||
@@ -92,11 +99,13 @@ void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode) {
|
void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode)
|
||||||
|
{
|
||||||
if (spi_mode != SPI_DMA_MODE && spi_mode != SPI_IT_MODE && spi_mode != SPI_BLOCK_MODE)
|
if (spi_mode != SPI_DMA_MODE && spi_mode != SPI_IT_MODE && spi_mode != SPI_BLOCK_MODE)
|
||||||
while (1); // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
|
while (1); // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
|
||||||
|
|
||||||
if (spi_ins->spi_work_mode != spi_mode) {
|
if (spi_ins->spi_work_mode != spi_mode)
|
||||||
|
{
|
||||||
spi_ins->spi_work_mode = spi_mode;
|
spi_ins->spi_work_mode = spi_mode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,11 +115,14 @@ void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode) {
|
|||||||
*
|
*
|
||||||
* @param hspi spi handle
|
* @param hspi spi handle
|
||||||
*/
|
*/
|
||||||
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
|
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||||
for (size_t i = 0; i < idx; i++) {
|
{
|
||||||
|
for (size_t i = 0; i < idx; i++)
|
||||||
|
{
|
||||||
// 如果是当前spi硬件发出的complete,且cs_pin为低电平(说明正在传输),则尝试调用回调函数
|
// 如果是当前spi硬件发出的complete,且cs_pin为低电平(说明正在传输),则尝试调用回调函数
|
||||||
if (spi_instance[i]->spi_handle == hspi && // 显然同一时间一条总线只能有一个从机在接收数据
|
if (spi_instance[i]->spi_handle == hspi && // 显然同一时间一条总线只能有一个从机在接收数据
|
||||||
HAL_GPIO_ReadPin(spi_instance[i]->GPIOx, spi_instance[i]->cs_pin) == GPIO_PIN_RESET) {
|
HAL_GPIO_ReadPin(spi_instance[i]->GPIOx, spi_instance[i]->cs_pin) == GPIO_PIN_RESET)
|
||||||
|
{
|
||||||
// 先拉高片选,结束传输,在判断是否有回调函数,如果有则调用回调函数
|
// 先拉高片选,结束传输,在判断是否有回调函数,如果有则调用回调函数
|
||||||
HAL_GPIO_WritePin(spi_instance[i]->GPIOx, spi_instance[i]->cs_pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(spi_instance[i]->GPIOx, spi_instance[i]->cs_pin, GPIO_PIN_SET);
|
||||||
// @todo 后续添加holdon模式,由用户自行决定何时释放片选,允许进行连续传输
|
// @todo 后续添加holdon模式,由用户自行决定何时释放片选,允许进行连续传输
|
||||||
@@ -126,6 +138,7 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
|
|||||||
* 这是对HAL库的__weak函数的重写,传输使用IT或DMA模式,在传输完成时会调用此函数
|
* 这是对HAL库的__weak函数的重写,传输使用IT或DMA模式,在传输完成时会调用此函数
|
||||||
* @param hspi spi handle
|
* @param hspi spi handle
|
||||||
*/
|
*/
|
||||||
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
|
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||||
|
{
|
||||||
HAL_SPI_RxCpltCallback(hspi); // 直接调用接收完成的回调函数
|
HAL_SPI_RxCpltCallback(hspi); // 直接调用接收完成的回调函数
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,9 @@
|
|||||||
//7 - si - 1976Hz
|
//7 - si - 1976Hz
|
||||||
|
|
||||||
//高中低音的实际频率 Hz
|
//高中低音的实际频率 Hz
|
||||||
const uint16_t music_steps[3][7] = {{262,294,330,349,392,440,494},
|
const uint16_t music_steps[3][7] = {{262, 294, 330, 349, 392, 440, 494},
|
||||||
{523,587,659,698,784,880,988},
|
{523, 587, 659, 698, 784, 880, 988},
|
||||||
{1046,1175,1318,1397,1568,1760,1976}};
|
{1046, 1175, 1318, 1397, 1568, 1760, 1976}};
|
||||||
|
|
||||||
//乐谱1:小星星
|
//乐谱1:小星星
|
||||||
/*const uint16_t song_littlestar[42] = {0x1411,0x1411,0x1415,0x1415,0x1416,0x1416,0x1815,
|
/*const uint16_t song_littlestar[42] = {0x1411,0x1411,0x1415,0x1415,0x1416,0x1416,0x1815,
|
||||||
@@ -91,17 +91,17 @@ const uint16_t music_steps[3][7] = {{262,294,330,349,392,440,494},
|
|||||||
0x1221,0x0222,0x1422,0x8001,0x1215,0x1221,0x1117,0x0121,0x1821};*/
|
0x1221,0x0222,0x1422,0x8001,0x1215,0x1221,0x1117,0x0121,0x1821};*/
|
||||||
|
|
||||||
//乐谱4-2:机甲大师主题曲—你(舔狗之歌)
|
//乐谱4-2:机甲大师主题曲—你(舔狗之歌)
|
||||||
const uint16_t song_robomasterlickdog[3] = {0x1411,0x1412,0x0415};
|
const uint16_t song_robomasterlickdog[3] = {0x1411, 0x1412, 0x0415};
|
||||||
//DJI
|
//DJI
|
||||||
//const uint16_t song_starting[3] = {};const uint16_t song_robomasterlickdog[13] = {0x1411,0x1414,0x0415,0x1215,0x1213,0x1215,0x1113,0x0115,0x1215,0x0217,0x1217,0x0221,0x1421};//125
|
//const uint16_t song_starting[3] = {};const uint16_t song_robomasterlickdog[13] = {0x1411,0x1414,0x0415,0x1215,0x1213,0x1215,0x1113,0x0115,0x1215,0x0217,0x1217,0x0221,0x1421};//125
|
||||||
|
|
||||||
//提示音
|
//提示音
|
||||||
const uint8_t sound_warning[6] = {0x4B,0x40,0x4B,0x40,0x4B,0x40}; //B__B__B__
|
const uint8_t sound_warning[6] = {0x4B, 0x40, 0x4B, 0x40, 0x4B, 0x40}; //B__B__B__
|
||||||
const uint8_t sound_error[6] = {0x2B,0x10,0x2B,0x10,0x2B,0x10}; //B_B_B_
|
const uint8_t sound_error[6] = {0x2B, 0x10, 0x2B, 0x10, 0x2B, 0x10}; //B_B_B_
|
||||||
|
|
||||||
|
|
||||||
const uint8_t sound_gyrocalibrating[6] = {0x2D,0x40,0x2D,0x40,0x2D,0x40}; //D__D__D__
|
const uint8_t sound_gyrocalibrating[6] = {0x2D, 0x40, 0x2D, 0x40, 0x2D, 0x40}; //D__D__D__
|
||||||
const uint8_t sound_autoaiming[6] = {0x2D,0x10,0x2D,0x10,0x2D,0x10}; //D_D_D_
|
const uint8_t sound_autoaiming[6] = {0x2D, 0x10, 0x2D, 0x10, 0x2D, 0x10}; //D_D_D_
|
||||||
|
|
||||||
//
|
//
|
||||||
static uint16_t bzply_n = 0; //乐谱播放位置计数
|
static uint16_t bzply_n = 0; //乐谱播放位置计数
|
||||||
@@ -133,7 +133,7 @@ void SetBuzzerFrequence(uint16_t freq)
|
|||||||
{
|
{
|
||||||
//buzzer --> tim12.channel2
|
//buzzer --> tim12.channel2
|
||||||
//分频后为1000000Hz
|
//分频后为1000000Hz
|
||||||
uint16_t period = 1000000/freq -1;
|
uint16_t period = 1000000 / freq - 1;
|
||||||
|
|
||||||
__HAL_TIM_SET_AUTORELOAD(&htim12, period);
|
__HAL_TIM_SET_AUTORELOAD(&htim12, period);
|
||||||
__HAL_TIM_SET_COMPARE(&htim12, TIM_CHANNEL_2, period/2);
|
__HAL_TIM_SET_COMPARE(&htim12, TIM_CHANNEL_2, period/2);
|
||||||
@@ -170,12 +170,13 @@ void buzzer_off(void)
|
|||||||
* @param note note
|
* @param note note
|
||||||
* @param volume 音量,范围[0.0\\~1.0],用百分比表示
|
* @param volume 音量,范围[0.0\\~1.0],用百分比表示
|
||||||
*/
|
*/
|
||||||
void buzzer_note(uint16_t note,float volume)
|
void buzzer_note(uint16_t note, float volume)
|
||||||
{
|
{
|
||||||
if(volume > 1.0f)
|
if (volume > 1.0f)
|
||||||
{
|
{
|
||||||
volume = 1.0f;
|
volume = 1.0f;
|
||||||
}else if(volume < 0.0f)
|
}
|
||||||
|
else if (volume < 0.0f)
|
||||||
{
|
{
|
||||||
volume = 0.0f;
|
volume = 0.0f;
|
||||||
}
|
}
|
||||||
@@ -186,7 +187,7 @@ void buzzer_note(uint16_t note,float volume)
|
|||||||
// 设置自动重装载寄存器(ARR),以控制PWM信号的频率
|
// 设置自动重装载寄存器(ARR),以控制PWM信号的频率
|
||||||
htim12.Instance->ARR = (1000000 / note - 1) * 1u;
|
htim12.Instance->ARR = (1000000 / note - 1) * 1u;
|
||||||
// 设置比较寄存器(CCR3),以控制PWM信号的占空比
|
// 设置比较寄存器(CCR3),以控制PWM信号的占空比
|
||||||
htim12.Instance->CCR3 = (8*10500 / note - 1) * volume * 1u;
|
htim12.Instance->CCR3 = (8 * 10500 / note - 1) * volume * 1u;
|
||||||
// 重新启用定时器
|
// 重新启用定时器
|
||||||
__HAL_TIM_ENABLE(&htim12);
|
__HAL_TIM_ENABLE(&htim12);
|
||||||
// 启动PWM信号
|
// 启动PWM信号
|
||||||
@@ -280,33 +281,33 @@ void SongLaoda(void)
|
|||||||
// delay_ticks(450);
|
// delay_ticks(450);
|
||||||
// buzzer_note(90,0.5);
|
// buzzer_note(90,0.5);
|
||||||
// delay_ticks(450);
|
// delay_ticks(450);
|
||||||
buzzer_note(100,0.5);//so
|
buzzer_note(100, 0.5); //so
|
||||||
HAL_Delay(200);
|
HAL_Delay(200);
|
||||||
buzzer_note(150,0.5);//re
|
buzzer_note(150, 0.5); //re
|
||||||
HAL_Delay(200);
|
HAL_Delay(200);
|
||||||
buzzer_note(135,0.5);//do
|
buzzer_note(135, 0.5); //do
|
||||||
HAL_Delay(200);
|
HAL_Delay(200);
|
||||||
buzzer_note(100,0.5);//so
|
buzzer_note(100, 0.5); //so
|
||||||
HAL_Delay(500);
|
HAL_Delay(500);
|
||||||
buzzer_note(135,0.5);//do
|
buzzer_note(135, 0.5); //do
|
||||||
HAL_Delay(170);
|
HAL_Delay(170);
|
||||||
buzzer_note(150,0.5);//re
|
buzzer_note(150, 0.5); //re
|
||||||
HAL_Delay(170);
|
HAL_Delay(170);
|
||||||
buzzer_note(165,0.5);//mi
|
buzzer_note(165, 0.5); //mi
|
||||||
HAL_Delay(170);
|
HAL_Delay(170);
|
||||||
buzzer_note(150,0.5);//re
|
buzzer_note(150, 0.5); //re
|
||||||
HAL_Delay(170);
|
HAL_Delay(170);
|
||||||
buzzer_note(135,0.5);//do
|
buzzer_note(135, 0.5); //do
|
||||||
HAL_Delay(170);
|
HAL_Delay(170);
|
||||||
buzzer_note(150,0.5);//re
|
buzzer_note(150, 0.5); //re
|
||||||
HAL_Delay(170);
|
HAL_Delay(170);
|
||||||
buzzer_note(100,0.5);//so
|
buzzer_note(100, 0.5); //so
|
||||||
HAL_Delay(215);
|
HAL_Delay(215);
|
||||||
buzzer_note(150,0.5);//re
|
buzzer_note(150, 0.5); //re
|
||||||
HAL_Delay(215);
|
HAL_Delay(215);
|
||||||
buzzer_note(135,0.5);//do
|
buzzer_note(135, 0.5); //do
|
||||||
HAL_Delay(215);
|
HAL_Delay(215);
|
||||||
buzzer_note(100,0.5);//so
|
buzzer_note(100, 0.5); //so
|
||||||
HAL_Delay(215);
|
HAL_Delay(215);
|
||||||
|
|
||||||
// buzzer_note(45,0.5);//do
|
// buzzer_note(45,0.5);//do
|
||||||
|
|||||||
Reference in New Issue
Block a user