feat: 完成 can 初测

This commit is contained in:
2024-04-13 22:52:03 +08:00
parent d90e5d0e61
commit 2200c58e43
7 changed files with 199 additions and 135 deletions

View File

@@ -7,8 +7,8 @@ Language: Cpp
################################### ###################################
UseTab: Never UseTab: Never
IndentWidth: 4 IndentWidth: 2
TabWidth: 4 TabWidth: 2
ColumnLimit: 0 ColumnLimit: 0
AccessModifierOffset: -4 AccessModifierOffset: -4
NamespaceIndentation: All NamespaceIndentation: All

View File

@@ -94,6 +94,11 @@
<ModeSub name="Channel1 mode" value="Output_CH1"/> <ModeSub name="Channel1 mode" value="Output_CH1"/>
<ModeSub name="Activated" value="TRUE"/> <ModeSub name="Activated" value="TRUE"/>
</Mode> </Mode>
<Parameters>
<ParametersSub name="DividerValue" value="99"/>
<ParametersSub name="Period" value="2399"/>
<ParametersSub name="OCMode_1" value="TMR_OUTPUT_CONTROL_PWM_MODE_A"/>
</Parameters>
</TMR11> </TMR11>
<TMR12> <TMR12>
<Mode> <Mode>
@@ -124,7 +129,7 @@
<CRM_IRQHandler>0;0;0</CRM_IRQHandler> <CRM_IRQHandler>0;0;0</CRM_IRQHandler>
<ADC1_2_IRQHandler>0;0;0</ADC1_2_IRQHandler> <ADC1_2_IRQHandler>0;0;0</ADC1_2_IRQHandler>
<USBFS_H_CAN1_TX_IRQHandler>0;0;0</USBFS_H_CAN1_TX_IRQHandler> <USBFS_H_CAN1_TX_IRQHandler>0;0;0</USBFS_H_CAN1_TX_IRQHandler>
<USBFS_L_CAN1_RX0_IRQHandler>0;0;0</USBFS_L_CAN1_RX0_IRQHandler> <USBFS_L_CAN1_RX0_IRQHandler>1;0;0</USBFS_L_CAN1_RX0_IRQHandler>
<CAN1_RX1_IRQHandler>0;0;0</CAN1_RX1_IRQHandler> <CAN1_RX1_IRQHandler>0;0;0</CAN1_RX1_IRQHandler>
<CAN1_SE_IRQHandler>0;0;0</CAN1_SE_IRQHandler> <CAN1_SE_IRQHandler>0;0;0</CAN1_SE_IRQHandler>
<TMR1_TRG_HALL_TMR11_IRQHandler>0;0;0</TMR1_TRG_HALL_TMR11_IRQHandler> <TMR1_TRG_HALL_TMR11_IRQHandler>0;0;0</TMR1_TRG_HALL_TMR11_IRQHandler>
@@ -141,7 +146,7 @@
<TMR8_CH_IRQHandler>0;0;0</TMR8_CH_IRQHandler> <TMR8_CH_IRQHandler>0;0;0</TMR8_CH_IRQHandler>
<TMR6_GLOBAL_IRQHandler>1;0;0</TMR6_GLOBAL_IRQHandler> <TMR6_GLOBAL_IRQHandler>1;0;0</TMR6_GLOBAL_IRQHandler>
<CAN2_TX_IRQHandler>0;0;0</CAN2_TX_IRQHandler> <CAN2_TX_IRQHandler>0;0;0</CAN2_TX_IRQHandler>
<CAN2_RX0_IRQHandler>0;0;0</CAN2_RX0_IRQHandler> <CAN2_RX0_IRQHandler>1;0;0</CAN2_RX0_IRQHandler>
<CAN2_RX1_IRQHandler>0;0;0</CAN2_RX1_IRQHandler> <CAN2_RX1_IRQHandler>0;0;0</CAN2_RX1_IRQHandler>
<CAN2_SE_IRQHandler>0;0;0</CAN2_SE_IRQHandler> <CAN2_SE_IRQHandler>0;0;0</CAN2_SE_IRQHandler>
</NVIC> </NVIC>

View File

@@ -33,18 +33,5 @@
} }
}, },
"extensions": { "extensions": {
"recommendations": [
"cl.eide",
"keroc.hex-fmt",
"xiaoyongdong.srecord",
"hars.cppsnippets",
"zixuanwang.linkerscript",
"redhat.vscode-yaml",
"IBM.output-colorizer",
"cschlosser.doxdocgen",
"ms-vscode.vscode-serial-monitor",
"dan-c-underwood.arm",
"marus25.cortex-debug"
]
} }
} }

View File

@@ -65,10 +65,12 @@ void SVC_Handler(void);
void DebugMon_Handler(void); void DebugMon_Handler(void);
void PendSV_Handler(void); void PendSV_Handler(void);
void USBFS_L_CAN1_RX0_IRQHandler(void);
void USART1_IRQHandler(void); void USART1_IRQHandler(void);
void USART2_IRQHandler(void); void USART2_IRQHandler(void);
void USART3_IRQHandler(void); void USART3_IRQHandler(void);
void TMR6_GLOBAL_IRQHandler(void); void TMR6_GLOBAL_IRQHandler(void);
void CAN2_RX0_IRQHandler(void);
/* add user code begin exported functions */ /* add user code begin exported functions */
/* add user code end exported functions */ /* add user code end exported functions */

View File

@@ -74,13 +74,13 @@
*/ */
void NMI_Handler(void) void NMI_Handler(void)
{ {
/* add user code begin NonMaskableInt_IRQ 0 */ /* add user code begin NonMaskableInt_IRQ 0 */
/* add user code end NonMaskableInt_IRQ 0 */ /* add user code end NonMaskableInt_IRQ 0 */
/* add user code begin NonMaskableInt_IRQ 1 */ /* add user code begin NonMaskableInt_IRQ 1 */
/* add user code end NonMaskableInt_IRQ 1 */ /* add user code end NonMaskableInt_IRQ 1 */
} }
/** /**
@@ -90,15 +90,15 @@ void NMI_Handler(void)
*/ */
void HardFault_Handler(void) void HardFault_Handler(void)
{ {
/* add user code begin HardFault_IRQ 0 */ /* add user code begin HardFault_IRQ 0 */
/* add user code end HardFault_IRQ 0 */ /* add user code end HardFault_IRQ 0 */
/* go to infinite loop when hard fault exception occurs */ /* go to infinite loop when hard fault exception occurs */
while (1) { while (1) {
/* add user code begin W1_HardFault_IRQ 0 */ /* add user code begin W1_HardFault_IRQ 0 */
/* add user code end W1_HardFault_IRQ 0 */ /* add user code end W1_HardFault_IRQ 0 */
} }
} }
/** /**
@@ -108,15 +108,15 @@ void HardFault_Handler(void)
*/ */
void MemManage_Handler(void) void MemManage_Handler(void)
{ {
/* add user code begin MemoryManagement_IRQ 0 */ /* add user code begin MemoryManagement_IRQ 0 */
/* add user code end MemoryManagement_IRQ 0 */ /* add user code end MemoryManagement_IRQ 0 */
/* go to infinite loop when memory manage exception occurs */ /* go to infinite loop when memory manage exception occurs */
while (1) { while (1) {
/* add user code begin W1_MemoryManagement_IRQ 0 */ /* add user code begin W1_MemoryManagement_IRQ 0 */
/* add user code end W1_MemoryManagement_IRQ 0 */ /* add user code end W1_MemoryManagement_IRQ 0 */
} }
} }
/** /**
@@ -126,15 +126,15 @@ void MemManage_Handler(void)
*/ */
void BusFault_Handler(void) void BusFault_Handler(void)
{ {
/* add user code begin BusFault_IRQ 0 */ /* add user code begin BusFault_IRQ 0 */
/* add user code end BusFault_IRQ 0 */ /* add user code end BusFault_IRQ 0 */
/* go to infinite loop when bus fault exception occurs */ /* go to infinite loop when bus fault exception occurs */
while (1) { while (1) {
/* add user code begin W1_BusFault_IRQ 0 */ /* add user code begin W1_BusFault_IRQ 0 */
/* add user code end W1_BusFault_IRQ 0 */ /* add user code end W1_BusFault_IRQ 0 */
} }
} }
/** /**
@@ -144,15 +144,15 @@ void BusFault_Handler(void)
*/ */
void UsageFault_Handler(void) void UsageFault_Handler(void)
{ {
/* add user code begin UsageFault_IRQ 0 */ /* add user code begin UsageFault_IRQ 0 */
/* add user code end UsageFault_IRQ 0 */ /* add user code end UsageFault_IRQ 0 */
/* go to infinite loop when usage fault exception occurs */ /* go to infinite loop when usage fault exception occurs */
while (1) { while (1) {
/* add user code begin W1_UsageFault_IRQ 0 */ /* add user code begin W1_UsageFault_IRQ 0 */
/* add user code end W1_UsageFault_IRQ 0 */ /* add user code end W1_UsageFault_IRQ 0 */
} }
} }
/** /**
@@ -162,12 +162,12 @@ void UsageFault_Handler(void)
*/ */
void SVC_Handler(void) void SVC_Handler(void)
{ {
/* add user code begin SVCall_IRQ 0 */ /* add user code begin SVCall_IRQ 0 */
/* add user code end SVCall_IRQ 0 */ /* add user code end SVCall_IRQ 0 */
/* add user code begin SVCall_IRQ 1 */ /* add user code begin SVCall_IRQ 1 */
/* add user code end SVCall_IRQ 1 */ /* add user code end SVCall_IRQ 1 */
} }
/** /**
@@ -177,12 +177,12 @@ void SVC_Handler(void)
*/ */
void DebugMon_Handler(void) void DebugMon_Handler(void)
{ {
/* add user code begin DebugMonitor_IRQ 0 */ /* add user code begin DebugMonitor_IRQ 0 */
/* add user code end DebugMonitor_IRQ 0 */ /* add user code end DebugMonitor_IRQ 0 */
/* add user code begin DebugMonitor_IRQ 1 */ /* add user code begin DebugMonitor_IRQ 1 */
/* add user code end DebugMonitor_IRQ 1 */ /* add user code end DebugMonitor_IRQ 1 */
} }
/** /**
@@ -192,12 +192,32 @@ void DebugMon_Handler(void)
*/ */
void PendSV_Handler(void) void PendSV_Handler(void)
{ {
/* add user code begin PendSV_IRQ 0 */ /* add user code begin PendSV_IRQ 0 */
/* add user code end PendSV_IRQ 0 */ /* add user code end PendSV_IRQ 0 */
/* add user code begin PendSV_IRQ 1 */ /* add user code begin PendSV_IRQ 1 */
/* add user code end PendSV_IRQ 1 */ /* add user code end PendSV_IRQ 1 */
}
/**
* @brief this function handles USB Low Priority or CAN1 RX0 handler.
* @param none
* @retval none
*/
void USBFS_L_CAN1_RX0_IRQHandler(void)
{
/* add user code begin USBFS_L_CAN1_RX0_IRQ 0 */
if (SET == can_flag_get(CAN1, CAN_RF0MN_FLAG)) {
can_rx_message_type rx_message_struct;
can_message_receive(CAN1, CAN_RX_FIFO0, &rx_message_struct);
lwprintf("CAN1 RECV: ID = 0x%X\r\n", rx_message_struct.standard_id);
can_flag_clear(CAN1, CAN_RF0MN_FLAG);
}
/* add user code end USBFS_L_CAN1_RX0_IRQ 0 */
/* add user code begin USBFS_L_CAN1_RX0_IRQ 1 */
/* add user code end USBFS_L_CAN1_RX0_IRQ 1 */
} }
/** /**
@@ -207,12 +227,12 @@ void PendSV_Handler(void)
*/ */
void USART1_IRQHandler(void) void USART1_IRQHandler(void)
{ {
/* add user code begin USART1_IRQ 0 */ /* add user code begin USART1_IRQ 0 */
/* add user code end USART1_IRQ 0 */ /* add user code end USART1_IRQ 0 */
/* add user code begin USART1_IRQ 1 */ /* add user code begin USART1_IRQ 1 */
/* add user code end USART1_IRQ 1 */ /* add user code end USART1_IRQ 1 */
} }
/** /**
@@ -222,15 +242,15 @@ void USART1_IRQHandler(void)
*/ */
void USART2_IRQHandler(void) void USART2_IRQHandler(void)
{ {
/* add user code begin USART2_IRQ 0 */ /* add user code begin USART2_IRQ 0 */
if (SET == usart_flag_get(USART2, USART_RDBF_FLAG)) { if (SET == usart_flag_get(USART2, USART_RDBF_FLAG)) {
usart_data_receive(USART2); usart_data_receive(USART2);
usart_flag_clear(USART2, USART_RDBF_FLAG); usart_flag_clear(USART2, USART_RDBF_FLAG);
} }
/* add user code end USART2_IRQ 0 */ /* add user code end USART2_IRQ 0 */
/* add user code begin USART2_IRQ 1 */ /* add user code begin USART2_IRQ 1 */
/* add user code end USART2_IRQ 1 */ /* add user code end USART2_IRQ 1 */
} }
/** /**
@@ -240,12 +260,12 @@ void USART2_IRQHandler(void)
*/ */
void USART3_IRQHandler(void) void USART3_IRQHandler(void)
{ {
/* add user code begin USART3_IRQ 0 */ /* add user code begin USART3_IRQ 0 */
/* add user code end USART3_IRQ 0 */ /* add user code end USART3_IRQ 0 */
/* add user code begin USART3_IRQ 1 */ /* add user code begin USART3_IRQ 1 */
/* add user code end USART3_IRQ 1 */ /* add user code end USART3_IRQ 1 */
} }
/** /**
@@ -255,15 +275,39 @@ void USART3_IRQHandler(void)
*/ */
void TMR6_GLOBAL_IRQHandler(void) void TMR6_GLOBAL_IRQHandler(void)
{ {
/* add user code begin TMR6_GLOBAL_IRQ 0 */ /* add user code begin TMR6_GLOBAL_IRQ 0 */
static uint64_t i = 0; if (SET == tmr_flag_get(TMR6, TMR_OVF_FLAG)) {
if (SET == tmr_flag_get(TMR6, TMR_OVF_FLAG)) { tmr_flag_clear(TMR6, TMR_OVF_FLAG);
tmr_flag_clear(TMR6, TMR_OVF_FLAG); }
} /* add user code end TMR6_GLOBAL_IRQ 0 */
/* add user code end TMR6_GLOBAL_IRQ 0 */ /* add user code begin TMR6_GLOBAL_IRQ 1 */
/* add user code begin TMR6_GLOBAL_IRQ 1 */
/* add user code end TMR6_GLOBAL_IRQ 1 */ /* add user code end TMR6_GLOBAL_IRQ 1 */
}
/**
* @brief this function handles CAN2 RX0 handler.
* @param none
* @retval none
*/
void CAN2_RX0_IRQHandler(void)
{
/* add user code begin CAN2_RX0_IRQ 0 */
if (SET == can_flag_get(CAN2, CAN_RF0MN_FLAG)) {
can_rx_message_type rx_message_struct;
can_message_receive(CAN2, CAN_RX_FIFO0, &rx_message_struct);
lwprintf("CAN2 RECV: ID = 0x%X\r\n", rx_message_struct.standard_id);
lwprintf("---------------------\r\n| ");
for (uint8_t i = 0; i < 8; i++) {
lwprintf("0x%02X |", rx_message_struct.data[i]);
}
lwprintf("\r\n---------------------\r\n");
can_flag_clear(CAN2, CAN_RF0MN_FLAG);
}
/* add user code end CAN2_RX0_IRQ 0 */
/* add user code begin CAN2_RX0_IRQ 1 */
/* add user code end CAN2_RX0_IRQ 1 */
} }
/* add user code begin 1 */ /* add user code begin 1 */

View File

@@ -220,10 +220,12 @@ void wk_nvic_config(void)
{ {
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(USBFS_L_CAN1_RX0_IRQn, 0, 0);
nvic_irq_enable(USART1_IRQn, 1, 0); nvic_irq_enable(USART1_IRQn, 1, 0);
nvic_irq_enable(USART2_IRQn, 1, 0); nvic_irq_enable(USART2_IRQn, 1, 0);
nvic_irq_enable(USART3_IRQn, 1, 0); nvic_irq_enable(USART3_IRQn, 1, 0);
nvic_irq_enable(TMR6_GLOBAL_IRQn, 0, 0); nvic_irq_enable(TMR6_GLOBAL_IRQn, 0, 0);
nvic_irq_enable(CAN2_RX0_IRQn, 0, 0);
} }
/** /**
@@ -741,13 +743,13 @@ void wk_tmr11_init(void)
gpio_init(GPIOB, &gpio_init_struct); gpio_init(GPIOB, &gpio_init_struct);
/* configure counter settings */ /* configure counter settings */
tmr_base_init(TMR11, 65535, 0); tmr_base_init(TMR11, 2399, 99);
tmr_cnt_dir_set(TMR11, TMR_COUNT_UP); tmr_cnt_dir_set(TMR11, TMR_COUNT_UP);
tmr_clock_source_div_set(TMR11, TMR_CLOCK_DIV1); tmr_clock_source_div_set(TMR11, TMR_CLOCK_DIV1);
tmr_period_buffer_enable(TMR11, FALSE); tmr_period_buffer_enable(TMR11, FALSE);
/* configure channel 1 output settings */ /* configure channel 1 output settings */
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_OFF; tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
tmr_output_struct.oc_output_state = TRUE; tmr_output_struct.oc_output_state = TRUE;
tmr_output_struct.occ_output_state = FALSE; tmr_output_struct.occ_output_state = FALSE;
tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH; tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
@@ -758,6 +760,8 @@ void wk_tmr11_init(void)
tmr_channel_value_set(TMR11, TMR_SELECT_CHANNEL_1, 0); tmr_channel_value_set(TMR11, TMR_SELECT_CHANNEL_1, 0);
tmr_output_channel_buffer_enable(TMR11, TMR_SELECT_CHANNEL_1, FALSE); tmr_output_channel_buffer_enable(TMR11, TMR_SELECT_CHANNEL_1, FALSE);
tmr_output_channel_immediately_set(TMR11, TMR_SELECT_CHANNEL_1, FALSE);
tmr_counter_enable(TMR11, TRUE); tmr_counter_enable(TMR11, TRUE);
/* add user code begin tmr11_init 2 */ /* add user code begin tmr11_init 2 */
@@ -910,8 +914,19 @@ void wk_can1_init(void)
can_filter_init(CAN1, &can_filter_init_struct); can_filter_init(CAN1, &can_filter_init_struct);
/* add user code begin can1_init 2 */ /**
* Users need to configure CAN1 interrupt functions according to the actual application.
* 1. Call the below function to enable the corresponding CAN1 interrupt.
* --can_interrupt_enable(...)
* 2. Add the user's interrupt handler code into the below function in the at32f403a_407_int.c file.
* --void USBFS_L_CAN1_RX0_IRQHandler(void)
*/
/*can1 rx0 interrupt config--------------------------------------------------------*/
// can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);
/* add user code begin can1_init 2 */
can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);
/* add user code end can1_init 2 */ /* add user code end can1_init 2 */
} }
@@ -988,8 +1003,19 @@ void wk_can2_init(void)
can_filter_init(CAN2, &can_filter_init_struct); can_filter_init(CAN2, &can_filter_init_struct);
/* add user code begin can2_init 2 */ /**
* Users need to configure CAN2 interrupt functions according to the actual application.
* 1. Call the below function to enable the corresponding CAN2 interrupt.
* --can_interrupt_enable(...)
* 2. Add the user's interrupt handler code into the below function in the at32f403a_407_int.c file.
* --void CAN2_RX0_IRQHandler(void)
*/
/*can2 rx0 interrupt config--------------------------------------------------------*/
// can_interrupt_enable(CAN2, CAN_RF0MIEN_INT, TRUE);
/* add user code begin can2_init 2 */
can_interrupt_enable(CAN2, CAN_RF0MIEN_INT, TRUE);
/* add user code end can2_init 2 */ /* add user code end can2_init 2 */
} }

View File

@@ -71,70 +71,70 @@
*/ */
int main(void) int main(void)
{ {
/* add user code begin 1 */ /* add user code begin 1 */
by_debug_init(); by_debug_init();
/* add user code end 1 */ /* add user code end 1 */
/* system clock config. */ /* system clock config. */
wk_system_clock_config(); wk_system_clock_config();
/* config periph clock. */ /* config periph clock. */
wk_periph_clock_config(); wk_periph_clock_config();
/* init debug function. */ /* init debug function. */
wk_debug_config(); wk_debug_config();
/* nvic config. */ /* nvic config. */
wk_nvic_config(); wk_nvic_config();
/* init usart1 function. */ /* init usart1 function. */
wk_usart1_init(); wk_usart1_init();
/* init usart2 function. */ /* init usart2 function. */
wk_usart2_init(); wk_usart2_init();
/* init usart3 function. */ /* init usart3 function. */
wk_usart3_init(); wk_usart3_init();
/* init i2c1 function. */ /* init i2c1 function. */
wk_i2c1_init(); wk_i2c1_init();
/* init i2c2 function. */ /* init i2c2 function. */
wk_i2c2_init(); wk_i2c2_init();
/* init adc1 function. */ /* init adc1 function. */
wk_adc1_init(); wk_adc1_init();
/* init tmr6 function. */ /* init tmr6 function. */
wk_tmr6_init(); wk_tmr6_init();
/* init tmr8 function. */ /* init tmr8 function. */
wk_tmr8_init(); wk_tmr8_init();
/* init tmr11 function. */ /* init tmr11 function. */
wk_tmr11_init(); wk_tmr11_init();
/* init tmr12 function. */ /* init tmr12 function. */
wk_tmr12_init(); wk_tmr12_init();
/* init can1 function. */ /* init can1 function. */
wk_can1_init(); wk_can1_init();
/* init can2 function. */ /* init can2 function. */
wk_can2_init(); wk_can2_init();
/* init gpio function. */ /* init gpio function. */
wk_gpio_config(); wk_gpio_config();
/* add user code begin 2 */ /* add user code begin 2 */
DWT_Init(); DWT_Init();
lwprintf("init done!\r\n"); lwprintf("init done!\r\n");
/* add user code end 2 */ /* add user code end 2 */
while (1) { while (1) {
/* add user code begin 3 */ /* add user code begin 3 */
// DWT_Delay(1000000); // DWT_Delay(1000000);
/* add user code end 3 */ /* add user code end 3 */
} }
} }