This is sample program for Nucleo L152RE (and F401RE & F411RE) mbed-rtos. You need to modify mbed-src and mbed-rtos before compile it.

Dependencies:   mbed-rtos mbed-src SetRTC

Fork of GR-PEACH_test_on_rtos_works_well by Kenji Arai

Please refer below link.
/users/kenjiArai/notebook/necleo-l152re-rtos-sample-also-for-f401re--f411re-/

Committer:
kenjiArai
Date:
Thu Feb 12 07:53:25 2015 +0000
Revision:
9:de986e74bd93
Parent:
8:4006b111c0d4
Child:
10:1c0f58b9c048
use standard library mbed & mbed-rtos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 4:76b3113c79ff 1 /*
kenjiArai 4:76b3113c79ff 2 * mbed Application program
kenjiArai 4:76b3113c79ff 3 *
kenjiArai 4:76b3113c79ff 4 * Copyright (c) 2010-2014 Kenji Arai / JH1PJL
kenjiArai 4:76b3113c79ff 5 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 4:76b3113c79ff 6 * http://mbed.org/users/kenjiArai/
kenjiArai 4:76b3113c79ff 7 * Created: May 15th, 2010
kenjiArai 4:76b3113c79ff 8 * Spareted: June 25th, 2014 mon() & mon_hw()
kenjiArai 4:76b3113c79ff 9 * Ported: July 12th, 2014 from L152RE
kenjiArai 8:4006b111c0d4 10 * Revised: Feburary 8th, 2015
kenjiArai 4:76b3113c79ff 11 *
kenjiArai 4:76b3113c79ff 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
kenjiArai 4:76b3113c79ff 13 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
kenjiArai 4:76b3113c79ff 14 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kenjiArai 4:76b3113c79ff 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 4:76b3113c79ff 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kenjiArai 4:76b3113c79ff 17 */
kenjiArai 4:76b3113c79ff 18
kenjiArai 4:76b3113c79ff 19 // Include ---------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 20 #include "mbed.h"
kenjiArai 4:76b3113c79ff 21 #include "rtos.h"
kenjiArai 4:76b3113c79ff 22
kenjiArai 4:76b3113c79ff 23 // Object ----------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 24 Serial pc(USBTX, USBRX);
kenjiArai 4:76b3113c79ff 25
kenjiArai 4:76b3113c79ff 26 // Definition ------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 27 #define BAUD(x) pc.baud(x)
kenjiArai 4:76b3113c79ff 28 #define GETC(x) pc.getc(x)
kenjiArai 8:4006b111c0d4 29 #define PUTC(x) pc.putc(x);
kenjiArai 4:76b3113c79ff 30 #define PRINTF(...) pc.printf(__VA_ARGS__)
kenjiArai 4:76b3113c79ff 31 #define READABLE(x) pc.readable(x)
kenjiArai 4:76b3113c79ff 32
kenjiArai 4:76b3113c79ff 33 // RAM -------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 34 static char linebuf[64];
kenjiArai 4:76b3113c79ff 35
kenjiArai 4:76b3113c79ff 36 extern float fa[3]; // Acc 0:X, 1:Y, 2:Z
kenjiArai 4:76b3113c79ff 37 extern float fg[3]; // Gyro 0:X, 1:Y, 2:Z
kenjiArai 8:4006b111c0d4 38 extern uint8_t show_flag0;
kenjiArai 8:4006b111c0d4 39 extern uint8_t show_flag1;
kenjiArai 4:76b3113c79ff 40
kenjiArai 4:76b3113c79ff 41 // ROM / Constant data ---------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 42 static char *const mon_msg = "Monitor for mbed system, created on "__DATE__"";
kenjiArai 4:76b3113c79ff 43
kenjiArai 4:76b3113c79ff 44 // Function prototypes ---------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 45 extern void debug_interface(void);
kenjiArai 4:76b3113c79ff 46 extern int read_sw(uint8_t n);
kenjiArai 4:76b3113c79ff 47
kenjiArai 4:76b3113c79ff 48 //-------------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 49 // Control Program
kenjiArai 4:76b3113c79ff 50 //-------------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 51 // Put \r\n
kenjiArai 4:76b3113c79ff 52 static void put_rn ( void ){
kenjiArai 4:76b3113c79ff 53 Thread::yield(); // change thread
kenjiArai 4:76b3113c79ff 54 PUTC('\r');
kenjiArai 4:76b3113c79ff 55 Thread::yield(); // change thread
kenjiArai 4:76b3113c79ff 56 PUTC('\n');
kenjiArai 4:76b3113c79ff 57 }
kenjiArai 4:76b3113c79ff 58
kenjiArai 4:76b3113c79ff 59 // Put \r
kenjiArai 4:76b3113c79ff 60 static void put_r ( void ){
kenjiArai 4:76b3113c79ff 61 Thread::yield(); // change thread
kenjiArai 4:76b3113c79ff 62 PUTC('\r');
kenjiArai 4:76b3113c79ff 63 }
kenjiArai 4:76b3113c79ff 64
kenjiArai 4:76b3113c79ff 65 // Change string -> integer
kenjiArai 4:76b3113c79ff 66 //int xatoi (char **str, unsigned long *res){
kenjiArai 4:76b3113c79ff 67 static int xatoi (char **str, int32_t *res){
kenjiArai 4:76b3113c79ff 68 unsigned long val;
kenjiArai 4:76b3113c79ff 69 unsigned char c, radix, s = 0;
kenjiArai 4:76b3113c79ff 70
kenjiArai 4:76b3113c79ff 71 while ((c = **str) == ' ') (*str)++;
kenjiArai 4:76b3113c79ff 72 if (c == '-') {
kenjiArai 4:76b3113c79ff 73 s = 1;
kenjiArai 4:76b3113c79ff 74 c = *(++(*str));
kenjiArai 4:76b3113c79ff 75 }
kenjiArai 4:76b3113c79ff 76 if (c == '0') {
kenjiArai 4:76b3113c79ff 77 c = *(++(*str));
kenjiArai 4:76b3113c79ff 78 if (c <= ' ') { *res = 0; return 1; }
kenjiArai 4:76b3113c79ff 79 if (c == 'x') {
kenjiArai 4:76b3113c79ff 80 radix = 16;
kenjiArai 4:76b3113c79ff 81 c = *(++(*str));
kenjiArai 4:76b3113c79ff 82 } else {
kenjiArai 4:76b3113c79ff 83 if (c == 'b') {
kenjiArai 4:76b3113c79ff 84 radix = 2;
kenjiArai 4:76b3113c79ff 85 c = *(++(*str));
kenjiArai 4:76b3113c79ff 86 } else {
kenjiArai 4:76b3113c79ff 87 if ((c >= '0')&&(c <= '9')){ radix = 8;
kenjiArai 4:76b3113c79ff 88 } else { return 0;}
kenjiArai 4:76b3113c79ff 89 }
kenjiArai 4:76b3113c79ff 90 }
kenjiArai 4:76b3113c79ff 91 } else {
kenjiArai 4:76b3113c79ff 92 if ((c < '1')||(c > '9')){ return 0;}
kenjiArai 4:76b3113c79ff 93 radix = 10;
kenjiArai 4:76b3113c79ff 94 }
kenjiArai 4:76b3113c79ff 95 val = 0;
kenjiArai 4:76b3113c79ff 96 while (c > ' ') {
kenjiArai 4:76b3113c79ff 97 if (c >= 'a') c -= 0x20;
kenjiArai 4:76b3113c79ff 98 c -= '0';
kenjiArai 4:76b3113c79ff 99 if (c >= 17) {
kenjiArai 4:76b3113c79ff 100 c -= 7;
kenjiArai 4:76b3113c79ff 101 if (c <= 9) return 0;
kenjiArai 4:76b3113c79ff 102 }
kenjiArai 4:76b3113c79ff 103 if (c >= radix) return 0;
kenjiArai 4:76b3113c79ff 104 val = val * radix + c;
kenjiArai 4:76b3113c79ff 105 c = *(++(*str));
kenjiArai 4:76b3113c79ff 106 }
kenjiArai 4:76b3113c79ff 107 if (s) val = -val;
kenjiArai 4:76b3113c79ff 108 *res = val;
kenjiArai 4:76b3113c79ff 109 return 1;
kenjiArai 4:76b3113c79ff 110 }
kenjiArai 4:76b3113c79ff 111
kenjiArai 4:76b3113c79ff 112 //-------------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 113 // Monitor
kenjiArai 4:76b3113c79ff 114 //-------------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 115 // Help Massage
kenjiArai 4:76b3113c79ff 116 static void msg_hlp (void){
kenjiArai 4:76b3113c79ff 117 PRINTF(mon_msg); put_rn();
kenjiArai 4:76b3113c79ff 118 PRINTF("d - Show control data"); put_rn();
kenjiArai 4:76b3113c79ff 119 PRINTF("m - Show mail data"); put_rn();
kenjiArai 4:76b3113c79ff 120 PRINTF("s - Show USER Button"); put_rn();
kenjiArai 4:76b3113c79ff 121 PRINTF("t - Check and set RTC"); put_rn();
kenjiArai 8:4006b111c0d4 122 PRINTF("r - Show RTC date & time"); put_rn();
kenjiArai 4:76b3113c79ff 123 PRINTF("x - Goto HW monitor"); put_rn();
kenjiArai 4:76b3113c79ff 124 PRINTF("q - Return to main"); put_rn();
kenjiArai 4:76b3113c79ff 125 }
kenjiArai 4:76b3113c79ff 126
kenjiArai 4:76b3113c79ff 127 // Get key input data
kenjiArai 4:76b3113c79ff 128 static void get_line (char *buff, int len){
kenjiArai 4:76b3113c79ff 129 char c;
kenjiArai 4:76b3113c79ff 130 int idx = 0;
kenjiArai 4:76b3113c79ff 131
kenjiArai 4:76b3113c79ff 132 for (;;) {
kenjiArai 4:76b3113c79ff 133 c = GETC();
kenjiArai 4:76b3113c79ff 134 // Added by Kenji Arai / JH1PJL May 9th, 2010
kenjiArai 4:76b3113c79ff 135 if (c == '\r') {
kenjiArai 4:76b3113c79ff 136 buff[idx++] = c;
kenjiArai 4:76b3113c79ff 137 break;
kenjiArai 4:76b3113c79ff 138 }
kenjiArai 4:76b3113c79ff 139 if ((c == '\b') && idx) {
kenjiArai 4:76b3113c79ff 140 idx--;
kenjiArai 4:76b3113c79ff 141 PUTC(c);
kenjiArai 4:76b3113c79ff 142 PUTC(' ');
kenjiArai 4:76b3113c79ff 143 PUTC(c);
kenjiArai 4:76b3113c79ff 144 }
kenjiArai 4:76b3113c79ff 145 if (((uint8_t)c >= ' ') && (idx < len - 1)) {
kenjiArai 4:76b3113c79ff 146 buff[idx++] = c;
kenjiArai 4:76b3113c79ff 147 PUTC(c);
kenjiArai 4:76b3113c79ff 148 }
kenjiArai 4:76b3113c79ff 149 Thread::yield(); // change thread
kenjiArai 4:76b3113c79ff 150 Thread::wait(10); // Wait 10mS
kenjiArai 4:76b3113c79ff 151 }
kenjiArai 4:76b3113c79ff 152 buff[idx] = 0;
kenjiArai 4:76b3113c79ff 153 PUTC('\n');
kenjiArai 4:76b3113c79ff 154 }
kenjiArai 4:76b3113c79ff 155
kenjiArai 4:76b3113c79ff 156 // RTC related subroutines
kenjiArai 4:76b3113c79ff 157 static void chk_and_set_time(char *ptr){
kenjiArai 4:76b3113c79ff 158 //unsigned long p1;
kenjiArai 4:76b3113c79ff 159 int32_t p1;
kenjiArai 4:76b3113c79ff 160 struct tm t;
kenjiArai 4:76b3113c79ff 161 time_t seconds;
kenjiArai 4:76b3113c79ff 162 char buf[40];
kenjiArai 4:76b3113c79ff 163
kenjiArai 4:76b3113c79ff 164 if (xatoi(&ptr, &p1)){
kenjiArai 4:76b3113c79ff 165 t.tm_year = (uint8_t)p1 + 100;
kenjiArai 4:76b3113c79ff 166 PRINTF("Year:%d ",p1);
kenjiArai 4:76b3113c79ff 167 xatoi( &ptr, &p1 );
kenjiArai 4:76b3113c79ff 168 t.tm_mon = (uint8_t)p1 - 1;
kenjiArai 4:76b3113c79ff 169 PRINTF("Month:%d ",p1);
kenjiArai 4:76b3113c79ff 170 xatoi( &ptr, &p1 );
kenjiArai 4:76b3113c79ff 171 t.tm_mday = (uint8_t)p1;
kenjiArai 4:76b3113c79ff 172 PRINTF("Day:%d ",p1);
kenjiArai 4:76b3113c79ff 173 xatoi( &ptr, &p1 );
kenjiArai 4:76b3113c79ff 174 t.tm_hour = (uint8_t)p1;
kenjiArai 4:76b3113c79ff 175 PRINTF("Hour:%d ",p1);
kenjiArai 4:76b3113c79ff 176 xatoi( &ptr, &p1 );
kenjiArai 4:76b3113c79ff 177 t.tm_min = (uint8_t)p1;
kenjiArai 4:76b3113c79ff 178 PRINTF("Min:%d ",p1);
kenjiArai 4:76b3113c79ff 179 xatoi( &ptr, &p1 );
kenjiArai 4:76b3113c79ff 180 t.tm_sec = (uint8_t)p1;
kenjiArai 4:76b3113c79ff 181 PRINTF("Sec: %d \r\n",p1);
kenjiArai 4:76b3113c79ff 182 seconds = mktime(&t);
kenjiArai 4:76b3113c79ff 183 set_time(seconds);
kenjiArai 4:76b3113c79ff 184 }
kenjiArai 4:76b3113c79ff 185 seconds = time(NULL);
kenjiArai 4:76b3113c79ff 186 strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds));
kenjiArai 4:76b3113c79ff 187 PRINTF("Date: %s\r\n", buf);
kenjiArai 4:76b3113c79ff 188 }
kenjiArai 4:76b3113c79ff 189
kenjiArai 4:76b3113c79ff 190 // ---------- Program starts here! ---------------------------------------------------------------
kenjiArai 4:76b3113c79ff 191 int mon(void) {
kenjiArai 4:76b3113c79ff 192 char *ptr;
kenjiArai 4:76b3113c79ff 193
kenjiArai 4:76b3113c79ff 194 BAUD(9600);
kenjiArai 4:76b3113c79ff 195 put_rn();
kenjiArai 4:76b3113c79ff 196 put_rn();
kenjiArai 4:76b3113c79ff 197 PRINTF("%s [Help:'?' key]", mon_msg);
kenjiArai 4:76b3113c79ff 198 put_rn();
kenjiArai 4:76b3113c79ff 199 for (;;) {
kenjiArai 4:76b3113c79ff 200 put_r();
kenjiArai 4:76b3113c79ff 201 PUTC('>');
kenjiArai 4:76b3113c79ff 202 ptr = linebuf;
kenjiArai 4:76b3113c79ff 203 get_line(ptr, sizeof(linebuf));
kenjiArai 4:76b3113c79ff 204 switch (*ptr++) {
kenjiArai 4:76b3113c79ff 205 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 206 // Check control data
kenjiArai 4:76b3113c79ff 207 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 208 case 'd' :
kenjiArai 4:76b3113c79ff 209 while(true) {
kenjiArai 4:76b3113c79ff 210 put_r();
kenjiArai 5:dccdaaa1e57b 211 PRINTF("G:%+6.1f,%+6.1f,%+6.1f, ", fg[0], fg[1], fg[2]);
kenjiArai 4:76b3113c79ff 212 Thread::yield(); // change thread
kenjiArai 4:76b3113c79ff 213 PRINTF("A:%+6.1f,%+6.1f,%+6.1f ", fa[0], fa[1], fa[2]);
kenjiArai 4:76b3113c79ff 214 put_rn();
kenjiArai 4:76b3113c79ff 215 if ( READABLE() ) {
kenjiArai 4:76b3113c79ff 216 break;
kenjiArai 4:76b3113c79ff 217 }
kenjiArai 4:76b3113c79ff 218 Thread::wait(200); // Wait 200mS
kenjiArai 4:76b3113c79ff 219 }
kenjiArai 4:76b3113c79ff 220 break;
kenjiArai 4:76b3113c79ff 221 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 222 // Show Mail data
kenjiArai 4:76b3113c79ff 223 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 224 case 'm' :
kenjiArai 4:76b3113c79ff 225 put_rn();
kenjiArai 4:76b3113c79ff 226 while(true) {
kenjiArai 8:4006b111c0d4 227 show_flag0 = 1;
kenjiArai 4:76b3113c79ff 228 if ( READABLE() ) {
kenjiArai 4:76b3113c79ff 229 break;
kenjiArai 4:76b3113c79ff 230 }
kenjiArai 4:76b3113c79ff 231 Thread::wait(100); // Wait 100mS
kenjiArai 4:76b3113c79ff 232 }
kenjiArai 8:4006b111c0d4 233 show_flag0 = 0;
kenjiArai 4:76b3113c79ff 234 break;
kenjiArai 4:76b3113c79ff 235 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 236 // Show switch status
kenjiArai 4:76b3113c79ff 237 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 238 case 's' :
kenjiArai 4:76b3113c79ff 239 put_r();
kenjiArai 4:76b3113c79ff 240 PRINTF("Show USER_Button status");
kenjiArai 4:76b3113c79ff 241 put_rn();
kenjiArai 4:76b3113c79ff 242 PRINTF("every 500mS - hit any key for stop");
kenjiArai 4:76b3113c79ff 243 put_rn();
kenjiArai 4:76b3113c79ff 244 while (true){
kenjiArai 4:76b3113c79ff 245 PRINTF("SW0 = ");
kenjiArai 4:76b3113c79ff 246 if (read_sw(0) == 1) { PRINTF("ON ");
kenjiArai 4:76b3113c79ff 247 } else { PRINTF("OFF");
kenjiArai 4:76b3113c79ff 248 }
kenjiArai 4:76b3113c79ff 249 PRINTF(", SW1 = ");
kenjiArai 4:76b3113c79ff 250 if (read_sw(1) == 1) { PRINTF("ON");
kenjiArai 4:76b3113c79ff 251 } else { PRINTF("OFF");
kenjiArai 4:76b3113c79ff 252 }
kenjiArai 4:76b3113c79ff 253 put_rn();
kenjiArai 4:76b3113c79ff 254 if (READABLE()){ GETC(); break;}
kenjiArai 4:76b3113c79ff 255 wait(0.5);
kenjiArai 4:76b3113c79ff 256 }
kenjiArai 4:76b3113c79ff 257 break;
kenjiArai 4:76b3113c79ff 258 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 259 // check and set RTC
kenjiArai 4:76b3113c79ff 260 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 261 case 't' :
kenjiArai 4:76b3113c79ff 262 put_r();
kenjiArai 4:76b3113c79ff 263 chk_and_set_time(ptr);
kenjiArai 4:76b3113c79ff 264 break;
kenjiArai 4:76b3113c79ff 265 //---------------------------------------------------------------------------------------------
kenjiArai 8:4006b111c0d4 266 // check and set RTC
kenjiArai 8:4006b111c0d4 267 //---------------------------------------------------------------------------------------------
kenjiArai 8:4006b111c0d4 268 case 'r' :
kenjiArai 9:de986e74bd93 269 put_r();
kenjiArai 8:4006b111c0d4 270 while(true) {
kenjiArai 8:4006b111c0d4 271 show_flag1 = 1;
kenjiArai 8:4006b111c0d4 272 if ( READABLE() ) {
kenjiArai 8:4006b111c0d4 273 break;
kenjiArai 8:4006b111c0d4 274 }
kenjiArai 8:4006b111c0d4 275 Thread::wait(50); // Wait 100mS
kenjiArai 8:4006b111c0d4 276 }
kenjiArai 8:4006b111c0d4 277 show_flag1 = 0;
kenjiArai 8:4006b111c0d4 278 break;
kenjiArai 8:4006b111c0d4 279 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 280 // help
kenjiArai 4:76b3113c79ff 281 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 282 case '?' :
kenjiArai 4:76b3113c79ff 283 put_r();
kenjiArai 4:76b3113c79ff 284 msg_hlp();
kenjiArai 4:76b3113c79ff 285 break;
kenjiArai 4:76b3113c79ff 286 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 287 // Go back to main()
kenjiArai 4:76b3113c79ff 288 //---------------------------------------------------------------------------------------------
kenjiArai 4:76b3113c79ff 289 case 'q' : // Quit
kenjiArai 4:76b3113c79ff 290 PRINTF("\rReturn to main\r\n");
kenjiArai 4:76b3113c79ff 291 PRINTF("cannot control anymore from here\r\n");
kenjiArai 4:76b3113c79ff 292 return 0;
kenjiArai 4:76b3113c79ff 293 }
kenjiArai 4:76b3113c79ff 294 }
kenjiArai 4:76b3113c79ff 295 }