mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
300:55638feb26a4
Parent:
270:e2babe29baf8
Child:
314:b682143dd337
--- a/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/i2c_api.c	Wed Aug 27 04:00:07 2014 +0100
+++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/i2c_api.c	Wed Aug 27 08:45:06 2014 +0100
@@ -21,175 +21,187 @@
 static const PinMap PinMap_I2C_SDA[] = {
     {p22, I2C_0, 1},
     {p13, I2C_1, 2},
-    {NC  , NC   , 0}
+    {NC, NC, 0}
 };
 
 static const PinMap PinMap_I2C_SCL[] = {
     {p20, I2C_0, 1},
     {p15, I2C_1, 2},
-    {NC  , NC,    0}
+    {NC, NC,    0}
 };
 
-uint8_t addrSet=0;
+uint8_t addrSet = 0;
 
-void i2c_interface_enable(i2c_t *obj){
+void i2c_interface_enable(i2c_t *obj)
+{
     obj->i2c->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
 }
 
-void twi_master_init(i2c_t *obj, PinName sda, PinName scl, int frequency) {
-    NRF_GPIO->PIN_CNF[scl]  = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
-                               (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
-                               (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
-                               (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
-                               (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));
+void twi_master_init(i2c_t *obj, PinName sda, PinName scl, int frequency)
+{
+    NRF_GPIO->PIN_CNF[scl] = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
+                              (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
+                              (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
+                              (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
+                              (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));
 
-    NRF_GPIO->PIN_CNF[sda]  = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
-                               (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
-                               (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
-                               (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
-                               (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));
+    NRF_GPIO->PIN_CNF[sda] = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
+                              (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
+                              (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
+                              (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
+                              (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));
 
-    obj->i2c->PSELSCL               = scl;
-    obj->i2c->PSELSDA               = sda;
+    obj->i2c->PSELSCL = scl;
+    obj->i2c->PSELSDA = sda;
     // set default frequency at 100k
     i2c_frequency(obj, frequency);
     i2c_interface_enable(obj);
 }
-void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
+
+void i2c_init(i2c_t *obj, PinName sda, PinName scl)
+{
     // determine the SPI to use
     I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
     I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
-    I2CName i2c     = (I2CName)pinmap_merge(i2c_sda,i2c_scl);
-    obj->i2c        = (NRF_TWI_Type            *)i2c;
-    
+    I2CName i2c     = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
+    obj->i2c = (NRF_TWI_Type *)i2c;
+
     MBED_ASSERT((int)obj->i2c != NC);
 
-    obj->scl=scl;
-    obj->sda=sda;
+    obj->scl               = scl;
+    obj->sda               = sda;
     obj->i2c->EVENTS_ERROR = 0;
     obj->i2c->ENABLE       = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
     obj->i2c->POWER        = 0;
-    
-    for(int i=0;i<100;i++){
+
+    for (int i = 0; i<100; i++) {
     }
-    
-    obj->i2c->POWER        = 1;
-    twi_master_init(obj,sda,scl,100000);
+
+    obj->i2c->POWER = 1;
+    twi_master_init(obj, sda, scl, 100000);
 }
-void i2c_reset(i2c_t *obj) {
+
+void i2c_reset(i2c_t *obj)
+{
     obj->i2c->EVENTS_ERROR = 0;
     obj->i2c->ENABLE       = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
     obj->i2c->POWER        = 0;
-    for(int i=0;i<100;i++){
+    for (int i = 0; i<100; i++) {
     }
-    
-    obj->i2c->POWER        = 1;
-    twi_master_init(obj,obj->sda,obj->scl,obj->freq);
+
+    obj->i2c->POWER = 1;
+    twi_master_init(obj, obj->sda, obj->scl, obj->freq);
 }
 
-int i2c_start(i2c_t *obj) {
+int i2c_start(i2c_t *obj)
+{
     int status = 0;
     i2c_reset(obj);
-    addrSet=0;
+    addrSet = 0;
     return status;
 }
 
-int i2c_stop(i2c_t *obj) {
+int i2c_stop(i2c_t *obj)
+{
     int timeOut = 100000;
     obj->i2c->EVENTS_STOPPED = 0;
     // write the stop bit
     obj->i2c->TASKS_STOP = 1;
-    while(!obj->i2c->EVENTS_STOPPED){
+    while (!obj->i2c->EVENTS_STOPPED) {
         timeOut--;
-        if(timeOut<0)
+        if (timeOut<0) {
             return 1;
+        }
     }
     addrSet = 0;
     i2c_reset(obj);
     return 0;
 }
 
-
-int i2c_do_write(i2c_t *obj, int value) {
+int i2c_do_write(i2c_t *obj, int value)
+{
     int timeOut = 100000;
     obj->i2c->TXD = value;
-    while(!obj->i2c->EVENTS_TXDSENT){
+    while (!obj->i2c->EVENTS_TXDSENT) {
         timeOut--;
-        if(timeOut<0)
+        if (timeOut<0) {
             return 1;
+        }
     }
     obj->i2c->EVENTS_TXDSENT = 0;
     return 0;
 }
 
-int i2c_do_read(i2c_t *obj, char * data, int last) {
+int i2c_do_read(i2c_t *obj, char *data, int last)
+{
     int timeOut = 100000;
 
-    if (last){
+    if (last) {
         obj->i2c->TASKS_STOP = 1;
     }
-    while(!obj->i2c->EVENTS_RXDREADY){
+    while (!obj->i2c->EVENTS_RXDREADY) {
         timeOut--;
-        if(timeOut<0)
+        if (timeOut<0) {
             return 1;
+        }
     }
-    obj->i2c->EVENTS_RXDREADY       = 0;
-    
+    obj->i2c->EVENTS_RXDREADY = 0;
+
     *data = obj->i2c->RXD;
-    
-    for(int i=0;i<320;i++){
+
+    for (int i = 0; i<320; i++) {
     }
-    
+
     obj->i2c->TASKS_RESUME = 1;
-    
+
     return 0;
 }
 
-
-void i2c_frequency(i2c_t *obj, int hz) {
-    if(hz<250000){
+void i2c_frequency(i2c_t *obj, int hz)
+{
+    if (hz<250000) {
         obj->freq           = 100000;
         obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);
-    }
-    else if(hz<400000){
+    } else if (hz<400000) {
         obj->freq           = 250000;
         obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K250 << TWI_FREQUENCY_FREQUENCY_Pos);
-    }
-    else{
+    } else {
         obj->freq           = 400000;
         obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K400 << TWI_FREQUENCY_FREQUENCY_Pos);
     }
 }
 
-int checkError(i2c_t *obj){
-    if (obj->i2c->EVENTS_ERROR == 1){
-        if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk){
-            obj->i2c->EVENTS_ERROR  = 0;
-            obj->i2c->TASKS_STOP    = 1;
+int checkError(i2c_t *obj)
+{
+    if (obj->i2c->EVENTS_ERROR == 1) {
+        if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk) {
+            obj->i2c->EVENTS_ERROR = 0;
+            obj->i2c->TASKS_STOP   = 1;
             return I2C_ERROR_BUS_BUSY;
         }
-        
-        obj->i2c->EVENTS_ERROR  = 0;
-        obj->i2c->TASKS_STOP    = 1;
+
+        obj->i2c->EVENTS_ERROR = 0;
+        obj->i2c->TASKS_STOP   = 1;
         return I2C_ERROR_NO_SLAVE;
     }
     return 0;
 }
 
-int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
-    int status,count,errorResult;
-    obj->i2c->ADDRESS         = (address>>1);
+int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
+{
+    int status, count, errorResult;
+    obj->i2c->ADDRESS         = (address >> 1);
     obj->i2c->SHORTS          = 0;
     obj->i2c->EVENTS_RXDREADY = 0;
     obj->i2c->TASKS_STARTRX   = 1;
-    
+
     // Read in all except last byte
     for (count = 0; count < (length - 1); count++) {
-        status = i2c_do_read(obj,&data[count], 0);
+        status = i2c_do_read(obj, &data[count], 0);
         if (status) {
             errorResult = checkError(obj);
             i2c_reset(obj);
-            if(errorResult<0){
+            if (errorResult<0) {
                 return errorResult;
             }
             return count;
@@ -197,77 +209,78 @@
     }
 
     // read in last byte
-    status = i2c_do_read(obj,&data[length-1], 1);
+    status = i2c_do_read(obj, &data[length - 1], 1);
     if (status) {
         i2c_reset(obj);
         return length - 1;
     }
     // If not repeated start, send stop.
     if (stop) {
-        while(!obj->i2c->EVENTS_STOPPED){
+        while (!obj->i2c->EVENTS_STOPPED) {
         }
         obj->i2c->EVENTS_STOPPED = 0;
     }
     return length;
 }
 
-int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
+int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
+{
     int status, errorResult;
-    obj->i2c->ADDRESS       = (address>>1);
+    obj->i2c->ADDRESS       = (address >> 1);
     obj->i2c->SHORTS        = 0;
     obj->i2c->TASKS_STARTTX = 1;
- 
-    for (int i=0; i<length; i++) {
+
+    for (int i = 0; i<length; i++) {
         status = i2c_do_write(obj, data[i]);
-        if(status) {
+        if (status) {
             i2c_reset(obj);
             errorResult = checkError(obj);
-            if(errorResult<0){
+            if (errorResult<0) {
                 return errorResult;
             }
             return i;
         }
     }
-    
+
     // If not repeated start, send stop.
     if (stop) {
-        if(i2c_stop(obj)){
+        if (i2c_stop(obj)) {
             return I2C_ERROR_NO_SLAVE;
         }
     }
     return length;
 }
 
-int i2c_byte_read(i2c_t *obj, int last) {
+int i2c_byte_read(i2c_t *obj, int last)
+{
     char data;
     int status;
-    
-    status = i2c_do_read(obj,&data, last);
+
+    status = i2c_do_read(obj, &data, last);
     if (status) {
         i2c_reset(obj);
     }
     return data;
 }
 
-int i2c_byte_write(i2c_t *obj, int data) {
+int i2c_byte_write(i2c_t *obj, int data)
+{
     int status = 0;
-    if(!addrSet){
-        addrSet = 1;
-        obj->i2c->ADDRESS = (data>>1);
-        
-        if(data&1){
+    if (!addrSet) {
+        addrSet           = 1;
+        obj->i2c->ADDRESS = (data >> 1);
+
+        if (data & 1) {
             obj->i2c->EVENTS_RXDREADY = 0;
             obj->i2c->TASKS_STARTRX   = 1;
-        }
-        else{
-            obj->i2c->TASKS_STARTTX   = 1;
+        } else {
+            obj->i2c->TASKS_STARTTX = 1;
         }
-    }
-    else{
+    } else {
         status = i2c_do_write(obj, data);
-        if(status) {
+        if (status) {
             i2c_reset(obj);
         }
     }
-    return (1-status);
+    return (1 - status);
 }