use mbed-src latest version and everything works well. RTC is also fine.

Dependencies:   L3GD20 LIS3DH TextLCD mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * mbed Application program for the mbed
00003  *      Test program for GR-PEACH
00004  *
00005  * Copyright (c) 2014,'15 Kenji Arai / JH1PJL
00006  *  http://www.page.sannet.ne.jp/kenjia/index.html
00007  *  http://mbed.org/users/kenjiArai/
00008  *      Created: November  29th, 2014
00009  *      Revised: Feburary   8th, 2015
00010  *
00011  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00012  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00013  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00014  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00015  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016  */
00017 
00018 //  Include ---------------------------------------------------------------------------------------
00019 #include    "mbed.h"
00020 #include    "rtos.h"
00021 #include    "L3GD20.h"
00022 #include    "LIS3DH.h"
00023 #include    "TextLCD.h"
00024 
00025 //  Definition ------------------------------------------------------------------------------------
00026 #define USE_COM         // use Communication with PC(UART)
00027 #define USE_I2C_LCD
00028 #define USE_I2C_SENSOR
00029  
00030 // Com
00031 #ifdef  USE_COM
00032 #define BAUD(x)             pcx.baud(x)
00033 #define GETC(x)             pcx.getc(x)
00034 #define PUTC(x)             pcx.putc(x)
00035 #define PRINTF(...)         pcx.printf(__VA_ARGS__)
00036 #define READABLE(x)         pcx.readable(x)
00037 #else
00038 #define BAUD(x)             {;}
00039 #define GETC(x)             {;}
00040 #define PUTC(x)             {;}
00041 #define PRINTF(...)         {;}
00042 #define READABLE(x)         {;}
00043 #endif
00044 
00045 #define TIMEBASE        12000
00046 #define FIXED_STEPS     100
00047 
00048 #define PI              3.1415926536
00049 #define RAD_TO_DEG      57.29578
00050 #define TIME_BASE_S     0.02
00051 #define TIME_BASE_MS    ( TIME_BASE_S * 1000)
00052 #define RATE            0.1
00053 
00054 //  Object ----------------------------------------------------------------------------------------
00055 // LED's
00056 DigitalOut LEDs[4] = {
00057     DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
00058 };
00059 // Swiches
00060 DigitalIn   USER_SWITCH[2] = {
00061 #if defined(TARGET_RZ_A1H)
00062     DigitalIn(P6_0),  DigitalIn(P6_1)
00063 #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
00064     DigitalIn(PC_13),  DigitalIn(A0)
00065 #endif
00066 };
00067 
00068 // OS check
00069 #if defined(TARGET_RZ_A1H)
00070 DigitalOut task0(P1_8);
00071 DigitalOut task1(P1_9);
00072 #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
00073 DigitalOut task0(A0);
00074 DigitalOut task1(A1);
00075 #endif
00076 
00077 // com
00078 #ifdef USE_COM
00079 Serial      pcx(USBTX, USBRX);      // Communication with Host
00080 #endif
00081 I2C i2c(D14,D15);
00082 // Gyro
00083 L3GX_GYRO   gyro(i2c, L3GD20_V_CHIP_ADDR, L3GX_DR_95HZ, L3GX_BW_HI, L3GX_FS_250DPS);
00084 // Acc
00085 LIS3DH      acc(i2c, LIS3DH_G_CHIP_ADDR, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_8G);
00086 #ifdef USE_I2C_LCD
00087 // LCD
00088 TextLCD_I2C_N lcd0(&i2c, 0x7c, TextLCD::LCD16x2);  // LCD(Akizuki AQM0802A)
00089 #endif
00090 // Mutex
00091 Mutex       i2c_mutex; 
00092 
00093 //  RAM -------------------------------------------------------------------------------------------
00094 Queue<uint32_t, 2> queue0;
00095 float fa[3];    // Acc  0:X, 1:Y, 2:Z
00096 float fg[3];    // Gyro 0:X, 1:Y, 2:Z
00097 
00098 /* Mail */
00099 typedef struct {
00100   float    voltage; /* AD result of measured voltage */
00101   float    current; /* AD result of measured current */
00102   uint32_t counter; /* A counter value               */
00103 } mail_t;
00104  
00105 Mail<mail_t, 16> mail_box;
00106 
00107 uint8_t show_flag0;
00108 uint8_t show_flag1;
00109 uint32_t count;
00110 
00111 #if defined(TARGET_RZ_A1H)
00112 uint8_t big_data[4096*1024];
00113 uint32_t big_data_index = 0;
00114 #endif
00115 
00116 //  ROM / Constant data ---------------------------------------------------------------------------
00117 
00118 //  Function prototypes ---------------------------------------------------------------------------
00119 
00120 //  Function prototypes ---------------------------------------------------------------------------
00121 extern int mon( void);
00122 
00123 //-------------------------------------------------------------------------------------------------
00124 //  Control Program
00125 //-------------------------------------------------------------------------------------------------
00126 void send_thread (void const *args) {
00127     uint32_t i = 0;
00128     while (true) {
00129         i++; // fake data update
00130         mail_t *mail = mail_box.alloc();
00131         mail->voltage = (i * 0.1) * 33; 
00132         mail->current = (i * 0.1) * 11;
00133         mail->counter = i;
00134         mail_box.put(mail);
00135         Thread::wait(1000);
00136     }
00137 }
00138 
00139 void blink(void const *n) {
00140     LEDs[(int)n] = !LEDs[(int)n];
00141 }
00142 
00143 // Read switch status
00144 int read_sw(uint8_t n){
00145     if (USER_SWITCH[n] == 0){   return 1;
00146     } else {                    return 0;}
00147 }
00148 
00149 // Monitor program
00150 void monitor(void const *args){
00151     Thread::wait(1000);
00152     while (true){
00153         mon();
00154     }
00155 }
00156 
00157 void watch_time (void const *args) {
00158     uint32_t i = 0;
00159     time_t seconds;
00160     char buf[64];
00161 #if 0
00162     struct tm t;
00163 
00164     t.tm_year       = 15 + 100;
00165     t.tm_mon        = 2 - 1;
00166     t.tm_mday       = 7;
00167     t.tm_hour       = 22;
00168     t.tm_min        = 21;
00169     t.tm_sec        = 20;
00170     seconds = mktime(&t);
00171     set_time(seconds);
00172 #endif
00173     while (true) {
00174         seconds = time(NULL);
00175         strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds));
00176         if (show_flag1){
00177             printf("[TASK1] %s, No:%5d, Ticker:%10u\r\n",buf, i++,  us_ticker_read());
00178         }
00179         Thread::wait(1000);
00180     }
00181 }
00182 
00183 // Interrupt routine
00184 void queue_isr0() {
00185     queue0.put((uint32_t*)1);
00186 }
00187 
00188 // Update sensor data
00189 void update_angle(void const *args){
00190     while (true) {
00191         osEvent evt = queue0.get();
00192         // ---->lock
00193         i2c_mutex.lock();
00194 #ifdef USE_I2C_SENSOR
00195         // read acceleration data from sensor
00196         acc.read_data(fa);
00197         // read gyroscope data from sensor
00198         gyro.read_data(fg);
00199 #else
00200         fa[0] = fa[1] = fa[2] = 1.11f;
00201         fg[0] = fg[1] = fg[2] = 1.11f;
00202 #endif
00203         // <----unlock
00204         i2c_mutex.unlock();
00205         // debug
00206         task0 = !task0;
00207     }
00208 }
00209 
00210 // Update sensor data
00211 void display(void const *args){
00212     uint32_t n = 0;
00213 
00214     while (true) {
00215 #ifdef USE_I2C_LCD
00216         i2c_mutex.lock();
00217         lcd0.locate(0, 0);    // 1st line top
00218         lcd0.printf(" G=%4.1f ", sqrt(fa[0]*fa[0] + fa[1]*fa[1] + fa[2]*fa[2]));
00219         lcd0.locate(0, 1);    // 2nd line top
00220         lcd0.printf("%8d",n++);
00221         i2c_mutex.unlock();
00222 #endif
00223         Thread::wait(500);
00224         // debug
00225         task1 = !task1;
00226     }
00227 }
00228 
00229 // Thread definition
00230 osThreadDef(update_angle, osPriorityNormal, 1024);
00231 osThreadDef(monitor, osPriorityNormal, 1024);
00232 osThreadDef(display, osPriorityAboveNormal, 1024);
00233 osThreadDef(watch_time, osPriorityNormal, 1024);
00234 
00235 int main(void) {
00236     PRINTF("\r\nstep1\r\n");
00237     
00238     RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
00239     RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
00240     RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
00241     RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
00242 
00243     PRINTF("step2\r\n");    
00244     led_1_timer.start(2000);
00245     led_2_timer.start(1000);
00246     led_3_timer.start(500);
00247     led_4_timer.start(250);
00248 
00249     PRINTF("step3\r\n");    
00250     Thread thread(send_thread);
00251 
00252     PRINTF("step4\r\n");
00253 
00254     // IRQ
00255     Ticker ticker0;
00256     Ticker ticker1;
00257     ticker0.attach(queue_isr0, TIME_BASE_S);
00258 
00259  
00260     PRINTF("step5\r\n");
00261     // Starts threads
00262     if (osThreadCreate(osThread(display), NULL) == NULL){
00263         PRINTF("ERROR4\r\n");
00264     }
00265     if (osThreadCreate(osThread(monitor), NULL) == NULL){
00266         PRINTF("ERROR3\r\n");
00267     }
00268     if (osThreadCreate(osThread(update_angle), NULL) == NULL){
00269         PRINTF("ERROR1\r\n");
00270     }
00271     if (osThreadCreate(osThread(watch_time), NULL) == NULL){
00272         printf("ERROR5\r\n");
00273     }
00274     // I2C LCD
00275 #ifdef USE_I2C_LCD
00276     // ---->lock
00277     i2c_mutex.lock();
00278     lcd0.locate(0, 0);    // 1st line top
00279     lcd0.printf("I2C test");
00280     lcd0.locate(0, 1);    // 2nd line top
00281     lcd0.puts(" JH1PJL ");
00282     lcd0.setContrast(0x14);
00283     // <----unlock
00284     i2c_mutex.unlock();
00285 #endif
00286     count = 0;
00287     PRINTF("step6\r\n");
00288     while (true) {
00289         osEvent evt = mail_box.get();
00290         if (evt.status == osEventMail) {
00291             mail_t *mail = (mail_t*)evt.value.p;
00292             if (show_flag0){
00293                 PRINTF("[MAIN]\r\n");
00294                 PRINTF("This is dummy!, ");
00295                 PRINTF("Volt: %.2f V, "   , mail->voltage);
00296                 PRINTF("Current: %.2f A, "     , mail->current);
00297                 PRINTF("# of cycles: %u\r\n", mail->counter);
00298             }
00299             mail_box.free(mail);
00300         }
00301 #if defined(TARGET_RZ_A1H)
00302         for (uint32_t i = 0; i < 100; i++){
00303             big_data[big_data_index] = ++big_data_index;
00304             if (big_data_index == (4096 * 1024)){
00305                 big_data_index = 0;
00306             }
00307         }
00308 #endif
00309     }
00310 }
00311 
00312 void mbed_die(void) {
00313     PRINTF("Error, came from os_error()!\r\n");
00314     gpio_t led_1; gpio_init_out(&led_1, LED1);
00315     gpio_t led_2; gpio_init_out(&led_2, LED2);
00316     gpio_t led_3; gpio_init_out(&led_3, LED3);
00317     gpio_t led_4; gpio_init_out(&led_4, LED4);
00318 
00319     while (1) {
00320         gpio_write(&led_1, 1);
00321         gpio_write(&led_2, 0);
00322         gpio_write(&led_3, 0);
00323         gpio_write(&led_4, 1);
00324         wait_ms(100);
00325         gpio_write(&led_1, 0);
00326         gpio_write(&led_2, 1);
00327         gpio_write(&led_3, 1);
00328         gpio_write(&led_4, 0);
00329         wait_ms(100);
00330     }
00331 }