forked from TMaxElectronics/UD3-Fibernet-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED.c
146 lines (120 loc) · 3.65 KB
/
LED.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* File: LED.c
* Author: Thorb
*
* Created on October 19, 2020, 9:20 PM
*/
#include <xc.h>
#include <stdint.h>
#include "FreeRTOSConfig.h"
#include "FreeRTOS.h"
#include "LED.h"
#include "FreeRTOS/TCPIP/include/FreeRTOS_Sockets.h"
#include "include/System.h"
#define LED_BLINK_TIME 10
int32_t minLEDTime = 0;
int32_t ethLEDTime = 0;
int32_t errLEDTime = 0;
unsigned ethError = 1;
unsigned ethReady = 0;
const uint8_t LED_generalExceptionCode[] = {1,1,0,0xfe,0,0xff};
const uint8_t LED_stackOverflowCode[] = {0,0,0,0xfe,1,1,1,0xff};
const uint8_t LED_mallocFailedCode[] = {1,1,0xfe,0,0,1,0,0xff};
const uint8_t LED_assertCode[] = {0, 1, 0xfe, 0, 0, 0, 0xfe, 0, 0, 0, 0xff};
void LED_init(){
//IO
TRISACLR = _TRISA_TRISA2_MASK;
TRISBCLR = _TRISB_TRISB2_MASK | _TRISB_TRISB3_MASK;
//Set LED pins to open drain and turn off r,g and b
ODCASET = _ODCA_ODCA2_MASK;
ODCBSET = _ODCB_ODCB2_MASK | _ODCB_ODCB3_MASK;
LATASET = _LATA_LATA2_MASK;
LATBSET = _LATB_LATB2_MASK | _LATB_LATB3_MASK;
xTaskCreate(LED_task, "LED Task", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
}
//Handle led IO
void LED_task(void * params){
while(1){
if(--minLEDTime < 0){
minLEDTime = 0;
LATASET = _LATA_LATA2_MASK;
}
if(ethError){
LATBSET = _LATB_LATB2_MASK;
LATBCLR = _LATB_LATB3_MASK;
}else{
if(--ethLEDTime < 0){
ethLEDTime = 0;
if(ethReady){
LATBCLR = _LATB_LATB2_MASK;
}else{
LATBSET = _LATB_LATB2_MASK;
}
}
if(--errLEDTime < 0){
errLEDTime = 0;
LATBSET = _LATB_LATB3_MASK;
}else{
LATASET = _LATA_LATA2_MASK;
LATBSET = _LATB_LATB2_MASK;
}
}
vTaskDelay(pdMS_TO_TICKS(5));
}
}
void LED_ethLinkStateChangeHook(unsigned newState){
ethError = !newState;
}
void LED_ethDHCPStateChangeHook(unsigned newState){
ethReady = newState;
}
void LED_ethPacketReceivedHook(){
ethLEDTime = LED_BLINK_TIME;
LATBINV = _LATB_LATB2_MASK;
}
void LED_minPacketReceivedHook(){
minLEDTime = LED_BLINK_TIME;
LATAINV = _LATA_LATA2_MASK;
}
void LED_errorFlashHook(){
errLEDTime = LED_BLINK_TIME;
LATBCLR = _LATB_LATB3_MASK;
LATASET = _LATA_LATA2_MASK;
LATBSET = _LATB_LATB2_MASK;
}
void LED_showCode(uint8_t * code){
uint8_t currentPos = 0;
//turn off all LEDs
LATASET = _LATA_LATA2_MASK;
LATBSET = _LATB_LATB2_MASK;
LATBSET = _LATB_LATB3_MASK;
//allows the debugger to skip out of this loop if desired
unsigned cont = 1;
while(cont){
switch(code[currentPos]){
case 0: //short flash
LATBCLR = _LATB_LATB3_MASK;
SYS_waitCP0(LED_CODE_0_TIME);
LATBSET = _LATB_LATB3_MASK;
break;
case 1: //long flash
LATBCLR = _LATB_LATB3_MASK;
SYS_waitCP0(LED_CODE_1_TIME);
LATBSET = _LATB_LATB3_MASK;
break;
case 0xfe: //inter-character pause
SYS_waitCP0(LED_CODE_IC_PAUSE_TIME);
currentPos ++;
continue;
case 0xff: //sequence end
SYS_waitCP0(LED_CODE_END_TIME);
currentPos = 0;
continue;
default:
currentPos = 0;
continue;
}
currentPos ++;
SYS_waitCP0(LED_CODE_IB_PAUSE_TIME);
}
}