forked from Polarisru/updiprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phy.c
112 lines (96 loc) · 2.52 KB
/
phy.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
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <unistd.h>
#include "com.h"
#include "log.h"
#include "phy.h"
#include "updi.h"
#include "sleep.h"
uint32_t baudrate_setting =9600;
uint8_t retries = 0;
/** \brief Initialize physical interface
*
* \param [in] port Port name as string
* \param [in] baudrate Transmission baudrate
* \param [in] onDTR True if using DTR for power
* \return true if success
*
*/
bool PHY_Init(char *port, uint32_t baudrate, bool onDTR)
{
baudrate_setting=baudrate;
retries=0;
LOG_Print(LOG_LEVEL_INFO, "Initializing phy %s at %u baud\n", port, baudrate);
return COM_Open(port, baudrate, true, true);
}
/** \brief Sends a double break to reset the UPDI port
* BREAK is actually just a slower zero frame
* A double break is guaranteed to push the UPDI state
* machine into a known state, albeit rather brutally
*
* \param [in] port Port name as string
* \return true if success
*
*/
bool PHY_DoBreak(char *port)
{
uint8_t buf[] = {UPDI_BREAK, UPDI_BREAK};
uint32_t breakbaudrate=300;
LOG_Print(LOG_LEVEL_INFO, "Sending double break @ %u", breakbaudrate);
COM_Close();
if (COM_Open(port, breakbaudrate, false, false) != true)
return false;
// Send two break characters, with 1 stop bit in between
COM_Write(buf, sizeof(buf));
// Wait for the double break end
//msleep(10); // wait for 10mS second
if (COM_Read(buf, 2) != 2)
LOG_Print(LOG_LEVEL_WARNING, "No answer received");
COM_Close();
// Re open port at original baudrate setting.
COM_Open(port, baudrate_setting, false, false);
return true;
}
/** \brief Send data to physical interface
*
* \param [in] data Buffer with data
* \param [in] len Length of data buffer
* \return true if success
*
*/
bool PHY_Send(uint8_t *data, uint8_t len)
{
//uint8_t i;
/*for (i = 0; i < len; i++)
{
COM_Write(&data[i], 1);
}*/
COM_Write(data, len);
// read echo
usleep(12);
//msleep(COM_GetTransTime(len));
//Sleep(10);
COM_Read(data, len);
return true;
}
/** \brief Receive data from physical interface to data buffer
*
* \param [out] data Data buffer to write data in
* \param [in] len Length of data to be received
* \return true if success
*
*/
bool PHY_Receive(uint8_t *data, uint16_t len)
{
int val = COM_Read(data, len);
if ((val < 0) || (val != len))
return false;
return true;
}
/** \brief Close physical interface
*
* \return Nothing
*
*/
void PHY_Close(void)
{
COM_Close();
}