libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
serial.test.h
Go to the documentation of this file.
1 // Copyright 2013 SRLM and Red9
2 
3 #include <propeller.h>
4 #include <stdio.h>
5 
6 #include "unity.h"
7 #include "serial.h"
8 #include "c++-alloc.h"
9 
16 const int rxpin = 18;
17 const int txpin = 19;
18 
19 const int baud = 460800;
20 
21 const int ctspin = 20; //Input to Propeller
22 const int rtspin = 21; //Output from Propeller (currently not used by driver, but driven.
23 
24 const int MAXTIME = 10; //maximum time (in milliseconds) to wait for GetCCheck for tests. Prevents hangups.
25 
27 
28 class UnityTests {
29 public:
30 
31  static void setUp(void) {
32  sut.Start(rxpin, txpin, baud);
33  }
34 
35  static void tearDown(void) {
36  sut.Stop();
37  }
38 
39  // -----------------------------------------------------------------------------
40 
41  static void test_PinsConnected(void) {
42  sut.Stop();
43 
44 
45  DIRA |= 1 << rtspin;
46  DIRA &= ~(1 << ctspin);
47 
48  OUTA |= 1 << rtspin;
49  TEST_ASSERT_TRUE((INA & (1 << ctspin)) != 0);
50 
51  OUTA &= ~(1 << rtspin);
52  TEST_ASSERT_TRUE((INA & (1 << ctspin)) == 0);
53 
54 
55 
56  DIRA |= 1 << txpin;
57  DIRA &= ~(1 << rxpin);
58 
59  OUTA |= 1 << txpin;
60  TEST_ASSERT_TRUE((INA & (1 << rxpin)) != 0);
61 
62  OUTA &= ~(1 << txpin);
63  TEST_ASSERT_TRUE((INA & (1 << rxpin)) == 0);
64  }
65 
66  static void test_Start(void) {
67  sut.Stop();
68  int32_t result = sut.Start(rxpin, txpin, baud);
69  TEST_ASSERT_TRUE(result);
70  }
71 
72  static void test_SetsPinDirectionsCorrectly(void) {
73  sut.Stop();
74 
75  // Swap the expected direction of pins.
76  DIRA |= 1 << rxpin;
77  DIRA &= ~(1 << txpin);
78 
79  sut.Start(rxpin, txpin, baud);
80 
81  const char value = 'A';
82  sut.Put(value);
83  TEST_ASSERT_EQUAL_INT(value, sut.Get(MAXTIME));
84  }
85 
86  static void cog_DoNothing(void * arg) {
87  waitcnt(CLKFREQ * 50 + CNT);
88  }
89 
90  static int help_CountNumberOfFreeCogs(void) {
91  const int stacksize = sizeof (_thread_state_t) + sizeof (int) * 10;
92  int * cog_stack = (int*) malloc(stacksize);
93  int cog_id = cogstart(cog_DoNothing, NULL, cog_stack, stacksize);
94 
95  int free_cogs = 0;
96 
97  if (cog_id != -1) {
98  free_cogs = help_CountNumberOfFreeCogs() + 1;
99  cogstop(cog_id);
100  }
101 
102  free(cog_stack);
103 
104  return free_cogs;
105  }
106 
107  static void test_StopStopsCog(void) {
108  int beforeCount = help_CountNumberOfFreeCogs();
109  sut.Stop();
110  TEST_ASSERT_EQUAL_INT(beforeCount + 1, help_CountNumberOfFreeCogs());
111  }
112 
113  static void test_DestructorCallsStop(void) {
114  sut.Stop();
115 
116  int beforeCount = help_CountNumberOfFreeCogs();
117  {
118  Serial temp;
119  temp.Start(rxpin, txpin, baud);
120  TEST_ASSERT_EQUAL_INT(beforeCount - 1, help_CountNumberOfFreeCogs());
121  }
122 
123  TEST_ASSERT_EQUAL_INT(beforeCount, help_CountNumberOfFreeCogs());
124  }
125 
126  // -----------------------------------------------------------------------------
127  // Single or Few Character Checks
128  // -----------------------------------------------------------------------------
129 
130  static void test_PutCGetC(void) {
131  sut.Put('a');
132  TEST_ASSERT_EQUAL_INT('a', sut.Get(MAXTIME));
133  }
134 
135  static void test_PutCGetCLowerByteBound(void) {
136  sut.Put('\0');
137  TEST_ASSERT_EQUAL_INT(0, sut.Get(MAXTIME));
138  }
139 
140  static void test_PutCGetCUpperByteBound(void) {
141  sut.Put(255);
142  TEST_ASSERT_EQUAL_INT(255, sut.Get(MAXTIME));
143  }
144 
145  static void test_GetCCheckNoPutC(void) {
146  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME));
147  }
148 
149  static void test_GetCCheck(void) {
150  sut.Put('G');
151  TEST_ASSERT_EQUAL_INT('G', sut.Get(MAXTIME));
152  }
153 
154  static void test_GetCCheckNoWait(void) {
155  sut.Put('T');
156  TEST_ASSERT_EQUAL_INT('T', sut.Get(1));
157  }
158 
159  static void test_GetCCheckTimeTimeout(void) {
160  TEST_ASSERT_EQUAL_INT(-1, sut.Get(1));
161  }
162 
163  static void test_GetCValidateTimeoutDuration(void) {
164 
165  const int kTimeoutDuration = 10 * 1000;
166  const int kMaximumTimeoutDelta = 100;
167 
168  for (int i = 0; i < 10; i++) {
169  unsigned int start_CNT = CNT;
170  sut.Get(kTimeoutDuration / 1000);
171  unsigned int actual_timeout_duration = (CNT - start_CNT) / (CLKFREQ / 1000000);
172  TEST_ASSERT_INT_WITHIN(kMaximumTimeoutDelta, kTimeoutDuration, actual_timeout_duration);
173  }
174 
175 
176  }
177 
178  static void test_RxTxNoExtraTxChars(void) {
179  sut.Put('z');
180  sut.Get(MAXTIME);
181  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME));
182  }
183 
184  // -----------------------------------------------------------------------------
185  // String and Long Buffer Tests
186  // -----------------------------------------------------------------------------
187 
188  static void test_PutBufferFormatted(void) {
189  // sut.GetFlush();
190 
191  TEST_ASSERT_EQUAL_INT(3, sut.PutFormatted("abc"));
192 
193 
194 
195  TEST_ASSERT_EQUAL_INT('a', sut.Get(MAXTIME));
196  TEST_ASSERT_EQUAL_INT('b', sut.Get(MAXTIME));
197  TEST_ASSERT_EQUAL_INT('c', sut.Get(MAXTIME));
198  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME));
199  }
200 
201  //static void test_PutBufferNoSize(void)
202  //{
203  // sut.Put("abc", 0);
204  // TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME));
205  //}
206 
207  static void test_PutBufferNoPointer(void) {
208  TEST_ASSERT_EQUAL_INT(0, sut.PutFormatted((char *) NULL));
209  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME));
210  }
211 
212  //static void test_PutBufferNoWaitActuallyDoesntWait(void)
213  //{
214  // //If this test fails, it's possible because PutBufferNoWait() ends up waiting,
215  // //so that the GetFlush empties out all the bytes it will receive. In that
216  // //case, there are no bytes left to receive and it times out.
217  // sut.PutBufferNoWait("123456789012345678901234567890", 30);
218  // sut.GetFlush();
219  // int result = sut.Get(MAXTIME);
220  // TEST_ASSERT_TRUE(result != -1);
221  //}
222 
223  static void test_PutS(void) {
224  // char * volatile temp = new char(6);
225  // temp[0] = 'H';
226  // temp[1] = 'e';
227  // temp[2] = 'l';
228  // temp[3] = 'l';
229  // temp[4] = 'o';
230  // temp[5] = '\0';
231  //
232  //
233  // TEST_ASSERT_EQUAL_INT(5, sut.Put(temp));
234  //
235  // volatile char message[] = "Hello";
236  // sut.PutBuffer(message);
237 
238 
239  TEST_ASSERT_EQUAL_INT(5, sut.PutFormatted("Hello"));
240  TEST_ASSERT_EQUAL_INT('H', sut.Get(MAXTIME));
241  TEST_ASSERT_EQUAL_INT('e', sut.Get(MAXTIME));
242  TEST_ASSERT_EQUAL_INT('l', sut.Get(MAXTIME));
243  TEST_ASSERT_EQUAL_INT('l', sut.Get(MAXTIME));
244  TEST_ASSERT_EQUAL_INT('o', sut.Get(MAXTIME));
245  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME));
246 
247 
248 
249  }
250 
251  // -----------------------------------------------------------------------------
252  // Extra Functions Test
253  // -----------------------------------------------------------------------------
254 
255  static void test_GetFlush(void) {
256  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME)); //Empty before test
257  sut.Put('1');
258  waitcnt(CLKFREQ / 100 + CNT); //In LMM mode, it's too fast to flush...
259  sut.GetFlush();
260  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME)); //Empty after flush
261  }
262 
263  static void test_GetFlushEmptyBuffer(void) {
264  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME)); //Empty before test
265  sut.GetFlush();
266  TEST_ASSERT_EQUAL_INT(-1, sut.Get(MAXTIME)); //Empty after flush
267  }
268 
269 
270 
271  // -----------------------------------------------------------------------------
272  // Baud Checks
273  // -----------------------------------------------------------------------------
274 
275  static void test_SetBaud(void) {
276  TEST_ASSERT_TRUE(sut.SetBaud(9600));
277  }
278 
279  static void test_SetBaudTooHigh(void) {
280  TEST_ASSERT_EQUAL_INT(0, sut.SetBaud(1000000));
281  }
282 
283  static void test_SetBaudToZero(void) {
284  TEST_ASSERT_EQUAL_INT(0, sut.SetBaud(0));
285  }
286 
287  static void test_SetBaudTransmitAfterBaudChange(void) {
288  sut.SetBaud(9600);
289  sut.Put('a');
290  TEST_ASSERT_EQUAL_INT('a', sut.Get(MAXTIME));
291  }
292 
293  static void test_Setbaudclock(void) {
294  TEST_ASSERT_TRUE(sut.SetBaudClock(9600, 80000000));
295  }
296 
297  // -----------------------------------------------------------------------------
298 
299  static void test_PutPrintfReturnsWrittenBytes(void) {
300  int size = 30;
301  char inputBuffer[size];
302 
303  TEST_ASSERT_EQUAL_INT(17, sut.PutFormatted("My:%i, Your:%i", 123, -531));
304  TEST_ASSERT_EQUAL_INT(17, sut.Get(inputBuffer, 17, MAXTIME));
305  sut.GetFlush();
306  }
307 
308  static void test_PutPrintfBasic(void) {
309 
310  int size = 30;
311  char inputBuffer[size];
312  for (int i = 0; i < size; i++) {
313  inputBuffer[i] = 0;
314  }
315 
316  sut.PutFormatted("My number: %i.", 123);
317  sut.Get(inputBuffer, 15, MAXTIME);
318  TEST_ASSERT_EQUAL_STRING("My number: 123.", inputBuffer);
319 
320 
321  }
322 
323  static void test_PutPrintfMultipleIntegers(void) {
324  int size = 30;
325  char inputBuffer[size];
326  for (int i = 0; i < size; i++) {
327  inputBuffer[i] = 0;
328  }
329 
330  sut.PutFormatted("My:%i, Your:%i", 123, -531);
331  sut.Get(inputBuffer, 17, MAXTIME);
332  TEST_ASSERT_EQUAL_STRING("My:123, Your:-531", inputBuffer);
333  }
334 
335  static void test_PutPrintfNoSpecifiers(void) {
336  char string[] = "Hello, World.";
337  int size = 30;
338  char inputBuffer[size];
339  for (int i = 0; i < size; i++) {
340  inputBuffer[i] = 0;
341  }
342 
343  sut.PutFormatted(string);
344  sut.Get(inputBuffer, 13, MAXTIME);
345  TEST_ASSERT_EQUAL_STRING(string, inputBuffer);
346  }
347 
348  static void test_PutPrintfHexSpecifiers(void) {
349  int size = 30;
350  char inputBuffer[size];
351  for (int i = 0; i < size; i++) {
352  inputBuffer[i] = 0;
353  }
354  sut.PutFormatted("My:%x, Your:%X", 0xAB, 0xCDE);
355  sut.Get(inputBuffer, 15, MAXTIME);
356  TEST_ASSERT_EQUAL_STRING("My:AB, Your:CDE", inputBuffer);
357  }
358 
359  static void test_PutPrintfDecpad(void) {
360  int size = 30;
361  char inputBuffer[size];
362  for (int i = 0; i < size; i++) {
363  inputBuffer[i] = 0;
364  }
365  sut.PutFormatted("My:%10d", 1234);
366  sut.Get(inputBuffer, 13, MAXTIME);
367  TEST_ASSERT_EQUAL_STRING("My: 1234", inputBuffer);
368  }
369 
370  static void test_PutPrintfDecpadSmaller(void) {
371  int size = 30;
372  char inputBuffer[size];
373  for (int i = 0; i < size; i++) {
374  inputBuffer[i] = 0;
375  }
376  sut.PutFormatted("My:%2d", 1234);
377  sut.Get(inputBuffer, 13, MAXTIME);
378  TEST_ASSERT_EQUAL_STRING("My:1234", inputBuffer);
379  }
380 
381  static void test_PutPrinfHexpad(void) {
382  int size = 30;
383  char inputBuffer[size];
384  for (int i = 0; i < size; i++) {
385  inputBuffer[i] = 0;
386  }
387  sut.PutFormatted("My:%10x", 0x1234);
388  sut.Get(inputBuffer, 13, MAXTIME);
389  TEST_ASSERT_EQUAL_STRING("My: 1234", inputBuffer);
390  }
391 
392  static void test_PutPrinfHexpadTooSmall(void) {
393  int size = 30;
394  char inputBuffer[size];
395  for (int i = 0; i < size; i++) {
396  inputBuffer[i] = 0;
397  }
398  sut.PutFormatted("My:%2x", 0x1234);
399  sut.Get(inputBuffer, 13, MAXTIME);
400  TEST_ASSERT_EQUAL_STRING("My:1234", inputBuffer);
401  }
402 
403  static void test_PutPrinfHexpadZero(void) {
404  int size = 30;
405  char inputBuffer[size];
406  for (int i = 0; i < size; i++) {
407  inputBuffer[i] = 0;
408  }
409  sut.PutFormatted("My:%010x", 0x1234);
410  sut.Get(inputBuffer, 13, MAXTIME);
411  TEST_ASSERT_EQUAL_STRING("My:0000001234", inputBuffer);
412  }
413 
414  static void test_PutPrintfChar(void) {
415  int size = 30;
416  char inputBuffer[size];
417  for (int i = 0; i < size; i++) {
418  inputBuffer[i] = 0;
419  }
420  sut.PutFormatted("My:%c", 'a');
421  sut.Get(inputBuffer, 4, MAXTIME);
422  TEST_ASSERT_EQUAL_STRING("My:a", inputBuffer);
423  }
424 
425  static void test_PutPrintfString(void) {
426  int size = 30;
427  char inputBuffer[size];
428  for (int i = 0; i < size; i++) {
429  inputBuffer[i] = 0;
430  }
431  sut.PutFormatted("My:%s", "World");
432  sut.Get(inputBuffer, 8, MAXTIME);
433  TEST_ASSERT_EQUAL_STRING("My:World", inputBuffer);
434  }
435 
436  static void test_PutPrinfAllTogether(void) {
437  int size = 30;
438  char inputBuffer[size];
439  for (int i = 0; i < size; i++) {
440  inputBuffer[i] = 0;
441  }
442  sut.PutFormatted("%x%i%s%c%03x%4i", 0x23, 32, "hello", 'w', 0xF, 13);
443  sut.Get(inputBuffer, 17, MAXTIME);
444  TEST_ASSERT_EQUAL_STRING("2332hellow00F 13", inputBuffer);
445  }
446 
447  static void test_PutPrintfPercentSignAtEndOfStringDisappears(void) {
448  const int size = strlen("Hello");
449  char inputBuffer[size];
450  sut.PutFormatted("Hello'%");
451  sut.Put("'");
452  sut.Get(inputBuffer, size, MAXTIME);
453  TEST_ASSERT_EQUAL_MEMORY("Hello''", inputBuffer, size);
454 
455  }
456 
457  static void test_PutPrintfTwoPercentSigns(void) {
458  const int size = strlen("Hello% ");
459  char inputBuffer[size];
460  sut.PutFormatted("Hello%% ");
461  sut.Get(inputBuffer, size, MAXTIME);
462  TEST_ASSERT_EQUAL_MEMORY("Hello% ", inputBuffer, size);
463 
464  }
465  // -----------------------------------------------------------------------------
466 
467  static void test_GetBuffer(void) {
468  char string[] = "Hello World!";
469  int size = 12;
470  char inputBuffer[size + 1];
471  inputBuffer[size] = 0; //null terminate
472  sut.PutFormatted(string);
473  TEST_ASSERT_EQUAL_INT(size, sut.Get(inputBuffer, size, MAXTIME));
474  TEST_ASSERT_EQUAL_STRING(string, inputBuffer);
475  }
476 
477  static void test_GetBufferString(void) {
478  char string[] = "Hello World!\n";
479  int size = 13;
480  char buffer[50];
481  sut.PutFormatted(string);
482  TEST_ASSERT_EQUAL_INT(size, sut.Get(buffer, '\n'));
483  TEST_ASSERT_EQUAL_STRING(string, buffer);
484  }
485 
486  // -----------------------------------------------------------------------------
487 
488  static void transmitAlphabet(void * param) {
489  // ~60 ms long
490  for (int i = 'A'; i <= 'z'; i++) {
491  sut.Put(i);
492  waitcnt(CLKFREQ / 1000 + CNT);
493  }
494 
495 
496  cogstop(cogid());
497  }
498 
499  static void test_CTSPinBasic(void) {
500  sut.Stop();
501  sut.Start(rxpin, txpin, baud, ctspin);
502 
503  int stacksize = sizeof (_thread_state_t) + sizeof (int)*8;
504  int *stack = (int*) malloc(stacksize);
505  cogstart(transmitAlphabet, NULL, stack, stacksize);
506 
507  waitcnt(CLKFREQ * 5 / 1000 + CNT); // Give it some time to transmit a few chars
508 
509  // Turn off transmission
510  DIRA |= 1 << rtspin;
511  OUTA |= 1 << rtspin;
512 
513  // Flush out buffer
514  int current = 0;
515  int last = current;
516  while (current != -1) {
517  last = current;
518  current = sut.Get(MAXTIME);
519  }
520 
521  //At this point, no more characters.
522 
523  // Turn on transmission
524  OUTA &= ~(1 << rtspin);
525  waitcnt(CLKFREQ / 10 + CNT); // Give it time to transmit the last of it's characters
526 
527  for (int i = last + 1; i <= 'z'; i++) {
528  TEST_ASSERT_EQUAL_INT(i, sut.Get(MAXTIME));
529  }
530 
531  TEST_ASSERT_EQUAL_INT(-1, sut.Get(0));
532  }
533 
534  // -----------------------------------------------------------------------------
535 
536  static void test_PutBuffer(void) {
537  char data [] = "Hello, long string!";
538  const int length = strlen(data) + 1;
539 
540  char inputbuffer[length];
541 
542  TEST_ASSERT_EQUAL_INT(length, sut.Put(data, length));
543 
544  sut.Get(inputbuffer, length, MAXTIME);
545 
546  TEST_ASSERT_EQUAL_STRING(data, inputbuffer);
547 
548  }
549 
550  static void test_PutBufferNullTerminatedString(void) {
551  char data [] = "Hello, long string!";
552  const int length = strlen(data);
553 
554  char inputbuffer[length];
555 
556  TEST_ASSERT_EQUAL_INT(length, sut.Put(data));
557 
558  sut.Get(inputbuffer, length, MAXTIME);
559 
560  TEST_ASSERT_EQUAL_MEMORY(data, inputbuffer, length);
561  }
562 
563 
564  // -----------------------------------------------------------------------------
565 
566  static void test_GetCountEmpty(void) {
567  TEST_ASSERT_EQUAL_INT(0, sut.GetCount());
568  }
569 
570  static void test_GetCountOne(void) {
571  sut.Put(32);
572  waitcnt(CLKFREQ / 100 + CNT);
573  TEST_ASSERT_EQUAL_INT(1, sut.GetCount());
574  }
575 
576  static void test_GetCountFew(void) {
577  for (int i = 0; i < Serial::kBufferLength / 2; i++) {
578  sut.Put(i);
579  }
580  waitcnt(CLKFREQ / 100 + CNT);
581  TEST_ASSERT_EQUAL_INT(Serial::kBufferLength / 2, sut.GetCount());
582  }
583 
584  static void test_GetCountWrapAround(void) {
585  for (int i = 0; i < Serial::kBufferLength - 1; i++) {
586  sut.Put(i);
587  sut.Get(i);
588  }
589 
590  sut.Put(32);
591  sut.Put(33);
592  sut.Put(34);
593 
594  waitcnt(CLKFREQ / 100 + CNT);
595  TEST_ASSERT_EQUAL_INT(3, sut.GetCount());
596  }
597 
598 
599 
600 
601 };
602 
603 
604 
605 
606 
607 
608 
609 
610 
611 
612 
613 
614 
615 
616 
617 
618 
619 
620 
621 
622 
623 
624 
625 
626 
627 
628 
629 
630 
631 
632 
633 
634 
635 
636 
637 
638 
639 
640 
641 
642 
643 
644 
645 
646 
647 
648 
649 
650 
651 
652