feat: 移植 lwprintf

feat: 创建 can 发送接口
feat: 添加编码器读数接口
This commit is contained in:
2024-04-19 17:16:24 +08:00
parent f0cbc9f1d3
commit 8408ab1cf3
21 changed files with 2522 additions and 94 deletions

31
project/inc/by_debug.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef _BY_DEBUG_H__
#define _BY_DEBUG_H__
#include <stdio.h>
#include "at32f413.h"
#include "lwprintf.h"
#define BY_DEBUG_USART_INDEX (USART2)
#define BY_DEBUG_LOG_MODE (2) // 0-not log, 1-no debug log, 2-all
#if (BY_DEBUG_LOG_MODE == 2)
#define LOGI(format, ...) lwprintf("[INFO] " format "\r\n", ##__VA_ARGS__)
#define LOGW(format, ...) lwprintf("[WARN] " format "\r\n", ##__VA_ARGS__)
#define LOGE(format, ...) lwprintf("[ERR] " format "\r\n", ##__VA_ARGS__)
#define LOGD(format, ...) lwprintf("[DEBUG] " format "\r\n", ##__VA_ARGS__)
#elif (BY_DEBUG_LOG_MODE == 1)
#define LOGI(format, ...) lwprintf("[INFO] " format "\r\n", ##__VA_ARGS__)
#define LOGW(format, ...) lwprintf("[WARN] " format "\r\n", ##__VA_ARGS__)
#define LOGE(format, ...) lwprintf("[ERR] " format "\r\n", ##__VA_ARGS__)
#define LOGD(format, ...)
#elif (BY_DEBUG_LOG_MODE == 0)
#define LOGI(format, ...)
#define LOGW(format, ...)
#define LOGE(format, ...)
#define LOGD(format, ...)
#endif
extern void by_debug_init(void);
#endif

View File

@@ -1,27 +1,27 @@
/* add user code begin Header */
/**
**************************************************************************
* @file at32f413_int.c
* @brief main interrupt service routines.
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
**************************************************************************
* @file at32f413_int.c
* @brief main interrupt service routines.
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
/* add user code end Header */
/* includes ------------------------------------------------------------------*/
@@ -29,7 +29,7 @@
/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "by_motion.h"
/* add user code end private includes */
/* private typedef -----------------------------------------------------------*/
@@ -68,10 +68,10 @@
/* add user code end external variables */
/**
* @brief this function handles nmi exception.
* @param none
* @retval none
*/
* @brief this function handles nmi exception.
* @param none
* @retval none
*/
void NMI_Handler(void)
{
/* add user code begin NonMaskableInt_IRQ 0 */
@@ -84,18 +84,17 @@ void NMI_Handler(void)
}
/**
* @brief this function handles hard fault exception.
* @param none
* @retval none
*/
* @brief this function handles hard fault exception.
* @param none
* @retval none
*/
void HardFault_Handler(void)
{
/* add user code begin HardFault_IRQ 0 */
/* add user code end HardFault_IRQ 0 */
/* go to infinite loop when hard fault exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_HardFault_IRQ 0 */
/* add user code end W1_HardFault_IRQ 0 */
@@ -103,18 +102,17 @@ void HardFault_Handler(void)
}
/**
* @brief this function handles memory manage exception.
* @param none
* @retval none
*/
* @brief this function handles memory manage exception.
* @param none
* @retval none
*/
void MemManage_Handler(void)
{
/* add user code begin MemoryManagement_IRQ 0 */
/* add user code end MemoryManagement_IRQ 0 */
/* go to infinite loop when memory manage exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_MemoryManagement_IRQ 0 */
/* add user code end W1_MemoryManagement_IRQ 0 */
@@ -122,18 +120,17 @@ void MemManage_Handler(void)
}
/**
* @brief this function handles bus fault exception.
* @param none
* @retval none
*/
* @brief this function handles bus fault exception.
* @param none
* @retval none
*/
void BusFault_Handler(void)
{
/* add user code begin BusFault_IRQ 0 */
/* add user code end BusFault_IRQ 0 */
/* go to infinite loop when bus fault exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_BusFault_IRQ 0 */
/* add user code end W1_BusFault_IRQ 0 */
@@ -141,18 +138,17 @@ void BusFault_Handler(void)
}
/**
* @brief this function handles usage fault exception.
* @param none
* @retval none
*/
* @brief this function handles usage fault exception.
* @param none
* @retval none
*/
void UsageFault_Handler(void)
{
/* add user code begin UsageFault_IRQ 0 */
/* add user code end UsageFault_IRQ 0 */
/* go to infinite loop when usage fault exception occurs */
while (1)
{
while (1) {
/* add user code begin W1_UsageFault_IRQ 0 */
/* add user code end W1_UsageFault_IRQ 0 */
@@ -160,10 +156,10 @@ void UsageFault_Handler(void)
}
/**
* @brief this function handles svcall exception.
* @param none
* @retval none
*/
* @brief this function handles svcall exception.
* @param none
* @retval none
*/
void SVC_Handler(void)
{
/* add user code begin SVCall_IRQ 0 */
@@ -175,10 +171,10 @@ void SVC_Handler(void)
}
/**
* @brief this function handles debug monitor exception.
* @param none
* @retval none
*/
* @brief this function handles debug monitor exception.
* @param none
* @retval none
*/
void DebugMon_Handler(void)
{
/* add user code begin DebugMonitor_IRQ 0 */
@@ -190,10 +186,10 @@ void DebugMon_Handler(void)
}
/**
* @brief this function handles pendsv_handler exception.
* @param none
* @retval none
*/
* @brief this function handles pendsv_handler exception.
* @param none
* @retval none
*/
void PendSV_Handler(void)
{
/* add user code begin PendSV_IRQ 0 */
@@ -205,14 +201,18 @@ void PendSV_Handler(void)
}
/**
* @brief this function handles TMR4 handler.
* @param none
* @retval none
*/
* @brief this function handles TMR4 handler.
* @param none
* @retval none
*/
void TMR4_GLOBAL_IRQHandler(void)
{
/* add user code begin TMR4_GLOBAL_IRQ 0 */
if (SET == tmr_interrupt_flag_get(TMR4, TMR_OVF_FLAG)) {
by_motion_get_speed_m1();
by_motion_get_speed_m2();
tmr_flag_clear(TMR4, TMR_OVF_FLAG);
}
/* add user code end TMR4_GLOBAL_IRQ 0 */
/* add user code begin TMR4_GLOBAL_IRQ 1 */

View File

@@ -282,6 +282,7 @@ void wk_usart2_init(void)
/* configure param */
usart_init(USART2, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_transmitter_enable(USART2, TRUE);
usart_receiver_enable(USART2, TRUE);
usart_parity_selection_config(USART2, USART_PARITY_NONE);
@@ -348,7 +349,7 @@ void wk_tmr1_init(void)
gpio_init(GPIOA, &gpio_init_struct);
/* configure counter settings */
tmr_base_init(TMR1, 999, 499);
tmr_base_init(TMR1, 999, 19);
tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);
tmr_clock_source_div_set(TMR1, TMR_CLOCK_DIV1);
tmr_repetition_counter_set(TMR1, 0);
@@ -562,7 +563,7 @@ void wk_tmr4_init(void)
/* add user code end tmr4_init 1 */
/* configure counter settings */
tmr_base_init(TMR4, 199, 999);
tmr_base_init(TMR4, 1999, 999);
tmr_cnt_dir_set(TMR4, TMR_COUNT_UP);
tmr_clock_source_div_set(TMR4, TMR_CLOCK_DIV1);
tmr_period_buffer_enable(TMR4, FALSE);
@@ -582,7 +583,7 @@ void wk_tmr4_init(void)
*/
/* add user code begin tmr4_init 2 */
tmr_interrupt_enable(TMR4, TMR_OVF_INT, TRUE);
/* add user code end tmr4_init 2 */
}

23
project/src/by_debug.c Normal file
View File

@@ -0,0 +1,23 @@
#include "by_debug.h"
#include "at32f413.h"
#include "lwprintf.h"
int lwprintf_out(int ch, lwprintf_t *lwp)
{
/* May use printf to output it for test */
if (ch != '\0') {
while (usart_flag_get(BY_DEBUG_USART_INDEX, USART_TDC_FLAG) == RESET)
;
usart_data_transmit(BY_DEBUG_USART_INDEX, (char)ch);
}
return ch;
}
void by_debug_init(void)
{
lwprintf_init(lwprintf_out);
}

View File

@@ -31,6 +31,8 @@
/* add user code begin private includes */
#include "dwt_delay.h"
#include "by_debug.h"
#include "by_can.h"
#include "by_motion.h"
/* add user code end private includes */
@@ -72,7 +74,7 @@
int main(void)
{
/* add user code begin 1 */
by_debug_init();
/* add user code end 1 */
/* system clock config. */
@@ -110,8 +112,12 @@ int main(void)
/* add user code begin 2 */
DWT_Init();
LOGD("dwt init done");
by_motion_init();
LOGD("motion init done");
LOGI("init done");
/* add user code end 2 */
while(1)