Loading...
Searching...
No Matches
flashwrite.c
1/***********************************************************************************
2 * @file flashWrite.c *
3 * @author Matt Ricci *
4 * @addtogroup RTOS *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "FreeRTOS.h"
10#include "event_groups.h"
11#include "stdbool.h"
12
13#include "groups.h"
14
15#include "devicelist.h"
16#include "flash.h"
17#include "membuff.h"
18
19#include "state.h"
20#include "stm32f439xx.h"
21#include "flashwrite.h"
22
23extern EventGroupHandle_t xTaskEnableGroup;
24
25/* =============================================================================== */
34void vApplicationIdleHook() {
35
36 State *state = State_getState();
37
38 // Write if a page is available in the buffer
39 if (state->flightState >= LAUNCH && state->mem.pageReady)
40 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_FLASH);
41}
42
43/* =============================================================================== */
52void vFlashBuffer(void *argument) {
53 const TickType_t timeout = portMAX_DELAY;
54 uint32_t pageAddr = 0;
55
56 State *state = State_getState();
57
58 Flash_t *flash = DeviceList_getDeviceHandle(DEVICE_FLASH).device;
59 uint8_t outBuff[flash->pageSize];
60
61 for (;;) {
62 // Wait for write flag to be ready, clear flag on exit
63 EventBits_t uxBits = xEventGroupWaitBits(xTaskEnableGroup, GROUP_TASK_ENABLE_FLASH, pdTRUE, pdFALSE, timeout);
64 if (uxBits & GROUP_TASK_ENABLE_FLASH) {
65 taskENTER_CRITICAL();
66 // Flush data to output buffer
67 bool success = state->mem.readPage(&state->mem, outBuff);
68 if (success) {
69 // Write data to flash memory if within bounds
70 if (pageAddr / flash->pageSize < flash->pageCount - 1)
71 flash->writePage(flash, pageAddr, outBuff);
72
73 // Increment page address
74 pageAddr += 0x100;
75 }
76 taskEXIT_CRITICAL();
77 }
78 }
79}
80
Defines the API for Flash memory storage.
int pageSize
Number of bytes per page.
Definition flash.h:22
long pageCount
Total number of pages.
Definition flash.h:23
void(* writePage)(struct Flash *, uint32_t, uint8_t *)
Write page method.
Definition flash.h:26
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
State * State_getState()
Definition state.c:69
@ LAUNCH
Body reference Z-axis acceleration above threshold.
Definition state.h:26
State variable struct.
Definition state.h:36