/* * This is a dummy function, used as demonstration */ SFPResult dummyFunction(SFPFunction *msg) { /* Check for arg count */ if (SFPFunction_getArgumentCount(msg) != 1) return SFP_ERR_ARG_COUNT; /* Check for arg type */ if (SFPFunction_getArgumentType(msg, 0) != SFP_ARG_INT) return SFP_ERR_ARG_TYPE; /* Get the argument */ uint32_t dummyData = SFPFunction_getArgument_int32(msg, 0); /* Do some stupid things */ LPC_GPIO->DIR[1] = 1 << 14; if (dummyData) LPC_GPIO->SET[1] = 1 << 14; else LPC_GPIO->CLR[1] = 1 << 14; /* Start a new frame */ SFPFunction *outFunc = SFPFunction_new(); if (outFunc == NULL) return SFP_ERR_ALLOC_FAILED; /* Then fill the frame */ SFPFunction_setType(outFunc, SFPFunction_getType(msg)); SFPFunction_setID(outFunc, 200); /* Put the Function ID */ SFPFunction_setName(outFunc, "dummy"); /* Put the function name */ SFPFunction_addArgument_int32(outFunc, dummyData); /* Put the values you want to send back */ /* Send it */ SFPFunction_send(outFunc, &stream); /* End finalize */ SFPFunction_delete(outFunc); return SFP_OK; }
static inline void GPIO_SEND_INT(uint8_t intID, uint32_t intStatus) { SFPFunction *func = SFPFunction_new(); if (func != NULL) { SFPFunction_setType(func, LPC_INTERRUPT_FUNCTION_TYPE[intID]); SFPFunction_setID(func, UPER_FID_INTERRUPT); SFPFunction_setName(func, UPER_FNAME_INTERRUPT); SFPFunction_addArgument_int32(func, intID); SFPFunction_addArgument_int32(func, intStatus); SFPFunction_send(func, &stream); SFPFunction_delete(func); } }
SFPResult lpc_system_getDeviceInfo(SFPFunction *msg) { if (SFPFunction_getArgumentCount(msg) != 0) return SFP_ERR_ARG_COUNT; SFPFunction *func = SFPFunction_new(); if (func == NULL) return SFP_ERR_ALLOC_FAILED; SFPFunction_setType(func, SFPFunction_getType(msg)); SFPFunction_setID(func, UPER_FID_GETDEVICEINFO); SFPFunction_setName(func, UPER_FNAME_GETDEVICEINFO); SFPFunction_addArgument_int32(func, UPER_FIRMWARE_VERSION); SFPFunction_addArgument_barray(func, (uint8_t*)&GUID[0], 16); SFPFunction_addArgument_int32(func, UPER_PART_NUMBER); SFPFunction_addArgument_int32(func, UPER_BOOT_CODE_VERSION); SFPFunction_send(func, &stream); SFPFunction_delete(func); return SFP_OK; }
SFPResult hcsr04Read(SFPFunction *msg) { if (SFPFunction_getArgumentCount(msg) != 2) return SFP_ERR_ARG_COUNT; if (SFPFunction_getArgumentType(msg, 0) != SFP_ARG_INT) return SFP_ERR_ARG_TYPE; if (SFPFunction_getArgumentType(msg, 1) != SFP_ARG_INT) return SFP_ERR_ARG_TYPE; uint8_t trigger = SFPFunction_getArgument_int32(msg, 0); uint8_t pulse = SFPFunction_getArgument_int32(msg, 1); uint32_t startTime = 0; uint32_t distance; if (trigger >= LPC_PIN_COUNT) return SFP_ERR_ARG_VALUE; if (pulse >= LPC_PIN_COUNT) return SFP_ERR_ARG_VALUE; uint8_t trigger_port = 0; uint8_t trigger_pinNum = LPC_PIN_IDS[trigger]; if (trigger_pinNum > 23) { // if not PIO0_0 to PIO0_23 trigger_port = 1; trigger_pinNum -= 24; } uint8_t pulse_port = 0; uint8_t pulse_pinNum = LPC_PIN_IDS[pulse]; if (pulse_pinNum > 23) { // if not PIO0_0 to PIO0_23 pulse_port = 1; pulse_pinNum -= 24; } /* Set the trigger as output, LOW */ LPC_GPIO->CLR[trigger_port] = (1 << trigger_pinNum); *LPC_PIN_REGISTERS[trigger] &= ~LPC_PIN_MODE_MASK; // Remove pull-up/down resistors LPC_GPIO->DIR[trigger_port] |= (1 << trigger_pinNum); /* Set the pulse as input, no resistors */ *LPC_PIN_REGISTERS[pulse] &= ~LPC_PIN_MODE_MASK; // Remove pull-up/down resistors *LPC_PIN_REGISTERS[pulse] |= (2 << 2) & LPC_PIN_MODE_MASK;; LPC_GPIO->DIR[pulse_port] &= ~(1 << pulse_pinNum); /* Send the trigger signal for 10us*/ startTime = Time_getSystemTime_us(); LPC_GPIO->SET[trigger_port] = (1 << trigger_pinNum); while ((Time_getSystemTime_us()-startTime) <= 10); LPC_GPIO->CLR[trigger_port] = (1 << trigger_pinNum); /* Wait for an answer */ startTime = Time_getSystemTime_us(); while (!(LPC_GPIO->PIN[pulse_port] & (1 << pulse_pinNum))) { if ((Time_getSystemTime_us()-startTime) >= 25000) { // Timeout : 25ms break; } } /* Measure the pulse */ startTime = Time_getSystemTime_us(); while ((LPC_GPIO->PIN[pulse_port] & (1 << pulse_pinNum))) { if ((Time_getSystemTime_us()-startTime) >= 25000) { // Timeout : 25ms break; } } distance = (Time_getSystemTime_us() - startTime) / 58; SFPFunction *outFunc = SFPFunction_new(); if (outFunc == NULL) return SFP_ERR_ALLOC_FAILED; SFPFunction_setType(outFunc, SFPFunction_getType(msg)); SFPFunction_setID(outFunc, UPER_FID_HCSR04); SFPFunction_setName(outFunc, UPER_FNAME_HCSR04); SFPFunction_addArgument_int32(outFunc, (distance < 25000 ? distance : 0)); SFPFunction_send(outFunc, &stream); SFPFunction_delete(outFunc); return SFP_OK; }