Commit 99f3f6b5 authored by danicampora's avatar danicampora
Browse files

cc3200: Add I2C module. Only master mode is currently supported.

parent 601c8146
......@@ -18,6 +18,7 @@ APP_INC += -Iutil
APP_INC += -Ibootmgr
APP_INC += -I$(PY_SRC)
APP_INC += -I$(BUILD)
APP_INC += -I$(BUILD)/genhdr
APP_INC += -I../lib/fatfs
APP_INC += -I../lib/mp-readline
APP_INC += -I../stmhal
......@@ -89,6 +90,7 @@ APP_MODS_SRC_C = $(addprefix mods/,\
modutime.c \
modwlan.c \
pybextint.c \
pybi2c.c \
pybpin.c \
pybrtc.c \
pybsystick.c \
......
......@@ -112,7 +112,7 @@ DRESULT sflash_disk_read(BYTE *buff, DWORD sector, UINT count) {
return STA_NOINIT;
}
if (sector + count > SFLASH_SECTOR_COUNT || count == 0) {
if ((sector + count > SFLASH_SECTOR_COUNT) || (count == 0)) {
return RES_PARERR;
}
......@@ -145,7 +145,8 @@ DRESULT sflash_disk_write(const BYTE *buff, DWORD sector, UINT count) {
return STA_NOINIT;
}
if (sector + count > SFLASH_SECTOR_COUNT || count == 0) {
if ((sector + count > SFLASH_SECTOR_COUNT) || (count == 0)) {
sflash_disk_flush();
return RES_PARERR;
}
......
......@@ -169,10 +169,8 @@ _I2CIntNumberGet(uint32_t ui32Base)
//
//*****************************************************************************
void
I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
bool bFast)
I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32SCLFreq)
{
uint32_t ui32SCLFreq;
uint32_t ui32TPR;
//
......@@ -185,25 +183,13 @@ I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
//
I2CMasterEnable(ui32Base);
//
// Get the desired SCL speed.
//
if(bFast == true)
{
ui32SCLFreq = 400000;
}
else
{
ui32SCLFreq = 100000;
}
//
// Compute the clock divider that achieves the fastest speed less than or
// equal to the desired speed. The numerator is biased to favor a larger
// clock divider so that the resulting clock is always less than or equal
// to the desired clock, never greater.
//
ui32TPR = ((ui32I2CClk + (2 * 10 * ui32SCLFreq) - 1) /
ui32TPR = ((80000000 + (2 * 10 * ui32SCLFreq) - 1) /
(2 * 10 * ui32SCLFreq)) - 1;
HWREG(ui32Base + I2C_O_MTPR) = ui32TPR;
......@@ -213,7 +199,7 @@ I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
//
if(HWREG(ui32Base + I2C_O_PP) & I2C_PP_HS)
{
ui32TPR = ((ui32I2CClk + (2 * 3 * 3400000) - 1) /
ui32TPR = ((80000000 + (2 * 3 * 3400000) - 1) /
(2 * 3 * 3400000)) - 1;
HWREG(ui32Base + I2C_O_MTPR) = I2C_MTPR_HS | ui32TPR;
}
......
......@@ -309,8 +309,7 @@ extern void I2CMasterDataPut(uint32_t ui32Base, uint8_t ui8Data);
extern void I2CMasterDisable(uint32_t ui32Base);
extern void I2CMasterEnable(uint32_t ui32Base);
extern uint32_t I2CMasterErr(uint32_t ui32Base);
extern void I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
bool bFast);
extern void I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32SCLFreq);
extern void I2CMasterIntClear(uint32_t ui32Base);
extern void I2CMasterIntDisable(uint32_t ui32Base);
extern void I2CMasterIntEnable(uint32_t ui32Base);
......
......@@ -84,4 +84,5 @@
#undef ROM_SPIConfigSetExpClk
#undef ROM_GPIODirModeGet
#undef ROM_GPIOIntTypeGet
#undef ROM_I2CMasterInitExpClk
......@@ -60,3 +60,13 @@ const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t na
}
return NULL;
}
const pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num) {
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
for (uint i = 0; i < named_map->used; i++) {
if (((pin_obj_t *)named_map->table[i].value)->pin_num == pin_num) {
return named_map->table[i].value;
}
}
return NULL;
}
......@@ -62,6 +62,8 @@
#include "mpexception.h"
#include "random.h"
#include "pybextint.h"
#include "pybi2c.h"
#include "utils.h"
#ifdef DEBUG
......@@ -218,10 +220,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
mp_int_t usec = mp_obj_get_int(usec_in);
if (usec > 0) {
uint32_t count = 0;
const uint32_t utime = ((HAL_FCPU_HZ / 1000000) * (usec / 4));
while (++count <= utime) {
}
UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec));
}
return mp_const_none;
}
......@@ -261,6 +260,17 @@ STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
/// \function mkdisk('path')
/// Formats the selected drive, useful when the filesystem has been damaged beyond repair
STATIC mp_obj_t pyb_mkdisk(mp_obj_t path_o) {
const char *path = mp_obj_str_get_str(path_o);
if (FR_OK != f_mkfs(path, 1, 0)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_mkdisk_obj, pyb_mkdisk);
MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
STATIC const mp_map_elem_t pyb_module_globals_table[] = {
......@@ -291,8 +301,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
//{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdisk), (mp_obj_t)&pyb_mkdisk_obj },
#if MICROPY_HW_ENABLE_RNG
{ 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[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ExtInt), (mp_obj_t)&extint_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
};
......
......@@ -283,7 +283,7 @@ extint_obj_t* extint_register(mp_obj_t pin_obj, uint32_t intmode, uint32_t pull,
intmode != GPIO_BOTH_EDGES &&
intmode != GPIO_LOW_LEVEL &&
intmode != GPIO_HIGH_LEVEL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_value_invalid_arguments));
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
if (pull != PIN_TYPE_STD &&
pull != PIN_TYPE_STD_PU &&
......@@ -291,7 +291,7 @@ extint_obj_t* extint_register(mp_obj_t pin_obj, uint32_t intmode, uint32_t pull,
pull != PIN_TYPE_OD &&
pull != PIN_TYPE_OD_PU &&
pull != PIN_TYPE_OD_PD) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_value_invalid_arguments));
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
if (NULL == (self = extint_find(pin->port, pin->bit))) {
......
This diff is collapsed.
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Daniel Campora
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
extern const mp_obj_type_t pyb_i2c_type;
void i2c_init0(void);
void i2c_init (uint mode, uint slvaddr, uint baudrate);
void i2c_deinit(void);
......@@ -71,11 +71,16 @@
///
/// g = pyb.Pin('GPIO9', 0)
///
/// And finally, you can also pass a pin number directly:
///
/// g = pyb.Pin(64, 0)
///
/// To summarise, the following order determines how things get mapped into
/// an ordinal pin number:
///
/// 1. Directly specify a Pin object
/// 2. Supply a string which matches a CPU port/pin
/// 2. Supply a string which matches a CPU pin name
/// 3. Provide a pin number
void pin_init0(void) {
}
......@@ -96,6 +101,16 @@ const pin_obj_t *pin_find(mp_obj_t user_obj) {
return pin_obj;
}
// See if the pin number matches a cpu pin
mp_int_t pin_num;
if (mp_obj_get_int_maybe(user_obj, &pin_num)) {
// The Pins dictionary has pin indexes, so we must substract one from the value passed
pin_obj = pin_find_pin(&pin_cpu_pins_locals_dict, (pin_num - 1));
if (pin_obj) {
return pin_obj;
}
}
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
......@@ -218,9 +233,9 @@ STATIC mp_obj_t pin_make_new(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw,
/// - `Pin.OD_PD` - open drain with pull-down resistor.
/// - `Pin.ANALOG` - configured in analog (adc) mode
/// - `strength` can be one of:
/// - `Pin.2MA` - 2ma drive strength;
/// - `Pin.4MA` - 4ma drive strength;
/// - `Pin.6MA` - 6ma drive strength;
/// - `Pin.S2MA` - 2ma drive strength;
/// - `Pin.S4MA` - 4ma drive strength;
/// - `Pin.S6MA` - 6ma drive strength;
///
/// Returns: `None`.
STATIC const mp_arg_t pin_init_args[] = {
......
......@@ -60,6 +60,7 @@ void pin_init0(void);
void pin_config(const pin_obj_t *self, uint af, uint mode, uint type, uint strength);
const pin_obj_t *pin_find(mp_obj_t user_obj);
const pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name);
const pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num);
uint32_t pin_get_mode(const pin_obj_t *self);
uint32_t pin_get_type(const pin_obj_t *self);
uint32_t pin_get_strenght(const pin_obj_t *self);
......
......@@ -41,22 +41,18 @@
#include "runtime.h"
#include "stream.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_uart.h"
#include "rom_map.h"
#include "interrupt.h"
#include "prcm.h"
#include "gpio.h"
#include "uart.h"
#include "pin.h"
#include "pybuart.h"
#include "pybioctl.h"
#include "mpexception.h"
#include "osi.h"
/// \moduleref pyb
/// \class UART - duplex serial communication bus
///
......@@ -90,8 +86,6 @@
///
/// uart.any() # returns True if any characters waiting
/******************************************************************************
DECLARE PRIVATE FUNCTIONS
******************************************************************************/
......@@ -150,16 +144,12 @@ bool uart_init2(pyb_uart_obj_t *self) {
case PYB_UART_1:
self->reg = UARTA0_BASE;
uartPerh = PRCM_UARTA0;
MAP_PinTypeUART(PIN_55, PIN_MODE_3);
MAP_PinTypeUART(PIN_57, PIN_MODE_3);
MAP_UARTIntRegister(UARTA0_BASE, UART0IntHandler);
MAP_IntPrioritySet(INT_UARTA0, INT_PRIORITY_LVL_7);
break;
case PYB_UART_2:
self->reg = UARTA1_BASE;
uartPerh = PRCM_UARTA1;
MAP_PinTypeUART(PIN_58, PIN_MODE_6);
MAP_PinTypeUART(PIN_59, PIN_MODE_6);
MAP_UARTIntRegister(UARTA1_BASE, UART1IntHandler);
MAP_IntPrioritySet(INT_UARTA1, INT_PRIORITY_LVL_7);
break;
......@@ -469,9 +459,10 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
pyb_uart_obj_t *self;
if (MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1] == NULL) {
// create new UART object
self = m_new0(pyb_uart_obj_t, 1);
self = m_new_obj(pyb_uart_obj_t);
self->base.type = &pyb_uart_type;
self->uart_id = uart_id;
self->read_buf = NULL;
self->enabled = false;
MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1] = self;
} else {
......
......@@ -123,6 +123,7 @@ extern const struct _mp_obj_module_t mp_module_network;
mp_obj_list_t pyb_extint_list; \
struct _pyb_uart_obj_t *pyb_stdio_uart; \
struct _pyb_uart_obj_t *pyb_uart_obj_all[2]; \
struct _pyb_i2c_obj_t *pyb_i2c_obj; \
mp_obj_list_t mod_network_nic_list; \
......
......@@ -41,6 +41,8 @@
#include "runtime.h"
#include "repl.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "pin.h"
#include "pybuart.h"
#include "pybpin.h"
#include "pybrtc.h"
......@@ -64,6 +66,8 @@
#include "mpexception.h"
#include "random.h"
#include "pybextint.h"
#include "pybi2c.h"
#include "pins.h"
/******************************************************************************
DECLARE PRIVATE CONSTANTS
......@@ -142,7 +146,13 @@ soft_reset:
mpexception_init0();
uart_init0();
pin_init0();
i2c_init0();
// configure stdio uart pins with the correct af
// param 3 ("mode") is DON'T CARE" for AFs others than GPIO
pin_config(&pin_GPIO1, PIN_MODE_3, 0, PIN_TYPE_STD, PIN_STRENGTH_2MA);
pin_config(&pin_GPIO2, PIN_MODE_3, 0, PIN_TYPE_STD, PIN_STRENGTH_2MA);
// Instantiate the stdio uart
mp_obj_t args[2] = {
mp_obj_new_int(MICROPY_STDIO_UART),
......@@ -151,7 +161,6 @@ soft_reset:
MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
readline_init0();
pin_init0();
extint_init0();
mod_network_init();
wlan_init0();
......
......@@ -58,6 +58,7 @@ Q(chdir)
Q(getcwd)
Q(listdir)
Q(mkdir)
Q(mkdisk)
Q(remove)
Q(rmdir)
Q(unlink)
......@@ -135,6 +136,26 @@ Q(FLOW_TX)
Q(FLOW_RX)
Q(FLOW_TXRX)
// for I2C class
Q(I2C)
Q(mode)
Q(addr)
Q(baudrate)
Q(data)
Q(memaddr)
Q(addr_size)
Q(timeout)
Q(init)
Q(deinit)
Q(is_ready)
Q(scan)
Q(send)
Q(recv)
Q(mem_read)
Q(mem_write)
Q(MASTER)
Q(SLAVE)
// for RTC class
Q(RTC)
Q(datetime)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment