1 #ifndef libpropeller_full_duplex_serial_h_
2 #define libpropeller_full_duplex_serial_h_
8 static inline int Rotl__(
unsigned int a,
unsigned int b) {
9 return (a << b) | (a >> (32 - b));
12 static inline int Lookup__(
int x,
int b,
int a[],
int n) {
14 return ((
unsigned) i >= n) ? 0 : (a)[i];
17 extern char _load_start_full_duplex_serial_cog[];
22 #define buffersize 256
23 #define buffermask (buffersize - 1)
57 void Start(
int Rxpin,
int Txpin,
int Mode,
int Baudrate) {
58 volatile void * asm_reference = NULL;
59 __asm__
volatile (
"mov %[asm_reference], #FullDuplexSerial_Entry \n\t"
60 : [asm_reference]
"+r" (asm_reference));
74 Bit_ticks = (
CLKFREQ / Baudrate);
75 Buffer_ptr = Rx_buffer;
77 Cog =
cognew(_load_start_full_duplex_serial_cog, &Rx_head) + 1;
107 while (CheckBuffer() >= 0) {
123 while ((Rxbyte = CheckBuffer()) < 0) {
127 while (!(((Rxbyte = CheckBuffer()) >= 0)
138 void Put(
const char Txbyte) {
139 while (!(Tx_tail != ((Tx_head + 1) & buffermask))) {
141 Tx_buffer[Tx_head] = Txbyte;
142 Tx_head = ((Tx_head + 1) & buffermask);
143 if (Rxtx_mode & 0x8) {
154 void Put(
const char *
string) {
155 const int length = strlen(
string);
156 for (
int i = 0; i < length; i++) {
178 if (Value == (
int) 0x80000000U) {
188 for (_idx__0043 = 1; _idx__0043 <= 10; (_idx__0043 = (_idx__0043 + 1))) {
190 Put((((Value / I) +
'0')));
194 if ((result) || (I == 1)) {
205 void PutHex(
int Value,
int Digits) {
206 static int look__0044[] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,};
208 Value = (Value << ((8 - Digits) << 2));
211 int _limit__0046 = Digits;
212 for (_idx__0045 = 1; _idx__0045 <= _limit__0046; (_idx__0045 = (_idx__0045 + 1))) {
213 Put(Lookup__(((Value = (Rotl__(Value, 4))) & 0xf), 0, look__0044, 16));
218 void PutBin(
int Value,
int Digits) {
219 Value = (Value << (32 - Digits));
222 int _limit__0048 = Digits;
223 for (_idx__0047 = 1; _idx__0047 <= _limit__0048; (_idx__0047 = (_idx__0047 + 1))) {
224 Put((((Value = (Rotl__(Value, 1))) & 0x1) +
'0'));
233 volatile int Rx_head;
234 volatile int Rx_tail;
235 volatile int Tx_head;
236 volatile int Tx_tail;
239 volatile int Rxtx_mode;
240 volatile int Bit_ticks;
241 volatile char * Buffer_ptr;
242 volatile char Rx_buffer[buffersize];
243 volatile char Tx_buffer[buffersize];
245 int CheckBuffer(
void) {
247 if (Rx_tail != Rx_head) {
248 Rxbyte = Rx_buffer[Rx_tail];
249 Rx_tail = (Rx_tail + 1) & buffermask;
257 #endif // libpropeller_full_duplex_serial_h_