Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ConfigFile DS1307 OneWire SDFileSystem USBDeviceLite mbed-rtos mbed
Revision 3:ede67e9b60eb, committed 2014-05-06
- Comitter:
- alpov
- Date:
- Tue May 06 19:21:27 2014 +0000
- Parent:
- 2:0e0131d51df5
- Child:
- 4:9065e9ccb078
- Commit message:
- working with RTOS, changed USBDevice library to lite version without malloc()
Changed in this revision
--- a/USBDevice.lib Mon Apr 28 13:10:52 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/alpov/code/USBDevice/#c7639faed2eb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDeviceLite.lib Tue May 06 19:21:27 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/alpov/code/USBDeviceLite/#96a0946caef5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/comm.h Tue May 06 19:21:27 2014 +0000 @@ -0,0 +1,6 @@ +#ifndef _COMM_H +#define _COMM_H + +extern void comm_thread(void const *args); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/rtc.h Tue May 06 19:21:27 2014 +0000 @@ -0,0 +1,7 @@ +#ifndef _RTC_H +#define _RTC_H + +extern time_t get_rtc(); +extern bool set_rtc(time_t time); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/sensor.h Tue May 06 19:21:27 2014 +0000 @@ -0,0 +1,12 @@ +#ifndef _SENSOR_H +#define _SENSOR_H + +extern uint8_t sensor_roms[8][16]; +extern int16_t sensor_temps[16]; +extern int sensor_count; + +extern Mutex sensor_mutex; + +extern void sensor_thread(void const *args); + +#endif
--- a/main.cpp Mon Apr 28 13:10:52 2014 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,149 +0,0 @@
-#include "mbed.h"
-#include "SDFileSystem.h"
-#include "USBSerial.h"
-#include "DS1307.h"
-#include "1wire.h"
-
-Serial pc(MBED_UARTUSB);
-
-SDFileSystem sd(MBED_SPI0, "sd");
-
-USBSerial usb(0x1f00, 0x2012, 0x0001, false);
-
-DS1307 rtc(I2C_SDA, I2C_SCL);
-
-OneWire ow(A4, A2, A3);
-
-DigitalOut ledR(LED_RED, 1);
-DigitalOut ledG(LED_GREEN, 1);
-//DigitalOut ledB(LED_BLUE, 1);
-
-DigitalOut OUT1(D2, 0);
-DigitalOut OUT2(D3, 0);
-DigitalOut OUT3(D4, 0);
-DigitalOut OUT4(D5, 0);
-
-DigitalIn IN1(D8, PullUp);
-DigitalIn IN2(D9, PullUp);
-
-
-void blik_usb()
-{
- if (usb.configured())
- ledG = !ledG;
- else
- ledG = 1;
-}
-
-
-int main(void)
-{
- Timer t;
- Ticker usbstat;
- char c;
-
- usbstat.attach(&blik_usb, 0.2);
-
- pc.printf("Hello World!\n");
-
- FILE *fp = fopen("/sd/sdtest2.txt", "w");
- if (fp == NULL) {
- pc.printf("Could not open file for write\n");
- } else {
- pc.printf("Writing... ");
- fprintf(fp, "Hello fun SD Card World!\nOK!\n");
- pc.printf("closing... ");
- fclose(fp);
- }
- pc.printf("done\n");
-
- OUT1 = 1;
- OUT2 = 1;
- OUT3 = 1;
- OUT4 = 1;
- wait(1.0);
- OUT1 = 0;
- OUT2 = 0;
- OUT3 = 0;
- OUT4 = 0;
-
- pc.printf("Entering loop\n");
-
- while (1)
- {
- if (!usb.readable()) continue;
- c = usb.getc();
- //pc.printf("USB command received\n");
-
- if (c == 'r') {
- // perform read
- t.reset();
- t.start();
- time_t m_time = rtc.now();
- t.stop();
-
- struct tm *now;
- now = localtime(&m_time);
-
- usb.printf("Current time is %lu, %02d:%02d:%02d, %d.%d.%04d\n",
- m_time,
- now->tm_hour, now->tm_min, now->tm_sec,
- now->tm_mday, now->tm_mon+1, now->tm_year+1900
- );
- usb.printf("Internal datetime format is %s\n", asctime(now));
- usb.printf("Read complete, elapsed %uus\n", t.read_us());
-
- }
- else if (c == 'w') {
- // perform write
- int date, month, year, hours, minutes, seconds;
- usb.printf("Enter the date (date 1..31)\n"); usb.scanf("%d", &date);
- usb.printf("Enter the date (month 1..12)\n"); usb.scanf("%d", &month);
- usb.printf("Enter the date (year >2000)\n"); usb.scanf("%d", &year);
- usb.printf("Enter the time (hours 0..23)\n"); usb.scanf("%d", &hours);
- usb.printf("Enter the time (minutes 0..59)\n"); usb.scanf("%d", &minutes);
- usb.printf("Enter the time (seconds 0..59)\n"); usb.scanf("%d", &seconds);
-
- struct tm now = {seconds, minutes, hours, date, month-1, year-1900};
- time_t m_time = mktime(&now);
-
- t.reset();
- t.start();
- bool b = rtc.set_time(m_time);
- t.stop();
-
- usb.printf("Write complete (UNIX %lu, result %d), elapsed %uus\n", m_time, b, t.read_us());
- }
- else if (c == 'c') {
- ow.ConvertAll(true);
- usb.printf("Convert done\n");
- }
- else if (c == 't') {
- int result, temp;
- uint8_t rom[8];
-
- memset(rom, 0, sizeof(rom));
- result = ow.First(rom);
- while (result == OW_FOUND) {
- for (int i = 0; i < 8; i++) usb.printf("%.2X", rom[i]);
-
- t.reset();
- t.start();
- int b = ow.ReadTemperature(rom, &temp);
- t.stop();
- if (b) usb.printf(": read failed, code 0x%.4x, elapsed %uus\n", b, t.read_us());
- else usb.printf(": read ok, temperature %.2f'C, elapsed %uus\n", temp / 100., t.read_us());
-
- result = ow.Next(rom);
- }
- usb.printf("Done\n");
- }
- else {
- usb.printf("Syntax error\n");
- }
-
- usb.printf("\n");
- }
-
-}
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue May 06 19:21:27 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/comm.cpp Tue May 06 19:21:27 2014 +0000
@@ -0,0 +1,83 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "USBSerial.h"
+#include "rtc.h"
+#include "sensor.h"
+#include "comm.h"
+
+void comm_thread(void const *args)
+{
+ USBSerial usb(0x1f00, 0x2012, 0x0001, false);
+ Timer t;
+ char c;
+
+ while (1)
+ {
+ c = usb.getc();
+
+ if (c == 'r') {
+ // perform read
+ t.reset();
+ t.start();
+ time_t m_time = get_rtc();
+ t.stop();
+
+ struct tm *now;
+ now = localtime(&m_time);
+
+ usb.printf("Current time is %lu, %02d:%02d:%02d, %d.%d.%04d\n",
+ m_time,
+ now->tm_hour, now->tm_min, now->tm_sec,
+ now->tm_mday, now->tm_mon+1, now->tm_year+1900
+ );
+ usb.printf("Internal datetime format is %s\n", asctime(now));
+ usb.printf("Read complete, elapsed %uus\n", t.read_us());
+
+ }
+ else if (c == 'i') {
+ // perform read
+ time_t m_time = time(NULL);
+
+ struct tm *now;
+ now = localtime(&m_time);
+
+ usb.printf("Internal datetime format is %s\n", asctime(now));
+ }
+ else if (c == 'w') {
+ // perform write
+ int date, month, year, hours, minutes, seconds;
+ usb.printf("Enter the date (date 1..31)\n"); usb.scanf("%d", &date);
+ usb.printf("Enter the date (month 1..12)\n"); usb.scanf("%d", &month);
+ usb.printf("Enter the date (year >2000)\n"); usb.scanf("%d", &year);
+ usb.printf("Enter the time (hours 0..23)\n"); usb.scanf("%d", &hours);
+ usb.printf("Enter the time (minutes 0..59)\n"); usb.scanf("%d", &minutes);
+ usb.printf("Enter the time (seconds 0..59)\n"); usb.scanf("%d", &seconds);
+
+ struct tm now = {seconds, minutes, hours, date, month-1, year-1900};
+ time_t m_time = mktime(&now);
+
+ t.reset();
+ t.start();
+ bool b = set_rtc(m_time);
+ t.stop();
+
+ usb.printf("Write complete (UNIX %lu, result %d), elapsed %uus\n", m_time, b, t.read_us());
+ }
+ else if (c == 't') {
+ sensor_mutex.lock();
+ for (int j = 0; j < sensor_count; j++) {
+ for (int i = 0; i < 8; i++) usb.printf("%.2X", sensor_roms[j][i]);
+ usb.printf(": temperature %.2f'C\n", sensor_temps[j] / 100.);
+ }
+ sensor_mutex.unlock();
+ usb.printf("Done\n");
+ }
+ else {
+ usb.printf("Syntax error, use {r|i|w|t}\n");
+ }
+
+ usb.printf("\n");
+ }
+
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/disk.txt Tue May 06 19:21:27 2014 +0000 @@ -0,0 +1,12 @@ + + +void read_config() + +void write_config() + +void log_temperature() + +void log_io() + +void log_event() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main.cpp Tue May 06 19:21:27 2014 +0000
@@ -0,0 +1,63 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "SDFileSystem.h"
+#include "sensor.h"
+#include "comm.h"
+
+Serial pc(MBED_UARTUSB);
+
+SDFileSystem sd(MBED_SPI0, "sd");
+
+DigitalOut ledR(LED_RED, 1);
+DigitalOut ledG(LED_GREEN, 1);
+//DigitalOut ledB(LED_BLUE, 1);
+
+DigitalOut OUT1(D2, 0);
+DigitalOut OUT2(D3, 0);
+DigitalOut OUT3(D4, 0);
+DigitalOut OUT4(D5, 0);
+
+DigitalIn IN1(D8, PullUp);
+DigitalIn IN2(D9, PullUp);
+
+extern "C" void HardFault_Handler()
+{
+ mbed_die();
+}
+
+int main(void)
+{
+ pc.printf("Starting threads... ");
+ Thread sensor(sensor_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
+ Thread comm(comm_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
+ pc.printf("done\n");
+
+ FILE *fp = fopen("/sd/sdtest2.txt", "w");
+ if (fp == NULL) {
+ pc.printf("Could not open file for write\n");
+ } else {
+ pc.printf("Writing... ");
+ fprintf(fp, "Hello fun SD Card World!\nOK!\n");
+ pc.printf("closing... ");
+ fclose(fp);
+ }
+ pc.printf("done\n");
+
+ OUT1 = 1;
+ OUT2 = 1;
+ OUT3 = 1;
+ OUT4 = 1;
+ wait(1.0);
+ OUT1 = 0;
+ OUT2 = 0;
+ OUT3 = 0;
+ OUT4 = 0;
+
+ while (1)
+ {
+ ledG = !ledG;
+ Thread::wait(200);
+ }
+
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/rtc.cpp Tue May 06 19:21:27 2014 +0000
@@ -0,0 +1,25 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "DS1307.h"
+#include "rtc.h"
+
+DS1307 rtc(I2C_SDA, I2C_SCL);
+Mutex rtc_mutex;
+
+time_t get_rtc()
+{
+ rtc_mutex.lock();
+ time_t m_time = rtc.now();
+ rtc_mutex.unlock();
+ set_time(m_time);
+ return m_time;
+}
+
+bool set_rtc(time_t time)
+{
+ rtc_mutex.lock();
+ bool b = rtc.set_time(time);
+ rtc_mutex.unlock();
+ set_time(time);
+ return b;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sensor.cpp Tue May 06 19:21:27 2014 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "1wire.h"
+#include "sensor.h"
+
+uint8_t sensor_roms[8][16];
+int16_t sensor_temps[16];
+int sensor_count;
+
+Mutex sensor_mutex;
+
+void sensor_thread(void const *args)
+{
+ OneWire ow(A4, A2, A3);
+ int result, temp;
+ uint8_t rom[8];
+
+ while (1) {
+ ow.ConvertAll(false);
+ Thread::wait(CONVERT_T_DELAY);
+
+ sensor_mutex.lock();
+ sensor_count = 0;
+ memset(rom, 0, sizeof(rom));
+ result = ow.First(rom);
+ while (result == OW_FOUND) {
+ if (ow.ReadTemperature(rom, &temp) == 0) {
+ memcpy(sensor_roms[sensor_count], rom, sizeof(rom));
+ sensor_temps[sensor_count] = temp;
+ sensor_count++;
+ }
+ result = ow.Next(rom);
+ }
+ sensor_mutex.unlock();
+ }
+}
+