Subversion Repositories libSerial

Rev

Rev 7 | Rev 9 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7 Rev 8
Line 25... Line 25...
25
#endif
25
#endif
26
#if defined SERIAL_UART5
26
#if defined SERIAL_UART5
27
usart_ctl uc5;
27
usart_ctl uc5;
28
#endif
28
#endif
29
 
29
 
30
 
-
 
31
/* returns the number of characters received by the Rx USART */
30
/* returns the number of characters received by the Rx USART */
32
uint16_t
31
uint16_t
33
SerialCharsReceived (usart_ctl *instance)
32
SerialCharsReceived(usart_ctl *instance)
34
{
33
{
35
  uint16_t result = 0;  // assume no characters received yet
34
  uint16_t result = 0; // assume no characters received yet
36
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
35
  __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_RXNE);
37
 
36
 
38
  if (instance->rx_usart_buffer_full)
37
  if (instance->rx_usart_buffer_full)
39
    {   // buffer is full...
38
  { // buffer is full...
40
      result = RX_USART_BUFF_SIZ;
39
    result = RX_USART_BUFF_SIZ;
41
    }
40
  }
42
  else if (instance->rx_usart_in_Ptr >= instance->rx_usart_out_Ptr)
41
  else if (instance->rx_usart_in_Ptr >= instance->rx_usart_out_Ptr)
43
    {   // buffer has not wrapped...
42
  { // buffer has not wrapped...
44
      result = instance->rx_usart_in_Ptr - instance->rx_usart_out_Ptr;
43
    result = instance->rx_usart_in_Ptr - instance->rx_usart_out_Ptr;
45
    }
44
  }
46
  else
45
  else
47
    {   // buffer has possibly wrapped...
46
  { // buffer has possibly wrapped...
48
      result = RX_USART_BUFF_SIZ - instance->rx_usart_out_Ptr
47
    result = RX_USART_BUFF_SIZ - instance->rx_usart_out_Ptr + instance->rx_usart_in_Ptr;
49
          + instance->rx_usart_in_Ptr;
-
 
50
    }
48
  }
51
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
49
  __HAL_UART_ENABLE_IT(instance->Handle, UART_IT_RXNE);
52
 
50
 
53
  return result;
51
  return result;
54
}
52
}
55
 
53
 
56
uint16_t SerialTransmitSpace(usart_ctl * instance)
54
uint16_t SerialTransmitSpace(usart_ctl *instance)
57
{
55
{
58
  return TX_USART_BUFF_SIZ- instance->tx_usart_count;
56
  return TX_USART_BUFF_SIZ - instance->tx_usart_count;
59
}
57
}
60
 
58
 
61
 
-
 
62
 
-
 
63
inline uint8_t
59
inline uint8_t
64
PollSerial (usart_ctl *instance)
60
PollSerial(usart_ctl *instance)
65
{
61
{
66
  uint8_t rc;
62
  uint8_t rc;
67
 
63
 
68
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
64
  __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_RXNE);
69
  rc = (instance->rx_usart_buffer_full
-
 
70
      || (instance->rx_usart_in_Ptr != instance->rx_usart_out_Ptr));
65
  rc = (instance->rx_usart_buffer_full || (instance->rx_usart_in_Ptr != instance->rx_usart_out_Ptr));
71
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
66
  __HAL_UART_ENABLE_IT(instance->Handle, UART_IT_RXNE);
72
 
67
 
73
  return rc;
68
  return rc;
74
}
69
}
75
 
70
 
76
/***!
71
/***!
77
 * @brief return the next character in the Serial input buffer
72
 * @brief return the next character in the Serial input buffer
78
 * This function will wait until a character arrives.
73
 * This function will wait until a character arrives.
79
 */
74
 */
80
inline uint8_t
75
inline uint8_t
81
GetCharSerial (usart_ctl *instance)
76
GetCharSerial(usart_ctl *instance)
82
{
77
{
83
  uint8_t c;
78
  uint8_t c;
84
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
79
  __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_RXNE);
85
  while (!instance->rx_usart_buffer_full
-
 
86
      && (instance->rx_usart_in_Ptr == instance->rx_usart_out_Ptr))
80
  while (!instance->rx_usart_buffer_full && (instance->rx_usart_in_Ptr == instance->rx_usart_out_Ptr))
87
    {
81
  {
88
      __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
82
    __HAL_UART_ENABLE_IT(instance->Handle, UART_IT_RXNE);
89
      __WFI (); /* wait for something */
83
    __WFI(); /* wait for something */
90
      __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
84
    __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_RXNE);
91
    }
85
  }
92
 
86
 
93
  c = instance->rx_usart_buff[instance->rx_usart_out_Ptr];
87
  c = instance->rx_usart_buff[instance->rx_usart_out_Ptr];
94
  instance->rx_usart_buffer_full = 0; /* removed character */
88
  instance->rx_usart_buffer_full = 0; /* removed character */
95
  instance->rx_usart_out_Ptr++;
89
  instance->rx_usart_out_Ptr++;
96
  if (instance->rx_usart_out_Ptr >= RX_USART_BUFF_SIZ)
90
  if (instance->rx_usart_out_Ptr >= RX_USART_BUFF_SIZ)
97
    {
91
  {
98
      instance->rx_usart_out_Ptr = 0;
92
    instance->rx_usart_out_Ptr = 0;
99
    }
93
  }
100
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
94
  __HAL_UART_ENABLE_IT(instance->Handle, UART_IT_RXNE);
101
  return c;
95
  return c;
102
}
96
}
103
 
97
 
104
/*
98
/*
105
 * \brief
99
 * \brief
106
 * void EnableSerialRxInterrupt(void) - this function is used from the interrupt handler and the main scheduler loop
100
 * void EnableSerialRxInterrupt(void) - this function is used from the interrupt handler and the main scheduler loop
107
 * to enable the serial rx interrupt after resetting the serial rx buffer...
101
 * to enable the serial rx interrupt after resetting the serial rx buffer...
108
 */
102
 */
109
inline void
103
inline void
110
EnableSerialRxInterrupt (usart_ctl *instance)
104
EnableSerialRxInterrupt(usart_ctl *instance)
111
{
105
{
112
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
106
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
113
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
107
  __HAL_UART_ENABLE_IT(instance->Handle, UART_IT_RXNE);
114
 
-
 
115
}
108
}
116
 
109
 
117
/****!
110
/****!
118
 * @brief send a character to the serial USART via placing it in the serial buffer
111
 * @brief send a character to the serial USART via placing it in the serial buffer
119
 *
112
 *
120
 */
113
 */
121
 
114
 
122
static void
115
static void
123
PutCharSerialFIFO (usart_ctl *instance, uint8_t c)
116
PutCharSerialFIFO(usart_ctl *instance, uint8_t c)
124
{
117
{
125
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_TXE);
118
  __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_TXE);
126
 
119
 
127
  instance->tx_usart_buff[instance->tx_usart_in_Ptr++] = c;
120
  instance->tx_usart_buff[instance->tx_usart_in_Ptr++] = c;
128
  instance->tx_usart_count += 1;
121
  instance->tx_usart_count += 1;
129
  if (instance->tx_usart_in_Ptr >= TX_USART_BUFF_SIZ)
122
  if (instance->tx_usart_in_Ptr >= TX_USART_BUFF_SIZ)
130
    {
123
  {
131
      instance->tx_usart_in_Ptr = 0;
124
    instance->tx_usart_in_Ptr = 0;
132
    }
125
  }
133
  /* Handle overrun by losing oldest characters */
126
  /* Handle overrun by losing oldest characters */
134
  if (instance->tx_usart_in_Ptr == instance->tx_usart_out_Ptr)
127
  if (instance->tx_usart_in_Ptr == instance->tx_usart_out_Ptr)
-
 
128
  {
-
 
129
    instance->tx_usart_out_Ptr++;
-
 
130
    if (instance->tx_usart_out_Ptr >= TX_USART_BUFF_SIZ)
135
    {
131
    {
136
      instance->tx_usart_out_Ptr++;
132
      instance->tx_usart_out_Ptr = 0;
137
      if (instance->tx_usart_out_Ptr >= TX_USART_BUFF_SIZ)
-
 
138
        {
-
 
139
          instance->tx_usart_out_Ptr = 0;
-
 
140
        }
-
 
141
    }
133
    }
-
 
134
  }
142
 
135
 
143
  instance->tx_usart_running = 1;
136
  instance->tx_usart_running = 1;
144
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_TXE);
137
  __HAL_UART_ENABLE_IT(instance->Handle, UART_IT_TXE);
145
}
138
}
146
 
139
 
147
void
-
 
148
UART_IRQHandler (usart_ctl *instance)
140
void UART_IRQHandler(usart_ctl *instance)
149
{
141
{
150
 
142
 
151
  __disable_irq ();
143
  __disable_irq();
152
  uint32_t rxStatus;    // status from USART receiver
144
  uint32_t rxStatus; // status from USART receiver
153
 
145
 
154
  rxStatus = instance->Handle->Instance->SR;// read the status bits - this resets all the hardware signalling flags
146
  rxStatus = instance->Handle->Instance->SR; // read the status bits - this resets all the hardware signalling flags
155
 
147
 
156
  if ((rxStatus & USART_SR_LBD))
148
  if ((rxStatus & USART_SR_LBD))
157
      __HAL_UART_CLEAR_FLAG(instance->Handle,USART_SR_LBD);
149
    __HAL_UART_CLEAR_FLAG(instance->Handle, USART_SR_LBD);
158
 
150
 
159
  if ((rxStatus & USART_SR_RXNE)!= RESET)
151
  if ((rxStatus & USART_SR_RXNE) != RESET)
-
 
152
  {
-
 
153
    // no error has occurred...
-
 
154
    uint8_t rxChar = (uint8_t)(instance->Handle->Instance->DR & 0xff); // read the bottom 8-bits only
-
 
155
 
-
 
156
    if (!instance->rx_usart_buffer_full)
160
    {
157
    {
161
      // no error has occurred...
-
 
162
      uint8_t rxChar = (uint8_t) (instance->Handle->Instance->DR & 0xff);// read the bottom 8-bits only
158
      instance->rx_usart_buff[instance->rx_usart_in_Ptr++] = rxChar;
163
 
159
 
164
      if (!instance->rx_usart_buffer_full)
-
 
165
        {
-
 
166
          instance->rx_usart_buff[instance->rx_usart_in_Ptr++] = rxChar;
-
 
167
 
-
 
168
          if (instance->rx_usart_in_Ptr >= RX_USART_BUFF_SIZ)
160
      if (instance->rx_usart_in_Ptr >= RX_USART_BUFF_SIZ)
169
            {
161
      {
170
              instance->rx_usart_in_Ptr = 0;
162
        instance->rx_usart_in_Ptr = 0;
171
            }
163
      }
172
          if (instance->rx_usart_in_Ptr == instance->rx_usart_out_Ptr)
164
      if (instance->rx_usart_in_Ptr == instance->rx_usart_out_Ptr)
173
            {
165
      {
174
              instance->rx_usart_buffer_full = 1; /* buffer overrun */
166
        instance->rx_usart_buffer_full = 1; /* buffer overrun */
175
            }
167
      }
176
        }
-
 
177
    }
168
    }
-
 
169
  }
178
  /* check for transmitter interrupt : this code is used */
170
  /* check for transmitter interrupt : this code is used */
179
  if ((rxStatus & USART_SR_TXE) != RESET)
171
  if ((rxStatus & USART_SR_TXE) != RESET)
-
 
172
  {
-
 
173
 
-
 
174
    /* Only enable the transmitter when baud detect has completed or check expired.
-
 
175
     * and the software is ready for it to be enabled as programming mode is wanting
-
 
176
     * to receive a response and that can get blocked if we're streaming a lot of debug messages*/
-
 
177
    if (instance->tx_usart_in_Ptr != instance->tx_usart_out_Ptr)
180
    {
178
    {
181
 
179
 
-
 
180
      instance->Handle->Instance->DR =
-
 
181
          instance->tx_usart_buff[instance->tx_usart_out_Ptr++];
-
 
182
      if (instance->tx_usart_count != 0)
-
 
183
        instance->tx_usart_count -= 1;
182
 
184
 
183
      /* Only enable the transmitter when baud detect has completed or check expired.
-
 
184
       * and the software is ready for it to be enabled as programming mode is wanting
-
 
185
       * to receive a response and that can get blocked if we're streaming a lot of debug messages*/
-
 
186
      if (instance->tx_usart_in_Ptr != instance->tx_usart_out_Ptr)
-
 
187
        {
-
 
188
          instance->Handle->Instance->DR =
-
 
189
              instance->tx_usart_buff[instance->tx_usart_out_Ptr++];
-
 
190
          if (instance->tx_usart_count != 0)
-
 
191
            instance->tx_usart_count -= 1;
-
 
192
 
-
 
193
          if (instance->tx_usart_out_Ptr >= TX_USART_BUFF_SIZ)
185
      if (instance->tx_usart_out_Ptr >= TX_USART_BUFF_SIZ)
194
            {
186
      {
195
              instance->tx_usart_out_Ptr = 0;
187
        instance->tx_usart_out_Ptr = 0;
196
            }
188
      }
197
        }
-
 
198
      if (instance->tx_usart_count == 0)
-
 
199
        {
-
 
200
          __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_TXE);
-
 
201
          instance->tx_usart_running = 0;
-
 
202
        }
-
 
203
    }
189
    }
-
 
190
    if (instance->tx_usart_count == 0)
-
 
191
    {
-
 
192
      __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_TXE);
-
 
193
      instance->tx_usart_running = 0;
-
 
194
    }
204
 
195
  }
205
  __enable_irq ();
196
  __enable_irq();
206
}
197
}
207
 
198
 
208
void
-
 
209
PutCharSerial (usart_ctl *instance, uint8_t c)
199
void PutCharSerial(usart_ctl *instance, uint8_t c)
210
{
200
{
211
  // Put character in Crayon/Pen interface
201
  // Put character in Crayon/Pen interface
212
  PutCharSerialFIFO (instance, c);
202
  PutCharSerialFIFO(instance, c);
213
}
203
}
214
 
204
 
215
/*
205
/*
216
 * \brief
206
 * \brief
217
 * ResetTxBuffer(void) - resets the serial transmitter buffer
207
 * ResetTxBuffer(void) - resets the serial transmitter buffer
218
 */
208
 */
219
void
-
 
220
ResetTxBuffer (usart_ctl *instance)
209
void ResetTxBuffer(usart_ctl *instance)
221
{
210
{
222
 
211
 
223
  instance->tx_usart_out_Ptr = 0;
212
  instance->tx_usart_out_Ptr = 0;
224
  instance->tx_usart_running = 0;
213
  instance->tx_usart_running = 0;
225
  instance->tx_usart_in_Ptr = 0; /* setup in pointer last to drop any chars come in */
214
  instance->tx_usart_in_Ptr = 0; /* setup in pointer last to drop any chars come in */
226
  instance->tx_usart_count = 0;
215
  instance->tx_usart_count = 0;
227
 
-
 
228
}
216
}
229
 
217
 
230
/*
218
/*
231
 * \brief
219
 * \brief
232
 * void ResetRxBuffer(void) - resets the serial receiver buffer
220
 * void ResetRxBuffer(void) - resets the serial receiver buffer
233
 */
221
 */
234
void
-
 
235
ResetRxBuffer (usart_ctl *instance)
222
void ResetRxBuffer(usart_ctl *instance)
236
{
223
{
237
 
224
 
238
  instance->rx_usart_out_Ptr = 0;
225
  instance->rx_usart_out_Ptr = 0;
239
  instance->rx_usart_buffer_full = 0;
226
  instance->rx_usart_buffer_full = 0;
240
  instance->rx_usart_in_Ptr = 0; /* setup in pointer last to drop any chars come in */
227
  instance->rx_usart_in_Ptr = 0; /* setup in pointer last to drop any chars come in */
241
 
-
 
242
}
228
}
243
 
229
 
244
/***!
230
/***!
245
 * @brief Flush Serial input and output buffers
231
 * @brief Flush Serial input and output buffers
246
 */
232
 */
247
void
-
 
248
FlushSerial (usart_ctl *instance)
233
void FlushSerial(usart_ctl *instance)
249
{
234
{
250
  ResetRxBuffer (instance);
235
  ResetRxBuffer(instance);
251
  ResetTxBuffer (instance);
236
  ResetTxBuffer(instance);
252
}
237
}
253
 
238
 
254
/***!
239
/***!
255
 * @brief check if tx buffer is empty...
240
 * @brief check if tx buffer is empty...
256
 */
241
 */
257
uint8_t
242
uint8_t
258
TxBufferEmpty (usart_ctl *instance)
243
TxBufferEmpty(usart_ctl *instance)
259
{
244
{
260
  return (0 == instance->tx_usart_count );
245
  return (0 == instance->tx_usart_count);
261
}
246
}
262
 
247
 
263
/***!
248
/***!
264
 * @brief wait for transmission to finish
249
 * @brief wait for transmission to finish
265
 */
250
 */
266
 
251
 
267
void TxWaitEmpty(usart_ctl *instance)
252
void TxWaitEmpty(usart_ctl *instance)
268
{
253
{
269
  while (instance->tx_usart_count ||
254
  while (instance->tx_usart_count ||
270
       (instance->Handle->Instance->SR & USART_SR_TC) != RESET) {};
255
         (instance->Handle->Instance->SR & USART_SR_TC) != RESET)
-
 
256
  {
-
 
257
  };
271
}
258
}
272
 
259
 
273
/****
260
/****
274
 * @brief Initialise control structure
261
 * @brief Initialise control structure
275
 */
262
 */
276
void
-
 
277
init_usart_ctl (usart_ctl *instance, UART_HandleTypeDef * handle )
263
void init_usart_ctl(usart_ctl *instance, UART_HandleTypeDef *handle)
278
{
264
{
279
 
265
 
280
  instance->Handle = handle;
266
  instance->Handle = handle;
281
 
267
 
282
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
268
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
283
   __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_TXE);
269
  __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_TXE);
284
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
270
  __HAL_UART_DISABLE_IT(instance->Handle, UART_IT_RXNE);
285
 
271
 
286
  instance->tx_usart_in_Ptr = 0;
272
  instance->tx_usart_in_Ptr = 0;
287
  instance->tx_usart_out_Ptr = 0;
273
  instance->tx_usart_out_Ptr = 0;
288
  instance->tx_usart_running = 0;
274
  instance->tx_usart_running = 0;
289
  instance->tx_usart_count = 0;
275
  instance->tx_usart_count = 0;
290
 
276
 
291
  instance->rx_usart_in_Ptr = 0;
277
  instance->rx_usart_in_Ptr = 0;
292
  instance->rx_usart_out_Ptr = 0;
278
  instance->rx_usart_out_Ptr = 0;
293
  instance->rx_usart_buffer_full = 0;
279
  instance->rx_usart_buffer_full = 0;
294
 
-
 
295
}
280
}
296
 
281
 
297
void
-
 
298
setBaud (usart_ctl *ctl, uint32_t baud)
282
void setBaud(usart_ctl *ctl, uint32_t baud)
299
{
283
{
300
  ctl->Handle->Init.BaudRate = baud;
284
  ctl->Handle->Init.BaudRate = baud;
301
  __disable_irq ();
285
  __disable_irq();
302
  HAL_UART_Init (ctl->Handle);
286
  HAL_UART_Init(ctl->Handle);
303
  __enable_irq ();
287
  __enable_irq();
304
}
288
}
305
 
289
 
-
 
290
 
-
 
291
 
306
void sendString(usart_ctl *ctl, char *string, int length)
292
void sendString(usart_ctl *ctl, char const * string , int length)
307
{
293
{
308
  int i;
-
 
309
  for (i = 0; i < length; i++)
294
  for (int i = 0; i < length; i++)
310
    PutCharSerial(ctl, string[i]);
295
    PutCharSerial(ctl, string[i]);
311
}
296
}
312
 
297
 
313
 
-
 
314
 
-
 
315
 
-
 
316
/////////////////////////////////////////////////////////
298
/////////////////////////////////////////////////////////
317
/// Moved from generated code  to avoid crappy HAL handler
299
/// Moved from generated code  to avoid crappy HAL handler
318
#if defined SERIAL_UART1
300
#if defined SERIAL_UART1
319
void USART1_IRQHandler(void)
301
void USART1_IRQHandler(void)
320
{
302
{
321
        UART_IRQHandler(&uc1);
303
  UART_IRQHandler(&uc1);
322
}
304
}
323
#endif
305
#endif
324
#if defined SERIAL_UART2
306
#if defined SERIAL_UART2
325
void USART2_IRQHandler(void)
307
void USART2_IRQHandler(void)
326
{
308
{
327
        UART_IRQHandler(&uc2);
309
  UART_IRQHandler(&uc2);
328
}
310
}
329
#endif
311
#endif
330
#if defined SERIAL_UART3
312
#if defined SERIAL_UART3
331
void USART3_IRQHandler(void)
313
void USART3_IRQHandler(void)
332
{
314
{
333
        UART_IRQHandler(&uc3);
315
  UART_IRQHandler(&uc3);
334
}
316
}
335
#endif
317
#endif
336
#if defined SERIAL_UART4
318
#if defined SERIAL_UART4
337
void UART4_IRQHandler(void)
319
void UART4_IRQHandler(void)
338
{
320
{
339
        UART_IRQHandler(&uc4);
321
  UART_IRQHandler(&uc4);
340
}
322
}
341
#endif
323
#endif
342
#if defined SERIAL_UART5
324
#if defined SERIAL_UART5
343
void UART5_IRQHandler(void)
325
void UART5_IRQHandler(void)
344
{
326
{
345
        UART_IRQHandler(&uc5);
327
  UART_IRQHandler(&uc5);
346
}
328
}
347
#endif
329
#endif