Skip to content

Commit

Permalink
abstract system functions
Browse files Browse the repository at this point in the history
  • Loading branch information
p-h-a-i-l committed Jul 7, 2019
1 parent dc96d64 commit 9383e49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
25 changes: 10 additions & 15 deletions machine_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@



// if not using from STM32, provide dummy functions from your project...
extern void (*HAL_Delay)(uint32_t Delay);
extern void (*HAL_NVIC_SystemReset)(void);
extern uint32_t (*HAL_GetTick)(void);



static unsigned char mpGetTxByte(MACHINE_PROTOCOL_TX_BUFFER *buf);
Expand Down Expand Up @@ -74,7 +69,7 @@ void protocol_byte(PROTOCOL_STAT *s, unsigned char byte ){
case PROTOCOL_STATE_IDLE:
if ((byte == PROTOCOL_SOM_ACK) || (byte == PROTOCOL_SOM_NOACK)){
s->curr_msg.SOM = byte;
s->last_char_time = HAL_GetTick();
s->last_char_time = protocol_GetTick();
s->state = PROTOCOL_STATE_WAIT_CI;
s->CS = 0;
} else {
Expand All @@ -85,29 +80,29 @@ void protocol_byte(PROTOCOL_STAT *s, unsigned char byte ){
ascii_byte(s, byte );
//////////////////////////////////////////////////////
} else {
s->last_char_time = HAL_GetTick();
s->last_char_time = protocol_GetTick();
s->state = PROTOCOL_STATE_BADCHAR;
}
}
break;

case PROTOCOL_STATE_WAIT_CI:
s->last_char_time = HAL_GetTick();
s->last_char_time = protocol_GetTick();
s->curr_msg.CI = byte;
s->CS += byte;
s->state = PROTOCOL_STATE_WAIT_LEN;
break;

case PROTOCOL_STATE_WAIT_LEN:
s->last_char_time = HAL_GetTick();
s->last_char_time = protocol_GetTick();
s->curr_msg.len = byte;
s->count = 0;
s->CS += byte;
s->state = PROTOCOL_STATE_WAIT_END;
break;

case PROTOCOL_STATE_WAIT_END:
s->last_char_time = HAL_GetTick();
s->last_char_time = protocol_GetTick();
s->curr_msg.bytes[s->count++] = byte;
s->CS += byte;

Expand Down Expand Up @@ -147,7 +142,7 @@ void protocol_byte(PROTOCOL_STAT *s, unsigned char byte ){
if (s->ack.retries > 0){
s->ack.counters.txRetries++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->ack.last_send_time = HAL_GetTick();
s->ack.last_send_time = protocol_GetTick();
s->ack.retries--;
} else {
s->send_state = PROTOCOL_ACK_TX_IDLE;
Expand Down Expand Up @@ -328,7 +323,7 @@ int protocol_send(PROTOCOL_STAT *s, PROTOCOL_MSG2 *msg){
s->ack.counters.tx++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->send_state = PROTOCOL_ACK_TX_WAITING;
s->ack.last_send_time = HAL_GetTick();
s->ack.last_send_time = protocol_GetTick();
s->ack.retries = 2;
return 0;

Expand All @@ -355,7 +350,7 @@ int protocol_send(PROTOCOL_STAT *s, PROTOCOL_MSG2 *msg){
s->ack.counters.tx++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->send_state = PROTOCOL_ACK_TX_WAITING;
s->ack.last_send_time = HAL_GetTick();
s->ack.last_send_time = protocol_GetTick();
s->ack.retries = 2;
return 0;

Expand Down Expand Up @@ -398,7 +393,7 @@ void protocol_send_raw(int (*send_serial_data)( unsigned char *data, int len ),
// called regularly from main.c
// externed from protocol.h
void protocol_tick(PROTOCOL_STAT *s){
s->last_tick_time = HAL_GetTick();
s->last_tick_time = protocol_GetTick();


if(s->send_state == PROTOCOL_ACK_TX_WAITING) {
Expand All @@ -407,7 +402,7 @@ void protocol_tick(PROTOCOL_STAT *s){
if (s->ack.retries > 0){
s->ack.counters.txRetries++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->ack.last_send_time = HAL_GetTick();
s->ack.last_send_time = protocol_GetTick();
s->ack.retries--;
} else {
// if we run out of retries, then try to send a next message
Expand Down
21 changes: 15 additions & 6 deletions protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@
#include <stdlib.h>


// if not using from STM32, provide dummy functions from your project...
extern void (*HAL_Delay)(uint32_t Delay);
extern void (*HAL_NVIC_SystemReset)(void);
extern uint32_t (*HAL_GetTick)(void);
///////////////////////////////////////////////////////
// Function Pointers to system functions
//////////////////////////////////////////////////////////

// Need to be assigned to functions "real" system fucntions
uint32_t noTick(void) { return 0; };
uint32_t (*protocol_GetTick)() = noTick;

void noDelay(uint32_t Delay) {};
void (*protocol_Delay)(uint32_t Delay) = noDelay;

void noReset(void) {};
void (*protocol_SystemReset)() = noReset;



Expand Down Expand Up @@ -514,8 +523,8 @@ void protocol_process_message(PROTOCOL_STAT *s, PROTOCOL_MSG2 *msg) {

case PROTOCOL_CMD_REBOOT:
//protocol_send_ack(); // we no longer ack from here
HAL_Delay(500);
HAL_NVIC_SystemReset();
protocol_Delay(500);
protocol_SystemReset();
break;

case PROTOCOL_CMD_TEST:
Expand Down
8 changes: 8 additions & 0 deletions protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ extern PARAMSTAT *params[256];



///////////////////////////////////////////////////////
// Function Pointers to system functions
//////////////////////////////////////////////////////////

// Need to be assigned to functions "real" system fucntions
extern void (*protocol_Delay)(uint32_t Delay);
extern void (*protocol_SystemReset)(void);
extern uint32_t (*protocol_GetTick)(void);


#ifdef __cplusplus
Expand Down

0 comments on commit 9383e49

Please sign in to comment.