I use GD32VF103C_START kit from GigaDevice and try LED blinking sample project. I found this project in the Internet and it compiles just fine (I've only changed from 500ms to 1000ms=1s).
main.c:
#include "gd32vf103.h"
#include "gd32vf103c_start.h"
#include "systick.h"
int main(void)
{
/* enable the LED clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* configure LED GPIO port */
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
gpio_bit_reset(GPIOA, GPIO_PIN_7);
while(1){
/* insert 1s delay */
delay_1ms(1000);
/* toggle the LED */
gpio_bit_write(GPIOA, GPIO_PIN_7, (bit_status)(1-gpio_input_bit_get(GPIOA, GPIO_PIN_7)));
/* insert 1s delay */
delay_1ms(1000);
gpio_bit_write(GPIOA, GPIO_PIN_7, (bit_status)(1-gpio_input_bit_get(GPIOA, GPIO_PIN_7)));
}
}
systick.c:
#include "gd32vf103.h"
#include "systick.h"
void delay_1ms(uint32_t count)
{
uint64_t start_mtime, delta_mtime;
/* don't start measuruing until we see an mtime tick */
uint64_t tmp = get_timer_value();
do{
start_mtime = get_timer_value();
}while(start_mtime == tmp);
do{
delta_mtime = get_timer_value() - start_mtime;
}while(delta_mtime <(SystemCoreClock/4000.0 *count));
}
But instead of 1s delay it delays like for 13s. Where is mistake?
Problem is non in main.c, neither in systick.c, it's in system_gd32vf103.c.
There are:
/* system frequency define */
#define __IRC8M (IRC8M_VALUE) /* internal 8 MHz RC oscillator frequency */
#define __HXTAL (HXTAL_VALUE) /* high speed crystal oscillator frequency */
#define __SYS_OSC_CLK (__IRC8M) /* main oscillator frequency */
/* select a system clock by uncommenting the following line */
/* use IRC8M */
//#define __SYSTEM_CLOCK_48M_PLL_IRC8M (uint32_t)(48000000)
//#define __SYSTEM_CLOCK_72M_PLL_IRC8M (uint32_t)(72000000)
//#define __SYSTEM_CLOCK_108M_PLL_IRC8M (uint32_t)(108000000)
/********************************************************************/
//#define __SYSTEM_CLOCK_HXTAL (HXTAL_VALUE)
//#define __SYSTEM_CLOCK_24M_PLL_HXTAL (uint32_t)(24000000)
/********************************************************************/
//#define __SYSTEM_CLOCK_36M_PLL_HXTAL (uint32_t)(36000000)
//#define __SYSTEM_CLOCK_48M_PLL_HXTAL (uint32_t)(48000000)
//#define __SYSTEM_CLOCK_56M_PLL_HXTAL (uint32_t)(56000000)
//#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
//#define __SYSTEM_CLOCK_96M_PLL_HXTAL (uint32_t)(96000000)
#define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000)
and you need uncomment #define __SYSTEM_CLOCK_HXTAL (HXTAL_VALUE) line. That's it.
Related
I have C type struct like this code below
typedef struct GPIO_PIN_CONFIG
{
uint8_t GPIO_PinNumber;
uint8_t GPIO_PinMode;
uint8_t GPIO_PinSpeed;
uint8_t GPIO_PinPuPdControl;
uint8_t GPIO_PinOpType;
uint8_t GPIO_PinAltFunMode;
}GPIO_PinConfig_t;
and I need to initialize it with certain C macros
/*
* GPIO modes
*/
#define GPIO_INPUT_MODE 0 /* 00: Input (reset state) */
#define GPIO_OUTPUT_MODE 1 /* 01: General purpose output mode */
#define GPIO_ALT_FUNC_MODE 2 /* 10: Alternate function mode */
#define GPIO_ANALOG_MODE 3 /* 11: Analog mode */
#define GPIO_INPUT_INT_FT 4 /* GPIO input interrupt mode falling edge triger*/
#define GPIO_INPUT_INT_RT 5 /* GPIO input interrupt mode Raising edge triger*/
/*
* GPIO output types
*/
#define GPIO_OP_TYPE_PP 1 /* 00: GPIO output push-pull type */
#define GPIO_OP_TYPE_OD 2 /* 01: GPIO output open-drain type */
/*
* GPIO output speeds
*/
#define GPIO_SPEED_LOW 0 /* 00: Low speed */
#define GPIO_SPEED_MEDIUM 1 /* 01: Medium speed */
#define GPIO_SPEED_FAST 2 /* 10: Fast speed */
#define GPIO_SPEED_HIGH 3 /* 11: High speed */
/*
* GPIO pull-up/pull-down configuration
*/
#define GPIO_PIN_NO_PUPD 0 /* 00: No pull-up, pull-down */
#define GPIO_PIN_PULLUP 1 /* 01: Activate Pull-up */
#define GPIO_PIN_PULLDOWN 2 /* 10: Activate Pull-down */
so I was wondering If there's a way to generate autosuggestion for a group of certain macros, for each member of the struct
I have tried comments but I would like autosuggest
I want to implement a simple "GPT" timer that generates an interrupt every 1ms.
However, I get an interrupt exactly every 3ms (instead of the desired 1ms).
Where is my error? What values should I set to get a 1ms timer?
Here is my calculation for the GPT timer:
EXPLANATION OF TIMER VALUES:
We take for source clock the PLL1 DIV2 400MHz
We define the root divisor at 4 => 400MHz / 4 = 100MHz
100MHz = one increment every 10ns
We want an interrupt to be generated every 1 ms
So we have :
Output_compare_value = delay_time x GPT_frequency
Output_compare_value = 1 x 10^-3 x (1/(10 x 10^-9)) = 100000
Here is my code (I change the state of a GPIO at each interrupt to check the operation of my timer on the oscilloscope):
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_gpt.h"
#include "fsl_gpio.h"
#include "fsl_common.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define GPT_IRQ_ID GPT1_IRQn
#define EXAMPLE_GPT GPT1
//#define EXAMPLE_GPT_CLK_FREQ \
// (CLOCK_GetPllFreq(kCLOCK_SystemPll1Ctrl) / (CLOCK_GetRootPreDivider(kCLOCK_RootGpt1)) / \
// (CLOCK_GetRootPostDivider(kCLOCK_RootGpt1)) / 2) /* SYSTEM PLL1 DIV2 */
#define EXAMPLE_GPT_CLK_FREQ 100000
#define EXAMPLE_GPT_IRQHandler GPT1_IRQHandler
#define EXAMPLE_LED_GPIO GPIO3
#define EXAMPLE_LED_GPIO_PIN 23U
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
volatile bool gptIsrFlag = false;
/* The PIN status */
volatile bool g_pinSet = false;
/*******************************************************************************
* Code
******************************************************************************/
void EXAMPLE_GPT_IRQHandler(void)
{
/* Clear interrupt flag.*/
GPT_ClearStatusFlags(EXAMPLE_GPT, kGPT_OutputCompare1Flag);
gptIsrFlag = true;
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F, Cortex-M7, Cortex-M7F Store immediate overlapping
exception return operation might vector to incorrect interrupt */
#if defined __CORTEX_M && (__CORTEX_M == 4U || __CORTEX_M == 7U)
__DSB();
#endif
}
/*!
* #brief Main function
*/
int main(void)
{
uint32_t gptFreq;
gpt_config_t gptConfig;
/* Define the init structure for the output LED pin*/
gpio_pin_config_t led_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
/* Board pin, clock, debug console init */
/* Board specific RDC settings */
BOARD_RdcInit();
BOARD_InitBootPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
BOARD_InitMemory();
CLOCK_SetRootMux(kCLOCK_RootGpt1, kCLOCK_GptRootmuxSysPll1Div2); /* Set GPT1 source to SYSTEM PLL1 DIV2 400MHZ */
CLOCK_SetRootDivider(kCLOCK_RootGpt1, 1U, 4U); /* Set root clock to 400MHZ / 4 = 100MHZ */
GPT_GetDefaultConfig(&gptConfig);
/* Initialize GPT module */
GPT_Init(EXAMPLE_GPT, &gptConfig);
/* Divide GPT clock source frequency by 3 inside GPT module */
GPT_SetClockDivider(EXAMPLE_GPT, 1);
/* Get GPT clock frequency */
gptFreq = EXAMPLE_GPT_CLK_FREQ;
/* GPT frequency is divided by 3 inside module */
gptFreq /= 1;
/* Set both GPT modules to 1 second duration */
GPT_SetOutputCompareValue(EXAMPLE_GPT, kGPT_OutputCompare_Channel1, gptFreq);
/* Enable GPT Output Compare1 interrupt */
GPT_EnableInterrupts(EXAMPLE_GPT, kGPT_OutputCompare1InterruptEnable);
/* Enable at the Interrupt */
EnableIRQ(GPT_IRQ_ID);
PRINTF("\r\nPress any key to start the example");
GETCHAR();
/* Init output LED GPIO. */
GPIO_PinInit(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, &led_config);
/* Start Timer */
PRINTF("\r\nStarting GPT timer ...");
GPT_StartTimer(EXAMPLE_GPT);
while (true)
{
/* Check whether occur interupt and toggle LED */
if (true == gptIsrFlag)
{
PRINTF("\r\n GPT interrupt is occurred !");
gptIsrFlag = false;
if (g_pinSet)
{
GPIO_PinWrite(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, 0U);
g_pinSet = false;
}
else
{
GPIO_PinWrite(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, 1U);
g_pinSet = true;
}
}
else
{
__WFI();
}
}
}
I found out what my problem was with the timer. The truth is that all my values were fine, but it was the execution of the logging that was taking time (line PRINTF("GPT interrupt is occurred !");) So I could have lowered my reload value even more, but I would still have the logging that was taking time to run.
I write a Linux kernel module on Beaglebone Black to interface with GPIO module use its registers. A button connect with GPIO20, a LED connect with GPIO30 via a transistor 200 Ohm. I want when insmod kernel module then press button, LED ON and press again LED OFF, and so on. Following is my code for kernel module. My problem is when press button, LED not switch state.
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/io.h>
#include <linux/delay.h>
#define GPIO_ADDR_BASE 0X44E07000
#define ADDR_SIZE 0X1000
#define GPIO_SETDATAOUT_OFFSET 0X194
#define GPIO_CLEARDATAOUT_OFFSET 0X190
#define GPIO_OE_OFFSET 0X134
#define GPIO_DATAIN_OFFSET 0X138
// enable IRQ
#define GPIO_IRQSTATUS_RAW_0_OFFSET 0X24
// enable interrupt
#define GPIO_IRQSTATUS_SET_0_OFFSET 0X34
// define intterupt type
#define GPIO_RISINGDETECT_OFFSET 0X148
// enable debounce feature
#define GPIO_DEBOUNCENABLE_OFFSET 0X150
// set debounce time
#define GPIO_DEBOUNCINGTIME_OFFSET 0X154
#define LED_VALUE_0 ~(1 << 30)
#define LED_VALUE_1 (1 << 30)
#define BUTTON_VALUE_0 ~(1 << 20)
#define BUTTON_VALUE_1 (1 << 20)
void __iomem *base_addr;
int init_module(void)
{
uint32_t reg_data = 0;
printk(KERN_EMERG "Hello");
base_addr = ioremap(GPIO_ADDR_BASE, ADDR_SIZE);
// CONFIG for LED
/* write 0 to GPIO Pin 30 mean config this pin is output */
writel_relaxed(LED_VALUE_0, base_addr + GPIO_OE_OFFSET);
// at insert module time, led off
writel_relaxed(LED_VALUE_1, base_addr + GPIO_CLEARDATAOUT_OFFSET);
// CONFIG for BUTTON
// config button pin is GPIO input
writel_relaxed(BUTTON_VALUE_1, base_addr + GPIO_OE_OFFSET);
// active IRQ
writel_relaxed(0x1, base_addr + GPIO_IRQSTATUS_RAW_0_OFFSET);
// enable interrupt
writel_relaxed(0x1, base_addr + GPIO_IRQSTATUS_SET_0_OFFSET);
// enable rising interrupt
writel_relaxed(0x1, base_addr + GPIO_RISINGDETECT_OFFSET);
// enable debounce feature
writel_relaxed(1 << 20, base_addr + GPIO_DEBOUNCENABLE_OFFSET);
// set debounce time
writel_relaxed(0xFF, base_addr + GPIO_DEBOUNCINGTIME_OFFSET);
while (1) {
reg_data = readl_relaxed(base_addr + GPIO_DATAIN_OFFSET);
if (reg_data == 1)
{
writel_relaxed(LED_VALUE_1, base_addr + GPIO_SETDATAOUT_OFFSET);
mdelay(1000);
}
}
return 0;
}
void cleanup_module(void)
{
printk(KERN_EMERG "Goodbye");
/* off led */
writel_relaxed(LED_VALUE_1, base_addr + GPIO_CLEARDATAOUT_OFFSET);
/* config default GPIO Pin is input
* set Pin 30 to default
*/
writel_relaxed(LED_VALUE_1, base_addr + GPIO_CLEARDATAOUT_OFFSET);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Phu Luu An");
MODULE_DESCRIPTION("Hello world kernel module");
I'm attempting to set up UART communication with a GPS module but keep running into some communication error. The GPS sends a packet approximately once a second to the SAM4E16E micro, which should be trigger an interrupt and read in the data. I have two UARTS which are configured like this
conf_uart.h
#include "asf.h" //uart.h etc. included here
#define UART_SERIAL_BAUDRATE 4800
#define UART_SERIAL_CHANNEL_MODE UART_MR_CHMODE_NORMAL
#define UART_SERIAL_MODE UART_MR_PAR_NO
#define UART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT
/* =============== UART0 =============== */
#define PINS_UART0 (PIO_PA9A_URXD0 | PIO_PA10A_UTXD0)
#define PINS_UART0_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
#define PINS_UART0_MASK (PIO_PA9A_URXD0 | PIO_PA9A_URXD0)
#define PINS_UART0_PIO PIOA
#define PINS_UART0_ID ID_PIOA
#define PINS_UART0_TYPE PIO_PERIPH_A
#define PINS_UART0_ATTR PIO_DEFAULT
/* =============== UART1 =============== */
#define PINS_UART1 (PIO_PB2A_URXD1 | PIO_PB3A_UTXD1)
#define PINS_UART1_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
#define PINS_UART1_MASK (PIO_PB2A_URXD1 | PIO_PB3A_UTXD1)
#define PINS_UART1_PIO PIOB
#define PINS_UART1_ID ID_PIOB
#define PINS_UART1_TYPE PIO_PERIPH_A
#define PINS_UART1_ATTR PIO_DEFAULT
conf_uart.c
#include "conf_uart.h"
void uart_custom_init_1(void) {
// set the pins to use the uart peripheral
pio_configure(PINS_UART1_PIO, PINS_UART1_TYPE, PINS_UART1_MASK, PINS_UART1_ATTR);
//enable the uart peripherial clock
pmc_enable_periph_clk(ID_UART1);
const sam_uart_opt_t uart1_settings =
{ sysclk_get_cpu_hz(), UART_SERIAL_BAUDRATE, UART_SERIAL_MODE };
uart_init(UART1,&uart1_settings); //Init UART1 and enable Rx and Tx
uart_enable_interrupt(UART1,UART_IER_RXRDY); //Interrupt reading ready
NVIC_EnableIRQ(UART1_IRQn);
}
uart_custom_init_0(void)
{
// 1. Enable UART peripheral clock
//2. Enable the required PIO's
pio_configure(PINS_UART0_PIO, PINS_UART0_TYPE, PINS_UART0_MASK, PINS_UART0_ATTR);
pmc_enable_periph_clk(ID_UART0);
// 3. call init
const sam_uart_opt_t uart0_settings =
{ sysclk_get_cpu_hz(), UART_SERIAL_BAUDRATE, UART_SERIAL_MODE };
uart_init(UART0,&uart0_settings); //Init UART1 and enable Rx and Tx
// read on interrupt
uart_enable_interrupt(UART0,UART_IER_RXRDY); //Interrupt reading ready
NVIC_EnableIRQ(UART0_IRQn);
}
void UART1_Handler() {
uint32_t dw_status = uart_get_status(UART1);
if(dw_status & UART_SR_RXRDY) {
uint8_t received_byte1;
uart_read(UART1, &received_byte);
read_buffer[read_buffer_index] = received_byte;
read_buffer_index++;
}
}
void UART0_Handler() {
uint32_t dw_status = uart_get_status(UART0);
if(dw_status & UART_SR_RXRDY)
{
uint8_t received_byte0;
uart_read(UART0, &received_byte);
read_buffer[read_buffer_index] = received_byte;
read_buffer_index++;
}
}
UART1 works perfectly. UART0 Only fires roughly once per packet, and is not even getting the first byte in the packet (should be 36, receives 16). As far as I can tell they are identical. The chip is working, I can see it transmitting on the scope. I'm basically fresh out of ideas.
I am designing an IIR 2nd order Lowpass filter with sampling frequency = 100Hz and cutoff frequency = 10 Hz. The filter coefficients are of Chebyshev Type I using fdatool in Matlab.
But the code is not able to filter the signal (i.e. for all frequencies it gives the output with same amplitudes as the input signal) . Only minor decrease in amplitude is observed for an input signal of 10 KHz and above. I assure you that the ADC and DAC are working fine as i have tested the for FFT filter.
Here is the code:
/* Include core modules */
#include "stm32f4xx.h"
#include "stdint.h"
#include "stdlib.h"
#include "arm_math.h"
#include "my_files.h"
#define URS 2
#define numStages 1
#define NUM_TAPS 5*numStages
#define samples 3
////////ADC FUNCTION//////////////////
void ADC_configure(void)
{
RCC->APB2ENR|=1Ul<<8; // ADC1 clock enabled
ADC1->CR2|=0x00000001; // enable ADC
ADC1->CR1|=0; // single conversion ADC1 pin 0 has been selected
}
int32_t readADC(void)
{
ADC1->CR2|=(1UL<<30);
return(ADC1->DR);
}
////////DAC FUNCTION/////////////////
int32_t dv1,dv2,ds;
//---function declaration--//
// initilising DAC---------//
void DAC_init(void)
{
RCC->APB1ENR|=1UL<<29;
DAC->CR|=((1UL<<16)|(1UL<<0));
RCC->AHB1ENR|=0x00000001; // clock to gpio A
GPIOA->MODER|=0x00000F03; // pt0,4,5 in Analog mode
}
// Sending to DAC...........//
void Send_DAC(int32_t data_in1, int32_t data_in2)
{ dv1=data_in1;
dv2=data_in2<<16;
ds=dv2+dv1;
DAC->DHR12RD=ds;
}
/* IIR settings */
float32_t pState[2*numStages];
const float pCoeffs[NUM_TAPS] = {1,2,1,-1.1997,0.5157};//{b0,b1,b2,a1,a2}
/* Global variables */
float32_t Input[samples]; /* Data to be read from ADC */
float32_t InputData[samples]; /* Data to be processed */
float32_t Output[samples]; /* Output filtered Data */
arm_biquad_cascade_df2T_instance_f32 S; /* ARM IIR module */
uint16_t i;
void TIM3_Init (void) {
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; /* enable clock for TIM1 */
TIM3->PSC = 8600; /* set prescaler = 10KHz */
TIM3->ARR = 100; /* set auto-reload = 10ms */
TIM3->RCR = 0; /* set repetition counter */
TIM3->CR1 |= (1UL << URS);
TIM3->DIER = TIM_DIER_UIE; /* Update Interrupt enable */
NVIC_EnableIRQ(TIM3_IRQn); /* TIM1 Interrupt enable */
NVIC_SetPriority (TIM3_IRQn, 0);
TIM3->CR1 |= TIM_CR1_CEN; /* timer enable */
}
void TIM3_IRQHandler() {
/*Shift Operation*/
for(i=samples-1;i>0;i--){
Input[i]= Input[i-1];
InputData[i]= Input[i];
}
/* Input part from the ADC */
Input[0] = (float32_t)readADC();
InputData[0] = Input[0];
//////////IIR//////////////////////
/* Initialize the IIR module */
arm_biquad_cascade_df2T_init_f32(&S, numStages, pCoeffs, pState);
/* Process the data through the IIR module */
arm_biquad_cascade_df2T_f32(&S, InputData, Output, samples);
////////DAC Output/////////////////
Send_DAC(Input[0], Output[0]);
}
/////////main function///////////////
int main(void) {
/* Initialize system */
SystemInit();
DAC_init();
ADC_configure();
TIM3_Init();
while (1) {
}
}
Any suggestion or solution would be of great help.
Some possible problems:
Did you enable the FPU?
Check alignment for ADC (and DAC?).
Ensure the interrupt-handler does not run too long (overflow).
Good you do not use the stdlib for much more tha init, btw. But you really should use symbolic constants for the register initialization! This does not cost extra.
Not directly related, but will(!) give wrong results: If I get it right, you trigger each conversion in readADC. This leads to jitter (resulting in noise on the digitized signal); trigger the conversations by a timer (that's what the trigger system is for actually) and use the ADC-interrupt to read the data or use a DMA (the STM DMA provides a double-buffer mode which is perfect for this). In this simple example, if using DMA, you can even get along completely without interrupt and do the calculations in the main program.
For the DAC you should the same.
Not sure why use a timer anyway; the ADC can self-trigger. Is that not sufficient?
You do not need to init IIR filter every time. Do it only once in init code. Init procedure clears previous values in pState, but they are required for IIR to perform correctly. That's the reason why your filter doesn't work. Presence of FPU only influences the speed of computation.