Skip to content

Commit

Permalink
cc3k: Change configurability of the CC3000 module.
Browse files Browse the repository at this point in the history
Optional compiling of the CC3K module is moved from mpconfigboard.h.
To enable compiling of the module, set the MICROPY_PY_CC3K variable,
either on the command line (make MICROPY_PY_CC3K=1) on in the
mpconfigport.mk file.   It is enabled by default.

Setting of the SPI and pins for the CC3K module is now done dynamically
by the arguments to wlan.init().
  • Loading branch information
dpgeorge committed Sep 1, 2014
1 parent 6f70542 commit a2ee194
Show file tree
Hide file tree
Showing 27 changed files with 119 additions and 152 deletions.
41 changes: 22 additions & 19 deletions stmhal/Makefile
Expand Up @@ -22,7 +22,6 @@ HAL_DIR=hal
USBDEV_DIR=usbdev
#USBHOST_DIR=usbhost
FATFS_DIR=fatfs
CC3K_DIR=cc3k
DFU=../tools/dfu.py
# may need to prefix dfu-util with sudo
DFU_UTIL ?= dfu-util
Expand All @@ -39,7 +38,6 @@ INC += -I$(HAL_DIR)/inc
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc_msc_hid/inc
#INC += -I$(USBHOST_DIR)
INC += -I$(FATFS_DIR)/src
INC += -I$(CC3K_DIR)/inc

CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_CORTEX_M4) $(COPT)
Expand Down Expand Up @@ -119,9 +117,6 @@ SRC_C = \
servo.c \
dac.c \
adc.c \
modwlan.c \
modsocket.c \
modselect.c \

SRC_S = \
startup_stm32f40xx.s \
Expand Down Expand Up @@ -186,7 +181,28 @@ SRC_FATFS = $(addprefix $(FATFS_DIR)/src/,\
option/ccsbcs.c \
)

SRC_CC3K = $(addprefix $(CC3K_DIR)/src/,\
ifeq ($(MICROPY_PY_WIZNET5K),1)
WIZNET5K_DIR=drivers/wiznet5k
INC += -I$(TOP)/$(WIZNET5K_DIR)
CFLAGS_MOD += -DMICROPY_PY_WIZNET5K=1
SRC_MOD += modwiznet5k.c
SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\
ethernet/w5200/w5200.c \
ethernet/wizchip_conf.c \
ethernet/socket.c \
internet/dns/dns.c \
)
endif

ifeq ($(MICROPY_PY_CC3K),1)
CC3K_DIR=cc3k
INC += -I$(CC3K_DIR)/inc
CFLAGS_MOD += -DMICROPY_PY_CC3K=1
SRC_MOD += \
modwlan.c \
modsocket.c \
modselect.c
SRC_MOD += $(addprefix $(CC3K_DIR)/src/,\
cc3000_common.c \
evnt_handler.c \
hci.c \
Expand All @@ -202,18 +218,6 @@ SRC_CC3K = $(addprefix $(CC3K_DIR)/src/,\
# patch.c \
# patch_prog.c \
)

ifeq ($(MICROPY_PY_WIZNET5K),1)
WIZNET5K_DIR=drivers/wiznet5k
INC += -I$(TOP)/$(WIZNET5K_DIR)
CFLAGS_MOD += -DMICROPY_PY_WIZNET5K=1
SRC_MOD += modwiznet5k.c
SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\
ethernet/w5200/w5200.c \
ethernet/wizchip_conf.c \
ethernet/socket.c \
internet/dns/dns.c \
)
endif

OBJ =
Expand All @@ -224,7 +228,6 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_FATFS:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_CC3K:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
OBJ += $(BUILD)/pins_$(BOARD).o

Expand Down
1 change: 0 additions & 1 deletion stmhal/boards/HYDRABUS/mpconfigboard.h
Expand Up @@ -16,7 +16,6 @@
#define MICROPY_HW_ENABLE_I2C1 (1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#define MICROPY_HW_ENABLE_SPI3 (1)
#define MICROPY_HW_ENABLE_CC3K (0)

// USRSW/UBTN (Needs Jumper UBTN) is pulled low. Pressing the button makes the input go high.
#define MICROPY_HW_USRSW_PIN (pin_A0)
Expand Down
1 change: 0 additions & 1 deletion stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h
Expand Up @@ -20,7 +20,6 @@
#define MICROPY_HW_ENABLE_I2C1 (0)
#define MICROPY_HW_ENABLE_SPI1 (0)
#define MICROPY_HW_ENABLE_SPI3 (0)
#define MICROPY_HW_ENABLE_CC3K (0)

// USRSW is pulled low. Pressing the button makes the input go high.
#define MICROPY_HW_USRSW_PIN (pin_B11)
Expand Down
8 changes: 0 additions & 8 deletions stmhal/boards/PYBV10/mpconfigboard.h
Expand Up @@ -17,7 +17,6 @@
#define MICROPY_HW_ENABLE_I2C1 (1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#define MICROPY_HW_ENABLE_SPI3 (0)
#define MICROPY_HW_ENABLE_CC3K (1)

// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
#define MICROPY_HW_USRSW_PIN (pin_B3)
Expand All @@ -38,10 +37,3 @@
#define MICROPY_HW_SDCARD_DETECT_PIN (pin_A8)
#define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP)
#define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET)

// CC3K WLAN
#define MICROPY_HW_WLAN_PIN_CS (pin_B12) // Y5
#define MICROPY_HW_WLAN_PIN_EN (pin_B9) // Y4
#define MICROPY_HW_WLAN_PIN_IRQ (pin_B8) // Y3
#define MICROPY_HW_WLAN_SPI_HANDLE (SPIHandle2) // SPI on Y position (Y6=B13=SCK, Y7=B14=MISO, Y8=B15=MOSI)
#define MICROPY_HW_WLAN_IRQ_LINE (8) // B8 on line 8
1 change: 0 additions & 1 deletion stmhal/boards/PYBV3/mpconfigboard.h
Expand Up @@ -16,7 +16,6 @@
#define MICROPY_HW_ENABLE_I2C1 (1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#define MICROPY_HW_ENABLE_SPI3 (0)
#define MICROPY_HW_ENABLE_CC3K (0)

// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
#define MICROPY_HW_USRSW_PIN (pin_A13)
Expand Down
1 change: 0 additions & 1 deletion stmhal/boards/PYBV4/mpconfigboard.h
Expand Up @@ -16,7 +16,6 @@
#define MICROPY_HW_ENABLE_I2C1 (1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#define MICROPY_HW_ENABLE_SPI3 (0)
#define MICROPY_HW_ENABLE_CC3K (0)

// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
#define MICROPY_HW_USRSW_PIN (pin_B3)
Expand Down
8 changes: 0 additions & 8 deletions stmhal/boards/STM32F4DISC/mpconfigboard.h
Expand Up @@ -16,7 +16,6 @@
#define MICROPY_HW_ENABLE_I2C1 (1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#define MICROPY_HW_ENABLE_SPI3 (0)
#define MICROPY_HW_ENABLE_CC3K (0)

// USRSW is pulled low. Pressing the button makes the input go high.
#define MICROPY_HW_USRSW_PIN (pin_A0)
Expand All @@ -32,10 +31,3 @@
#define MICROPY_HW_LED_OTYPE (GPIO_MODE_OUTPUT_PP)
#define MICROPY_HW_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask)
#define MICROPY_HW_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask)

// CC3K WLAN
#define MICROPY_HW_WLAN_PIN_CS (pin_A15)
#define MICROPY_HW_WLAN_PIN_EN (pin_B10)
#define MICROPY_HW_WLAN_PIN_IRQ (pin_B11)
#define MICROPY_HW_WLAN_SPI_HANDLE (SPIHandle2)
#define MICROPY_HW_WLAN_IRQ_LINE (11)
4 changes: 4 additions & 0 deletions stmhal/cc3k/inc/ccspi.h
Expand Up @@ -56,6 +56,10 @@ extern unsigned char wlan_tx_buffer[];
// Prototypes for the APIs.
//
//*****************************************************************************

// the arguments must be of type pin_obj_t* and SPI_HandleTypeDef*
extern void SpiInit(void *spi, const void *pin_cs, const void *pin_en, const void *pin_irq);

extern void SpiOpen(gcSpiHandleRx pfRxHandler);
extern void SpiClose(void);
extern void SpiPauseSpi(void);
Expand Down
3 changes: 0 additions & 3 deletions stmhal/cc3k/src/cc3000_common.c
Expand Up @@ -43,8 +43,6 @@
* Include files
*
*****************************************************************************/
#include "mpconfigport.h"
#if MICROPY_HW_ENABLE_CC3K

#include "cc3000_common.h"
#include "socket.h"
Expand Down Expand Up @@ -164,4 +162,3 @@ UINT32 STREAM_TO_UINT32_f(CHAR* p, UINT16 offset)
//! @}
//
//*****************************************************************************
#endif // MICROPY_HW_ENABLE_CC3K
88 changes: 46 additions & 42 deletions stmhal/cc3k/src/ccspi.c
Expand Up @@ -31,8 +31,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
#include "mpconfigport.h"
#if MICROPY_HW_ENABLE_CC3K

#include <string.h>

Expand All @@ -44,7 +42,6 @@
#include "obj.h"
#include "runtime.h"
#include "pin.h"
#include "genhdr/pins.h"
#include "led.h"
#include "extint.h"
#include "spi.h"
Expand All @@ -58,14 +55,14 @@
#define DEBUG_printf(args...) (void)0
#endif

#define PIN_CS MICROPY_HW_WLAN_PIN_CS
#define PIN_EN MICROPY_HW_WLAN_PIN_EN
#define PIN_IRQ MICROPY_HW_WLAN_PIN_IRQ
#define SPI_HANDLE MICROPY_HW_WLAN_SPI_HANDLE
#define IRQ_LINE MICROPY_HW_WLAN_IRQ_LINE
// these need to be set to valid values before anything in this file will work
STATIC SPI_HandleTypeDef *SPI_HANDLE = NULL;
STATIC const pin_obj_t *PIN_CS = NULL;
STATIC const pin_obj_t *PIN_EN = NULL;
STATIC const pin_obj_t *PIN_IRQ = NULL;

#define CS_LOW() HAL_GPIO_WritePin(PIN_CS.gpio, PIN_CS.pin_mask, GPIO_PIN_RESET)
#define CS_HIGH() HAL_GPIO_WritePin(PIN_CS.gpio, PIN_CS.pin_mask, GPIO_PIN_SET)
#define CS_LOW() HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_RESET)
#define CS_HIGH() HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_SET)

#define READ 3
#define WRITE 1
Expand Down Expand Up @@ -106,10 +103,19 @@ tSpiInformation sSpiInformation;
char spi_buffer[CC3000_RX_BUFFER_SIZE];
unsigned char wlan_tx_buffer[CC3000_TX_BUFFER_SIZE];

static const mp_obj_fun_native_t irq_callback_obj;
STATIC const mp_obj_fun_builtin_t irq_callback_obj;
void SpiWriteDataSynchronous(unsigned char *data, unsigned short size);
void SpiReadDataSynchronous(unsigned char *data, unsigned short size);

// set the pins to use to communicate with the CC3000
// the arguments must be of type pin_obj_t* and SPI_HandleTypeDef*
void SpiInit(void *spi, const void *pin_cs, const void *pin_en, const void *pin_irq) {
SPI_HANDLE = spi;
PIN_CS = pin_cs;
PIN_EN = pin_en;
PIN_IRQ = pin_irq;
}

void SpiClose(void)
{
if (sSpiInformation.pRxPacket) {
Expand All @@ -118,7 +124,7 @@ void SpiClose(void)

tSLInformation.WlanInterruptDisable();

//HAL_SPI_DeInit(&SPI_HANDLE);
//HAL_SPI_DeInit(SPI_HANDLE);
}

void SpiOpen(gcSpiHandleRx pfRxHandler)
Expand All @@ -136,18 +142,18 @@ void SpiOpen(gcSpiHandleRx pfRxHandler)
wlan_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER;

/* SPI configuration */
SPI_HANDLE.Init.Mode = SPI_MODE_MASTER;
SPI_HANDLE.Init.Direction = SPI_DIRECTION_2LINES;
SPI_HANDLE.Init.DataSize = SPI_DATASIZE_8BIT;
SPI_HANDLE.Init.CLKPolarity = SPI_POLARITY_LOW;
SPI_HANDLE.Init.CLKPhase = SPI_PHASE_2EDGE;
SPI_HANDLE.Init.NSS = SPI_NSS_SOFT;
SPI_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
SPI_HANDLE.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPI_HANDLE.Init.TIMode = SPI_TIMODE_DISABLED;
SPI_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
SPI_HANDLE.Init.CRCPolynomial = 7;
spi_init(&SPI_HANDLE);
SPI_HANDLE->Init.Mode = SPI_MODE_MASTER;
SPI_HANDLE->Init.Direction = SPI_DIRECTION_2LINES;
SPI_HANDLE->Init.DataSize = SPI_DATASIZE_8BIT;
SPI_HANDLE->Init.CLKPolarity = SPI_POLARITY_LOW;
SPI_HANDLE->Init.CLKPhase = SPI_PHASE_2EDGE;
SPI_HANDLE->Init.NSS = SPI_NSS_SOFT;
SPI_HANDLE->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
SPI_HANDLE->Init.FirstBit = SPI_FIRSTBIT_MSB;
SPI_HANDLE->Init.TIMode = SPI_TIMODE_DISABLED;
SPI_HANDLE->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
SPI_HANDLE->Init.CRCPolynomial = 7;
spi_init(SPI_HANDLE);

// configure wlan CS and EN pins
GPIO_InitTypeDef GPIO_InitStructure;
Expand All @@ -156,48 +162,48 @@ void SpiOpen(gcSpiHandleRx pfRxHandler)
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Alternate = 0;

GPIO_InitStructure.Pin = PIN_CS.pin_mask;
HAL_GPIO_Init(PIN_CS.gpio, &GPIO_InitStructure);
GPIO_InitStructure.Pin = PIN_CS->pin_mask;
HAL_GPIO_Init(PIN_CS->gpio, &GPIO_InitStructure);

GPIO_InitStructure.Pin = PIN_EN.pin_mask;
HAL_GPIO_Init(PIN_EN.gpio, &GPIO_InitStructure);
GPIO_InitStructure.Pin = PIN_EN->pin_mask;
HAL_GPIO_Init(PIN_EN->gpio, &GPIO_InitStructure);

HAL_GPIO_WritePin(PIN_CS.gpio, PIN_CS.pin_mask, GPIO_PIN_SET);
HAL_GPIO_WritePin(PIN_EN.gpio, PIN_EN.pin_mask, GPIO_PIN_RESET);
HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_SET);
HAL_GPIO_WritePin(PIN_EN->gpio, PIN_EN->pin_mask, GPIO_PIN_RESET);

/* do a dummy read, this ensures SCLK is low before
actual communications start, it might be required */
CS_LOW();
uint8_t buf[1];
HAL_SPI_Receive(&SPI_HANDLE, buf, sizeof(buf), SPI_TIMEOUT);
HAL_SPI_Receive(SPI_HANDLE, buf, sizeof(buf), SPI_TIMEOUT);
CS_HIGH();

// register EXTI
extint_register((mp_obj_t)&PIN_IRQ, GPIO_MODE_IT_FALLING, GPIO_PULLUP, (mp_obj_t)&irq_callback_obj, true, NULL);
extint_enable(IRQ_LINE);
extint_register((mp_obj_t)PIN_IRQ, GPIO_MODE_IT_FALLING, GPIO_PULLUP, (mp_obj_t)&irq_callback_obj, true, NULL);
extint_enable(PIN_IRQ->pin);

DEBUG_printf("SpiOpen finished; IRQ.pin=%d IRQ_LINE=%d\n", PIN_IRQ.pin, IRQ_LINE);
DEBUG_printf("SpiOpen finished; IRQ.pin=%d IRQ_LINE=%d\n", PIN_IRQ->pin, PIN_IRQ->pin);
}


void SpiPauseSpi(void)
{
extint_disable(IRQ_LINE);
extint_disable(PIN_IRQ->pin);
}

void SpiResumeSpi(void)
{
extint_enable(IRQ_LINE);
extint_enable(PIN_IRQ->pin);
}

long ReadWlanInterruptPin(void)
{
return HAL_GPIO_ReadPin(PIN_IRQ.gpio, PIN_IRQ.pin_mask);
return HAL_GPIO_ReadPin(PIN_IRQ->gpio, PIN_IRQ->pin_mask);
}

void WriteWlanPin(unsigned char val)
{
HAL_GPIO_WritePin(PIN_EN.gpio, PIN_EN.pin_mask,
HAL_GPIO_WritePin(PIN_EN->gpio, PIN_EN->pin_mask,
(WLAN_ENABLE)? GPIO_PIN_SET:GPIO_PIN_RESET);
}

Expand Down Expand Up @@ -306,7 +312,7 @@ void SpiWriteDataSynchronous(unsigned char *data, unsigned short size)
{
DEBUG_printf("SpiWriteDataSynchronous(data=%p [%x %x %x %x], size=%u)\n", data, data[0], data[1], data[2], data[3], size);
__disable_irq();
if (HAL_SPI_TransmitReceive(&SPI_HANDLE, data, data, size, SPI_TIMEOUT) != HAL_OK) {
if (HAL_SPI_TransmitReceive(SPI_HANDLE, data, data, size, SPI_TIMEOUT) != HAL_OK) {
//BREAK();
}
__enable_irq();
Expand All @@ -317,7 +323,7 @@ void SpiReadDataSynchronous(unsigned char *data, unsigned short size)
{
memset(data, READ, size);
__disable_irq();
if (HAL_SPI_TransmitReceive(&SPI_HANDLE, data, data, size, SPI_TIMEOUT) != HAL_OK) {
if (HAL_SPI_TransmitReceive(SPI_HANDLE, data, data, size, SPI_TIMEOUT) != HAL_OK) {
//BREAK();
}
__enable_irq();
Expand Down Expand Up @@ -453,5 +459,3 @@ STATIC mp_obj_t irq_callback(mp_obj_t line)
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(irq_callback_obj, irq_callback);

#endif // MICROPY_HW_ENABLE_CC3K
3 changes: 0 additions & 3 deletions stmhal/cc3k/src/evnt_handler.c
Expand Up @@ -42,8 +42,6 @@
//******************************************************************************
// INCLUDE FILES
//******************************************************************************
#include "mpconfigport.h"
#if MICROPY_HW_ENABLE_CC3K

#include "cc3000_common.h"
#include "string.h"
Expand Down Expand Up @@ -849,4 +847,3 @@ void SimpleLinkWaitData(UINT8 *pBuf, UINT8 *from, UINT8 *fromlen)
//! @}
//
//*****************************************************************************
#endif // MICROPY_HW_ENABLE_CC3K
3 changes: 0 additions & 3 deletions stmhal/cc3k/src/hci.c
Expand Up @@ -39,8 +39,6 @@
//! @{
//
//*****************************************************************************
#include "mpconfigport.h"
#if MICROPY_HW_ENABLE_CC3K

#include <string.h>
#include "cc3000_common.h"
Expand Down Expand Up @@ -225,4 +223,3 @@ void hci_patch_send(UINT8 ucOpcode, UINT8 *pucBuff, CHAR *patch, UINT16 usDataLe
//
//
//*****************************************************************************
#endif // MICROPY_HW_ENABLE_CC3K

0 comments on commit a2ee194

Please sign in to comment.