Skip to content

Commit

Permalink
feat: add pyocd
Browse files Browse the repository at this point in the history
  • Loading branch information
IOsetting committed Dec 7, 2022
1 parent fe2e5a1 commit d05535b
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 87 deletions.
2 changes: 1 addition & 1 deletion User/at32f421_clock.h → Libraries/bsp/inc/at32f421_clock.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern "C" {
#include "at32f421.h"

/* exported functions ------------------------------------------------------- */
void system_clock_config(void);
void sclk_120m_hext_config(void);

#ifdef __cplusplus
}
Expand Down
39 changes: 39 additions & 0 deletions Libraries/bsp/inc/at32f421_delay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef __AT32F421_DELAY_H
#define __AT32F421_DELAY_H

#ifdef __cplusplus
extern "C" {
#endif

/* includes ------------------------------------------------------------------*/
#include "at32f421.h"

/* exported functions ------------------------------------------------------- */
/**
* @brief initialize delay function
* @param none
* @retval none
*/
void delay_init(void);

/**
* @brief inserts a delay time.
* @param nus: specifies the delay time length, in microsecond.
* @retval none
*/
void delay_us(uint32_t nus);

/**
* @brief inserts a delay time.
* @param nms: specifies the delay time length, in milliseconds.
* @retval none
*/
void delay_ms(uint16_t nms);


#ifdef __cplusplus
}
#endif

#endif /* __AT32F421_DELAY_H */

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __DEBUG_H
#define __DEBUG_H
#ifndef __AT32F421_PRINTF_H
#define __AT32F421_PRINTF_H

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion User/at32f421_clock.c → Libraries/bsp/src/at32f421_clock.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @param none
* @retval none
*/
void system_clock_config(void)
void sclk_120m_hext_config(void)
{
/* config flash psr register */
flash_psr_set(FLASH_WAIT_CYCLE_3);
Expand Down
75 changes: 75 additions & 0 deletions Libraries/bsp/src/at32f421_delay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* includes ------------------------------------------------------------------*/
#include "at32f421_delay.h"

#define STEP_DELAY_MS 50

/* delay variable */
static __IO uint32_t fac_us;
static __IO uint32_t fac_ms;


/**
* @brief initialize delay function
* @param none
* @retval none
*/
void delay_init()
{
/* configure systick */
systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
fac_us = system_core_clock / (1000000U);
fac_ms = fac_us * (1000U);
}


/**
* @brief inserts a delay time.
* @param nus: specifies the delay time length, in microsecond.
* @retval none
*/
void delay_us(uint32_t nus)
{
uint32_t temp = 0;
SysTick->LOAD = (uint32_t)(nus * fac_us);
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
do
{
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (1 << 16)));

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}

/**
* @brief inserts a delay time.
* @param nms: specifies the delay time length, in milliseconds.
* @retval none
*/
void delay_ms(uint16_t nms)
{
uint32_t temp = 0;
while(nms)
{
if(nms > STEP_DELAY_MS)
{
SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
nms -= STEP_DELAY_MS;
}
else
{
SysTick->LOAD = (uint32_t)(nms * fac_ms);
nms = 0;
}
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
do
{
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (1 << 16)));

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "debug.h"
#include "at32f421_printf.h"


/* support printf function, usemicrolib is unnecessary */
Expand Down
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ BUILD_DIR = Build
ENABLE_PRINTF_FLOAT ?= n
# Build with FreeRTOS, y:yes, n:no
USE_FREERTOS ?= n
# Programmer, jlink
FLASH_PROGRM ?= jlink
# Programmer, jlink or pyocd
FLASH_PROGRM ?= pyocd


##### Toolchains #######
Expand All @@ -20,9 +20,12 @@ FLASH_PROGRM ?= jlink
#ARM_TOOCHAIN ?= /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin
ARM_TOOCHAIN ?= /opt/gcc-arm/arm-gnu-toolchain-12.2.mpacbti-bet1-x86_64-arm-none-eabi/bin

# path to JLinkExe (or should be specified in PATH)
# path to JLinkExe
JLINKEXE ?= /opt/SEGGER/JLink/JLinkExe
JLINK_DEVICE ?= AT32F421C8T7
# path to PyOCD
PYOCD_EXE ?= pyocd
PYOCD_DEVICE ?= _at32f421c8t7


##### Paths ############
Expand All @@ -33,10 +36,11 @@ LDSCRIPT = Libraries/cmsis/cm4/device_support/startup/gcc/linker/AT32F421x8_FLA
LIB_FLAGS = USE_STDPERIPH_DRIVER AT32F421C8T7

# C source folders
CDIRS := Libraries/cmsis/cm4/device_support \
CDIRS := User \
Libraries/cmsis/cm4/device_support \
Libraries/drivers/src \
Libraries/debug \
User
Libraries/bsp/src

# C source files (if there are any single ones)
CFILES :=

Expand All @@ -50,7 +54,7 @@ INCLUDES := User \
Libraries/cmsis/cm4/core_support \
Libraries/cmsis/cm4/device_support \
Libraries/drivers/inc \
Libraries/debug
Libraries/bsp/inc

ifeq ($(USE_FREERTOS),y)
CDIRS += Middlewares/FreeRTOS \
Expand Down
Binary file added Misc/ArteryTek.AT32F421_DFP.2.1.0.pack
Binary file not shown.
2 changes: 2 additions & 0 deletions Misc/pyocd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pack:
- Misc/ArteryTek.AT32F421_DFP.2.1.0.pack
74 changes: 2 additions & 72 deletions User/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include "at32f421_clock.h"
#include "at32f421_delay.h"

typedef enum
{
Expand All @@ -33,8 +34,6 @@ typedef enum
LED4 = 2
} led_type;

#define STEP_DELAY_MS 50

#define LED_NUM 3

#define LED2_PIN GPIO_PINS_6
Expand All @@ -49,80 +48,11 @@ typedef enum
#define LED4_GPIO GPIOC
#define LED4_GPIO_CRM_CLK CRM_GPIOC_PERIPH_CLOCK

/* delay variable */
static __IO uint32_t fac_us;
static __IO uint32_t fac_ms;

gpio_type *led_gpio_port[LED_NUM] = {LED2_GPIO, LED3_GPIO, LED4_GPIO};
uint16_t led_gpio_pin[LED_NUM] = {LED2_PIN, LED3_PIN, LED4_PIN};
crm_periph_clock_type led_gpio_crm_clk[LED_NUM] = {LED2_GPIO_CRM_CLK, LED3_GPIO_CRM_CLK, LED4_GPIO_CRM_CLK};

/**
* @brief initialize delay function
* @param none
* @retval none
*/
void delay_init()
{
/* configure systick */
systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
fac_us = system_core_clock / (1000000U);
fac_ms = fac_us * (1000U);
}


/**
* @brief inserts a delay time.
* @param nus: specifies the delay time length, in microsecond.
* @retval none
*/
void delay_us(uint32_t nus)
{
uint32_t temp = 0;
SysTick->LOAD = (uint32_t)(nus * fac_us);
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
do
{
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (1 << 16)));

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}

/**
* @brief inserts a delay time.
* @param nms: specifies the delay time length, in milliseconds.
* @retval none
*/
void delay_ms(uint16_t nms)
{
uint32_t temp = 0;
while(nms)
{
if(nms > STEP_DELAY_MS)
{
SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
nms -= STEP_DELAY_MS;
}
else
{
SysTick->LOAD = (uint32_t)(nms * fac_ms);
nms = 0;
}
SysTick->VAL = 0x00;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
do
{
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (1 << 16)));

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL = 0x00;
}
}

/**
* @brief configure led gpio
* @param led: specifies the led to be configured.
Expand Down Expand Up @@ -171,7 +101,7 @@ void at32_led_toggle(led_type led)
*/
int main(void)
{
system_clock_config();
sclk_120m_hext_config();

delay_init();

Expand Down
13 changes: 10 additions & 3 deletions rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ ARCH_FLAGS := -mthumb -mcpu=cortex-m4
# -g: system’s native format, -g0:off, -g/g1,-g2,-g3 -> more verbosely
# -ggdb: for gdb, -ggdb0:off, -ggdb/ggdb1,-ggdb2,-ggdb3 -> more verbosely
# -gdwarf: in DWARF format, -gdwarf-2,-gdwarf-3,-gdwarf-4,-gdwarf-5
DEBUG_FLAGS ?= -gdwarf-2
DEBUG_FLAGS ?= -gdwarf-3

# c flags
OPT ?= -O0
CSTD ?= -std=c99
TGT_CFLAGS += $(ARCH_FLAGS) $(DEBUG_FLAGS) $(OPT) $(CSTD) $(addprefix -D, $(LIB_FLAGS)) -Wall
TGT_CFLAGS += $(ARCH_FLAGS) $(DEBUG_FLAGS) $(OPT) $(CSTD) $(addprefix -D, $(LIB_FLAGS)) -Wall -ffunction-sections -fdata-sections

# asm flags
TGT_ASFLAGS += $(ARCH_FLAGS) $(DEBUG_FLAGS) $(OPT) -Wa,--warn
Expand All @@ -62,7 +62,7 @@ TGT_INCFLAGS := $(addprefix -I $(TOP)/, $(INCLUDES))

.PHONY: all clean flash echo

all: $(BDIR)/$(PROJECT).elf $(BDIR)/$(PROJECT).bin
all: $(BDIR)/$(PROJECT).elf $(BDIR)/$(PROJECT).bin $(BDIR)/$(PROJECT).hex

# for debug
echo:
Expand Down Expand Up @@ -94,12 +94,19 @@ $(BDIR)/$(PROJECT).elf: $(OBJS) $(TOP)/$(LDSCRIPT)
@printf " OBJCP\t$@\n"
$(Q)$(OBJCOPY) -O binary $< $@

%.hex: %.elf
@printf " OBJCP\t$@\n"
$(Q)$(OBJCOPY) -O ihex $< $@

clean:
rm -rf $(BDIR)/*

flash:
ifeq ($(FLASH_PROGRM),jlink)
$(JLINKEXE) -device $(JLINK_DEVICE) -if swd -speed 4000 -CommanderScript $(TOP)/Misc/flash.jlink
else ifeq ($(FLASH_PROGRM),pyocd)
$(PYOCD_EXE) erase -c -t $(PYOCD_DEVICE) --config $(TOP)/Misc/pyocd.yaml
$(PYOCD_EXE) load $(BDIR)/$(PROJECT).hex -t $(PYOCD_DEVICE) --config $(TOP)/Misc/pyocd.yaml
else
@echo "FLASH_PROGRM is invalid\n"
endif

0 comments on commit d05535b

Please sign in to comment.