Understanding the use of memset in this example - driver

I'm studying an example from the Linux Device Driver book(http://lwn.net/Kernel/LDD3/), and I don't understand the use and usefullness of the function memset in this context and I hoped that someone could explain it to me. I understand that we allocate memory for our device structure using kmalloc and with memset we put 0's in front of the memory address? Here is the example nonortheless:
int scull_p_init(dev_t firstdev)
{
int i, result;
result = register_chrdev_region(firstdev, scull_p_nr_devs, "scullp");
if (result < 0) {
printk(KERN_NOTICE "Unable to get scullp region, error %d\n", result);
return 0;
}
scull_p_devno = firstdev;
scull_p_devices = kmalloc(scull_p_nr_devs * sizeof(struct scull_pipe), GFP_KERNEL);
if (scull_p_devices == NULL) {
unregister_chrdev_region(firstdev, scull_p_nr_devs);
return 0;
}
memset(scull_p_devices, 0, scull_p_nr_devs * sizeof(struct scull_pipe));
for (i = 0; i < scull_p_nr_devs; i++) {
init_waitqueue_head(&(scull_p_devices[i].inq));
init_waitqueue_head(&(scull_p_devices[i].outq));
init_MUTEX(&scull_p_devices[i].sem);
scull_p_setup_cdev(scull_p_devices + i, i);
}

The memset is not putting 0 in front of scull_p_devices. It is overwriting the memory from the address in scull_p_devices up to the size of the allocated region with zeros.

Related

Store the ADC stream on µSD Card without loss on STM32H743ZI

I am working on a project in which I have to store the datas of an ADC Stream on a µSD card. However even if I use a 16 bits buffer, I lose data from the ADC stream. My ADC is used with DMA and I use FATFS (WITHOUT DMA) and the SDMMC1 peripheral to fill a .bin file with the datas.
Do you have an idea to avoid this loss ?
Here is my project : https://github.com/mathieuchene/STM32H743ZI
I use a nucleo-h743zi2 Board, CubeIDE, and CubeMx in their last version.
EDIT 1
I tried to implement Colin's solution, it's better but I have a strange things in the middle of my acquisition. However when I increase the maximal count value or try to debug, the HardFault_Handler appears. I modified main.c file by creating 2 blocks (uint16_t blockX[BUFFERLENGTH/2]) and 2 flags for when adcBuffer is half filled or completely filled.
I also changed the while(1) part in main function like this
if (flagHlfCplt){
//flagCplt=0;
res = f_write(&SDFile, block1, strlen((char*)block1), (void *)&byteswritten);
memcpy(block2, adcBuffer, BUFFERLENGTH/2);
flagHlfCplt = 0;
count++;
}
if (flagCplt){
//flagHlfCplt=0;
res = f_write(&SDFile, block2, strlen((char*)block2), (void *)&byteswritten);
memcpy(block1, adcBuffer[(BUFFERLENGTH/2)-1], BUFFERLENGTH/2);
flagCplt = 0;
count++;
}
if (count == 10){
f_close(&SDFile);
HAL_ADC_Stop_DMA(&hadc1);
while(1){
HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
HAL_Delay(1000);
}
}
}
EDIT 2
I modified my program. I set block 1 and block 2 with the length of BUFFERLENGTH and I added a pointer (*idx) to change the buffer which is filled. I don't have HardFault_Handler anymore but I still loose some datas from my adc's stream.
Here are the modification I made:
// my pointer and buffers
uint16_t block1[BUFFERLENGTH], block2[BUFFERLENGTH], *idx;
// init of pointer and adc start
idx=block1;
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)idx, BUFFERLENGTH);
// while(1) part
while (1)
{
if (flagCplt){
if (flagToChangeBuffer) {
idx=block1;
res = f_write(&SDFile, block2, strlen((char*)block2), (void *)&byteswritten);
flagCplt = 0;
flagToChangeBuffer=0;
count++;
}
else {
idx=block2;
res = f_write(&SDFile, block1, strlen((char*)block1), (void *)&byteswritten);
flagCplt = 0;
flagToChangeBuffer=1;
count++;
}
}
if (count == 150){
f_close(&SDFile);
HAL_ADC_Stop_DMA(&hadc1);
while(1){
HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
HAL_Delay(1000);
}
}
}
Does someone know how to solve my matter with these loss?
Best Regards
Mathieu

Can't pass in struct into function for pthread_create correctly

I'm trying to pass in a Professor struct into my professor function but I can't properly get the information stored in it to pass into the function. I suspect it has something to do with how I malloc'd p but I thought freeing after it's completed would solve that problem. I get a segfault when I try to print *professor->id, because apparently it decides to read p as memory location 0x0, even though it's not in main
typedef struct{
int *id;
int *assignings;
int *min_wait;
int *max_wait;
int *min_assignments;
int *max_assignments;
int *min_hrs;
int *max_hrs;
} Professor;
Professor* makeProfessor(){
Professor *professor = malloc(sizeof *professor);
return professor;
}
void * professorFunc(void *p){
Professor *professor = (Professor*)p;
fprintf(stdout,"Starting Professor %d\n", *professor->id);
pthread_exit(0);
}
int main(int argc, char **argv){
//Creating threads
pthread_t professor[num_professors];
Professor *p;
int i;
int id;
for(i = 0; i < num_professors; ++i){
id = i + 1;
p = malloc (sizeof *p);
p->id = &id;
if(pthread_create(&professor[i], NULL, professorFunc, (void*)p) != 0){
perror("pthread_create");
exit(1);
}
free(p);
}
for(i = 0; i < num_professors; ++i){
if(pthread_join(professor[i], NULL) != 0){
perror("pthread_join");
exit(1);
}
}
You are allocating the array of Professor structs, and immediately freeing them, likely before your thread has the chance to operate on them. A better way to implement this, would be to allocate the whole array, process them, and then free the memory, once you know the threads have exited (example below).
pthread_t professor[num_professors];
Professor *p;
int i;
int id;
p = malloc (sizeof(*p) * num_professors);
for(i = 0; i < num_professors; ++i){
id = i + 1;
p->id = &id;
if(pthread_create(&professor[i], NULL, professorFunc, (void*)p) != 0){
perror("pthread_create");
exit(1);
}
}
for(i = 0; i < num_professors; ++i){
if(pthread_join(professor[i], NULL) != 0){
perror("pthread_join");
exit(1);
}
}
free(p);

How can I receive simple integer values over bluetooth in ios

I am trying to learn how to get some sensors plugged into an Arduino board to talk to an iPhone over Bluetooth with a Red Bear Labs mini board but have hit a brick wall.
The sensors get a reading and this is sent to the phone over BLE. So far I've connected to the device and I get back what appears to be data but I can't make sense of it.
I've written a little sketch that looks like this, to simulate the sensor data.
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(5, 6);
void setup() {
bluetooth.begin(57600);
}
void loop() {
//int reading = analogRead(2);
int reading = 123; // fake reading
byte lowerByte = (byte) reading & 0xFF;
byte upperByte = (byte) (reading >> 8) & 0xFF;
bluetooth.write(reading);
bluetooth.write(upperByte);
bluetooth.write(lowerByte);
delay(1000);
}
In iOS I send a call to read the data and then the data is received by a piece of code that looks something like:
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error
{
Byte data[20];
static unsigned char buf[512];
static int len = 0;
NSInteger data_len;
if (!error && [characteristic.UUID isEqual:[CBUUID UUIDWithString:#RBL_CHAR_TX_UUID]]){
data_len = characteristic.value.length;
[characteristic.value getBytes:data length:data_len];
if (data_len == 20){
memcpy(&buf[len], data, 20);
len += data_len;
if (len >= 64){
[[self delegate] bleDidReceiveData:buf length:len];
len = 0;
}
} else if (data_len < 20) {
memcpy(&buf[len], data, data_len);
len += data_len;
[[self delegate] bleDidReceiveData:buf length:len];
len = 0;
}
}
}...
}
But when I look at the data that comes back it just makes no sense to me at all.. (I'll dig out an example as soon as I can).
Does anyone know a simple step I'm missing or a good example I could look at to try and better understand this?
I finally realised that the data was correct, I had to 'pull out' the data by bit shifting it.
UInt16 value;
UInt16 pin;
for (int i = 0; i < length; i+=3) {
pin = data[i];
value = data[i+2] | data[i+1] << 8;
NSLog(#"Pin: %d", pin);
NSLog(#"Value %d",value);
}

OpenCv image unstable while running AES decryption

I am trying to capture video from webcam using Opencv and transmit it over TCP. In addition, I wanted to encrypt the video using AES. But whenever run the AES decrpt function the video is unstable.
I am using the opencv over tcp example and AES example
Whenever I run this function:
img->imageData = aes_decrypt(&de, img->imageData, &imgsize);
my video gets unstable.
I have attached the code segment where I wrote the function.
/* start receiving images*/
while(1)
{
/* get raw data */
for (i = 0; i < imgsize; i += bytes) {
if ((bytes = recv(sock, sockdata + i, imgsize - i, 0)) == -1) {
quit("recv failed", 1);
}
}
pthread_mutex_lock(&mutex);
for (i = 0, k = 0; i < img->height; i++) {
for (j = 0; j < img->width; j++) {
((uchar*)(img->imageData + i * img->widthStep))[j] = sockdata[k++];
}
}
img->imageData = aes_decrypt(&de, img->imageData, &imgsize);
is_data_ready = 1;
pthread_mutex_unlock(&mutex);
/* have we terminated yet? */
pthread_testcancel();
/* no, take a rest for a while */
usleep(1000);
}
This is my first post, sorry for my bad English and format of the post.

madvise() function not working

I am trying madvise() to mark allocated memory as mergeable so that two applications having same pages can be merged.
While using the madvise() function it shows "invalid argument".
#include<stdio.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<errno.h>
#define ADDR 0xf900f000
int main()
{
int *var1=NULL,*var2=NULL;
size_t size=0;
size = 1000*sizeof(int);
var1 = (int*)malloc(size);
var2 = (int *)malloc(size);
int i=0;
for(i=0;i<999;i++)
{
var1[i] = 1;
}
for(i=0;i<999;i++)
{
var2[i] = 1;
}
i = -1;
while(i<0)
{
i = madvise((void *)var1, size, MADV_MERGEABLE); //to declare mergeable
printf("%d %p\n", i, var1); //to print the output value
err(1,NULL); //to print the generated error
i = madvise((void *)var2, size, MADV_MERGEABLE); //to declare mergeable
printf("%d\n", i);
}
return 0;
}
Error:
a.out: Invalid argument
Please help me.
Thank You.
You can only merge whole pages. You can't merge arbitrary chunks of data.

Resources