pub

Fork of CANnucleo by Zoltan Hudak

Revision:
25:353237492903
Parent:
23:ea766d08c9db
Child:
28:eed6929956ea
--- a/CANnucleo.cpp	Sat Aug 13 12:44:12 2016 +0000
+++ b/CANnucleo.cpp	Tue Aug 16 21:09:11 2016 +0000
@@ -54,7 +54,10 @@
  * @retval
  */
 int CAN::frequency(int f) {
-    return can_frequency(f);
+    lock();
+    int ret = can_frequency(f);
+    unlock();
+    return ret;
 }
 
 /**
@@ -64,7 +67,10 @@
  * @retval
  */
 int CAN::write(CANMessage msg) {
-    return can_write(msg, 0);
+    lock();
+    int ret = can_write(msg, 0);
+    unlock();
+    return ret;
 }
 
 /**
@@ -74,7 +80,10 @@
  * @retval
  */
 int CAN::read(CANMessage& msg, int handle) {
-    return can_read(&msg, handle);
+    lock();
+    int ret = can_read(&msg, handle);
+    unlock();
+    return ret;
 }
 
 /**
@@ -84,7 +93,9 @@
  * @retval
  */
 void CAN::reset(void) {
+    lock();
     can_reset();
+    unlock();
 }
 
 /**
@@ -94,7 +105,10 @@
  * @retval
  */
 unsigned char CAN::rderror(void) {
-    return can_rderror();
+    lock();
+    unsigned char ret = can_rderror();
+    unlock();
+    return ret;
 }
 
 /**
@@ -104,7 +118,10 @@
  * @retval
  */
 unsigned char CAN::tderror(void) {
-    return can_tderror();
+    lock();
+    unsigned char ret = can_tderror();
+    unlock();
+    return ret;
 }
 
 /**
@@ -114,7 +131,9 @@
  * @retval
  */
 void CAN::monitor(bool silent) {
+    lock();
     can_monitor((silent) ? 1 : 0);
+    unlock();
 }
 
 /**
@@ -124,7 +143,10 @@
  * @retval
  */
 int CAN::mode(Mode mode) {
-    return can_mode((CanMode) mode);
+    lock();
+    int ret = can_mode((CanMode) mode);
+    unlock();
+    return ret;
 }
 
 /**
@@ -221,7 +243,10 @@
  *          3 - time out  
  */
 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format /* = CANAny */, int handle /* = 0 */) {
-    return can_filter(id, mask, format, handle);
+    lock();
+    int ret = can_filter(id, mask, format, handle);
+    unlock();
+    return ret;
 }
 
 /**
@@ -231,11 +256,13 @@
  * @param   type: not used (only CAN1 RX0 Interrupt supported) 
  * @retval
  */
-void CAN::attach(void (*fptr) (void), IrqType type) {
+void CAN::attach(mbed::Callback<void()> func, IrqType type) {
+    lock();
     HAL_NVIC_DisableIRQ(CAN_IRQ);
-    if(fptr)
-        _irq[(CanIrqType)type].attach(fptr);
+    if (func)
+        _irq[(CanIrqType)type].attach(func);
     HAL_NVIC_EnableIRQ(CAN_IRQ);
+    unlock();
 }
 
 /**
@@ -249,6 +276,14 @@
     handler->_irq[type].call();
 }
 
+void CAN::lock() {
+    _mutex.lock();
+}
+
+void CAN::unlock() {
+    _mutex.unlock();
+}
+
 }   // namespace CANnucleo
 
 
@@ -258,3 +293,5 @@
 
 
 
+
+