UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
eps.c
Go to the documentation of this file.
1 
10 #include "board.h"
11 #include "../uart.h"
12 #include "../rtc.h"
13 #include "../eps.h"
14 
15 #define EPS_BAUDRATE 57600
16 
17 #define EPS_REG_CONFIG 0x00
18 #define EPS_REG_STATUS 0x01
19 #define EPS_REG_SW_ON 0x02
20 #define EPS_REG_POWER 0x03
21 #define EPS_REG_BAT_V 0x04
22 #define EPS_REG_BAT_I 0x05
23 #define EPS_REG_BAT_T 0x06
24 #define EPS_REG_SOLAR_N1_I 0x07
25 #define EPS_REG_SOLAR_N2_I 0x08
26 #define EPS_REG_SOLAR_S1_I 0x09
27 #define EPS_REG_SOLAR_S2_I 0x0A
28 #define EPS_REG_SOLAR_E1_I 0x0B
29 #define EPS_REG_SOLAR_E2_I 0x0C
30 #define EPS_REG_SOLAR_W1_I 0x0D
31 #define EPS_REG_SOLAR_W2_I 0x0E
32 #define EPS_REG_SOLAR_T1_I 0x0F
33 #define EPS_REG_SOLAR_T2_I 0x10
34 #define EPS_REG_RESERVED 0x11 // Maybe make known ID? ( https://github.com/uos3/eps-firmware/issues/1 )
35 #define EPS_REG_SOLAR_2_V 0x12
36 #define EPS_REG_RAIL1_BOOT 0x13
37 #define EPS_REG_RAIL1_FAIL 0x14
38 #define EPS_REG_RAIL1_V 0x15
39 #define EPS_REG_RAIL1_I 0x16
40 #define EPS_REG_RAIL2_BOOT 0x17
41 #define EPS_REG_RAIL2_FAIL 0x18
42 #define EPS_REG_RAIL2_V 0x19
43 #define EPS_REG_RAIL2_I 0x1A
44 #define EPS_REG_RAIL3_BOOT 0x1B
45 #define EPS_REG_RAIL3_FAIL 0x1C
46 #define EPS_REG_RAIL3_V 0x1D
47 #define EPS_REG_RAIL3_I 0x1E
48 #define EPS_REG_RAIL4_BOOT 0x1F
49 #define EPS_REG_RAIL4_FAIL 0x20
50 #define EPS_REG_RAIL4_V 0x21
51 #define EPS_REG_RAIL4_I 0x22
52 #define EPS_REG_RAIL5_BOOT 0x23
53 #define EPS_REG_RAIL5_FAIL 0x24
54 #define EPS_REG_RAIL5_V 0x25
55 #define EPS_REG_RAIL5_I 0x26
56 #define EPS_REG_RAIL6_BOOT 0x27
57 #define EPS_REG_RAIL6_FAIL 0x28
58 #define EPS_REG_RAIL6_V 0x29
59 #define EPS_REG_RAIL6_I 0x2A
60 #define EPS_REG_SUPPLY_3_V 0x2B
61 #define EPS_REG_SUPPLY_3_I 0x2C
62 #define EPS_REG_SUPPLY_5_V 0x2D
63 #define EPS_REG_SUPPLY_5_I 0x2E
64 #define EPS_REG_CHARGE_I 0x2F
65 #define EPS_REG_MPPT_BUS_V 0x30
66 #define EPS_REG_CRC_ER_CNT 0x31
67 #define EPS_REG_DROP_CNT 0x32
68 
69 typedef struct eps_master_packet_t {
70  bool write:1;
71  uint8_t register_id:7;
72  uint8_t value_l;
73  uint8_t value_h;
74  uint8_t crc_l;
75  uint8_t crc_h;
77 
78 typedef struct eps_slave_packet_single_t {
79  bool reserved_a:1;
80  uint8_t register_id:7;
81  bool reserved_b:1;
82  uint8_t register_quantity:7;
83  uint16_t value;
84  uint16_t crc;
86 
87 static eps_master_packet_t eps_master_packet;
88 static eps_slave_packet_single_t eps_slave_packet_single;
89 
90 
91 static bool EPS_readRegister(uint8_t register_id, eps_slave_packet_single_t *data);
92 static uint16_t EPS_crc(uint8_t *message, int32_t offset, int32_t length);
93 
94 
95 void EPS_init(void)
96 {
97  UART_init(UART_EPS, EPS_BAUDRATE);
98 }
99 
100 bool EPS_selfTest(void)
101 {
102  /* Ref: https://github.com/uos3/eps-firmware/issues/1 */
103  //EPS_readRegister(EPS_REG_RESERVED, &eps_slave_packet_single);
104  //return (eps_slave_packet_single.value == EPS_ID);
105 
106  return true;
107 }
108 
109 bool EPS_getBatteryVoltage(uint16_t *voltage)
110 {
111  if(EPS_readRegister(EPS_REG_BAT_V, &eps_slave_packet_single))
112  {
113  *voltage = eps_slave_packet_single.value;
114  return true;
115  }
116  return false;
117 }
118 
119 static bool EPS_readRegister(uint8_t register_id, eps_slave_packet_single_t *data)
120 {
121  char c;
122  uint16_t crc, bytes_expected, bytes_received;
123  uint32_t timeout, current_time;
124 
125  /* Construct Master Read Packet */
126  eps_master_packet.write = false;
127  eps_master_packet.register_id = (uint8_t)(register_id & 0x7F);
128  eps_master_packet.value_l = 1;
129  eps_master_packet.value_h = 0;
130  crc = EPS_crc((uint8_t *)&eps_master_packet, 0, 3);
131  eps_master_packet.crc_l = (uint8_t)crc & 0xFF;
132  eps_master_packet.crc_h = (uint8_t)((crc >> 8) & 0xFF);
133 
134  /* Send Master Read Packet */
135  UART_putb(UART_EPS, (char *)&eps_master_packet, 5);
136 
137  bytes_expected = 6;
138  bytes_received = 0;
139  RTC_getTime(&current_time);
140  timeout = current_time;
141  while(current_time < (timeout + 2))
142  {
143  if(UART_getc_nonblocking(UART_EPS, &c))
144  {
145  ((char *)(data))[bytes_received] = c;
146  if(++bytes_received == bytes_expected)
147  {
148  break;
149  }
150  }
151  RTC_getTime(&current_time);
152  }
153  if(bytes_received != bytes_expected)
154  {
155  return false;
156  }
157 
158  crc = EPS_crc((uint8_t *)data, 0, 4);
159  if(crc != data->crc)
160  {
161  return false;
162  }
163 
164  return true;
165 }
166 
167 /* Implementation copied from eps-firmware/PowerMcu/PowerMcu/util/crc.c */
168 static uint16_t EPS_crc(uint8_t *message, int32_t offset, int32_t length)
169 {
170  uint16_t crcFull = 0xFFFF;
171  int32_t i, j;
172 
173  for (i = offset; i < length; i++)
174  {
175  crcFull = (uint16_t)(crcFull ^ message[i]);
176 
177  for (j = 0; j < 8; j++)
178  {
179  uint8_t crcLsb = (uint8_t)(crcFull & 0x0001);
180  crcFull = (uint16_t)((crcFull >> 1) & 0x7FFF);
181 
182  if (crcLsb == 1)
183  crcFull = (uint16_t)(crcFull ^ 0xA001);
184  }
185  }
186  return crcFull;
187 }
188 
void RTC_getTime(uint32_t *time)
Definition: rtc.c:20
void UART_init(uint8_t uart_num, uint32_t baudrate)
Definition: uart.c:78
bool UART_getc_nonblocking(uint8_t uart_num, char *c)
Definition: uart.c:145
bool EPS_getBatteryVoltage(uint16_t *voltage)
Definition: eps.c:109
void UART_putb(uint8_t uart_num, char *str, uint32_t len)
Definition: uart.c:137
bool EPS_selfTest(void)
Definition: eps.c:100
void EPS_init(void)
Definition: eps.c:95