-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCANGPIO.c
170 lines (146 loc) · 6.05 KB
/
CANGPIO.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* File: CANGPIO.c
* Authors: Jaden Bottemiller, Benton Kwong, Dylan Tomberlin.
* Organization: Husky Robotics Team
*
* This file includes fuction implementations for the GPIO board CAN Communication
* using the Hindsight CAN Communication standard.
* Documentation: https://huskyroboticsteam.slite.com/app/channels/iU0BryG7M9/collections/aXvWTcIR6c/notes/4otlSFsSp2
*/
//#include "CANPacket.h"
#include "CANGPIO.h"
//Set PWM Frequency Packet
//Assembles a CAN Packet to set GPIO PWM Frequency
// Inputs: CANPacket pointer to assemble packet
// targetGroup & targetSerial - for CAN ID
// pwmChannel - board specific PWM channel
// frequency - frequency in Hz
void AssembleGPIOSetPWMFrequencyPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t pwmChannel,
uint16_t frequency){
packetToAssemble->id = ConstructCANID(PACKET_PRIORITY_NORMAL, targetDeviceGroup, targetDeviceSerial);
packetToAssemble->dlc = DLC_GPIO_PWM_FREQ;
int nextByte = WritePacketIDOnly(packetToAssemble->data, ID_GPIO_PWM_FREQ);
packetToAssemble->data[nextByte++] = pwmChannel;
PackIntIntoDataMSBFirst(packetToAssemble->data, frequency, nextByte); //consider making a 16bit version of this function?
}
//returns GPIO PWM Channel
//accepts packet to return data from
uint8_t GetGPIOPWMChannelFromPacket(CANPacket *packet){
return packet->data[1];
}
//returns GPIO PWM frequency
//accepts packet to return data from
uint16_t GetGPIOPWMFrequencyFromPacket(CANPacket *packet){
return DecodeBytesToIntMSBFirst(packet->data, 2, 3);
}
//Set PWM Duty Cycle
//Assembles a CAN Packet to set GPIO PWM Duty Cycle
// Inputs: CANPacket pointer to assemble packet
// targetGroup & targetSerial - for CAN ID
// pwmChannel - board specific PWM channel
// dutyCycle - duty cycle resolution
void AssembleGPIOSetPWMDutyCyclePacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t pwmChannel,
uint16_t dutyCycle){
packetToAssemble->id = ConstructCANID(PACKET_PRIORITY_NORMAL, targetDeviceGroup, targetDeviceSerial);
packetToAssemble->dlc = DLC_GPIO_PWM_DUTY_CYCLE;
int nextByte = WritePacketIDOnly(packetToAssemble->data, ID_GPIO_PWM_DUTY_CYCLE);
packetToAssemble->data[nextByte++] = pwmChannel;
PackIntIntoDataMSBFirst(packetToAssemble->data, dutyCycle, nextByte);
}
//returns GPIO PWM duty cycle
//accepts packet to return data from
uint16_t GetGPIOPWMDutyCycle(CANPacket *packetToAssemble){
return DecodeBytesToIntMSBFirst(packetToAssemble->data, 2, 3);
}
//Set ADC State
//Assembles a CAN Packet to set GPIO ADC
// Inputs: CANPacket pointer to assemble packet
// targetGroup & targetSerial - for CAN ID
// ADCChannel - board specific ADC channel
// state - bool (1 enable 0 disable)
void AssembleGPIOSetADCStateConfiguration(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t ADCChannel,
uint8_t state){
packetToAssemble->id = ConstructCANID(PACKET_PRIORITY_NORMAL, targetDeviceGroup, targetDeviceSerial);
packetToAssemble->dlc = DLC_GPIO_ADC_STATE;
int nextByte = WritePacketIDOnly(packetToAssemble->data, ID_GPIO_ADC_STATE);
packetToAssemble->data[nextByte++] = ADCChannel;
packetToAssemble->data[nextByte] = state;
}
//returns GPIO ADC channel
//accepts packet to return data from
uint8_t GetGPIOADCChannelFromPacket(CANPacket *packet){
return packet->data[1];
}
//returns GPIO ADC state
//accepts packet to return data from
uint8_t GetGPIOADCStateFromPacket(CANPacket *packet){
return packet->data[2];
}
//Set GPIO Configuration
//Assembles a CAN Packet to set GPIO Config
// Inputs: CANPacket pointer to assemble packet
// targetGroup & targetSerial - for CAN ID
// GPIORegister - GPIO Register
// GPIO bit number - (number is the bit which is being set)
// GPIO bit state - off, in, out, in/out, adc, pwm
void AssembleGPIOSetConfigurationPacket(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t GPIORegister,
uint8_t bitNumber,
uint8_t bitConfig){
packetToAssemble->id = ConstructCANID(PACKET_PRIORITY_NORMAL, targetDeviceGroup, targetDeviceSerial);
packetToAssemble->dlc = DLC_GPIO_CONFIG;
int nextByte = WritePacketIDOnly(packetToAssemble->data, ID_GPIO_CONFIG);
packetToAssemble->data[nextByte++] = GPIORegister;
packetToAssemble->data[nextByte++] = bitNumber;
packetToAssemble->data[nextByte] = bitConfig;
}
//returns GPIO register
//accepts packet to return data from
uint8_t GetGPIORegisterFromPacket(CANPacket *packet){
return packet->data[1];
}
//returns GPIO bit number
//accepts packet to return data from
uint8_t GetGPIOBitNumberFromPacket(CANPacket *packet){
return packet->data[2];
}
//returns GPIO bit config
//accepts packet to return data from
uint8_t GetGPIOBitConfigFromPacket(CANPacket *packet){
return packet->data[3];
}
//GPIO Write
//Assembles a CAN Packet to set GPIO Config
// Inputs: CANPacket pointer to assemble packet
// targetGroup & targetSerial - for CAN ID
// GPIORegister - GPIO Register
// GPIO bit number - (number is the bit which is being set)
// GPIO bit writevalues - off, on, flip
void AssembleGPIOWrite(CANPacket *packetToAssemble,
uint8_t targetDeviceGroup,
uint8_t targetDeviceSerial,
uint8_t GPIORegister,
uint8_t bitNumber,
uint8_t bitWriteValue){
packetToAssemble->id = ConstructCANID(PACKET_PRIORITY_NORMAL, targetDeviceGroup, targetDeviceSerial);
packetToAssemble->dlc = DLC_GPIO_WRITE;
int nextByte = WritePacketIDOnly(packetToAssemble->data, ID_GPIO_WRITE);
packetToAssemble->data[nextByte++] = GPIORegister;
packetToAssemble->data[nextByte++] = bitNumber;
packetToAssemble->data[nextByte] = bitWriteValue;
}
//returns GPIO write values
//accepts packet to return data from
uint8_t GetGPIOWriteValuesFromPacket(CANPacket *packet){
return packet->data[3];
}