Created
December 24, 2017 13:44
-
-
Save aykevl/dcaaed2805bd7e54408b2b53f770d8f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ports/nrf/bluetooth_conf.h b/ports/nrf/bluetooth_conf.h | |
index 6a3cbdc83..9e649c940 100644 | |
--- a/ports/nrf/bluetooth_conf.h | |
+++ b/ports/nrf/bluetooth_conf.h | |
@@ -6,7 +6,7 @@ | |
#if (BLUETOOTH_SD == 110) | |
#define MICROPY_PY_BLE (1) | |
-#define MICROPY_PY_BLE_NUS (0) | |
+#define MICROPY_PY_BLE_NUS (1) | |
#define BLUETOOTH_WEBBLUETOOTH_REPL (0) | |
#define MICROPY_PY_UBLUEPY (1) | |
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1) | |
diff --git a/ports/nrf/hal/hal_twi.c b/ports/nrf/hal/hal_twi.c | |
index 65d729c94..01f78d18d 100644 | |
--- a/ports/nrf/hal/hal_twi.c | |
+++ b/ports/nrf/hal/hal_twi.c | |
@@ -37,6 +37,22 @@ static const uint32_t hal_twi_frequency_lookup[] = { | |
void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { | |
+ // Configure pins: direction = input, drive strength = S0D1 | |
+ // TODO: deconfigure at deinit | |
+ GPIO_BASE(p_twi_init->scl_pin->port)->PIN_CNF[p_twi_init->scl_pin->pin] = | |
+ (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | |
+ | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
+ | (GPIO_PIN_CNF_PULL_Pullup << 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); | |
+ | |
+ GPIO_BASE(p_twi_init->sda_pin->port)->PIN_CNF[p_twi_init->sda_pin->pin] = | |
+ (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | |
+ | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
+ | (GPIO_PIN_CNF_PULL_Pullup << 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); | |
+ | |
#if NRF52840_XXAA | |
p_instance->PSEL.SCL = p_twi_init->scl_pin->pin; | |
p_instance->PSEL.SDA = p_twi_init->sda_pin->pin; | |
@@ -51,32 +67,45 @@ void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi | |
p_instance->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos); | |
} | |
#include <stdio.h> | |
-void hal_twi_master_tx(NRF_TWI_Type * p_instance, | |
+uint32_t hal_twi_master_tx(NRF_TWI_Type * p_instance, | |
uint8_t addr, | |
- uint16_t transfer_size, | |
+ size_t transfer_size, | |
const uint8_t * tx_data, | |
bool stop) { | |
+ return 1; | |
- uint16_t number_of_txd_bytes = 0; | |
+ if (transfer_size == 0) { | |
+ return 0; | |
+ } | |
p_instance->ADDRESS = addr; | |
p_instance->EVENTS_TXDSENT = 0; | |
- p_instance->TXD = tx_data[number_of_txd_bytes]; | |
+ p_instance->TXD = tx_data[0]; | |
p_instance->TASKS_STARTTX = 1; | |
+ size_t number_of_txd_bytes = 1; | |
+ | |
+ uint32_t err_code = 0; | |
+ | |
+ | |
while (number_of_txd_bytes < transfer_size) { | |
+ uint32_t timeout = 100000; | |
+ | |
// wait for the transaction complete | |
- while (p_instance->EVENTS_TXDSENT == 0) { | |
+ while (p_instance->EVENTS_TXDSENT == 0 && timeout--) { | |
; | |
} | |
+ if (!timeout) { | |
+ err_code = 1; | |
+ break; | |
+ } | |
- number_of_txd_bytes++; | |
- | |
- // TODO: This could go one byte out of bound. | |
p_instance->TXD = tx_data[number_of_txd_bytes]; | |
p_instance->EVENTS_TXDSENT = 0; | |
+ | |
+ number_of_txd_bytes++; | |
} | |
@@ -88,11 +117,13 @@ void hal_twi_master_tx(NRF_TWI_Type * p_instance, | |
; | |
} | |
} | |
+ | |
+ return err_code; | |
} | |
void hal_twi_master_rx(NRF_TWI_Type * p_instance, | |
uint8_t addr, | |
- uint16_t transfer_size, | |
+ size_t transfer_size, | |
uint8_t * rx_data, | |
bool stop) { | |
diff --git a/ports/nrf/hal/hal_twi.h b/ports/nrf/hal/hal_twi.h | |
index 834c512a0..f912671c4 100644 | |
--- a/ports/nrf/hal/hal_twi.h | |
+++ b/ports/nrf/hal/hal_twi.h | |
@@ -99,15 +99,15 @@ typedef struct __TWI_HandleTypeDef | |
void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init); | |
-void hal_twi_master_tx(NRF_TWI_Type * p_instance, | |
+uint32_t hal_twi_master_tx(NRF_TWI_Type * p_instance, | |
uint8_t addr, | |
- uint16_t transfer_size, | |
+ size_t transfer_size, | |
const uint8_t * tx_data, | |
bool stop); | |
void hal_twi_master_rx(NRF_TWI_Type * p_instance, | |
uint8_t addr, | |
- uint16_t transfer_size, | |
+ size_t transfer_size, | |
uint8_t * rx_data, | |
bool stop); | |
diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c | |
index 943599816..e6e5a42d1 100644 | |
--- a/ports/nrf/modules/machine/i2c.c | |
+++ b/ports/nrf/modules/machine/i2c.c | |
@@ -29,6 +29,7 @@ | |
#include "py/nlr.h" | |
#include "py/runtime.h" | |
+#include "py/mperrno.h" | |
#include "py/mphal.h" | |
#include "extmod/machine_i2c.h" | |
#include "i2c.h" | |
@@ -141,7 +142,10 @@ int machine_hard_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *de | |
int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) { | |
machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)self_in; | |
- hal_twi_master_tx(self->i2c->instance, addr, len, src, stop); | |
+ // TODO: pass timeout in microseconds | |
+ if (hal_twi_master_tx(self->i2c->instance, addr, len, src, stop) != 0) { | |
+ mp_raise_OSError(MP_EIO); | |
+ } | |
return 0; | |
} | |
diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h | |
index 61729f6d0..e88c5819f 100644 | |
--- a/ports/nrf/mpconfigport.h | |
+++ b/ports/nrf/mpconfigport.h | |
@@ -43,7 +43,7 @@ | |
#define MICROPY_READER_VFS (MICROPY_VFS) | |
#define MICROPY_ENABLE_GC (1) | |
#define MICROPY_ENABLE_FINALISER (1) | |
-#define MICROPY_STACK_CHECK (0) | |
+#define MICROPY_STACK_CHECK (1) | |
#define MICROPY_HELPER_REPL (1) | |
#define MICROPY_REPL_EMACS_KEYS (0) | |
#define MICROPY_REPL_AUTO_INDENT (1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment