Skip to content

Commit ecb43e7

Browse files
committed
Cleanup
1 parent 4af2665 commit ecb43e7

File tree

8 files changed

+184
-166
lines changed

8 files changed

+184
-166
lines changed

applications/app.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void app_set_configuration(app_configuration *conf) {
5151

5252
app_ppm_stop();
5353
app_adc_stop();
54-
app_uartcomm_stop(0);
54+
app_uartcomm_stop(UART_PORT_COMM_HEADER);
5555
app_nunchuk_stop();
5656
app_balance_stop();
5757
app_pas_stop();
@@ -84,19 +84,19 @@ void app_set_configuration(app_configuration *conf) {
8484

8585
case APP_UART:
8686
hw_stop_i2c();
87-
app_uartcomm_start(0);
87+
app_uartcomm_start(UART_PORT_COMM_HEADER);
8888
break;
8989

9090
case APP_PPM_UART:
9191
hw_stop_i2c();
9292
app_ppm_start();
93-
app_uartcomm_start(0);
93+
app_uartcomm_start(UART_PORT_COMM_HEADER);
9494
break;
9595

9696
case APP_ADC_UART:
9797
hw_stop_i2c();
9898
app_adc_start(false);
99-
app_uartcomm_start(0);
99+
app_uartcomm_start(UART_PORT_COMM_HEADER);
100100
break;
101101

102102
case APP_NUNCHUK:
@@ -107,7 +107,7 @@ void app_set_configuration(app_configuration *conf) {
107107
app_balance_start();
108108
if(appconf.imu_conf.type == IMU_TYPE_INTERNAL){
109109
hw_stop_i2c();
110-
app_uartcomm_start(0);
110+
app_uartcomm_start(UART_PORT_COMM_HEADER);
111111
}
112112
break;
113113

@@ -141,8 +141,8 @@ void app_set_configuration(app_configuration *conf) {
141141
app_ppm_configure(&appconf.app_ppm_conf);
142142
app_adc_configure(&appconf.app_adc_conf);
143143
app_pas_configure(&appconf.app_pas_conf);
144-
app_uartcomm_configure(appconf.app_uart_baudrate, true, 0);
145-
app_uartcomm_configure(0, appconf.permanent_uart_enabled, 1);
144+
app_uartcomm_configure(appconf.app_uart_baudrate, true, UART_PORT_COMM_HEADER);
145+
app_uartcomm_configure(0, appconf.permanent_uart_enabled, UART_PORT_BUILTIN);
146146
app_nunchuk_configure(&appconf.app_chuk_conf);
147147

148148
#ifdef APP_CUSTOM_TO_USE

applications/app.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ float app_adc_get_voltage(void);
4343
float app_adc_get_decoded_level2(void);
4444
float app_adc_get_voltage2(void);
4545

46+
typedef enum {
47+
UART_PORT_COMM_HEADER = 0,
48+
UART_PORT_BUILTIN,
49+
UART_PORT_EXTRA_HEADER
50+
} UART_PORT;
51+
4652
void app_uartcomm_initialize(void);
47-
void app_uartcomm_start(unsigned int port_number);
48-
void app_uartcomm_stop(unsigned int port_number);
49-
void app_uartcomm_configure(uint32_t baudrate, bool permanent_enabled, unsigned int port_number);
50-
void app_uartcomm_send_packet(unsigned char *data, unsigned int len, unsigned int port_number);
51-
void app_uartcomm_send_packet_p(unsigned char *data, unsigned int len);
53+
void app_uartcomm_start(UART_PORT port_number);
54+
void app_uartcomm_stop(UART_PORT port_number);
55+
void app_uartcomm_configure(uint32_t baudrate, bool permanent_enabled, UART_PORT port_number);
56+
void app_uartcomm_send_packet(unsigned char *data, unsigned int len, UART_PORT port_number);
5257

5358
void app_nunchuk_start(void);
5459
void app_nunchuk_stop(void);

applications/app_uartcomm.c

+39-24
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ static volatile bool uart_is_running[UART_NUMBER] = {false};
5050
static mutex_t send_mutex[UART_NUMBER];
5151
static bool send_mutex_init_done[UART_NUMBER] = {false};
5252
static SerialConfig uart_cfg[UART_NUMBER] = {{
53-
BAUDRATE,
54-
0,
55-
USART_CR2_LINEN,
56-
0
53+
BAUDRATE,
54+
0,
55+
USART_CR2_LINEN,
56+
0
5757
}};
5858

5959
// Different for Rx and Tx because it is possible for hardware to use different UART driver
@@ -67,48 +67,51 @@ static PACKET_STATE_t packet_state[UART_NUMBER];
6767

6868
// Private functions
6969
static void process_packet(unsigned char *data, unsigned int len, unsigned int port_number);
70-
static void write_packet(unsigned char *data, unsigned int , unsigned int port_number);
70+
static void write_packet(unsigned char *data, unsigned int len, unsigned int port_number);
7171

7272
static void process_packet_1(unsigned char *data, unsigned int len) {process_packet(data,len,0);}
7373
static void write_packet_1(unsigned char *data, unsigned int len) {write_packet(data,len,0);}
74-
static void send_packet_1(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,0);}
74+
static void send_packet_1(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,UART_PORT_COMM_HEADER);}
7575

7676
static void process_packet_2(unsigned char *data, unsigned int len) {process_packet(data,len,1);}
7777
static void write_packet_2(unsigned char *data, unsigned int len) {write_packet(data,len,1);}
78-
static void send_packet_2(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,1);}
78+
static void send_packet_2(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,UART_PORT_BUILTIN);}
7979

8080
static void process_packet_3(unsigned char *data, unsigned int len) {process_packet(data,len,2);}
8181
static void write_packet_3(unsigned char *data, unsigned int len) {write_packet(data,len,2);}
82-
static void send_packet_3(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,2);}
82+
static void send_packet_3(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,UART_PORT_EXTRA_HEADER);}
8383

8484
typedef void (*data_func) (unsigned char *data, unsigned int len);
8585
static data_func write_functions[3] = {write_packet_1, write_packet_2, write_packet_3};
8686
static data_func process_functions[3] = {process_packet_1, process_packet_2, process_packet_3};
8787
static data_func send_functions[3] = {send_packet_1, send_packet_2, send_packet_3};
8888

8989
static void write_packet(unsigned char *data, unsigned int len, unsigned int port_number) {
90-
if(port_number >= UART_NUMBER){
90+
if (port_number >= UART_NUMBER) {
9191
return;
9292
}
93+
9394
if (uart_is_running[port_number]) {
9495
sdWrite(serialPortDriverTx[port_number], data, len);
9596
}
9697
}
98+
9799
static void process_packet(unsigned char *data, unsigned int len, unsigned int port_number) {
98-
if(port_number >= UART_NUMBER){
100+
if (port_number >= UART_NUMBER) {
99101
return;
100102
}
103+
101104
commands_process_packet(data, len, send_functions[port_number]);
102105
}
103106

104-
105107
void app_uartcomm_initialize(void) {
106108
serialPortDriverTx[0] = &HW_UART_DEV;
107109
serialPortDriverRx[0] = &HW_UART_DEV;
108110
uart_cfg[0].speed = BAUDRATE;
109111
RxGpioPort[0] = HW_UART_RX_PORT; RxGpioPin[0] = HW_UART_RX_PIN;
110112
TxGpioPort[0] = HW_UART_TX_PORT; TxGpioPin[0] = HW_UART_TX_PIN;
111113
gpioAF[0] = HW_UART_GPIO_AF;
114+
112115
#ifdef HW_UART_P_DEV
113116
#ifdef HW_UART_P_DEV_TX
114117
serialPortDriverTx[1] = &HW_UART_P_DEV_TX;
@@ -121,6 +124,7 @@ void app_uartcomm_initialize(void) {
121124
TxGpioPort[1] = HW_UART_P_TX_PORT; TxGpioPin[1] = HW_UART_P_TX_PIN;
122125
gpioAF[1] = HW_UART_P_GPIO_AF;
123126
#endif
127+
124128
#ifdef HW_UART_3_DEV
125129
serialPortDriverTx[2] = &HW_UART_3_DEV;
126130
serialPortDriverRx[2] = &HW_UART_3_DEV;
@@ -131,32 +135,34 @@ void app_uartcomm_initialize(void) {
131135
#endif
132136
}
133137

134-
void app_uartcomm_start(unsigned int port_number) {
138+
void app_uartcomm_start(UART_PORT port_number) {
135139
if(port_number >= UART_NUMBER){
136140
return;
137141
}
138142

139143
packet_init(write_functions[port_number], process_functions[port_number], &packet_state[port_number]);
144+
140145
if (!thread_is_running) {
141146
chThdCreateStatic(packet_process_thread_wa, sizeof(packet_process_thread_wa),
142-
NORMALPRIO, packet_process_thread, NULL);
147+
NORMALPRIO, packet_process_thread, NULL);
143148
thread_is_running = true;
144149
}
150+
145151
sdStart(serialPortDriverRx[port_number], &uart_cfg[port_number]);
146152
sdStart(serialPortDriverTx[port_number], &uart_cfg[port_number]);
147153
uart_is_running[port_number] = true;
154+
148155
palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
149-
PAL_STM32_OSPEED_HIGHEST |
150-
PAL_STM32_PUDR_PULLUP);
156+
PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP);
151157
palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
152-
PAL_STM32_OSPEED_HIGHEST |
153-
PAL_STM32_PUDR_PULLUP);
158+
PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP);
154159
}
155160

156-
void app_uartcomm_stop(unsigned int port_number) {
157-
if(port_number >= UART_NUMBER){
161+
void app_uartcomm_stop(UART_PORT port_number) {
162+
if(port_number >= UART_NUMBER) {
158163
return;
159164
}
165+
160166
if (uart_is_running[port_number]) {
161167
sdStop(serialPortDriverRx[port_number]);
162168
sdStop(serialPortDriverTx[port_number]);
@@ -167,29 +173,34 @@ void app_uartcomm_stop(unsigned int port_number) {
167173
// Notice that the processing thread is kept running in case this call is made from it.
168174
}
169175

170-
void app_uartcomm_send_packet(unsigned char *data, unsigned int len, unsigned int port_number) {
171-
if(port_number >= UART_NUMBER){
176+
void app_uartcomm_send_packet(unsigned char *data, unsigned int len, UART_PORT port_number) {
177+
if (port_number >= UART_NUMBER) {
172178
return;
173179
}
180+
174181
if (!send_mutex_init_done[port_number]) {
175182
chMtxObjectInit(&send_mutex[port_number]);
176183
send_mutex_init_done[port_number] = true;
177184
}
185+
178186
chMtxLock(&send_mutex[port_number]);
179187
packet_send_packet(data, len, &packet_state[port_number]);
180188
chMtxUnlock(&send_mutex[port_number]);
181189
}
182190

183-
void app_uartcomm_configure(uint32_t baudrate, bool enabled, unsigned int port_number) {
184-
if(port_number >= UART_NUMBER){
191+
void app_uartcomm_configure(uint32_t baudrate, bool enabled, UART_PORT port_number) {
192+
if (port_number >= UART_NUMBER) {
185193
return;
186194
}
187-
if(baudrate>0){
195+
196+
if (baudrate > 0) {
188197
uart_cfg[port_number].speed = baudrate;
189198
}
199+
190200
if (thread_is_running && uart_is_running[port_number]) {
191201
sdStart(serialPortDriverRx[port_number], &uart_cfg[port_number]);
192202
}
203+
193204
if (enabled) {
194205
palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
195206
PAL_STM32_OSPEED_HIGHEST |
@@ -205,13 +216,17 @@ void app_uartcomm_configure(uint32_t baudrate, bool enabled, unsigned int port_n
205216

206217
static THD_FUNCTION(packet_process_thread, arg) {
207218
(void)arg;
219+
208220
chRegSetThreadName("uartcomm proc");
221+
209222
event_listener_t el[UART_NUMBER];
210223
for(int port_number = 0; port_number < UART_NUMBER; port_number++) {
211224
chEvtRegisterMaskWithFlags(&(*serialPortDriverRx[port_number]).event, &el[port_number], EVENT_MASK(0), CHN_INPUT_AVAILABLE);
212225
}
226+
213227
for(;;) {
214228
chEvtWaitAnyTimeout(ALL_EVENTS, ST2MS(10));
229+
215230
bool rx = true;
216231
while (rx) {
217232
rx = false;

commands.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1450,9 +1450,9 @@ void commands_set_ble_name(char* name) {
14501450
buffer[ind++] = '\0';
14511451

14521452
#ifdef HW_UART_P_DEV
1453-
app_uartcomm_send_packet(buffer, ind, 1);
1453+
app_uartcomm_send_packet(buffer, ind, UART_PORT_BUILTIN);
14541454
#else
1455-
app_uartcomm_send_packet(buffer, ind, 0);
1455+
app_uartcomm_send_packet(buffer, ind, UART_PORT_COMM_HEADER);
14561456
#endif
14571457
}
14581458

@@ -1469,9 +1469,9 @@ void commands_set_ble_pin(char* pin) {
14691469
ind += pin_len;
14701470
buffer[ind++] = '\0';
14711471
#ifdef HW_UART_P_DEV
1472-
app_uartcomm_send_packet(buffer, ind, 1);
1472+
app_uartcomm_send_packet(buffer, ind, UART_PORT_BUILTIN);
14731473
#else
1474-
app_uartcomm_send_packet(buffer, ind, 0);
1474+
app_uartcomm_send_packet(buffer, ind, UART_PORT_COMM_HEADER);
14751475
#endif
14761476
}
14771477

conf_general.h

+7-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define FW_VERSION_MAJOR 5
2525
#define FW_VERSION_MINOR 03
2626
// Set to 0 for building a release and iterate during beta test builds
27-
#define FW_TEST_VERSION_NUMBER 1
27+
#define FW_TEST_VERSION_NUMBER 2
2828

2929
#include "datatypes.h"
3030

@@ -72,10 +72,10 @@
7272
// Mark3 version of HW60 with power switch and separate NRF UART.
7373
//#define HW60_IS_MK3
7474
//#define HW60_IS_MK4
75-
//#define HW60_IS_MK5
75+
#define HW60_IS_MK5
7676

77-
//#define HW_SOURCE "hw_60.c"
78-
//#define HW_HEADER "hw_60.h"
77+
#define HW_SOURCE "hw_60.c"
78+
#define HW_HEADER "hw_60.h"
7979

8080
//#define HW_SOURCE "hw_r2.c"
8181
//#define HW_HEADER "hw_r2.h"
@@ -148,10 +148,9 @@
148148
//#define HW_SOURCE "hw_stormcore_100d.c"
149149
//#define HW_HEADER "hw_stormcore_100d.h"
150150

151-
#define HW_VER_IS_60D_PLUS
152-
#define HW_SOURCE "hw_stormcore_60d.c"
153-
#define HW_HEADER "hw_stormcore_60d.h"
154-
151+
//#define HW_VER_IS_60D_PLUS
152+
//#define HW_SOURCE "hw_stormcore_60d.c"
153+
//#define HW_HEADER "hw_stormcore_60d.h"
155154

156155
//#define HW_SOURCE "hw_stormcore_100s.c"
157156
//#define HW_HEADER "hw_stormcore_100s.h"
@@ -188,18 +187,13 @@
188187
/*
189188
* Select default user motor configuration
190189
*/
191-
//#include "mcconf_sten.h"
192-
//#include "mcconf_sp_540kv.h"
193-
//#include "mcconf_castle_2028.h"
194-
//#include "mcconf_ellwee.h"
195190
//#include "conf_test.h"
196191

197192
/*
198193
* Select default user app configuration
199194
*/
200195
//#include "appconf_example_ppm.h"
201196
//#include "appconf_custom.h"
202-
//#include "appconf_ellwee.h"
203197

204198
/*
205199
* Set APP_CUSTOM_TO_USE to the name of the main C file of the custom application.

0 commit comments

Comments
 (0)