/*----------------------------------------------------------------------------*/ int main(void) { const struct Pin led = pinInit(LED_PIN); pinOutput(led, false); struct Interface * const flash = init(Flash, 0); assert(flash); size_t flashSize, pageSize; enum Result res; pinSet(led); if ((res = ifGetParam(flash, IF_SIZE, &flashSize)) == E_OK) pinReset(led); assert(res == E_OK); pinSet(led); if ((res = ifGetParam(flash, IF_FLASH_PAGE_SIZE, &pageSize)) == E_OK) pinReset(led); assert(res == E_OK); uint8_t * const buffer = malloc(pageSize); for (size_t i = 0; i < pageSize; ++i) buffer[i] = i; /* Test sector erase */ const size_t address = findNearestSector(); assert(address < flashSize); pinSet(led); if ((res = ifSetParam(flash, IF_FLASH_ERASE_SECTOR, &address)) == E_OK) pinReset(led); assert(res == E_OK); pinSet(led); if ((res = program(flash, buffer, pageSize, address)) == E_OK) pinReset(led); assert(res == E_OK); pinSet(led); if ((res = verify(flash, buffer, pageSize, address)) == E_OK) pinReset(led); assert(res == E_OK); /* Page erase is not available on some parts */ while (1); return 0; }
static void deviceCallback(void *argument) { struct DeviceDriver *device = argument; enum Result status; if ((status = ifGetParam(device->interface, IF_STATUS, 0)) != E_OK) return; switch (device->state) { case DEVICE_PH1_ADDRESS: device->state = DEVICE_PH1_DATA; ifRead(device->interface, device->buffer, sizeof(device->buffer) - MEMORY_ADDRESS_SIZE); break; case DEVICE_PH1_DATA: for (size_t i = 0; i < sizeof(device->buffer) - MEMORY_ADDRESS_SIZE; ++i) { if (device->buffer[i] != (uint8_t)(device->change ? ~i : i)) return; } /* Falls through */ case DEVICE_PH2_DATA: device->state = DEVICE_IDLE; pinReset(device->led); break; default: break; } }
/*----------------------------------------------------------------------------*/ int main(void) { const struct Pin led = pinInit(LED_PIN); pinOutput(led, false); struct Interface * const adc = init(AdcOneShot, &adcConfig); assert(adc); struct Timer * const timer = init(GpTimer, &timerConfig); assert(timer); timerSetOverflow(timer, 1000); bool event = false; timerSetCallback(timer, onTimerOverflow, &event); timerEnable(timer); while (1) { while (!event) barrier(); event = false; pinSet(led); uint16_t voltage; const size_t bytesRead = ifRead(adc, &voltage, sizeof(voltage)); assert(bytesRead == sizeof(voltage)); (void)bytesRead; /* Suppress warning */ pinReset(led); } return 0; }
/*----------------------------------------------------------------------------*/ static enum Result interfaceSetParam(void *object, enum IfParameter parameter, const void *data) { struct InterfaceWrapper * const interface = object; switch (parameter) { case IF_RELEASE: pinReset(interface->rx); pinReset(interface->tx); break; default: break; } return ifSetParam(interface->pipe, parameter, data); }
/*----------------------------------------------------------------------------*/ int main(void) { const uint32_t maxPeriod = captureUnitConfig.frequency / pwmUnitConfig.frequency + 1; const uint32_t minPeriod = captureUnitConfig.frequency / pwmUnitConfig.frequency - 1; setupClock(); const struct Pin led = pinInit(LED_PIN); pinOutput(led, false); struct GpTimerPwmUnit * const pwmUnit = init(GpTimerPwmUnit, &pwmUnitConfig); assert(pwmUnit); struct Pwm * const pwm = gpTimerPwmCreate(pwmUnit, OUTPUT_PIN); assert(pwm); pwmSetEdges(pwm, 0, pwmGetResolution(pwm) / 2); struct GpTimerCaptureUnit * const captureUnit = init(GpTimerCaptureUnit, &captureUnitConfig); assert(pwmUnit); struct Capture * const capture = gpTimerCaptureCreate(captureUnit, INPUT_PIN, PIN_RISING, PIN_PULLDOWN); assert(capture); uint32_t previousTime = 0; bool event = false; captureSetCallback(capture, onCaptureEvent, &event); captureEnable(capture); pwmEnable(pwm); while (1) { while (!event) barrier(); event = false; const uint32_t currentTime = captureGetValue(capture); const uint32_t period = currentTime - previousTime; if (period >= minPeriod && period <= maxPeriod) pinReset(led); else pinSet(led); previousTime = currentTime; } return 0; }
/*----------------------------------------------------------------------------*/ int main(void) { static const uint32_t address = 0; uint8_t buffer[384]; for (size_t i = 0; i < sizeof(buffer); ++i) buffer[i] = i; const struct Pin led = pinInit(LED_PIN); pinOutput(led, false); struct Interface * const eeprom = init(Eeprom, 0); assert(eeprom); uint32_t size; enum Result res; pinSet(led); if ((res = ifGetParam(eeprom, IF_SIZE, &size)) == E_OK) pinReset(led); assert(res == E_OK); assert(address < size); pinSet(led); if ((res = program(eeprom, buffer, sizeof(buffer), address)) == E_OK) pinReset(led); assert(res == E_OK); pinSet(led); if ((res = verify(eeprom, buffer, sizeof(buffer), address)) == E_OK) pinReset(led); assert(res == E_OK); while (1); return 0; }
/*----------------------------------------------------------------------------*/ int main(void) { setupClock(); const struct Pin led = pinInit(LED_PIN); pinOutput(led, false); struct Interface * const adc = init(AdcDma, &adcConfig); assert(adc); ifSetCallback(adc, onConversionCompleted, adc); struct Timer * const conversionTimer = init(GpTimer, &timerConfig); assert(conversionTimer); /* * The overflow frequency of the timer should be two times higher * than that of the hardware events for ADC. */ timerSetOverflow(conversionTimer, timerConfig.frequency / (ADC_RATE * 2)); unsigned int iteration = 0; size_t count; /* Enqueue buffers */ while (ifGetParam(adc, IF_AVAILABLE, &count) == E_OK && count > 0) { const size_t index = iteration++ % BUFFER_COUNT; ifRead(adc, buffers[index], sizeof(buffers[index])); } /* Start conversion */ timerEnable(conversionTimer); while (1) { while (!event) barrier(); event = false; pinSet(led); while (ifGetParam(adc, IF_AVAILABLE, &count) == E_OK && count > 0) { const size_t index = iteration++ % BUFFER_COUNT; ifRead(adc, buffers[index], sizeof(buffers[index])); } pinReset(led); } return 0; }
/*----------------------------------------------------------------------------*/ int main(void) { setupClock(); const struct Pin led = pinInit(LED_PIN); pinOutput(led, true); struct GpTimerPwmUnit * const pwmUnit = init(GpTimerPwmUnit, &pwmUnitConfig); assert(pwmUnit); struct Pwm * const pwmOutput = gpTimerPwmCreate(pwmUnit, OUTPUT_PIN); assert(pwmOutput); pwmSetDuration(pwmOutput, pwmGetResolution(pwmOutput) / 2); struct Timer * const counter = init(GpTimerCounter, &counterConfig); assert(counter); struct Timer * const timer = init(GpTimer, &timerConfig); assert(timer); timerSetOverflow(timer, 1000); bool event = false; timerSetCallback(timer, onTimerOverflow, &event); timerEnable(counter); pwmEnable(pwmOutput); timerEnable(timer); while (1) { while (!event) barrier(); event = false; const uint32_t period = timerGetValue(counter); timerSetValue(counter, 0); if (period >= 999 && period <= 1001) pinReset(led); else pinSet(led); } return 0; }
/*----------------------------------------------------------------------------*/ static void deviceWrite(struct DeviceDriver *device) { deviceConfigIO(device, false); device->change = !device->change; memcpy(device->buffer, &device->localAddress, MEMORY_ADDRESS_SIZE); for (size_t i = 0; i < sizeof(device->buffer) - MEMORY_ADDRESS_SIZE; ++i) device->buffer[i + MEMORY_ADDRESS_SIZE] = device->change ? ~i : i; pinSet(device->led); device->state = DEVICE_PH2_DATA; #ifdef TEST_ZEROCOPY ifWrite(device->interface, device->buffer, sizeof(device->buffer)); #else const size_t bytesWritten = ifWrite(device->interface, device->buffer, sizeof(device->buffer)); if (bytesWritten == sizeof(device->buffer)) pinReset(device->led); #endif }
/*----------------------------------------------------------------------------*/ static void deviceRead(struct DeviceDriver *device) { deviceConfigIO(device, true); pinSet(device->led); device->state = DEVICE_PH1_ADDRESS; #ifdef TEST_ZEROCOPY ifWrite(device->interface, &device->localAddress, MEMORY_ADDRESS_SIZE); #else const size_t bytesWritten = ifWrite(device->interface, &device->localAddress, MEMORY_ADDRESS_SIZE); if (bytesWritten == MEMORY_ADDRESS_SIZE) { const size_t bytesRead = ifRead(device->interface, device->buffer, sizeof(device->buffer) - MEMORY_ADDRESS_SIZE); if (bytesRead == sizeof(device->buffer) - MEMORY_ADDRESS_SIZE) pinReset(device->led); } #endif }
static inline void hubTask() { color_t pixel, pixel2; if (state == ST_PERIOD0) { //Period 0 - Shift row data pinReset(hub_time); //__HAL_TIM_SetAutoreload(&hubtim, period0 - 1); HUB_TIMER->ARR = period0 - 1; if (colorBit == HUB_COLOR_BIT0) { if (row == 0) { //Begin of Frame setBrightness(); if (hubBrightness == 0) { //Display turn off //__HAL_TIM_SetAutoreload(&hubtim, FRAME_PERIOD - 1); HUB_TIMER->ARR = FRAME_PERIOD - 1; pinSet(hub_time); return; } if (hubNeedRedraw) { swapPages(); setOrientSwap(); } bufLine = &DISPLAY[screen0]; bufLine2 = &bufLine[HUB_ROWS * screen_dy]; } outputRow(); } int red, green, blue; int red2, green2, blue2; buf = bufLine; buf2 = bufLine2; for (int x = 0; x < SCREEN_W; x++) { pixel = *buf; red = hubLUT[pixel.R]; green = hubLUT[pixel.G]; blue = hubLUT[pixel.B]; pixel2 = *buf2; red2 = hubLUT[pixel2.R]; green2 = hubLUT[pixel2.G]; blue2 = hubLUT[pixel2.B]; pinReset(hub_clk); pinWrite(hub_r1, red & colorBit); pinWrite(hub_g1, green & colorBit); pinWrite(hub_b1, blue & colorBit); pinWrite(hub_r2, red2 & colorBit); pinWrite(hub_g2, green2 & colorBit); pinWrite(hub_b2, blue2 & colorBit); buf += screen_dx; buf2 += screen_dx; pinSet(hub_clk); } period1n = period1N[colorI]; prescaler2n = prescaler2N[colorI]; colorBit >>= 1; colorI--; if (colorBit == 0) { colorBit = HUB_COLOR_BIT0; colorI = HUB_COLOR_I0; //Change Row row = (row + 1) & HUB_ROWS_MASK; bufLine += screen_dy; bufLine2 += screen_dy; } pinSet(hub_lat); state = ST_PERIOD1; pinReset(hub_lat); } else if (state == ST_PERIOD1) {