Monday, December 2, 2013

Power Save with Atmega16A and Atmega8-PU

In my very first beginning of my electronic project and study (autodidact) was
so enthusiast with my Atmega8-PU (the -PU means than could run with 16Mhz crystal).
Playing with timer0/1/2 was very fun. And there was a time that i came to
power-save-mode, interesting part began.

In the power-save-mode there are several ways to wake up the chip (Atmega8):
1. External Interrupts (INT0/INT1)
2. TWI Address Match (I2C peripheral)
3. Timer2 (which has to be run in Async Mode)

I choose to use the Timer2 to trigger the wake-up. There is a cost to play with, since
i have set the fuses wrongly. The fuses were set to run in low frequency crystal, 32Khz.
And right after the setting applied, the chip is no longer be able to be programmed.

So how i can achieve to use the 32Khz crystal as my input in TOSC1 and TOSC2?
The datasheet shows that it is possible to run the 32KHz crystal at TOSC1 and
TOSC2 pins with workaround adding 2 caps of 22pF.

The timer2 prescaler set to /128 so a second period (1Hz) could be achieved, with
only set the TCNT2 = 0 (the timer2 counter) and the TIMSK |= (1 << TOEI2)
(the timer2 overflow interrupt). And it works pretty well.

OCR2 = (1 / desiredHertz) / (( N * 1 / Frequence_osc ) -1 )

desiredHertz = 1Hz
N (prescaler) = 128
Frequence_osc = 32.768Hz
Then, (1 / 1) / (( 128 * 1 / 32768) - 1) = 255. TIMSK is set to TOEI2, timer2 overflow.
Since timer2 is 8bit timer than the maximum count is 255 which is exact to our calculation
above.

Snippet code:
#define F_CPU   1000000UL

#include 
#include 
#include 
#include 

ISR ( TIMER2_OVF_vect )
{
    // Wake up and enter timer2 overflow interrupt
    PORTB ^= (1 << PB1);
}

int
main(void)
{
    // Enable INT0, set the PD2 to input
    DDRD &= ~(1 << PD2);

    // Enable puullup
    PORTD |= (1 << PD2);

    // Set the PB1 as output
    DDRB |= (1 << PB1);

    // Set the timer2 to asynchronous mode
    ASSR |= (1 << AS2);

    // Shutdown ADC
    // To save more power
    ACSR |= (1 << ACD);
    ADCSRA &= ~(1 << ADEN) ;

    // Sleep enable
    MCUCR |= (1 << SE );

    // Set to power-save mode
    MCUCR |= (0 << SM2 ) | ( 1 << SM1 ) | (1 << SM0 );

    // Set the INT0 to wakeup at any edge of signal,
    // whether rising or falling edge
    MCUCR |= (0 << ISC01) | (0 << ISC00); //Any Edge

    // Enable the INTO
    GICR |= (1 << INT0);

    // 1Hz
    // Prescaler set to 128, as the crystal input is 32.768KHz
    TCCR2 |= (1 << CS22 ) | (0 << CS21 ) | (1 << CS20);

    // Disable all timer interrupt
    // if is not disabled and interrupt is triggered the CPU
    // could be hung. Since we prematurely enter the power-save mode
    // without setting the proper timer2 interrupt to wake-up the
    // CPU at 1Hz period
    TIMSK = 0;
    TCNT2 = 0;

    // Enable global interrupt, which the INT0
    sei();

    // Enable the timer interrupt
    // Set to Timer2 overflow
    TIMSK |= (1 << TOIE2 );

    // Make sure the asynchronous mode is not busy
    // setup and ready
    while ( ASSR & (1 << TCR2UB ) ){ /* Loop if Async is busy */}

    // We are ready to enter the sleep of power-save mode
    sleep_cpu();
    // When the CPU is wake up the process will continue at this point

    while(1)
    {
        // Make sure the ASSR or the asynchronous mode is not busy
        while ( ASSR & (1 << TCR2UB ) ){/* Loop if Async is busy */}

        // We are ready to enter the sleep of power-save mode
        sleep_cpu();
        // When the CPU is wake up the process will continue 
        // at this point
    }
}

The next level, i measured the amperage, around ~0.20mA during power-save mode,
while the datasheet says should be around 15uA. Well it is time to take a peek
to the datasheet. You really have to get yourself used to read datasheet ^^.
The circuit was arranged with 1MHz of internal oscillator, 32KHz crystal at TOSC1/2
paired with 2 caps of 22pF and 5Vcc. The ADC, BOD, Watchdog. are disabled
at the fuses setting, use engbedded fuse calculator.


I have review a lot about the atmega8 then why i am saying that use atmega16/32 instead
of atmega8? Well my focus the power consumption and atmega16A, the datasheet
shows Icc ~11.5uA during power-save mode and i got 11uA from my measurement.
When the atmega16 to use 3V, the datasheet shows ~7uA.


I personally prefer to use atmega8535A/16A/32A in my future projects.
Though the size of Atmega16A is ~4 times larger than atmega8 but the trade-off
is fair enough. I get significant power save device therefore battery enabled device at 3Vcc.


Thursday, October 24, 2013

Linux Fedora 19 NVidia Brightness setting

NVidia in Fedora 14 is unable to bet set via GNOME desktop. The workaround was
# echo 5 > /sys/class/backlight/acpi_video0/brightness
And to retained the setting, simply add the command line in the /etc/rc.local

Now I am in Fedora 19 64bit with kernel updated and NVidia installed. The boot
parameter already set to acpi_mode=vendor. Attempting the same workaround but
The result is not like the expected. Right after the # echo 5 > ... The brightness stays
at 10, nothing happen.

Accidentally suspend (standby) my laptop session and resume runs the new
brightness setting. So the conclusion is that the brightness setting will take
effect if you somehow shutdown the display and turn it back on. The way that
I know is to pm-suspend or going to standby mode.

Hope it helps.

Tuesday, October 22, 2013

Linux STM32F4-DISCOVERY STLINK-V2 not detected

Just got my STM32F4-DISCOVERY from ebay.com, was having hard time to work with.
Since i've plugged the USB mini cable to the board and my laptop. I've managed the CN3
jumpers are ON. The LED LD1 or the COM led was blinking the shinny yellow light.
Curious... ahah! Just found that my USB mini cable is not a data cable but a power cable
that bundled with my portable USB Powerbank.



Sunday, October 20, 2013

Linux openOCD build failed 'missing makeinfo texi' of stellaris launchpad, stm32FX discovery

Stuck while building the openOCD? And it stops with error building the docs?? Ouchh...
luckily got this. Just set --enable-maintainer-mode in the configure, also as my addition
is to install the texinfo ( I am using Fedora 19 64bit), actually got the tip somewhere
i forgot.

Before:

./configure --prefix=/usr --enable-maintainer-mode --enable-stlink --enable-ti-icdi

and it ends up with error building the docs of openOCD

After:

./configure --prefix=/usr --enable-maintainer-mode --enable-stlink --enable-ti-icdi
--enable-maintainer-mode

It works! And now to get the lm4flash to work, first you need to use the usb port next to the switch with
device -- debug label then switch it to debug mode. Test the sudo lm4flash blinky.bin.

It works like charm! Happy electrifying..

Friday, October 18, 2013

Linux Fedora 19 Nautilus shoot CPU 100%

My laptop's fan keep spinning in high rpm. Curious, i open up the system monitor, Nautilus
process is using the CPU to 100%. And i google found this,

$ killall nautilus

apparently it is a  known issued. Well this is a short article hope it helps.

Saturday, October 12, 2013

Linux Fedora 19 Oracle XE 11g Uninstall failed

I just upgrade my fedora 14 to Fedora 19. Done with some rsync and moving stuffs.
And here come the new Oracle XE 11 g installation process, finished and run smooth.
Heh?! So what was the problem? The problem is i've messed up with these 

impdp  system/system_password full=Y directory=DUMP_DIR 
 dumpfile=DB10G.dmp logfile=expdpDB10G1.log
impdp  system/system_password directory=DUMP_DIR 
 TABLE_EXISTS_ACTION=APPEND TABLES=FLOWS_FILES.WWV_FLOW_FILE_OBJECTS$ 
 dumpfile=DB10G2.dmp logfile=expdpDB10G1b.log



Which i followed from here which guide me to migrating the whole data
of my Oracle XE 10g. The full expdp of XE 10g did not include the whole
users tablespace, forgive me i am not that familiar with this thing,
so to copy or just create new tablespace or something were not taken
or even tested. Since i've frustated Ctrl-C the import process and
ended up with broken database. The Oracle refused to start! Ok calm down 
lets just uninstall, i did the sudo yum remove oracle-xe...rpm, done!

Fresh localinstall with yum and the prompt says that i have to execute the init
script as root. Script run by root and set the port and it shows configuring the
database and done and load the listener and finish! I open my sqlplus / as sysdba.
It shows me idle connection prompt and hey! I did a little test, i queried

sql> select * from cat;

And it returns me that the oracle is shutting down in progress. I run this query

sql> shutdown immediate;

Run well, and then

sql> startup mount;

Unable to mount since there is no controll file??? Alright did some googling and
found that the previous yum uninstall did not remove the post-init oracle generated
files. What and where are the files you could have a very good reference here.
I run these commands:

rm -Rf /usr/lib/oracle/xe
rm -Rf /etc/oratab
rm -Rf /etc/init.d/oracle-xe
rm -Rf /etc/sysconfig/oracle-xe

Then yum uninstall and re-install and now everything work as expected. Hope
that this article could help you to solve the same problem. Actually you could
do re-init or re-configure without reinstall the oracle-xe. Just remove these files
/etc/sysconfig/oracle-xe, /etc/oratab. And as root run /etc/init.d/oracle-xe configure.
The controll files and database will be refreshed.

Hope it helps.

Monday, September 30, 2013

Western Digital External Harddisk: Spinning up Disk Not Responding

My external harddisk acting weird today. And yes my external harddisk is Western Digital.
Here is the full description of my device, right after the 4th attempt that finally worked.













At my 1st attempt this is the screenshot, dmesg shows me that the system trying to spin up
the disk, but failed! I attached the USB cable to one of my laptop 
(LENOVO Y450, Fedora 14 64bit, kernel is 2.6.35.14-106.fc14.x86_64) usb port.













This the 2nd and i attached the USB cable of my WD external hdd to another port.













Yet it failed. And this is the 3rd attempt, but it showed me that spin up timeout..
Humm.. that was something new.













And at last the 4th is the very first image, at the top, of this article. The system
finally spin up the disk.

Just a personall assumption that my WD Ext-hdd needs warmed-up, since 
it has been 3 months sit inside my drawer. 

Hope this helps..

Sunday, September 29, 2013

Microcontroller connect a Relay

Ohh.. Dear, look! I got an idea to automate my water tap valve.
I got this crazy goal in mind, just too lazy to pour water to
my many fruit-on-pot-plants. After did some research, **Ding**.

I now come up with an abstract sketch, a microcontroller (Atmega8)
a relay (HKE 12V, NC-NO, 230V 6A, 230V 10A) and 2 electric PVC
valves (SANKING, 3/4" - 3/4") would do, well if i am not too lazy,
the 2 valves could be minimized to just 1. But lets skip the
optimization, since it is an evil, so i bought these materials with
the diode and transistor refer to this.
Actually i already have this Atmega8 along with the USB Programmer

the K-125R.

Ok first-thing-first, i have to drive the relay... **BUMP**
The problem begins, i am a total newbie in electronic things.
And got this huge goal, never mind.. calm down.. internet is
a best friend.

Finally got this as my reference. Lets do it! i connect the coil
points on my breadboard. This is how i arrange it in minimum
configuration just to see, actually hear, the relay switching..



**Click** Yeah! As you could see in the video that the relay
still active even that i switch my DC power source down to 4.5V
and not below that. So i think the relay would still working
if the power source set to 9V as my atmega8 requires 9V to
operate (well the LM7805 step it down to 5V though)

*) Edited :
I know this is an old post. But to clarify that the Relay does not work
if i power it up with 4.5V of high-end power supply. In the video
my PSU was set to 4.5V but when i check it with my digital meter
it shows 8.1V, so this is PSU failure.

Well that is the relay part, will share the experience to
drive the electric valve in the next article.

Happy electrifying... ^^

Thursday, September 19, 2013

VIM: Project.vim and ctags

Strange i could not found my function definition...?
Ahh.. I see, the function declaration is in a file with *.inc
extention, PHP is what i am using and working with
frontaccounting (to be short, will called as FA) .
So i search my FA a .vimprj/ inside the working directory.
Aha! No .vimprj/ that is why no tags file created. So i create
the .vimprj/ on the root directory of FA, remember the
"/" trailing of .vimprj to remind you that its a directory.

So i type,
:Project
And new separated window created inside my GVim.
I hit my keyboard with \C (create new project) and
supplied 3 inputs, the root of my FA project directory,
the CD=. and the filters "*.php *.inc *.html *.php*".
After a few seconds the project tree created. Lets get
to work.

Bla bla bla, what! I hit my keyboard with g] which means
"go to declaration", in this case is the declaration of
a function that where my current cursor pointing at,
nothing happened. So tried to
:IndexerDebugLog
Nahh... the debug message throws something like
... C++-Kind=... Well its a PHP scope why c++ being
detected by my ctags? Ok, i realized that my .vimprj/
still has nothing inside so i add a file called mysettings.vim

And copy paste of my other PHP project mysettings.vim,

let g:indexer_disableCtagsWarning = 1

let g:indexer_ctagsCommandLineOptions = " --exclude='*.sql' --exclude='*.js'
--exclude='*.css' --PHP-kinds=+cf --fields=+iaS --extra=+q
--regex-PHP='/(abstract class |class ) ([^ ]*)/\\1/c/'
--regex-PHP='/interface ([^ ]*)/\\1/c/'
--regex-PHP='/(public |static |abstract |protected |private )+function.*([^ ()]*)/\\2/f/'
--regex-PHP='/define[(].([a-zA-Z0-9_]*)/\\1/f/' "

Save the file, done! I restarted GVim, and hit g] at my function name.
Arrghhh.. nothing happened, again??? And i hit
:IndexerDebugInfo
Still it showed me C++-kind, let me think.. After several googling
and "man ctags" i ended up with

let g:indexer_disableCtagsWarning = 1

let g:indexer_ctagsCommandLineOptions = " --langdef=PHP --langmap=PHP:.inc
--exclude='*.sql' --exclude='*.js' --exclude='*.css'
--PHP-kinds=+cf --fields=+iaS --extra=+q
--regex-PHP='/(abstract class |class ) ([^ ]*)/\\1/c/'
--regex-PHP='/interface ([^ ]*)/\\1/c/'
--regex-PHP='/(public |static |abstract |protected |private )+function.*([^ ()]*)/\\2/f/'
--regex-PHP='/define[(].([a-zA-Z0-9_]*)/\\1/f/' -h '.inc' "

I've added with langmap, langdef and -h option. Save and restart my
GVim, hurray.. it works my function declaration inside *.inc is now
there in the ~/.vimprojects_tag/frontaccounting. And if i g] it jumps
to the source file where function declaration is in.

Happy Vim-ing..

Tuesday, July 30, 2013

Flask-SQLAlchemy, Mysql, Error #1005: Can't create table (errno: 105)?

Got Error #1005: Can't create table (errno: 105)?
I got the error when the Flask-SQLAlchemy tries
to init_db() of my models.py.

Well here is the solution you could check. That
you have to do is to create index  for the reference
ForeignKey column.

Hope It helps

MySQL, InnoDB: Operating system error number 13 in a file operation

Yeahh, it took me about an hour to two to figure it out.
I am using LAMPP 1.7 (old yes but i just personally decided
to use it) in Fedora14 64bit.
 
The steps were:
1. Check the error in your /opt/lampp/var/mysql/.err
    ** it is assumed that you copy the extracted xampp-linux-xxx.tar.gz in
         your /opt
2. What got from #1

130730 13:12:59 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
130730 13:12:59  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name /opt/lampp/var/mysql/ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.

3. And then i just chmod 777 -Rf /opt/lampp/var/mysql
4. Problem solved and hopefully not to add a new one :)

Hope it helps.

Thursday, July 18, 2013

How gVim saved my day

Hi, in this article i personally record how gVim come very handy
to help me to filter out a CSV file.

Steps:

# to get rid the header
1.  :g/^Sa...ka,,,,*/d
2.  :g/^"General Ledger Detail",,,,*/d
3.  :g/^"From 01 Jan .*",,,,*/d
4.  :g/^"ACC.* Report",,,,*/d
4.  :g/^"Printed.*",,,,*/d
5.  :g/^"Page.*",,,*/d
6.  :g/^,,,*/d

To use :g/regexRules/d <-- and="" d="" delete="" find="" is="" p="" to="">
Hope it helps.



Oracle Split String or Query Result

Hi, just found this Oracle split string. It saves me alot.
Since the first thing came into my head is like:
1. Query and fetch result.
2. Explode or split the result (i use PHP).
3. Perform array search.
Those are computational expensive due to large records.


Hope it helps.

Monday, April 8, 2013

Codeigniter 2 Template Render Doubling

I've been using This Template in my CI 2 app heavily.
And suddenly something happens very awkward today.
Everything seems rendered twice.

I've tried to var_dump in the Template.php line 549,
it returned me:
array
  '_scripts' => boolean false
  '_styles' => boolean false
  'header' => boolean false
  'content' => boolean false
  'footer' => string ' ' (length=1)
array
  '_scripts' => boolean false
  '_styles' => boolean false
  'header' => boolean false
  'content' => boolean false
  'footer' => string ' ' (length=1)

Then i add debug_backtrace(), a very handy func,
right before the var_dump. I was shocked since
render() func was called in one of my model,
was accidentally pasted.

Hope it helps

Flask SQLAlchemy FormAlchemy

Finally ericflo has done a great work, as the integration
reference, and hosted it at github. I've spent almost 2-3 hours
to search how Flask request.form data to be sent
into formAlchemy. Make sure you want to visit his great
work, 72 lines of a reference won't hurt you :P

Hope it helps

Wednesday, April 3, 2013

Codeigniter 2.1.2 Disallowed Key Characters

Following up this thread well it just happened that my ci 2
application won't load up and only showed me "Disallowed
Key Characters" on screen. Arrghhhh... hate to say this but
the only way i got this fixed is to modify the core!

Go to your %system/core/Input.php and find
"if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))".
Modify it to
"if ( ! preg_match("/^[a-z0-9:_\/-\|]+$/i", $str))".
It stops showing me the disallowed key error.

Anyway right before i touched the core, had tried
to add the "|" char in to %application/config/config.php.
But nothing seems to be happened and error screen
still showing.

Hope it helps.

Thursday, March 21, 2013

Flask-Blog

Just found this cool project in Github. It uses python-markdown
which something i've be doing recently. Well rather than
reinventing-the-wheel now i've forked the project and completing
the project. Why Markdown? Well you should take a look at it.

Saturday, March 16, 2013

To Use Document.ly

Well i hate to write this down but finally i found a tool
that i could write something in markdown.
I've started similar project of my own to create a blog
using Flask and has markdown embedded editor or its Github.

I found document.ly from Hackers Newsletter many thanks
to Kale Davis of his great work to keep me and others up-to-date. Hope it helps.

Wednesday, March 13, 2013

Annoying Blackberry with "JVM 102 error"

Spending 4 hours just to troubleshoot "JVM 102 Error"
really time wasting and dumb. For almost a year an app,
twitter, sitting in my bb 8900. Never had issue until
this morning. Frequently my device got "white-screen"
and have to release the battery as the "hard-reset".
Everything went well before not this morning. The screen
showed me "JVM 102 error" and a reset button.

Let me think... uhmmm maybe it's just another "bb"
things. Alright just take off the battery and put back
everything will be fine. Argh!! 2 times of reset and took
me almost 15 minutes to get the screen with
"JVM 102 error" and a reset button again! What on
earth is happening?? Ok calm down just do the "hard-reset"
after bed-rest my device for about 10 minutes. Maybe it's
just overheat things well that was in my head. Untill the
"JVM 102 error" shows up again.

Spending hours to search on the web and do the JL_Cmder
things and BBsak didn't help much. Well at least after
done severals trial and error just got these receipt to
get off the error:

1. You must have windowsOS.
    (i used win7 under evaluation)
2. Install the BB device manager.
3. Make sure you run the device manager and
    it goes background.
4. Download BBsak
5. Run BBsak and then plug the USB cable to
    your device.
6. I experienced that the bootup must reach the
    error point to get the device recognition working.
    And 2 computer icons connecting will show up
    in your device.
7. Choose the "misc" tab of BBsak and check the eventLog
8. Use this guide to help you out. For looking up
    which *.cod files causing the error message.
    Remove em!!
9. After the removal your device will auto reset.
    Hopefully you will have your device finish the booting
    so you dont have to wipe it.
10. Backup your device frequently as final tip.

If anyone of you have the experience the same error
and could fixed it up in linuxOS please please i beg you
to share.

Hope it helps

Static HTML files in GAE

I've just finished one of my project' reStructuredText
documentation. Got it build in HTML and ready to
deploy in GAE. With a little searching on the web
found static HTML files how to in GAE, saved my day.

Hope it helps.. Uhmmm why getting shorter
in my article? Just trying to write everything up.
All these things are like blazing cloud in my head
and have to got them all out in the blog :D.
So let the blog memorize and share them.

Monday, March 4, 2013

Python cx_Oracle not working in Cronjob

cx_Oracle error in Cronjob execution. I've found that
adding environment variables in the crontab works
like charm. Steps to check if the cx_Oracle work:
test_cx_oracle.py
try:
import cx_Oracle
print 'cx_Oracle works'
except:
print 'cx_Oracle error'
In your crontab add,
 * * * * * python /path/to/test_cx_oracle.py.
Notice that the cron will execute every minute.
You will received error notification in your email
$ tail /var/spool/mail/oracle

Edit your cron:

ORACLE_HOME = '/path/to/db_1'
LD_LIBRARY_PATH = '/path/to/db_1/lib:/lib:/usr/lib'

* * * * * python /path/to/test_cx_oracle.py

Save and exit, wait for cron to fire then check your
mail again. You should received 'cx_Oracle works'
notification. Hope it helps.

Saturday, March 2, 2013

Installing OpenERP 7

Hello OpenERP 7, well i had downloaded a week ago and
just got time to install it. I had installed OpenERP since
version 5 and then 6. I had them installed in a virtualenv.
They are almost have typical `problems`. And those are:

1. psycopg , usually because of the pg_config
    not found, default is in /etc/pg_config, in my case was
    in /opt/PostgreSQL/8.4/bin/pg_config

2. PyChart, well just google and you will find it in
    sourceforge. Download it then :
    $ python setup.py build ; python setup.py install

3. pyparsing, occured in version 7, where current version
    is 2.0, as this article created, which only works with
    python 3. You will need to install it manually
    $ pip install pyparsing==1.5.7

Hope it helps. Happy OpenERP 7.