This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comms.c
99 lines (82 loc) · 2.7 KB
/
Comms.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
#include "osObjects.h" // RTOS object definitions
#include "comms.h"
#include "MKL46Z4.h" // Device header
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "commands.h"
#include "bluetoothDMA.h"
/******************************** Thread: 'Comms' **************************/
osThreadId tid_comms = 0; // thread id
/**
@brief Comms thread initialization function
*/
int Init_comms (void) {
tid_comms = osThreadCreate (osThread(comms), NULL);
if(!tid_comms) return(-1);
return(0);
}
/**
@brief This function sends a message in the same way as using printf.
*/
void SendMessage(const char * fmt , ...){
if(tid_comms){
Message_t * Message;
va_list vl; // stores number and type of arguments
Message = osMailAlloc(qid_MessageTX, 0); // allocate memory for new message
if (Message != 0){
__disable_irq(); // disable interrupts
va_start(vl, fmt); // start agument iteration
vsnprintf( Message->msg, 100, fmt, vl); // add arguments to buffor based on format string
va_end( vl); // finish argument iteration
__enable_irq(); //enable interrupts
Message->msg[100] = '\0'; // Add null sign just in cass
osMailPut(qid_MessageTX, Message); // send Mail
/* trigger DMA1 interrupt if not busy */
if(!(DMA0->DMA[1].DSR_BCR & DMA_DSR_BCR_BSY_MASK)){
NVIC_SetPendingIRQ(DMA1_IRQn);
}
}
}
}
/**
@brief Get recieved sting from mail, pare and execute;
*/
void ParseAndExecute(void){
osEvent osEvt;
Message_t * Message;
cmd_t NewCommand;
while(1){
osEvt = osMailGet(qid_MessageRX,0);
if ( osEvt.status == osEventMail){
Message = osEvt.value.p;
NewCommand = parse_command(Message->msg);
osMailFree(qid_MessageRX,Message);
ExecuteCommand(&NewCommand);
} else {
break;
}
}
}
/**
@brief Comms main thread loop.
This function waits for external signal and executes appropriate function.
*/
void comms (void const *argument) {
osEvent SigEvt;
while (1) {
SigEvt = osSignalWait(0,osWaitForever);
if(SigEvt.value.signals & SIG_UART_DATA_RECIEVED){
ParseAndExecute();
}
if(SigEvt.value.signals & SIG_START_ZUMOAI){
Init_ZumoAI();
}
if(SigEvt.value.signals & SIG_KILL_ZUMOAI){
Kill_ZumoAI();
}
if(SigEvt.value.signals & SIG_START_DEBOUNCE_TIMER){
osTimerStart(tid_DebounceTimer,500);
}
}
}