Stepper-Motor-Control  v3.0.0
System on a Chip 2014 - Group 04
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
registerAccess.h
Go to the documentation of this file.
1 /**
2  *****************************************************************************
3  * @file registerAccess.h
4  * @author Michael Riedel
5  * @author Marc Kossmann
6  * @version v2.0.0
7  * @date 18.11.2014
8  * @brief Header file with inline functions to access the registers that
9  * are used by the Stepper-Motor-Control VHDL-component.
10  * @details The bitmask is as follows:
11  * | Bit | Function| Usage |
12  * | :--: | :-----: | :-----------------------------------------------------------------|
13  * | 7 | IR | The Interrupt that will be set via the VHDL Stepper-Motor-Control |
14  * | 6 | IE | IE (Interrupt Enable) to enable the interrupt for Chain of Steps |
15  * | 2..5 | MODE | Mode according to table below |
16  * | 1 | LR | Turn the motor Left (0) or Right (1) |
17  * | 0 | RS | Run (1) or Stop (0) the motor |
18  *
19  * | Modes | Effect |
20  * | :---: | :-----------------------------|
21  * | xx00 | Stop |
22  * | xx01 | Continuous Run |
23  * | 0010 | Chain of Steps (1/4 rotation) |
24  * | 0110 | Chain of Steps (1/2 rotation) |
25  * | 1010 | Chain of Steps (1 rotation) |
26  * | 1110 | Chain of Steps (2 rotations) |
27  * | other | reserved |
28  *****************************************************************************
29  * @par History:
30  * @details v0.1.0 02.11.2014 Riedel
31  * - first draft for milestone 1b
32  * @details v0.1.1 03.11.2014 Kossmann
33  * - moved all register masks in this file
34  * - added registerMutex for save access
35  * @details v0.1.2 04.11.2014 Kossmann
36  * - removed switches masks defines. They already exist in
37  * hardwareAccess.h
38  * @details v1.0.0 11.11.2014 Riedel & Kossmann
39  * - Moved register masks to headers' documentation
40  * - Added ctrlRegBitClr and ctrlRegBitSet functions for bitwise control
41  * @details v1.0.1 15.11.2014 Kossmann
42  * - adopted all functions to use real register interface
43  * @details v2.0.0 18.11.2014 Riedel & Kossmann
44  * - fixed mistake in register addressing offset
45  * - verified functionality -> release MS2
46  *****************************************************************************
47  */
48 
49 #ifndef __REGISTER_ACCESS_H__
50 #define __REGISTER_ACCESS_H__
51 
52 #include <stdint.h>
53 #include <io.h>
54 #include "includes.h"
55 #include "debugAndErrorOutput.h"
56 
57 #define CTRL_REG_RS_MSK (0b00000001) //!< Run (1) or Stop (0)-Bit
58 #define CTRL_REG_LR_MSK (0b00000010) //!< Left (0) or Right (1)-Bit
59 #define CTRL_REG_MODE_MSK (0b00111100) //!< Mode-combination according to ctrlRegSet()-function
60 #define CTRL_REG_IE_MSK (0b01000000) //!< Interrupt-Enable Bit
61 #define CTRL_REG_IR_MSK (0b10000000) //!< Interrupt-Request Bit
62 #define MODE_STOP_CON_RUN_MSK (0b0011) //!< Bit 0 & 1 only
63 #define MODE_STOP (0b0000) //!< MODE_STOP_CON_RUN_MSK; Must be `0b00`
64 #define MODE_CON_RUN (0b0001) //!< Use "MODE_STOP_CON_RUN_MSK"; Must be `0b01`
65 #define MODE_CH_OF_ST_1_4 (0b0010) //!< Bits must be `0b0010`
66 #define MODE_CH_OF_ST_1_2 (0b0110) //!< Bits must be `0b0110`
67 #define MODE_CH_OF_ST_1 (0b1010) //!< Bits must be `0b1010`
68 #define MODE_CH_OF_ST_2 (0b1110) //!< Bits must be `0b1110`
69 
70 
71 /**
72  * @brief base address of the complete register interface component
73  */
74 #define IOWR_REGS_ADDR(base, addr, data) IOWR(base, addr, data)
75 
76 /**
77  * @name Ctrl-Register
78  */
79 /** @brief address offset Ctrl-Register */
80 #define REGS_CTRL 0b000
81 
82 /** @brief Macro to calculate the address of the Ctrl-Register */
83 #define IOADDR_REGS_CTRL(base) \
84  __IO_CALC_ADDRESS_NATIVE(base, REGS_CTRL)
85 /** @brief Macro to read Ctrl-Register */
86 #define IORD_REGS_CTRL(base) IORD(base, REGS_CTRL)
87 /** @brief Macro to write Ctrl-Register */
88 #define IOWR_REGS_CTRL(base, data) IOWR(base, REGS_CTRL, data)
89 
90 /**
91  * @brief Overwrites the complete CtrlReg
92  * @details CtrlReg content is stored in external register interface
93  * @param newCtrlReg The new register-content to set
94  */
95 static __inline__ void ctrlRegSet(uint8_t newCtrlReg) {
96  IOWR_REGS_CTRL(REGISTERS_BASE , newCtrlReg);
97 }
98 
99 /**
100  * @brief Returns the actual content of the control-register.
101  * @details CtrlReg content is stored in external register interface
102  * @return The actual content of the control-register
103  */
104 static __inline__ uint8_t ctrlRegGet(void) {
105  return IORD_REGS_CTRL(REGISTERS_BASE);
106 }
107 
108 /**
109  * @name CtrlSet-Register
110  */
111 /** @brief address offset CtrlSet-Register */
112 #define REGS_CTRL_SET 0b001
113 
114 /** @brief Macro to calculate the address of the CtrlSet-Register */
115 #define IOADDR_REGS_CTRL_SET(base) \
116  __IO_CALC_ADDRESS_NATIVE(base, REGS_CTRL_SET)
117 /** @brief Macro to read CtrlSet-Register */
118 #define IORD_REGS_CTRL_SET(base) IORD(base, REGS_CTRL_SET)
119 /** @brief Macro to write CtrlSet-Register */
120 #define IOWR_REGS_CTRL_SET(base, data) IOWR(base, REGS_CTRL_SET, data)
121 
122 /**
123  * @brief Sets the CtrlReg bitwise
124  * @details Writes into ctrlSetReg in register interface which modifies the
125  * ctrlReg accordingly.
126  * @param bitsToSet Bits to set in CtrlReg
127  */
128 static __inline__ void ctrlRegBitSet(uint8_t bitsToSet) {
129  IOWR_REGS_CTRL_SET(REGISTERS_BASE, bitsToSet);
130 }
131 
132 /**
133  * @name CtrlClr-Register
134  */
135 /** @brief address offset CtrlClr-Register */
136 #define REGS_CTRL_CLR 0b010
137 
138 /** @brief Macro to calculate the address of the CtrlClr-Register */
139 #define IOADDR_REGS_CTRL_CLR(base) \
140  __IO_CALC_ADDRESS_NATIVE(base, REGS_CTRL_CLR)
141 /** @brief Macro to read CtrlClr-Register */
142 #define IORD_REGS_CTRL_CLR(base) IORD(base, REGS_CTRL_CLR)
143 /** @brief Macro to write CtrlClr-Register */
144 #define IOWR_REGS_CTRL_CLR(base, data) IOWR(base, REGS_CTRL_CLR, data)
145 
146 /**
147  * @brief Clears the CtrlReg bitwise
148  * @details Writes into ctrlClrReg in register interface which modifies the
149  * ctrlReg accordingly.
150  * @param bitsToClr Bits to clear in CtrlReg
151  */
152 static __inline__ void ctrlRegBitClr(uint8_t bitsToClr) {
153  IOWR_REGS_CTRL_CLR(REGISTERS_BASE, bitsToClr);
154 }
155 
156 /**
157  * @name Speed-Register
158  */
159 /** @brief address offset Speed-Register */
160 #define REGS_SPEED 0b011
161 
162 /** @brief Macro to calculate the address of the Speed-Register */
163 #define IOADDR_REGS_SPEED(base) \
164  __IO_CALC_ADDRESS_NATIVE(base, REGS_SPEED)
165 /** @brief Macro to read Speed-Register */
166 #define IORD_REGS_SPEED(base) IORD(base, REGS_SPEED)
167 /** @brief Macro to write Speed-Register */
168 #define IOWR_REGS_SPEED(base, data) IOWR(base, REGS_SPEED, data)
169 
170 /**
171  * @brief Sets the speed, how fast the Stepper-Motor-Control turns the motor.
172  * @details The speed ranges from 0 (very slow, used for debugging, each step can be
173  * monitored with the LEDs) to 7 (maximum speed). See the following table for
174  * more information:
175  * | speed | pulse-width (t_pulse) |
176  * | :---: | :-------------------: |
177  * | 0 | 2 s |
178  * | 1 | 1 s |
179  * | 2 | 500 ms |
180  * | 3 | 250 ms |
181  * | 4 | 100 ms |
182  * | 5 | 50 ms |
183  * | 6 | 25 ms |
184  * | 7 | 10 ms |
185  *
186  * @param newSpeed The new speed from 0 to 7 (only the first 3 bits are used!).
187  */
188 static __inline__ void speedRegSet(uint8_t newSpeed) {
189  IOWR_REGS_SPEED(REGISTERS_BASE, newSpeed);
190 }
191 
192 /**
193  * @brief Returns the actual content of the speed-register.
194  * @details The speed-step is used accordingly to the speedRegSet()-function.
195  * @return The actual content of the speed-step which is set (only the first 3 bits
196  * are used ,the rest is reserved!).
197  */
198 static __inline__ uint8_t speedRegGet(void) {
199  return IORD_REGS_SPEED(REGISTERS_BASE);
200 }
201 
202 /**
203  * @name Steps-Register
204  */
205 /** @brief address offset Steps-Register */
206 #define REGS_STEPS 0b100
207 
208 /** @brief Macro to calculate the address of the Steps-Register */
209 #define IOADDR_REGS_STEPS(base) \
210  __IO_CALC_ADDRESS_NATIVE(base, REGS_STEPS)
211 /** @brief Macro to read Steps-Register */
212 #define IORD_REGS_STEPS(base) IORD(base, REGS_STEPS)
213 /** @brief Macro to write Steps-Register */
214 #define IOWR_REGS_STEPS(base, data) IOWR(base, REGS_STEPS, data)
215 
216 /**
217  * @brief Sets the given steps for the Stepper-Motor-Control VHDL-component.
218  * @details The VHDL-component moves the motor according to this step-count.
219  *
220  * @param newStepCount The number of steps, the motor should turn.
221  */
222 static __inline__ void stepsRegSet(uint32_t newStepCount) {
223  IOWR_REGS_STEPS(REGISTERS_BASE, newStepCount);
224 }
225 
226 /**
227  * @brief Returns the actual content of the steps-register.
228  * @details The register contains a 32-bit integer value since the last start of the motor.
229  * @return The actual content of steps left.
230  */
231 static __inline__ uint32_t stepsRegGet(void) {
232  return IORD_REGS_STEPS(REGISTERS_BASE);
233 }
234 
235 #endif // __REGISTER_ACCESS_H__
#define IOWR_REGS_CTRL_CLR(base, data)
Macro to write CtrlClr-Register.
#define IORD_REGS_STEPS(base)
Macro to read Steps-Register.
#define IORD_REGS_CTRL(base)
Macro to read Ctrl-Register.
static __inline__ void stepsRegSet(uint32_t newStepCount)
Sets the given steps for the Stepper-Motor-Control VHDL-component.
#define IOWR_REGS_STEPS(base, data)
Macro to write Steps-Register.
static __inline__ void ctrlRegBitSet(uint8_t bitsToSet)
Sets the CtrlReg bitwise.
static __inline__ void speedRegSet(uint8_t newSpeed)
Sets the speed, how fast the Stepper-Motor-Control turns the motor.
#define IOWR_REGS_SPEED(base, data)
Macro to write Speed-Register.
#define IOWR_REGS_CTRL_SET(base, data)
Macro to write CtrlSet-Register.
#define IOWR_REGS_CTRL(base, data)
Macro to write Ctrl-Register.
#define IORD_REGS_SPEED(base)
Macro to read Speed-Register.
static __inline__ uint32_t stepsRegGet(void)
Returns the actual content of the steps-register.
static __inline__ uint8_t speedRegGet(void)
Returns the actual content of the speed-register.
static __inline__ void ctrlRegBitClr(uint8_t bitsToClr)
Clears the CtrlReg bitwise.
static __inline__ void ctrlRegSet(uint8_t newCtrlReg)
Overwrites the complete CtrlReg.
static __inline__ uint8_t ctrlRegGet(void)
Returns the actual content of the control-register.
Defines for Error and Debug-Output Management.