Exemple #1
0
/*!
    \brief      configure comparator
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cmp_config(void)
{
    /* enable GPIOA clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    
    /* configure PA1 as comparator input */
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
    gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_PULLUP, GPIO_PIN_1);
    
    /* enable comparator clock */
    rcu_periph_clock_enable(RCU_CFGCMP);
    
    /* configure comparator channel0 */
    cmp_mode_init(CMP_CHANNEL_CMP0, CMP_LOWSPEED, CMP_VREFINT, CMP_HYSTERESIS_HIGH);
    cmp_output_init(CMP_CHANNEL_CMP0, CMP_OUTPUT_NONE, CMP_OUTPUT_POLARITY_NOINVERTED);
    
    /* configure comparator channel1 */
    cmp_mode_init(CMP_CHANNEL_CMP1, CMP_LOWSPEED, CMP_1_2VREFINT, CMP_HYSTERESIS_HIGH);
    cmp_output_init(CMP_CHANNEL_CMP1, CMP_OUTPUT_NONE, CMP_OUTPUT_POLARITY_NOINVERTED);
    
    /* configure exti line */
    exti_init(EXTI_21, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_init(EXTI_22, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    
    /* configure ADC_CMP nvic */
    nvic_irq_enable(ADC_CMP_IRQn, 0, 0);
    
    /* enable comparator window */
    cmp_window_enable();
    
    /* enable comparator channels */
    cmp_channel_enable(CMP_CHANNEL_CMP0);
    cmp_channel_enable(CMP_CHANNEL_CMP1);
}
Exemple #2
0
/*!
    \brief      init the GPIO port of SLCD peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void slcd_gpio_config(void)
{
    /* enable the clock of GPIO */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOF);

    /* SLCD GPIO */
    /* configure GPIOA */
    gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
    gpio_output_options_set(GPIOA,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
    gpio_af_set(GPIOA,GPIO_AF_11,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
    
    /* configure GPIOB */
    gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_9|GPIO_PIN_14|GPIO_PIN_15);
    gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9|GPIO_PIN_14|GPIO_PIN_15);
    gpio_af_set(GPIOB,GPIO_AF_11,GPIO_PIN_9|GPIO_PIN_14|GPIO_PIN_15);
    
    /* configure GPIOC */
    gpio_mode_set(GPIOC,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9);
    gpio_output_options_set(GPIOC,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9);
    gpio_af_set(GPIOC,GPIO_AF_11,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9);
    
    /* configure GPIOF */
    gpio_mode_set(GPIOF,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
    gpio_output_options_set(GPIOF,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
    gpio_af_set(GPIOF,GPIO_AF_11,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
}
Exemple #3
0
/*!
    \brief      configure GPIO
    \param[in]  none
    \param[out] none
    \retval     none
*/
void gpio_config(void)
{
    /* enable can clock */
    rcu_periph_clock_enable(RCU_CAN0);
    rcu_periph_clock_enable(RCU_CAN1);
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    
    /* configure CAN0 GPIO */
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_5);
    gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_5);

    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
    gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_6);
    
    /* configure CAN1 GPIO */
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
    gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_12);
    
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
    gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_13);
    
}
Exemple #4
0
/**
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
  */
void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);

    /*Configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);

    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);

    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);

    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_9);
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_10);

    /*Configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);

    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_14);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_14);

    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_15);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_15);

    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_13);
    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_14);
    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_15);
}
Exemple #5
0
/*!
    \brief      configure the CEC peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_config(void)
{
    /* enable clocks */
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_CEC);

    /* configure CEC_LINE_GPIO as output open drain */
    gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_8);
    gpio_output_options_set(GPIOB,GPIO_OTYPE_OD,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
    gpio_af_set(GPIOB,GPIO_AF_0,GPIO_PIN_8);

    /* configure priority group */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);

    /* enable the CEC global interrupt (with higher priority) */
    nvic_irq_enable(CEC_IRQn,0,0);

    /* configure CEC */
    cec_init(CEC_SFT_START_SOM,CEC_SFT_1POINT5_PERIOD,CEC_OWN_ADDRESS2);
    cec_error_config(CEC_BROADCAST_ERROR_BIT_OFF,CEC_LONG_PERIOD_ERROR_BIT_OFF,CEC_RISING_PERIOD_ERROR_BIT_OFF,CEC_STOP_RISING_ERROR_BIT_OFF);
    cec_reception_tolerance_disable();

    /* activate CEC interrupts associated to the set of TX and RX flags */
    cec_interrupt_enable(CEC_INTEN_TENDIE | CEC_INTEN_TBRIE | CEC_INTEN_RENDIE | CEC_INTEN_RBRIE);

    /* activate CEC interrupts associated to the set of TX and RX error */
    cec_interrupt_enable(CEC_INTEN_ROIE | CEC_INTEN_RBREIE | CEC_INTEN_RSBPEIE | CEC_INTEN_RLBPEIE
                         | CEC_INTEN_RAEIE | CEC_INTEN_LSTARBIE | CEC_INTEN_TUIE | CEC_INTEN_TERRIE | CEC_INTEN_TAERRIE );

    /* enable CEC */
    cec_enable();
}
Exemple #6
0
/*!
    \brief      configure RCU
    \param[in]  none
    \param[out] none
    \retval     none
*/
void rcu_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_TIMER1);
    rcu_periph_clock_enable(RCU_CFGCMP);
}
Exemple #7
0
/** Initialize the serial peripheral. It sets the default parameters for serial
 *  peripheral, and configures its specifieds pins.
 *
 * @param obj The serial object
 * @param tx  The TX pin name
 * @param rx  The RX pin name
 */
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    struct serial_s *p_obj = GET_SERIAL_S(obj);

    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);

    p_obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    MBED_ASSERT(p_obj->uart != (UARTName)NC);

    /* enable UART peripheral clock */
    if (p_obj->uart == UART_0) {
        p_obj->index = 0;
        rcu_periph_clock_enable(usart_clk[p_obj->index]);
    } else if (p_obj->uart == UART_1) {
        p_obj->index = 1;
        rcu_periph_clock_enable(usart_clk[p_obj->index]);
    } else if (p_obj->uart == UART_2) {
        p_obj->index = 2;
        rcu_periph_clock_enable(usart_clk[p_obj->index]);
    } else if (p_obj->uart == UART_3) {
        p_obj->index = 3;
        rcu_periph_clock_enable(usart_clk[p_obj->index]);
    } else if (p_obj->uart == UART_4) {
        p_obj->index = 4;
        rcu_periph_clock_enable(usart_clk[p_obj->index]);
    }

    /* configurte the pins */
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);

    /* default UART parameters */
    p_obj->baudrate    = 9600U;
    p_obj->databits    = USART_WL_8BIT;
    p_obj->stopbits    = USART_STB_1BIT;
    p_obj->parity      = USART_PM_NONE;

#if DEVICE_SERIAL_FC
    p_obj->hw_flow_ctl = USART_HWCONTROL_NONE;
#endif /* DEVICE_SERIAL_FC */

    p_obj->pin_tx = tx;
    p_obj->pin_rx = rx;

    p_obj->tx_state = OP_STATE_BUSY;
    p_obj->rx_state = OP_STATE_BUSY;

    usart_init(p_obj);

    p_obj->tx_state = OP_STATE_READY;
    p_obj->rx_state = OP_STATE_READY;

    if (p_obj->uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemple #8
0
/** Initialize the SPI peripheral
 *
 * Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
 * @param[out] obj  The SPI object to initialize
 * @param[in]  mosi The pin to use for MOSI
 * @param[in]  miso The pin to use for MISO
 * @param[in]  sclk The pin to use for SCLK
 * @param[in]  ssel The pin to use for SSEL
 */
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
{
    struct spi_s *spiobj = SPI_S(obj);

    SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
    SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
    SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
    SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);

    /* return SPIName according to PinName */
    SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
    SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);

    spiobj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
    MBED_ASSERT(spiobj->spi != (SPIName)NC);

    /* Set iqr type */
    if (spiobj->spi == SPI0) {
        rcu_periph_clock_enable(RCU_SPI0);
        spiobj->spi_irq = SPI0_IRQn;
    }
    if (spiobj->spi == SPI1) {
        rcu_periph_clock_enable(RCU_SPI1);
        spiobj->spi_irq = SPI1_IRQn;
    }
    if (spiobj->spi == SPI2) {
        rcu_periph_clock_enable(RCU_SPI2);
        spiobj->spi_irq = SPI2_IRQn;
    }

    /* config GPIO mode of SPI pins */
    pinmap_pinout(mosi, PinMap_SPI_MOSI);
    pinmap_pinout(miso, PinMap_SPI_MISO);
    pinmap_pinout(sclk, PinMap_SPI_SCLK);
    spiobj->pin_miso = miso;
    spiobj->pin_mosi = mosi;
    spiobj->pin_sclk = sclk;
    spiobj->pin_ssel = ssel;
    if (ssel != NC) {
        pinmap_pinout(ssel, PinMap_SPI_SSEL);
        spiobj->spi_struct.nss = SPI_NSS_HARD;
        spi_nss_output_enable(spiobj->spi);
    } else {
        spiobj->spi_struct.nss = SPI_NSS_SOFT;
    }

    spiobj->spi_struct.device_mode          = SPI_MASTER;
    spiobj->spi_struct.prescale             = SPI_PSC_256;
    spiobj->spi_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
    spiobj->spi_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
    spiobj->spi_struct.frame_size           = SPI_FRAMESIZE_8BIT;
    spiobj->spi_struct.endian               = SPI_ENDIAN_MSB;

    dev_spi_struct_init(obj);
}
Exemple #9
0
/*!
    \brief      configure the different system clocks
    \param[in]  none
    \param[out] none
    \retval     none
*/
void rcu_config(void)
{
    /* enable USB pull-up pin clock */ 
    rcu_periph_clock_enable(RCC_AHBPeriph_GPIO_PULLUP);

    /* configure USB model clock from PLL clock */
    rcu_usbd_clock_config(RCU_USBD_CKPLL_DIV1_5);

    /* enable USB APB1 clock */
    rcu_periph_clock_enable(RCU_USBD);
}
Exemple #10
0
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    /* GPIO clock enable */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOF);

    /* delay time initialize */
    systick_config();

    /* configure the CEC peripheral */
    cec_config();

    /* configure the EXTI */
    gd_eval_keyinit(KEY_TAMPER, KEY_MODE_EXTI);

#ifdef GD32F130_150

    gd_eval_keyinit(KEY_USER, KEY_MODE_EXTI);

#elif defined(GD32F170_190)

    gd_eval_keyinit(KEY_WAKEUP, KEY_MODE_EXTI);

#endif /* GD32F130_150 */

    /* LED1 LED2 initialize */
    gd_eval_ledinit(LED1);
    gd_eval_ledinit(LED2);
    gd_eval_ledoff(LED1);
    gd_eval_ledoff(LED2);

    /* main loop */
    while(1) {
        /* wait receive data */
        while(rcvstatus==0);

        if(rcvstatus == 1) {
            if((rcvdata[1]==0xA5)&&(rcvdata[2]==0x5A)) {
                gd_eval_ledon(LED1);
                delay_1ms(10);
                gd_eval_ledoff(LED1);
            }
            if((rcvdata[1]==0x5A)&&(rcvdata[2]==0xA5)) {
                gd_eval_ledon(LED2);
                delay_1ms(10);
                gd_eval_ledoff(LED2);
            }
        }
        /* a reception error occured */
        rcvstatus = 0;
    }
}
Exemple #11
0
/** Initialize the I2C peripheral. It sets the default parameters for I2C
 *  peripheral, and configures its specifieds pins.
 *
 *  @param obj  The I2C object
 *  @param sda  The sda pin
 *  @param scl  The scl pin
 */
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
    struct i2c_s *obj_s = I2C_S(obj);

    /* find the I2C by pins */
    uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
    uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);

    obj_s->sda = sda;
    obj_s->scl = scl;
    obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT(obj_s->i2c != (I2CName)NC);

    switch (obj_s->i2c) {
        case I2C_0:
            /* enable I2C0 clock and configure the pins of I2C0 */
            obj_s->index = 0;
            rcu_periph_clock_enable(RCU_I2C0);

            break;

        case I2C_1:
            /* enable I2C1 clock and configure the pins of I2C1 */
            obj_s->index = 1;
            rcu_periph_clock_enable(RCU_I2C1);

            break;

        default:
            break;
    }

    /* configure the pins of I2C */
    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);

    /* 100 KHz as the default I2C frequence */
    i2c_frequency(obj, 100000);

    obj_s->state = (operation_state_enum)I2C_STATE_NONE;
    obj_s->previous_state_mode = I2C_STATE_NONE;
    obj_s->global_trans_option = I2C_FIRST_AND_LAST_FRAME;

#if DEVICE_I2CSLAVE
    /* I2C master by default */
    obj_s->slave = 0;
#endif
}
Exemple #12
0
/** Initialize the analogout peripheral
 *
 * Configures the pin used by analogout.
 * @param obj The analogout object to initialize
 * @param pin The analogout pin name
 */
void analogout_init(dac_t *obj, PinName pin)
{
    /* get the peripheral name from the pin and assign it to the object */
    obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
    MBED_ASSERT(obj->dac != (DACName)NC);

    /* get the pin function and assign the used channel to the object */
    uint32_t function = pinmap_function(pin, PinMap_DAC);
    MBED_ASSERT(function != (uint32_t)NC);

    obj->channel = GD_PIN_CHANNEL_GET(function);
    MBED_ASSERT(obj->channel <= DAC1);

    /* configure GPIO */
    pinmap_pinout(pin, PinMap_DAC);

    /* save the pin for future use */
    obj->pin = pin;

    /* enable DAC clock */
    rcu_periph_clock_enable(RCU_DAC);

    /* configure DAC */
    dac_wave_mode_config(obj->channel, DAC_WAVE_DISABLE);
    dac_trigger_disable(obj->channel);
    dac_output_buffer_enable(obj->channel);
    analogout_write_u16(obj, 0);
}
Exemple #13
0
/*!
    \brief      init the GPIO port of the SLCD and SLCD peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void slcd_seg_config(void)
{
    uint16_t i;

    /* configure the SLCD GPIO pins */
    slcd_gpio_config();

    /* enable the clock of SLCD */
    rcu_periph_clock_enable(RCU_SLCD);
    /* wait 2 RTC clock to write SLCD register */
    for(i = 0;i < 500;i++);
    /* config the prescaler and the divider of SLCD clock */
    slcd_clock_config(SLCD_PRESCALER_4,SLCD_DIVIDER_19);
    /* SLCD bias voltage select */
    slcd_bias_voltage_select(SLCD_BIAS_1_3);
    /* SLCD duty cycle select */
    slcd_duty_select(SLCD_DUTY_1_4);
    /* SLCD voltage source select */
    slcd_voltage_source_select(SLCD_VOLTAGE_EXTERNAL);
    /* SLCD pulse on duration config */
    slcd_pulse_on_duration_config(SLCD_PULSEON_DURATION_7);
    /* SLCD dead time duration config */
    slcd_dead_time_config(SLCD_DEADTIME_PERIOD_7);
    /* enable the permanent high drive */
    slcd_high_drive_config(ENABLE);
    /* wait for SLCD CFG register synchronization */
    while (!slcd_flag_get(SLCD_FLAG_SYNF));
    /* enable SLCD interface */
    slcd_enable();
    /* wait for SLCD controller on flag */
    while (!slcd_flag_get(SLCD_FLAG_ONF));
    /* wait for SLCD voltage ready flag */
    while (!slcd_flag_get(SLCD_FLAG_VRDYF));
}
Exemple #14
0
/**
    \brief      configure the DMA peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void dma_config(void)
{
    dma_parameter_struct dma_init_struct;

    /* enable DMA clock */
    rcu_periph_clock_enable(RCU_DMA);

    /* initialize DMA channel4 */
    dma_deinit(DMA_CH4);

    /* DMA channel4 initialize */
    dma_deinit(DMA_CH4);
    dma_init_struct.direction    = DMA_MEMORY_TO_PERIPHERA;
    dma_init_struct.memory_addr  = (uint32_t)buffer;
    dma_init_struct.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;
    dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
    dma_init_struct.number       = 8;
    dma_init_struct.periph_addr  = (uint32_t)TIMER0_DMATB;
    dma_init_struct.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
    dma_init_struct.priority     = DMA_PRIORITY_HIGH;
    dma_init(DMA_CH4,dma_init_struct);

    /* configure DMA mode */
    dma_circulation_enable(DMA_CH4);
    dma_memory_to_memory_disable(DMA_CH4);

    /* enable DMA channel4 */
    dma_channel_enable(DMA_CH4);
}
Exemple #15
0
/**
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void timer_config(void)
{
    /* -----------------------------------------------------------------------
    TIMER1 configuration: single pulse mode
    the external signal is connected to TIMER1 CH0 pin (PA0) and the falling 
    edge is used as active edge.
    the single pulse signal is output on TIMER1 CH1 pin (PA1).

    the TIMER1CLK frequency is set to systemcoreclock (72MHz),the prescaler is 
    4,so the TIMER1 counter clock is 18MHz.

    single pulse value = (TIMER1_Period - TIMER1_Pulse) / TIMER1 counter clock
                       = (65535 - 11535) / 18MHz = 3.0 ms.
    ----------------------------------------------------------------------- */
    timer_oc_parameter_struct timer_ocinitpara;
    timer_parameter_struct timer_initpara;
    timer_ic_parameter_struct timer_icinitpara;

    rcu_periph_clock_enable(RCU_TIMER1);

    timer_deinit(TIMER1);

    /* TIMER1 configuration */
    timer_initpara.timer_prescaler         = 3;
    timer_initpara.timer_alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.timer_counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.timer_period            = 65535;
    timer_initpara.timer_clockrivision     = TIMER_CKDIV_DIV1;
    timer_initpara.timer_repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);

    /* auto-reload preload disable */
    timer_auto_reload_shadow_disable(TIMER1);

    /* CH1 configuration in OC PWM2 mode */
    timer_ocinitpara.timer_ocpolarity  = TIMER_OC_POLARITY_HIGH;
    timer_ocinitpara.timer_outputState = TIMER_CCX_ENABLE;
    timer_ocinitpara.timer_ocidleState = TIMER_OC_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocinitpara);

    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,11535);
    timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /* TIMER1 CH0 input capture configuration */
    timer_icinitpara.timer_icpolarity  = TIMER_IC_POLARITY_FALLING;
    timer_icinitpara.timer_icselection = TIMER_IC_SELECTION_DIRECTTI;
    timer_icinitpara.timer_icprescaler = TIMER_IC_PSC_DIV1;
    timer_icinitpara.timer_icfilter = 0x00;
    timer_input_capture_config(TIMER1,TIMER_CH_0,&timer_icinitpara);

    /* single pulse mode selection */
    timer_single_pulse_mode_config(TIMER1,TIMER_SP_MODE_SINGLE);

    /* slave mode selection : TIMER1 */
    /* TIMER1 input trigger : external trigger connected to CI0 */
    timer_input_trigger_source_select(TIMER1,TIMER_SMCFG_TRGS_CI0FE0);
    timer_slave_mode_select(TIMER1,TIMER_SLAVE_MODE_EVENT);
}
Exemple #16
0
/*!
    \brief      configure deepsleep mode
    \param[in]  none
    \param[out] none
    \retval     none
*/
void deepsleep_mode_config(void)  
{
    /* enable pmu clock */
    rcu_periph_clock_enable(RCU_PMU);

   /* enter to deepsleep mode */
    pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);  
}
Exemple #17
0
/**
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void timer_config(void)
{
/* -----------------------------------------------------------------------
    TIMER0 configuration to:
    generate 3 complementary PWM signals with 3 different duty cycles:
    TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 3600 so the 
    TIMER0 counter clock used is 20KHz.
    the three duty cycles are computed as the following description: 
    the channel 0 duty cycle is set to 25% so channel 1N is set to 75%.
    the channel 1 duty cycle is set to 50% so channel 2N is set to 50%.
    the channel 2 duty cycle is set to 75% so channel 3N is set to 25%.
  ----------------------------------------------------------------------- */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER0);

    timer_deinit(TIMER0);

    /* TIMER0 configuration */
    timer_initpara.timer_prescaler         = 3599;
    timer_initpara.timer_alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.timer_counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.timer_period            = 15999;
    timer_initpara.timer_clockrivision     = TIMER_CKDIV_DIV1;
    timer_initpara.timer_repetitioncounter = 0;
    timer_init(TIMER0,&timer_initpara);

     /* CH1,CH2 and CH3 configuration in PWM mode */
    timer_ocintpara.timer_outputState  = TIMER_CCX_ENABLE;
    timer_ocintpara.timer_outputnState = TIMER_CCXN_ENABLE;
    timer_ocintpara.timer_ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.timer_ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.timer_ocidleState  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.timer_ocnidleState = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,3999);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,7999);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,11999);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

    timer_primary_output_config(TIMER0,ENABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER0);
    timer_enable(TIMER0);
}
Exemple #18
0
static int rt_hw_spi5_init(void)
{
    /* register spi bus */
    {
		rt_err_t result;
        
        rcu_periph_clock_enable(RCU_GPIOG);
        rcu_periph_clock_enable(RCU_SPI5);

        /* SPI5_CLK(PG13), SPI5_MISO(PG12), SPI5_MOSI(PG14),SPI5_IO2(PG10) and SPI5_IO3(PG11) GPIO pin configuration */
        gpio_af_set(GPIOG, GPIO_AF_5, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
        gpio_mode_set(GPIOG, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);
        gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14);

		result = gd32_spi_bus_register(SPI5, SPI_BUS_NAME);
        if (result != RT_EOK)
		{
			return result;
		}
    }

    /* attach cs */
    {
        static struct rt_spi_device spi_device;
        static struct gd32_spi_cs  spi_cs;
		rt_err_t result;
        
        spi_cs.GPIOx = GPIOG;
        spi_cs.GPIO_Pin = GPIO_PIN_9;
        
        /* SPI5_CS(PG9) GPIO pin configuration */
        gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
        gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
        
        gpio_bit_set(GPIOG,GPIO_PIN_9);

        result = rt_spi_bus_attach_device(&spi_device, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs);
		if (result != RT_EOK)
		{
			return result;
		}
    }

	return RT_EOK;
}
Exemple #19
0
/** Initialize the TRNG peripheral
 *
 * @param obj The TRNG object
 */
void trng_init(trng_t *obj)
{
    (void)obj;
    /* TRNG module clock enable */
    rcu_periph_clock_enable(RCU_TRNG);

    /* TRNG registers reset */
    trng_deinit();
    trng_enable();
}
Exemple #20
0
/**
* @brief UART MSP Initialization
*        This function configures the hardware resources used in this example:
*           - Peripheral's clock enable
*           - Peripheral's GPIO Configuration
*           - NVIC configuration for UART interrupt request enable
* @param uart: UART handle pointer
* @retval None
*/
void gd32_uart_gpio_init(struct gd32_uart *uart)
{
    /* enable USART clock */
    rcu_periph_clock_enable(uart->tx_gpio_clk);
    rcu_periph_clock_enable(uart->rx_gpio_clk);
    rcu_periph_clock_enable(uart->per_clk);    

    /* connect port to USARTx_Tx */
    gpio_af_set(uart->tx_port, uart->tx_af, uart->tx_pin);
    gpio_mode_set(uart->tx_port, GPIO_MODE_AF, GPIO_PUPD_NONE, uart->tx_pin);
    gpio_output_options_set(uart->tx_port, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, uart->tx_pin);
	
    /* connect port to USARTx_Rx */
    gpio_af_set(uart->rx_port, uart->rx_af, uart->rx_pin);
    gpio_mode_set(uart->rx_port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, uart->rx_pin);
    gpio_output_options_set(uart->rx_port, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, uart->rx_pin);
	
    NVIC_SetPriority(uart->irqn, 0);
    NVIC_EnableIRQ(uart->irqn);
}
Exemple #21
0
/** init the CAN frequency.
 *
 *  @param rd receive pin.
 *  @param td transmit pin.
 *  @param hz The bus frequency in hertz.
 */
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
{
    CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
    CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
    obj->can = (CANName)pinmap_merge(can_rd, can_td);

    MBED_ASSERT((int)obj->can != NC);

    if (obj->can == CAN_0) {
        rcu_periph_clock_enable(RCU_CAN0);
        can_deinit(obj->can);
        obj->index = 0;
    } else if (obj->can == CAN_1) {
        rcu_periph_clock_enable(RCU_CAN0);
        rcu_periph_clock_enable(RCU_CAN1);
        can_deinit(obj->can);
        obj->index = 1;
    } else {
        return;
    }

    /* Configure the CAN pins */
    pinmap_pinout(rd, PinMap_CAN_RD);
    pinmap_pinout(td, PinMap_CAN_TD);
    if (rd != NC) {
        pin_mode(rd, PullUp);
    }
    if (td != NC) {
        pin_mode(td, PullUp);
    }

    dev_can_mode_config(obj->can, CAN_NORMAL_MODE);

    can_frequency(obj, hz);

    if (obj->can == CAN_0) {
        can_filter(obj, 0, 0, CANStandard, 0);
    } else {
        can_filter(obj, 0, 0, CANStandard, 14);
    }
}
Exemple #22
0
/*!
    \brief      configure the different system clocks
    \param[in]  none
    \param[out] none
    \retval     none
*/
void rcu_config(void)
{
    /* enable PMU clock */
    rcu_periph_clock_enable(RCU_PMU);
    /* PMU backup domain write enable */
    pmu_backup_write_enable();
    /* enable IRC40K */
    rcu_osci_on(RCU_IRC40K);
    /* wait for IRC40K stabilization flags */
    rcu_osci_stab_wait(RCU_IRC40K);
    /* configure the RTC clock source selection */
    rcu_slcd_clock_config(RCU_RTC_IRC40K);
}
Exemple #23
0
/**
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
  */
void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);

    /*configure PA0 PA1(TIMER1 CH0 CH1) as alternate function*/
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0);

    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);

    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_0);
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_1);
}
Exemple #24
0
void gd32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{
    const struct pin_index *index;
    rt_uint32_t pin_mode;
	  rt_uint32_t otype;
	  rt_uint32_t pull_up_down;
    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return;
    }

    /* GPIO Periph clock enable */
    rcu_periph_clock_enable(index->clk);
    pin_mode = GPIO_MODE_OUTPUT;
		otype = GPIO_OTYPE_PP;
		pull_up_down = GPIO_PUPD_NONE;
    
   switch(mode)
   {
   case PIN_MODE_OUTPUT:
        /* output setting */
        break;
   case PIN_MODE_OUTPUT_OD:
        /* output setting: od. */
		    otype = GPIO_OTYPE_OD;
        break;
   case PIN_MODE_INPUT:
        /* input setting: not pull. */
        pin_mode = GPIO_MODE_INPUT;
        break;
   case PIN_MODE_INPUT_PULLUP:
        /* input setting: pull up. */
        pin_mode = GPIO_MODE_INPUT;
	      pull_up_down = GPIO_PUPD_PULLUP;
        break;
   case PIN_MODE_INPUT_PULLDOWN:
        /* input setting: pull down. */
	      pin_mode = GPIO_MODE_INPUT;
	      pull_up_down = GPIO_PUPD_PULLDOWN;
        break;
   default:
        break;
   }

	  gpio_mode_set(index->gpio_periph, pin_mode, pull_up_down, index->pin);
    gpio_output_options_set(index->gpio_periph, otype, GPIO_OSPEED_50MHZ, index->pin);
	 
}
Exemple #25
0
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    /* NVIC config */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE0_SUB4);
    nvic_irq_enable(LVD_IRQn,0,0);
    /* clock enable */
    rcu_periph_clock_enable(RCU_PMU);
    /* led1 config */
    gd_eval_ledinit (LED1);
    /* led1 turn on */
    gd_eval_ledon(LED1);
    /* EXTI_16 config */
    exti_init(EXTI_16, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    /* configure the lvd threshold to 2.9v(gd32f130_150) or 4.5v(gd32f170_190), and enable the lvd */
    pmu_lvd_select(PMU_LVDT_7);
          
    while(1);
}
Exemple #26
0
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{	  
    /* systick config */
    systick_config();
    /* led config */
    led_config();
    /* clock enable */
    rcu_periph_clock_enable(RCU_PMU);
    /* wakeup key config */
    gd_eval_keyinit(KEY_WAKEUP,KEY_MODE_GPIO);
    /* tamper key EXTI config */
    gd_eval_keyinit(KEY_TAMPER,KEY_MODE_EXTI);
    /* press wakeup key to enter deepsleep mode and use tamper key generate a exti interrupt to wakeup mcu */
    while(1){
        if(RESET == gpio_input_bit_get(WAKEUP_KEY_GPIO_PORT,WAKEUP_KEY_PIN))
            pmu_to_deepsleepmode(PMU_LDO_LOWPOWER,WFI_CMD);
    }
}
Exemple #27
0
/** \brief init and register gd32 spi bus.
 *
 * \param SPI: gd32 SPI, e.g: SPI1,SPI2,SPI3.
 * \param spi_bus_name: spi bus name, e.g: "spi1"
 * \return
 *
 */
rt_err_t gd32_spi_bus_register(uint32_t spi_periph,
                            //struct gd32_spi_bus * gd32_spi,
                            const char * spi_bus_name)
{
    int i;
    
    RT_ASSERT(spi_bus_name != RT_NULL);
    
    for (i = 0; i < ARR_LEN(spis); i++)
    {
        if (spi_periph == spis[i].spi_periph)
        {
            rcu_periph_clock_enable(spis[i].spi_clk);
            spis[i].spi_bus->parent.user_data = (void *)&spis[i];
            rt_spi_bus_register(spis[i].spi_bus, spi_bus_name, &gd32_spi_ops);
            return RT_EOK;
        }
    }
    
    return RT_ERROR;
}
Exemple #28
0
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    /* clock enable */
    rcu_periph_clock_enable(RCU_PMU);
    /* led configuration and turn on all led */
    led_config();
    gd_eval_ledon(LED1);
    gd_eval_ledon(LED3);
    gd_eval_ledon(LED2);
    gd_eval_ledon(LED4);
    /* tamper key configuration */
    gd_eval_keyinit(KEY_TAMPER,KEY_MODE_GPIO);
    /* wakeup pin enable */
    pmu_wakeup_pin_enable(PMU_WAKEUP_PIN0);
    /* press tamper key to enter standby mode and use wakeup key to wakeup mcu */
    while(1){
        if(RESET == gpio_input_bit_get(TAMPER_KEY_GPIO_PORT,TAMPER_KEY_PIN))
            pmu_to_stdbymode(WFI_CMD);
    }

}
Exemple #29
0
/**
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void timer_config(void)
{
    /* TIMER0 DMA transfer example -------------------------------------------------
    TIMER0CLK = 72MHz, prescaler = 72 
    TIMER0 counter clock = systemcoreclock/72 = 1MHz.

    the objective is to configure TIMER0 channel 1 to generate PWM signal with a
    frequency equal to 1KHz and a variable duty cycle(25%,50%,75%) that is 
    changed by the DMA after a specific number of update DMA request.

    the number of this repetitive requests is defined by the TIMER0 repetition counter,
    each 2 update requests, the TIMER0 channel 0 duty cycle changes to the next new 
    value defined by the buffer . 
    -----------------------------------------------------------------------------*/
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER0);

    timer_deinit(TIMER0);

    /* TIMER0 configuration */
    timer_initpara.timer_prescaler         = 71;
    timer_initpara.timer_alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.timer_counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.timer_period            = 999;
    timer_initpara.timer_clockrivision     = TIMER_CKDIV_DIV1;
    timer_initpara.timer_repetitioncounter = 0;
    timer_init(TIMER0,&timer_initpara);

    timer_ocintpara.timer_outputState  = TIMER_CCX_ENABLE;
    timer_ocintpara.timer_outputnState = TIMER_CCXN_ENABLE;
    timer_ocintpara.timer_ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.timer_ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.timer_ocidleState  = TIMER_OC_IDLE_STATE_HIGH;
    timer_ocintpara.timer_ocnidleState = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_3,&timer_ocintpara);

    /* CH0 configuration in PWM1 mode */
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,buffer[0]);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    /* CH1 configuration in PWM1 mode */
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,buffer[0]);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /* CH2 configuration in PWM1 mode */
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,buffer[0]);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

    /* CH3 configuration in PWM1 mode */
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,buffer[0]);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

    /* TIMER0 primary output enable */
    timer_primary_output_config(TIMER0,ENABLE);

    /* TIMER0 update DMA request enable */
    timer_DMA_transfer_config(TIMER0,TIMER_DMACFG_DMATA_CH0CV,TIMER_DMACFG_DMATC_4TRANSFER);
    timer_DMA_enable(TIMER0,TIMER_DMA_UPDEN);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER0);

    /* TIMER0 counter enable */
    timer_enable(TIMER0);
}
Exemple #30
0
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    int i=0;

    /* TSI peripheral and GPIOB periph clock enable */
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_TSI);

    /* PB0   TSI_CHCFG_G2P1     SAMPCAP
       PB1   TSI_CHCFG_G2P2     CHANNEL
       PB2   TSI_CHCFG_G2P3     CHANNEL */

    /* configure the GPIO ports */
    gpio_config();

    /* configure the TSI peripheral */
    tsi_config();

    /* configure the LED */
    led_config();

    /* reference cycle value acquisition and processing */
    for(i=0;i<20;i++){
        /* get charge transfer complete cycle number of group2 pin2 */
        tsi_transfer_pin(TSI_CHCFG_G2P2);
        
        /* check the TSI flag:end of acquisition interrupt */
        if(tsi_interrupt_flag_get(TSI_INTF_CTCF) == (uint8_t)SET){
            /* get charge transfer complete cycle number */
            sample_refnum_array2[i] = tsi_group2_cycle_get();
        }

        /* disable the selected pin as channel pin:pin2 */
        tsi_channel_pin_disable(TSI_CHCFG_G2P2);

        /* get charge transfer complete cycle number of group2 pin3 */
        tsi_transfer_pin(TSI_CHCFG_G2P3);
        if(tsi_interrupt_flag_get(TSI_INTF_CTCF) == (uint8_t)SET){
            sample_refnum_array3[i] = tsi_group2_cycle_get();
        }
        tsi_channel_pin_disable(TSI_CHCFG_G2P3);
        /* delay for a period of time while all banks have been acquired */
        delay(0xFFFF);
    }

    /* sum of sample_refnum_array */
    for(i=1;i<20;i++){
        sample_refnum[0] += sample_refnum_array2[i];
        sample_refnum[1] += sample_refnum_array3[i];
    }

    /* average channel cycle value are obtained */
    sample_refnum[0] = sample_refnum[0]/19;
    sample_refnum[1] = sample_refnum[1]/19;

    while (1){
        /* acquisition group2 pin2 */
        tsi_transfer_pin(TSI_CHCFG_G2P2);
     
        /* check the TSI flag end of acquisition interrupt */
        if(tsi_interrupt_flag_get(TSI_INTF_CTCF) == (uint8_t)SET){
            /* get charge transfer complete cycle number */
            samplenum[0] = tsi_group2_cycle_get();
        }

        /* light LED1 */
        if((sample_refnum[0]-samplenum[0]) > THRESHOLD1){
            /* group2 pin2 is touched */
            gd_eval_ledon(LED1);
        }else{
            gd_eval_ledoff(LED1);
        }
        tsi_channel_pin_disable(TSI_CHCFG_G2P2);

        /* acquisition group2 pin3 */
        tsi_transfer_pin(TSI_CHCFG_G2P3);
        if(tsi_interrupt_flag_get(TSI_INTF_CTCF) == (uint8_t)SET){
            samplenum[1] = tsi_group2_cycle_get();
        }

        /* light LED2 */
        if((sample_refnum[1]-samplenum[1]) > THRESHOLD2){
            /* group2 pin3 is touched */
            gd_eval_ledon(LED2);
        }else{
            gd_eval_ledoff(LED2);
        }
        tsi_channel_pin_disable(TSI_CHCFG_G2P3);

        /* delay for a period of time while all banks have been acquired */
        delay(0xFFFF);
    }
}