170 likes | 364 Views
Intro to MicroControllers : Stellaris Launchpad. Class 2: API Initialization & Use. Topics for Today. Direct Register Access vs Software Driver Model System Level vs Device Level Examples code for relevant devices. Direct Register Access. Total Control but more tedious inc /*
E N D
Intro to MicroControllers : Stellaris Launchpad Class 2: API Initialization & Use
Topics for Today • Direct Register Access vs Software Driver Model • System Level vs Device Level • Examples code for relevant devices
Direct Register Access • Total Control but more tedious • inc /* • Header files for DRA (inc/lm4fh5qr.h) • *_R files used to access register • *_M used as mask • *_S used to shift
Software Driver Model • Rapid development • Simple Plain English Interface • Slightly less control (sometimes)
Layout of Program #includes . . . //#defines & Global Variables . . . //Other Functions (interrupt handlers, subfunctions, etc) . . . int main(void){ //system level init //peripheral init . . . //peripheral use }
System Level Init //EnableFloating Point FPULazyStackingEnable(); //Set the clockspeed SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); IntMasterEnable();
Device Init //Config Enable System Peripheral (Pin Base) Set Pad Config (aka pin mux) Set Direction of Data //Use Read Write
Interruptsregister handler, then enable, clear flag in interrupt handler // Interrupt Init IntMasterEnable(); //System Level IntEnable(INT_GPIOA); //Device Level IntPrioritySet(INT_GPIOA, 0x00); //inverse priority // Interrupt functions IntPendClear(INT_GPIOA); //remove pending interrupt from que IntDisable(INT_GPIOA); IntMasterDisable(); You can register / unregister interrupts on the fly, but it does so by loading the NVIC Table into RAM, I would advise against this and make your interrupts static at compile time. The NVIC can be found in the startup_rvmdk.c file in the project root.
Devices • GPIO • Timers • Hibernate • I2C • UART • SSI (aka SPI) • ADC
GPIO8 pins per port //GPIO Init SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_1); GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2); GPIOPinConfigure(); //GPIO Functions GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, GPIO_PIN_1); GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_1, GPIO_PIN_2);
UART //UART Init #include "utils/uartstdio.c" SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, 115200, SysCtlClockGet()); //UART functions UARTprintf(“text to print”);
Timers //Timer Init TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); //32bit periodic TimerLoadSet(TIMER0_BASE, TIMER_A, LengthOfTime); //duration for timer TimerEnable(TIMER0_BASE, TIMER_A); //Timer functions usually used as timer interrupts NOTE: LengthOfTime is in system ticks, to calculate real time use this equation Realtime=(1/ClockSpeed)*LengthOfTime
Hibernate32 bit RTC, 15bit sub second timer, battery backed memory Hibernate is complicated, go look at http://www.ti.com/lit/ug/spmu019o/spmu019o.pdf Page 197 for examples.
Analog to Digital Converter (ADC) // ADC Init ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0); ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH0 ); ADCSequenceEnable(ADC0_BASE, 0); // ADC functions int ulValue; ADCProcessorTrigger(ADC0_BASE, 0); //trigger sample while(!ADCIntStatus(ADC0_BASE, 0, false)){} //wait until sampling complete ADCSequenceDataGet(ADC0_BASE, 0, &ulValue); //get data
SSI // SSI Init SSIConfigSetExpClk(SSI_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE0, SSI_MODE_MASTER, 2000000, 8); SSIEnable(SSI_BASE); // SSI functions SSIDataGet(SSI_BASE, &data); SSIDataPut(SSI_Base,data);
I2C // Initialize Master and Slave I2CMasterInitExpClk(I2C_MASTER_BASE, SysCtlClockGet(), true); // Specify slave address I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x3B, false); // Place the character to be sent in the data register I2CMasterDataPut(I2C_MASTER_BASE, ’Q’); // Initiate send of character from Master to Slave I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); // Delay until transmission completes while(I2CMasterBusBusy(I2C_MASTER_BASE)) { }