📄 bsp.c
字号:
/*
*********************************************************************************************************
* SELECT COMMAND OR DATA REGISTER
*
* Description : This function manipulates the Register Select control line to the LCD controller.
*
* Arguments : none
*********************************************************************************************************
*/
#ifdef DISP_MODULE_PRESENT
void DispSel (CPU_INT08U sel)
{
if (sel == DISP_SEL_CMD_REG) {
GPIO_WriteBit(GPIO2, GPIO_PIN_LCD_RS, Bit_RESET); /* Select the command register (RS low) */
} else {
GPIO_WriteBit(GPIO2, GPIO_PIN_LCD_RS, Bit_SET); /* Select the data register (RS high) */
}
}
#endif
/*
*********************************************************************************************************
* WRITE DATA TO DISPLAY DEVICE
*
* Description : This function sends a single BYTE to the display device.
*
* Arguments : data is the BYTE to send to the display device
*
* Returns : none
*********************************************************************************************************
*/
#ifdef DISP_MODULE_PRESENT
void DispDataWr (CPU_INT08U data)
{
CPU_INT32U value;
GPIO_WriteBit(GPIO2, GPIO_PIN_LCD_E, Bit_SET); /* Set E high */
value = GPIO_Read(GPIO2);
value = (value & GPIO_MASK_LCD_DATA) | (GPIO_PIN_LCD_DATA(data));
GPIO_Write(GPIO2, value);
DispDly_uS(10);
GPIO_WriteBit(GPIO2, GPIO_PIN_LCD_E, Bit_RESET); /* Set E low */
}
#endif
/*
*********************************************************************************************************
* DELAY
*
* Description : This function is called to delay for the specified number of microseconds.
*
* Arguments : us Number of microseconds
*
* Returns : none
*********************************************************************************************************
*/
#ifdef DISP_MODULE_PRESENT
void DispDly_uS (CPU_INT32U us)
{
CPU_INT32U us_per_tick;
CPU_INT32U ticks;
us_per_tick = BSP_CPU_ClkFreq() / OS_TICKS_PER_SEC;
ticks = us / us_per_tick + 1;
OSTimeDly(ticks);
}
#endif
/*
*********************************************************************************************************
* INTIALIZE SYSTEM CLOCKS
*
* Description : Initialize the system clocks
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
static void MRCC_Init (void)
{
MRCC_DeInit(); /* Reset the MRCC system */
MRCC_WaitForOSC4MStartUp(); /* Wait for the OSC4M to start */
/* Set AHB clock (HCLK): */
MRCC_HCLKConfig(MRCC_CKSYS_Div2); /* ... 1) Set HCLK = CKSYS / 2 = 32 MHz */
/* Set TIM clock (CK_TIM): */
MRCC_CKTIMConfig(MRCC_HCLK_Div1); /* ... 1) Set CK_TIM = HCLK = 32 MHz */
/* Set APB clock (PCLK): */
MRCC_PCLKConfig(MRCC_CKTIM_Div2); /* ... 1) Set PCLK = CKTIM / 2 = 16 MHz */
/* Setup PLL and system clock (CKSYS): */
/* ... 1) Set CKSYS = OSC4MPLL output */
MRCC_CKSYSConfig(MRCC_CKSYS_OSC4MPLL, MRCC_PLL_Mul_16); /* ... 2) Set OSC4MPLL = 16 * OSC4M = 64MHz */
MRCC_IOVoltageRangeConfig(MRCC_IOVoltageRange_3V3); /* Optimize GPIO pins for 3V3 operation */
}
/*
*********************************************************************************************************
* INITIALIZE I/Os
*
* Description : This function initializes the GPIO pins used by the application.
*
* Arguments : none
*********************************************************************************************************
*/
static void IO_Init (void)
{
MRCC_PeripheralClockConfig(MRCC_Peripheral_GPIO, ENABLE); /* Enable GPIO clock */
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_GPIO, DISABLE);/* Release GPIO reset */
}
/*
*********************************************************************************************************
* EXTERNAL INTERRUPT INITIALIZATION
*
* Description : The external interrupt is configured to respond to change in the push buttons. An
* interrupt will be generated on a rising edge.
*
* Arguments : isr is the ISR which will receive the interrupt.
*
* Returns : none
*********************************************************************************************************
*/
void EXTINT_Init (void (*isr)(void))
{
EXTIT_InitTypeDef extit_init_struct;
EIC_IRQInitTypeDef eic_irq_init_struct;
MRCC_PeripheralClockConfig(MRCC_Peripheral_EXTIT, ENABLE); /* Enable EXTINT clock */
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_EXTIT, DISABLE); /* Release EXTINT reset */
/* Clear any pending interrupts */
EXTIT_ClearITPendingBit(EXTIT_ITLine7 | EXTIT_ITLine8 |
EXTIT_ITLine9 | EXTIT_ITLine11 );
/* Define initialization structure: */
extit_init_struct.EXTIT_ITLine = EXTIT_ITLine7 | EXTIT_ITLine8 | /* ... Specify EXTIT lines to enable; */
EXTIT_ITLine9 | EXTIT_ITLine11;
extit_init_struct.EXTIT_ITTrigger = EXTIT_ITTrigger_Rising; /* ... Trigger on rising signal edge; */
extit_init_struct.EXTIT_ITLineCmd = ENABLE; /* ... Enable EXTIT. */
EXTIT_Init(&extit_init_struct); /* Initialize EXTIT */
/* Define IRQ initialization structure for EXTIT: */
eic_irq_init_struct.EIC_IRQChannel = EXTIT_IRQChannel; /* ... Specify EXTIT IRQ channel; */
eic_irq_init_struct.EIC_IRQChannelPriority = 2; /* ... Use priority of 2; */
eic_irq_init_struct.EIC_IRQChannelCmd = ENABLE; /* ... Enable IRQ. */
EIC_IRQInit(&eic_irq_init_struct); /* Initialize EXTIT IRQ */
BSP_VectSet(EXTIT_IRQChannel, isr); /* Set vector for EXTIT IRQ */
}
/*
*********************************************************************************************************
* TICKER INITIALIZATION
*
* Description : This function is called to initialize uC/OS-II's tick source. TIM0 is used.
*
* Arguments : none
*********************************************************************************************************
*/
static void Tmr_TickInit (void)
{
TIM_InitTypeDef tim_init_struct;
EIC_IRQInitTypeDef eic_irq_init_struct;
CPU_INT32U tim_clk_freq;
CPU_INT16U prescaler;
CPU_INT16U reload;
MRCC_PeripheralClockConfig(MRCC_Peripheral_TIM0, ENABLE); /* Enable TIM0 clock */
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM0, DISABLE);/* Release TIM0 reset */
/* Calculate prescaler and reload/period */
tim_clk_freq = BSP_TIM_ClkFreq();
prescaler = 1000;
reload = (tim_clk_freq / (prescaler + 1)) / OS_TICKS_PER_SEC;
/* Define TIM initialization structure for TIM0: */
tim_init_struct.TIM_Period = reload; /* ... Specify reload as the period; */
tim_init_struct.TIM_Prescaler = prescaler; /* ... Specify the prescaler; */
tim_init_struct.TIM_Mode = TIM_Mode_OCTiming; /* ... Use OCTiming mode; */
tim_init_struct.TIM_ClockSource = TIM_ClockSource_Internal; /* ... Use internal clock (CKTIM); */
tim_init_struct.TIM_CounterMode = TIM_CounterMode_Up; /* ... Use UP counter; */
tim_init_struct.TIM_Channel = TIM_Channel_2; /* ... Use channel 2 (OC2) */
TIM_Init(TIM0, &tim_init_struct); /* Initialize TIM TIM0 */
TIM_ClearFlag(TIM0, TIM_FLAG_OC1 | TIM_FLAG_OC2 | TIM_FLAG_Update); /* Clear any flags */
TIM_ITConfig(TIM0, TIM_IT_Update, ENABLE); /* Configure interrupt on Update event */
TIM_Cmd(TIM0, ENABLE); /* Enable TIM TIM0 */
/* Define IRQ initialization structure for TIM0: */
eic_irq_init_struct.EIC_IRQChannel = TIM0_UP_IRQChannel; /* ... Specify TIM0 Update channel; */
eic_irq_init_struct.EIC_IRQChannelPriority = 1; /* ... Use priority of 1; */
eic_irq_init_struct.EIC_IRQChannelCmd = ENABLE; /* ... Enable IRQ. */
EIC_IRQInit(&eic_irq_init_struct); /* Initialize TIM0 Update IRQ */
BSP_VectSet(TIM0_UP_IRQChannel, Tmr_TickISR_Handler); /* Set vector for TIM0 Update IRQ */
}
/*
*********************************************************************************************************
* TICK IRQ HANDLER
*
* Description : This function handles the timer interrupt that is used to generate TICKs for uC/OS-II.
*
* Arguments : none
*
* Note(s) : The TICK ISR uses Timer 0
*********************************************************************************************************
*/
void Tmr_TickISR_Handler (void)
{
OSTimeTick(); /* Call uC/OS-II's OSTimeTick() to signal a tick */
TIM_ClearITPendingBit(TIM0, TIM_IT_Update); /* Clear interrupt bit in TB register */
EIC->IPR = (CPU_INT32U)(1 << TIM0_UP_IRQChannel); /* Clear interrupt pending bit in EIC_IPR register */
}
/*
*********************************************************************************************************
* INITIALIZE TIMER FOR uC/OS-View
*
* Description : This function is called to by uC/OS-View to initialize the free running timer that is
* used to make time measurements.
*
* Arguments : none
*
* Returns : none
*
* Note(s) : (1) This function also configures the GPIO for the UART.
*********************************************************************************************************
*/
#if OS_VIEW_MODULE > 0
void OSView_TmrInit (void)
{
TB_InitTypeDef tb_init_struct;
GPIO_InitTypeDef gpio_init_struct;
CPU_INT16U prescaler;
CPU_INT16U reload;
MRCC_PeripheralClockConfig(MRCC_Peripheral_TB, ENABLE); /* Enable TB clock */
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TB, DISABLE); /* Release TB reset */
/* Calculate prescaler and autoreload values */
prescaler = 0;
reload = 0xFFFF;
/* Define TB initization structure: */
tb_init_struct.TB_Mode = TB_Mode_Timing; /* ... Use Timing mode; */
tb_init_struct.TB_CounterMode = TB_CounterMode_Down; /* ... Use DOWN counter; */
tb_init_struct.TB_Prescaler = prescaler; /* ... Specify prescaler; */
tb_init_struct.TB_AutoReload = reload; /* ... Specify autoreload. */
TB_Init(&tb_init_struct); /* Initialize TB */
TB_ITConfig(TB_IT_Update | TB_IT_GlobalUpdate | TB_IT_IC, DISABLE); /* Disable interrupts */
TB_Cmd(ENABLE); /* Enable TB */
/* Define GPIO initialization structure: */
gpio_init_struct.GPIO_Mode = GPIO_Mode_AF_PP; /* ... Use Alternate Function Push-Pull; */
gpio_init_struct.GPIO_Pin = GPIO_PIN_UART0_TX; /* ... On UART0 TX pin. */
GPIO_Init(GPIO0, &gpio_init_struct); /* Initialize on GPIO0. */
/* Define GPIO initialization structure: */
gpio_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* ... Use INput FLOATING; */
gpio_init_struct.GPIO_Pin = GPIO_PIN_UART0_RX; /* ... On UART0 RX pin. */
GPIO_Init(GPIO0, &gpio_init_struct); /* Initialize on GPIO0. */
}
#endif
/*
*********************************************************************************************************
* READ TIMER FOR uC/OS-View
*
* Description : This function is called to read the current counts of a 16-bit free running timer.
*
* Arguments : none
*
* Returns : A 16-bit value representing the current time
*********************************************************************************************************
*/
#if OS_VIEW_MODULE > 0
CPU_INT32U OSView_TmrRd (void)
{
if (OSRunning) {
return ((CPU_INT32U)(~TB_GetCounter()) & 0x0000FFFF);
} else {
return (0);
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -