These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
I2C/Monitor/i2c_monitor.c@0:bf7b9fba3924, 2011-03-20 (annotated)
- Committer:
- frank26080115
- Date:
- Sun Mar 20 05:38:56 2011 +0000
- Revision:
- 0:bf7b9fba3924
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frank26080115 | 0:bf7b9fba3924 | 1 | /******************** (C) COPYRIGHT 2010 NXPSemiconductors ************ |
frank26080115 | 0:bf7b9fba3924 | 2 | * @file i2c_monitor.c |
frank26080115 | 0:bf7b9fba3924 | 3 | * @purpose This example describes how to uses I2C peripheral on LPC1768 |
frank26080115 | 0:bf7b9fba3924 | 4 | * in monitor mode. |
frank26080115 | 0:bf7b9fba3924 | 5 | * @version 1.0 |
frank26080115 | 0:bf7b9fba3924 | 6 | * @date 16. July. 2010 |
frank26080115 | 0:bf7b9fba3924 | 7 | * @author NXP MCU SW Application Team |
frank26080115 | 0:bf7b9fba3924 | 8 | *--------------------------------------------------------------------- |
frank26080115 | 0:bf7b9fba3924 | 9 | * Software that is described herein is for illustrative purposes only |
frank26080115 | 0:bf7b9fba3924 | 10 | * which provides customers with programming information regarding the |
frank26080115 | 0:bf7b9fba3924 | 11 | * products. This software is supplied "AS IS" without any warranties. |
frank26080115 | 0:bf7b9fba3924 | 12 | * NXP Semiconductors assumes no responsibility or liability for the |
frank26080115 | 0:bf7b9fba3924 | 13 | * use of the software, conveys no license or title under any patent, |
frank26080115 | 0:bf7b9fba3924 | 14 | * copyright, or mask work right to the product. NXP Semiconductors |
frank26080115 | 0:bf7b9fba3924 | 15 | * reserves the right to make changes in the software without |
frank26080115 | 0:bf7b9fba3924 | 16 | * notification. NXP Semiconductors also make no representation or |
frank26080115 | 0:bf7b9fba3924 | 17 | * warranty that such application will be suitable for the specified |
frank26080115 | 0:bf7b9fba3924 | 18 | * use without further testing or modification. |
frank26080115 | 0:bf7b9fba3924 | 19 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 20 | #include "lpc17xx_i2c.h" |
frank26080115 | 0:bf7b9fba3924 | 21 | #include "lpc17xx_libcfg.h" |
frank26080115 | 0:bf7b9fba3924 | 22 | #include "lpc17xx_pinsel.h" |
frank26080115 | 0:bf7b9fba3924 | 23 | #include "debug_frmwrk.h" |
frank26080115 | 0:bf7b9fba3924 | 24 | |
frank26080115 | 0:bf7b9fba3924 | 25 | /* Example group ----------------------------------------------------------- */ |
frank26080115 | 0:bf7b9fba3924 | 26 | /** @defgroup I2C_Monitor Monitor |
frank26080115 | 0:bf7b9fba3924 | 27 | * @ingroup I2C_Examples |
frank26080115 | 0:bf7b9fba3924 | 28 | * @{ |
frank26080115 | 0:bf7b9fba3924 | 29 | */ |
frank26080115 | 0:bf7b9fba3924 | 30 | |
frank26080115 | 0:bf7b9fba3924 | 31 | /************************** PRIVATE DEFINITIONS *************************/ |
frank26080115 | 0:bf7b9fba3924 | 32 | #define I2CDEV LPC_I2C0 |
frank26080115 | 0:bf7b9fba3924 | 33 | |
frank26080115 | 0:bf7b9fba3924 | 34 | /** Max buffer length */ |
frank26080115 | 0:bf7b9fba3924 | 35 | #define BUFFER_SIZE 0x80 |
frank26080115 | 0:bf7b9fba3924 | 36 | /************************** PRIVATE VARIABLES *************************/ |
frank26080115 | 0:bf7b9fba3924 | 37 | uint8_t menu1[] = |
frank26080115 | 0:bf7b9fba3924 | 38 | "********************************************************************************\n\r" |
frank26080115 | 0:bf7b9fba3924 | 39 | "Hello NXP Semiconductors \n\r" |
frank26080115 | 0:bf7b9fba3924 | 40 | "I2C monitor I2C bus demo \n\r" |
frank26080115 | 0:bf7b9fba3924 | 41 | "\t - MCU: LPC17xx \n\r" |
frank26080115 | 0:bf7b9fba3924 | 42 | "\t - Core: ARM Cortex-M3 \n\r" |
frank26080115 | 0:bf7b9fba3924 | 43 | "\t - This example describes how to uses I2C peripheral on LPC1768 \n\r" |
frank26080115 | 0:bf7b9fba3924 | 44 | "\t in monitor mode. \n\r" |
frank26080115 | 0:bf7b9fba3924 | 45 | "\t I2C0 monitors I2C data transfered on I2C bus. \n\r" |
frank26080115 | 0:bf7b9fba3924 | 46 | "\t UART0 (115200bps, 8 data bit, no parity, 1 stop bit, no flow control) \n\r" |
frank26080115 | 0:bf7b9fba3924 | 47 | "\t is used to display captured data. \n\r" |
frank26080115 | 0:bf7b9fba3924 | 48 | "********************************************************************************\n\r"; |
frank26080115 | 0:bf7b9fba3924 | 49 | uint8_t buffer[BUFFER_SIZE]; |
frank26080115 | 0:bf7b9fba3924 | 50 | BOOL_8 done=FALSE; |
frank26080115 | 0:bf7b9fba3924 | 51 | uint32_t count=0; |
frank26080115 | 0:bf7b9fba3924 | 52 | |
frank26080115 | 0:bf7b9fba3924 | 53 | /************************** PRIVATE FUNCTIONS *************************/ |
frank26080115 | 0:bf7b9fba3924 | 54 | void I2C0_IRQHandler(void); |
frank26080115 | 0:bf7b9fba3924 | 55 | void print_menu(void); |
frank26080115 | 0:bf7b9fba3924 | 56 | /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 57 | /*********************************************************************//** |
frank26080115 | 0:bf7b9fba3924 | 58 | * @brief Main I2C0 interrupt handler sub-routine |
frank26080115 | 0:bf7b9fba3924 | 59 | * @param[in] None |
frank26080115 | 0:bf7b9fba3924 | 60 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 61 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 62 | void I2C0_IRQHandler(void) |
frank26080115 | 0:bf7b9fba3924 | 63 | { |
frank26080115 | 0:bf7b9fba3924 | 64 | done = I2C_MonitorHandler(LPC_I2C0,buffer,count); |
frank26080115 | 0:bf7b9fba3924 | 65 | if(done) |
frank26080115 | 0:bf7b9fba3924 | 66 | { |
frank26080115 | 0:bf7b9fba3924 | 67 | I2C_MonitorModeConfig(I2CDEV,(uint32_t)I2C_MONITOR_CFG_MATCHALL, DISABLE); |
frank26080115 | 0:bf7b9fba3924 | 68 | I2C_MonitorModeCmd(I2CDEV, DISABLE); |
frank26080115 | 0:bf7b9fba3924 | 69 | } |
frank26080115 | 0:bf7b9fba3924 | 70 | } |
frank26080115 | 0:bf7b9fba3924 | 71 | |
frank26080115 | 0:bf7b9fba3924 | 72 | /*-------------------------PRIVATE FUNCTIONS------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 73 | /*********************************************************************//** |
frank26080115 | 0:bf7b9fba3924 | 74 | * @brief Print Welcome menu |
frank26080115 | 0:bf7b9fba3924 | 75 | * @param[in] none |
frank26080115 | 0:bf7b9fba3924 | 76 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 77 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 78 | void print_menu(void) |
frank26080115 | 0:bf7b9fba3924 | 79 | { |
frank26080115 | 0:bf7b9fba3924 | 80 | _DBG(menu1); |
frank26080115 | 0:bf7b9fba3924 | 81 | } |
frank26080115 | 0:bf7b9fba3924 | 82 | |
frank26080115 | 0:bf7b9fba3924 | 83 | /*-------------------------MAIN FUNCTION------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 84 | /*********************************************************************//** |
frank26080115 | 0:bf7b9fba3924 | 85 | * @brief c_entry: Main program body |
frank26080115 | 0:bf7b9fba3924 | 86 | * @param[in] None |
frank26080115 | 0:bf7b9fba3924 | 87 | * @return int |
frank26080115 | 0:bf7b9fba3924 | 88 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 89 | int c_entry(void) |
frank26080115 | 0:bf7b9fba3924 | 90 | { |
frank26080115 | 0:bf7b9fba3924 | 91 | PINSEL_CFG_Type PinCfg; |
frank26080115 | 0:bf7b9fba3924 | 92 | uint8_t idx,i; |
frank26080115 | 0:bf7b9fba3924 | 93 | /* Initialize debug via UART0 |
frank26080115 | 0:bf7b9fba3924 | 94 | * 115200bps |
frank26080115 | 0:bf7b9fba3924 | 95 | * 8 data bit |
frank26080115 | 0:bf7b9fba3924 | 96 | * No parity |
frank26080115 | 0:bf7b9fba3924 | 97 | * 1 stop bit |
frank26080115 | 0:bf7b9fba3924 | 98 | * No flow control |
frank26080115 | 0:bf7b9fba3924 | 99 | */ |
frank26080115 | 0:bf7b9fba3924 | 100 | debug_frmwrk_init(); |
frank26080115 | 0:bf7b9fba3924 | 101 | |
frank26080115 | 0:bf7b9fba3924 | 102 | //print menu screen |
frank26080115 | 0:bf7b9fba3924 | 103 | print_menu(); |
frank26080115 | 0:bf7b9fba3924 | 104 | |
frank26080115 | 0:bf7b9fba3924 | 105 | /* I2C block ------------------------------------------------------------------- */ |
frank26080115 | 0:bf7b9fba3924 | 106 | |
frank26080115 | 0:bf7b9fba3924 | 107 | /* |
frank26080115 | 0:bf7b9fba3924 | 108 | * Init I2C pin connect |
frank26080115 | 0:bf7b9fba3924 | 109 | */ |
frank26080115 | 0:bf7b9fba3924 | 110 | PinCfg.OpenDrain = 0; |
frank26080115 | 0:bf7b9fba3924 | 111 | PinCfg.Pinmode = 0; |
frank26080115 | 0:bf7b9fba3924 | 112 | PinCfg.Funcnum = 1; |
frank26080115 | 0:bf7b9fba3924 | 113 | PinCfg.Pinnum = 27; |
frank26080115 | 0:bf7b9fba3924 | 114 | PinCfg.Portnum = 0; |
frank26080115 | 0:bf7b9fba3924 | 115 | PINSEL_ConfigPin(&PinCfg);//SDA0 |
frank26080115 | 0:bf7b9fba3924 | 116 | PinCfg.Pinnum = 28; |
frank26080115 | 0:bf7b9fba3924 | 117 | PINSEL_ConfigPin(&PinCfg);//SCL0 |
frank26080115 | 0:bf7b9fba3924 | 118 | |
frank26080115 | 0:bf7b9fba3924 | 119 | // Initialize I2C peripheral |
frank26080115 | 0:bf7b9fba3924 | 120 | I2C_Init(I2CDEV, 100000); |
frank26080115 | 0:bf7b9fba3924 | 121 | |
frank26080115 | 0:bf7b9fba3924 | 122 | /* Configure interrupt for I2C in NVIC of ARM core */ |
frank26080115 | 0:bf7b9fba3924 | 123 | /* Disable I2C0 interrupt */ |
frank26080115 | 0:bf7b9fba3924 | 124 | NVIC_DisableIRQ(I2C0_IRQn); |
frank26080115 | 0:bf7b9fba3924 | 125 | /* preemption = 1, sub-priority = 0 */ |
frank26080115 | 0:bf7b9fba3924 | 126 | NVIC_SetPriority(I2C0_IRQn, ((0x01<<3)|0x01)); |
frank26080115 | 0:bf7b9fba3924 | 127 | //enable I2C interrupt |
frank26080115 | 0:bf7b9fba3924 | 128 | I2C_IntCmd(LPC_I2C0, ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 129 | |
frank26080115 | 0:bf7b9fba3924 | 130 | /* Enable I2C operation */ |
frank26080115 | 0:bf7b9fba3924 | 131 | I2C_Cmd(I2CDEV, ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 132 | |
frank26080115 | 0:bf7b9fba3924 | 133 | while(1) |
frank26080115 | 0:bf7b9fba3924 | 134 | { |
frank26080115 | 0:bf7b9fba3924 | 135 | idx=0;count=0; |
frank26080115 | 0:bf7b9fba3924 | 136 | while(idx<2) |
frank26080115 | 0:bf7b9fba3924 | 137 | { |
frank26080115 | 0:bf7b9fba3924 | 138 | if(idx==0) |
frank26080115 | 0:bf7b9fba3924 | 139 | { |
frank26080115 | 0:bf7b9fba3924 | 140 | _DBG_("\n\rEnter monitor buffer size: "); |
frank26080115 | 0:bf7b9fba3924 | 141 | } |
frank26080115 | 0:bf7b9fba3924 | 142 | idx++; |
frank26080115 | 0:bf7b9fba3924 | 143 | switch(_DG) |
frank26080115 | 0:bf7b9fba3924 | 144 | { |
frank26080115 | 0:bf7b9fba3924 | 145 | case '0': count=(count<<4)|0x00;break; |
frank26080115 | 0:bf7b9fba3924 | 146 | case '1': count=(count<<4)|0x01;break; |
frank26080115 | 0:bf7b9fba3924 | 147 | case '2': count=(count<<4)|0x02;break; |
frank26080115 | 0:bf7b9fba3924 | 148 | case '3': count=(count<<4)|0x03;break; |
frank26080115 | 0:bf7b9fba3924 | 149 | case '4': count=(count<<4)|0x04;break; |
frank26080115 | 0:bf7b9fba3924 | 150 | case '5': count=(count<<4)|0x05;break; |
frank26080115 | 0:bf7b9fba3924 | 151 | case '6': count=(count<<4)|0x06;break; |
frank26080115 | 0:bf7b9fba3924 | 152 | case '7': count=(count<<4)|0x07;break; |
frank26080115 | 0:bf7b9fba3924 | 153 | case '8': count=(count<<4)|0x08;break; |
frank26080115 | 0:bf7b9fba3924 | 154 | case '9': count=(count<<4)|0x09;break; |
frank26080115 | 0:bf7b9fba3924 | 155 | case 'a': count=(count<<4)|0x0A;break; |
frank26080115 | 0:bf7b9fba3924 | 156 | case 'A': count=(count<<4)|0x0A;break; |
frank26080115 | 0:bf7b9fba3924 | 157 | case 'b': count=(count<<4)|0x0B;break; |
frank26080115 | 0:bf7b9fba3924 | 158 | case 'B': count=(count<<4)|0x0B;break; |
frank26080115 | 0:bf7b9fba3924 | 159 | case 'c': count=(count<<4)|0x0C;break; |
frank26080115 | 0:bf7b9fba3924 | 160 | case 'C': count=(count<<4)|0x0C;break; |
frank26080115 | 0:bf7b9fba3924 | 161 | case 'd': count=(count<<4)|0x0D;break; |
frank26080115 | 0:bf7b9fba3924 | 162 | case 'D': count=(count<<4)|0x0D;break; |
frank26080115 | 0:bf7b9fba3924 | 163 | case 'e': count=(count<<4)|0x0E;break; |
frank26080115 | 0:bf7b9fba3924 | 164 | case 'E': count=(count<<4)|0x0E;break; |
frank26080115 | 0:bf7b9fba3924 | 165 | case 'f': count=(count<<4)|0x0F;break; |
frank26080115 | 0:bf7b9fba3924 | 166 | case 'F': count=(count<<4)|0x0F;break; |
frank26080115 | 0:bf7b9fba3924 | 167 | default: idx=0;count=0;break; |
frank26080115 | 0:bf7b9fba3924 | 168 | } |
frank26080115 | 0:bf7b9fba3924 | 169 | if(idx==2) |
frank26080115 | 0:bf7b9fba3924 | 170 | { |
frank26080115 | 0:bf7b9fba3924 | 171 | if(count>BUFFER_SIZE) |
frank26080115 | 0:bf7b9fba3924 | 172 | { |
frank26080115 | 0:bf7b9fba3924 | 173 | _DBG_("invalid! The size is bigger than ");_DBH(BUFFER_SIZE); |
frank26080115 | 0:bf7b9fba3924 | 174 | idx=0;count=0; |
frank26080115 | 0:bf7b9fba3924 | 175 | } |
frank26080115 | 0:bf7b9fba3924 | 176 | else |
frank26080115 | 0:bf7b9fba3924 | 177 | _DBH(count); |
frank26080115 | 0:bf7b9fba3924 | 178 | } |
frank26080115 | 0:bf7b9fba3924 | 179 | } |
frank26080115 | 0:bf7b9fba3924 | 180 | //Configure I2C in monitor mode |
frank26080115 | 0:bf7b9fba3924 | 181 | I2C_MonitorModeConfig(I2CDEV,(uint32_t)I2C_MONITOR_CFG_MATCHALL, ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 182 | I2C_MonitorModeCmd(I2CDEV, ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 183 | |
frank26080115 | 0:bf7b9fba3924 | 184 | _DBG_("\n\rStart monitoring I2C bus..."); |
frank26080115 | 0:bf7b9fba3924 | 185 | |
frank26080115 | 0:bf7b9fba3924 | 186 | while(done==FALSE); done=FALSE; |
frank26080115 | 0:bf7b9fba3924 | 187 | _DBG_("done!"); |
frank26080115 | 0:bf7b9fba3924 | 188 | for(i=0;i<count;i++) |
frank26080115 | 0:bf7b9fba3924 | 189 | { |
frank26080115 | 0:bf7b9fba3924 | 190 | if((i%16)==0) _DBG_(""); |
frank26080115 | 0:bf7b9fba3924 | 191 | _DBH(buffer[i]);_DBC(0x20); |
frank26080115 | 0:bf7b9fba3924 | 192 | buffer[i]=0; |
frank26080115 | 0:bf7b9fba3924 | 193 | } |
frank26080115 | 0:bf7b9fba3924 | 194 | |
frank26080115 | 0:bf7b9fba3924 | 195 | } |
frank26080115 | 0:bf7b9fba3924 | 196 | return 1; |
frank26080115 | 0:bf7b9fba3924 | 197 | } |
frank26080115 | 0:bf7b9fba3924 | 198 | |
frank26080115 | 0:bf7b9fba3924 | 199 | /* With ARM and GHS toolsets, the entry point is main() - this will |
frank26080115 | 0:bf7b9fba3924 | 200 | allow the linker to generate wrapper code to setup stacks, allocate |
frank26080115 | 0:bf7b9fba3924 | 201 | heap area, and initialize and copy code and data segments. For GNU |
frank26080115 | 0:bf7b9fba3924 | 202 | toolsets, the entry point is through __start() in the crt0_gnu.asm |
frank26080115 | 0:bf7b9fba3924 | 203 | file, and that startup code will setup stacks and data */ |
frank26080115 | 0:bf7b9fba3924 | 204 | int main(void) |
frank26080115 | 0:bf7b9fba3924 | 205 | { |
frank26080115 | 0:bf7b9fba3924 | 206 | return c_entry(); |
frank26080115 | 0:bf7b9fba3924 | 207 | } |
frank26080115 | 0:bf7b9fba3924 | 208 | |
frank26080115 | 0:bf7b9fba3924 | 209 | |
frank26080115 | 0:bf7b9fba3924 | 210 | #ifdef DEBUG |
frank26080115 | 0:bf7b9fba3924 | 211 | /******************************************************************************* |
frank26080115 | 0:bf7b9fba3924 | 212 | * @brief Reports the name of the source file and the source line number |
frank26080115 | 0:bf7b9fba3924 | 213 | * where the CHECK_PARAM error has occurred. |
frank26080115 | 0:bf7b9fba3924 | 214 | * @param[in] file Pointer to the source file name |
frank26080115 | 0:bf7b9fba3924 | 215 | * @param[in] line assert_param error line source number |
frank26080115 | 0:bf7b9fba3924 | 216 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 217 | *******************************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 218 | void check_failed(uint8_t *file, uint32_t line) |
frank26080115 | 0:bf7b9fba3924 | 219 | { |
frank26080115 | 0:bf7b9fba3924 | 220 | /* User can add his own implementation to report the file name and line number, |
frank26080115 | 0:bf7b9fba3924 | 221 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
frank26080115 | 0:bf7b9fba3924 | 222 | |
frank26080115 | 0:bf7b9fba3924 | 223 | /* Infinite loop */ |
frank26080115 | 0:bf7b9fba3924 | 224 | while(1); |
frank26080115 | 0:bf7b9fba3924 | 225 | } |
frank26080115 | 0:bf7b9fba3924 | 226 | #endif |
frank26080115 | 0:bf7b9fba3924 | 227 | |
frank26080115 | 0:bf7b9fba3924 | 228 | /* |
frank26080115 | 0:bf7b9fba3924 | 229 | * @} |
frank26080115 | 0:bf7b9fba3924 | 230 | */ |