📄 bsp.c
字号:
/****************************************************************************** Product: Game example* Last Updated for Version: 4.0.00* Date of the Last Update: Apr 07, 2008** Q u a n t u m L e a P s* ---------------------------* innovating embedded systems** Copyright (C) 2002-2008 Quantum Leaps, LLC. All rights reserved.** This software may be distributed and modified under the terms of the GNU* General Public License version 2 (GPL) as published by the Free Software* Foundation and appearing in the file GPL.TXT included in the packaging of* this file. Please note that GPL Section 2[b] requires that all works based* on this software must also be made publicly available under the terms of* the GPL ("Copyleft").** Alternatively, this software may be distributed and modified under the* terms of Quantum Leaps commercial licenses, which expressly supersede* the GPL and are specifically designed for licensees interested in* retaining the proprietary status of their code.** Contact information:* Quantum Leaps Web site: http://www.quantum-leaps.com* e-mail: info@quantum-leaps.com*****************************************************************************/#include "qp_port.h"#include "game.h"#include "bsp.h"#include "hw_ints.h"#include "hw_memmap.h"#include "hw_nvic.h"#include "src/adc.h"#include "src/gpio.h"#include "src/interrupt.h"#include "src/timer.h"#include "src/sysctl.h"#include "src/systick.h"Q_DEFINE_THIS_FILE/* Local-scope objects -----------------------------------------------------*/#define PUSH_BUTTON GPIO_PIN_4#define USER_LED GPIO_PIN_5#ifdef Q_SPY#include "hw_uart.h"#include "src/uart.h"QSTimeCtr QS_tickTime_;QSTimeCtr QS_tickPeriod_;#define UART_TXFIFO_DEPTH 16#endif/*..........................................................................*/void ISR_SysTick(void) { static QEvent const tickEvt = { TIME_TICK_SIG, 0 };#ifdef Q_SPY uint32_t dummy = HWREG(NVIC_ST_CTRL); /* clear NVIC_ST_CTRL_COUNT flag */ QS_tickTime_ += QS_tickPeriod_; /* account for the clock rollover */#endif QF_tick(); /* process all armed time events */ QF_publish(&tickEvt); /* publish the tick event to all subscribers */}/*..........................................................................*/void ISR_ADC(void) { static uint32_t adcLPS = 0; /* Low-Pass-Filtered ADC reading */ static uint32_t wheel = 0; /* the last wheel position */ static uint32_t btn_debounced = 0; static uint8_t debounce_state = 0; unsigned long tmp; ADCIntClear(ADC_BASE, 3); /* clear the ADC interrupt */ ADCSequenceDataGet(ADC_BASE, 3, &tmp); /* read the data from the ADC */ /* 1st order low-pass filter: time constant ~= 2^n samples * TF = (1/2^n)/(z-((2^n - 1)/2^n)), * eg, n=3, y(k+1) = y(k) - y(k)/8 + x(k)/8 => y += (x - y)/8 */ adcLPS += (((int)tmp - (int)adcLPS + 4) >> 3); /* Low-Pass-Filter */ /* compute the next position of the wheel */ tmp = (((1 << 10) - adcLPS)*(BSP_SCREEN_HEIGHT - 2)) >> 10; if (tmp != wheel) { /* did the wheel position change? */ ObjectPosEvt *ope = Q_NEW(ObjectPosEvt, PLAYER_SHIP_MOVE_SIG); ope->x = (uint8_t)GAME_SHIP_X; /* x-position is fixed */ ope->y = (uint8_t)tmp; QActive_postFIFO(AO_Ship, (QEvent *)ope); /* post to the Ship */ wheel = tmp; /* save the last position of the wheel */ } tmp = GPIOPinRead(GPIO_PORTC_BASE, PUSH_BUTTON); /* read the push btn */ switch (debounce_state) { case 0: if (tmp != btn_debounced) { debounce_state = 1; /* transition to the next state */ } break; case 1: if (tmp != btn_debounced) { debounce_state = 2; /* transition to the next state */ } else { debounce_state = 0; /* transition back to state 0 */ } break; case 2: if (tmp != btn_debounced) { debounce_state = 3; /* transition to the next state */ } else { debounce_state = 0; /* transition back to state 0 */ } break; case 3: if (tmp != btn_debounced) { btn_debounced = tmp; /* save the debounced button value */ if (tmp == 0) { /* is the button depressed? */ static QEvent const fireEvt = { PLAYER_TRIGGER_SIG, 0 }; QF_publish(&fireEvt); /* publish to all subscribers */ } } debounce_state = 0; /* transition back to state 0 */ break; }}/*..........................................................................*/void ISR_Nmi(void) { for (;;) { /* sit in an infinite loop */ }}/*..........................................................................*/void ISR_Fault(void) { for (;;) { /* sit in an infinite loop */ }}/*..........................................................................*/void ISR_DefaultHandler(void) { for (;;) { /* sit in an infinite loop */ }}/*..........................................................................*/void BSP_init(int argc, char *argv[]) { (void)argc; /* unused: avoid the complier warning */ (void)argv; /* unused: avoid the compiler warning */ /* Set the clocking to run at 20MHz from the PLL. */ SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ); /* Enable the peripherals used by the application. */ SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); /* Configure the ADC to sample the potentiometer when the timer expires. * After sampling, the ADC will interrupt the processor; this is used as * the system time tick. */ ADCSequenceConfigure(ADC_BASE, 3, ADC_TRIGGER_TIMER, 0); ADCSequenceStepConfigure(ADC_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END); ADCSequenceEnable(ADC_BASE, 3); /* Configure the timer to generate triggers to the ADC to sample the * potentiometer. */ TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_PER); TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet() / 120); TimerControlStall(TIMER1_BASE, TIMER_A, true); TimerControlTrigger(TIMER1_BASE, TIMER_A, true); /* Configure the LED, push button, and UART GPIOs as required. */ GPIODirModeSet(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW); GPIODirModeSet(GPIO_PORTC_BASE, PUSH_BUTTON, GPIO_DIR_MODE_IN); GPIODirModeSet(GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_OUT); GPIOPinWrite(GPIO_PORTC_BASE, USER_LED, 0); /* Initialize the OSRAM OLED display. */ OSRAMInit(1); if (QS_INIT((void *)0) == 0) { /* initialize the QS software tracing */ Q_ERROR(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -