Subversion Repositories libSerial

Rev

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

Rev 4 Rev 5
Line 24... Line 24...
24
/* returns the number of characters received by the Rx USART */
24
/* returns the number of characters received by the Rx USART */
25
uint16_t
25
uint16_t
26
SerialCharsReceived (usart_ctl *instance)
26
SerialCharsReceived (usart_ctl *instance)
27
{
27
{
28
  uint16_t result = 0;  // assume no characters received yet
28
  uint16_t result = 0;  // assume no characters received yet
29
  __HAL_UART_DISABLE_IT (instance, UART_IT_RXNE);
29
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
30
 
30
 
31
  if (instance->rx_usart_buffer_full)
31
  if (instance->rx_usart_buffer_full)
32
    {   // buffer is full...
32
    {   // buffer is full...
33
      result = RX_USART_BUFF_SIZ;
33
      result = RX_USART_BUFF_SIZ;
34
    }
34
    }
Line 39... Line 39...
39
  else
39
  else
40
    {   // buffer has possibly wrapped...
40
    {   // buffer has possibly wrapped...
41
      result = RX_USART_BUFF_SIZ - instance->rx_usart_out_Ptr
41
      result = RX_USART_BUFF_SIZ - instance->rx_usart_out_Ptr
42
          + instance->rx_usart_in_Ptr;
42
          + instance->rx_usart_in_Ptr;
43
    }
43
    }
44
  __HAL_UART_ENABLE_IT (instance, UART_IT_RXNE);
44
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
45
 
45
 
46
  return result;
46
  return result;
47
}
47
}
48
 
48
 
49
inline uint8_t
49
inline uint8_t
50
PollSerial (usart_ctl *instance)
50
PollSerial (usart_ctl *instance)
51
{
51
{
52
  uint8_t rc;
52
  uint8_t rc;
53
 
53
 
54
  __HAL_UART_DISABLE_IT (instance, UART_IT_RXNE);
54
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
55
  rc = (instance->rx_usart_buffer_full
55
  rc = (instance->rx_usart_buffer_full
56
      || (instance->rx_usart_in_Ptr != instance->rx_usart_out_Ptr));
56
      || (instance->rx_usart_in_Ptr != instance->rx_usart_out_Ptr));
57
  __HAL_UART_ENABLE_IT (instance, UART_IT_RXNE);
57
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
58
 
58
 
59
  return rc;
59
  return rc;
60
}
60
}
61
 
61
 
62
/***!
62
/***!
Line 65... Line 65...
65
 */
65
 */
66
inline uint8_t
66
inline uint8_t
67
GetCharSerial (usart_ctl *instance)
67
GetCharSerial (usart_ctl *instance)
68
{
68
{
69
  uint8_t c;
69
  uint8_t c;
70
  __HAL_UART_DISABLE_IT (instance, UART_IT_RXNE);
70
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
71
  while (!instance->rx_usart_buffer_full
71
  while (!instance->rx_usart_buffer_full
72
      && (instance->rx_usart_in_Ptr == instance->rx_usart_out_Ptr))
72
      && (instance->rx_usart_in_Ptr == instance->rx_usart_out_Ptr))
73
    {
73
    {
74
      __HAL_UART_ENABLE_IT (instance, UART_IT_RXNE);
74
      __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
75
      __WFI (); /* wait for something */
75
      __WFI (); /* wait for something */
76
      __HAL_UART_DISABLE_IT (instance, UART_IT_RXNE);
76
      __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
77
    }
77
    }
78
 
78
 
79
  c = instance->rx_usart_buff[instance->rx_usart_out_Ptr];
79
  c = instance->rx_usart_buff[instance->rx_usart_out_Ptr];
80
  instance->rx_usart_buffer_full = 0; /* removed character */
80
  instance->rx_usart_buffer_full = 0; /* removed character */
81
  instance->rx_usart_out_Ptr++;
81
  instance->rx_usart_out_Ptr++;
82
  if (instance->rx_usart_out_Ptr >= RX_USART_BUFF_SIZ)
82
  if (instance->rx_usart_out_Ptr >= RX_USART_BUFF_SIZ)
83
    {
83
    {
84
      instance->rx_usart_out_Ptr = 0;
84
      instance->rx_usart_out_Ptr = 0;
85
    }
85
    }
86
  __HAL_UART_ENABLE_IT (instance, UART_IT_RXNE);
86
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
87
  return c;
87
  return c;
88
}
88
}
89
 
89
 
90
/*
90
/*
91
 * \brief
91
 * \brief
Line 94... Line 94...
94
 */
94
 */
95
inline void
95
inline void
96
EnableSerialRxInterrupt (usart_ctl *instance)
96
EnableSerialRxInterrupt (usart_ctl *instance)
97
{
97
{
98
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
98
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
99
  __HAL_UART_ENABLE_IT (instance, UART_IT_RXNE);
99
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_RXNE);
100
 
100
 
101
}
101
}
102
 
102
 
103
/****!
103
/****!
104
 * @brief send a character to the serial USART via placing it in the serial buffer
104
 * @brief send a character to the serial USART via placing it in the serial buffer
Line 106... Line 106...
106
 */
106
 */
107
 
107
 
108
static void
108
static void
109
PutCharSerialFIFO (usart_ctl *instance, uint8_t c)
109
PutCharSerialFIFO (usart_ctl *instance, uint8_t c)
110
{
110
{
111
  __HAL_UART_DISABLE_IT (instance, UART_IT_TXE);
111
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_TXE);
112
 
112
 
113
  instance->tx_usart_buff[instance->tx_usart_in_Ptr++] = c;
113
  instance->tx_usart_buff[instance->tx_usart_in_Ptr++] = c;
114
  instance->tx_usart_count += 1;
114
  instance->tx_usart_count += 1;
115
  if (instance->tx_usart_in_Ptr >= TX_USART_BUFF_SIZ)
115
  if (instance->tx_usart_in_Ptr >= TX_USART_BUFF_SIZ)
116
    {
116
    {
Line 125... Line 125...
125
          instance->tx_usart_out_Ptr = 0;
125
          instance->tx_usart_out_Ptr = 0;
126
        }
126
        }
127
    }
127
    }
128
 
128
 
129
  instance->tx_usart_running = 1;
129
  instance->tx_usart_running = 1;
130
  __HAL_UART_ENABLE_IT (instance, UART_IT_TXE);
130
  __HAL_UART_ENABLE_IT (instance->Handle, UART_IT_TXE);
131
}
131
}
132
 
132
 
133
void
133
void
134
UART_IRQHandler (usart_ctl *instance)
134
UART_IRQHandler (usart_ctl *instance)
135
{
135
{
136
 
136
 
137
  __disable_irq ();
137
  __disable_irq ();
138
  uint32_t rxStatus;    // status from USART receiver
138
  uint32_t rxStatus;    // status from USART receiver
139
 
139
 
140
  rxStatus = instance->Instance->SR;// read the status bits - this resets all the hardware signalling flags
140
  rxStatus = instance->Handle->Instance->SR;// read the status bits - this resets all the hardware signalling flags
141
 
141
 
142
  if ((rxStatus & USART_SR_RXNE)!= RESET)
142
  if ((rxStatus & USART_SR_RXNE)!= RESET)
143
    {
143
    {
144
      // no error has occurred...
144
      // no error has occurred...
145
      uint8_t rxChar = (uint8_t) (instance->Instance->DR & 0xff);// read the bottom 8-bits only
145
      uint8_t rxChar = (uint8_t) (instance->Handle->Instance->DR & 0xff);// read the bottom 8-bits only
146
 
146
 
147
      if (!instance->rx_usart_buffer_full)
147
      if (!instance->rx_usart_buffer_full)
148
        {
148
        {
149
          instance->rx_usart_buff[instance->rx_usart_in_Ptr++] = rxChar;
149
          instance->rx_usart_buff[instance->rx_usart_in_Ptr++] = rxChar;
150
 
150
 
Line 165... Line 165...
165
      /* Only enable the transmitter when baud detect has completed or check expired.
165
      /* Only enable the transmitter when baud detect has completed or check expired.
166
       * and the software is ready for it to be enabled as programming mode is wanting
166
       * and the software is ready for it to be enabled as programming mode is wanting
167
       * to receive a response and that can get blocked if we're streaming a lot of debug messages*/
167
       * to receive a response and that can get blocked if we're streaming a lot of debug messages*/
168
      if (instance->tx_usart_in_Ptr != instance->tx_usart_out_Ptr)
168
      if (instance->tx_usart_in_Ptr != instance->tx_usart_out_Ptr)
169
        {
169
        {
170
          instance->Instance->DR =
170
          instance->Handle->Instance->DR =
171
              instance->tx_usart_buff[instance->tx_usart_out_Ptr++];
171
              instance->tx_usart_buff[instance->tx_usart_out_Ptr++];
172
          if (instance->tx_usart_count != 0)
172
          if (instance->tx_usart_count != 0)
173
            instance->tx_usart_count -= 1;
173
            instance->tx_usart_count -= 1;
174
 
174
 
175
          if (instance->tx_usart_out_Ptr >= TX_USART_BUFF_SIZ)
175
          if (instance->tx_usart_out_Ptr >= TX_USART_BUFF_SIZ)
Line 177... Line 177...
177
              instance->tx_usart_out_Ptr = 0;
177
              instance->tx_usart_out_Ptr = 0;
178
            }
178
            }
179
        }
179
        }
180
      else
180
      else
181
        {
181
        {
182
          __HAL_UART_DISABLE_IT (instance, UART_IT_TXE);
182
          __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_TXE);
183
          instance->tx_usart_running = 0;
183
          instance->tx_usart_running = 0;
184
        }
184
        }
185
    }
185
    }
186
 
186
 
187
  __enable_irq ();
187
  __enable_irq ();
Line 247... Line 247...
247
 */
247
 */
248
 
248
 
249
void TxWaitEmpty(usart_ctl *instance)
249
void TxWaitEmpty(usart_ctl *instance)
250
{
250
{
251
  while (instance->tx_usart_count ||
251
  while (instance->tx_usart_count ||
252
       (instance->Instance->SR & USART_SR_TC) != RESET) {};
252
       (instance->Handle->Instance->SR & USART_SR_TC) != RESET) {};
253
}
253
}
254
 
254
 
255
/****
255
/****
256
 * @brief Initialise control structure
256
 * @brief Initialise control structure
257
 */
257
 */
258
void
258
void
259
init_usart_ctl (usart_ctl *instance, USART_TypeDef * usart )
259
init_usart_ctl (usart_ctl *instance, UART_HandleTypeDef * handle )
260
{
260
{
261
 
261
 
262
  instance->Instance = usart;
262
  instance->Handle = handle;
263
 
263
 
264
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
264
  /* cheat here - this is a macro and I have the same Instance member as the HAL handle, with the same meaning */
265
   __HAL_UART_DISABLE_IT (instance, UART_IT_TXE);
265
   __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_TXE);
266
  __HAL_UART_DISABLE_IT (instance, UART_IT_RXNE);
266
  __HAL_UART_DISABLE_IT (instance->Handle, UART_IT_RXNE);
267
 
267
 
268
  instance->tx_usart_in_Ptr = 0;
268
  instance->tx_usart_in_Ptr = 0;
269
  instance->tx_usart_out_Ptr = 0;
269
  instance->tx_usart_out_Ptr = 0;
270
  instance->tx_usart_running = 0;
270
  instance->tx_usart_running = 0;
271
  instance->tx_usart_count = 0;
271
  instance->tx_usart_count = 0;
Line 274... Line 274...
274
  instance->rx_usart_out_Ptr = 0;
274
  instance->rx_usart_out_Ptr = 0;
275
  instance->rx_usart_buffer_full = 0;
275
  instance->rx_usart_buffer_full = 0;
276
 
276
 
277
}
277
}
278
 
278
 
-
 
279
void
-
 
280
setBaud (usart_ctl *ctl, uint32_t baud)
-
 
281
{
-
 
282
  ctl->Handle->Init.BaudRate = baud;
-
 
283
  __disable_irq ();
-
 
284
  HAL_UART_Init (ctl->Handle);
-
 
285
  __enable_irq ();
-
 
286
}
-
 
287
 
-
 
288
 
-
 
289
 
279
/////////////////////////////////////////////////////////
290
/////////////////////////////////////////////////////////
280
/// Moved from generated code  to avoid crappy HAL handler
291
/// Moved from generated code  to avoid crappy HAL handler
281
#if defined SERIAL_UART1
292
#if defined SERIAL_UART1
282
void USART1_IRQHandler(void)
293
void USART1_IRQHandler(void)
283
{
294
{