[Updated on OS6] TimerEvent is no longer existing!! Checked TimerEvent function on os5 latest version. Result is okay on FRDM-K64F but STM32 series are NOT okay e.g. Nucleo-L152RE, -F446RE, -L432KC and so on. This TimerEvent is using SoftSerial library and works well on old os2 but not OS5 latest version STM32 series Mbed board.

Forum discussion.
https://forums.mbed.com/t/how-to-port-software-serial-to-os5-6/12641
https://forums.mbed.com/t/softserial-problem-l432kc/8288

Committer:
kenjiArai
Date:
Tue Mar 30 07:30:39 2021 +0000
Revision:
1:3e3b7ec1f33d
Parent:
0:0a78edc7ac85
checked on OS6.8.0 (Online compile) & OS6.9.0(Mbed Studio 1.4.0 GCC)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:0a78edc7ac85 1 /*
kenjiArai 1:3e3b7ec1f33d 2 * Mbed-OS5 & OS-2 --> Added OS6
kenjiArai 1:3e3b7ec1f33d 3 * Check EventTimer(on OS6, change name Ticker) behavior
kenjiArai 0:0a78edc7ac85 4 *
kenjiArai 1:3e3b7ec1f33d 5 * Copyright (c) 2020,'21 Kenji Arai / JH1PJL
kenjiArai 0:0a78edc7ac85 6 * http://www7b.biglobe.ne.jp/~kenjia/
kenjiArai 0:0a78edc7ac85 7 * https://os.mbed.com/users/kenjiArai/
kenjiArai 1:3e3b7ec1f33d 8 * Created: May 12th, 2020
kenjiArai 1:3e3b7ec1f33d 9 * Revised: March 30th, 2021
kenjiArai 1:3e3b7ec1f33d 10 */
kenjiArai 1:3e3b7ec1f33d 11 /*
kenjiArai 1:3e3b7ec1f33d 12 test result target = 50uS
kenjiArai 1:3e3b7ec1f33d 13 Nucleo-L432KC
kenjiArai 1:3e3b7ec1f33d 14 2.162
kenjiArai 1:3e3b7ec1f33d 15 0= 223, 1= 51, 2= 49, 3= 50, 4= 51, 5= 49, 6= 50, 7= 51, 8= 49
kenjiArai 1:3e3b7ec1f33d 16 5.15.3
kenjiArai 1:3e3b7ec1f33d 17 0= 109, 1= 50, 2= 50, 3= 48, 4= 51, 5= 52, 6= 47, 7= 52, 8= 48
kenjiArai 1:3e3b7ec1f33d 18 6.8.0
kenjiArai 1:3e3b7ec1f33d 19 0= 613, 1= 51, 2= 50, 3= 51, 4= 50, 5= 51, 6= 50, 7= 51, 8= 50
kenjiArai 1:3e3b7ec1f33d 20
kenjiArai 1:3e3b7ec1f33d 21 Nucleo-F446RE
kenjiArai 1:3e3b7ec1f33d 22 2.162
kenjiArai 1:3e3b7ec1f33d 23 0= 85, 1= 50, 2= 50, 3= 50, 4= 50, 5= 50, 6= 50, 7= 50, 8= 50
kenjiArai 1:3e3b7ec1f33d 24 5.15.3
kenjiArai 1:3e3b7ec1f33d 25 0= 136, 1= 50, 2= 50, 3= 50, 4= 50, 5= 50, 6= 50, 7= 50, 8= 50
kenjiArai 1:3e3b7ec1f33d 26 6.8.0
kenjiArai 1:3e3b7ec1f33d 27 0= 138, 1= 50, 2= 50, 3= 50, 4= 50, 5= 50, 6= 50, 7= 50, 8= 50
kenjiArai 1:3e3b7ec1f33d 28
kenjiArai 1:3e3b7ec1f33d 29 FRDM-K64F
kenjiArai 1:3e3b7ec1f33d 30 2.162
kenjiArai 1:3e3b7ec1f33d 31 0= 61, 1= 51, 2= 49, 3= 51, 4= 49, 5= 51, 6= 49, 7= 51, 8= 49
kenjiArai 1:3e3b7ec1f33d 32 5.15.3
kenjiArai 1:3e3b7ec1f33d 33 0= 61, 1= 53, 2= 49, 3= 48, 4= 49, 5= 53, 6= 49, 7= 49, 8= 50
kenjiArai 1:3e3b7ec1f33d 34 6.8.0
kenjiArai 1:3e3b7ec1f33d 35 0= 66, 1= 51, 2= 48, 3= 50, 4= 51, 5= 51, 6= 51, 7= 47, 8= 50
kenjiArai 1:3e3b7ec1f33d 36
kenjiArai 1:3e3b7ec1f33d 37
kenjiArai 0:0a78edc7ac85 38 */
kenjiArai 0:0a78edc7ac85 39
kenjiArai 0:0a78edc7ac85 40 #include "mbed.h"
kenjiArai 0:0a78edc7ac85 41
kenjiArai 1:3e3b7ec1f33d 42 #if (MBED_MAJOR_VERSION == 6)
kenjiArai 1:3e3b7ec1f33d 43 class FlexTicker: public Ticker
kenjiArai 1:3e3b7ec1f33d 44 #else
kenjiArai 0:0a78edc7ac85 45 class FlexTicker: public TimerEvent
kenjiArai 1:3e3b7ec1f33d 46 #endif
kenjiArai 0:0a78edc7ac85 47 {
kenjiArai 0:0a78edc7ac85 48 public:
kenjiArai 0:0a78edc7ac85 49 void attach(void(*fptr)(void)) { _function = Callback<void()>(fptr);}
kenjiArai 0:0a78edc7ac85 50 void detach() { remove();}
kenjiArai 0:0a78edc7ac85 51 void setNext(int delay) { insert(event.timestamp + delay);}
kenjiArai 1:3e3b7ec1f33d 52 void prime() { event.timestamp = us_ticker_read();}
kenjiArai 0:0a78edc7ac85 53 protected:
kenjiArai 0:0a78edc7ac85 54 virtual void handler() { _function.call();}
kenjiArai 0:0a78edc7ac85 55 unsigned int _delay;
kenjiArai 0:0a78edc7ac85 56 Callback<void()> _function;
kenjiArai 0:0a78edc7ac85 57 };
kenjiArai 0:0a78edc7ac85 58
kenjiArai 0:0a78edc7ac85 59 DigitalOut led(LED1);
kenjiArai 0:0a78edc7ac85 60 FlexTicker tk;
kenjiArai 0:0a78edc7ac85 61
kenjiArai 0:0a78edc7ac85 62 #define TARGET_TIME 50 // 50us
kenjiArai 0:0a78edc7ac85 63
kenjiArai 0:0a78edc7ac85 64 volatile uint32_t num;
kenjiArai 0:0a78edc7ac85 65 uint32_t time_data[10];
kenjiArai 0:0a78edc7ac85 66
kenjiArai 0:0a78edc7ac85 67 // call by TimerEvent
kenjiArai 0:0a78edc7ac85 68 void pulse_out(void)
kenjiArai 0:0a78edc7ac85 69 {
kenjiArai 0:0a78edc7ac85 70 led = 1;
kenjiArai 0:0a78edc7ac85 71 num++;
kenjiArai 0:0a78edc7ac85 72 time_data[num] = us_ticker_read();
kenjiArai 0:0a78edc7ac85 73 if (num < 10) {
kenjiArai 0:0a78edc7ac85 74 tk.setNext(TARGET_TIME);
kenjiArai 0:0a78edc7ac85 75 }
kenjiArai 0:0a78edc7ac85 76 led = 0;
kenjiArai 0:0a78edc7ac85 77 }
kenjiArai 0:0a78edc7ac85 78
kenjiArai 0:0a78edc7ac85 79 int main()
kenjiArai 0:0a78edc7ac85 80 {
kenjiArai 0:0a78edc7ac85 81 printf("MBED_MAJOR_VERSION = %d, ", MBED_MAJOR_VERSION);
kenjiArai 0:0a78edc7ac85 82 printf("MINOR = %d, ", MBED_MINOR_VERSION);
kenjiArai 0:0a78edc7ac85 83 printf("PATCH = %d\r\n", MBED_PATCH_VERSION);
kenjiArai 0:0a78edc7ac85 84 tk.attach(&pulse_out);
kenjiArai 0:0a78edc7ac85 85 while(true) {
kenjiArai 0:0a78edc7ac85 86 led = 1;
kenjiArai 0:0a78edc7ac85 87 num = 0;
kenjiArai 0:0a78edc7ac85 88 time_data[0] = us_ticker_read();
kenjiArai 0:0a78edc7ac85 89 tk.prime();
kenjiArai 0:0a78edc7ac85 90 led = 0;
kenjiArai 0:0a78edc7ac85 91 tk.setNext(TARGET_TIME);
kenjiArai 1:3e3b7ec1f33d 92 #if (MBED_MAJOR_VERSION == 6)
kenjiArai 1:3e3b7ec1f33d 93 ThisThread::sleep_for(500ms);
kenjiArai 1:3e3b7ec1f33d 94 #elif (MBED_MAJOR_VERSION == 5)
kenjiArai 0:0a78edc7ac85 95 thread_sleep_for(500);
kenjiArai 1:3e3b7ec1f33d 96 #else
kenjiArai 1:3e3b7ec1f33d 97 wait_ms(500);
kenjiArai 1:3e3b7ec1f33d 98 #endif
kenjiArai 0:0a78edc7ac85 99 for (uint32_t i = 0; i < 9; i++) {
kenjiArai 0:0a78edc7ac85 100 printf("%d=%4d, ", i, time_data[i+1] - time_data[i]);
kenjiArai 0:0a78edc7ac85 101 }
kenjiArai 1:3e3b7ec1f33d 102 printf("(<- No=time[us] (target %dus))\r\n", TARGET_TIME);
kenjiArai 0:0a78edc7ac85 103 }
kenjiArai 0:0a78edc7ac85 104 }