]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/commitdiff
Make the RPP layer thread safe
authorMichal Horn <hornmich@fel.cvut.cz>
Tue, 11 Aug 2015 13:28:31 +0000 (15:28 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 28 Aug 2015 14:17:01 +0000 (16:17 +0200)
29 files changed:
rpp/include/drv/spi.h
rpp/include/rpp/.gitattributes
rpp/include/rpp/adc.h
rpp/include/rpp/can.h
rpp/include/rpp/dac.h
rpp/include/rpp/din.h
rpp/include/rpp/fr.h
rpp/include/rpp/gio.h
rpp/include/rpp/hbr.h
rpp/include/rpp/irc.h
rpp/include/rpp/lout.h
rpp/include/rpp/mout.h
rpp/include/rpp/mutex.h [new file with mode: 0644]
rpp/include/rpp/rpp.h
rpp/include/rpp/sci.h
rpp/include/rpp/sdr.h
rpp/src/drv/adc.c
rpp/src/drv/spi.c
rpp/src/rpp/adc.c
rpp/src/rpp/can.c
rpp/src/rpp/dac.c
rpp/src/rpp/din.c
rpp/src/rpp/fr.c
rpp/src/rpp/gio.c
rpp/src/rpp/hbr.c
rpp/src/rpp/irc.c
rpp/src/rpp/lout.c
rpp/src/rpp/mout.c
rpp/src/rpp/sci.c

index 5ab98540098f7c847476ca18c181647153eca209..ba4fb43a22bcc5781fa3351e8d59ad2d3135b011 100644 (file)
 
 UL_LIST_CUST_DEC(spi_rq_queue, spi_drv_t, spi_msg_head_t, rq_queue, node)
 
-/* ------------------------------------------------------------------------- */
-
-//typedef unsigned long spi_isr_lock_level_t;
-//#define spi_isr_lock    save_and_cli
-//#define  spi_isr_unlock restore_flags
-
-/* ------------------------------------------------------------------------- */
 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf);
-//spi_drv_t *spi_find_drv(char *name, int number);
 int spi_msg_rq_ins(spi_drv_t *ifc, spi_msg_head_t *msg);
 int8_t port_spi_set(const struct port_desc *port, void *values, size_t size);
 
index 0ceb40b57b0ec5ddc60619ad6a930bcb93fae975..44db08bfeda30f93015b541d213b7442f1f91282 100644 (file)
@@ -1,5 +1,6 @@
 /adc.h eaton
 /can.h eaton
 /gio.h eaton
+/mutex.h       eaton
 /rpp.h eaton
 /sci.h eaton
index 02e9c9c52bce1f14ccfed1f639ee68eb6eaa2fe6..0894aff849f39d90ea9678f59cc5fbb9a5b889ca 100644 (file)
@@ -17,6 +17,8 @@
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -27,6 +29,8 @@ int8_t rpp_adc_init();
 /**
  * Get the current analog value on the given pin.
  *
+ * The function is thread thread safe.
+ *
  * @param[in] pin       The pin number to read [1-16].
  *
  * @return [0-4095] Value representing the analog value on given pin
@@ -37,6 +41,8 @@ int8_t rpp_adc_init();
 /**
  * Get the current analog value on the given pin.
  *
+ * The function is thread safe.
+ *
  * @param[in] pin       The pin number to read [1-12].
  *
  * @return [0-4095] Value representing the analog value on given pin
@@ -50,6 +56,8 @@ int16_t rpp_adc_get(uint8_t pin);
 /**
  * Read and update analog cached values.
  *
+ * The function is thread safe.
+ *
  * @return SUCCESS when transaction was successful.\n
  *         FAILURE if transaction could not be confirmed.
  */
index e9b72f7b98ccb318f19f1b5145de6b45170cd7ff..a125800a3ae7c289599013871547f9c89c957233 100644 (file)
@@ -169,6 +169,8 @@ struct rpp_can_config {
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @param[in] config Configuration of CAN controllers and message objects
  *
  * @return @c SUCCESS if initialization was successful,\n
@@ -179,6 +181,8 @@ int8_t rpp_can_init(const struct rpp_can_config *config);
 /**
  * Submit a CAN message for transmission.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * Data pointed by @a pdu is copied to the given message object for
  * transmission. A successful call to this function also resets the TX
  * pending flag that can be retrieved with rpp_can_check_tx_pend().
@@ -203,6 +207,8 @@ int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu);
 /**
  * Checks whether message transmission is pending.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * Call this function to check whether a message previously submitted
  * for transmission with rpp_can_write() still waits for transmission
  * or has already been transmitted to the bus.
@@ -218,6 +224,8 @@ int8_t rpp_can_check_tx_pend(rpp_can_hw_obj hw_obj, bool *tx_pend);
 /**
  * Read a message from a message object.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * Data in the given message object is copied to @a pdu. A successful
  * call to this function also clears the indication that can be
  * retrieved with rpp_can_check_rx_ind().
@@ -236,6 +244,8 @@ int8_t rpp_can_read(rpp_can_hw_obj hw_obj, struct rpp_can_pdu *pdu);
 /**
  * Checks whether a message was received by the message object.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] hw_obj Hardware object to be checked.
  * @param[out] rx_ind Where to store the RX indication flag. Its value
  * will be @c true if a message was received, @c false otherwise.
index a8dd064736733b74e4a93f80dcb9626a066404aa..adcffb85c7bac5a3b918ee7407f482cf982817bc 100644 (file)
@@ -26,6 +26,8 @@
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -37,6 +39,8 @@ int8_t rpp_dac_init();
  *
  * Call rpp_dac_update() to commit setup changes to real hardware.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] pin       The pin number to setup [1-4].
  * @param[in] enabled   TRUE to enable pin or FALSE to disable it.
  *
@@ -51,6 +55,8 @@ int8_t rpp_dac_setup(uint8_t pin, boolean_t enabled);
  *
  * Call rpp_dac_update() to flush cached values to real hardware.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] pin       The pin number to set [1-4].
  * @param[in] val       The value to be set [0-4095] (DAC is 12bit resolution).
  *
@@ -71,6 +77,8 @@ int8_t rpp_dac_set(uint8_t pin, uint16_t val);
  *
  * Helper function that calculates DAC value given a voltage in millivolts.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] pin       The pin number to set [1-4].
  * @param[in] mv        Voltage level in mV to be set on specified pin [0-12000].
  *
@@ -84,6 +92,8 @@ int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv);
 /**
  * Flush cached output values and configuration changes.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @bug This function should be called only after the FreeRTOS Scheduler has
  * started (which implies from a FreeRTOS Task). If called before starting the
  * scheduler, like for library initialization, or application DAC
index 5e36bfc4e2792187bbcdecd0eb6bae84444e6bac..7c6a81bcb5c51992a10c8f09d9e9b8ad39bfec19 100644 (file)
@@ -17,6 +17,8 @@
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -27,6 +29,8 @@ int8_t rpp_din_init();
  * Configure voltage reference levels for digital inputs using variable
  * reference threshold.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] refA      [0-4095] value to set (DAC is 12bits) the reference
  *                      voltage A (pins 12-15).
  * @param[in] refB      [0-4095] value to set (DAC is 12bits) the reference
@@ -43,6 +47,8 @@ int8_t rpp_din_ref(uint16_t refA, uint16_t refB);
  *
  * Call rpp_din_update() to commit configuration changes to the hardware.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] pin       The pin number to setup [0-15].
  * @param[in] pull_up   TRUE to setup pin as pull-up (a switch-to-ground device
  *                      is connected) or FALSE to setup as pull-down
@@ -67,6 +73,8 @@ int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
  *
  * Call rpp_din_update() to update cached values.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] pin       The pin number to read [0-15].
  *
  * @return RPP_CLOSED or RPP_OPEN if successful.\n
@@ -83,6 +91,8 @@ int8_t rpp_din_get(uint8_t pin);
  * Inputs [8-11] use programmable threshold B and [12-15] use
  * programmable threshold A.\n
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @see rpp_din_ref().
  *
  * @param[in] pin       The pin number to read [8-15].
@@ -99,6 +109,8 @@ int8_t rpp_din_get_tr(uint8_t pin);
  *
  * Call rpp_din_update() to update cached values.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @param[in] pin       The pin number to read [0-15].
  *
  * @return HIGH or LOW if successful.\n
@@ -112,6 +124,8 @@ int8_t rpp_din_diag(uint8_t pin);
  * Read and update cached values and diagnostic values of all pins. Also commit
  * configuration changes.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0
+ *
  * @return SUCCESS when transaction was successful.\n
  *         FAILURE if transaction could not be confirmed.
  */
index 973aea511daa31899ec2cd6e0cdd328840c690c1..afea18ef71a202fd901523c52fc6f2137cc19f56 100644 (file)
@@ -3,7 +3,7 @@
  *
  * @file fr.h
  *
- * @copyright Copyright (C) 2013 Czech Technical University in Prague
+ * @copyright Copyright (C) 2013, 2015 Czech Technical University in Prague
  *
  * @author Carlos Jenkins <carlos@jenkins.co.cr>
  * @author Michal Horn <hornmich@fel.cvut.cz>
@@ -38,6 +38,8 @@ typedef enum {
 /**
  * FlexRay driver initialization.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * This method should be called before any other function from this
  * module.
  *
@@ -64,6 +66,9 @@ int8_t rpp_fr_init_driver(const Fr_ConfigType *config_ptr, uint32_t *error);
 /**
  * FlexRay controller initialization.
  *
+ * The function is thread safe, unless compiled with
+ * -DRPP_THREADSAFE=0.
+ *
  * This method should be called after rpp_fr_init_driver(), rpp_fr_halt_communication() or
  * rpp_fr_abort_communication() and before any other function from this module.
  *
@@ -101,6 +106,9 @@ int8_t rpp_fr_init_controller(uint8_t ctrl, uint32_t *error);
 /**
  * Start communication
  *
+ * The function is thread safe, unless compiled with
+ * -DRPP_THREADSAFE=0.
+ *
  * This method should be called after rpp_fr_init_controller() and
  * before any attempt to send or receive messages.
  *
@@ -145,6 +153,8 @@ int8_t rpp_fr_start_communication(uint8_t ctrl, uint32_t *error);
 /**
  * Allow communication on all slots
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The FlexRay node can be configured to communicate only on key
  * frames after the startup. If this is the case, after calling this
  * function, communication on all configured slots is allowed.
@@ -162,6 +172,8 @@ int8_t rpp_fr_all_slots(uint8_t ctrl);
 /**
  * Stop communication after the end of the communication cycle.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function should be called only after
  * rpp_fr_start_communication() has been called.
  *
@@ -193,6 +205,8 @@ int8_t rpp_fr_halt_communication(uint8_t ctrl);
 /**
  * Stop communication immediately.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function should be called only after
  * rpp_fr_start_communication() has been called.
  *
@@ -223,6 +237,8 @@ int8_t rpp_fr_abort_communication(uint8_t ctrl);
 /**
  * Send wakeup pattern.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function should be called after FlexRay controller is initialized
  * and before the communication start.
  *
@@ -237,6 +253,8 @@ int8_t rpp_fr_send_wup(uint8_t ctrl);
 /**
  * Set channel for wakeup pattern sending.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * This allows to change the configuration passed to the
  * rpp_fr_init_driver().
  *
@@ -255,6 +273,8 @@ int8_t rpp_fr_set_wu_channel(uint8_t ctrl, Fr_ChannelType channel);
 /**
  * Get FlexRay POC state
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after the driver is
  * initialized.
  *
@@ -270,6 +290,8 @@ int8_t rpp_fr_get_poc_status(uint8_t ctrl, Fr_POCStatusType *poc_status_ptr);
 /**
  * Send a message on the bus in the specified frame
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time when communication is running
  * (rpp_fr_start_communication() was called).
  *
@@ -293,6 +315,8 @@ int8_t rpp_fr_transmit_lpdu(uint8_t ctrl, uint16_t lpdu_idx, const uint8_t *lsdu
 /**
  * Cancel a transmission of the message.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time when communication is running
  * (rpp_fr_start_communication() was called).
  *
@@ -311,6 +335,8 @@ int8_t rpp_fr_cancel_transmit_lpdu(uint8_t ctrl, uint16_t lpdu_idx);
 /**
  * Receive a message from the bus from the specified frame.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time when communication is running
  * (rpp_fr_start_communication() was called).
  *
@@ -341,6 +367,8 @@ int8_t rpp_fr_receive_lpdu(uint8_t ctrl, uint16_t lpdu_idx, uint8_t *lsdu, Fr_Rx
 /**
  * Check TX buffer status
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time when communication is running
  * (rpp_fr_start_communication() was called).
  *
@@ -367,6 +395,8 @@ int8_t rpp_fr_check_tx_lpdu_status(uint8_t ctrl, uint16_t lpdu_idx, Fr_TxLPduSta
 /**
  * Reconfigure buffer
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time when after controller was initialized
  * (rpp_fr_init_controller() was called).
  *
@@ -397,6 +427,8 @@ int8_t rpp_fr_reconfigure_lpdu(uint8_t ctrl, uint16_t lpdu_idx, uint16_t frame_i
 /**
  * Disable a FlexRay buffer
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time when communication is running
  * (rpp_fr_start_communication() was called).
  *
@@ -415,6 +447,8 @@ int8_t rpp_fr_disable_lpdu(uint8_t ctrl, uint16_t lpdu_idx);
 /**
  * Get FlexRay global time
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after the controller is initialized.
  *
  * @param [in] ctrl Not used, set always to zero.
@@ -430,6 +464,8 @@ int8_t rpp_fr_get_global_time(uint8_t ctrl, uint8_t *cycle, uint16_t *macroticks
 /**
  * Get Network management vector.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after the controller is initialized.
  *
  * The FlexRay controller updates the vector at the end of every cycle
@@ -453,6 +489,8 @@ int8_t rpp_fr_get_network_management_vector(uint8_t ctrl, uint8_t *nm_vector);
 /**
  * Get channel status.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after the controller is
  * initialized.
  *
@@ -487,6 +525,8 @@ int8_t rpp_fr_get_channel_status(uint8_t ctrl, uint16_t *channel_a_status, uint1
 /**
  * Get clock correction.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called while the communication is running or
  * was stopped.
  *
@@ -503,6 +543,8 @@ int8_t rpp_fr_get_clock_correction(uint8_t ctrl, int16_t *rate_correction, int32
 /**
  * Gets a list of syncframes received or transmitted on channel A and channel B via the even and odd communication cycle.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called while communication is running or was stopped.
  *
  * @param [in] ctrl Not used, set always to zero.
@@ -533,6 +575,8 @@ int8_t rpp_fr_get_sync_frame_list(uint8_t ctrl, uint8_t list_size, uint16_t *cha
 /**
  * Get the status of WUP receiving
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after controller is initialized.
  *
  * If the Wake Up Pattern was received on some channel, the flag is set in status address.
@@ -552,6 +596,8 @@ int8_t rpp_fr_get_wakeup_rx_status(uint8_t ctrl, uint8_t *status);
 /**
  * Set the absolute timer and start it.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after communication is running.
  *
  * The FlexRay controller has two absolute timers. Each of them can be
@@ -577,6 +623,8 @@ int8_t rpp_fr_set_timer(uint8_t ctrl, uint8_t timer_idx, uint8_t cycle_set, uint
 /**
  * Stop the absolute timer, clear IRQ.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after communication is running.
  *
  * The FlexRay controller has two absolute timers. Each of them can be
@@ -601,6 +649,8 @@ int8_t rpp_fr_cancel_timer(uint8_t ctrl, uint8_t timer_idx);
 /**
  * Clear the IRQ flag of the absolute timer
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after communication is running.
  *
  * The FlexRay controller has two absolute timers. Each of them can be
@@ -623,6 +673,8 @@ int8_t rpp_fr_clear_timer_irq(uint8_t ctrl, uint8_t timer_idx);
 /**
  * Get IRQ flag of the absolute timer
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time after communication is running.
  *
  * The FlexRay controller has two absolute timers. Each of them can be
@@ -646,6 +698,8 @@ int8_t rpp_fr_get_timer_irq_status(uint8_t ctrl, uint8_t timer_idx, boolean_t *i
 /**
  * Get information about the driver vendor, module and version.
  *
+ * The function is thread safe
+ *
  * The function can be called any time.
  *
  * @param [out] version Address, where the information will be stored.
@@ -658,6 +712,8 @@ int8_t rpp_fr_get_driver_version(Std_VersionInfoType *version);
 /**
  * Get configuration parameter value from the internal driver structures.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * The function can be called any time.
  *
  * @param [in] ctrl Not used, set always to zero.
index 0ffc8426847abb7b6aafcc00494a5cd886fc7d50..9b00364089276f655a40652d6c4dc2eef0cc8fa1 100644 (file)
@@ -31,6 +31,9 @@
  * @param[in] init_ports Specifies which ports to initialize.
  * RPP_GIO_PORT_ALL causes all ports to be initialized.
  *
+ * This function is not thread safe. Do not call it from multiple
+ * threads.
+ *
  * @return SUCCESS if successful, FAILURE otherwise.
  */
 int8_t rpp_gio_init(uint32_t init_ports);
@@ -38,6 +41,8 @@ int8_t rpp_gio_init(uint32_t init_ports);
 /**
  * Sets GIO output pin to a value.
  *
+ * The function is thread safe.
+ *
  * @param pin Pin to set
  * @param value Value to the pin to.
  *
@@ -48,6 +53,8 @@ int8_t rpp_gio_set(enum pin_name pin, boolean_t value);
 /**
  * Reads the value of a GIO pin.
  *
+ * The function is thread safe.
+ *
  * @param pin Pin to read.
  *
  * @return Pin value (0 or 1) in case of success, FAILURE otherwise.
@@ -70,6 +77,8 @@ enum rpp_gio_io {
 /**
  * Configure GIO pin.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * @param pin Pin to configure
  * @param io Configure the pin as input or output.
  * @param in_mode Input settings. Ignored when pin is configured as
index 8d3c0d7e8867c7054b9544acf78ac6c2abbc1d0a..cb3c9b3cd93b2325afb750bc87fd03c4942b9ecc 100644 (file)
@@ -17,6 +17,8 @@
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -26,6 +28,8 @@ int8_t rpp_hbr_init();
 /**
  * Enable the H-Bridge output and set PWM frequency.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Sets duty cycle to zero, direction to LOW and enables the H-Bridge
  * output. Duty cycle and direction can be later controlled with
  * rpp_hbr_control() (see below).
@@ -55,6 +59,8 @@ int8_t rpp_hbr_enable(int32_t period);
 /**
  * Control the H-Bridge direction and PWM duty cycle.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * @param[in] cmd       [-1.0, 1.0] A double between the previous range to
  *                      command the H-Bridge in the following manner:
  *                          - cmd > 0 : direction set to HIGH and PWM
@@ -86,6 +92,8 @@ int8_t rpp_hbr_control(double cmd);
 /**
  * Disable the H-Bridge output.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Completely disable H-Bridge. After this call the H-Bridge cannot be
  * controlled again until another call to rpp_hbr_enable() is made.
  *
index 0af093217b513c2208433d39d7b5a7655bfed8be..8b1a000b292d280826cb210befb80f84a4c94ddb 100644 (file)
@@ -24,7 +24,9 @@
 /**
  * IRC module initialization.
  *
- * Call this fuction before using this module.
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
+ * Call this function before using this module.
  *
  * @return SUCCESS    if initialization successful.\n
  *         FAILURE    if module already initialized.
@@ -34,6 +36,8 @@ int8_t rpp_irc_init();
 /**
  * IRC enable
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Call this function to enable IRC.
  * Reconfigures PINMUX register and enables N2HET program for specified IRC.
  * After calling this method corresponding pins are not usable as DIN.
@@ -51,6 +55,8 @@ int8_t rpp_irc_enable(uint8_t irc);
 /**
  * Check IRC status.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * @param[in] irc IRC sensor connection number.\n
  *                RPP_IRC_1 IRC connected to DIN10, DIN11\n
  *                RPP_IRC_2 IRC connected to DIN14, DIN15\n
@@ -65,6 +71,8 @@ int8_t rpp_irc_status(uint8_t irc);
 /**
  * Get the current value.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * @param[in] irc IRC sensor connection number.\n
  *                RPP_IRC_1 IRC connected to DIN10, DIN11\n
  *                RPP_IRC_2 IRC connected to DIN14, DIN15\n
@@ -76,6 +84,8 @@ int32_t rpp_irc_get(uint8_t irc);
 /**
  * IRC disable
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Call this function to disable IRC.
  * This function reenables the DIN functionality.
  *
index 450c7901593617876f7c4ca0821a390ed53033f4..4873a73e6f7bfaecac075348644edf7cebfbc3f4 100644 (file)
@@ -17,6 +17,8 @@
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -26,6 +28,8 @@ int8_t rpp_lout_init();
 /**
  * Set the output cache of given pin to given value.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Call rpp_lout_update() to flush cached values to real hardware.
  *
  * @param[in] pin       The pin number to set [1-8].
@@ -40,6 +44,8 @@ int8_t rpp_lout_set(uint8_t pin, uint8_t val);
 /**
  * Get the diagnostic cached value for given pin.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Call rpp_lout_update() to update cached values.
  *
  * @param[in] pin       The pin number to read [1-8].
@@ -53,6 +59,8 @@ int8_t rpp_lout_diag(uint8_t pin);
 /**
  * Flush cached output values and read back diagnostic values of all pins.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * @return SUCCESS when transaction was successful.\n
  *         FAILURE if transaction could not be confirmed.
  */
index a51cb4ee86ac3060d562c873f0fd075576996dd0..1250dd82fd7c9605c6ed5c565fbc6cee3c8173bf 100644 (file)
@@ -17,6 +17,8 @@
  *
  * Call this method before using this module.
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -26,6 +28,8 @@ int8_t rpp_mout_init();
 /**
  * Set the output of given pin to given value.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * This function will also verify if a faulty condition is detected. See return
  * documentation below for details.
  *
@@ -47,6 +51,8 @@ int8_t rpp_mout_set(uint8_t pin, uint8_t val);
 /**
  * Get the cached value of the given pin set by rpp_mout_set().
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * This will not read the value on the pin. To confirm if the output is
  * correctly set call rpp_mout_diag() and confirm SUCCESS.
  *
@@ -61,6 +67,8 @@ int8_t rpp_mout_get(uint8_t pin);
 /**
  * Reads the value on the given diagnostic pin.
  *
+ * The function is thread safe, unless compiled with -DRPP_THREADSAFE=0.
+ *
  * Note that rpp_mout_set() calls this routine already before returning.
  *
  * @param[in] pin       The pin number to read [1-6].
diff --git a/rpp/include/rpp/mutex.h b/rpp/include/rpp/mutex.h
new file mode 100644 (file)
index 0000000..6c30c50
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2015 Czech Technical University in Prague
+ *
+ * Authors:
+ *     - Michal Sojka <sojkam1@fel.cvut.cz>
+ *
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
+ *
+ */
+
+#ifndef RPP_MUTEX_H
+#define RPP_MUTEX_H
+
+#ifndef RPP_THREADSAFE
+#define RPP_THREADSAFE 1
+#endif
+
+#if RPP_THREADSAFE
+
+#include <os/semphr.h>
+
+#define RPP_MUTEX_DEFINE(mutex) static xSemaphoreHandle mutex
+#define RPP_MUTEX_INIT(mutex)   (((mutex) = xSemaphoreCreateMutex()) != NULL)
+#define RPP_MUTEX_LOCK(mutex)   xSemaphoreTake((mutex), portMAX_DELAY)
+#define RPP_MUTEX_UNLOCK(mutex) xSemaphoreGive(mutex)
+
+#else
+
+#define RPP_MUTEX_DEFINE(mutex)
+#define RPP_MUTEX_INIT(mutex) true
+#define RPP_MUTEX_LOCK(mutex)
+#define RPP_MUTEX_UNLOCK(mutex)
+
+#endif
+
+#endif
index 37ae7a9ba48ea15f689b9966d19b3de75403cb9c..824d52a7750d6a81d81f96e25b4cee1067f80754 100644 (file)
@@ -15,6 +15,8 @@
 /* Base includes */
 #include "base.h"
 
+#include "rpp/mutex.h"
+
 /* Include modules */
 #if defined(TARGET_POSIX)
 #include "rpp/adc.h"
@@ -51,8 +53,6 @@
 #error No supported target specified!
 #endif /* TARGET_TMS570_HDK */
 
-
-
 /* Library main functions */
 
 /**
index 2e7b9bf349b6e5286bc28e341c3790be15112bfb..e3481ff244daecfb34831f87c16fea8e0357d1a0 100644 (file)
@@ -3,7 +3,7 @@
  *
  * @file sci.h
  *
- * @copyright Copyright (C) 2013-2014 Czech Technical University in Prague
+ * @copyright Copyright (C) 2013-2015 Czech Technical University in Prague
  *
  * @author Carlos Jenkins <carlos@jenkins.co.cr>
  */
@@ -22,6 +22,8 @@
  * initialized to 115200. It can be changed later by calling
  * rpp_sci_setup().
  *
+ * This function is not thread safe. Do not call it from multiple threads.
+ *
  * @return SUCCESS if initialization successful.\n
  *         FAILURE if module already initialized.
  */
@@ -31,6 +33,8 @@ int8_t rpp_sci_init();
 /**
  * SCI module setup.
  *
+ * The function is thread safe.
+ *
  * Configure the SCI module.
  *
  * @param[in] baud      Baud rate for the SCI. Tested values are 9600 and 115200
@@ -46,6 +50,8 @@ boolean_t rpp_sci_setup(uint32_t baud);
 /**
  * Number of bytes available on input buffer.
  *
+ * The function is thread safe.
+ *
  * Get the number of bytes (characters) available for reading from the serial
  * port. This is data that's already arrived and stored in the SCI input buffer.
  *
@@ -57,6 +63,8 @@ uint16_t rpp_sci_available();
 /**
  * Read n number of bytes from input buffer.
  *
+ * The function is thread safe.
+ *
  * Transfer given amount of bytes from SCI input buffer into given buffer.
  * Buffer should be at least as large as the amount of data requested.
  *
@@ -75,6 +83,8 @@ int8_t rpp_sci_read(uint32_t amount, uint8_t *buffer);
 /**
  * Read n number of bytes from input buffer if possible.
  *
+ * The function is thread safe.
+ *
  * Transfer given amount of bytes from SCI input buffer into given buffer.
  * Buffer should be at least as large as the amount of data requested.
  *
@@ -95,6 +105,8 @@ int8_t rpp_sci_read_nb(uint32_t amount, uint8_t *buffer);
 /**
  * Write n number of bytes to the output buffer.
  *
+ * The function is thread safe.
+ *
  * Transfer given amount of bytes from given buffer to the SCI output buffer.
  * Data buffer should be at least as large as the amount of data requested to be
  * sent.
@@ -115,6 +127,8 @@ int8_t rpp_sci_write(uint32_t amount, uint8_t *data);
 /**
  * Write n number of bytes to the output buffer if possible.
  *
+ * The function is thread safe.
+ *
  * Transfer given amount of bytes from given buffer to the SCI output buffer.
  * Data buffer should be at least as large as the amount of data requested to be
  * sent.
@@ -140,6 +154,8 @@ int8_t rpp_sci_write_nb(uint32_t amount, uint8_t *data);
 /**
  * Flush incomming or outgoing buffers.
  *
+ * The function is thread safe.
+ *
  * This is a blocking call.
  *
  * This will block if another process is writing to or reading data from the
@@ -158,6 +174,8 @@ int8_t rpp_sci_flush(boolean_t buff);
 /**
  * C style printk using RPP SCI module.
  *
+ * The function is not thread safe.
+ *
  * This call is intended to be used from interrupt only.
  * Function blocks until whole string was sent to the SCI output, therefore
  * it should be used with care. Frequent usage is for debugging.
@@ -187,6 +205,8 @@ int32_t rpp_sci_printk(const char *format, ...);
 /**
  * C style printk using RPP SCI module.
  *
+ * The function is not thread safe.
+ *
  * This call is intended to be used from interrupt only.
  * Function tries to append string to the output buffer.
  *
@@ -215,6 +235,8 @@ int32_t rpp_sci_printkb(const char *format, ...);
 /**
  * C style printf using RPP SCI module.
  *
+ * The function is thread safe.
+ *
  * This is a blocking call.
  *
  * This function will wait until all the bytes are sent to the SCI output
@@ -246,6 +268,8 @@ int32_t rpp_sci_printf(const char *format, ...);
 /**
  * C style vprintf using RPP SCI module.
  *
+ * The function is thread safe.
+ *
  * This is a blocking call.
  *
  * This function will wait until all the bytes are sent to the SCI output
@@ -277,6 +301,8 @@ int32_t rpp_sci_vprintf(const char *format, va_list args);
 /**
  * C style putc (put character) using RPP SCI module.
  *
+ * The function is not thread safe.
+ *
  * This is a blocking call.
  *
  * This function will wait until the given byte is put into SCI output buffer if
@@ -292,6 +318,8 @@ int8_t rpp_sci_putc(uint8_t byte);
 /**
  * C style getc (get character) using RPP SCI module.
  *
+ * The function is not thread safe.
+ *
  * This is a blocking call.
  *
  * This function will wait until a byte is available in the SCI input buffer if
index bdceb866f7a997cfdabab798fcc6a05710c4ebb7..8e960788ce341578c33cb24f132733640bae7c1a 100644 (file)
@@ -102,7 +102,7 @@ int32_t rpp_sdr_printf(const char *format, ...);
 /**
  * Clear log.
  *
- * This funtion will clear all the data from the log. This will also stop the
+ * This function will clear all the data from the log. This will also stop the
  * show task if it's currently flushing the log.
  *
  * @return SUCCESS if log was cleared.\n
index e8f8fca3367b8efd0421abf6f96e5de60af875d1..02c0861c24aad26f7f85646fcb063f373be50554 100644 (file)
@@ -76,6 +76,7 @@ uint32_t drv_adc_generic_read(adcBASE_t *adc, uint32_t group,
        int adc_idx = (adc == adcREG1) ? 0 : 1;
        int grp_idx = (group == adcGROUP1) ? 0 : 1;
 
+       /* Note: Thread safety of rpp_adc_update depends on this! */
        xSemaphoreTake(mutex[adc_idx], portMAX_DELAY);
 
        // Calibrate
index 1cb0ca5be44ff2e08aaa286967b6b612650d6e0c..ceca8ac50add34bc463870af64a150fb5ab88564 100644 (file)
@@ -38,6 +38,9 @@ int spi_transfer_callback(struct spi_drv *ifc, int code, struct spi_msg_head *ms
        return 0;
 }
 
+/*
+ * This function is thread safe.
+ */
 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf)
 {
        spi_msg_head_t msg;
index 30e180d7294ee2de45ffffb7096a58fed099789c..911b6e1244dbf962fcd7af3042d67d5db3f0cbb2 100644 (file)
@@ -33,6 +33,7 @@ int8_t rpp_adc_init()
 {
        if (initialized)
                return FAILURE;
+
        initialized = TRUE;
 
 #ifndef FREERTOS_POSIX
@@ -64,6 +65,9 @@ int8_t rpp_adc_update()
 {
 #ifndef FREERTOS_POSIX
        const struct port_desc *port = &port_desc[PORT_ID_ADC];
+
+       /* Thread safety is ensured in port_adc_get() resp.
+        * drv_adc_generic_read() */
        port->get(port, in_cache, sizeof(in_cache));
 #endif
 
index fbea3bef3a3beb103d185eff0439f709617fd517..ef86da43fed5819e4d536e677f06cf709df7821a 100644 (file)
@@ -22,6 +22,9 @@
 #include "rpp/rpp.h"
 #include <math.h>
 #include "sys/ti_drv_dmm.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_can);
 
 static const struct rpp_can_config *can_config = NULL;
 
@@ -562,6 +565,9 @@ int8_t rpp_can_init(const struct rpp_can_config *config)
 {
        uint16_t i;
 
+       if (!RPP_MUTEX_INIT(mutex_can))
+               return FAILURE;
+
 #ifdef TARGET_TMS570_RPP
        dmmInit();
 
@@ -621,7 +627,6 @@ int8_t rpp_can_init(const struct rpp_can_config *config)
        return SUCCESS;
 }
 
-
 int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu)
 {
        uint8_t i;
@@ -631,6 +636,8 @@ int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu)
        if (!(controller = map_controller(tx_cfg->controller)))
                return -RPP_EINVAL;
 
+       RPP_MUTEX_LOCK(mutex_can);
+
        // Wait until IF1 is ready to use
        while (controller->IF1STAT & (1U << 7)) ;
 
@@ -660,6 +667,8 @@ int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu)
        // Copy TX data into message box
        controller->IF1NO = tx_cfg->msg_obj;
 
+       RPP_MUTEX_UNLOCK(mutex_can);
+
        return SUCCESS;
 }
 
@@ -692,9 +701,13 @@ int8_t rpp_can_read(rpp_can_hw_obj hw_obj, struct rpp_can_pdu *pdu)
        reg_index = (can_config->rx_config[hw_obj].msg_obj - 1) >> 5;
        bit_mask = 1 << ((can_config->rx_config[hw_obj].msg_obj - 1) & 0x1FU);
 
+       RPP_MUTEX_LOCK(mutex_can);
+
        // FIXME: Check whether to abort if there are no new data
-       if (!(controller->NWDATx[reg_index] & bit_mask))
+       if (!(controller->NWDATx[reg_index] & bit_mask)) {
+               RPP_MUTEX_UNLOCK(mutex_can);
                return -RPP_ENODATA;
+       }
 
        // Wait until IF2 is ready to use
        while (controller->IF2STAT & (1U << 7)) ;
@@ -723,6 +736,8 @@ int8_t rpp_can_read(rpp_can_hw_obj hw_obj, struct rpp_can_pdu *pdu)
 #endif
        }
 
+       RPP_MUTEX_UNLOCK(mutex_can);
+
        return SUCCESS;
 }
 
index b2bf2ba3249ccd41f0c6eb3e4d337d9196d6e043..f2bdbe0527050b7581f10da24bf5f44fba1d62b3 100644 (file)
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
 #ifndef FREERTOS_POSIX
 #include "drv/dac.h"
 #include "drv/spi_tms570.h"
 #endif
 
+RPP_MUTEX_DEFINE(mutex_dac);
+
 static boolean_t initialized = FALSE;
 
 int8_t rpp_dac_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_dac))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        spi_tms570_init();
@@ -64,12 +69,13 @@ int8_t rpp_dac_setup(uint8_t pin, boolean_t enabled)
 
        uint8_t index = pin - 1;
 
+       RPP_MUTEX_LOCK(mutex_dac);
        // Mark state
        enabled_cache[index] = enabled;
 
        // Mark as changed
        changed_st[index] = TRUE;
-
+       RPP_MUTEX_UNLOCK(mutex_dac);
        return SUCCESS;
 }
 
@@ -101,11 +107,13 @@ int8_t rpp_dac_set(uint8_t pin, uint16_t val)
 
        uint8_t index = pin - 1;
 
+       RPP_MUTEX_LOCK(mutex_dac);
        // Set value to output cache
        out_cache[index] = val;
 
        // Mark as changed
        changed_st[index] = TRUE;
+       RPP_MUTEX_UNLOCK(mutex_dac);
 
        return SUCCESS;
 }
@@ -128,11 +136,13 @@ int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
 
        uint8_t index = pin - 1;
 
+       RPP_MUTEX_LOCK(mutex_dac);
        // Set value to output cache
        out_cache[index] = val;
 
        // Mark as changed
        changed_st[index] = TRUE;
+       RPP_MUTEX_UNLOCK(mutex_dac);
 
        return SUCCESS;
 }
@@ -143,7 +153,7 @@ int8_t rpp_dac_update()
        int i = 0;
 
        for (i = 0; i < 4; i++) {
-
+               RPP_MUTEX_LOCK(mutex_dac);
                // If changed commit changes to hardware
                if (changed_st[i]) {
 
@@ -157,6 +167,7 @@ int8_t rpp_dac_update()
 
                        changed_st[i] = FALSE;
                }
+               RPP_MUTEX_UNLOCK(mutex_dac);
        }
 
        return SUCCESS;
index 1892faa440cdf8703139185c6f53f73fbf6bb2e6..e65380a059a82d8d66335dac61c1e7484757cd92 100644 (file)
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
 #ifndef FREERTOS_POSIX
 #include "drv/din.h"
 #include "drv/spi_tms570.h"
 #endif
 
+RPP_MUTEX_DEFINE(mutex_din);
+
 static boolean_t initialized = FALSE;
 
 int8_t rpp_din_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_din))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        dmmInit();
@@ -48,7 +53,9 @@ int8_t rpp_din_ref(uint16_t ref_a, uint16_t ref_b)
                return -1;
 
 #ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_din);
        drv_din_ref(ref_a, ref_b);
+       RPP_MUTEX_UNLOCK(mutex_din);
 #endif
        return SUCCESS;
 }
@@ -82,9 +89,12 @@ int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
        if (!pull_up && (pin > 7))
                return -2;
 
+       RPP_MUTEX_LOCK(mutex_din);
        // Check blockade of specific pins
-       if (check_pin_busy(pin))
+       if (check_pin_busy(pin)) {
+               RPP_MUTEX_UNLOCK(mutex_din);
                return -RPP_EBUSY;
+       }
 
        // Set bits
        if (pull_up)
@@ -103,6 +113,7 @@ int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
                bit_clear(can_wake_cache, pin);
 
        config_changed = TRUE;
+       RPP_MUTEX_UNLOCK(mutex_din);
        return SUCCESS;
 }
 
@@ -119,10 +130,7 @@ int8_t rpp_din_get(uint8_t pin)
        if (check_pin_busy(pin))
                return -RPP_EBUSY;
 
-
-       if (is_bit_set(in_cache, pin))
-               return RPP_CLOSED;
-       return RPP_OPEN;
+       return is_bit_set(in_cache, pin) ? RPP_CLOSED : RPP_OPEN;
 }
 
 int8_t rpp_din_get_tr(uint8_t pin)
@@ -135,11 +143,9 @@ int8_t rpp_din_get_tr(uint8_t pin)
        if (check_pin_busy(pin))
                return -RPP_EBUSY;
 
-
 #ifndef FREERTOS_POSIX
        if (drv_din_get_varthr(pin) == 1)
                return HIGH;
-
 #endif
        return LOW;
 }
@@ -158,10 +164,7 @@ int8_t rpp_din_diag(uint8_t pin)
        if (check_pin_busy(pin))
                return -RPP_EBUSY;
 
-
-       if (is_bit_set(diag_cache, pin))
-               return HIGH;
-       return LOW;
+       return is_bit_set(diag_cache, pin) ? HIGH : LOW;
 }
 
 /*
@@ -169,6 +172,7 @@ int8_t rpp_din_diag(uint8_t pin)
  */
 int8_t rpp_din_update()
 {
+       RPP_MUTEX_LOCK(mutex_din);
 #ifndef FREERTOS_POSIX
        /// Setup pins
        if (config_changed) {
@@ -219,12 +223,15 @@ int8_t rpp_din_update()
        // FIXME: Implement. Dummy assign for now.
        diag_cache = in_cache;
 
-       if (diag_cache != in_cache)
+       if (diag_cache != in_cache) {
+               RPP_MUTEX_UNLOCK(mutex_din);
                return FAILURE;
+       }
 
        #else /* ifndef FREERTOS_POSIX */
        UNUSED(config_changed);
        #endif /* ifndef FREERTOS_POSIX */
 
+       RPP_MUTEX_UNLOCK(mutex_din);
        return SUCCESS;
 }
index 9193a509374cf3809ea1d4b6fd3451fd7444a76a..728ee8053facfc58673bfb975b5c1f92954d6274 100644 (file)
@@ -27,6 +27,9 @@
 #include "string.h"
 #include "drv/drv.h"
 #include "drv/spi_tms570.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_fr);
 
 static rpp_fr_state_t rpp_fr_state = RPP_FR_NOT_INITIALIZED;    /**< Stores the actual state of the FlexRay module */
 
@@ -39,6 +42,8 @@ int8_t rpp_fr_init_driver(const Fr_ConfigType *config_ptr, uint32_t *error)
 #ifndef FREERTOS_POSIX
        spi_tms570_init();
 #endif
+       if (!RPP_MUTEX_INIT(mutex_fr))
+               return FAILURE;
        Fr_Init(config_ptr);
        rpp_fr_state = RPP_FR_DRV_INITIALIZED;
        return SUCCESS;
@@ -47,197 +52,329 @@ int8_t rpp_fr_init_driver(const Fr_ConfigType *config_ptr, uint32_t *error)
 int8_t rpp_fr_init_controller(uint8_t ctrl, uint32_t *error)
 {
        Std_ReturnType retVal = ERR_PARAM_NO_ERROR;
-
+       RPP_MUTEX_LOCK(mutex_fr);
        if (rpp_fr_state == RPP_FR_DRV_INITIALIZED ||
                rpp_fr_state == RPP_FR_HALTED ||
                rpp_fr_state == RPP_FR_ABORTED) {
                retVal = Fr_ControllerInit(ctrl);
                if (retVal & E_OK) {
                        rpp_fr_state = RPP_FR_CTR_INITIALIZED;
+                       RPP_MUTEX_UNLOCK(mutex_fr);
                        return SUCCESS;
                }
                else {
                        *error = retVal;
+                       RPP_MUTEX_UNLOCK(mutex_fr);
                        return FAILURE;
                }
        }
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_start_communication(uint8_t ctrl, uint32_t *error)
 {
        Std_ReturnType retVal = ERR_PARAM_NO_ERROR;
 
+       RPP_MUTEX_LOCK(mutex_fr);
        if (rpp_fr_state == RPP_FR_CTR_INITIALIZED) {
                retVal = Fr_StartCommunication(ctrl);
                if (retVal & E_OK) {
                        rpp_fr_state = RPP_FR_RUNNING;
+                       RPP_MUTEX_UNLOCK(mutex_fr);
                        return SUCCESS;
+
                }
                else {
                        *error = retVal;
+                       RPP_MUTEX_UNLOCK(mutex_fr);
                        return FAILURE;
                }
        }
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
 }
 
 int8_t rpp_fr_all_slots(uint8_t ctrl)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_AllSlots(ctrl) & E_OK)
-               return SUCCESS;
-       return FAILURE;
+       int8_t ret;
+
+       RPP_MUTEX_LOCK(mutex_fr);
+       ret = (rpp_fr_state == RPP_FR_RUNNING && Fr_AllSlots(ctrl) & E_OK);
+       RPP_MUTEX_UNLOCK(mutex_fr);
+
+       return ret ? SUCCESS : FAILURE;
 }
 
 int8_t rpp_fr_halt_communication(uint8_t ctrl)
 {
+       RPP_MUTEX_LOCK(mutex_fr);
        if (rpp_fr_state == RPP_FR_RUNNING && Fr_HaltCommunication(ctrl) & E_OK) {
                rpp_fr_state = RPP_FR_HALTED;
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+
        }
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
 }
 
 int8_t rpp_fr_abort_communication(uint8_t ctrl)
 {
+       RPP_MUTEX_LOCK(mutex_fr);
        if (rpp_fr_state == RPP_FR_RUNNING && Fr_AbortCommunication(ctrl) & E_OK) {
                rpp_fr_state = RPP_FR_ABORTED;
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+
        }
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
 }
 
 int8_t rpp_fr_send_wup(uint8_t ctrl)
 {
-       if (rpp_fr_state == RPP_FR_CTR_INITIALIZED && Fr_SendWUP(ctrl) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_CTR_INITIALIZED && Fr_SendWUP(ctrl) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
 }
 
 int8_t rpp_fr_set_wu_channel(uint8_t ctrl, Fr_ChannelType channel)
 {
-       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_SetWakeupChannel(ctrl, channel) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_SetWakeupChannel(ctrl, channel) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
 }
 
 int8_t rpp_fr_get_poc_status(uint8_t ctrl, Fr_POCStatusType *poc_status_ptr)
 {
-       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetPOCStatus(ctrl, poc_status_ptr) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetPOCStatus(ctrl, poc_status_ptr) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
 }
 
 int8_t rpp_fr_transmit_lpdu(uint8_t ctrl, uint16_t lpdu_idx, const uint8_t *lsdu, uint8_t lsdu_length)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_TransmitTxLPdu(ctrl, lpdu_idx, lsdu, lsdu_length) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_TransmitTxLPdu(ctrl, lpdu_idx, lsdu, lsdu_length) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+
+       }
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_cancel_transmit_lpdu(uint8_t ctrl, uint16_t lpdu_idx)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_CancelTxLPdu(ctrl, lpdu_idx) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_CancelTxLPdu(ctrl, lpdu_idx) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_receive_lpdu(uint8_t ctrl, uint16_t lpdu_idx, uint8_t *lsdu, Fr_RxLPduStatusType *lpdu_status, uint8_t *lsdu_length)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_ReceiveRxLPdu(ctrl, lpdu_idx, lsdu, lpdu_status, lsdu_length) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_ReceiveRxLPdu(ctrl, lpdu_idx, lsdu, lpdu_status, lsdu_length) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_check_tx_lpdu_status(uint8_t ctrl, uint16_t lpdu_idx, Fr_TxLPduStatusType *lpdu_status)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_CheckTxLPduStatus(ctrl, lpdu_idx, lpdu_status) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_CheckTxLPduStatus(ctrl, lpdu_idx, lpdu_status) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_reconfigure_lpdu(uint8_t ctrl, uint16_t lpdu_idx, uint16_t frame_id, Fr_ChannelType channel, uint8_t cycle_set, uint8_t cycle_offset, uint8_t payload, uint16_t header_crc)
 {
-       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_ReconfigLPdu(ctrl, lpdu_idx, frame_id, channel, cycle_set, cycle_offset, payload, header_crc) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_ReconfigLPdu(ctrl, lpdu_idx, frame_id, channel, cycle_set, cycle_offset, payload, header_crc) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_disable_lpdu(uint8_t ctrl, uint16_t lpdu_idx)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_DisableLPdu(ctrl, lpdu_idx) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_DisableLPdu(ctrl, lpdu_idx) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_global_time(uint8_t ctrl, uint8_t *cycle, uint16_t *macroticks)
 {
-       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetGlobalTime(ctrl, cycle, macroticks) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetGlobalTime(ctrl, cycle, macroticks) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_network_management_vector(uint8_t ctrl, uint8_t *nm_vector)
 {
-       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetNmVector(ctrl, nm_vector) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetNmVector(ctrl, nm_vector) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_channel_status(uint8_t ctrl, uint16_t *channel_a_status, uint16_t *channel_b_status)
 {
-       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetChannelStatus(ctrl, channel_a_status, channel_b_status) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetChannelStatus(ctrl, channel_a_status, channel_b_status) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_clock_correction(uint8_t ctrl, int16_t *rate_correction, int32_t *offset_correction)
 {
-       if (rpp_fr_state >= RPP_FR_RUNNING && Fr_GetClockCorrection(ctrl, rate_correction, offset_correction) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_RUNNING && Fr_GetClockCorrection(ctrl, rate_correction, offset_correction) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_sync_frame_list(uint8_t ctrl, uint8_t list_size, uint16_t *channel_a_even_list, uint16_t *channel_b_even_list, uint16_t *channel_a_odd_list, uint16_t *channel_b_odd_list)
 {
-       if (rpp_fr_state >= RPP_FR_RUNNING && Fr_GetSyncFrameList(ctrl, list_size, channel_a_even_list, channel_b_even_list, channel_a_odd_list, channel_b_odd_list) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_RUNNING && Fr_GetSyncFrameList(ctrl, list_size, channel_a_even_list, channel_b_even_list, channel_a_odd_list, channel_b_odd_list) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_wakeup_rx_status(uint8_t ctrl, uint8_t *status)
 {
-       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetWakeupRxStatus(ctrl, status) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetWakeupRxStatus(ctrl, status) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_set_timer(uint8_t ctrl, uint8_t timer_idx, uint8_t cycle_set, uint16_t offset_threshold)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_SetAbsoluteTimer(ctrl, timer_idx, cycle_set, offset_threshold) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_SetAbsoluteTimer(ctrl, timer_idx, cycle_set, offset_threshold) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_cancel_timer(uint8_t ctrl, uint8_t timer_idx)
 {
-       if (rpp_fr_state == RPP_FR_RUNNING && Fr_CancelAbsoluteTimer(ctrl, timer_idx) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state == RPP_FR_RUNNING && Fr_CancelAbsoluteTimer(ctrl, timer_idx) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_clear_timer_irq(uint8_t ctrl, uint8_t timer_idx)
 {
-       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_AckAbsoluteTimerIRQ(ctrl, timer_idx) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_AckAbsoluteTimerIRQ(ctrl, timer_idx) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_timer_irq_status(uint8_t ctrl, uint8_t timer_idx, boolean_t *irq_pending)
 {
-       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetAbsoluteTimerIRQStatus(ctrl, timer_idx, irq_pending) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetAbsoluteTimerIRQStatus(ctrl, timer_idx, irq_pending) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
 
 int8_t rpp_fr_get_driver_version(Std_VersionInfoType *version)
@@ -248,8 +385,15 @@ int8_t rpp_fr_get_driver_version(Std_VersionInfoType *version)
 
 int8_t rpp_fr_read_com_ctrl_config(uint8_t ctrl, uint8_t param_idx, uint32_t *param_value)
 {
-       if (Fr_ReadCCConfig(ctrl, param_idx, param_value) & E_OK)
+       RPP_MUTEX_LOCK(mutex_fr);
+       if (Fr_ReadCCConfig(ctrl, param_idx, param_value) & E_OK) {
+               RPP_MUTEX_UNLOCK(mutex_fr);
                return SUCCESS;
+       }
+
+       RPP_MUTEX_UNLOCK(mutex_fr);
        return FAILURE;
+
 }
+
 #endif /* FREERTOS_POSIX */
index 4cbdefcb4eefc13535992c66e2cce00bf32a69ba..8c83c484e1462e28a28ab6328c6e3d2d93fea238 100644 (file)
@@ -13,6 +13,9 @@
 #include "base.h"
 #include "rpp/gio.h"
 #include "drv/gio_tab.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_gio);
 
 static uint32_t ports_initialized = 0;
 
@@ -25,6 +28,8 @@ STATIC_ASSERT(RPP_GIO_PORT_GIOA == (1 << GIO_PORT_GIOA) &&
 int8_t rpp_gio_init(uint32_t init_ports)
 {
        enum pin_name pin;
+       if (!RPP_MUTEX_INIT(mutex_gio))
+               return FAILURE;
 
        gioREG->GCR0 = 1;   // Bring GIO out of reset
 
@@ -45,7 +50,7 @@ int8_t rpp_gio_set(enum pin_name pin, boolean_t value)
        if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
                return FAILURE;
 
-       gio_tab_set(pin, value);
+       gio_tab_set(pin, value);        /* Thread safe */
        return SUCCESS;
 }
 
@@ -54,7 +59,9 @@ int8_t rpp_gio_get(enum pin_name pin)
        if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
                return FAILURE;
 
-       return gio_tab_get(pin) ? 1 : 0;
+       boolean_t ret_val = gio_tab_get(pin) ? 1 : 0; /* Thread safe */
+
+       return ret_val;
 }
 
 int8_t rpp_gio_setup(enum pin_name pin, enum rpp_gio_io io, enum rpp_gio_in_mode in_mode,
@@ -73,6 +80,9 @@ int8_t rpp_gio_setup(enum pin_name pin, enum rpp_gio_io io, enum rpp_gio_in_mode
        dsc |= mode_flags[in_mode];
        dsc |= open_drain ? GIO_PIN_CONF_OD_ON : GIO_PIN_CONF_OD_OFF;
 
+       RPP_MUTEX_LOCK(mutex_gio);
        gio_setup(dsc);
+       RPP_MUTEX_UNLOCK(mutex_gio);
+
        return SUCCESS;
 }
index fe4e300abc73a346f264ab26c515a499e85a911d..eb411198371c72a8523274e4a5e798c2b684c41a 100644 (file)
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
 #ifndef FREERTOS_POSIX
 #include "drv/hbridge.h"
 #include "drv/spi_tms570.h"
 #endif
 
+RPP_MUTEX_DEFINE(mutex_hbr);
+
 static boolean_t initialized = FALSE;
 
 int8_t rpp_hbr_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_hbr))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        dmmInit();
@@ -63,22 +68,26 @@ int8_t rpp_hbr_enable(int32_t period)
        if (period < 1)
                period = 55;  // ~18kHz (18181.818181818 Hz to be precise)
 
-       rpp_hdr_reset();
-
 #ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_hbr);
+       rpp_hdr_reset();
 
        // Configure N2HET
-       if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS)
+       if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS) {
+               RPP_MUTEX_UNLOCK(mutex_hbr);
                return FAILURE;
+       }
        drv_hbr_pwm_start();
 
        drv_hbr_set_en(HIGH);
 
        // Start watchdog
        int ret = drv_hbr_wdg_start();
-       if (ret != SUCCESS && ret != -RPP_EBUSY)   // Don't fail if already started
+       if (ret != SUCCESS && ret != -RPP_EBUSY) {   // Don't fail if already started
+               RPP_MUTEX_UNLOCK(mutex_hbr);
                return FAILURE;
-
+       }
+       RPP_MUTEX_UNLOCK(mutex_hbr);
 #endif
 
        enabled = TRUE;
@@ -102,10 +111,12 @@ int8_t rpp_hbr_control(double cmd)
 
 
        // Set direction
+       RPP_MUTEX_LOCK(mutex_hbr);
        drv_hbr_set_dir(scaled > 0);
 
        // Set PWM duty cycle
        drv_hbr_pwm_set_duty(abs(scaled));
+       RPP_MUTEX_UNLOCK(mutex_hbr);
 #endif
 
        return SUCCESS;
@@ -118,12 +129,14 @@ int8_t rpp_hbr_disable()
                return FAILURE;
 
 #ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_hbr);
        rpp_hdr_reset();
 
        // We ignore is watchdog could not be stopped, because is harmless.
        // It would be worse if we just could not stop the H-Bridge just because
        // the watchdog could not be stopped.
        drv_hbr_wdg_stop();
+       RPP_MUTEX_UNLOCK(mutex_hbr);
 #endif
 
        enabled = FALSE;
index 5002922684f799ca392d4bbb2b51fab4cebe2aeb..6886744dd76f52ed66bf4e9e4db4a3128f67f5f5 100644 (file)
@@ -18,6 +18,9 @@
  */
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_irc);
 
 #ifndef FREERTOS_POSIX
 #include "sys/sys.h"
@@ -28,7 +31,6 @@
 #endif
 
 static boolean_t initialized = FALSE;
-
 static boolean_t rpp_irc_enabled[2] = {FALSE, FALSE};
 
 int8_t rpp_irc_init()
@@ -36,6 +38,8 @@ int8_t rpp_irc_init()
 
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_irc))
+               return FAILURE;
 
 #ifndef FREERTOS_POSIX
        het2Init();
@@ -54,10 +58,12 @@ int8_t rpp_irc_enable(uint8_t irc)
        if (irc < 1 || irc > 2)
                return -RPP_EINVAL;
 
+       RPP_MUTEX_LOCK(mutex_irc);
        rpp_irc_enabled[irc-1] = TRUE;
 
        setMuxForIRC(irc, TRUE);
        ircEnable(irc);
+       RPP_MUTEX_UNLOCK(mutex_irc);
 
        return SUCCESS;
 }
@@ -71,7 +77,11 @@ int8_t rpp_irc_status(uint8_t irc)
        if (irc < 1 || irc > 2)
                return -RPP_EINVAL;
 
-       return rpp_irc_enabled[irc-1];
+       RPP_MUTEX_LOCK(mutex_irc);
+       boolean_t ret_val = rpp_irc_enabled[irc-1];
+       RPP_MUTEX_UNLOCK(mutex_irc);
+
+       return ret_val;
 }
 
 int32_t rpp_irc_get(uint8_t irc)
@@ -87,7 +97,10 @@ int32_t rpp_irc_get(uint8_t irc)
                return FAILURE;
 
 #ifndef FREERTOS_POSIX
-       return ircGet(irc);
+       RPP_MUTEX_LOCK(mutex_irc);
+       int32_t ret_val = ircGet(irc);
+       RPP_MUTEX_UNLOCK(mutex_irc);
+       return ret_val;
 #else
        return 1;
 #endif
@@ -105,8 +118,10 @@ int8_t rpp_irc_disable(uint8_t irc)
        if (rpp_irc_status(irc) != 1)
                return FAILURE;
 
+       RPP_MUTEX_LOCK(mutex_irc);
        ircDisable(irc);
        setMuxForIRC(irc, FALSE);
        rpp_irc_enabled[irc-1] = FALSE;
+       RPP_MUTEX_UNLOCK(mutex_irc);
        return SUCCESS;
 }
index 507506844b3b63839866c11f2ef6ded4a1496201..ca2366574fc936d57b328b19d6c2cf20666769f2 100644 (file)
@@ -19,6 +19,9 @@
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_lout);
 
 #ifndef FREERTOS_POSIX
 #include "drv/lout.h"
@@ -31,6 +34,8 @@ int8_t rpp_lout_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_lout))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        spi_tms570_init();
@@ -49,10 +54,12 @@ int8_t rpp_lout_set(uint8_t pin, uint8_t val)
                return -1;
 
        uint8_t index = pin - 1;
+       RPP_MUTEX_LOCK(mutex_lout);
        if (val)
                bit_set(out_cache, index);
        else
                bit_clear(out_cache, index);
+       RPP_MUTEX_UNLOCK(mutex_lout);
        return SUCCESS;
 }
 
@@ -64,15 +71,20 @@ int8_t rpp_lout_diag(uint8_t pin)
        if ((pin < 1) || (pin > 8))
                return -1;
 
+       int8_t ret_val = LOW;
+
+       RPP_MUTEX_LOCK(mutex_lout);
        if (is_bit_set(diag_cache, pin - 1))
-               return HIGH;
-       return LOW;
+               ret_val = HIGH;
+       RPP_MUTEX_UNLOCK(mutex_lout);
+       return ret_val;
 }
 
 
 int8_t rpp_lout_update()
 {
 #ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_lout);
        // Update output values
        lout_set_word(out_cache);
        // FIXME: Check which SPI transfer statuses could be considered errors
@@ -82,9 +94,11 @@ int8_t rpp_lout_update()
        // FIXME: Implement. Dummy assign for now.
        diag_cache = out_cache;
 
-       if (diag_cache != out_cache)
+       if (diag_cache != out_cache) {
+               RPP_MUTEX_UNLOCK(mutex_lout);
                return FAILURE;
-
+       }
+       RPP_MUTEX_UNLOCK(mutex_lout);
 #endif
 
        return SUCCESS;
index 9428af01a9db3327614ccfa53713caeb37ecd3df..c5a93d61b7e8641c037ca1a02ead733274f5244f 100644 (file)
@@ -19,6 +19,9 @@
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_mout);
 
 #ifndef FREERTOS_POSIX
 #include "drv/mout.h"
@@ -30,6 +33,8 @@ int8_t rpp_mout_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_mout))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        dmmInit();
@@ -59,9 +64,11 @@ int8_t rpp_mout_set(uint8_t pin, uint8_t val)
 
 #ifndef FREERTOS_POSIX
        // Set and store value
+       RPP_MUTEX_LOCK(mutex_mout);
        if (drv_mout_set(idx, val) != SUCCESS)
                return -3;
        cache[idx] = val;
+       RPP_MUTEX_UNLOCK(mutex_mout);
 
        // FIXME Wait some time for synchronization
        // Don't use vTaskDelay() here because the minimum time to wait is one tick,
@@ -72,12 +79,18 @@ int8_t rpp_mout_set(uint8_t pin, uint8_t val)
                asm (" nop");
        }
 
+       RPP_MUTEX_LOCK(mutex_mout);
        // Get value back and compare
-       if (drv_mout_diag(idx) == FAILURE)
+       if (drv_mout_diag(idx) == FAILURE) {
+               RPP_MUTEX_UNLOCK(mutex_mout);
                return -4;
+       }
+       RPP_MUTEX_UNLOCK(mutex_mout);
 
 #else
+       RPP_MUTEX_LOCK(mutex_mout);
        cache[idx] = val;
+       RPP_MUTEX_UNLOCK(mutex_mout);
 #endif
 
        return SUCCESS;
@@ -90,7 +103,11 @@ int8_t rpp_mout_get(uint8_t pin)
        if ((pin < 1) || (pin > 6))
                return -1;
 
-       return cache[pin - 1];
+       RPP_MUTEX_LOCK(mutex_mout);
+       int8_t ret_val = cache[pin - 1];
+       RPP_MUTEX_UNLOCK(mutex_mout);
+
+       return ret_val;
 }
 
 
@@ -101,9 +118,11 @@ int8_t rpp_mout_diag(uint8_t pin)
                return -1;
 
 #ifndef FREERTOS_POSIX
-       if (drv_mout_diag(pin - 1) == 0)
+       RPP_MUTEX_LOCK(mutex_mout);
+       if (drv_mout_diag(pin - 1) == 0) {
+               RPP_MUTEX_UNLOCK(mutex_mout);
                return FAILURE;
-
+       }
 #endif
        return SUCCESS;
 }
index 08893808be2c75914ccb5205fa2ae6003a4b8b5b..f324c9ad8821f9ebc7b7df9476eb868e3a054816 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2015 Czech Technical University in Prague
  *
  * Authors:
  *     - Carlos Jenkins <carlos@jenkins.co.cr>