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: MODSERIAL USBDevice mbed-rtos mbed
Fork of mbed_sv_firmware_mt by
Diff: main.cpp
- Revision:
- 9:046247707ffb
- Parent:
- 8:3313aa7f9082
- Child:
- 10:55e35536d493
--- a/main.cpp Thu Jan 22 19:14:46 2015 +0000
+++ b/main.cpp Thu Jan 22 23:04:42 2015 +0000
@@ -1,7 +1,6 @@
#include "mbed.h"
#include "USBSerial.h"
#include "MODSERIAL.h"
-#include "InterruptIn.h"
// Constants
#define LEDON 0 // Low active for LEDs - turns LED on
@@ -58,9 +57,12 @@
int i, j; // index variables
int status = 0x00; // return value
-int prox_irq(void)
+uint8_t led_com_state = LEDOFF;
+uint8_t prox_irq_state = 0; // interrupt state passed from service routine
+
+void prox_irq(void)
{
- return 0;
+ prox_irq_state = 1;
}
int init_periph(void)
@@ -79,6 +81,9 @@
// Prox & EEPROM
i2c.frequency(I2CRATE); // I2C speed = 400Kbps
prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU
+ prox_int.fall(&prox_irq);
+ prox_int.enable_irq();
+ cdc.printf("Interrupt active");
// LEDs // Cycle through the LEDs.
led_err.write(LEDON);
@@ -176,37 +181,36 @@
return i2c_err; // 0 = ACK received, 1 = NAK/failure
}
+
// GPIO messages = 0xDD, r/w#, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
int gpio_rd()
{
- led_err.write(LEDON);
- led_com.write(LEDON);
-rfid_int.write(0);
-rfid_isp.write(0);
-cdc.putc( led_err.read());// && 0x01); // read all of the GPIO pins and store in a single byte
-cdc.putc( led_com.read());// << 1) && 0x02);
-cdc.putc( rfid_int.read());// << 2) && 0x04);
-cdc.putc( rfid_isp.read());// << 3) && 0x08);
-
+ gpio_buffer[2] = ( led_err.read() & 0x01); // read all of the GPIO pins and store in a single byte
+ gpio_buffer[2] |= ((led_com_state << 1) & 0x02); // use of led_com_state allows the led to be ON during this call, but send back the pre-call state.
+ gpio_buffer[2] |= ((rfid_int.read() << 2) & 0x04);
+ gpio_buffer[2] |= ((rfid_isp.read() << 3) & 0x08);
+ gpio_buffer[2] |= ((rfid_rst.read() << 4) & 0x10);
+ gpio_buffer[2] |= ((rfid_pwr.read() << 5) & 0x20);
return ERR_NONE;
}
int gpio_wr()
{
- cdc.putc(gpio_buffer[2]);
- cdc.putc(gpio_buffer[2] && 0x01);
- cdc.putc(gpio_buffer[2] && 0x02);
- cdc.putc(gpio_buffer[2] && 0x04);
- cdc.putc(gpio_buffer[2] && 0x08);
- cdc.putc(gpio_buffer[2] && 0x10);
- cdc.putc(gpio_buffer[2] && 0x20);
- led_err.write(gpio_buffer[2] && 0x01); // any bit set will write a 0
- led_com.write(gpio_buffer[2] && 0x02); // any bit set will write a 0
- rfid_int.write(gpio_buffer[2] && 0x04); // any bit set will write a 0
- rfid_isp.write(gpio_buffer[2] && 0x05); // any bit set will write a 0
- rfid_rst.write(gpio_buffer[2] && 0x10); // any bit set will write a 0
- rfid_pwr.write(gpio_buffer[2] && 0x20); // any bit set will write a 0
+ if ((gpio_buffer[2] & 0x02) == 0x00)
+ {
+ led_com_state = LEDON;
+ }
+ else
+ {
+ led_com_state = LEDOFF;
+ }
+ led_err.write(gpio_buffer[2] & 0x01);
+ led_com.write(led_com_state);
+ rfid_int.write(gpio_buffer[2] & 0x04);
+ rfid_isp.write(gpio_buffer[2] & 0x08);
+ rfid_rst.write(gpio_buffer[2] & 0x10);
+ rfid_pwr.write(gpio_buffer[2] & 0x20);
return ERR_NONE;
}
@@ -243,15 +247,24 @@
{
// initialize everything
- wait(2.0);
-
+ wait(5.0); // gives a chance to connect the COM port - this can be removed for production
+
init_periph();
-
while(1)
{
- led_com.write(LEDOFF); // turn off communication LED
- while(!cdc.readable()); // spin here until a message comes in from the host PC
+ led_com.write(led_com_state); // turn off communication LED unless it was specifically turned on by GPIO command
+ while(!cdc.readable())
+ {
+ if(prox_irq_state == 1)
+ {
+ prox_irq_state = 0;
+ cdc.putc(0xFF);
+ cdc.putc(0x7E);
+ cdc.putc(0x0F);
+ cdc.putc(0xF0);
+ }
+ } // spin here until a message comes in from the host PC
led_com.write(LEDON); // Message received - turn on LED
bool end_mark = FALSE;
uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start
