Пример #1
0
int SSDInit(void)
{
    uint32_t clock;
    uint32_t flash_clock = CLOCK_GetClockFrequency(kFlashClock, &clock);
    /* fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify) */     
    return Init(0x00000000, clock, 2);
}
uint8_t UART_QuickInit(uint32_t MAP, uint32_t baudrate)
{
    uint8_t i;
    uint32_t clock;
    UART_InitTypeDef UART_InitStruct1;
    QuickInit_Type * pq = (QuickInit_Type*)&(MAP);
    UART_InitStruct1.baudrate = baudrate;
    UART_InitStruct1.instance = pq->ip_instance;
    
    /* clock source */
    if(pq->ip_instance == HW_UART0)
    {
        CLOCK_GetClockFrequency(kMCGOutClock, &clock);
        
        /* use PLL/2 or FLL */
        SIM->SOPT2 &= ~SIM_SOPT2_UART0SRC_MASK;
        SIM->SOPT2 |= SIM_SOPT2_UART0SRC(1);  
	
        if(MCG->C6 & MCG_C6_PLLS_MASK) /* PLL */
        {
            SIM->SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK;
            clock /= 2;
        }
        else /* FLL */
        {
            SIM->SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_MASK;
        }
    }
    else
    {
        CLOCK_GetClockFrequency(kBusClock, &clock);
    }
    UART_InitStruct1.srcClock = clock;
    
    /* init pinmux */
    for(i = 0; i < pq->io_offset; i++)
    {
        PORT_PinMuxConfig(pq->io_instance, pq->io_base + i, (PORT_PinMux_Type) pq->mux); 
    }
    
    /* init UART */
    UART_Init(&UART_InitStruct1);
    
    return pq->ip_instance;
}
 /**
 * @brief  初始化SysTick时钟
 * @code
 *     // 初始化SysTick时钟 设定中断周期为10000us(10ms)
 *      SYSTICK_Init(10000);
 * @endcode
 * @param  timeInUs: 中断周期 单位us
 * @retval None
 */
void SYSTICK_Init(uint32_t timeInUs)
{
    /* Set clock source = core clock */
    SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; 
    CLOCK_GetClockFrequency(kCoreClock, &fac_us);
    fac_us /= 1000000;
    fac_ms = fac_us * 1000;
    /* every 100ms in a int */
    SysTick->LOAD = fac_us * timeInUs;
}
Пример #4
0
void PDB_QuickInit(PDB_TriggerSrc_Type triggerSrc, uint32_t timeInUs)
{
    uint32_t clock;
    PDB_InitTypeDef  PDB_InitStruct1;
    
    CLOCK_GetClockFrequency(kBusClock, &clock);

    PDB_InitStruct1.inputTrigSource = kPDB_SoftwareTrigger;  /* software Trigger */
    PDB_InitStruct1.isContinuesMode = true;
    PDB_InitStruct1.srcClock = clock;
    PDB_InitStruct1.timeInUs = timeInUs;
    PDB_Init(&PDB_InitStruct1);
}
Пример #5
0
/**
 * @brief  初始化I2C模块
 * @note 需要其它函数配合使用
 * @code
 *     //使用i2c的1模块 通信速度为48000hz
 *     I2C_InitTypeDef I2C_InitStruct1; //申请一个结构体
 *     I2C_InitStruct1.baudrate = HW_I2C1; //选择i2c的1模块
 *     I2C_InitStruct1.instance = 48000;   //设置通信速度48000
 *     I2C_Init(&I2C_InitStruct1);
 * @endcode
 * @param  I2C_InitStruct :i2c初始化配置结构体
 * @retval None
 */
void I2C_Init(I2C_InitTypeDef* I2C_InitStruct)
{
	/* param check */
    assert_param(IS_I2C_ALL_INSTANCE(I2C_InitStruct->instance));
    uint32_t freq;
    SIM->SCGC4 |= SIM_I2CClockGateTable[I2C_InitStruct->instance];
    /* disable first */
    I2C_InstanceTable[I2C_InitStruct->instance]->C1 &= ~I2C_C1_IICEN_MASK;
    /* set baudrate */
    CLOCK_GetClockFrequency(kBusClock, &freq);
    I2C_SetBaudrate(I2C_InitStruct->instance, freq, I2C_InitStruct->baudrate);
    /* enable i2c */
    I2C_InstanceTable[I2C_InitStruct->instance]->C1 |= I2C_C1_IICEN_MASK;
}
Пример #6
0
int main(void)
{
    uint32_t clock;
    DelayInit();

    GPIO_QuickInit(HW_GPIOD, 7, kGPIO_Mode_OPP);
    GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP);
    
    UART_QuickInit(UART0_RX_PA01_TX_PA02, 115200);
    
    printf("HelloWorld\r\n");
    
    CLOCK_GetClockFrequency(kCoreClock, &clock);
    printf("core clock:%dHz\r\n", clock);
    CLOCK_GetClockFrequency(kBusClock, &clock);
    printf("bus clock:%dHz\r\n", clock);
    
    while(1)
    {
        GPIO_ToggleBit(HW_GPIOD, 7);
        GPIO_ToggleBit(HW_GPIOE, 6);
        DelayMs(50);
    }
}
Пример #7
0
/**
 * @brief  SPI数据帧配置
 * @note   内部函数,用户无需调用
 * @retval None
 */
void SPI_FrameConfig(uint32_t instance, uint32_t ctar, SPI_FrameFormat_Type frameFormat, uint8_t dataSize, uint8_t bitOrder, uint32_t baudrate)
{
    uint32_t clock;
    /* data size */
    SPI_InstanceTable[instance]->CTAR[ctar] &= ~SPI_CTAR_FMSZ_MASK;
    SPI_InstanceTable[instance]->CTAR[ctar] |= SPI_CTAR_FMSZ(dataSize-1);
    /* bit order */
    switch(bitOrder)
    {
        case kSPI_MSBFirst:
            SPI_InstanceTable[instance]->CTAR[ctar] &= ~SPI_CTAR_LSBFE_MASK;
            break;
        case kSPI_LSBFirst:
            SPI_InstanceTable[instance]->CTAR[ctar] |= SPI_CTAR_LSBFE_MASK;
            break;
        default:
            break;
    }
    /* frame format */
    switch(frameFormat)
    {
        case kSPI_CPOL0_CPHA0:
            SPI_InstanceTable[instance]->CTAR[ctar] &= ~SPI_CTAR_CPOL_MASK;
            SPI_InstanceTable[instance]->CTAR[ctar] &= ~SPI_CTAR_CPHA_MASK;
            break;
        case kSPI_CPOL0_CPHA1:
            SPI_InstanceTable[instance]->CTAR[ctar] &= ~SPI_CTAR_CPOL_MASK;
            SPI_InstanceTable[instance]->CTAR[ctar] |= SPI_CTAR_CPHA_MASK;
            break;   
        case kSPI_CPOL1_CPHA0:
            SPI_InstanceTable[instance]->CTAR[ctar] |= SPI_CTAR_CPOL_MASK;
            SPI_InstanceTable[instance]->CTAR[ctar] &= ~SPI_CTAR_CPHA_MASK;
            break;  
        case kSPI_CPOL1_CPHA1:
            SPI_InstanceTable[instance]->CTAR[ctar] |= SPI_CTAR_CPOL_MASK;
            SPI_InstanceTable[instance]->CTAR[ctar] |= SPI_CTAR_CPHA_MASK;  
            break;  
        default:
            break;
    }
    /* set SPI clock, SPI use Busclock */
    CLOCK_GetClockFrequency(kBusClock, &clock);
    dspi_hal_set_baud(instance, ctar, baudrate, clock);
}
Пример #8
0
Файл: tpm.c Проект: zwzmzd/kl02z
/**
 * @brief  初始化PWM波产生
 * @code
 *      
 *  //TPM0_CH0_PE24 使用FTM0模块通道0 引脚E24上产生1KHz PWM波
 *   FTM_PWM_QuickInit(TPM0_CH0_PE24, 1000); 
 * @endcode         
 * @param  MAP            : 快速初始化通道
 * @param  frequencyInHZ  : 频率
 * @retval None
 */
uint8_t TPM_PWM_QuickInit(uint32_t MAP, TPM_PWM_Mode_Type mode, uint32_t req)
{
    uint8_t i;
    uint32_t modulo;
    uint32_t clock;
    int32_t pres;
    uint8_t ps = 0;
    QuickInit_Type * pq = (QuickInit_Type*)&(MAP); 
    /* init pinmux */
    for(i = 0; i < pq->io_offset; i++)
    {
        PORT_PinMuxConfig(pq->io_instance, pq->io_base + i, (PORT_PinMux_Type) pq->mux); 
    }
    /* calc req and ps */
    uint32_t min_val = 0xFFFF;
    /* cal ps */
    CLOCK_GetClockFrequency(kMCGOutClock, &clock);
//    if(MCG->C6 & MCG_C6_PLLS_MASK) /* PLL */
//    {
//        clock /= 2;
//    }
//    else /* FLL */
//    {
//        
//    }
    LIB_TRACE("%s %d\r\n", __func__, clock);
    pres = (clock/req)/TPM_MOD_MOD_MASK;
    if((clock/req)/pres > TPM_MOD_MOD_MASK)
    {
        pres++;
    }
    for(i = 0; i < 7; i++)
    {
        if((ABS(pres - (1<<i))) < min_val)
        {
            ps = i;
            min_val = ABS(pres - (1<<i));
        }
    }
    if(pres > (1<<ps)) ps++;
    if(ps > 7) ps = 7;
    modulo = ((clock/(1<<ps))/req) - 1;
    LIB_TRACE("input frequency:%dHz\r\n", req);
    LIB_TRACE("input clk:%d\r\n", clock);
    LIB_TRACE("ps:%d\r\n", pres);
    LIB_TRACE("modulo:%d\r\n", modulo);
    LIB_TRACE("ip_instance:%d\r\n", pq->ip_instance);
    LIB_TRACE("io_instance:%d\r\n", pq->io_instance);
    LIB_TRACE("io_base:%d\r\n", pq->io_base);
    LIB_TRACE("io_offset:%d\r\n", pq->io_offset);
    LIB_TRACE("chl:%d\r\n", pq->channel);
    _TPM_InitBasic(pq->ip_instance, modulo, (TPM_ClockDiv_Type)ps);
    /* set FTM mode */
    switch(mode)
    {
        case kPWM_EdgeAligned:
            TPM_SetMode(pq->ip_instance, pq->channel, kTPM_Mode_EdgeAligned);
            break;
        default:
            LIB_TRACE("error in FTM_PWM_Init\r\n");
            break;
    }
    /* set duty to 50% */
    TPM_PWM_ChangeDuty(pq->ip_instance, pq->channel, 5000);
    return pq->ip_instance;
}
Пример #9
0
Файл: spi.c Проект: zwzmzd/kl02z
/**
 * @brief  初始化SPI模块
 * @param  SPI_InitStruct :spi初始化配置结构体
 * @retval None
 */
void SPI_Init(SPI_InitTypeDef* SPI_InitStruct)
{
    uint32_t clock;
    /* enable clock gate */
    *(uint32_t*)SIM_SPIClockGateTable[SPI_InitStruct->instance].addr |= SIM_SPIClockGateTable[SPI_InitStruct->instance].mask;
    
    /* stop SPI */
    SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_SPE_MASK;
    
    /* master or slave */
    switch(SPI_InitStruct->mode)
    {
        case kSPI_Master:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_MSTR_MASK;
            break;
        case kSPI_Slave:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_MSTR_MASK;
            break;
        default:
            break;
    }
    
    /* bit order */
    switch(SPI_InitStruct->bitOrder)
    {
        case kSPI_MSBFirst:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_LSBFE_MASK;
            break;
        case kSPI_LSBFirst:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_LSBFE_MASK;
            break;
        default:
            break;
    }
    
    /* clock parity */
    switch(SPI_InitStruct->frameFormat)
    {
        case kSPI_CPOL0_CPHA0:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPHA_MASK;
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPOL_MASK;
            break;
        case kSPI_CPOL0_CPHA1:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 |=  SPI_C1_CPHA_MASK;
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPOL_MASK;
            break;
        case kSPI_CPOL1_CPHA0:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPHA_MASK;
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 |=  SPI_C1_CPOL_MASK;
            break;
        case kSPI_CPOL1_CPHA1:
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_CPHA_MASK;
            SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_CPOL_MASK;
            break;
        default:
            break;
    }
    
    /* config SS as PCS0 output */
    SPI_InstanceTable[SPI_InitStruct->instance]->C2 |= SPI_C2_MODFEN_MASK;
    SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_SSOE_MASK;
    
    /* baudrate */
    if(SPI_InitStruct->instance == HW_SPI0)
    {
        CLOCK_GetClockFrequency(kBusClock, &clock);
    }
    else
    {
        CLOCK_GetClockFrequency(kSystemClock, &clock);
    }
    SPI_HAL_SetBaud(SPI_InitStruct->instance, SPI_InitStruct->baudrate, clock);
    
    /* enable SPI */
    SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_SPE_MASK;
}