LCOV - code coverage report
Current view: top level - firmware/hw_layer - io_pins.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 16 100.0 %
Date: 2024-04-25 02:23:43 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file        io_pins.cpp
       3             :  * @brief       his file is about general input/output utility methods, not much EFI-specifics
       4             :  *
       5             :  *
       6             :  * @date Jan 24, 2013
       7             :  * @author Andrey Belomutskiy, (c) 2012-2020
       8             :  */
       9             : 
      10             : #include "pch.h"
      11             : 
      12             : #if EFI_PROD_CODE
      13             : 
      14             : #include "gpio/gpio_ext.h"
      15             : 
      16             : #include "status_loop.h"
      17             : #include "console_io.h"
      18             : #endif /* EFI_PROD_CODE */
      19             : 
      20           1 : void efiSetPadUnused(brain_pin_e brainPin) {
      21             : #if EFI_PROD_CODE
      22             :         /* input with pull up, is it safe? */
      23             :         iomode_t mode = PAL_STM32_MODE_INPUT | PAL_STM32_PUPDR_PULLUP;
      24             : 
      25             :         if (brain_pin_is_onchip(brainPin)) {
      26             :                 ioportid_t port = getHwPort("unused", brainPin);
      27             :                 ioportmask_t pin = getHwPin("unused", brainPin);
      28             : 
      29             :                 /* input with pull up, is it safe?
      30             :                  * todo: shall we reuse 'default state' constants with board.h?
      31             :                  * */
      32             :                 palSetPadMode(port, pin, mode);
      33             :                 palWritePad(port, pin, 0);
      34             :         }
      35             :         #if (BOARD_EXT_GPIOCHIPS > 0)
      36             :                 else {
      37             :                         gpiochips_setPadMode(brainPin, mode);
      38             :                 }
      39             :         #endif
      40             : #endif /* EFI_PROD_CODE */
      41             : 
      42           1 :         brain_pin_markUnused(brainPin);
      43           1 : }
      44             : 
      45             : /**
      46             :  * This method would set an error condition if pin is already used
      47             :  */
      48          15 : void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode) {
      49          15 :         if (!isBrainPinValid(brainPin)) {
      50             :                 // No pin configured, nothing to do here.
      51           1 :                 return;
      52             :         }
      53             : 
      54          14 :         bool wasUsed = brain_pin_markUsed(brainPin, msg);
      55             : 
      56          14 :         if (!wasUsed) {
      57          14 :                 efiSetPadModeWithoutOwnershipAcquisition(msg, brainPin, mode);
      58             :         }
      59             : }
      60             : 
      61          14 : void efiSetPadModeWithoutOwnershipAcquisition(const char *msg, brain_pin_e brainPin, iomode_t mode) {
      62             : #if EFI_PROD_CODE
      63             :         /*check if on-chip pin or external */
      64             :         if (brain_pin_is_onchip(brainPin)) {
      65             :                 /* on-chip */
      66             :                 ioportid_t port = getHwPort(msg, brainPin);
      67             :                 ioportmask_t pin = getHwPin(msg, brainPin);
      68             :                 /* paranoid */
      69             :                 if (port == GPIO_NULL)
      70             :                         return;
      71             : 
      72             :                 palSetPadMode(port, pin, mode);
      73             :         }
      74             :         #if (BOARD_EXT_GPIOCHIPS > 0)
      75             :                 else {
      76             :                         gpiochips_setPadMode(brainPin, mode);
      77             :                 }
      78             :         #endif
      79             : 
      80             : #endif /* EFI_PROD_CODE */
      81          14 : }
      82             : 
      83             : #if EFI_PROD_CODE
      84             : 
      85             : #if EFI_ENGINE_CONTROL
      86             : #include "main_trigger_callback.h"
      87             : #endif /* EFI_ENGINE_CONTROL */
      88             : 
      89             : bool efiReadPin(brain_pin_e pin) {
      90             :         if (!isBrainPinValid(pin))
      91             :                 return false;
      92             :         if (brain_pin_is_onchip(pin))
      93             :                 return palReadPad(getHwPort("readPin", pin), getHwPin("readPin", pin));
      94             :         #if (BOARD_EXT_GPIOCHIPS > 0)
      95             :                 else if (brain_pin_is_ext(pin))
      96             :                         return (gpiochips_readPad(pin) > 0);
      97             :         #endif
      98             : 
      99             :         /* incorrect pin */
     100             :         return false;
     101             : }
     102             : 
     103             : iomode_t getInputMode(pin_input_mode_e mode) {
     104             :         switch (mode) {
     105             :         case PI_PULLUP:
     106             :                 return PAL_MODE_INPUT_PULLUP;
     107             :         case PI_PULLDOWN:
     108             :                 return PAL_MODE_INPUT_PULLDOWN;
     109             :         case PI_DEFAULT:
     110             :         default:
     111             :                 return PAL_MODE_INPUT;
     112             :         }
     113             : }
     114             : 
     115             : void writePad(const char *msg, brain_pin_e pin, int bit) {
     116             :         palWritePad(getHwPort(msg, pin), getHwPin(msg, pin), bit);
     117             : }
     118             : 
     119             : #else /* EFI_PROD_CODE */
     120             : 
     121             : // This has been made global so we don't need to worry about efiReadPin having access the object
     122             : //  we store it in, every time we need to use efiReadPin.
     123             : bool mockPinStates[BRAIN_PIN_COUNT];
     124             : 
     125          59 : bool efiReadPin(brain_pin_e pin) {
     126          59 :         return mockPinStates[static_cast<int>(pin)];
     127             : }
     128             : 
     129         310 : void setMockState(brain_pin_e pin, bool state) {
     130         310 :         mockPinStates[static_cast<int>(pin)] = state;
     131         310 : }
     132             : 
     133             : #endif /* EFI_PROD_CODE */

Generated by: LCOV version 1.14