ESOS32
ESOSOn32-bitProcessors
esos_comm.h
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
40 #ifndef ESOS_COMM_H
41 #define ESOS_COMM_H
42 
43 /* I N C L U D E S **********************************************************/
44 #include "esos.h"
45 
46 /* P R O T O T Y P E S ******************************************************/
47 void __esos_InitCommSystem(void);
48 uint8_t __esos_u8_GetMSBHexCharFromUint8(uint8_t u8_x);
49 uint8_t __esos_u8_GetLSBHexCharFromUint8(uint8_t u8_x);
50 ESOS_CHILD_TASK( __esos_OutChar, uint8_t u8_c);
51 ESOS_CHILD_TASK( __esos_OutUint8AsDecString, uint8_t u8_x);
52 ESOS_CHILD_TASK( __esos_OutUint8AsHexString, uint8_t u8_x);
53 ESOS_CHILD_TASK( __esos_OutUint32AsHexString, uint32_t u32_x);
54 ESOS_CHILD_TASK( __esos_OutCharBuffer, uint8_t* pu8_out, uint8_t u8_len);
55 ESOS_CHILD_TASK( __esos_getBuffer, uint8_t* pau8_buff, uint8_t u8_size);
56 ESOS_CHILD_TASK( __esos_getString, char* pau8_buff);
57 ESOS_CHILD_TASK( __esos_OutString, char* psz_out );
58 void __esos_unsafe_PutUint8(uint8_t u8_c);
59 void __esos_unsafe_PutString(char* psz_in);
60 uint8_t __esos_unsafe_GetUint8(void);
61 
62 /* D E F I N E S ************************************************************/
63 #define ESOS_COMM_SYS_USB 0x80
64 #define ESOS_COMM_SYS_SERIAL 0x00
65 #define ESOS_COMM_SYS_SERIAL_REV (ESOS_COMM_SYS_SERIAL + 0x01)
66 // size of buffer to catch data incoming to MCU (based on USB terminology)
67 #define ESOS_SERIAL_OUT_EP_SIZE 64
68 // size of buffer to hold data leaving the MCU (based on USB terminology)
69 #define ESOS_SERIAL_IN_EP_SIZE 64
70 #define DEFAULT_BAUDRATE 57600
71 
72 /***
73  *** A few defines to help make data transfer easier
74  ***/
75 #define __ESOS_COMM_TXFIFO_PREP() \
76  do{u16_tmp = __st_TxBuffer.u16_Head; \
77  u16_tmp++; \
78  if (u16_tmp == ESOS_SERIAL_IN_EP_SIZE) u16_tmp = 0; \
79  } while (0)
80 
81 #define __ESOS_COMM_WRITE_TXFIFO( u8_out ) \
82  do{__st_TxBuffer.pau8_Data[u16_tmp] = (u8_out); \
83  __st_TxBuffer.u16_Head = u16_tmp;} while (0)
84 
85 
86 
87 /* M A C R O S ************************************************************/
88 
89 /*
90  * Evaluates to the number of bytes in the ESOS "in" communications buffer
91  *
92  * \param pstTask A pointer to the ESOS task control structure specific to this task.
93  *
94  * \hideinitializer
95  */
96 
97 /*
98 * Evaluates to the number of bytes in the ESOS "in" communications buffer
99 *
100 * \param pfnTaskFcn pointer to task function
101 * \retval NULLPTR if no more tasks can execute at this time (scheduler is full)
102 * \retval pstTask pointer to the scheduled task's structure
103 *
104 * \hideinitializer
105 */
106 
112 #define GET_ESOS_COMM_IN_DATA_LEN() ((__st_RxBuffer.u16_Head>=__st_RxBuffer.u16_Tail)?(__st_RxBuffer.u16_Head-__st_RxBuffer.u16_Tail):(__st_RxBuffer.u16_Length-__st_RxBuffer.u16_Tail+__st_RxBuffer.u16_Head))
113 
124 #define IS_ESOS_COMM_GOT_EXACTLY_DATA_BYTES(x) (GET_ESOS_COMM_IN_DATA_LEN() == x)
125 
136 #define IS_ESOS_COMM_GOT_AT_LEAST_DATA_BYTES(x) (GET_ESOS_COMM_IN_DATA_LEN() >= x)
137 
147 #define FLUSH_ESOS_COMM_IN_DATA() (__st_RxBuffer.u16_Head = __st_RxBuffer.u16_Tail)
148 
158 #define IS_ESOS_COMM_GOT_IN_DATA() (__st_RxBuffer.u16_Head != __st_RxBuffer.u16_Tail)
159 
160 // should use PEEK... It is unsafe since IRQs can occur at anytime....
178 #define PEEK_ESOS_COMM_IN_DATA(x) ( __st_RxBuffer.pau8_Data[((__st_RxBuffer.u16_Tail+1+x)% ESOS_SERIAL_OUT_EP_SIZE)] )
179 
196 #define PEEK_ESOS_COMM_IN_LATEST_DATA() ( __st_RxBuffer.pau8_Data[__st_RxBuffer.u16_Head] )
197 
207 #define IS_ESOS_COMM_READY_OUT_DATA() (__st_TxBuffer.u16_Head != __st_TxBuffer.u16_Tail)
208 
209 // communications commands used by ESOS tasks
216 #define ESOS_TASK_WAIT_ON_AVAILABLE_IN_COMM() \
217  ESOS_TASK_WAIT_WHILE( __esos_IsSystemFlagSet( __ESOS_SYS_COMM_RX_IS_BUSY ) ); \
218  __esos_SetSystemFlag( __ESOS_SYS_COMM_RX_IS_BUSY )
219 
227 #define ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM() \
228  ESOS_TASK_WAIT_WHILE( __esos_IsSystemFlagSet( __ESOS_SYS_COMM_TX_IS_BUSY ) ); \
229  __esos_SetSystemFlag( __ESOS_SYS_COMM_TX_IS_BUSY )
230 
238 #define ESOS_TASK_SIGNAL_AVAILABLE_IN_COMM() __esos_ClearSystemFlag( __ESOS_SYS_COMM_RX_IS_BUSY )
239 
246 #define ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM() __esos_ClearSystemFlag( __ESOS_SYS_COMM_TX_IS_BUSY )
247 
257 #define ESOS_TASK_RELEASE_IN_COMM() ESOS_TASK_SIGNAL_AVAILABLE_IN_COMM()
258 
267 #define ESOS_TASK_RELEASE_OUT_COMM() ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM()
268 
269 
270 // **************** spawn child tasks to do getting and putting to comm streams ***********************
284 #define ESOS_TASK_WAIT_ON_GET_UINT8( u8_in ) \
285  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskRx, __esos_getBuffer, &(u8_in), 1 )
286 
301 #define ESOS_TASK_WAIT_ON_GET_U8BUFFER( pau8_in, u8_size) \
302  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE) &__stChildTaskRx, __esos_getBuffer, (pau8_in), (u8_size) )
303 
317 #define ESOS_TASK_WAIT_ON_GET_UINT16( u16_in ) \
318  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskRx, __esos_getBuffer, (uint8_t*) &(u16_in), 2 )
319 
333 #define ESOS_TASK_WAIT_ON_GET_UINT32( u32_in ) \
334  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskRx, __esos_getBuffer, (uint8_t*) &(u32_in), 4 )
335 
336 
337 
356 #define ESOS_TASK_WAIT_ON_GET_STRING( pau8_in ) \
357  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskRx, __esos_getString, (pau8_in) )
358 
367 #define ESOS_TASK_WAIT_ON_SEND_UINT8( u8_out) \
368  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE) &__stChildTaskTx, __esos_OutChar, (u8_out) )
369 
379 #define ESOS_TASK_WAIT_ON_SEND_UINT8_AS_HEX_STRING( u8_out) \
380  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskTx, __esos_OutUint8AsHexString, (u8_out) )
381 
382 
392 #define ESOS_TASK_WAIT_ON_SEND_UINT8_AS_DEC_STRING( u8_out) \
393  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskTx, __esos_OutUint8AsDecString,(u8_out))
394 
403 #define ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( u32_out) \
404  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskTx, __esos_OutUint32AsHexString, (u32_out) )
405 
414 #define ESOS_TASK_WAIT_ON_SEND_STRING( psz_out) \
415  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskTx, __esos_OutString, (psz_out) )
416 
426 #define ESOS_TASK_WAIT_ON_SEND_U8BUFFER( pau8_out, u8_size) \
427  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskTx, __esos_OutCharBuffer, (pau8_out), (u8_size) )
428 
429 
430 /* S T R U C T U R E S ******************************************************/
431 
432 
433 /* E X T E R N S ************************************************************/
434 extern volatile uint8_t __esos_comm_tx_buff[ESOS_SERIAL_IN_EP_SIZE];
435 extern volatile uint8_t __esos_comm_rx_buff[ESOS_SERIAL_OUT_EP_SIZE];
436 extern volatile struct stTask __stChildTaskTx, __stChildTaskRx;
437 
438 /* P U B L I C P R O T O T Y P E S *****************************************/
444 uint8_t esos_GetCommSystemVersion(void);
445 
451 uint8_t esos_GetCommSystemMaxOutDataLen(void);
452 
458 uint8_t esos_GetCommSystemMaxInDataLen(void);
459 
460 void __esos_InitCommSystem(void);
461 
462 /* prototypes of the unsafe comm functions provided by ESOS */
463 void __esos_unsafe_PutUint8(uint8_t u8_c);
464 void __esos_unsafe_PutString(char* psz_in);
465 uint8_t __esos_unsafe_GetUint8(void);
466 
467 /* prototypes of external functions provided by hardware */
468 void __esos_hw_signal_start_tx(void);
469 void __esos_hw_signal_stop_tx(void);
470 void __esos_hw_InitCommSystem(void);
471 
474 #endif // ESOS_COMM_H
esos_GetCommSystemVersion
uint8_t esos_GetCommSystemVersion(void)
esos.h
esos_GetCommSystemMaxOutDataLen
uint8_t esos_GetCommSystemMaxOutDataLen(void)
Definition: esos_hwxxxx_rs232.c:222
ESOS_CHILD_TASK
#define ESOS_CHILD_TASK(taskname,...)
Definition: esos_task.h:246
stTask
Definition: esos_task.h:54
esos_GetCommSystemMaxInDataLen
uint8_t esos_GetCommSystemMaxInDataLen(void)
Definition: esos_hwxxxx_rs232.c:194