-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuowi.c
288 lines (239 loc) · 6.84 KB
/
uowi.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
******************************************************************************
* @file uowi.c
* @author Khusainov Timur
* @version 0.0.0.0
* @date 31.10.2012
* @brief
******************************************************************************
* @attention
* <h2><center>© COPYRIGHT [email protected] </center></h2>
******************************************************************************
*/
#include <stdint.h>
#include <stddef.h>
#include "board.h"
#include "irq/interrupt.h"
#include "uowi.h"
volatile tUOWI_State UOWI_State = UOWI_STATE_NOINIT;
volatile uint8_t UOWI_TxData[UOWI_TX_DATA_MAX_LENGTH];
volatile uint8_t UOWI_RxData[UOWI_RX_DATA_MAX_LENGTH];
volatile uint16_t UOWI_TxLen = 0;
volatile uint16_t UOWI_RxLen = 0;
typedef enum
{
UOWI_MODE_DETECT,
UOWI_MODE_RX_TX
} tUOWI_Mode;
void UOWI_IRQHandlerUART1_Rx(void);
void UOWI_SetMode(tUOWI_Mode mode)
{
UART1->CR1 |= UART1_CR1_UARTD; /* disable UART peripheral */
static const union
{
uint16_t Result;
struct
{
uint16_t BRR2_03 :4;
uint16_t BRR1 :8;
uint16_t BRR2_47 :4;
};
} CalcBaud[2] =
{
{(uint16_t)(((float)F_CPU/(float)9600) + 0.5)}, /* generate detect presence */
{(uint16_t)(((float)F_CPU/(float)115200) + 0.5)}, /* write/read single bit */
};
/* get magic value */
const uint8_t m_brr2 = (uint8_t)(CalcBaud[mode].BRR2_47 << 4)|(CalcBaud[mode].BRR2_03 & 0x0f);
const uint8_t m_brr1 = (uint8_t) CalcBaud[mode].BRR1;
UART1->BRR2 = m_brr2; /* set new baudrate */
UART1->BRR1 = m_brr1; /* set new baudrate */
/* clear status and data register */
uint8_t _dummy = UART1->SR;
_dummy = UART1->DR;
UART1->CR1 &=~ UART1_CR1_UARTD; /* enable UART peripheral */
}
void UOWI_Init()
{
//----------------------------------------------------------------------------
// Init port pinouts
//----------------------------------------------------------------------------
BSP_OWI_PORT->ODR |= BSP_OWI_P_TX; // set 1
BSP_OWI_PORT->DDR |= BSP_OWI_P_TX; // as output
BSP_OWI_PORT->CR1 |= BSP_OWI_P_TX; // push-pull
BSP_OWI_PORT->CR2 |= BSP_OWI_P_TX; // fast mode
BSP_OWI_PORT->ODR &= ~BSP_OWI_P_RX; // set 0
BSP_OWI_PORT->DDR &= ~BSP_OWI_P_RX; // as input
BSP_OWI_PORT->CR1 &= ~BSP_OWI_P_RX; // floating mode, Z-state
BSP_OWI_PORT->CR2 &= ~BSP_OWI_P_TX; // no interrupt
//----------------------------------------------------------------------------
BSP_InterruptSet(UOWI_IRQHandlerUART1_Rx, BSP_IRQ_VECTOR_UART1_RX, bspitStd);
//----------------------------------------------------------------------------
// Init UART peripheral
//----------------------------------------------------------------------------
UART1->CR1 = UART1_CR1_UARTD; /* disable UART peripheral */
/* enable receiver, transmitter and Rx interrupt */
UART1->CR2 = UART1_CR2_REN|UART1_CR2_TEN|UART1_CR2_RIEN;
/* Set the Clock Polarity, lock Phase, last Bit Clock pulse, stop bit 1 */
UART1->CR3 = UART1_CR3_CPOL|UART1_CR3_CPHA|UART1_CR3_LBCL;
UART1->CR4 = 0; /* clear register */
UART1->CR5 = 0; /* clear register */
/* enable half-duplex (single wire) mode */
//UART1->CR5 = UART1_CR5_HDSEL;
/* clear status and data register */
uint8_t _dummy = UART1->SR;
_dummy = UART1->DR;
//----------------------------------------------------------------------------
}
tUOWI_State UOWI_GetState()
{
return UOWI_State;
}
uint8_t UOWI_IsDetect()
{
return (UOWI_State == UOWI_STATE_DETECT_COMPLETE);
}
uint8_t UOWI_IsSend()
{
return (UOWI_State == UOWI_STATE_SEND_COMPLETE);
}
uint8_t UOWI_IsRecv()
{
return (UOWI_State == UOWI_STATE_RECV_COMPLETE);
}
uint8_t UOWI_IsBusy()
{
if (UOWI_State != UOWI_STATE_STATUS_EMPTY)
{
UART1->DR = UOWI_CMD_READ_STATE;
UOWI_State = UOWI_STATE_STATUS_BUSY;
}
return (UOWI_State == UOWI_STATE_STATUS_EMPTY);
}
void UOWI_StartDetect()
{
UOWI_SetMode(UOWI_MODE_DETECT);
UART1->DR = UOWI_CMD_DETECT;
UOWI_State = UOWI_STATE_DETECT_WAIT;
}
void UOWI_StartSend()
{
UOWI_SetMode(UOWI_MODE_DETECT);
UART1->DR = UOWI_CMD_DETECT;
UOWI_State = UOWI_STATE_SEND_START;
}
void UOWI_SendData(const uint8_t *pdata, uint16_t txlen, uint16_t rxlen)
{
if (pdata && txlen < UOWI_TX_DATA_MAX_LENGTH && rxlen < UOWI_RX_DATA_MAX_LENGTH)
{
UOWI_TxLen = txlen;
UOWI_RxLen = rxlen;
uint8_t *_out_buf = (uint8_t *)(UOWI_TxData);
while (txlen--)
*(_out_buf++) = *(pdata++);
UOWI_StartSend();
}
}
void UOWI_RecvData(uint8_t * pdata, uint16_t rxlen)
{
if (rxlen <= UOWI_RxLen)
{
uint8_t *_in_buf = (uint8_t *)(UOWI_RxData);
while (rxlen--)
*(pdata++) = *(_in_buf++);
}
}
void UOWI_IRQHandlerUART1_Rx(void)
{
uint8_t m_err = UART1->SR & (UART1_SR_NF|UART1_SR_PE|UART1_SR_FE|UART1_SR_OR);
uint8_t m_data = UART1->DR;
static uint16_t _bit_counter;
static uint8_t _current_byte;
switch (UOWI_State)
{
case UOWI_STATE_DETECT_WAIT:
case UOWI_STATE_DETECT_ERROR:
{
if (m_data == UOWI_RET_DETECT_ERROR)
{
UOWI_State = UOWI_STATE_DETECT_ERROR;
UART1->DR = UOWI_CMD_DETECT;
}
else
UOWI_State = UOWI_STATE_DETECT_COMPLETE;
}
break;
case UOWI_STATE_SEND_START:
case UOWI_STATE_SEND_ERROR:
{
if (m_data == UOWI_RET_DETECT_ERROR)
{
UOWI_State = UOWI_STATE_SEND_ERROR;
UART1->DR = UOWI_CMD_DETECT;
break;
}
else
{
UOWI_State = UOWI_STATE_SEND_WAIT;
UOWI_SetMode(UOWI_MODE_RX_TX);
_bit_counter = 0;
}
}
case UOWI_STATE_SEND_WAIT:
{
uint16_t _tx_byte_num = (_bit_counter >> 3);
if (_tx_byte_num != UOWI_TxLen)
{
if ((_bit_counter & 0x7) == 0)
_current_byte = UOWI_TxData[_tx_byte_num];
UART1->DR = (_current_byte & 1) ? UOWI_CMD_WRITE_BIT1 : UOWI_CMD_WRITE_BIT0;
_current_byte >>= 1;
_bit_counter++;
}
else
{
if (UOWI_RxLen)
{
UART1->DR = UOWI_CMD_READ_BIT;
UOWI_State = UOWI_STATE_RECV_WAIT;
_bit_counter = 0;
_current_byte = 0;
}
else
UOWI_State = UOWI_STATE_SEND_COMPLETE;
}
}
break;
case UOWI_STATE_RECV_WAIT:
{
_bit_counter++;
_current_byte >>= 1;
if (m_data == UOWI_RET_READ_BIT1)
_current_byte |= 0x80;
if ((_bit_counter & 0x7) == 0)
{
uint16_t _rx_byte_num = (_bit_counter >> 3);
UOWI_RxData[_rx_byte_num - 1] = _current_byte;
if (_rx_byte_num == UOWI_RxLen)
{
UOWI_State = UOWI_STATE_RECV_COMPLETE;
}
else
{
UART1->DR = UOWI_CMD_READ_BIT;
_current_byte = 0;
}
}
else
UART1->DR = UOWI_CMD_READ_BIT;
}
break;
case UOWI_STATE_STATUS_BUSY:
{
if (m_data == UOWI_RET_STATE_EMPTY)
UOWI_State = UOWI_STATE_STATUS_EMPTY;
}
break;
}
UART1->SR &=~ UART1_SR_RXNE;
}