Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:53

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2010, 2013 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #define NDEBUG
0029 
0030 #include <bsp/bestcomm.h>
0031 
0032 #include <string.h>
0033 
0034 #include <bsp/mpc5200.h>
0035 
0036 static void bestcomm_irq_handler(void *arg)
0037 {
0038   bestcomm_irq *self = arg;
0039 
0040   bestcomm_irq_clear(self);
0041   bestcomm_irq_wakeup_event_task(self);
0042 }
0043 
0044 void bestcomm_irq_create(bestcomm_irq *self, int task_index)
0045 {
0046   assert(task_index >= 0 && task_index <= 15);
0047 
0048   self->task_index = task_index;
0049   self->event_task_id = rtems_task_self();
0050   bestcomm_glue_irq_install(task_index, bestcomm_irq_handler, self);
0051 }
0052 
0053 void bestcomm_irq_destroy(const bestcomm_irq *self)
0054 {
0055   bestcomm_glue_irq_install(self->task_index, NULL, NULL);
0056 }
0057 
0058 void bestcomm_task_create(bestcomm_task *self, TaskId task_index)
0059 {
0060   self->task_control_register = &mpc5200.sdma.tcr[task_index];
0061   self->variable_table = BESTCOMM_TASK_ENTRY_TABLE[task_index].var_table;
0062   self->task_index = task_index;
0063   self->tdt_begin = NULL;
0064   self->tdt_opcode_count = 0;
0065   bestcomm_task_stop(self);
0066   bestcomm_irq_create(&self->irq, task_index);
0067 }
0068 
0069 void bestcomm_task_create_and_load(
0070   bestcomm_task *self,
0071   TaskId task_index,
0072   const uint32_t *tdt_source_begin,
0073   size_t tdt_size
0074 )
0075 {
0076   bestcomm_task_create(self, task_index);
0077   bestcomm_task_load(self, tdt_source_begin, tdt_size);
0078 }
0079 
0080 void bestcomm_task_destroy(bestcomm_task *self)
0081 {
0082   bestcomm_task_stop(self);
0083   bestcomm_task_free_tdt(self);
0084 }
0085 
0086 void bestcomm_task_load(bestcomm_task *self, const uint32_t *tdt_source_begin, size_t tdt_size)
0087 {
0088   assert(tdt_size % 4 == 0);
0089 
0090   bestcomm_task_irq_disable(self);
0091   bestcomm_task_stop(self);
0092   bestcomm_task_irq_clear(self);
0093   bestcomm_task_irq_enable(self);
0094   bestcomm_task_free_tdt(self);
0095   bestcomm_task_clear_variables(self);
0096 
0097   self->tdt_opcode_count = tdt_size / 4;
0098 
0099   self->tdt_begin = bestcomm_malloc(tdt_size);
0100   assert(self->tdt_begin != NULL);
0101   uint32_t *tdt_last = self->tdt_begin + self->tdt_opcode_count - 1;
0102 
0103   memcpy(self->tdt_begin, tdt_source_begin, tdt_size);
0104 
0105   volatile bestcomm_task_entry *entry = bestcomm_task_get_task_entry(self);
0106   entry->tdt_begin = self->tdt_begin;
0107   entry->tdt_last = tdt_last;
0108 
0109   bestcomm_task_clear_pragmas(self);
0110   bestcomm_task_set_priority(self, 0);
0111 }
0112 
0113 void bestcomm_task_clear_variables(const bestcomm_task *self)
0114 {
0115   int i;
0116 
0117   for (i = 0; i < 32; ++i) {
0118     (*self->variable_table)[i] = 0;
0119   }
0120 }