Tuesday, February 28, 2017



Pattern count

Given a string, your task is to find the number of patterns of form 1[0]1 where [0] represents any number of zeroes (minimum requirement is one 0) there should not be any other character except 0 in the [0] sequence.

Example:
Input:
100001abc101
1001ab010abc01001
10101010

Output:
2
2
3

Check my geeksforgeeks submission: http://www.practice.geeksforgeeks.org/viewSol.php?subId=1574505&pid=1598

Answer 1: (Unoptimized)
#include <stdio.h>

int main(void)
{
char buff[32];
int count = 0, i = 0, zflag = 0,oflag = 0;

printf("String: ");
scanf("%s",buff);

while(buff[i] != '\0') {
if(buff[i] == '0') {
if(oflag == 1) {
oflag = 0;
zflag = 1;
}
}
else if(buff[i] == '1') {
oflag = 1;
if(zflag == 1) {
//oflag = 0;
zflag = 0;
count++;
}
}
else {
oflag = 0;
zflag = 0;
}
i++;
}
printf("Count = %d\n",count);

return 0;
}

Answer2: (Better version)
#include <stdio.h>
#include <string.h>

int patterncount(char *s)
{
int len = strlen(s), i, count = 0;

for(i = 0; i < len; i++) {
if(s[i] == '1' && s[i+1] == '0') {
i++;
for(; i < len; i++) {
if(s[i] == '1') {
count++;
i--;
break;
}
else if (s[i] != '0')
break;
}
}
}
return count;
}

int main(void)
{
char buff[32];

printf("String: ");
scanf("%s", buff);

printf("Count = %d\n",patterncount(buff));

return 0;
}

Sunday, November 1, 2015

custom sizeof implementation

#include <stdio.h>

#define my_sizeof(type) (((type *)0)+1)

int main()
{
    printf("size of int = %d\n", sizeof(int));
    printf("My size of int = %d\n", my_sizeof(int));
    printf("My size of char = %d\n", my_sizeof(char));
    printf("My size of float = %d\n", my_sizeof(float));
    printf("My size of double = %d\n", my_sizeof(double));
    return 0;
}


Result:
   size of int = 4
   My size of int = 4
   My size of char = 1
   My size of float = 4
   My size of double = 8

BeagleBoard notes


============================================
How to load files(rd-ext2.bin & uImage) from mmc card and boot Linux?

In u-boot:
mmc rescan
fatload mmc 0:1 81600000 rd-ext2.bin

setenv bootargs mem=88M ip=172.16.9.111 console=ttyO2,19200n8 root=/dev/ram0 rw initrd=0x81600000,16M ramdisk_size=16384

fatload mmc 0:1 80000000 uImage
============================================


----------------------------------------
make uImage LOADADDR=0x80008000
make CONFIG_PREFIX=/home/neo/tgt install
make omap2plus_defconfig

-------------------------------------------
git status
  201  arm-linux-gnueabi-
  202  export ARCH=arm
  203  export CROSS_COMPILE=arm-linux-gnueabi-
  204  make uImage
  205  ls arch/arm/configs/ | grep omap
  206  make omap2plus_defconfig
  207  make uImage
  208  rm drivers/video/built-in.o
  209  rm drivers/video/.built-in.o.cmd
  210  make uImage
  211  rm drivers/video/console/built-in.o
  212  make uImage
  213  make distclean
  214  make omap2plus_defconfig
  215  make uImage
  216  make uImage LOADADDR=0x80008000
  217  cp arch/arm/boot/uImage /media/neo/boot/

Monday, December 30, 2013

Delete same data in single linked list


#include <stdio.h>
#include <malloc.h>

typedef struct slink {
int data;
struct slink *next;
}node;


int a[] = {1, 2, 3, 3, 1, 2, 2, 1, 8, 9};


void append(node **q, int num)
{
node *temp = *q, *r = NULL;

r = (node *)malloc(sizeof(node));
r->data = num;
r->next = NULL;

if(*q == NULL)
*q = r;
else {
while(NULL != temp->next)
temp = temp->next;
temp->next = r;
}
}

void display(node *temp)
{
printf("\nThe list: ");
while(temp) {
printf("%d ", temp->data);
temp = temp->next;
}
}


void red_del(node **q)
{
node *temp = *q, *new = NULL,  *old = NULL;
int data;

while (temp != NULL && temp->next != NULL) {
new = temp;
data = new->data;
while(new->next != NULL) {
if(data == new->next->data) {
old = new->next;
new->next = new->next->next;
free(old);
} else 
new = new->next;
}
temp = temp->next;
}
}

void revlist(node **q)
{
node *temp1 = *q, *temp2 = NULL, *temp3 = NULL;

if (!temp1) return;

while(temp1) {
temp2 = temp1->next;
temp1->next = temp3;
temp3 = temp1;
temp1 = temp2;
}
if(temp3) *q = temp3;
}

int main()
{
node *head = NULL;


for (i = 0; i < 10; i++)
append(&head, a[i]);
display(head);

revlist(&head);
display(head);

red_del(&head);
display(head);

return 0;
}


Test
The list: 1 2 3 3 1 2 2 1 8 9
The list: 9 8 1 2 2 1 3 3 2 1
The list: 9 8 1 2 3 

Best method for setting & reading a hardware register

#include <stdio.h>


char *basecalc(unsigned int num, unsigned int base)
{
        static char buff[32];
        char *s = &buff[31];
        *s = '\0';

        while(num) {
                *(--s) = "0123456789ABCDEF"[num%base];
                num /= base;
        }
        return s;
}

void setreg(unsigned int *reg, unsigned int nofbits, unsigned int bitpos, unsigned int value)
{
        unsigned int mask;
        mask  = (~(~0 << nofbits) << bitpos);
        value = value << bitpos;
        *reg  = (*reg & ~mask) | (value & mask);
}

unsigned int getreg(unsigned int *reg, unsigned int nofbits, unsigned int bitpos)
{
        return (*reg >> bitpos) & (( 1 << nofbits) - 1) ;
}

int main()
{
        int num, nofbits, bitpos, value;
        printf("Enter the num: ");
        scanf("%d", &num);
        printf("Enter the nofbits: ");
        scanf("%d", &nofbits);
        printf("Enter the bitpos: ");
        scanf("%d", &bitpos);
        printf("Enter the value: ");
        scanf("%d", &value);

        printf("Read reg: %s\n", basecalc(getreg(&num, nofbits, bitpos), 2));

        printf("The num: %s\n", basecalc(num, 2));
        setreg(&num, nofbits, bitpos, value);
        printf("The result: %s\n", basecalc(num, 2));
        return 0;

}

Linux style
static void setreg(void __iomem *reg, u32 nofbits, u32 bitpos, u32 value)
{
        u32 mask;
        mask = (~(~0 << nofbits) << bitpos);
        value <<= bitpos;
        iowrite32(((ioread32(reg) & ~mask) | (value & mask)), reg);
}

static u32 getreg(void __iomem *reg, u32 nofbits, u32 bitpos)
{
        return (ioread32(reg) >> bitpos) & (~(~0 << nofbits));
}


Test 1
Enter the num: 25
Enter the nofbits: 2
Enter the bitpos: 3
Enter the value: 1

Read reg: 11
The num: 11001
The result: 1001

Test 2
Enter the num: 153
Enter the nofbits: 2
Enter the bitpos: 3
Enter the value: 2
The num: 10011001
The result: 10010001

Sunday, September 29, 2013

working kernel on beagleboard


Environment settings
~~~~~~~~~~~~~~~~~~~~
echo $PATH
export PATH=$PATH:/home/vinuz/CodeSourcery_2011_3_41/Sourcery_G++_Lite/bin/
export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabi-

u-boot (The initial MLO is also build along with this)
~~~~~~~
u-boot-main --> tested OK
    make distclean
    make omap3_beagle_config
    make
After build:
    cp MLO /media/boot
    cp u-boot.img /media/boot
    if reqd: cp u-boot.bin /media/boot

minicom
~~~~~~~
setenv baudrate 19200

Linux kernel
~~~~~~~~~~~~
linux --> linux-stable --> tested OK
Kernel cloning:
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git -v

make distclean
make omap2plus_defconfig
make uImage LOADADDR=0x80008000

cp arch/arm/boot/uImage /media/boot/
cp ~/rd-ext2.bin /media/boot/

Loading kernel from mmc
~~~~~~~~~~~~~~~~~~~~~~~
setenv bootargs console=ttyO2,115200n8 root=/dev/ram0 rw ramdisk_size=32768 initrd=0x81600000,32M
    OR
setenv bootargs console=ttyO2,19200n8 root=/dev/ram0 rw ramdisk_size=32768 initrd=0x81600000,32M

fatload mmc 0 81600000 rd-ext2.bin
fatload mmc 0 80000000 uImage
bootm 80000000


busybox
~~~~~~~
git clone git://busybox.net/busybox.git
make defconfig
make
make CONFIG_PREFIX=/home/vinux/targetfs install
--copy reqd files & create dirs --
dd if=/dev/zero of=ramdisk.bin bs=1k count=16384
mke2fs -F -m0 ramdisk.bin
mount -t ext2 ramdisk.bin /mnt -o loop
tar -C targetfs -cf - . | tar -C /mnt -xf -
umount /mnt
cp ramdisk.bin /media/boot
[/home/vinuz/targetfs/ - tested working fine]


tftp
~~~~
tftp -g -r hi.txt 172.16.9.111


Friday, July 12, 2013

uboot - For beagleboard XM

The configuration for BeagleBoard and BeagleBoard-xM is omap3_beagle. The configuration for BeagleBone is am335x_evm.
To rebuild:
  • Install ARM cross-compiler. On Ubuntu:
    • sudo opkg install gcc-arm-linux-gnueabi
  • git clone git://git.denx.de/u-boot.git && cd u-boot
  • ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- make am335x_evm_config|omap3_beagle_config
  • ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- make
Typical boot process for BeagleBoard using U-Boot includes two U-Boot phases:
  1. The BeagleBoard ROM bootloader first loads a file called "MLO" from the SD card. This requires special formatting of the SD card and copying of the "MLO" file to the SD card. The "MLO" file is provided by U-Boot's Secondary Program Loader (SPL) functionality. It is placed into the u-boot folder upon build.
  2. The U-Boot SPL configures the off-chip memory and then loads the full U-Boot. The file is typically called "u-boot.img" and SPL will typically look for it in the root folder of a FAT partition, but other options are possible
 Note: It seems that the latest u-boot clone from denx packaged with the latest buildroot supplies 'SPL' support in the menuconfig. From what I understand SPL ( named MLO ) now replaces xloader as the second stage bootloader after the OMAP 1st stage loader in ROM. Long story short enable SPL (MLO) support from u-boot and build with the omap3_beagle defconfig and everything works just fine!

serial boot: http://elinux.org/BeagleBoard#USB-to-Serial_Converter