Skip to content

Commit 3060a4b

Browse files
committed
Added DFR0538 motordriver for 4xPWM DC motor and 8x servo, added I2C write_buffer API to HAL, added external I2C functionality (internal was already possible, both can be used at the same time since they use different hardware addresses), added example to use motordriver in mecanumcar project, update ultrasonic driver (needs more work)
1 parent 5cbc2b8 commit 3060a4b

File tree

16 files changed

+699
-13
lines changed

16 files changed

+699
-13
lines changed

arch/ARM/Nordic/drivers/nrf_common/nrf-twi.adb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
2929
-- --
3030
------------------------------------------------------------------------------
31-
3231
with NRF_SVD.TWI; use NRF_SVD.TWI;
32+
--with Microbit.Console; use Microbit.Console;
3333

3434
package body nRF.TWI is
3535

@@ -143,6 +143,8 @@ package body nRF.TWI is
143143
loop
144144

145145
loop
146+
--Put("Send: ");
147+
--Put_Line(This.Periph.TXD.TXD'Image); --used for: debug bytes send
146148

147149
Evt_Err := This.Periph.EVENTS_ERROR;
148150
if Evt_Err /= 0 then
@@ -237,6 +239,9 @@ package body nRF.TWI is
237239
This.Periph.EVENTS_RXDREADY := 0;
238240
Data (Index) := This.Periph.RXD.RXD;
239241

242+
--Put("Receive: ");
243+
--Put_Line(This.Periph.RXD.RXD'Image); --used for: debug bytes received
244+
240245
if Index = Data'Last - 1 and then This.Do_Stop_Sequence then
241246

242247
-- Configure SHORTS to automatically stop the TWI port and produce
@@ -268,7 +273,7 @@ package body nRF.TWI is
268273

269274
This.Do_Stop_Sequence := False;
270275

271-
case Mem_Addr_Size is
276+
case Mem_Addr_Size is
272277
when Memory_Size_8b =>
273278
This.Master_Transmit (Addr => Addr,
274279
Data => (0 => UInt8 (Mem_Addr)),
@@ -296,6 +301,21 @@ package body nRF.TWI is
296301
Timeout => Timeout);
297302
end Mem_Write;
298303

304+
overriding procedure Mem_Write_Buffer
305+
(This : in out TWI_Master;
306+
Addr : I2C_Address;
307+
Data : I2C_Data;
308+
Status : out I2C_Status;
309+
Timeout : Natural := 1000) is
310+
begin
311+
This.Master_Transmit (Addr => Addr,
312+
Data => Data,
313+
Status => Status,
314+
Timeout => Timeout);
315+
316+
-- This.Stop_Sequence;
317+
end Mem_Write_Buffer;
318+
299319
--------------
300320
-- Mem_Read --
301321
--------------
@@ -338,5 +358,4 @@ package body nRF.TWI is
338358
Status => Status,
339359
Timeout => Timeout);
340360
end Mem_Read;
341-
342361
end nRF.TWI;

arch/ARM/Nordic/drivers/nrf_common/nrf-twi.ads

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,20 @@ package nRF.TWI is
9494
Timeout : Natural := 1000)
9595
with Pre => Enabled (This);
9696

97+
overriding
98+
procedure Mem_Write_Buffer
99+
(This : in out TWI_Master;
100+
Addr : I2C_Address;
101+
Data : I2C_Data;
102+
Status : out I2C_Status;
103+
Timeout : Natural := 1000)
104+
with Pre => Enabled (This);
105+
97106
private
98107

99108
type TWI_Master (Periph : not null access NRF_SVD.TWI.TWI_Peripheral) is
100109
new HAL.I2C.I2C_Port with record
101-
Do_Stop_Sequence : Boolean := True;
110+
Do_Stop_Sequence : Boolean := True; --do not repeat by default, so send stop condition
102111
end record;
103112

104113
end nRF.TWI;

boards/MicroBit_v2/src/zfp/microbit-i2c.adb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,23 @@ with nRF.TWI;
3535
package body MicroBit.I2C is
3636

3737
Init_Done : Boolean := False;
38+
Init_DoneExt : Boolean := False;
3839

3940
Device : nRF.TWI.TWI_Master renames nRF.Device.TWI_0;
4041
-- This device should not conflict with the device used in MicroBit.SPI.
4142
-- See nRF Series Reference Manual, chapter Memory.Instantiation.
4243

44+
--use this interface for external I2C
45+
DeviceExt : nRF.TWI.TWI_Master renames nRF.Device.TWI_1;
4346
-----------------
4447
-- Initialized --
4548
-----------------
4649

4750
function Initialized return Boolean
4851
is (Init_Done);
4952

53+
function InitializedExt return Boolean
54+
is (Init_DoneExt);
5055
----------------
5156
-- Initialize --
5257
----------------
@@ -66,11 +71,27 @@ package body MicroBit.I2C is
6671
Init_Done := True;
6772
end Initialize;
6873

74+
procedure InitializeExt (S : Speed := S400kbps) is
75+
begin
76+
DeviceExt.Configure
77+
(SCL => MB_SCL_EXT.Pin,
78+
SDA => MB_SDA_EXT.Pin,
79+
Speed => (case S is
80+
when S100kbps => nRF.TWI.TWI_100kbps,
81+
when S250kbps => nRF.TWI.TWI_250kbps,
82+
when S400kbps => nRF.TWI.TWI_400kbps)
83+
);
84+
85+
DeviceExt.Enable;
86+
Init_DoneExt := True;
87+
end InitializeExt;
6988
----------------
7089
-- Controller --
7190
----------------
7291

7392
function Controller return not null Any_I2C_Port
7493
is (Device'Access);
7594

95+
function ControllerExt return not null Any_I2C_Port
96+
is (DeviceExt'Access);
7697
end MicroBit.I2C;

boards/MicroBit_v2/src/zfp/microbit-i2c.ads

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,29 @@ package MicroBit.I2C is
3737

3838
function Initialized return Boolean;
3939
-- Return True if the I2C controller is initialized and ready to use
40+
function InitializedExt return Boolean;
41+
-- Return True if the I2C controller is initialized and ready to use
42+
4043

4144
procedure Initialize (S : Speed := S400kbps)
4245
with Post => Initialized;
4346
-- Initialize the I2C controller at given speed, using the micro:bit I2C
4447
-- pins:
48+
-- - P31 -> SCL
49+
-- - P30 -> SDA
50+
51+
procedure InitializeExt (S : Speed := S400kbps)
52+
with Post => InitializedExt;
53+
-- Initialize the I2C controller at given speed, using the micro:bit I2C
54+
-- external pins:
4555
-- - P19 -> SCL
4656
-- - P20 -> SDA
4757

58+
4859
function Controller return not null Any_I2C_Port;
4960
-- Return the HAL.I2C controller implementation
5061

62+
function ControllerExt return not null Any_I2C_Port;
63+
-- Return the HAL.I2C controller implementation
64+
5165
end MicroBit.I2C;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2018, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of the copyright holder nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
with MicroBit.I2C;
32+
package body MicroBit.MotorDriver is
33+
34+
35+
MD : DFR0548.MotorDriver (MicroBit.I2C.ControllerExt);
36+
37+
procedure Initialize is
38+
begin
39+
if not MicroBit.I2C.InitializedExt then
40+
MicroBit.I2C.InitializeExt;
41+
end if;
42+
43+
MD.Initialize;
44+
MD.Set_Frequency_Hz (50); --50 Hz
45+
end Initialize;
46+
47+
procedure Drive (Direction : Directions;
48+
Speed : Speeds := (4095,4095,4095,4095)) is
49+
--Note: See implementation of wheel to be (Forward, Backward)
50+
--!! They can never be a non zero value at the same time !! eg. rf => (4000,2000) is illegal
51+
--rf = right front wheel, rb = right back wheel, etc.
52+
--for example direction see:
53+
begin
54+
case Direction is
55+
when Forward =>
56+
Drive_Wheels(rf => (Speed.rf, 0),
57+
rb => (Speed.rb, 0),
58+
lf => (Speed.lf, 0),
59+
lb => (Speed.lb, 0));
60+
when Left =>
61+
Drive_Wheels(rf => (Speed.rf ,0),
62+
rb => (0, Speed.rb),
63+
lf => (0, Speed.lf),
64+
lb => (Speed.lb, 0));
65+
when Forward_Left => --forward left diagonal
66+
Drive_Wheels(rf => (Speed.rf, 0),
67+
rb => (0, 0),
68+
lf => (0, 0),
69+
lb => (Speed.lb, 0));
70+
when Backward_Left => --backward left diagonal
71+
Drive_Wheels(rf => (0, Speed.rf),
72+
rb => (0, 0),
73+
lf => (0, 0),
74+
lb => (0, Speed.lb));
75+
when Turning => --Same as Forward, wheelspeed left < wheelspeed right results in curved left
76+
Drive_Wheels(rf => (Speed.rf, 0),
77+
rb => (Speed.rb, 0),
78+
lf => (Speed.lf, 0),
79+
lb => (Speed.lb ,0));
80+
when Lateral_Left =>
81+
Drive_Wheels(rf => (Speed.rf,0),
82+
lb => (0, 0),
83+
lf => (0,Speed.lf),
84+
rb => (0,0));
85+
when Rotating_Left =>
86+
Drive_Wheels(rf => (Speed.rf,0),
87+
lb => (Speed.rb, 0),
88+
lf => (0,Speed.lf),
89+
rb => (0,Speed.lb));
90+
when Stop =>
91+
Drive_Wheels(rf => (0, 0),
92+
rb => (0, 0),
93+
lf => (0, 0),
94+
lb => (0, 0));
95+
end case;
96+
end Drive;
97+
98+
procedure Drive_Wheels(rf : Wheel;
99+
rb : Wheel;
100+
lf : Wheel;
101+
lb : Wheel ) is
102+
begin
103+
MD.Set_PWM_Wheels (rf, rb, lf, lb);
104+
end Drive_Wheels;
105+
106+
procedure Servo (ServoPin : ServoPins ;
107+
Angle : Degrees)
108+
is
109+
begin
110+
MD.Set_Servo(ServoPin, Angle);
111+
end Servo;
112+
113+
begin
114+
Initialize;
115+
end MicroBit.MotorDriver;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2018-2019, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of the copyright holder nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
with DFR0548; use DFR0548;
32+
with HAL; use HAL;
33+
34+
package MicroBit.MotorDriver is
35+
36+
type Directions is (Forward,
37+
Left,
38+
Forward_Left,
39+
Turning_Left,
40+
Lateral_Left,
41+
Stop);
42+
43+
type Speeds is record
44+
rf: UInt12;
45+
rb: UInt12;
46+
lf: UInt12;
47+
lb: UInt12;
48+
end record;
49+
50+
procedure Drive (Direction : Directions;
51+
Speed : Speeds := (4095,4095,4095,4095));
52+
53+
procedure Servo (ServoPin : ServoPins ;
54+
Angle : Degrees);
55+
56+
private
57+
procedure Drive_Wheels(rf : Wheel;
58+
rb : Wheel;
59+
lf : Wheel;
60+
lb : Wheel);
61+
62+
procedure Initialize;
63+
64+
end MicroBit.MotorDriver;

boards/MicroBit_v2/src/zfp/microbit.ads

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ package MicroBit is -- Fuction nRF528
106106
MB_SDA : nRF.GPIO.GPIO_Point renames MB_P30;
107107
MB_SCL : nRF.GPIO.GPIO_Point renames MB_P31;
108108

109+
MB_SDA_EXT : nRF.GPIO.GPIO_Point renames MB_P20;
110+
MB_SCL_EXT : nRF.GPIO.GPIO_Point renames MB_P19;
111+
109112
MB_UART_RX : nRF.GPIO.GPIO_Point renames MB_P33;
110113
MB_UART_TX : nRF.GPIO.GPIO_Point renames MB_P34;
111114
end MicroBit;

0 commit comments

Comments
 (0)