Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_serUart.h
Go to the documentation of this file.
1 
107 // UART - documentation module
110 
127 #ifndef NZ_SERUART_H
128 #define NZ_SERUART_H
129 
130 #if defined(HAS_SERPORT_UART)
131 
132 #include "nz_circularBuffer.h"
133 
135 
136 #if (CLOCK_FREQ==32000000ul)
137 #ifndef UART_BAUD_1K2
138 #define UART_BAUD_1K2 3332 //0.01% error
139 #endif
140 #ifndef UART_BAUD_2K4
141 #define UART_BAUD_2K4 1666 //0.01% error
142 #endif
143 #ifndef UART_BAUD_4K8
144 #define UART_BAUD_4K8 832
145 #endif
146 #ifndef UART_BAUD_9K6
147 #define UART_BAUD_9K6 416 //0.07% error
148 #endif
149 #ifndef UART_BAUD_19K2
150 #define UART_BAUD_19K2 207 //0.16% error
151 #endif
152 #ifndef UART_BAUD_38K4
153 #define UART_BAUD_38K4 103 //0.16% error
154 #endif
155 #ifndef UART_BAUD_56K
156 #define UART_BAUD_56K 70 //0.6% error
157 #endif
158 #ifndef UART_BAUD_115K
159 #define UART_BAUD_115K 34 //0.62% error
160 #endif
161 #ifndef UART_BAUD_250K
162 #define UART_BAUD_250K 15 //0% error
163 #endif
164 
165 #else
166 #error "UART does not support this CLOCK_FREQ"
167 #endif
168 
169 
170 //Transmit and Receive buffer sizes
171 #if !defined(UART1_RXBUF_SIZE)
172 #define UART1_RXBUF_SIZE 64
173 #endif
174 #if !defined(UART1_TXBUF_SIZE)
175 #define UART1_TXBUF_SIZE 256
176 #endif
177 #if !defined(UART2_RXBUF_SIZE)
178 #define UART2_RXBUF_SIZE 32
179 #endif
180 #if !defined(UART2_TXBUF_SIZE)
181 #define UART2_TXBUF_SIZE 128
182 #endif
183 #if !defined(UART3_RXBUF_SIZE)
184 #define UART3_RXBUF_SIZE 32
185 #endif
186 #if !defined(UART3_TXBUF_SIZE)
187 #define UART3_TXBUF_SIZE 128
188 #endif
189 
190 
192 //UART_TXION_STATUS_xx defines, 0=OK, 1=Added to Buffer, 0x80-0x8F=Error, 0xFF=Status Not Available
193 #define UART_TXION_STATUS_OK 0 //Currently OK, have not finished adding whole message to buffer
194 #define UART_TXION_STATUS_ADDED_TO_BUF 1 //Successfully added whole message to the buffer (from start to stop)
195 #define UART_TXION_STATUS_ERROR 4 //No space in buffer to add message
196 #define UART_TXION_STATUS_ERR_NOSPACE 5 //No space in buffer to add message
197 #define UART_TXION_STATUS_ERR_ADR_NOACK 6 //Slave did not ACK address we sent
198 #define UART_TXION_STATUS_ERR_DAT_NOACK 7 //Slave did not ACK data we sent
199 #define UART_TXION_STATUS_ERR_BUF_FRMT 8 //Buffer format error
200 #define UART_TXION_STATUS_ERR_TIMEOUT 9 //Timeout
201 #define UART_TXION_STATUS_NOT_AVAILABLE 0xFF //Status not available
202 
203 //UART "Serial Data Port"
204 #if (defined(HAS_SERPORT_UART1) || defined(HAS_SERPORT_UART2) || defined(HAS_SERPORT_UART3) || defined(HAS_SERPORT_UART4))
205  #if !defined(HAS_A_SERPORT)
206  #define HAS_A_SERPORT
207  #endif
208 #endif
209 
210 
212 
213 //If "Power of 2" buffer used, size must by a power of 2
214 #if defined(CIRBUF_USE_CIRCULAR_BUFFER_PWR2)
215 #if defined(HAS_SERPORT_UART1)
216  //Ensure size is power of 2
217  #if ((UART1_TXBUF_SIZE & ~(UART1_TXBUF_SIZE-1))!=UART1_TXBUF_SIZE)
218  #error "UART1 UART1_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
219  #endif
220 
221  //Ensure size is power of 2
222  #if ((UART1_RXBUF_SIZE & ~(UART1_RXBUF_SIZE-1))!=UART1_RXBUF_SIZE)
223  #error "UART1 UART1_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
224  #endif
225 #endif
226 #if defined(HAS_SERPORT_UART2)
227  //Ensure size is power of 2
228  #if ((UART2_TXBUF_SIZE & ~(UART2_TXBUF_SIZE-1))!=UART2_TXBUF_SIZE)
229  #error "UART2 UART2_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
230  #endif
231 
232  //Ensure size is power of 2
233  #if ((UART2_RXBUF_SIZE & ~(UART2_RXBUF_SIZE-1))!=UART2_RXBUF_SIZE)
234  #error "UART2 UART2_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
235  #endif
236 #endif
237 #if defined(HAS_SERPORT_UART3)
238  //Ensure size is power of 2
239  #if ((UART3_TXBUF_SIZE & ~(UART3_TXBUF_SIZE-1))!=UART3_TXBUF_SIZE)
240  #error "UART3 UART3_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
241  #endif
242 
243  //Ensure size is power of 2
244  #if ((UART3_RXBUF_SIZE & ~(UART3_RXBUF_SIZE-1))!=UART3_RXBUF_SIZE)
245  #error "UART3 UART3_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
246  #endif
247 #endif
248 #if defined(HAS_SERPORT_UART4)
249  //Ensure size is power of 2
250  #if ((UART4_TXBUF_SIZE & ~(UART4_TXBUF_SIZE-1))!=UART4_TXBUF_SIZE)
251  #error "UART4 UART4_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
252  #endif
253 
254  //Ensure size is power of 2
255  #if ((UART4_RXBUF_SIZE & ~(UART4_RXBUF_SIZE-1))!=UART4_RXBUF_SIZE)
256  #error "UART4 UART4_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
257  #endif
258 #endif
259 #endif
260 
261 
262 
266 typedef struct __attribute__((aligned(2), packed))
267 {
268  union {
269  struct
270  {
271  unsigned int bFill : 16; //Fill to ensure flags take up a 16-bit WORD
272  } flags;
273  WORD flagsVal;
274  };
275 
277  // Not all values for UART1CON are possible, only following can be set:
278  WORD config1;
279 
281  //???=9.6kbits/sec
282  WORD baud;
283 
284 } SERPORT_UART_CONFIG;
285 
286 //DON'T change! Code that checks wrDstType.bit0 and wrDstType.bit1 assumes there values!
287 #define RD_DEST_TYPE_LISTENERS 0
288 #define RD_DEST_TYPE_POINTER 2
289 #define RD_DEST_TYPE_CIRBUF 3
290 
291 typedef struct __attribute__((__packed__)) UART_ISR_SMFLAGS_
292 {
293  union {
294  struct
295  {
296  unsigned int sm : 4;
297  unsigned int isrTxionStatus : 1;
298  unsigned int fill12 : 11;
299  } bits;
300  WORD val;
301  };
302 } UART_ISR_SMFLAGS;
303 
304 //Must be a power of two value: 4,8,16,32,64
305 #define UART_STATUS_BUF_SIZE 8
306 
307 typedef struct UART_INFO_
308 //typedef struct __attribute__((__packed__)) UART_INFO_
309 //typedef struct __attribute__((aligned(2), packed)) UART_INFO_
310 {
311  //Buffer containing status. Two bytes are used for each entry, the LSB=Address (0=empty), and MSB=Status.
312  BYTE bufStat[UART_STATUS_BUF_SIZE];
313  BYTE adr; //Used by non-ISR functions to remember current slave address
314  BYTE taskSm; //ISR State machine. 0 is idle.
315  BYTE isrAdr; //Used in ISR - address of slave
316  BYTE isrRead; //Used in ISR - number of bytes to to read
317  BYTE type; //Protocol Type - 0=standard streaming serial port, 1=RS485
318 
319  WORD isrRdDest; //Destination for read, meaning depends on value of uart1.isr.bits.wrDstType
320  // - When 0, bits 0-15 indicates which "Circular Buffer" listeners are destinations (0=default)
321  // - When 1, contains pointer to destination BYTE array
322  WORD timeout; //Used in task to check for a timeout
323  CIRBUF* pCbufTx;
324  CIRBUF* pCbufRx;
325  UART_ISR_SMFLAGS isr; //ISR State machine AND flags.
326  union {
327  struct
328  {
329  BYTE ch;
330  BYTE currTxionStatus;
331  } flags;
332  WORD flagsVal;
333  };
334 } UART_INFO;
335 
336 
337 
339 //UART Functions for all ports
340 
344 void serUartInit(void);
345 
351 void serUartTask(UART_INFO* objUART);
352 
353 
367 BYTE serUartRegisterListener(CIRBUF* pBuf);
368 
369 
379 UART_INFO* uartGetInfoForBus(BYTE uartBus);
380 
381 
392 BYTE serUartIsBusy(UART_INFO* objUART);
393 
402 BYTE serUartWrite(UART_INFO* objUART, BYTE b);
403 
404 
416 BYTE serUartWriteArray(UART_INFO* objUART, BYTE* arr, BYTE size);
417 
418 
429 BYTE serUartWriteString(UART_INFO* objUART, const char* str);
430 
431 
449 //BYTE serUartRead(UART_INFO* objUART, BYTE size);
450 
451 
467 //BYTE serUartReadToArray(UART_INFO* objUART, BYTE* buf, BYTE size);
468 
469 
471 //UART 1
472 #if defined(HAS_SERPORT_UART1)
473 
474 #if !defined(SERPORT_UART1_CREATE_OWN_CIRBUFS)
475 #include "nz_serDataPorts.h"
476 #endif
477 
478 //This is used by others
479 #ifndef THIS_IS_NZ_SERUART1_C
480 extern UART_INFO uart1Info;
481 #endif
482 #define UART1_INFO &uart1Info
483 
484 //TX and RX "Circular Buffers". Use SERPORT_UART1_CREATE_OWN_CIRBUFS define to use this
485 //module without SERPORTs. If NOT defined, this modules requires nz_serDataPorts.c
486 #if defined(SERPORT_UART1_CREATE_OWN_CIRBUFS)
487  //This is used by others
488  #ifndef THIS_IS_NZ_SERUART1_C
489  extern CIRBUF cbufTxUART1;
490  extern CIRBUF cbufRxUART1;
491  #endif
492 
493  #define CIRBUF_TX_UART1 &cbufTxUART1
494  #define CIRBUF_RX_UART1 &cbufRxUART1
495 #endif
496 
497 
501 void serUart1Init(void);
502 
503 
507 void serUart1Task(void);
508 
509 
517 BYTE serUart1IsBusy(void);
518 
519 
520 
528 BYTE serUart1Write(BYTE b);
529 
530 
539 BYTE serUart1WriteArray(BYTE* arr, BYTE size);
540 
541 
550 BYTE serUart1WriteString(const char* str);
551 
552 
556 #define serUart1GetTxBufFree() cbufGetFree(CIRBUF_TX_UART1)
557 
558 
562 #define serUart1EmptyTxBuf() cbufEmpty(CIRBUF_TX_UART1)
563 
564 
570 #define serUart1IsTxBufEmpty() cbufIsEmpty(CIRBUF_TX_UART1)
571 
572 
578 #define serUart1IsTxBufFull() cbufIsFull(CIRBUF_TX_UART1)
579 
580 
588 #define serUart1GetRxBufCount() cbufGetCount(CIRBUF_RX_UART1)
589 
590 
597 #define serUart1HasData() cbufHasData(CIRBUF_RX_UART1)
598 
599 
606 #define serUart1IsRxBufEmpty() cbufIsEmpty(CIRBUF_RX_UART1)
607 
608 
617 #define serUart1Read() cbufGetByte(CIRBUF_RX_UART1)
618 #define serUart1GetByte() cbufGetByte(CIRBUF_RX_UART1)
619 
620 
632 #define serUart1ReadArray(pDstArr, size) cbufGetArray(CIRBUF_RX_UART1, pDstArr, size)
633 #define serUart1GetArray(pDstArr, size) cbufGetArray(CIRBUF_RX_UART1, pDstArr, size)
634 
635 #endif //#if defined(HAS_SERPORT_UART1)
636 
637 #endif //#if defined(HAS_SERPORT_UART)
638 #endif //#ifndef NZ_SERUART_H