Welcome to mirror list, hosted at ThFree Co, Russian Federation.

comm.c - github.com/ClusterM/famicom-dumper.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/comm.c
blob: ac344ea37ef617e93d80c7205f102cd5f4bf793e (plain)
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
#include "defines.h"
#include <inttypes.h>
#include <util/delay.h>
#include "comm.h"
#include "usart.h"
#include "crc.h"

static uint8_t comm_send_crc;     // CRC of outgoing packet with header
static uint16_t comm_send_length; // size of outgoing data
static uint16_t comm_send_pos;    // how many data sent by app

volatile uint8_t recv_buffer[RECV_BUFFER_SIZE];
static uint16_t comm_recv_pos;    // how many bytes of packet received
static uint8_t comm_recv_crc;
static uint8_t comm_recv_error;
volatile uint8_t comm_recv_command;
volatile uint16_t comm_recv_length;
volatile uint8_t comm_recv_done;

static void comm_send_and_calc(uint8_t data)
{
	comm_send_crc = calc_crc8(comm_send_crc, data);
	USART_TransmitByte(data);
#ifdef SEND_DELAY
	_delay_us(SEND_DELAY);
#endif
}

void comm_start(uint8_t command, unsigned int length)
{
	comm_send_crc = 0;
	comm_send_and_calc('F');
	comm_send_and_calc(command);
	comm_send_and_calc(length & 0xff);
	comm_send_and_calc((length >> 8) & 0xff);
	comm_send_length = length;
	comm_send_pos = 0;

	if (!comm_send_length)
		USART_TransmitByte(comm_send_crc);
}

void comm_send_byte(uint8_t data)
{
	comm_send_and_calc(data);
	comm_send_pos++;
	
	if (comm_send_pos == comm_send_length)
		USART_TransmitByte(comm_send_crc);
}

void comm_proceed(uint8_t data)
{
  if (comm_recv_error && data != 'F')
    return;
  comm_recv_error = 0;
  if (!comm_recv_pos)
  {
    comm_recv_crc = 0;
    comm_recv_done = 0;
  }
  comm_recv_crc = calc_crc8(comm_recv_crc, data);
  switch (comm_recv_pos)
  {
  case 0:
    {
      if (data != 'F')
      {
        comm_recv_error = 1;
        comm_start(COMMAND_ERROR_INVALID, 0);
      }
    }
    break;
  case 1:
    {
      comm_recv_command = data;
    }
    break;
  case 2:
    {
      comm_recv_length = data;
    }
    break;
  case 3:
    {
      comm_recv_length |= (uint16_t) data << 8;
    }
    break;
  default:
    {
      uint16_t pos = comm_recv_pos - 4;
      if (pos >= sizeof(recv_buffer))
      {
        comm_recv_pos = 0;
        comm_recv_error = 1;
        comm_start(COMMAND_ERROR_OVERFLOW, 0);
        return;
      } else if (pos < comm_recv_length)
      {
        recv_buffer[pos] = data;
      } else if (pos == comm_recv_length)
      {
        if (!comm_recv_crc)
        {
          comm_recv_done = 1;
        } else
        {
          comm_recv_error = 1;
          comm_start(COMMAND_ERROR_CRC, 0);
        }
        comm_recv_pos = 0;
        return;
      }
    }
    break;
  }
  comm_recv_pos++;
}

void comm_init()
{
	comm_recv_pos = 0;
	comm_recv_done = 0;	
	comm_recv_error = 0;
}