demo sample to drive PCU9955 and PCA9629

Dependencies:   mbed I2C_slaves PCU9669 parallel_bus

Fork of mini_board_PCU9669 by InetrfaceProducts NXP

What is this?

This is a sample code to operate PCU9955 (16ch constant-current LED controller) and PCA9629 (intelligent stepper motor controller) through PCU9669 (3 channels (UltraFast mode * 2ch, FastModePlus *1ch) I2C bus controller).

This demo is written based on mini_board_PCU9669 sample code library and its API.
http://mbed.org/users/nxp_ip/code/mini_board_PCU9669/

Demo will shows how the LED controllers and stepper motor controllers works.
It uses a mini_board_PCU9669 board with mbed, 8 of PCU9955s and 5 PCA9629s.

/media/uploads/nxp_ip/dsc_0414ss.png
Demo setup
(left-top: PCU9955 boards, left-bottom: mini-board PCU9669 with mbed, right: PCA9629 x5 board)

/media/uploads/nxp_ip/demo-config-ss.png
Board connections and device addresses

Reference:

User manual of PCU9669 demo board: Mini board PCU9669

http://www.nxp.com/documents/user_manual/UM10580.pdf

sample code : mbed programs

Import programmini_board_PCU9669

mini board PCU9669 (and PCA9665) sample code

Import programPCA9955_Hello

PCA9955 16 channel current drive(sink) LED driver sample code

Import programPCA9955_simple

very simple sample code for PCA9955 (16 channel current control LED driver)

Import programPCA9629_Hello

Sample code for PCA9629 operation

device infomation

PCU9669 (Parallel bus to 1 channel Fm+ and 2 channel UFm I2C-bus controller)
PCU9955 (16-channel UFm I²C-bus 57 mA constant current LED driver)
PCA9955 (16-channel Fm+ I²C-bus 57 mA constant current LED driver)
PCU9629 (Fm+ I2C-bus stepper motor controller)

Revision:
20:a266fa588bd8
Parent:
17:20837a1676d4
--- a/main_PCU9669.c	Tue Aug 28 00:59:16 2012 +0000
+++ b/main_PCU9669.c	Fri Oct 26 05:59:55 2012 +0000
@@ -1,153 +1,107 @@
-/** A sample code for "mini board PCU9669/PCA9665"
+/** A demo code for PCU9669, PCU9955 and PCA9629
  *
- *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
+ *  @author  Tedd OKANO, NXP Semiconductors
  *  @version 1.0
- *  @date    26-Mar-2012
- * 
+ *  @date    26-Oct-2012
+ *
  *  Released under the MIT License: http://mbed.org/license/mit
  *
- *  An operation sample of PCU9669/PCA9665 I2C bus controller.
- *  The mbed accesses the bus controller's parallel port (8/2 bit address and 8 bit data) by bit-banging.
+ *    An operation sample of PCU9669 I2C bus controller.
+ *  The mbed accesses the PCU9669's parallel port (8 bit address and 8 bit data) using bit-banging.
  *  The bit-banging is poerformed by PortInOut function of mbed library.
  *
- *    To make the code porting easier, all codes are partitioned into layers to abstract other parts.
- *    The mbed specific parts are concentrated in lowest layer: "hardware_abs.*".
- *    This module may need to be modified for the porting.
- *
- *    All other upper layers are writen in standard-C.
+ *    DEMO CODE version 
+ *       v0.5 written on 13-Oct-2011
+ *       v1.0 poerted to PCU9669 sample code API
  *
- *    base code is written from 05-Sep-2011 to 09-Sep-2011.
- *    And demo code has been build on 11-Sep-2011.
- *    Debug and code adjustment has been done on 08-Sep-2011.
- *    Small sanitization for main.cpp. All mbed related codes are moved in to "hardware_abs.*". 13-Oct-2011
- *    hardware_abs are moved into parallel_bus library folder, 3 LED driver operation sample 13-Feb.-2012
- *    PCU9669 and PCA9665 codes are packed in a project 14-Feb-2012.
- *
- *    Before builidng the code, please edit the file mini_board_PCU9669/config.h
- *    Un-comment the target name what you want to target.
- */
-
-/** @note Application layer module of PCU9669
- *
- *  This code is made to explain basic PCU9669 operation.
- *  And also, it demonstrates 3 channels of I2C operation with several slaves on those buses.
- *
- *  The ch0 is an Fm+ bus that has a PCA9955 as slave device
- *  The ch1 and ch2 are UFm buses. Each bus has a PCU9955 as slave devices.
+ *  Simple code sample for PCU9669 is available on 
+ *      http://mbed.org/users/nxp_ip/code/mini_board_PCU9669/
  */
 
-#include "config.h"
+
+#include "transfer_manager.h"
+#include "PCU9669_access.h"
 #include "hardware_abs.h"
-#include "transfer_manager.h"   //  abstracting the access of PCU9669 internal buffer
-#include "PCU9669_access.h"     //  PCU9669 chip access interface
-#include "PCx9955_reg.h"        //  slave specific definitions
+#include "PCx9955_reg.h"
 #include "PCA9629_reg.h"
-#include "utility.h"            //  
-//#include "mbed.h"   //  this header is required only when printf() is used.
-
-#if defined( CODE_FOR_PCU9669 ) || defined( CODE_FOR_PCA9663 )
-
-#ifdef  CODE_FOR_PCU9669
-#define     TARGET_CHIP_ID  PCU9669_ID
-#endif
-
-#ifdef  CODE_FOR_PCA9663
-#define     TARGET_CHIP_ID  PCA9663_ID
-#endif
+#include "demo_patterns.h"
 
-#define RESET_PULSE_WIDTH_US    10  //  Minimum pulse width is 4us for PCU9669
-#define RESET_RECOVERY_US       1000
+#include "mbed.h"
 
-/**
- *  register settings for PCx9955
- */
+#define     TICKER_INTERVAL         0.008
 
-char  PCx9955_reg_data[] = {
-    0x80,                                       //  Strat register address with AutoIncrement bit
-    0x00, 0x05,                                 //  MODE1, MODE2
-    0xAA, 0xAA, 0xAA, 0xAA,                     //  LEDOUT[3:0]
-    0x80, 0x00,                                 //  GRPPWM, GRPFREQ
-    PWM_INIT,  PWM_INIT,  PWM_INIT,  PWM_INIT,  //  PWM[3:0]
-    PWM_INIT,  PWM_INIT,  PWM_INIT,  PWM_INIT,  //  PWM[7:4]
-    PWM_INIT,  PWM_INIT,  PWM_INIT,  PWM_INIT,  //  PWM[11:8]
-    PWM_INIT,  PWM_INIT,  PWM_INIT,  PWM_INIT,  //  PWM[15:12]
-    IREF_INIT, IREF_INIT, IREF_INIT, IREF_INIT, //  IREF[3:0]
-    IREF_INIT, IREF_INIT, IREF_INIT, IREF_INIT, //  IREF[7:4]
-    IREF_INIT, IREF_INIT, IREF_INIT, IREF_INIT, //  IREF[11:8]
-    IREF_INIT, IREF_INIT, IREF_INIT, IREF_INIT, //  IREF[15:12]
-    0x08                                        //  OFFSET: 1uS offsets
-};
+#define     RESET_PULSE_WIDTH_US    10  //  Minimum pulse width is 4us for PCU9669
+#define     RESET_RECOVERY_US       1000
+
+Ticker  op;
 
 
-char  PCx9955_reg_read_start_address    = 0x80;
-
-char  read_buffer[ 41 ];
-
-transaction     ufm_transactions[]   =   {
-    { PCx9955_ADDR0, PCx9955_reg_data, sizeof( PCx9955_reg_data ) }
-};
-
-transaction     fm_plus_transactions[]   =   {
-    { PCx9955_ADDR0, PCx9955_reg_data, sizeof( PCx9955_reg_data ) },
-    { PCx9955_ADDR0, &PCx9955_reg_read_start_address, 1 },
-    { PCx9955_ADDR0 | 0x01, read_buffer, sizeof( read_buffer ) }
-};
-
-char    read_done   = 0;
-
+void operation( void );
+void led_action( int count );
+void motor_action( int count );
 void interrupt_handler( void );
+void init_slave_devices( void );
+void single_byte_write_into_a_PCA9629( char ch, char i2c_addr, char reg_addr, char val );
+void set_one_turn_on_each_end( void );
 
 int main() {
-    char    clear[ 16 ] = { 0 };
-    int     count       = 0;
-    int     i;
-    int     j;
-
-//    printf( "\r\nPCU9669 simple demo program on mbed\r\n  build : %s (UTC), %s \r\n\r\n", __TIME__, __DATE__ );
+    printf( "\r\nPCU9669 simple demo program on mbed (16 device check)\r\n  build : %s (UTC), %s \r\n\r\n", __TIME__, __DATE__ );
 
     hardware_initialize();          //  initializing bit-banging parallel port
     reset( RESET_PULSE_WIDTH_US, RESET_RECOVERY_US );  //  assert hardware /RESET sgnal
 
-    if ( start_bus_controller( TARGET_CHIP_ID ) )   //  wait the bus controller ready and check chip ID
+    if ( start_bus_controller( PCU9669_ID ) )   //  wait the bus controller ready and check chip ID
         return 1;
 
     write_ch_register( CH_FM_PLUS, INTMSK, 0x30 );  //  set bus controller to ignore NAK return from Fm+ slaves
     install_ISR( &interrupt_handler );              //  interrupt service routine install
 
-    setup_transfer( CH_FM_PLUS, fm_plus_transactions, sizeof( fm_plus_transactions ) / sizeof( transaction ) );
-    setup_transfer( CH_UFM1, ufm_transactions, sizeof( ufm_transactions ) / sizeof( transaction ) );
-    setup_transfer( CH_UFM2, ufm_transactions, sizeof( ufm_transactions ) / sizeof( transaction ) );
+    init_slave_devices();           //  set all slaves' all registers and configure buffer for the operation
+
+    op.attach( &operation, TICKER_INTERVAL );
 
     while ( 1 ) {
-        for ( i = 0; i < 16; i++ ) {
-            for ( j = 0; j < 256; j += 4 ) {
-                buffer_overwrite( CH_FM_PLUS, 0, 9 + i, (char *)&j, 1 );
-                buffer_overwrite( CH_UFM1,    0, 9 + i, (char *)&j, 1 );
-                buffer_overwrite( CH_UFM2,    0, 9 + i, (char *)&j, 1 );
+    }
+}
+
+void operation( void ) {
+    pattern_update( gCount );
 
-                read_done   = 0;
-
-                set_n_of_transaction( CH_FM_PLUS, (sizeof( fm_plus_transactions ) / sizeof( transaction )) - ((count % 256) ? 1 : 0) );
+    motor_action( gCount );      //  motor operation
+    led_action( gCount );        //  LED operation
 
-                start( CH_FM_PLUS );
-                start( CH_UFM1 );
-                start( CH_UFM2 );
+    gCount++;
+}
+
 
-                wait_sec( 0.01 );
-                count++;
+void led_action( int count ) {
+    buffer_overwrite( CH_UFM1, 0, 1, gLEDs + N_LED_PER_BOARD * 0, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM1, 1, 1, gLEDs + N_LED_PER_BOARD * 1, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM1, 2, 1, gLEDs + N_LED_PER_BOARD * 2, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM1, 3, 1, gLEDs + N_LED_PER_BOARD * 3, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM2, 0, 1, gLEDs + N_LED_PER_BOARD * 4, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM2, 1, 1, gLEDs + N_LED_PER_BOARD * 5, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM2, 2, 1, gLEDs + N_LED_PER_BOARD * 6, N_LED_PER_BOARD );
+    buffer_overwrite( CH_UFM2, 3, 1, gLEDs + N_LED_PER_BOARD * 7, N_LED_PER_BOARD );
+
+    start( CH_UFM1 );
+    start( CH_UFM2 );
+}
+
+
+void motor_action( int count ) {
 #if 0
-                if ( read_done ) {
-                    buffer_read( CH_FM_PLUS, 2, 0, read_buffer, sizeof( read_buffer ) );
-                    dump_read_data( read_buffer, sizeof( read_buffer ) );
-                    read_done   = 0;
-                }
+    static char     mot_cntl_order[ 5 ]    = {
+        MOT_ADDR5, MOT_ADDR6, MOT_ADDR7, MOT_ADDR8, MOT_ADDR9
+    };
+    static char     motor_count     = 0;
+
+    if ( !(count & 0x1F) ) {
+        single_byte_write_into_a_PCA9629( CH_FM_PLUS, mot_cntl_order[ motor_count % 5 ], 0x26, 0x80 | ((motor_count / 5) & 0x1) );
+        motor_count++;
+    }
 #endif
-            }
-        }
-        buffer_overwrite( CH_FM_PLUS, 0, 9, clear, 16 );
-        buffer_overwrite( CH_UFM1,    0, 9, clear, 16 );
-        buffer_overwrite( CH_UFM2,    0, 9, clear, 16 );
-    }
+
 }
 
 void interrupt_handler( void ) {
@@ -158,9 +112,6 @@
 
     if ( global_status & 0x01 ) {  //  ch0
         channel_status  = read_ch_register( 0, CHSTATUS );
-
-        if ( channel_status & 0x80 )
-            read_done   = 1;
     }
     if ( global_status & 0x02 ) {
         channel_status  = read_ch_register( 1, CHSTATUS );
@@ -170,11 +121,3 @@
     }
 //    printf( "ISR channel_status 0x%02X\r\n", channel_status );
 }
-
-#endif  //  CODE_FOR_PCU9669
-
-
-
-
-
-