| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
|
| 7 |
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../../tools")) |
| 8 |
import boardgen |
| 9 |
|
| 10 |
|
| 11 |
class RenesasRaPin(boardgen.Pin): |
| 12 |
def __init__(self, cpu_pin_name): |
| 13 |
super().__init__(cpu_pin_name) |
| 14 |
|
| 15 |
self._port = int(cpu_pin_name[1], 16) |
| 16 |
self._bit = int(cpu_pin_name[2:]) |
| 17 |
|
| 18 |
self._adc_name = None |
| 19 |
self._adc_bits = 0 |
| 20 |
self._adc_channel = 0 |
| 21 |
|
| 22 |
def add_adc(self, adc): |
| 23 |
# The previous version of make-pins.py handled len==4 to mean |
| 24 |
# adc_bits=8, but this is not used in any of the current af.csv files, |
| 25 |
# so this needs to be validated. |
| 26 |
assert len(adc) == 5 and (adc.startswith("AN0") or adc.startswith("AN1")) |
| 27 |
self._adc_name = adc |
| 28 |
self._adc_bit = 12 |
| 29 |
self._adc_channel = int(adc[2:]) |
| 30 |
|
| 31 |
# Called for each AF defined in the csv file for this pin. |
| 32 |
def add_af(self, af_idx, af_name, af): |
| 33 |
if af_idx == 0 and af: |
| 34 |
assert af_name == "Analog" |
| 35 |
self.add_adc(af) |
| 36 |
|
| 37 |
# Use the PIN() macro defined in ra_pin_prefix.c for defining the pin |
| 38 |
# objects. |
| 39 |
def definition(self): |
| 40 |
# PIN(p_name, p_pin, p_adc) |
| 41 |
adc = "&pin_{}_adc_obj".format(self.name()) if self._adc_name else "NULL" |
| 42 |
return "PIN({:s}, {:d}, {:s})".format(self.name(), self._port * 16 + self._bit, adc) |
| 43 |
|
| 44 |
# This will be called at the start of the output (after the prefix). Use |
| 45 |
# it to emit the ADC objects (via the PIN_ADC() macro). |
| 46 |
def print_source(self, out_source): |
| 47 |
# PIN_ADC(p_name, p_pin, adc_bit, adc_channel) |
| 48 |
if self._adc_name: |
| 49 |
print( |
| 50 |
"const machine_pin_adc_obj_t pin_{}_adc_obj = PIN_ADC({:s}, {:d}, {:d}, {:d});".format( |
| 51 |
self.name(), |
| 52 |
self._adc_name, |
| 53 |
self._port * 16 + self._bit, |
| 54 |
self._adc_bits, |
| 55 |
self._adc_channel, |
| 56 |
), |
| 57 |
file=out_source, |
| 58 |
) |
| 59 |
|
| 60 |
# RA cpu names must be "PXNN", where X is hexadecimal port, NN is decimal bit offset. |
| 61 |
@staticmethod |
| 62 |
def validate_cpu_pin_name(cpu_pin_name): |
| 63 |
boardgen.Pin.validate_cpu_pin_name(cpu_pin_name) |
| 64 |
|
| 65 |
if not re.match("^P[0-9A-F][0-9][0-9]$", cpu_pin_name): |
| 66 |
raise boardgen.PinGeneratorError("Invalid cpu pin name '{}'".format(cpu_pin_name)) |
| 67 |
|
| 68 |
|
| 69 |
class RenesasRaPinGenerator(boardgen.PinGenerator): |
| 70 |
def __init__(self): |
| 71 |
# Use custom pin type above, and also enable the --af-csv argument so |
| 72 |
# that add_af gets called on each pin. |
| 73 |
super().__init__( |
| 74 |
pin_type=RenesasRaPin, |
| 75 |
enable_af=True, |
| 76 |
) |
| 77 |
|
| 78 |
# Override the default implementation just to change the default arguments. |
| 79 |
def parse_af_csv(self, filename): |
| 80 |
return super().parse_af_csv(filename, header_rows=1, pin_col=0, af_col=3) |
| 81 |
|
| 82 |
# Renesas-specific behavior, we use pin_PNNN for the cpu names, but |
| 83 |
# pyb_pin_FOO for the board names. |
| 84 |
def board_name_define_prefix(self): |
| 85 |
return "pyb_" |
| 86 |
|
| 87 |
|
| 88 |
if __name__ == "__main__": |
| 89 |
RenesasRaPinGenerator().main() |