Skip to content

Instantly share code, notes, and snippets.

void CustomMallocInit(HEAP_INFO_t *pHeapInfo)
{
pHeapInfo->alloc_dll = &(pHeapInfo->dummy1);
pHeapInfo->alloc_dll->next = NULL;
pHeapInfo->alloc_dll->prev = NULL;
pHeapInfo->avail_dll = &(pHeapInfo->dummy2);
pHeapInfo->avail_dll->prev = NULL;
pHeapInfo->avail_dll->next = pHeapInfo->pHeap;
pHeapInfo->avail_dll->next->blkSize = pHeapInfo->heapSz - sizeof(BD_t);
pHeapInfo->avail_dll->next->next = NULL;
/*
* Initializes the heap. It internally created a BD and places at start of the
* heap. The BD is added to avail_dll.
* @param pHeapInfo: Pointer to the current heap info.
**/
void CustomMallocInit(HEAP_INFO_t *pHeapInfo);
/*
* Mimics the malloc function(i.e provides the requested memory). Returns NULL in
* case is it not possible to allocate the requested memory.
typedef struct BD_t
{
int blkSize;
struct BD_t *prev;
struct BD_t *next;
} BD_t;
/*
* premptive_scheduler.c
*
* Created on: Apr 26, 2020
* Author: Sudeep Chandrasekaran
*/
#include <stdint.h>
#include <stdbool.h>
#include "premptive_scheduler.h"
void portable_delay_cycles(unsigned long n)
{
while (n--)
{
asm volatile ("");
}
}
volatile void Task0()
{
__attribute__((naked)) void LaunchScheduler(void)
{
/// R0 contains the address of currentPt
__asm("LDR R0, =pCurntTcb");
/// R2 contains the address in currentPt(value of currentPt)
__asm("LDR R2, [R0]");
/// Load the SP reg with the stacked SP value
__asm("LDR R4, [R2]");
__asm("MOV SP, R4");
/// Pop registers R8-R11(user saved context)
void OsInitThreadStack()
{
/// Enter critical section
/// Disable interrupts
__asm("CPSID I");
/// Make the TCB linked list circular
tcbs[0].nextPt = &tcbs[1];
tcbs[1].nextPt = &tcbs[0];
/// Setup stack for task0
__attribute__((naked)) void SysTick_Handler(void)
{
/// STEP 1 - SAVE THE CURRENT TASK CONTEXT
/// At this point the processor has already pushed PSR, PC, LR, R12, R3, R2, R1 and R0
/// onto the stack. We need to push the rest(i.e R4, R5, R6, R7, R8, R9, R10 & R11) to save the
/// context of the current task.
/// Disable interrupts
__asm("CPSID I");
/// Push registers R4 to R7
@dheeptuck
dheeptuck / bare_blink.c
Created April 17, 2020 15:28
Bare metal LED blink
void PumpTask()
{
while(1)
{
if(GetTimeSinceLastToggle() ≥ 30)
{
TurnPumpOn();
}
}
}