Skip to content

Commit cd9bc14

Browse files
committed
cc3200: Add SPI module.
Only MASTER mode is supported. Transfer width is configurable to 8, 16 or 32 bits.
1 parent c45e641 commit cd9bc14

File tree

8 files changed

+499
-12
lines changed

8 files changed

+499
-12
lines changed

cc3200/application.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ APP_MODS_SRC_C = $(addprefix mods/,\
9696
pybrtc.c \
9797
pybsd.c \
9898
pybsleep.c \
99+
pybspi.c \
99100
pybuart.c \
100101
pybwdt.c \
101102
)

cc3200/hal/spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ SPIReset(unsigned long ulBase)
718718
//! - \b SPI_WL_16
719719
//! - \b SPI_WL_32
720720
//!
721-
//! Active state of Chip[ Selece can be defined by:-
721+
//! Active state of Chip Select can be defined by:-
722722
//! - \b SPI_CS_ACTIVELOW
723723
//! - \b SPI_CS_ACTIVEHIGH
724724
//!

cc3200/mods/modpyb.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include "pybuart.h"
4444
#include "pybpin.h"
4545
#include "pybrtc.h"
46-
#include "pybsystick.h"
46+
#include "mpsystick.h"
4747
#include "simplelink.h"
4848
#include "modwlan.h"
4949
#include "moduos.h"
@@ -62,6 +62,7 @@
6262
#include "pybsd.h"
6363
#include "pybwdt.h"
6464
#include "pybsleep.h"
65+
#include "pybspi.h"
6566
#include "utils.h"
6667
#include "gccollect.h"
6768
#include "mperror.h"
@@ -281,6 +282,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
281282
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_type },
282283
{ MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
283284
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
285+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&pyb_spi_type },
284286
{ MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
285287
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&pyb_wdt_obj },
286288
{ MP_OBJ_NEW_QSTR(MP_QSTR_Sleep), (mp_obj_t)&pyb_sleep_obj },

cc3200/mods/pybi2c.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ typedef struct _pyb_i2c_obj_t {
105105
#define PYBI2C_MODE_MASTER (0)
106106
#define PYBI2C_MODE_SLAVE (1)
107107

108-
#define PYBI2C_DEF_BAUD_RATE_HZ (100000)
108+
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
109109
#define PYBI2C_DEF_BAUD_RATE_HZ (100000)
110110
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
111111

@@ -123,7 +123,7 @@ typedef struct _pyb_i2c_obj_t {
123123
/******************************************************************************
124124
DECLARE PRIVATE DATA
125125
******************************************************************************/
126-
STATIC pyb_i2c_obj_t pyb_i2c_obj;
126+
STATIC pyb_i2c_obj_t pyb_i2c_obj = {.baudrate = 0};
127127

128128
/******************************************************************************
129129
DEFINE PRIVATE FUNCTIONS
@@ -140,6 +140,8 @@ STATIC void i2c_init (pyb_i2c_obj_t *self) {
140140
STATIC void i2c_deinit(void) {
141141
MAP_I2CMasterDisable(I2CA0_BASE);
142142
MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
143+
// invalidate the baudrate
144+
pyb_i2c_obj.baudrate = 0;
143145
}
144146

145147
STATIC bool pyb_i2c_transaction(uint cmd, int timeout) {
@@ -287,11 +289,12 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_uint_t n_args, co
287289
mp_arg_parse_all(n_args, args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, vals);
288290

289291
if (vals[0].u_int != PYBI2C_MODE_MASTER) {
290-
// thrown an exception since only master mode is supported for now
292+
// thrown an exception since only master mode is supported
291293
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
292294
}
293295

294-
self->baudrate = MIN(vals[2].u_int, PYBI2C_MAX_BAUD_RATE_HZ);
296+
// make sure the baudrate is between the valid range
297+
self->baudrate = MIN(MAX(vals[2].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
295298

296299
// init the I2C bus
297300
i2c_init(self);
@@ -337,7 +340,12 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
337340

338341
STATIC void pyb_i2c_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
339342
pyb_i2c_obj_t *self = self_in;
340-
print(env, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
343+
if (self->baudrate > 0) {
344+
print(env, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
345+
}
346+
else {
347+
print(env, "<I2C0>");
348+
}
341349
}
342350

343351
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
@@ -545,7 +553,7 @@ STATIC mp_obj_t pyb_i2c_mem_write(mp_uint_t n_args, const mp_obj_t *args, mp_map
545553
// determine width of mem_addr (1 or 2 bytes)
546554
mp_uint_t mem_addr_size = vals[4].u_int >> 3;
547555

548-
// Write the register address to be write to.
556+
// Write the register address to write to.
549557
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false, vals[3].u_int)) {
550558
// Write the specified length of data
551559
if (pyb_i2c_write (i2c_addr, bufinfo.buf, bufinfo.len, true, vals[3].u_int)) {

0 commit comments

Comments
 (0)