Skip to content

Commit 99f3f6b

Browse files
committed
cc3200: Add I2C module. Only master mode is currently supported.
1 parent 601c814 commit 99f3f6b

16 files changed

Lines changed: 761 additions & 46 deletions

File tree

cc3200/application.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ APP_INC += -Iutil
1818
APP_INC += -Ibootmgr
1919
APP_INC += -I$(PY_SRC)
2020
APP_INC += -I$(BUILD)
21+
APP_INC += -I$(BUILD)/genhdr
2122
APP_INC += -I../lib/fatfs
2223
APP_INC += -I../lib/mp-readline
2324
APP_INC += -I../stmhal
@@ -89,6 +90,7 @@ APP_MODS_SRC_C = $(addprefix mods/,\
8990
modutime.c \
9091
modwlan.c \
9192
pybextint.c \
93+
pybi2c.c \
9294
pybpin.c \
9395
pybrtc.c \
9496
pybsystick.c \

cc3200/fatfs/src/drivers/sflash_diskio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ DRESULT sflash_disk_read(BYTE *buff, DWORD sector, UINT count) {
112112
return STA_NOINIT;
113113
}
114114

115-
if (sector + count > SFLASH_SECTOR_COUNT || count == 0) {
115+
if ((sector + count > SFLASH_SECTOR_COUNT) || (count == 0)) {
116116
return RES_PARERR;
117117
}
118118

@@ -145,7 +145,8 @@ DRESULT sflash_disk_write(const BYTE *buff, DWORD sector, UINT count) {
145145
return STA_NOINIT;
146146
}
147147

148-
if (sector + count > SFLASH_SECTOR_COUNT || count == 0) {
148+
if ((sector + count > SFLASH_SECTOR_COUNT) || (count == 0)) {
149+
sflash_disk_flush();
149150
return RES_PARERR;
150151
}
151152

cc3200/hal/i2c.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ _I2CIntNumberGet(uint32_t ui32Base)
169169
//
170170
//*****************************************************************************
171171
void
172-
I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
173-
bool bFast)
172+
I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32SCLFreq)
174173
{
175-
uint32_t ui32SCLFreq;
176174
uint32_t ui32TPR;
177175

178176
//
@@ -185,25 +183,13 @@ I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
185183
//
186184
I2CMasterEnable(ui32Base);
187185

188-
//
189-
// Get the desired SCL speed.
190-
//
191-
if(bFast == true)
192-
{
193-
ui32SCLFreq = 400000;
194-
}
195-
else
196-
{
197-
ui32SCLFreq = 100000;
198-
}
199-
200186
//
201187
// Compute the clock divider that achieves the fastest speed less than or
202188
// equal to the desired speed. The numerator is biased to favor a larger
203189
// clock divider so that the resulting clock is always less than or equal
204190
// to the desired clock, never greater.
205191
//
206-
ui32TPR = ((ui32I2CClk + (2 * 10 * ui32SCLFreq) - 1) /
192+
ui32TPR = ((80000000 + (2 * 10 * ui32SCLFreq) - 1) /
207193
(2 * 10 * ui32SCLFreq)) - 1;
208194
HWREG(ui32Base + I2C_O_MTPR) = ui32TPR;
209195

@@ -213,7 +199,7 @@ I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
213199
//
214200
if(HWREG(ui32Base + I2C_O_PP) & I2C_PP_HS)
215201
{
216-
ui32TPR = ((ui32I2CClk + (2 * 3 * 3400000) - 1) /
202+
ui32TPR = ((80000000 + (2 * 3 * 3400000) - 1) /
217203
(2 * 3 * 3400000)) - 1;
218204
HWREG(ui32Base + I2C_O_MTPR) = I2C_MTPR_HS | ui32TPR;
219205
}

cc3200/hal/i2c.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ extern void I2CMasterDataPut(uint32_t ui32Base, uint8_t ui8Data);
309309
extern void I2CMasterDisable(uint32_t ui32Base);
310310
extern void I2CMasterEnable(uint32_t ui32Base);
311311
extern uint32_t I2CMasterErr(uint32_t ui32Base);
312-
extern void I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
313-
bool bFast);
312+
extern void I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32SCLFreq);
314313
extern void I2CMasterIntClear(uint32_t ui32Base);
315314
extern void I2CMasterIntDisable(uint32_t ui32Base);
316315
extern void I2CMasterIntEnable(uint32_t ui32Base);

cc3200/hal/rom_patch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@
8484
#undef ROM_SPIConfigSetExpClk
8585
#undef ROM_GPIODirModeGet
8686
#undef ROM_GPIOIntTypeGet
87+
#undef ROM_I2CMasterInitExpClk
8788

cc3200/misc/pin_named_pins.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,13 @@ const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t na
6060
}
6161
return NULL;
6262
}
63+
64+
const pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num) {
65+
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
66+
for (uint i = 0; i < named_map->used; i++) {
67+
if (((pin_obj_t *)named_map->table[i].value)->pin_num == pin_num) {
68+
return named_map->table[i].value;
69+
}
70+
}
71+
return NULL;
72+
}

cc3200/mods/modpyb.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#include "mpexception.h"
6363
#include "random.h"
6464
#include "pybextint.h"
65+
#include "pybi2c.h"
66+
#include "utils.h"
6567

6668

6769
#ifdef DEBUG
@@ -218,10 +220,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
218220
STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
219221
mp_int_t usec = mp_obj_get_int(usec_in);
220222
if (usec > 0) {
221-
uint32_t count = 0;
222-
const uint32_t utime = ((HAL_FCPU_HZ / 1000000) * (usec / 4));
223-
while (++count <= utime) {
224-
}
223+
UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec));
225224
}
226225
return mp_const_none;
227226
}
@@ -261,6 +260,17 @@ STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
261260
}
262261
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
263262

263+
/// \function mkdisk('path')
264+
/// Formats the selected drive, useful when the filesystem has been damaged beyond repair
265+
STATIC mp_obj_t pyb_mkdisk(mp_obj_t path_o) {
266+
const char *path = mp_obj_str_get_str(path_o);
267+
if (FR_OK != f_mkfs(path, 1, 0)) {
268+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
269+
}
270+
return mp_const_none;
271+
}
272+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_mkdisk_obj, pyb_mkdisk);
273+
264274
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
265275

266276
STATIC const mp_map_elem_t pyb_module_globals_table[] = {
@@ -291,8 +301,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
291301
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
292302
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
293303
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
294-
295-
//{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
304+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdisk), (mp_obj_t)&pyb_mkdisk_obj },
296305

297306
#if MICROPY_HW_ENABLE_RNG
298307
{ MP_OBJ_NEW_QSTR(MP_QSTR_rng), (mp_obj_t)&pyb_rng_get_obj },
@@ -304,7 +313,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
304313

305314
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_type },
306315
{ MP_OBJ_NEW_QSTR(MP_QSTR_ExtInt), (mp_obj_t)&extint_type },
307-
316+
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
308317
{ MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
309318
};
310319

cc3200/mods/pybextint.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,15 @@ extint_obj_t* extint_register(mp_obj_t pin_obj, uint32_t intmode, uint32_t pull,
283283
intmode != GPIO_BOTH_EDGES &&
284284
intmode != GPIO_LOW_LEVEL &&
285285
intmode != GPIO_HIGH_LEVEL) {
286-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_value_invalid_arguments));
286+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
287287
}
288288
if (pull != PIN_TYPE_STD &&
289289
pull != PIN_TYPE_STD_PU &&
290290
pull != PIN_TYPE_STD_PD &&
291291
pull != PIN_TYPE_OD &&
292292
pull != PIN_TYPE_OD_PU &&
293293
pull != PIN_TYPE_OD_PD) {
294-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_value_invalid_arguments));
294+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
295295
}
296296

297297
if (NULL == (self = extint_find(pin->port, pin->bit))) {

0 commit comments

Comments
 (0)