Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

Ram haxoring #236

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controller/inc/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
#define configTICK_RATE_HZ ( ( portTickType ) 1000 )
#define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 5 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 130 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 75 * 1024 ) )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 48 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 10 )
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0
Expand Down
24 changes: 24 additions & 0 deletions controller/inc/lowlevel_misc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to Pioneers in Engineering under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. Pioneers in Engineering licenses
// this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License

#ifndef INC_LOWLEVEL_MISC_H_
#define INC_LOWLEVEL_MISC_H_

#define CCM_DATA __attribute__((section(".ccmram")))
#define CCM_BSS __attribute__((section(".ccmbss")))

#endif // INC_LOWLEVEL_MISC_H_
18 changes: 12 additions & 6 deletions controller/inc/uart_serial_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ extern int uart_serial_packets_waiting(uart_serial_module *module);
// Returns 1 if the queue is full
extern int uart_serial_is_full(uart_serial_module *module);

extern void *uart_serial_send_data(uart_serial_module *module, uint8_t *data,
size_t len);
// This function makes a copy of the data. It is not necessary to retain it
// past the call.
extern void *uart_serial_send_data(uart_serial_module *module,
const uint8_t *data, size_t len);
extern int uart_serial_send_status(uart_serial_module *module,
void *transaction);
extern int uart_serial_send_finish(uart_serial_module *module,
void *transaction);

extern int uart_serial_send_and_finish_data(uart_serial_module *module,
uint8_t *data, size_t len);
extern uint8_t *uart_serial_receive_packet(uart_serial_module *module,
size_t *len_out, int shouldBlock);
extern uint8_t *uart_serial_receive_packet_timeout(uart_serial_module *module,
size_t *len_out, TickType_t timeout);

// The packet will be written into buf. len should contain the max size of the
// buffer on input and will be replaced with the actual number of bytes written
// on output. This function returns 0 on success.
extern int uart_serial_receive_packet(uart_serial_module *module,
uint8_t *buf, size_t *len, int shouldBlock);
extern int uart_serial_receive_packet_timeout(uart_serial_module *module,
uint8_t *buf, size_t *len, TickType_t timeout);

extern int uart_bus_logic_level(uart_serial_module *module);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,12 @@


/*
* Implementation of pvPortMalloc() and vPortFree() that relies on the
* compilers own malloc() and free() implementations.
* The simplest possible implementation of pvPortMalloc(). Note that this
* implementation does NOT allow allocated memory to be freed again.
*
* This file can only be used if the linker is configured to to generate
* a heap memory area.
*
* See heap_1.c, heap_2.c and heap_4.c for alternative implementations, and the
* See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
* memory management pages of http://www.FreeRTOS.org for more information.
*/

#include <stdlib.h>

/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
Expand All @@ -87,15 +83,52 @@ task.h is included from an application file. */

#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE

#include "inc/lowlevel_misc.h"

/* A few bytes might be lost to byte aligning the heap start address. */
#define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )

/* Allocate the memory for the heap. */
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] CCM_BSS;
static size_t xNextFreeByte = ( size_t ) 0;

/*-----------------------------------------------------------*/

void *pvPortMalloc( size_t xWantedSize )
{
void *pvReturn;
void *pvReturn = NULL;
static uint8_t *pucAlignedHeap = NULL;

/* Ensure that blocks are always aligned to the required number of bytes. */
#if portBYTE_ALIGNMENT != 1
if( xWantedSize & portBYTE_ALIGNMENT_MASK )
{
/* Byte alignment required. */
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
#endif

vTaskSuspendAll();
{
if( pucAlignedHeap == NULL )
{
/* Ensure the heap starts on a correctly aligned boundary. */
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
}

/* Check there is enough room left for the allocation. */
if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) )/* Check for overflow. */
{
/* Return the next free byte then increment the index past this
block. */
pvReturn = pucAlignedHeap + xNextFreeByte;
xNextFreeByte += xWantedSize;
}

// Our malloc already calls FreeRTOS to synchronize properly
pvReturn = malloc( xWantedSize );
traceMALLOC( pvReturn, xWantedSize );
traceMALLOC( pvReturn, xWantedSize );
}
( void ) xTaskResumeAll();

#if( configUSE_MALLOC_FAILED_HOOK == 1 )
{
Expand All @@ -113,11 +146,26 @@ void *pvReturn;

void vPortFree( void *pv )
{
if( pv )
{
free( pv );
traceFREE( pv, 0 );
}
/* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
heap_4.c for alternative implementations, and the memory management pages of
http://www.FreeRTOS.org for more information. */
( void ) pv;

/* Force an assert as it is invalid to call this function. */
configASSERT( pv == NULL );
}
/*-----------------------------------------------------------*/

void vPortInitialiseBlocks( void )
{
/* Only required when static memory is not cleared. */
xNextFreeByte = ( size_t ) 0;
}
/*-----------------------------------------------------------*/

size_t xPortGetFreeHeapSize( void )
{
return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );
}


Expand Down
8 changes: 4 additions & 4 deletions controller/src/i2c_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static portTASK_FUNCTION_PROTO(i2c_master_task, pvParameters) {

i2c_master_module *i2c_master_init_module(void *periph_base) {
i2c_master_module_private *module_obj =
pvPortMalloc(sizeof(i2c_master_module_private));
malloc(sizeof(i2c_master_module_private));
I2C_TypeDef *i2c = (I2C_TypeDef *)periph_base;

// TODO(rqou): Figure out magic to make pin init and clock init work
Expand Down Expand Up @@ -93,14 +93,14 @@ i2c_master_module *i2c_master_init_module(void *periph_base) {
module_obj->currentTxn = NULL;

// Start the task
xTaskCreate(i2c_master_task, "I2C", 256, module_obj, tskIDLE_PRIORITY, NULL);
xTaskCreate(i2c_master_task, "I2C", 128, module_obj, tskIDLE_PRIORITY, NULL);

return (i2c_master_module *)module_obj;
}

void *i2c_issue_transaction(i2c_master_module *module, uint8_t addr,
uint8_t *data_out, size_t len_out, uint8_t *data_in, size_t len_in) {
i2c_transaction_obj *obj = pvPortMalloc(sizeof(i2c_transaction_obj));
i2c_transaction_obj *obj = malloc(sizeof(i2c_transaction_obj));

obj->data_out = data_out;
obj->data_in = data_in;
Expand Down Expand Up @@ -239,7 +239,7 @@ int i2c_transaction_finish(i2c_master_module *module,
return 0;
}

vPortFree(transaction);
free(transaction);

return 1;
}
2 changes: 1 addition & 1 deletion controller/src/led_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ static portTASK_FUNCTION_PROTO(led_driver_task, pvParameters) {
void led_driver_init(void) {
led_driver_init_hw();

xTaskCreate(led_driver_task, "LEDs", 256, NULL, tskIDLE_PRIORITY, NULL);
xTaskCreate(led_driver_task, "LEDs", 128, NULL, tskIDLE_PRIORITY, NULL);
}

void led_driver_set_mode(uint8_t mode) {
Expand Down
15 changes: 7 additions & 8 deletions controller/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ static portTASK_FUNCTION_PROTO(radioTask, pvParameters) {
int started_angelic = 0;

while (1) {
uint8_t *buf;
// TODO(rqou): Size is hardcoded. Yuck.
uint8_t buf[256];
int ret;
size_t len;

if (!got_a_packet && should_harass) {
Expand Down Expand Up @@ -181,21 +183,20 @@ static portTASK_FUNCTION_PROTO(radioTask, pvParameters) {
}
got_a_packet = 0;

buf = uart_serial_receive_packet(radio_driver, &len, 0);
if (!buf) {
len = sizeof(buf);
ret = uart_serial_receive_packet(radio_driver, buf, &len, 0);
if (ret) {
// TODO(rqou): Proper timer, why the f*ck do I need to copy this here?
vTaskDelay(20 / portTICK_RATE_MS);
continue;
}
xbee_api_packet *packetIn = (xbee_api_packet *)buf;
if (!xbee_verify_checksum(packetIn)) {
vPortFree(buf);
// TODO(rqou): Proper timer, why the f*ck do I need to copy this here?
vTaskDelay(20 / portTICK_RATE_MS);
continue;
}
if (packetIn->payload.xbee_api_type != XBEE_API_TYPE_RX64) {
vPortFree(buf);
// TODO(rqou): Proper timer, why the f*ck do I need to copy this here?
vTaskDelay(20 / portTICK_RATE_MS);
continue;
Expand Down Expand Up @@ -224,7 +225,7 @@ static portTASK_FUNCTION_PROTO(radioTask, pvParameters) {
(tenshi_bulk_start *)(packetIn->payload.rx64.data);
// TODO(rqou): What happens if I already have one?
// TODO(rqou): Stream ID?
code_buffer = pvPortMalloc(bulk_start->length);
code_buffer = malloc(bulk_start->length);
code_received_to = 0;
code_buffer_len = code_received_len = bulk_start->length;
got_a_packet = 1;
Expand Down Expand Up @@ -287,8 +288,6 @@ static portTASK_FUNCTION_PROTO(radioTask, pvParameters) {
break;
}

vPortFree(buf);

// TODO(rqou): Proper timer
vTaskDelay(20 / portTICK_RATE_MS);
}
Expand Down
29 changes: 16 additions & 13 deletions controller/src/radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License

#include <stdio.h>
#include <string.h>

#include <ndl3.h>
Expand Down Expand Up @@ -102,7 +103,7 @@ BaseType_t radioInit() {

radioQueue = xQueueCreate(100, sizeof(RadioMessage));

return xTaskCreate(radioNewTask, "Radio", 1024, NULL, tskIDLE_PRIORITY,
return xTaskCreate(radioNewTask, "Radio", 512, NULL, tskIDLE_PRIORITY,
NULL);
}
void radioPushUbjson(const char *ubjson, size_t len) {
Expand Down Expand Up @@ -166,13 +167,13 @@ void radio_send_xbee(uint8_t *data, size_t len) {
}
void *ndAlloc(NDL3_size size, void * userdata) {
(void) userdata;
void *ret = pvPortMalloc(size);
void *ret = malloc(size);
while (!ret) {}
return ret;
}
void ndFree(void * to_free, void * userdata) {
(void) userdata;
vPortFree(to_free);
free(to_free);
}


Expand All @@ -189,7 +190,9 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
char * recvMsg = NULL;

const uint8_t prefixLen = 1;
xbee_api_packet *recXbeePacket;
// TODO(rqou): Ugly
uint8_t recXbeePacket_buf[256];
xbee_api_packet *recXbeePacket = (xbee_api_packet *)(recXbeePacket_buf);
xbee_rx64_header *recXbeeHeader;
uint8_t buffer[NDL3_PACKET_SIZE];
NDL3_size popSize = 0;
Expand All @@ -208,7 +211,7 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
// Trust this to free recvMsg
runtimeRecieveUbjson(recvMsg, recvSize);
} else {
vPortFree(recvMsg);
free(recvMsg);
}

recvMsg = NULL;
Expand All @@ -219,15 +222,15 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
// Trust this to free recvMsg
runtimeRecieveUbjson(recvMsg, recvSize);
} else {
vPortFree(recvMsg);
free(recvMsg);
}

recvMsg = NULL;
recvSize = 0;
NDL3_recv(target, NDL3_STRING_PORT, (void **) &recvMsg, &recvSize);
// Do stuff with recieved message

vPortFree(recvMsg);
free(recvMsg);

recvMsg = NULL;
recvSize = 0;
Expand All @@ -237,7 +240,7 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
// Trust this to free recvMsg
runtimeRecieveCode(recvMsg, recvSize);
} else {
vPortFree(recvMsg);
free(recvMsg);
}

recvMsg = NULL;
Expand All @@ -248,9 +251,9 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
// Trust this to free recvMsg
// TODO(cduck): Do something
printf("Got config data\n");
vPortFree(recvMsg);
free(recvMsg);
} else {
vPortFree(recvMsg);
free(recvMsg);
}


Expand All @@ -272,9 +275,10 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
printf("Radio error b%d\n", err);
}

recXbeePacket = (xbee_api_packet*)uart_serial_receive_packet(radio_driver,
uartRecvSize = sizeof(recXbeePacket_buf);
int ret = uart_serial_receive_packet(radio_driver, recXbeePacket,
&uartRecvSize, 0);
if (recXbeePacket) {
if (!ret) {
recXbeePacket->length = __REV16(recXbeePacket->length);
recXbeeHeader = (xbee_rx64_header*)&(recXbeePacket->payload);
if (recXbeeHeader->xbee_api_type == XBEE_API_TYPE_RX64) {
Expand All @@ -284,7 +288,6 @@ static portTASK_FUNCTION_PROTO(radioNewTask, pvParameters) {
recXbeePacket->length-sizeof(xbee_rx64_header)-prefixLen);
}
}
vPortFree(recXbeePacket);
}

err = NDL3_pop_error(target);
Expand Down
Loading