Koduino
types.h
1 
19 #ifndef types_h
20 #define types_h
21 
22 #include <stdint.h>
23 #include "chip.h"
24 
25 #ifdef __cplusplus
26 extern "C"{
27 #endif // __cplusplus
28 
29 // Types needed by variant.h are in here
30 #define NOT_SET 0xff
31 
32 // For the ioConfig bitmask
33 #define IOCFGBIT_ACTIVELOW 0 //
34 #define IOCFGBIT_BOARDLED 1 //
35 #define IOCFGBIT_PWM 2
36 // #define IOCFGBIT_PIN_MODE 2 //
37 // #define
38 // 0 => Digital, 1=> Timer PWM, 2=> Timer pulsein, 3=> Timer encoder, 4=>Analog input, 5=> EXTI, 6=>USART, 7=>I2C
39 
40 typedef struct PinInfo {
41  // GPIO port
42  GPIO_TypeDef * const port;
43  // GPIO pinSource
44  const uint8_t pin;
45  // ADC channel (if it is not an analog pin, adcChannel is set to 0xFF)
46  // FIXME if there are multiple ADC's make this a mux? lower nibble will be channel (0-15)
47  // and upper nibble will be binary ADC4|ADC3|ADC2|ADC1. E.g. if a pin is on ADC1, ADC2,
48  // and channel 1,
49  // this will be 00110001
50  // Currently the upper nibble is unused
51  const uint8_t adcChannel;
52  // Alternate function
53  uint8_t altFunc;
54  // Timer name (if this pin is connected to a timer)
55  uint8_t timer;
56  // Time channel (if this pin is connected to a timer)
57  uint8_t channel;
58  // Index into the array of ADC conversions (not all channels may be activated)
59  uint8_t adcConvIndex;
60  // Bitmask, where
61  // bit 0 = (0=>active high, 1=>active low) only for LED
62  // bit 1 = (0=>general pin, 1=>board LED)
63  // bit 2 = ?
64  uint8_t ioConfig;
65 } __attribute__ ((packed)) PinInfo;
66 
67 
68 // Timer channel configuration - changes at runtime
69 typedef struct TimerChannelData {
70  // Reference to the pin attached to this channel (for analogWrite, etc.)
71  uint8_t pin;
72  // PulseIn
73  uint8_t bPwmIn;
74  volatile int risingEdge, pulseWidth, period;
75  volatile uint32_t lastRollover;
76 } TimerChannelData;
77 
78 // Timer info
79 typedef struct TimerInfo {
80  TIM_TypeDef * const TIMx;
81  const IRQn_Type IRQn;
82  // non-constant
83  // frequency
84  int freqHz;
85  // how many times rolled over (for pwmIn)
86  volatile uint32_t numRollovers;
87  // variable number of channels; assigned in variant.cpp
88  TimerChannelData *channelData;
89 } TimerInfo;
90 
91 // pinMode
92 typedef enum WiringPinMode {
93  OUTPUT, OUTPUT_OPEN_DRAIN,
94  INPUT, INPUT_ANALOG, INPUT_PULLUP, INPUT_PULLDOWN,
95  PWM,
96  PWM_IN, PWM_IN_EXTI
97 } WiringPinMode;
98 
99 // Logic levels
100 typedef enum LogicValue {
101  LOW = 0, HIGH = 1, TOGGLE = 2
102 } LogicValue;
103 
104 // General prototype for a user callback (like from timer or external interrupts)
105 typedef void (*ISRType)(void);
106 
107 // Timebase
108 typedef struct {
109  uint8_t timer;
110  ISRType isr;
111 } __attribute__ ((packed)) TimebaseChannel;
112 
113 // USART
114 #define SERIAL_BUFFER_SIZE 80
115 
116 typedef struct RingBuffer {
117  uint8_t buffer[SERIAL_BUFFER_SIZE];
118  volatile uint8_t head;
119  volatile uint8_t tail;
120 } RingBuffer;
121 
122 typedef void (*ByteFunc)(uint8_t);
123 
124 typedef struct USARTInfo {
125  // STM32 stuff
126  USART_TypeDef *USARTx;
127  IRQn_Type irqChannel;
128  uint8_t txPin, rxPin;
129 
130  // Buffer pointers. These need to be global for IRQ handler access
131  RingBuffer* txBuf;
132  RingBuffer* rxBuf;
133 
134  // for callback on RX
135  ByteFunc rxCallback;
136  void *busObject;
137 } USARTInfo;
138 
143 typedef enum InterruptTrigger {
144  RISING, FALLING, CHANGE
145 } InterruptTrigger;
146 
147 
148 // Create a structure for user ISR function pointers
149 typedef struct {
150  void (*handler)();
151  uint8_t bPwmIn, pinName;
152  volatile int risingEdgeMs, risingEdgeSubMs, pulsewidth, period;
153 } EXTIChannel;
154 
155 
156 /* Define attribute */
157 #if defined ( __CC_ARM ) /* Keil uVision 4 */
158  #define WEAK (__attribute__ ((weak)))
159 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
160  #define WEAK __weak
161 #elif defined ( __GNUC__ ) /* GCC CS */
162  #define WEAK __attribute__ ((weak))
163 #endif
164 
165 
166 // Must be defined in variant.cpp
167 extern PinInfo PIN_MAP[];
168 extern TimerInfo TIMER_MAP[];
169 extern TimebaseChannel TIMEBASE_MAP[];
170 // extern const uint8_t SYSCLK_TIMEBASE;
171 extern USARTInfo USART_MAP[];
172 extern EXTIChannel EXTI_MAP[];
173 
174 
175 #ifdef __cplusplus
176 } // extern "C"
177 #endif // __cplusplus
178 
179 #endif
Definition: types.h:108