1 Star 0 Fork 0

风中的排骨/cmsis-dap-f103c6t6-swd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.c.orig 17.00 KB
一键复制 编辑 原始数据 按行查看 历史
admin 提交于 2022-01-11 11:04 . 2022年01月11日 11:04:07
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
#include "stm32f10x.h"
#include "stm32f10x_conf.h"
//=====================================
//CMSIS-DAP for Bluepill board
//--------based on x893 source code
//--------2018-07-24 by RadioOperator
//--------2019-12-22 updated for speed-up
//=====================================
#include <stdio.h>
#include <RTL.h>
#include <rl_usb.h>
#include <stm32f10x.h>
#define __NO_USB_LIB_C
#include "usb_config.c"
#include "DAP_config.h"
#include "DAP.h"
#if defined ( BLUEPILL )
#if defined ( SWD_REMAP )
#warning "BLUEPILL board: using Remapped SWD/SWC port, TDO-PB7, nRESET-PB6, TDI-PB5"
#else
#warning "BLUEPILL board: using SWD/TMS-PB9, SWC/TCK-PB8, TDO-PB7, nRESET-PB6, TDI-PB5"
#endif
#endif
#if defined ( BOARD_STM32RF ) \
|| defined ( STLINK_V20 ) \
|| defined ( STLINK_V21 ) \
|| defined ( STLINK_V2A )
#warning "SWD mode only, JTAG mode disabled."
#else
#warning "SWD mode + JTAG mode"
#endif
void BoardInit(void);
uint8_t usbd_hid_process(void);
void Delayms(uint32_t delay);
extern void PIN_nRESET_OUT(uint8_t bit);
#if (USBD_CDC_ACM_ENABLE == 1)
extern int32_t USBD_CDC_ACM_PortInitialize(void);
extern void CDC_ACM_UART_to_USB(void);
#endif
uint8_t u8SysTick_Counter = 3;
uint8_t u8LedMode = 0;
#define LED_FLASH_ON() SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk //turn-on SysTick, LED in flashing mode.
#define LED_FLASH_OFF() SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk //turn-off SysTick
#if defined ( BLUEPILL ) //Bluepill Board
void LedConnectedOn(void) { LED_CONNECTED_PORT->BRR = LED_CONNECTED_MASK; } //Low active
void LedConnectedOff(void) { LED_CONNECTED_PORT->BSRR = LED_CONNECTED_MASK; }
void LedConnectedToggle(void) { LED_CONNECTED_PORT->ODR ^= LED_CONNECTED_MASK; }
void LedRunningOn(void) { LED_RUNNING_PORT->BRR = LED_RUNNING_MASK; } //Low active
void LedRunningOff(void) { LED_RUNNING_PORT->BSRR = LED_RUNNING_MASK; }
void LedRunningToggle(void) { LED_RUNNING_PORT->ODR ^= LED_RUNNING_MASK; }
#else //all other Board
void LedConnectedOn(void) { LED_CONNECTED_PORT->BSRR = LED_CONNECTED_MASK; } //High active
void LedConnectedOff(void) { LED_CONNECTED_PORT->BRR = LED_CONNECTED_MASK; }
void LedConnectedToggle(void) { LED_CONNECTED_PORT->ODR ^= LED_CONNECTED_MASK; }
void LedRunningOn(void) { LED_RUNNING_PORT->BSRR = LED_RUNNING_MASK; } //High active
void LedRunningOff(void) { LED_RUNNING_PORT->BRR = LED_RUNNING_MASK; }
void LedRunningToggle(void) { LED_RUNNING_PORT->ODR ^= LED_RUNNING_MASK; }
#endif //#if defined ( BLUEPILL )
const GPIO_InitTypeDef INIT_LED_CONNECTED = {
LED_CONNECTED_MASK,
GPIO_Speed_50MHz,
GPIO_Mode_Out_PP
};
const GPIO_InitTypeDef INIT_LED_RUNNING = {
LED_RUNNING_MASK,
GPIO_Speed_50MHz,
GPIO_Mode_Out_PP
};
void LEDS_SETUP (void)
{
#if defined LED_CONNECTED_RCC
RCC->APB2ENR |= LED_CONNECTED_RCC;
LED_CONNECTED_PORT->BRR = LED_CONNECTED_MASK;
GPIO_INIT(LED_CONNECTED_PORT, INIT_LED_CONNECTED);
#endif
#if defined LED_RUNNING_RCC
RCC->APB2ENR |= LED_RUNNING_RCC;
LED_RUNNING_PORT->BRR = LED_RUNNING_MASK;
GPIO_INIT(LED_RUNNING_PORT, INIT_LED_RUNNING);
#endif
LedRunningOff();
}
void LedConnectedOut(uint16_t bit)
{
LedRunningOff();
u8LedMode &= ~0x01;
LED_FLASH_ON();
}
void LedRunningOut(uint16_t bit)
{
LedConnectedOn();
u8LedMode |= 0x01;
if (bit & 1)
{
LED_FLASH_OFF();
LedRunningOn();
}
else
{
LED_FLASH_ON();
LedRunningOff();
}
}
void SysTick_Init(void)
{
SysTick_Config(1800000); // =200ms, 1800000 ticks
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //Freq = 72/8 = 9MHz
LED_FLASH_OFF(); //turn-off SysTick
}
void SysTick_Handler(void)
{
u8SysTick_Counter--;
//Connected LED
if (u8LedMode & 0x02) //Connected LED: 200ms on/off for CDC, fast
{
u8LedMode &= ~0x02;
if (u8SysTick_Counter & 0x01) {
LedConnectedOn();
}
else
LedConnectedOff();
}
else
{
if ((u8SysTick_Counter & 0x07) == 0) //Connected LED: 200ms on, 1400ms off, slower
LedConnectedOn();
else
LedConnectedOff();
}
//Running LED
if (u8LedMode & 0x01) //Running LED: 200ms on, 600ms off
{
if ((u8SysTick_Counter & 0x01) == 0)
LedRunningOn();
else
LedRunningOff();
}
}
CoreDescriptor_t * pCoreDescriptor;
const CoreDescriptor_t CoreDescriptor = {
&LedConnectedOut,
&LedRunningOut,
};
void UserAppInit(CoreDescriptor_t *core)
{
pCoreDescriptor = core;
DAP_Setup();
}
void UserAppAbort(void)
{
DAP_TransferAbort = 1;
}
UserAppDescriptor_t * pUserAppDescriptor = NULL;
UserAppDescriptor_t UserAppDescriptor = {
&UserAppInit,
&DAP_ProcessCommand,
&UserAppAbort
};
//=============================================================================
//==main=======================================================================
//=============================================================================
int main(void)
{
SystemCoreClockUpdate();
BoardInit();
SysTick_Init(); //for LED flash
LedConnectedOn();
LedRunningOn();
if (UserAppDescriptor.UserInit != NULL)
{
pUserAppDescriptor = &UserAppDescriptor;
pUserAppDescriptor->UserInit((CoreDescriptor_t *)&CoreDescriptor);
}
Delayms(500);
// USB Device Initialization and connect
usbd_init();
usbd_connect(__TRUE);
while (!usbd_configured()) // Wait for USB Device to configure
{
LedConnectedOff();
LedRunningOff();
Delayms(10);
}
LED_FLASH_ON();
Delayms(100); // Wait for 100ms
#if (USBD_CDC_ACM_ENABLE == 1)
USBD_CDC_ACM_PortInitialize(); //initial CDC UART port
#endif
//-------------------------------------
while (1) //Main loop
{
usbd_hid_process(); //DAP process
#if (USBD_CDC_ACM_ENABLE == 1)
CDC_ACM_UART_to_USB(); //CDC, UART to USB
#endif
}
}
//=======End of main===========================================================
extern uint32_t __Vectors;
void HardFault_Handler(void);
void NMI_Handler(void) __attribute((alias("HardFault_Handler")));
void MemManage_Handler(void) __attribute((alias("HardFault_Handler")));
void BusFault_Handler(void) __attribute((alias("HardFault_Handler")));
void UsageFault_Handler(void) __attribute((alias("HardFault_Handler")));
void SVC_Handler(void) __attribute((alias("HardFault_Handler")));
void DebugMon_Handler(void) __attribute((alias("HardFault_Handler")));
void PendSV_Handler(void) __attribute((alias("HardFault_Handler")));
void HardFault_Handler(void)
{
__disable_irq();
__set_MSP(__Vectors);
LEDS_SETUP();
{
register int count;
for (count = 0; count < 5; count++)
{
LedRunningOn();
Delayms(100);
LedRunningOff();
LedConnectedOn();
Delayms(100);
LedConnectedOff();
Delayms(1000);
}
}
NVIC_SystemReset();
}
/* Control USB connecting via SW */
#ifdef PIN_USB_CONNECT_PORT
const GPIO_InitTypeDef INIT_PIN_USB_CONNECT = {
PIN_USB_CONNECT_MASK,
GPIO_Speed_50MHz,
PIN_USB_MODE
};
#endif
void PORT_USB_CONNECT_SETUP(void)
{
#ifdef PIN_USB_CONNECT_PORT
RCC->APB2ENR |= PIN_USB_CONNECT_RCC;
PIN_USB_CONNECT_OFF();
GPIO_INIT(PIN_USB_CONNECT_PORT, INIT_PIN_USB_CONNECT);
#endif
}
#if ( DAP_SWD != 0 )
#if ( DAP_JTAG != 0 )
const GPIO_InitTypeDef INIT_SWD_TDI = {
PIN_TDI_MASK,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IN_FLOATING
};
#endif //#if ( DAP_JTAG != 0 )
const GPIO_InitTypeDef INIT_SWD_TDO = {
PIN_TDO_MASK,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IN_FLOATING
};
const GPIO_InitTypeDef INIT_SWD_SWCLK_SWDIO = {
PIN_SWCLK_TCK_MASK | PIN_SWDIO_TMS_MASK,
GPIO_Speed_50MHz,
GPIO_Mode_Out_PP
};
/** Setup SWD I/O pins: SWCLK, SWDIO, and nRESET.
Configures the DAP Hardware I/O pins for Serial Wire Debug (SWD) mode:
- SWCLK, SWDIO, nRESET to output mode and set to default high level.
- TDI, TDO, nTRST to HighZ mode (pins are unused in SWD mode).
*/
void PORT_SWD_SETUP()
{
PIN_SWCLK_TCK_PORT->BSRR = (PIN_SWCLK_TCK_MASK | PIN_SWDIO_TMS_MASK);
#if ( DAP_JTAG != 0 )
GPIO_INIT(PIN_TDI_PORT, INIT_SWD_TDI);
#endif
GPIO_INIT(PIN_TDO_PORT, INIT_SWD_TDO);
#if defined ( BLUEPILL ) && defined ( SWD_REMAP )
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_DISABLE;
#endif
GPIO_INIT(PIN_SWCLK_TCK_PORT, INIT_SWD_SWCLK_SWDIO);
PIN_nRESET_OUT(0U);
Delayms(100);
PIN_nRESET_OUT(1U);
}
#endif //#if ( DAP_SWD != 0 )
#if ( DAP_JTAG != 0 )
const GPIO_InitTypeDef INIT_JTAG_TDO = {
PIN_TDO_MASK,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IN_FLOATING
};
const GPIO_InitTypeDef INIT_JTAG_TCK_TMS = {
PIN_SWCLK_TCK_MASK | PIN_SWDIO_TMS_MASK,
GPIO_Speed_50MHz,
GPIO_Mode_Out_PP
};
const GPIO_InitTypeDef INIT_JTAG_TDI = {
PIN_TDI_MASK,
GPIO_Speed_50MHz,
GPIO_Mode_Out_PP
};
/** Setup JTAG I/O pins: TCK, TMS, TDI, TDO, nTRST, and nRESET.
Configures the DAP Hardware I/O pins for JTAG mode:
- TCK, TMS, TDI, nTRST, nRESET to output mode and set to high level.
- TDO to input mode.
*/
void PORT_JTAG_SETUP()
{
PIN_SWCLK_TCK_PORT->BSRR = PIN_SWCLK_TCK_MASK | PIN_SWDIO_TMS_MASK;
PIN_TDI_PORT ->BSRR = PIN_TDI_MASK;
GPIO_INIT(PIN_TDO_PORT, INIT_JTAG_TDO);
GPIO_INIT(PIN_TDI_PORT, INIT_JTAG_TDI);
#if defined ( BLUEPILL ) && defined ( SWD_REMAP )
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_DISABLE;
#endif
GPIO_INIT(PIN_SWCLK_TCK_PORT, INIT_JTAG_TCK_TMS);
PIN_nRESET_OUT(0U);
Delayms(100);
PIN_nRESET_OUT(1U);
}
const GPIO_InitTypeDef INIT_OFF_TDI = {
PIN_TDI_MASK,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IN_FLOATING
};
#endif //#if ( DAP_JTAG != 0 )
const GPIO_InitTypeDef INIT_OFF_TCK_TMS = {
PIN_SWCLK_TCK_MASK | PIN_SWDIO_TMS_MASK,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IN_FLOATING
};
const GPIO_InitTypeDef INIT_OFF_TDO = {
PIN_TDO_MASK,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IN_FLOATING
};
const GPIO_InitTypeDef INIT_OFF_nRESET = {
(PIN_nRESET_MASK),
(GPIOSpeed_TypeDef)0,
GPIO_Mode_IPU
};
/** Disable JTAG/SWD I/O Pins.
Disables the DAP Hardware I/O pins which configures:
- TCK/SWCLK, TMS/SWDIO, TDI, TDO, nTRST, nRESET to High-Z mode.
*/
void PORT_OFF()
{
#if defined ( BLUEPILL ) && defined ( SWD_REMAP )
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_DISABLE;
#endif
GPIO_INIT(PIN_SWCLK_TCK_PORT, INIT_OFF_TCK_TMS);
#if ( DAP_JTAG != 0 )
GPIO_INIT(PIN_TDI_PORT, INIT_OFF_TDI);
GPIO_INIT(PIN_TDO_PORT, INIT_OFF_TDO);
#endif
GPIO_INIT(PIN_nRESET_PORT, INIT_OFF_nRESET);
}
const GPIO_InitTypeDef INIT_PINS_A = {
( GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 |
GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
// GPIO_Pin_11 | GPIO_Pin_12 | // USB pins
// GPIO_Pin_13 | GPIO_Pin_14 | // SWD pins
GPIO_Pin_15
),
(GPIOSpeed_TypeDef)0,
GPIO_Mode_AIN
};
const GPIO_InitTypeDef INIT_PINS_B = {
GPIO_Pin_All,
(GPIOSpeed_TypeDef)0,
GPIO_Mode_AIN
};
const GPIO_InitTypeDef INIT_PINS_C = {
(GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15),
(GPIOSpeed_TypeDef)0,
GPIO_Mode_AIN
};
void BoardInit(void)
{
#if defined ( BLUEPILL ) && defined ( SWD_REMAP )
//release JTAG-SWD Pins for GPIO
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_DISABLE;
#endif
// Enable GPIO clock
RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN);
// Reset all GPIO pins, except USB/SWD port
GPIO_INIT(GPIOA, INIT_PINS_A);
GPIO_INIT(GPIOB, INIT_PINS_B);
GPIO_INIT(GPIOC, INIT_PINS_C);
LEDS_SETUP();
}
void USBD_Error_Event(void)
{
LedConnectedOn();
LedRunningOn();
usbd_connect(__FALSE);
usbd_reset_core();
HardFault_Handler();
}
//-----Soft reset + Hard reset-------------------------------------------------
#define PIN_SWCLK_SET PIN_SWCLK_TCK_SET
#define PIN_SWCLK_CLR PIN_SWCLK_TCK_CLR
#define RST_CLOCK_CYCLE() \
PIN_SWCLK_CLR(); \
PIN_DELAY(); \
PIN_SWCLK_SET(); \
PIN_DELAY()
#define RST_WRITE_BIT(bit) \
PIN_SWDIO_OUT(bit); \
PIN_SWCLK_CLR(); \
PIN_DELAY(); \
PIN_SWCLK_SET(); \
PIN_DELAY()
#define RST_READ_BIT(bit) \
PIN_SWCLK_CLR(); \
PIN_DELAY(); \
bit = PIN_SWDIO_IN(); \
PIN_SWCLK_SET(); \
PIN_DELAY()
#define PIN_DELAY() PIN_DELAY_SLOW(DAP_Data.clock_delay)
uint8_t RST_Transfer(uint32_t request, uint32_t data)
{
uint32_t ack; \
uint32_t bit; \
uint32_t val; \
uint32_t parity; \
uint32_t n; \
\
/* Packet Request */ \
parity = 0U; \
RST_WRITE_BIT(1U); /* Start Bit */ \
bit = request >> 0; \
RST_WRITE_BIT(bit); /* APnDP Bit */ \
parity += bit; \
bit = request >> 1; \
RST_WRITE_BIT(bit); /* RnW Bit */ \
parity += bit; \
bit = request >> 2; \
RST_WRITE_BIT(bit); /* A2 Bit */ \
parity += bit; \
bit = request >> 3; \
RST_WRITE_BIT(bit); /* A3 Bit */ \
parity += bit; \
RST_WRITE_BIT(parity); /* Parity Bit */ \
RST_WRITE_BIT(0U); /* Stop Bit */ \
RST_WRITE_BIT(1U); /* Park Bit */ \
\
/* Turnaround */ \
PIN_SWDIO_OUT_DISABLE(); \
for (n = DAP_Data.swd_conf.turnaround; n; n--) { \
RST_CLOCK_CYCLE(); \
} \
\
/* Acknowledge response */ \
RST_READ_BIT(bit); \
ack = bit << 0; \
RST_READ_BIT(bit); \
ack |= bit << 1; \
RST_READ_BIT(bit); \
ack |= bit << 2; \
\
/* Data transfer */ \
/* Turnaround */ \
for (n = DAP_Data.swd_conf.turnaround; n; n--) { \
RST_CLOCK_CYCLE(); \
} \
PIN_SWDIO_OUT_ENABLE(); \
/* Write data */ \
val = data; \
parity = 0U; \
for (n = 32U; n; n--) { \
RST_WRITE_BIT(val); /* Write WDATA[0:31] */ \
parity += val; \
val >>= 1; \
} \
RST_WRITE_BIT(parity); /* Write Parity Bit */ \
PIN_SWDIO_OUT_ENABLE(); \
PIN_SWDIO_OUT(1U); \
return ((uint8_t)ack); \
}
void vResetTarget(uint8_t bit)
{
uint32_t i;
//soft-reset for Cortex-M
RST_Transfer(0x00000CC5, 0xE000ED0C); //set AIRCR address
for (i=0; i<100; i++);
RST_Transfer(0x00000CDD, 0x05FA0007); //set RESET data
for (i=0; i<100; i++);
RST_Transfer(0x00000CC5, 0xE000ED0C); //repeat
for (i=0; i<100; i++);
RST_Transfer(0x00000CDD, 0x05FA0007);
if (bit & 1) PIN_nRESET_HIGH();
else PIN_nRESET_LOW();
}
//=============END=====================
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ribs_in_the_wind/cmsis-dap-f103c6t6-swd.git
git@gitee.com:ribs_in_the_wind/cmsis-dap-f103c6t6-swd.git
ribs_in_the_wind
cmsis-dap-f103c6t6-swd
cmsis-dap-f103c6t6-swd
master

搜索帮助