/**************************************************************************** * NAME: bProcessGPDEvent * * DESCRIPTION: * Handles SW1 functionalities * * RETURNS: * Return whether device can go to sleep * ****************************************************************************/ bool bProcessGPDEvent(void) { bool bEnableSleep = TRUE; /* send command based on the state the device is in */ HandleGPDStateMachine(0); #ifdef DK4 /** wait till short press time is elapsed , enabling interrupts . If next * short press is initiated, interrupt should be fired while this time interval */ /* wait till button release */ bButtonPressed = FALSE; uint32 u32DIOVal = u32AHI_DioReadInput(); uint32 u32DIOVal2 = u32AHI_DioReadInput(); while(u32DIOVal2 == u32DIOVal) { u32DIOVal = u32AHI_DioReadInput(); } /* stop timer if already running (by previous button press )*/ vEH_StopTimer(); uint32 u32StartTime; /* start timer to account next button press interval */ u32StartTime = u32EH_StartTimer(); u32Button = 0; (void) u32AHI_DioInterruptStatus(); vAHI_DioInterruptEnable(APP_BUTTONS_DIO_MASK, 0); while( 1) { if(bEH_IsTimeElapsedInMicrosSec( APP_TIME_MS(SHORT_PRESS_TIME) ,u32StartTime)) { vEH_StopTimer(); sGPDData.u8ButtonCount = 0; DBG_vPrintf(DBG_DEVICE_SWITCH, "\n short press timer expired \n" ); break; } if((u32Button == APP_E_BUTTONS_BUTTON_SW1)|| (u32Button == APP_E_BUTTONS_BUTTON_SW2) || (u32Button == APP_E_BUTTONS_BUTTON_SW3)|| (u32Button == APP_E_BUTTONS_BUTTON_SW4) ) { bEnableSleep = FALSE; (void) u32AHI_DioInterruptStatus(); DBG_vPrintf(DBG_DEVICE_SWITCH, "\n short press timer detected \n" ); break; } } #endif return bEnableSleep; }
/** * Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, * suli_pulse_in() waits for the pin to go HIGH, starts timing, * then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. * Gives up and returns 0 if no pulse starts within a specified time out. * para - * pin: pins which you want to read the pulse. * state: type of pulse to read: either HIGH or LOW. (int) * timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long) */ uint32 suli_pulse_in(IO_T *pio, uint8 state, uint32 timeout) { #define LOOP_CYCLES 38 uint32 width = 0; uint32 numloops = 0; uint32 maxloops = timeout * 32 / (LOOP_CYCLES-5); while(suli_pin_read(pio) == state) { if (numloops++ == maxloops) return 0; } while(suli_pin_read(pio) != state) { if (numloops++ == maxloops) return 0; } while(((u32AHI_DioReadInput() >> (*pio)) & 0x1) == state) { if (numloops++ == maxloops) return 0; width++; } return (width * LOOP_CYCLES + 32) / 32; }
/**************************************************************************** * * NAME: vReadTempHumidity * * DESCRIPTION: * * Read temperature/humidity sensor. Reading is performed using a state machine * to ensure that it never blocks. * ****************************************************************************/ PRIVATE void vReadTempHumidity(void) { switch(sTempHumiditySensor.eState) { case E_STATE_READ_TEMP_HUMID_IDLE: vHTSstartReadHumidity(); sTempHumiditySensor.eState = E_STATE_READ_HUMID_RUNNING; break; case E_STATE_READ_HUMID_RUNNING: if ((u32AHI_DioReadInput() & HTS_DATA_DIO_BIT_MASK) == 0) { sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_COMPLETE; } break; case E_STATE_READ_TEMP_HUMID_COMPLETE: sTempHumiditySensor.u16HumidReading = u16HTSreadHumidityResult(); sTempHumiditySensor.eState = E_STATE_READ_TEMP_START; break; case E_STATE_READ_TEMP_START: vHTSstartReadTemp(); sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_RUNNING; break; case E_STATE_READ_TEMP_HUMID_RUNNING: if ((u32AHI_DioReadInput() & HTS_DATA_DIO_BIT_MASK) == 0) { sTempHumiditySensor.eState = E_STATE_READ_TEMP_COMPLETE; } break; case E_STATE_READ_TEMP_COMPLETE: sTempHumiditySensor.u16TempReading = u16HTSreadTempResult(); sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_READY; break; case E_STATE_READ_TEMP_HUMID_READY: break; default: break; } }
int32_t uart_write(int32_t dev, char *buf, size_t n) { size_t i; for(i=0; i<n; i++, buf++) { while (!BUFREADY(UART) || (u32AHI_DioReadInput() & CTS)) ; vAHI_UartWriteData(dev, *buf); } return i; }
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds * to 3 minutes in length, but must be called at least a few dozen microseconds * before the start of the pulse. */ extern uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) { //TODO Correct clock cycle constants. // cache the port and bit of the pin in order to speed up the // pulse width measuring loop and achieve finer resolution. calling // digitalRead() instead yields much coarser resolution. //PinDescription p = g_APinDescription[pin]; uint32_t width = 0; // keep initialization out of time critical area // convert the timeout from microseconds to a number of times through // the initial loop; it takes 22 clock cycles per iteration. uint32_t numloops = 0; uint32_t maxloops = microsecondsToClockCycles(timeout) / 22; // wait for any previous pulse to end while ( ((u32AHI_DioReadInput()>>pin) & 0x1) == state) if (numloops++ == maxloops) return 0; // wait for the pulse to start while ( ((u32AHI_DioReadInput()>>pin) & 0x1) != state) if (numloops++ == maxloops) return 0; // wait for the pulse to stop while ( ((u32AHI_DioReadInput()>>pin) & 0x1) == state) { if (numloops++ == maxloops) return 0; width++; } // convert the reading to microseconds. The loop has been determined // to be 52 clock cycles long and have about 16 clocks between the edge // and the start of the loop. There will be some error introduced by // the interrupt handlers. return clockCyclesToMicroseconds(width * 52 + 16); }
void vEH_ButtonCallback(uint32 u32DeviceId, uint32 u32ItemBitmap) { #ifdef DK4 /* clear pending DIO changed bits by reading register */ (void) u32AHI_DioInterruptStatus(); uint32 u32IntStatus = u32AHI_DioReadInput(); /* disable edge detection until handling of button press complete */ vAHI_DioInterruptEnable(0, APP_BUTTONS_DIO_MASK); DBG_vPrintf(DBG_DEVICE_SWITCH, "\nvEH_ButtonCallback u32DeviceId = 0x%8x, u32ItemBitmap = 0x%8x u32IntStatus = 0x%8x" , u32DeviceId,u32ItemBitmap, u32IntStatus); /* Update flags indicating that Button press detected */ bButtonPressed = TRUE; u32Button = u32ItemBitmap; #endif }
/**************************************************************************** * * NAME: APP_vButtonsInitialise * * DESCRIPTION: * Initialises buttons * * RETURNS: * bool_t Returns TRUE if any button was pressed * ****************************************************************************/ PUBLIC bool_t APP_bButtonInitialise(void) { /* Set DIO lines to inputs with buttons connected */ vAHI_DioSetDirection(APP_BUTTONS_DIO_MASK, 0); /* Turn on pull-ups for DIO lines with buttons connected */ vAHI_DioSetPullup(APP_BUTTONS_DIO_MASK, 0); /* Set the edge detection for falling edges */ vAHI_DioInterruptEdge(0, APP_BUTTONS_DIO_MASK); /* Enable interrupts to occur on selected edge */ vAHI_DioInterruptEnable(APP_BUTTONS_DIO_MASK, 0); uint32 u32Buttons = u32AHI_DioReadInput() & APP_BUTTONS_DIO_MASK; if (u32Buttons != APP_BUTTONS_DIO_MASK) { return TRUE; } return FALSE; }
/**************************************************************************** * NAME: bButtonInitialize * * DESCRIPTION: * Button Initialization function, enables interrupts only on DK4 * * RETURNS: * void * ****************************************************************************/ bool bButtonInitialize(void) { #ifdef DK4 /* Set DIO lines to inputs with buttons connected */ vAHI_DioSetDirection(APP_BUTTONS_DIO_MASK, 0); vAHI_SysCtrlRegisterCallback(&vEH_ButtonCallback); /* Turn on pull-ups for DIO lines with buttons connected */ vAHI_DioSetPullup(APP_BUTTONS_DIO_MASK, 0); /* Set the edge detection for falling edges */ vAHI_DioInterruptEdge(0, APP_BUTTONS_DIO_MASK); /* Enable interrupts to occur on selected edge */ vAHI_DioInterruptEnable(APP_BUTTONS_DIO_MASK, 0); uint32 u32ButtonStatus = u32AHI_DioReadInput() ; if ((u32ButtonStatus & 0x100) == 0x00) { u32Button = APP_BUTTONS_BUTTON_1; bButtonPressed = TRUE; /* disable edge detection until handling of button press complete */ vAHI_DioInterruptEnable(0, APP_BUTTONS_DIO_MASK); return TRUE; } return FALSE; #else /* Disable internal pull-up */ vAHI_DioSetPullup ((uint32) 0x0, APP_BUTTONS_DIO_MASK); /* Make button DIO an input */ vAHI_DioSetDirection ((uint32) APP_BUTTONS_DIO_MASK, 0x0); return TRUE; #endif }
/**************************************************************************** * * NAME: bModuleHasAntennaDiversity * * DESCRIPTION: * Checks if the antenna diversity select jumper is fitted or not. * * RETURNS: * TRUE if antenna diversity jumper fitted. * ****************************************************************************/ PUBLIC bool bModuleHasAntennaDiversity(void) { /* Invert due to internal pullup / external pulldown to enable */ return (!(u32AHI_DioReadInput() & MODULE_FEATURE_SELECT_PIN_ANTENNA_DIVERSITY)); }
/**************************************************************************** * * NAME: bModuleIsHighPower * * DESCRIPTION: * Checks if the high power module select jumper is fitted or not. * * RETURNS: * TRUE if high power jumper fitted. * ****************************************************************************/ PUBLIC bool bModuleIsHighPower(void) { /* Invert due to internal pullup / external pulldown to enable */ return (!(u32AHI_DioReadInput() & MODULE_FEATURE_SELECT_PIN_HIGH_POWER)); }
/* * read IO state * - pio: IO device pointer * return HIGH or LOW */ int16 suli_pin_read(IO_T *pio) { return ((u32AHI_DioReadInput() >> (*pio)) & 0x1); }
bool bHandleButtonPress(bool bIsPowerUp) { uint32 u32StartTime; bool bEnableSleep = TRUE; /* ISR will set bButtonPressed to true */ if(bButtonPressed) { switch(u32Button) { /* Switch 1: mapping * 1. Commissioning Commands * 2. On Or Toggle Command * 3. De commissioning command * 4. Factory Reset * */ case APP_E_BUTTONS_BUTTON_SW1: bEnableSleep = bProcessGPDEvent(); break; case APP_E_BUTTONS_BUTTON_SW2: DBG_vPrintf(DBG_DEVICE_SWITCH, "\n APP_E_BUTTONS_BUTTON_SW2 \n"); /* Button 2 mapping : * Off command */ HandleGPDStateMachine(E_GPD_OFF_CMD); bButtonPressed = FALSE; break; case APP_E_BUTTONS_BUTTON_SW3: DBG_vPrintf(DBG_DEVICE_SWITCH, "\n APP_E_BUTTONS_BUTTON_SW3 \n"); /* Button 3 mapping : * Move up with on/off( button press) , * Level control stop (button release ) */ { HandleGPDStateMachine(E_GPD_MOVE_UP_CMD); bButtonPressed = FALSE; uint32 u32DIOVal2 = u32AHI_DioReadInput(); uint32 u32DIOVal = u32AHI_DioReadInput(); /* start timer to account next button press interval */ u32StartTime = u32EH_StartTimer(); while(u32DIOVal2 == u32DIOVal) { if(bEH_IsTimeElapsedInMicrosSec((uint32)( APP_TIME_MS(NEXT_MOVE_CMD_DURATION)) ,u32StartTime)) { u32StartTime = u32EH_StartTimer(); u32DIOVal = u32AHI_DioReadInput(); } } DBG_vPrintf(DBG_DEVICE_SWITCH, "\n APP_E_BUTTONS_BUTTON_SW3 Release u32DIOVal = 0x%8x \n",u32DIOVal); HandleGPDStateMachine(E_GPD_LEVEL_CONTROL_STOP); } break; case APP_E_BUTTONS_BUTTON_SW4: DBG_vPrintf(DBG_DEVICE_SWITCH, "\n APP_E_BUTTONS_BUTTON_SW4 \n"); /* Button 4 mapping : * Move Down with on / off( button press), * Level control stop(button release ) */ { HandleGPDStateMachine(E_GPD_MOVE_DOWN_CMD); bButtonPressed = FALSE; uint32 u32DIOVal = u32AHI_DioReadInput(); uint32 u32DIOVal2 = u32AHI_DioReadInput(); u32StartTime = u32EH_StartTimer(); while(u32DIOVal2 == u32DIOVal) { if(bEH_IsTimeElapsedInMicrosSec((uint32)( APP_TIME_MS(NEXT_MOVE_CMD_DURATION)) ,u32StartTime)) { u32StartTime = u32EH_StartTimer(); u32DIOVal = u32AHI_DioReadInput(); } } DBG_vPrintf(DBG_DEVICE_SWITCH, "\n APP_E_BUTTONS_BUTTON_SW4 Release u32DIOVal = 0x%8x\n",u32DIOVal); HandleGPDStateMachine(E_GPD_LEVEL_CONTROL_STOP); } break; case APP_BUTTONS_BUTTON_1: if(bIsPowerUp) { vClearPersistentData(TRUE); } bButtonPressed = FALSE; break; default: DBG_vPrintf(DBG_DEVICE_SWITCH, "\n Button Handler u32Button = 0x%8x", u32Button); break; } /* enable interrupts again (on DK4) */ DBG_vPrintf(DBG_DEVICE_SWITCH, "\n Enable int 1 \n" ); (void) u32AHI_DioInterruptStatus(); vAHI_DioInterruptEnable(APP_BUTTONS_DIO_MASK, 0); } return bEnableSleep; }