demo sample to drive PCU9955 and PCA9629

Dependencies:   mbed I2C_slaves PCU9669 parallel_bus

Fork of mini_board_PCU9669 by InetrfaceProducts NXP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.c Source File

main.c

00001 /** A demo code for PCU9669, PCU9955 and PCA9629
00002  *
00003  *  @author  Tedd OKANO, NXP Semiconductors
00004  *  @version 1.0
00005  *  @date    26-Oct-2012
00006  *
00007  *  Released under the MIT License: http://mbed.org/license/mit
00008  *
00009  *    An operation sample of PCU9669 I2C bus controller.
00010  *  The mbed accesses the PCU9669's parallel port (8 bit address and 8 bit data) using bit-banging.
00011  *  The bit-banging is poerformed by PortInOut function of mbed library.
00012  *
00013  *    DEMO CODE version 
00014  *       v0.5 written on 13-Oct-2011
00015  *       v1.0 poerted to PCU9669 sample code API
00016  *
00017  *  Simple code sample for PCU9669 is available on 
00018  *      http://mbed.org/users/nxp_ip/code/mini_board_PCU9669/
00019  */
00020 
00021 
00022 #include "transfer_manager.h"
00023 #include "PCU9669_access.h"
00024 #include "hardware_abs.h"
00025 #include "PCx9955_reg.h"
00026 #include "PCA9629_reg.h"
00027 #include "demo_patterns.h"
00028 
00029 #include "mbed.h"
00030 
00031 #define     TICKER_INTERVAL         0.008
00032 
00033 #define     RESET_PULSE_WIDTH_US    10  //  Minimum pulse width is 4us for PCU9669
00034 #define     RESET_RECOVERY_US       1000
00035 
00036 Ticker  op;
00037 
00038 
00039 void operation( void );
00040 void led_action( int count );
00041 void motor_action( int count );
00042 void interrupt_handler( void );
00043 void init_slave_devices( void );
00044 void single_byte_write_into_a_PCA9629( char ch, char i2c_addr, char reg_addr, char val );
00045 void set_one_turn_on_each_end( void );
00046 
00047 int main() {
00048     printf( "\r\nPCU9669 simple demo program on mbed (16 device check)\r\n  build : %s (UTC), %s \r\n\r\n", __TIME__, __DATE__ );
00049 
00050     hardware_initialize();          //  initializing bit-banging parallel port
00051     reset( RESET_PULSE_WIDTH_US, RESET_RECOVERY_US );  //  assert hardware /RESET sgnal
00052 
00053     if ( start_bus_controller( PCU9669_ID ) )   //  wait the bus controller ready and check chip ID
00054         return 1;
00055 
00056     write_ch_register( CH_FM_PLUS, INTMSK, 0x30 );  //  set bus controller to ignore NAK return from Fm+ slaves
00057     install_ISR( &interrupt_handler );              //  interrupt service routine install
00058 
00059     init_slave_devices();           //  set all slaves' all registers and configure buffer for the operation
00060 
00061     op.attach( &operation, TICKER_INTERVAL );
00062 
00063     while ( 1 ) {
00064     }
00065 }
00066 
00067 void operation( void ) {
00068     pattern_update( gCount );
00069 
00070     motor_action( gCount );      //  motor operation
00071     led_action( gCount );        //  LED operation
00072 
00073     gCount++;
00074 }
00075 
00076 
00077 void led_action( int count ) {
00078     buffer_overwrite( CH_UFM1, 0, 1, gLEDs + N_LED_PER_BOARD * 0, N_LED_PER_BOARD );
00079     buffer_overwrite( CH_UFM1, 1, 1, gLEDs + N_LED_PER_BOARD * 1, N_LED_PER_BOARD );
00080     buffer_overwrite( CH_UFM1, 2, 1, gLEDs + N_LED_PER_BOARD * 2, N_LED_PER_BOARD );
00081     buffer_overwrite( CH_UFM1, 3, 1, gLEDs + N_LED_PER_BOARD * 3, N_LED_PER_BOARD );
00082     buffer_overwrite( CH_UFM2, 0, 1, gLEDs + N_LED_PER_BOARD * 4, N_LED_PER_BOARD );
00083     buffer_overwrite( CH_UFM2, 1, 1, gLEDs + N_LED_PER_BOARD * 5, N_LED_PER_BOARD );
00084     buffer_overwrite( CH_UFM2, 2, 1, gLEDs + N_LED_PER_BOARD * 6, N_LED_PER_BOARD );
00085     buffer_overwrite( CH_UFM2, 3, 1, gLEDs + N_LED_PER_BOARD * 7, N_LED_PER_BOARD );
00086 
00087     start( CH_UFM1 );
00088     start( CH_UFM2 );
00089 }
00090 
00091 
00092 void motor_action( int count ) {
00093 #if 0
00094     static char     mot_cntl_order[ 5 ]    = {
00095         MOT_ADDR5, MOT_ADDR6, MOT_ADDR7, MOT_ADDR8, MOT_ADDR9
00096     };
00097     static char     motor_count     = 0;
00098 
00099     if ( !(count & 0x1F) ) {
00100         single_byte_write_into_a_PCA9629( CH_FM_PLUS, mot_cntl_order[ motor_count % 5 ], 0x26, 0x80 | ((motor_count / 5) & 0x1) );
00101         motor_count++;
00102     }
00103 #endif
00104 
00105 }
00106 
00107 void interrupt_handler( void ) {
00108     char    global_status;
00109     char    channel_status;
00110 
00111     global_status  = read_data( CTRLSTATUS );
00112 
00113     if ( global_status & 0x01 ) {  //  ch0
00114         channel_status  = read_ch_register( 0, CHSTATUS );
00115     }
00116     if ( global_status & 0x02 ) {
00117         channel_status  = read_ch_register( 1, CHSTATUS );
00118     }
00119     if ( global_status & 0x04 ) {
00120         channel_status  = read_ch_register( 2, CHSTATUS );
00121     }
00122 //    printf( "ISR channel_status 0x%02X\r\n", channel_status );
00123 }