Browse Source

cc3200: Add alternate functions list to Pin object.

Also remove pin.high() and pin.low() methods.
pull/1458/head
Daniel Campora 9 years ago
parent
commit
36821d095a
  1. 1
      cc3200/application.mk
  2. 2
      cc3200/boards/cc3200_af.csv
  3. 16
      cc3200/boards/cc3200_prefix.c
  4. 111
      cc3200/boards/make-pins.py
  5. 71
      cc3200/misc/pin_named_pins.c
  6. 2
      cc3200/mods/pybpin.c
  7. 83
      cc3200/mods/pybpin.h
  8. 2
      cc3200/mods/pybsleep.c
  9. 2
      cc3200/qstrdefsport.h
  10. 8
      docs/library/pyb.Pin.rst
  11. 4
      docs/wipy/quickref.rst
  12. 7
      tests/wipy/pin.py
  13. 4
      tests/wipy/pin.py.exp

1
cc3200/application.mk

@ -77,7 +77,6 @@ APP_HAL_SRC_C = $(addprefix hal/,\
APP_MISC_SRC_C = $(addprefix misc/,\
antenna.c \
FreeRTOSHooks.c \
pin_named_pins.c \
help.c \
mpcallback.c \
mperror.c \

2
cc3200/boards/cc3200_af.csv

@ -53,7 +53,7 @@ Pin,Name,Default,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,AF8,AF9,AF10,AF11,AF12,AF13,AF1
52,RTC_XTAL_N,RTC_XTAL_N,GP32,,I2S0_CLK,,I2S0_DAT0,,UART0_RTS,,SPI0_MOSI,,,,,,,,
53,GP30,GP30,GP30,,I2S0_CLK,I2S0_FS,TIM2_CC1,,,SPI0_MISO,,UART0_TX,,,,,,,
54,VIN_IO2,VIN_IO2,VIN_IO2,,,,,,,,,,,,,,,,
55,GP1,GP1,GP1,,,SPI0_MISO,pCLK (PIXCLK),,UART1_TX,TIM0_CC1,,,,,,,,,
55,GP1,GP1,GP1,,,UART0_TX,pCLK (PIXCLK),,UART1_TX,TIM0_CC1,,,,,,,,,
56,VDD_DIG2,VDD_DIG2,VDD_DIG2,,,,,,,,,,,,,,,,
57,GP2,GP2,GP2,,,UART0_RX,,,UART1_RX,TIM1_CC0,,,,,,,,,ADC0_CH0
58,GP3,GP3,GP3,,,,pDATA7(CAM_D3),,UART1_TX,,,,,,,,,,ADC0_CH1

Can't render this file because it has a wrong number of fields in line 39.

16
cc3200/boards/cc3200_prefix.c

@ -39,17 +39,29 @@
#include "pybpin.h"
#define PIN(p_pin_name, p_port, p_bit, p_pin_num) \
#define AF(af_name, af_idx, af_fn, af_unit, af_type) \
{ \
.name = MP_QSTR_ ## af_name, \
.idx = (af_idx), \
.fn = PIN_FN_ ## af_fn, \
.unit = (af_unit), \
.type = PIN_TYPE_ ## af_fn ## _ ## af_type, \
}
#define PIN(p_pin_name, p_port, p_bit, p_pin_num, p_af_list, p_num_afs) \
{ \
{ &pin_type }, \
.name = MP_QSTR_ ## p_pin_name, \
.port = PORT_A ## p_port, \
.af_list = (p_af_list), \
.pull = PIN_TYPE_STD, \
.bit = (p_bit), \
.pin_num = (p_pin_num), \
.af = PIN_MODE_0, \
.strength = PIN_STRENGTH_4MA, \
.mode = GPIO_DIR_MODE_IN, \
.num_afs = (p_num_afs), \
.value = 0, \
.isused = false, \
.used = false, \
}

111
cc3200/boards/make-pins.py

@ -1,5 +1,5 @@
#!/usr/bin/env python
"""Generates the pins files for the CC3200."""
"""Generates the pins file for the CC3200."""
from __future__ import print_function
@ -8,6 +8,15 @@ import sys
import csv
SUPPORTED_AFS = { 'UART': ('TX', 'RX', 'RTS', 'CTS'),
'SPI': ('CLK', 'MOSI', 'MISO', 'CS0'),
#'I2S': ('CLK', 'FS', 'DAT0', 'DAT1'),
'I2C': ('SDA', 'SCL'),
'TIM': ('PWM0', 'PWM1', 'CC0', 'CC1'),
'SD': ('CLK', 'CMD', 'DAT0'),
'ADC': ('CH0', 'CH1', 'CH2', 'CH3')
}
def parse_port_pin(name_str):
"""Parses a string and returns a (port, gpio_bit) tuple."""
if len(name_str) < 3:
@ -21,7 +30,19 @@ def parse_port_pin(name_str):
return (port, gpio_bit)
class Pin(object):
class AF:
"""Holds the description of an alternate function"""
def __init__(self, name, idx, fn, unit, type):
self.name = name
self.idx = idx
self.fn = fn
self.unit = unit
self.type = type
def print(self):
print (' AF({:16s}, {:4d}, {:8s}, {:4d}, {:8s}), // {}'.format(self.name, self.idx, self.fn, self.unit, self.type, self.name))
class Pin:
"""Holds the information associated with a pin."""
def __init__(self, name, port, gpio_bit, pin_num):
self.name = name
@ -29,45 +50,44 @@ class Pin(object):
self.gpio_bit = gpio_bit
self.pin_num = pin_num
self.board_pin = False
self.afs = []
def cpu_pin_name(self):
return self.name
def is_board_pin(self):
return self.board_pin
def set_is_board_pin(self):
self.board_pin = True
def add_af(self, af):
self.afs.append(af)
def print(self):
print('pin_obj_t pin_{:6s} = PIN({:6s}, {:1d}, {:3d}, {:2d});'.format(
self.name, self.name, self.port, self.gpio_bit, self.pin_num))
print('// {}'.format(self.name))
print('const pin_af_t pin_{}_af[] = {{'.format(self.name))
for af in self.afs:
af.print()
print('};')
print('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, pin_{}_af, {});\n'.format(
self.name, self.name, self.port, self.gpio_bit, self.pin_num, self.name, len(self.afs)))
def print_header(self, hdr_file):
hdr_file.write('extern pin_obj_t pin_{:s};\n'.format(self.name))
class Pins(object):
class Pins:
def __init__(self):
self.cpu_pins = [] # list of pin objects
self.board_pins = [] # list of pin objects
def find_pin(self, port, gpio_bit):
for pin in self.cpu_pins:
for pin in self.board_pins:
if pin.port == port and pin.gpio_bit == gpio_bit:
return pin
def find_pin_by_num(self, pin_num):
for pin in self.cpu_pins:
for pin in self.board_pins:
if pin.pin_num == pin_num:
return pin
def find_pin_by_name(self, name):
for pin in self.cpu_pins:
for pin in self.board_pins:
if pin.name == name:
return pin
def parse_af_file(self, filename, pin_col, pinname_col):
def parse_af_file(self, filename, pin_col, pinname_col, af_start_col):
with open(filename, 'r') as csvfile:
rows = csv.reader(csvfile)
for row in rows:
@ -76,11 +96,21 @@ class Pins(object):
except:
continue
if not row[pin_col].isdigit():
raise ValueError("Invalid pin number: {:s} in row {:s}".format(row[pin_col]), row)
raise ValueError("Invalid pin number {:s} in row {:s}".format(row[pin_col]), row)
# Pin numbers must start from 0 when used with the TI API
pin_num = int(row[pin_col]) - 1;
pin = Pin(row[pinname_col], port_num, gpio_bit, pin_num)
self.cpu_pins.append(pin)
self.board_pins.append(pin)
af_idx = 0
for af in row[af_start_col:]:
af_splitted = af.split('_')
fn_name = af_splitted[0].rstrip('0123456789')
if fn_name in SUPPORTED_AFS:
type_name = af_splitted[1]
if type_name in SUPPORTED_AFS[fn_name]:
unit_idx = af_splitted[0][-1]
pin.add_af(AF(af, af_idx, fn_name, int(unit_idx), type_name))
af_idx += 1
def parse_board_file(self, filename, cpu_pin_col):
with open(filename, 'r') as csvfile:
@ -92,37 +122,44 @@ class Pins(object):
else:
pin = self.find_pin_by_name(row[cpu_pin_col])
if pin:
pin.set_is_board_pin()
pin.board_pin = True
def print_named(self, label, pins):
print('')
print('STATIC const mp_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{'.format(label))
for pin in pins:
if pin.is_board_pin():
print(' {{ MP_OBJ_NEW_QSTR(MP_QSTR_{:6s}), (mp_obj_t)&pin_{:6s} }},'.format(pin.cpu_pin_name(), pin.cpu_pin_name()))
if pin.board_pin:
print(' {{ MP_OBJ_NEW_QSTR(MP_QSTR_{:6s}), (mp_obj_t)&pin_{:6s} }},'.format(pin.name, pin.name))
print('};')
print('MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);'.format(label, label));
def print(self):
for pin in self.cpu_pins:
if pin.is_board_pin():
for pin in self.board_pins:
if pin.board_pin:
pin.print()
self.print_named('board', self.cpu_pins)
self.print_named('board', self.board_pins)
print('')
def print_header(self, hdr_filename):
with open(hdr_filename, 'wt') as hdr_file:
for pin in self.cpu_pins:
if pin.is_board_pin():
for pin in self.board_pins:
if pin.board_pin:
pin.print_header(hdr_file)
def print_qstr(self, qstr_filename):
with open(qstr_filename, 'wt') as qstr_file:
qstr_set = set([])
for pin in self.cpu_pins:
if pin.is_board_pin():
qstr_set |= set([pin.cpu_pin_name()])
for qstr in sorted(qstr_set):
pin_qstr_set = set([])
af_qstr_set = set([])
for pin in self.board_pins:
if pin.board_pin:
pin_qstr_set |= set([pin.name])
for af in pin.afs:
af_qstr_set |= set([af.name])
print('// Board pins', file=qstr_file)
for qstr in sorted(pin_qstr_set):
print('Q({})'.format(qstr), file=qstr_file)
print('\n// Pin AFs', file=qstr_file)
for qstr in sorted(af_qstr_set):
print('Q({})'.format(qstr), file=qstr_file)
@ -169,12 +206,12 @@ def main():
print('//')
if args.af_filename:
print('// --af {:s}'.format(args.af_filename))
pins.parse_af_file(args.af_filename, 0, 1)
pins.parse_af_file(args.af_filename, 0, 1, 3)
if args.board_filename:
print('// --board {:s}'.format(args.board_filename))
pins.parse_board_file(args.board_filename, 1)
pins.parse_board_file(args.board_filename, 1)
if args.prefix_filename:
print('// --prefix {:s}'.format(args.prefix_filename))
print('')

71
cc3200/misc/pin_named_pins.c

@ -1,71 +0,0 @@
/*
* 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.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "py/mpconfig.h"
#include MICROPY_HAL_H
#include "py/obj.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "pybpin.h"
STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pin_named_pins_obj_t *self = self_in;
mp_printf(print, "<Pin.%q>", self->name);
}
const mp_obj_type_t pin_board_pins_obj_type = {
{ &mp_type_type },
.name = MP_QSTR_board,
.print = pin_named_pins_obj_print,
.locals_dict = (mp_obj_t)&pin_board_pins_locals_dict,
};
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
mp_map_elem_t *named_elem = mp_map_lookup(named_map, name, MP_MAP_LOOKUP);
if (named_elem != NULL && named_elem->value != NULL) {
return named_elem->value;
}
return NULL;
}
pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit) {
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)->port == port) &&
(((pin_obj_t *)named_map->table[i].value)->bit == bit)) {
return named_map->table[i].value;
}
}
return NULL;
}

2
cc3200/mods/pybpin.c

@ -779,8 +779,6 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
// instance methods
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pin_init_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&pin_value_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pin_low_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pin_high_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&pin_toggle_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&pin_id_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mode), (mp_obj_t)&pin_mode_obj },

83
cc3200/mods/pybpin.h

@ -29,24 +29,90 @@
#define PYBPIN_H_
enum {
PORT_A0 = GPIOA0_BASE,
PORT_A1 = GPIOA1_BASE,
PORT_A2 = GPIOA2_BASE,
PORT_A3 = GPIOA3_BASE
PORT_A0 = GPIOA0_BASE,
PORT_A1 = GPIOA1_BASE,
PORT_A2 = GPIOA2_BASE,
PORT_A3 = GPIOA3_BASE,
};
enum {
PIN_FN_UART = 0,
PIN_FN_SPI,
PIN_FN_I2S,
PIN_FN_I2C,
PIN_FN_TIM,
PIN_FN_SD,
PIN_FN_ADC,
};
enum {
PIN_TYPE_UART_TX = 0,
PIN_TYPE_UART_RX,
PIN_TYPE_UART_RTS,
PIN_TYPE_UART_CTS,
};
enum {
PIN_TYPE_SPI_CLK = 0,
PIN_TYPE_SPI_MOSI,
PIN_TYPE_SPI_MISO,
PIN_TYPE_SPI_CS0,
};
enum {
PIN_TYPE_I2S_CLK = 0,
PIN_TYPE_I2S_FS,
PIN_TYPE_I2S_DAT0,
PIN_TYPE_I2S_DAT1,
};
enum {
PIN_TYPE_I2C_SDA = 0,
PIN_TYPE_I2C_SCL,
};
enum {
PIN_TYPE_TIM_PWM0 = 0,
PIN_TYPE_TIM_PWM1,
PIN_TYPE_TIM_CC0,
PIN_TYPE_TIM_CC1,
};
enum {
PIN_TYPE_SD_CLK = 0,
PIN_TYPE_SD_CMD,
PIN_TYPE_SD_DAT0,
};
enum {
PIN_TYPE_ADC_CH0 = -1,
PIN_TYPE_ADC_CH1 = -1,
PIN_TYPE_ADC_CH2 = -1,
PIN_TYPE_ADC_CH3 = -1,
};
typedef struct {
qstr name;
uint8_t idx;
uint8_t fn;
uint8_t unit;
uint8_t type;
} pin_af_t;
typedef struct {
const mp_obj_base_t base;
const qstr name;
const uint32_t port;
const pin_af_t *af_list;
uint16_t pull;
const uint8_t bit;
const uint8_t pin_num;
int8_t af;
uint8_t strength;
uint8_t mode; // this is now a combination of type and mode
int8_t value; // -1 means no defined value
bool isused;
uint8_t mode; // this is now a combination of type and mode
uint8_t num_afs: 6; // up to 63 AFs
uint8_t value : 1;
uint8_t used : 1;
} pin_obj_t;
extern const mp_obj_type_t pin_type;
@ -68,7 +134,6 @@ extern const mp_obj_dict_t pin_board_pins_locals_dict;
void pin_init0(void);
void pin_config(pin_obj_t *self, int af, uint mode, uint type, int value, uint strength);
pin_obj_t *pin_find(mp_obj_t user_obj);
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name);
pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit);
int8_t pin_find_af_index(const pin_obj_t* pin, uint8_t fn, uint8_t unit, uint8_t type);
#endif // PYBPIN_H_

2
cc3200/mods/pybsleep.c

@ -474,7 +474,7 @@ STATIC void pybsleep_iopark (bool hibernate) {
#endif
default:
// enable a weak pull-down if the pin is unused
if (!pin->isused) {
if (!pin->used) {
MAP_PinConfigSet(pin->pin_num, pin->strength, PIN_TYPE_STD_PD);
}
if (hibernate) {

2
cc3200/qstrdefsport.h

@ -113,8 +113,6 @@ Q(Pin)
Q(board)
Q(init)
Q(value)
Q(low)
Q(high)
Q(toggle)
Q(id)
Q(mode)

8
docs/library/pyb.Pin.rst

@ -185,14 +185,6 @@ Methods
Get the pin id.
.. method:: pin.high()
Set the pin to a high logic level.
.. method:: pin.low()
Set the pin to a low logic level.
.. method:: pin.value([value])
Get or set the digital logic level of the pin:

4
docs/wipy/quickref.rst

@ -29,8 +29,8 @@ See :ref:`pyb.Pin <pyb.Pin>`. ::
# initialize GP2 in gpio mode (af=0) and make it an output
p_out = Pin('GP2', mode=Pin.OUT)
p_out.high()
p_out.low()
p_out.value(1)
p_out.value(0)
p_out.toggle()
p_out(True)

7
tests/wipy/pin.py

@ -75,12 +75,7 @@ print(pin)
# test value in OUT mode
pin = Pin(pin_map[0], mode=Pin.OUT)
pin.high() # test high
print(pin.value())
print(pin())
pin.low() # test low
print(pin.value())
print(pin())
pin.value(0)
pin.toggle() # test toggle
print(pin())
pin.toggle() # test toggle again

4
tests/wipy/pin.py.exp

@ -35,10 +35,6 @@ Pin('GP23', mode=Pin.OUT, pull=Pin.PULL_UP, drive=Pin.HIGH_POWER, alt=-1)
Pin('GP23', mode=Pin.ALT, pull=Pin.PULL_NONE, drive=Pin.MED_POWER, alt=1)
Pin('GP23', mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_NONE, drive=Pin.MED_POWER, alt=15)
1
1
0
0
1
0
1
0

Loading…
Cancel
Save