stm32f0x2_hal_0.1.0_85eaea48/src/drivers/stm32-usb_device.ads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-------------------------------------------------------------------------------
--                                                                           --
--                              STM32F0 USB                                  --
--                                                                           --
--                  Copyright (C) 2022      Marc Poulhiès                    --
--                                                                           --
--    STM32F0 USB is free software: you can redistribute it and/or           --
--    modify it under the terms of the GNU General Public License as         --
--    published by the Free Software Foundation, either version 3 of the     --
--    License, or (at your option) any later version.                        --
--                                                                           --
--    STM32F0 USB is distributed in the hope that it will be useful,         --
--    but WITHOUT ANY WARRANTY; without even the implied warranty of         --
--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       --
--    General Public License for more details.                               --
--                                                                           --
--    You should have received a copy of the GNU General Public License      --
--    along with STM32F0 USB. If not, see <http://www.gnu.org/licenses/>.    --
--                                                                           --
-------------------------------------------------------------------------------
--
--  SPDX-License-Identifier: GPL-3.0-or-later

with System.Storage_Elements;
with System;
with USB;            use USB;
with USB.HAL.Device; use USB.HAL.Device;
with USB.Utils;

package STM32.USB_Device is

   Num_Endpoints : constant := 8;

   type UDC is new USB_Device_Controller with private;

   overriding procedure Initialize (This : in out UDC);

   overriding function Request_Buffer
     (This : in out UDC; Ep : EP_Addr; Len : USB.Packet_Size)
      return System.Address;

   overriding function Valid_EP_Id
     (This : in out UDC; EP : EP_Id) return Boolean is
     (Positive (EP) in 0 .. Num_Endpoints - 1);

   overriding procedure Start (This : in out UDC);

   overriding procedure Reset (This : in out UDC);

   overriding function Poll (This : in out UDC) return UDC_Event;

   overriding procedure EP_Send_Packet
     (This : in out UDC; Ep : EP_Id; Len : USB.Packet_Size);

   overriding procedure EP_Setup
     (This : in out UDC; EP : EP_Addr; Typ : EP_Type);

   overriding procedure EP_Ready_For_Data
     (This  : in out UDC; EP : EP_Id; Size : USB.Packet_Size;
      Ready :        Boolean := True);

   overriding procedure EP_Stall
     (This : in out UDC; EP : EP_Addr; Set : Boolean := True);

   overriding procedure Set_Address (This : in out UDC; Addr : UInt7);

   overriding function Early_Address (This : UDC) return Boolean is (False);

   function Send_Would_Block (This : UDC; Ep : EP_Id) return Boolean;
private
   Packet_Buffer_Base : constant System.Address :=
     System'To_Address (16#4000_6000#);
   subtype Packet_Buffer_Offset is
     System.Storage_Elements.Storage_Offset range 16#000# .. 16#3FF#;

   function Allocate_Buffer
     (This : in out UDC; Size : Natural) return Packet_Buffer_Offset;

   type Endpoint_Status is record

      -- Following pointers points in the packet memory.

      -- The memory is accessible by the CPU, but with particular constraints.
      -- Instead of enforcing constraints everywhere, we copy back and forth
      -- between the packet memory and RAM.

      Tx_Buffer_Offset : Packet_Buffer_Offset := Packet_Buffer_Offset'Last;

      Rx_Buffer_Offset : Packet_Buffer_Offset := Packet_Buffer_Offset'Last;
      Rx_Use_32b       : Boolean              := False;
      Rx_Num_Blocks    : Natural              := 0;

      -- Rx_Buffer_Address : System.Address := System.Null_Address;
      --  RX buffer in Packet memory

      --  Both folowing pointers points in regular memory: CPU can access it,
      --  no particular constraint.

      Tx_User_Buffer_Address : System.Address := System.Null_Address;
      --  Buffer where user writes data to be sent
      Tx_User_Buffer_Len : USB.Packet_Size := 0;

      Rx_User_Buffer_Address : System.Address := System.Null_Address;
      --  Buffer where user expects received data to be stored
      Rx_User_Buffer_Len : USB.Packet_Size := 0;

      Typ   : EP_Type := Bulk;
      Valid : Boolean := False;
   end record;

   type Endpoint_Status_Array is array (USB.EP_Id) of Endpoint_Status;
   type UDC is new USB_Device_Controller with record

      Alloc : Standard.USB.Utils.Basic_RAM_Allocator (64 * 4);

      --  4 x 16-bits per AP (ADDR_TX + COUNT_TX + ADDR_RX + COUNT_RX) = 64-bits
      --                                                                 (8 bytes)
      --    x 8 endpoints
      --    = 64 bytes for BTABLE
      --    + 128 bytes statically reserved for EP0 buffers. Maybe too much. 64 bytes is the minimum for USB FS control.
      Next_Buffer : Packet_Buffer_Offset :=
        System.Storage_Elements.Storage_Offset (Num_Endpoints * 8 + 128);
      EP_Status : Endpoint_Status_Array := [others => <>];
      In_Reset  : Boolean               := True;
   end record;
end STM32.USB_Device;