collab/mm20

git clone https://git.julia.blue/collab/mm20

Allen JiangIR LED tested and fixed8b79c2e

firmware
3.3 KiB140 linesraw
1#include "launchpad.h"
2#include "tivaware/gpio.h"
3#include "tivaware/hw_ints.h"
4#include "tivaware/hw_memmap.h"
5#include "tivaware/rom.h"
6#include "tivaware/sysctl.h"
7#include "tivaware/timer.h"
8#include "driverlib/adc.h"
9#include "driverlib/debug.h"
10#include "driverlib/fpu.h"
11#include "driverlib/gpio.h"
12#include "driverlib/pin_map.h"
13#include "driverlib/sysctl.h"
14#include <math.h>
15#include <stdint.h>
16#include <stdio.h>
17/* XDCtools Header files */
18#include <xdc/std.h>
19#include <xdc/runtime/System.h>
20
21/* BIOS Header files */
22#include <ti/sysbios/BIOS.h>
23#include <ti/sysbios/knl/Task.h>
24
25/* TI-RTOS Header files */
26#include <ti/drivers/GPIO.h>
27#include <ti/drivers/PWM.h>
28
29/* Example/Board Header files */
30#include "Board.h"
31#include "v_tm4c123gh6pm.h"
32
33#define TASKSTACKSIZE   1024
34
35extern void disable_interrupts(void);
36extern void enable_interrupts(void);
37
38volatile uint32_t delay_timer;
39
40void systick_handler(void) {
41    --delay_timer;
42}
43
44void wait_ms(uint32_t t) {
45
46}
47
48void blink_success(void) {
49    led_write(GREEN_LED, true);
50    Task_sleep(250);
51    led_write(GREEN_LED, false);
52}
53
54void timer0_init() {
55    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
56    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
57    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, 800000); // 100Hz
58    ROM_IntEnable(INT_TIMER0A);
59    ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
60    ROM_TimerEnable(TIMER0_BASE, TIMER_BOTH);
61}
62
63void timer0a_handler(void) {
64    ROM_TimerIntClear(TIMER0_BASE, TIMER_A);
65    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7,
66                     ~ROM_GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_7));
67}
68
69void LEDtask(UArg arg0, UArg arg1)
70{
71    while (1) {
72        blink_success();
73        printf("left: %d\t\tright:%d\n\r", left_switch(), right_switch());
74        Task_sleep(1000);
75    }
76}
77
78void Task_WallDetector(UArg arg0, UArg arg1) {
79    //  cycle through all 4 sensors
80
81    //  init
82    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
83
84    /*
85     *  Test circuit Pinout
86     *  Emitter On/Off Gate: PD0
87     *  Receiver Voltage Level: PD1
88     */
89    while(1)
90    {
91    //  1. turn on emitter
92    GPIO_PORTD_DATA_R = GPIO_PORTD_DATA_R | 0x0001;
93    //  2. wait for emitter to fully turn on
94    Task_sleep(1);
95    //  3. read receiver voltage using ADC
96
97    uint32_t result;
98    ADC0_PSSI_R = 0x0008;
99    while((ADC0_RIS_R&0x08)==0){};
100    result = ADC0_SSFIFO3_R&0xFFF;
101    ADC0_ISC_R = 0x0008;
102    System_printf("%d\n",result);
103    System_flush();
104
105    //  4. turn off emitter
106    GPIO_PORTD_DATA_R = GPIO_PORTD_DATA_R & 0xFFFE;
107
108    Task_sleep(1000);
109    }
110}
111
112Task_Struct tsk0Struct;
113Task_Struct tsk1Struct;
114UInt8 tsk0Stack[TASKSTACKSIZE];
115UInt8 tsk1Stack[TASKSTACKSIZE];
116
117int main(void) {
118    ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
119                       SYSCTL_OSC_MAIN);
120    ROM_SysTickEnable();
121    launchpad_init();
122
123    Board_initGeneral();
124    Board_initPWM();
125    Board_initGPIO();
126
127    timer0_init();
128
129    //  Task Initialization
130    Task_Params tskParams;
131    Task_Params_init(&tskParams);
132    tskParams.stackSize = TASKSTACKSIZE;
133    tskParams.stack = &tsk0Stack;
134    tskParams.arg0 = 0;
135
136//    Task_construct(&tsk0Struct, (Task_FuncPtr)LEDtask, &tskParams, NULL);
137    Task_construct(&tsk1Struct, (Task_FuncPtr)Task_WallDetector, &tskParams, NULL);
138
139    BIOS_start();   //  This must be the very last line!
140}