mirror of
https://github.com/ArteryTek/AT32F415_Firmware_Library.git
synced 2026-05-21 09:22:11 +00:00
upload version v2.0.0
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f415_clock.c
|
||||
* @version v2.0.0
|
||||
* @date 2021-11-26
|
||||
* @brief system clock config program
|
||||
**************************************************************************
|
||||
* 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.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
/* includes ------------------------------------------------------------------*/
|
||||
#include "at32f415_clock.h"
|
||||
|
||||
/**
|
||||
* @brief system clock config program
|
||||
* @note the system clock is configured as follow:
|
||||
* - system clock = hext / 2 * pll_mult
|
||||
* - system clock source = pll (hext)
|
||||
* - hext = 8000000
|
||||
* - sclk = 144000000
|
||||
* - ahbdiv = 1
|
||||
* - ahbclk = 144000000
|
||||
* - apb2div = 2
|
||||
* - apb2clk = 72000000
|
||||
* - apb1div = 2
|
||||
* - apb1clk = 72000000
|
||||
* - pll_mult = 36
|
||||
* - flash_wtcyc = 4 cycle
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void system_clock_config(void)
|
||||
{
|
||||
/* config flash psr register */
|
||||
flash_psr_set(FLASH_WAIT_CYCLE_4);
|
||||
|
||||
/* reset crm */
|
||||
crm_reset();
|
||||
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);
|
||||
|
||||
/* wait till hext is ready */
|
||||
while(crm_hext_stable_wait() == ERROR)
|
||||
{
|
||||
}
|
||||
|
||||
/* config pll clock resource */
|
||||
crm_pll_config(CRM_PLL_SOURCE_HEXT_DIV, CRM_PLL_MULT_36);
|
||||
|
||||
/* enable pll */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
|
||||
|
||||
/* wait till pll is ready */
|
||||
while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
|
||||
{
|
||||
}
|
||||
|
||||
/* config ahbclk */
|
||||
crm_ahb_div_set(CRM_AHB_DIV_1);
|
||||
|
||||
/* config apb2clk */
|
||||
crm_apb2_div_set(CRM_APB2_DIV_2);
|
||||
|
||||
/* config apb1clk */
|
||||
crm_apb1_div_set(CRM_APB1_DIV_2);
|
||||
|
||||
/* enable auto step mode */
|
||||
crm_auto_step_mode_enable(TRUE);
|
||||
|
||||
/* select pll as system clock source */
|
||||
crm_sysclk_switch(CRM_SCLK_PLL);
|
||||
|
||||
/* wait till pll is used as system clock source */
|
||||
while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
|
||||
{
|
||||
}
|
||||
|
||||
/* disable auto step mode */
|
||||
crm_auto_step_mode_enable(FALSE);
|
||||
|
||||
/* update system_core_clock global variable */
|
||||
system_core_clock_update();
|
||||
}
|
||||
142
utilities/at32f415_gen_random_number_demo/src/at32f415_int.c
Normal file
142
utilities/at32f415_gen_random_number_demo/src/at32f415_int.c
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f415_int.c
|
||||
* @version v2.0.0
|
||||
* @date 2021-11-26
|
||||
* @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.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
/* includes ------------------------------------------------------------------*/
|
||||
#include "at32f415_int.h"
|
||||
|
||||
/** @addtogroup UTILITIES_examples
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup GEN_random_number_demo
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief this function handles nmi exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles hard fault exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* go to infinite loop when hard fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles memory manage exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void MemManage_Handler(void)
|
||||
{
|
||||
/* go to infinite loop when memory manage exception occurs */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles bus fault exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
/* go to infinite loop when bus fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles usage fault exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void UsageFault_Handler(void)
|
||||
{
|
||||
/* go to infinite loop when usage fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles svcall exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles debug monitor exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void DebugMon_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles pendsv_handler exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles systick handler.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
140
utilities/at32f415_gen_random_number_demo/src/main.c
Normal file
140
utilities/at32f415_gen_random_number_demo/src/main.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file main.c
|
||||
* @version v2.0.0
|
||||
* @date 2021-11-26
|
||||
* @brief main program
|
||||
**************************************************************************
|
||||
* 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.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
#include "at32f415_board.h"
|
||||
#include "at32f415_clock.h"
|
||||
#include "stdio.h"
|
||||
#include "random.h"
|
||||
|
||||
/** @addtogroup UTILITIES_examples
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup GEN_random_number_demo
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* suport printf function, usemicrolib is unnecessary */
|
||||
#if (__ARMCC_VERSION > 6000000)
|
||||
__asm (".global __use_no_semihosting\n\t");
|
||||
void _sys_exit(int x)
|
||||
{
|
||||
x = x;
|
||||
}
|
||||
/* __use_no_semihosting was requested, but _ttywrch was */
|
||||
void _ttywrch(int ch)
|
||||
{
|
||||
ch = ch;
|
||||
}
|
||||
FILE __stdout;
|
||||
#else
|
||||
#ifdef __CC_ARM
|
||||
#pragma import(__use_no_semihosting)
|
||||
struct __FILE
|
||||
{
|
||||
int handle;
|
||||
};
|
||||
FILE __stdout;
|
||||
void _sys_exit(int x)
|
||||
{
|
||||
x = x;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined ( __GNUC__ ) && !defined (__clang__)
|
||||
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
|
||||
#else
|
||||
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief retargets the c library printf function to the usart.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
PUTCHAR_PROTOTYPE
|
||||
{
|
||||
while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
|
||||
usart_data_transmit(USART1, ch);
|
||||
return ch;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief initialize uart1
|
||||
* @param bound: uart baudrate
|
||||
* @retval none
|
||||
*/
|
||||
void uart_print_init(uint32_t bound)
|
||||
{
|
||||
gpio_init_type gpio_init_struct;
|
||||
|
||||
/*enable the uart1 and gpio clock*/
|
||||
crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
|
||||
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* configure the uart1 tx pin */
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_9;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOA, &gpio_init_struct);
|
||||
|
||||
/*configure uart param*/
|
||||
usart_init(USART1, bound, USART_DATA_8BITS, USART_STOP_1_BIT);
|
||||
usart_transmitter_enable(USART1, TRUE);
|
||||
usart_enable(USART1, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief main function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
system_clock_config();
|
||||
|
||||
at32_board_init();
|
||||
uart_print_init(115200);
|
||||
printf("gen random number test\r\n");
|
||||
randnum_test();
|
||||
while(1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
171
utilities/at32f415_gen_random_number_demo/src/random.c
Normal file
171
utilities/at32f415_gen_random_number_demo/src/random.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file random.c
|
||||
* @version v2.0.0
|
||||
* @date 2021-11-26
|
||||
* @brief set of firmware functions to random function
|
||||
**************************************************************************
|
||||
* 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.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
#include "at32f415_board.h"
|
||||
#include "random.h"
|
||||
#include "stdio.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/** @addtogroup UTILITIES_examples
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup GEN_random_number_demo
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* define at32 mcu uid address */
|
||||
#define DEVICE_ID_ADDR1 0x1FFFF7E8
|
||||
ertc_time_type *time;
|
||||
|
||||
uint16_t ertc_clk_div_a = 0;
|
||||
uint16_t ertc_clk_div_b = 0;
|
||||
|
||||
/**
|
||||
* @brief get uid value as seed
|
||||
* @param none
|
||||
* @retval uid_one_word
|
||||
*/
|
||||
int get_uid_for_seed (void)
|
||||
{
|
||||
uint32_t id[3] = {0};
|
||||
uint32_t uid_one_word;
|
||||
|
||||
/* get uid */
|
||||
id[0] = *(int*)DEVICE_ID_ADDR1;
|
||||
id[2] = *(int*)(DEVICE_ID_ADDR1+8);
|
||||
uid_one_word=id[0]&0x7fffffff+((id[2]&(~0x7fffffff))<<16);
|
||||
return uid_one_word;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief enable ertc as seed
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
#if ENABLE_ERTC_ASSEED
|
||||
void ertc_init_for_seed (void)
|
||||
{
|
||||
/* enable the pwc clock interface */
|
||||
crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* allow access to ertc */
|
||||
pwc_battery_powered_domain_access(TRUE);
|
||||
|
||||
/* reset ertc domain */
|
||||
crm_battery_powered_domain_reset(TRUE);
|
||||
crm_battery_powered_domain_reset(FALSE);
|
||||
|
||||
|
||||
/* the ertc clock may varies due to lick frequency dispersion. */
|
||||
/* enable the lick osc */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
|
||||
|
||||
/* wait till lick is ready */
|
||||
while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET)
|
||||
{
|
||||
}
|
||||
|
||||
/* select the ertc clock source */
|
||||
crm_ertc_clock_select(CRM_ERTC_CLOCK_LICK);
|
||||
|
||||
/* ck_spre(1hz) = ertcclk(lick) /(ertc_clk_div_a + 1)*(ertc_clk_div_b + 1)*/
|
||||
ertc_clk_div_b = 0xFF;
|
||||
ertc_clk_div_a = 0x7F;
|
||||
|
||||
|
||||
/* enable the ertc clock */
|
||||
crm_ertc_clock_enable(TRUE);
|
||||
|
||||
/* deinitializes the ertc registers */
|
||||
ertc_reset();
|
||||
|
||||
/* wait for ertc apb registers synchronisation */
|
||||
ertc_wait_update();
|
||||
|
||||
/* configure the ertc data register and ertc prescaler */
|
||||
ertc_divider_set(ertc_clk_div_a, ertc_clk_div_b);
|
||||
ertc_hour_mode_set(ERTC_HOUR_MODE_24);
|
||||
|
||||
/* set the alarm 05h:20min:30s */
|
||||
ertc_alarm_mask_set(ERTC_ALA, ERTC_ALARM_MASK_DATE_WEEK);
|
||||
ertc_alarm_week_date_select(ERTC_ALA, ERTC_SLECT_DATE);
|
||||
ertc_alarm_set(ERTC_ALA, 31, 5, 20, 30, ERTC_AM);
|
||||
|
||||
/* enable ertc alarm a interrupt */
|
||||
ertc_interrupt_enable(ERTC_ALA_INT, TRUE);
|
||||
|
||||
/* enable the alarm */
|
||||
ertc_alarm_enable(ERTC_ALA, TRUE);
|
||||
|
||||
ertc_flag_clear(ERTC_ALAF_FLAG);
|
||||
|
||||
/* set the date: friday january 11th 2013 */
|
||||
ertc_date_set(13, 1, 11, 5);
|
||||
|
||||
/* set the time to 05h 20mn 00s am */
|
||||
ertc_time_set(5, 20, 0, ERTC_AM);
|
||||
|
||||
/* indicator for the ertc configuration */
|
||||
ertc_bpr_data_write(ERTC_DT1, 0x32F1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief random number test
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void randnum_test( void)
|
||||
{
|
||||
#if ENABLE_ERTC_ASSEED
|
||||
|
||||
if(ertc_bpr_data_read(ERTC_DT1) != 0x32F1)
|
||||
{
|
||||
ertc_init_for_seed();
|
||||
}
|
||||
ertc_calendar_get(time);
|
||||
/* set uid and ertc as seed for random */
|
||||
srand(ERTC->time + get_uid_for_seed());
|
||||
#else
|
||||
/* set only uid as seed for random */
|
||||
srand(get_uid_for_seed());
|
||||
#endif
|
||||
while(1)
|
||||
{
|
||||
delay_ms(500);
|
||||
printf("%d\r\n",rand());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
Reference in New Issue
Block a user