Same S-Function used twice, one works one doesn't - matlab

I have a simulink model and I made a simple s-function block to add to my model and it works.
The catch if if I copy or use that same s-function twice in my model, when I run it only the last s-function called will do what it's supposed to do, the other will output a 0.
#define S_FUNCTION_NAME DivByZero
#define S_FUNCTION_LEVEL 2
#define NUMBER_OF_INPUTS 1
#define NUMBER_OF_OUTPUTS 1
#include "DivByZero.h"
#include "mex.h"
#if !defined(MATLAB_MEX_FILE)
/*
* This file cannot be used directly
*/
# error This_file_can_be_used_only_during_simulation_inside_Simulink
#endif
float32 DivByZeroInput;
float32 DivByZeroOutput;
const void * inputPorts[NUMBER_OF_INPUTS];
const void * outputPorts[NUMBER_OF_OUTPUTS];
static void mdlInitializeSizes(SimStruct *S)
{
uint8 i;
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
{
return; /* Parameter mismatch reported by the Simulink engine */
}
if (!ssSetNumInputPorts(S, NUMBER_OF_INPUTS)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortDataType(S, 0, SS_SINGLE);
for (i = 0; i < NUMBER_OF_INPUTS; i++)
{
ssSetInputPortDirectFeedThrough(S, i, 1); /* Set direct feedthrough flag */
ssSetInputPortRequiredContiguous(S, i, 1); /*direct input signal access*/
}
if (!ssSetNumOutputPorts(S, NUMBER_OF_OUTPUTS)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetOutputPortDataType(S, 0, SS_SINGLE);
/* Set Sample Time */
ssSetNumSampleTimes(S, 1);
/* Specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, SS_OPTION_ALLOW_PORT_SAMPLE_TIME_IN_TRIGSS);
ssSupportsMultipleExecInstances(S, true); //set so the s-function can be used in a for each subsystem block
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
/* Inherits sample time from the driving block */
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
#define MDL_START
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
uint8 i;
/* Save Input ports */
for (i = 0; i < NUMBER_OF_INPUTS; i++)
{
inputPorts[i] = ssGetInputPortSignal(S, i);
}
/* Save Output ports */
for (i = 0; i < NUMBER_OF_OUTPUTS; i++)
{
outputPorts[i] = ssGetOutputPortRealSignal(S, i);
}
}
#endif
static void mdlOutputs(SimStruct *S, int_T tid)
{
memcpy(&DivByZeroInput, inputPorts[0], sizeof(DivByZeroInput));//gets the input port info
if (fabs(DivByZeroInput) > 0.00001f)
{
DivByZeroOutput = DivByZeroInput;
}
else
{
if (DivByZeroInput >= 0.0f)
{
DivByZeroOutput = 0.00001f;
}
else
{
DivByZeroOutput= -0.00001f;
}
}
memcpy(outputPorts[0], &DivByZeroOutput, sizeof(DivByZeroOutput));//sets the output port
}
static void mdlTerminate(SimStruct *S)
{
/* MODEL TERMINATE */
}
/* END OF TEMPLATE */
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
The s-function is supposed to provide a simple division by zero protection as you can see from the code.
In the image below, if I delete S-Function1, S-Function will then work as intended.
I tried putting it in a library block and just put 2 library blocks and I got the same result

You are defining these variables:
float32 DivByZeroInput;
float32 DivByZeroOutput;
const void * inputPorts[NUMBER_OF_INPUTS];
const void * outputPorts[NUMBER_OF_OUTPUTS];
These variables are shared across both instances, which means both S-Functions write to the same output addresses.
Side note: Do you really have to use an S-Function? Creating the same from a few native simulink blocks is probably much easier.

Related

mbed os: using timer

I am using the board STM32F446RE to run [this project][1]. I am trying to set up and use timer 10 to count. The main.c file is below.
#include "radio.h"
#include <cstdio>
#if defined(SX128x_H)
#define BW_KHZ 200
#define SPREADING_FACTOR 7
#define CF_HZ 2487000000
#define TX_DBM 6
#else
#if defined(SX126x_H)
#define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14)
#else
#define TX_DBM 20
#endif
#define BW_KHZ 125
#define SPREADING_FACTOR 7
#define CF_HZ 915000000
#endif
/**********************************************************************/
EventQueue queue(4 * EVENTS_EVENT_SIZE);
TIM_HandleTypeDef htim10;
static void MX_TIM10_Init(void)
{
/* USER CODE BEGIN TIM10_Init 0 */
/* USER CODE END TIM10_Init 0 */
/* USER CODE BEGIN TIM10_Init 1 */
/* USER CODE END TIM10_Init 1 */
htim10.Instance = TIM10;
htim10.Init.Prescaler = 0;
htim10.Init.CounterMode = TIM_COUNTERMODE_UP;
htim10.Init.Period = 65535;
htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
//htim10.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim10) != HAL_OK)
{
printf("Error Error\r\n");
}
/* USER CODE BEGIN TIM10_Init 2 */
/* USER CODE END TIM10_Init 2 */
}
void tx_test()
{
static uint8_t seq = 0;
Radio::radio.tx_buf[0] = seq++; /* set payload */
Radio::Send(1, 0, 0, 0); /* begin transmission */
printf("sent\r\n");
/* {
mbed_stats_cpu_t stats;
mbed_stats_cpu_get(&stats);
printf("canDeep:%u ", sleep_manager_can_deep_sleep());
printf("Uptime: %llu ", stats.uptime / 1000);
printf("Sleep time: %llu ", stats.sleep_time / 1000);
printf("Deep Sleep: %llu\r\n", stats.deep_sleep_time / 1000);
}*/
}
void txDoneCB()
{
printf("got-tx-done\r\n");
queue.call_in(500, tx_test);
}
void rxDoneCB(uint8_t size, float rssi, float snr)
{
}
void radio_irq_callback()
{
queue.call(Radio::service);
}
const RadioEvents_t rev = {
/* DioPin_top_half */ radio_irq_callback,
/* TxDone_topHalf */ NULL,
/* TxDone_botHalf */ txDoneCB,
/* TxTimeout */ NULL,
/* RxDone */ rxDoneCB,
/* RxTimeout */ NULL,
/* RxError */ NULL,
/* FhssChangeChannel */NULL,
/* CadDone */ NULL
};
int main()
{
printf("\r\nreset-tx ");
Radio::Init(&rev);
Radio::Standby();
Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
Radio::SetChannel(CF_HZ);
Radio::set_tx_dbm(TX_DBM);
// preambleLen, fixLen, crcOn, invIQ
Radio::LoRaPacketConfig(8, false, true, false);
MX_TIM10_Init();
HAL_TIM_Base_Start_IT(&htim10);
HAL_Delay(500);
__HAL_TIM_SET_COUNTER(&htim10, 15);
uint16_t timer_val = __HAL_TIM_GET_COUNTER(&htim10);
printf("timer value = %d", timer_val);
queue.call_in(500, tx_test);
queue.dispatch();
}
Basically, I used the initialisation code taken from STM32CubeMX and I configured the clock of timer 10 in the file system_clock.c (normally in STM32CubeMX, clocks are configured in Clock Configuration tab). Then I initialized and started the time before getting the counter from that function. However, all I get from __HAL_TIM_GET_COUNTER is 0.
I want to ask what I did wrong.
Thank you,
Huy Nguyen
[1]: https://os.mbed.com/users/dudmuck/code/simple_tx/
This is purely speculation, but maybe you've not enabled the clock for TIM10. I don't know if that would explain what you are seeing or not. You might need something like __HAL_RCC_TIM10_CLK_ENABLE();

PyArg_ParseTuple doesn't parse a string correctly

So I'm learning how to make Python modules in C and today I wanted to learn how to create a custom type.
I wrote a module with one class named "Car" which stores a name, year and maxGas.
Problem is, when I try to create an instance of this class in Python the name property contains random characters instead of the given name.
#include <Python.h>
#include <stdio.h> //printf debugging
#include <stdlib.h> //free
#include "structmember.h"
typedef struct {
PyObject_HEAD
char* name; //allocated and freed by Python
int year;
float maxGas;
} cars_Car;
static int Car_init(cars_Car* self, PyObject* args, PyObject* kwargs){
/*
//Method #1
char* tmp;
// v- Note the &
if(!PyArg_ParseTuple(args, "sif", &tmp, &self->year, &self->maxGas)){
return -1;
}
self->name = (char*)malloc( sizeof(char) * (strlen(tmp) + 1) );
sprintf(self->name, "%s", tmp);
printf("Given name: %s\n", self->name);
//Works
*/
//Method #2
// v- Note the &
if(!PyArg_ParseTuple(args, "sif", &self->name, &self->year, &self->maxGas)){
return -1;
}
printf("Given name: %s\n", self->name); // Works, but repr() and name/getName() are broken
return 0;
}
static void Car_dealloc(cars_Car* self){
//free(self->name); //For Method #2 in init()
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject* Car_repr(cars_Car* self){
return PyUnicode_FromFormat("<Car name=\"%s\" year=%d tankSize=%SL>",
self->name, self->year, Py_BuildValue("f", self->maxGas) /* %f not supported, so we convert it to a Python string */ );
}
static PyObject* Car_new(PyTypeObject* type, PyObject* args, PyObject* kwargs){
cars_Car* self;
self = (cars_Car*)type->tp_alloc(type, 0);
self->name = NULL;
self->year = 0;
self->maxGas = 0.0f;
return (PyObject*)self;
}
/* Methods */
static PyObject* Car_getName(cars_Car* self, PyObject* args){
return Py_BuildValue("s", self->name);
}
static PyObject* Car_getYear(cars_Car* self, PyObject* args){
return Py_BuildValue("i", self->year);
}
static PyObject* Car_getTankSize(cars_Car* self, PyObject* args){
return Py_BuildValue("f", self->maxGas);
}
static PyMethodDef Car_methods[] = {
{"getName", (PyCFunction)Car_getName, METH_NOARGS,
"Get the car's name.",
},
{"getYear", (PyCFunction)Car_getYear, METH_NOARGS,
"Get the car's year.",
},
{"getTankSize", (PyCFunction)Car_getTankSize, METH_NOARGS,
"Get the car's tank size.",
},
{NULL}
};
static struct PyMemberDef Car_members[] = {
{"name", T_STRING, offsetof(cars_Car, name), READONLY, "Car name"},
{"year", T_INT, offsetof(cars_Car, year), READONLY, "Car year"},
{"maxGas", T_FLOAT, offsetof(cars_Car, maxGas), READONLY, "Car maxGas"},
{NULL}
};
static PyTypeObject cars_CarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cars.Car", /* tp_name */
sizeof(cars_Car), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Car_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
(reprfunc)Car_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"Cars objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Car_methods, /* tp_methods */
Car_members, /* tp_members */
NULL, /* tp_getset */
NULL, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Car_init, /* tp_init */
0, /* tp_alloc */
Car_new, /* tp_new */
0, /* tp_free */
0 /* tp_is_gc */
};
static PyModuleDef modcars = {
PyModuleDef_HEAD_INIT,
"cars",
"Example module that creates an extension type.",
-1,
NULL, NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_Cars(void) {
PyObject* m;
cars_CarType.tp_new = PyType_GenericNew;
if (PyType_Ready(&cars_CarType) < 0)
return NULL;
m = PyModule_Create(&modcars);
if (m == NULL)
return NULL;
Py_INCREF(&cars_CarType);
PyModule_AddObject(m, "Car", (PyObject *)&cars_CarType);
return m;
}
As you can see, I mentioned 2 different methods in the Car_init function.
When using the first method, I get the following result:
>>> import Cars
>>> x = Cars.Car("The Big Car", 2020, 4.2)
Given name: The Big Car #printf() works
>>> x
<Car name="????" year=2020 tankSize=4.199999809265137L #??? - bunch of random characters
I'm not sure what's causing this, although my guess is that my code is probably writing to some memory address where it's not supposed to. It also looks like there's a floating point precision error.
Interestingly when I directly call getName() or try to access the name attribute, it works.
>>> Cars.Car("The Big Car", 2020, 4.2).name
'The Big Car'
>>> Cars.Car("The Big Car", 2020, 4.2).getName()
'The Big Car'
>>> repr(Cars.Car("The Big Car", 2020, 4.2))
'<Car name="The Big Car" year=2020 tankSize=4.199999809265137L>'
If I switch to the second method everything works fine. I'm not sure why using a temporary variable helps. Also my code doesn't write anything to name, except when initialising it. Everywhere else it's just being read. I also thought about the garbage collector, maybe it's freeing name?
The documentation for the s argument says
A pointer to an existing string is stored in the character pointer variable whose address you pass
Implicitly this pointer is only valid while the existing object is valid (i.e. possibly only until the args tuple is freed).
For your first example that's fine - you allocate new memory with malloc and copy from it. You do that during your initial function call so you know the args tuple and all its contents still exists. You need to remember to free the memory of course.
For your second example you just take a pointer to some memory owned by something else. Keeping a pointer to the memory doesn't do anything to tell Python to keep the memory valid. Therefore the memory is valid during Car_init but most likely invalid shortly after that.

why bpf ringbuf can not use in uprobe of libbpf?

Recently, I am trying to use bpf ringbuf in uprobe example of libbpf. But when running, error occurred which is "libbpf: load bpf program failed: Invalid argument". I have no idea why this happened. Could anyone help? Below is my test code.
Kernel space code: uprobe.bpf.c, define a rb struct, and use bpf_ringbuf_reserve in uprobe code block.
#include <linux/bpf.h>
#include <linux/ptrace.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
char LICENSE[] SEC("license") = "Dual BSD/GPL";
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
} rb SEC(".maps");
SEC("uprobe/func")
int BPF_KPROBE(uprobe, int a, int b)
{
__u64* e = bpf_ringbuf_reserve(&rb, sizeof(__u64), 0);
if (!e)
return 0;
bpf_printk("UPROBE ENTRY: a = %d, b = %d\n", a, b);
return 0;
}
SEC("uretprobe/func")
int BPF_KRETPROBE(uretprobe, int ret)
{
bpf_printk("UPROBE EXIT: return = %d\n", ret);
return 0;
}
User space code: uprobe.c
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include "uprobe.skel.h"
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
return vfprintf(stderr, format, args);
}
static void bump_memlock_rlimit(void)
{
struct rlimit rlim_new = {
.rlim_cur = RLIM_INFINITY,
.rlim_max = RLIM_INFINITY,
};
if (setrlimit(RLIMIT_MEMLOCK, &rlim_new)) {
fprintf(stderr, "Failed to increase RLIMIT_MEMLOCK limit!\n");
exit(1);
}
}
/* Find process's base load address. We use /proc/self/maps for that,
* searching for the first executable (r-xp) memory mapping:
*
* 5574fd254000-5574fd258000 r-xp 00002000 fd:01 668759 /usr/bin/cat
* ^^^^^^^^^^^^ ^^^^^^^^
*
* Subtracting that region's offset (4th column) from its absolute start
* memory address (1st column) gives us the process's base load address.
*/
static long get_base_addr() {
size_t start, offset;
char buf[256];
FILE *f;
f = fopen("/proc/self/maps", "r");
if (!f)
return -errno;
while (fscanf(f, "%zx-%*x %s %zx %*[^\n]\n", &start, buf, &offset) == 3) {
if (strcmp(buf, "r-xp") == 0) {
fclose(f);
return start - offset;
}
}
fclose(f);
return -1;
}
static int handle_event(void *ctx, void *data, size_t data_sz)
{
return 0;
}
/* It's a global function to make sure compiler doesn't inline it. */
int uprobed_function(int a, int b)
{
return a + b;
}
int main(int argc, char **argv)
{
struct ring_buffer *rb = NULL;
struct uprobe_bpf *skel;
long base_addr, uprobe_offset;
int err, i;
/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);
/* Bump RLIMIT_MEMLOCK to allow BPF sub-system to do anything */
bump_memlock_rlimit();
/* Load and verify BPF application */
skel = uprobe_bpf__open_and_load();
if (!skel) {
fprintf(stderr, "Failed to open and load BPF skeleton\n");
return 1;
}
base_addr = get_base_addr();
if (base_addr < 0) {
fprintf(stderr, "Failed to determine process's load address\n");
err = base_addr;
goto cleanup;
}
/* uprobe/uretprobe expects relative offset of the function to attach
* to. This offset is relateve to the process's base load address. So
* easy way to do this is to take an absolute address of the desired
* function and substract base load address from it. If we were to
* parse ELF to calculate this function, we'd need to add .text
* section offset and function's offset within .text ELF section.
*/
uprobe_offset = (long)&uprobed_function - base_addr;
/* Attach tracepoint handler */
skel->links.uprobe = bpf_program__attach_uprobe(skel->progs.uprobe,
false /* not uretprobe */,
0 /* self pid */,
"/proc/self/exe",
uprobe_offset);
err = libbpf_get_error(skel->links.uprobe);
if (err) {
fprintf(stderr, "Failed to attach uprobe: %d\n", err);
goto cleanup;
}
/* we can also attach uprobe/uretprobe to any existing or future
* processes that use the same binary executable; to do that we need
* to specify -1 as PID, as we do here
*/
skel->links.uretprobe = bpf_program__attach_uprobe(skel->progs.uretprobe,
true /* uretprobe */,
-1 /* any pid */,
"/proc/self/exe",
uprobe_offset);
err = libbpf_get_error(skel->links.uretprobe);
if (err) {
fprintf(stderr, "Failed to attach uprobe: %d\n", err);
goto cleanup;
}
/* Set up ring buffer polling */
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, NULL, NULL);
if (!rb) {
err = -1;
fprintf(stderr, "Failed to create ring buffer\n");
goto cleanup;
}
printf("Successfully started! Please run `sudo cat /sys/kernel/debug/tracing/trace_pipe` "
"to see output of the BPF programs.\n");
for (i = 0; ; i++) {
err = ring_buffer__poll(rb, 100 /* timeout, ms */);
/* trigger our BPF programs */
fprintf(stderr, ".");
uprobed_function(i, i + 1);
sleep(1);
}
cleanup:
ring_buffer__free(rb);
uprobe_bpf__destroy(skel);
return -err;
}

Matlab S-Function crashing when set to dynamically sized

i have following problem. I have created a S-Function which should read a matrix from a text file, where the output size of the output port is not known and therefore set as dynamically sized. Matlab is then crashing. When setting for fixed size, there is no problem. I hope someone could help me out
#define S_FUNCTION_NAME Data_Input
#define S_FUNCTION_LEVEL 2
/*---- Define size of input ports ---------------------------------------------------*/
#define OUTPUTSIZE 50
/*-----------------------------------------------------------------------------------*/
#include <stdio.h>
#include "simstruc.h"
int_T in;
/*---- Define data types ------------------------------------------------------------*/
typedef struct SBufferData
{
real32_T data[50];
int32_T number;
} SBuffer;
/*==== mdlCheckParameters ===========================================================*/
#undef MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
static void mdlCheckParameters(SimStruct *S)
{
}
#endif /* MDL_CHECK_PARAMETERS */
/*===================================================================================*/
/*=== mdlInitializeSizes ============================================================*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S,1); /* number of expected parameters */
ssSetSFcnParamTunable(S, 0, 0); /* sets parameter 1 to be non-tunable */
ssSetSFcnParamTunable(S, 1, 0); /* sets parameter 2 to be non-tunable */
ssSetNumContStates(S,0); /* number of continuous states */
ssSetNumDiscStates(S,0); /* number of discrete states */
ssSetNumInputPorts(S,0); /* number of input ports */
ssSetNumOutputPorts(S,1); /* number of output ports */
ssSetOutputPortWidth(S,0,DYNAMICALLY_SIZED); /* first output port width */
ssSetOutputPortDataType(S,0,SS_SINGLE); /* first output port data type */
ssSetNumSampleTimes(S,0); /* number of sample times */
ssSetNumRWork(S,0); /* number real work vector elements */
ssSetNumIWork(S,0); /* number int_T work vector elements */
ssSetNumPWork(S,1); /* number ptr work vector elements */
ssSetNumModes(S,0); /* number mode work vector elements */
ssSetNumNonsampledZCs(S,0); /* number of nonsampled zero crossing */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch reported by the Simulink engine*/
}
}
/*===================================================================================*/
/*==== mdlInitializeSampleTimes =====================================================*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
real_T dSampleTime=(mxGetPr(ssGetSFcnParam(S,0))[0]);
ssSetSampleTime(S, 0, dSampleTime);
ssSetOffsetTime(S, 0, 0.0);
}
/*===================================================================================*/
/*==== mdlStart =====================================================================*/
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
FILE *fp;
int_T i, index=0;
SBuffer *buffer;
float temp;
/*---- Retrieve pointer to pointers work vector -------------------------------------*/
void **PWork = ssGetPWork(S);
buffer = (SBuffer *)malloc(OUTPUTSIZE * sizeof(SBuffer));
PWork[0] = (void *)buffer;
/*---- Read from text-file ----------------------------------------------------------*/
fp = fopen("myfile.txt", "r");
while(feof(fp)==0)
{
fscanf(fp,"%f",&temp);
buffer -> data[index] = temp;
index++;
}
buffer -> number = (int32_T) index;
fclose(fp);
}
#endif /* MDL_START */
/*===================================================================================*/
/*==== mdlOutputs ===================================================================*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
/*---- Get input ports --------------------------------------------------------------*/
real_T *y1=(real_T *)ssGetOutputPortSignal(S,0);
int_T i;
SBuffer *buffer;
/*---- Retrieve pointer to pointers work vector -------------------------------------*/
void **PWork = ssGetPWork(S);
buffer = PWork[0];
for (i = 0; i < buffer->number; i++)
{
y1[i] = buffer->data[i];
}
}
/*===================================================================================*/
/*==== mdlTerminate =================================================================*/
static void mdlTerminate(SimStruct *S)
{
SBuffer *buffer;
/*---- Retrieve pointer to pointers work vector -------------------------------------*/
void **PWork = ssGetPWork(S);
buffer = PWork[0];
/*---- Deallocate structure 'info' and 'buffer' -------------------------------------*/
free(buffer);
}
/*===================================================================================*/
/*==== Required S-function trailer ==================================================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
/*===================================================================================*/

How High the Pin of Parallel Port

/* Necessary includes for drivers */
#include <linux/init.h>
//#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/ioport.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */
#include <asm/io.h> /* inb, outb */
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Nikunj");
/* Function declaration of parlelport.c */
int parlelport_open(struct inode *inode, struct file *filp);
int parlelport_release(struct inode *inode, struct file *filp);
ssize_t parlelport_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t parlelport_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
void parlelport_exit(void);
int parlelport_init(void);
/* Structure that declares the common */
/* file access fcuntions */
struct file_operations parlelport_fops = {
read : parlelport_read,
write : parlelport_write,
open : parlelport_open,
release : parlelport_release
};
/* Driver global variables */
/* Major number */
int parlelport_major = 61;
/* Control variable for memory */
/* reservation of the parallel port*/
int port;
module_init(parlelport_init);
module_exit(parlelport_exit);
int parlelport_init(void)
{
int result;
/* Registering device */
result = register_chrdev(parlelport_major, "parlelport", &parlelport_fops);
if (result < 0)
{
printk("<1>parlelport: cannot obtain major number %d\n",parlelport_major);
return result;
}
/* Registering port */
port = check_region(0x378, 1);
if (port)
{
printk("<1>parlelport: cannot reserve 0x378\n");
result = port;
goto fail;
}
request_region(0x378, 1, "parlelport");
printk("<1>Inserting parlelport module\n");
return 0;
fail:
parlelport_exit();
return result;
}
void parlelport_exit(void)
{
/* Make major number free! */
unregister_chrdev(parlelport_major, "parlelport");
/* Make port free! */
if (!port)
{
release_region(0x378,1);
}
printk("<1>Removing parlelport module\n");
}
int parlelport_open(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
int parlelport_release(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
ssize_t parlelport_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
/* Buffer to read the device */
char parlelport_buffer;
/* Reading port */
parlelport_buffer = inb(0x378);
/* We transfer data to user space */
copy_to_user(buf,&parlelport_buffer,1);
/* We change the reading position as best suits */
if (*f_pos == 0)
{
*f_pos+=1;
return 1;
}
else
{
return 0;
}
}
ssize_t parlelport_write( struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
char *tmp;
/* Buffer writing to the device */
char parlelport_buffer;
tmp=buf+count-1;
copy_from_user(&parlelport_buffer,tmp,1);
/* Writing to the port */
outb(parlelport_buffer,0x378);
return 1;
}
this is the code of parallel port device driver and this is my first c code for that.
Please help me to solve the below problem
i have successfully compile the code and create the .ko file successfully and successfully load in the ubuntu 9.10 OS but thee is no high or low the pin so please help me
How you are checking answer?? After installed module do
dmesg
Actually you can not acquire region at 0x378. Because by default system allocate this one to parport0.
Do cat /proc/ioports so you will find that at 0x378 there is parport0.
You can directly write on that address (0x378), without using that checking and requesting.
My suggestion is that don't directly go in complex form. In write function just write
outb(0x378,1) and check (by inserting hardware having LEDs) LED on/off on data pins.